#define PIC_X 1024
#define PIC_Y 1024
#include <math.h>
#include <stdio.h>
#include <fcntl.h>
#include "rasterfile.h"

int pic_x,pic_y;
unsigned char red[PIC_X][PIC_Y];
unsigned char green[PIC_X][PIC_Y];
unsigned char blue[PIC_X][PIC_Y];
unsigned char data[PIC_X][PIC_Y][3];

/****************************************************************/
/* This function writes 3 coluur planes (2D arrays of blue,     */
/* green and red) as a 24 bit colour rasterfile                 */
/****************************************************************/
write_colour(filename,blue,green,red,pic_x,pic_y)
char filename[100];
unsigned char blue[PIC_X][PIC_Y];
unsigned char green[PIC_X][PIC_Y];
unsigned char red[PIC_X][PIC_Y];
int pic_x,pic_y;
{
int header[8],fd,i,j;

fd = creat(filename,0644);
printf("File %s created\n",filename);
/* Header information for SUN colour rasterfile */
header[0] = RAS_MAGIC;
header[1] = pic_y;
header[2] = pic_x;
header[3] = 24;
header[4] = pic_x*pic_y*3;
header[5] = RT_STANDARD;
header[6] = RMT_NONE;
header[7] = 0;
write(fd,header,32);
for(i=0;i<pic_x;i++)
for(j=0;j<pic_y;j++)
	{
	data[i][j][0] = blue[i][j];
	data[i][j][1] = green[i][j];
	data[i][j][2] = red[i][j];
	}
for(i=0;i<pic_x;i++)
     write(fd,&data[i][0][0],pic_y*3);
close(fd);
printf("File %s written\n",filename);
}

/****************************************************************/
/* This function reads a 24 bit colour rasterfile into 3 colour */
/* planes (3 2D arrays for blue, green and red).                */
/****************************************************************/
read_colour(filename,blue,green,red,pic_x,pic_y)
char filename[100];
unsigned char blue[PIC_X][PIC_Y];
unsigned char green[PIC_X][PIC_Y];
unsigned char red[PIC_X][PIC_Y];
int *pic_x,*pic_y;
{
int header[8],fd,i,j;

fd = open(filename,O_RDONLY,0644);
printf("File %s opened\n",filename);
/* Read 8 integer header information for SUN colour rasterfile */
read(fd,header,32);
*pic_y = header[1];
*pic_x = header[2];

for(i=0;i<(*pic_x);i++)
     read(fd,&data[i][0][0],(*pic_y)*3);
for(i=0;i<(*pic_x);i++)
for(j=0;j<(*pic_y);j++)
	{
	blue[i][j] = data[i][j][0];
	green[i][j] = data[i][j][1];
	red[i][j] = data[i][j][2];
	}
close(fd);
printf("File %s read\n",filename);
}

