#include <stdio.h> 
#include <rasterfile.h>
#include <fcntl.h>
#define TRUE 1
#define FALSE 0

#define NUMBER 1300*1300

unsigned char pic[NUMBER],temp[NUMBER];
struct rasterfile xxx;

main(argc,argv)
int argc;
char **argv;
{
int x,y,fd1,fd2,INVERT,i,j;
char infile[100],outfile[100];

INVERT = FALSE;
if(argc!=5 && argc!=6)
	{
	printf("Usage: %s <input binary file> <cols> <rows> <output raster file> -R\n",argv[0]);
	printf("-R inverts the file\n");
	exit(1);
	}
if(argc==6) INVERT = TRUE;
strcpy(infile,argv[1]);
sscanf(argv[2],"%d",&xxx.ras_width);
sscanf(argv[3],"%d",&xxx.ras_height);
xxx.ras_magic = RAS_MAGIC;
xxx.ras_depth = 8;
xxx.ras_length = xxx.ras_width*xxx.ras_height;
xxx.ras_type =  RT_STANDARD;
xxx.ras_maptype = RMT_NONE;
xxx.ras_maplength = 0;
if(xxx.ras_length > NUMBER)
	{
	printf("Fatal error - image size to big\n");
	printf("Increase NUMBER and re-compile\n");
	exit(1);
	}
strcpy(outfile,argv[4]);
	
fd1 = open(infile,O_RDONLY);
fd2 = creat(outfile,0755);
read(fd1,pic,(xxx.ras_length));
write(fd2,xxx,32);
if(INVERT == FALSE) write(fd2,pic,(xxx.ras_length));
else
{
for(i=0;i<xxx.ras_height;i++)
for(j=0;j<xxx.ras_width;j++)
	temp[((xxx.ras_height-1)-i)*xxx.ras_width+j] = pic[i*xxx.ras_width+j];
write(fd2,temp,(xxx.ras_length));
}
close(fd1);
close(fd2);
}


