/* 

makeRAS file.lcc frame_number

This program converts a frame taken from a m2vts .lcc sequence (286x350/4:2:2) 
into a .ras colour image (temp.ras)

							Stephane 13/12/95

Debug LCC to RGB conversion - George 02/01/96  

*/


#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

#define HEIGHT 286
#define WIDTH  350

int y_cb_cr_offset[3] = { 16, 128, 128 };

static float ycc2rgb219[3][3]={
 { 1.0 ,  0.   ,  1.371 },
 { 1.0 , -0.336, -0.698 },
 { 1.0 ,  1.732,  0. }};


unsigned char limitx(double x) {
	int y = (int) ((x<0) ? (x-0.5) : (x+0.5));
	if(y>255) return 255;
	if(y<0) return 0;
	return y;}

static void y_cb_cr2rgb(int y, int cb, int cr,unsigned char rgb[3])
{
   int i;
   y -= y_cb_cr_offset[0];
   cb-= y_cb_cr_offset[1];
   cr-= y_cb_cr_offset[2]; 
 
   for(i=0;i<3;i++)
    rgb[i] = limitx(ycc2rgb219[i][0]*y + ycc2rgb219[i][1]*cb + ycc2rgb219[i][2]*cr);
}

main(int argc, char** argv) {

// Input parameters
// ****************

char* usage="Usage:  %s file.lcc frame_no\n";

if (argc!=3) {
   printf(usage,argv[0]);
   exit(1);
   }

int number=atoi(argv[2]);

// Files
// *****

FILE* fin=fopen(argv[1],"r");
if (!fin) {
   printf("Can't open file %s\n",argv[1]);
   exit(1);
   }

FILE* fout=fopen("temp.ras","w");
if (!fout) {
   printf("Can't open output file (temp.ras)");
   exit(1);
   }


// Browsing the input file
// ***********************

long int max=HEIGHT*WIDTH*(number-1)*2;
if (fseek(fin,max,0)) printf("fseek error");

// Image storage
// *************

unsigned char image_y[HEIGHT][WIDTH];
unsigned char image_cb422[HEIGHT][WIDTH/2];
unsigned char image_cr422[HEIGHT][WIDTH/2];
unsigned char image_cb444[HEIGHT][WIDTH];
unsigned char image_cr444[HEIGHT][WIDTH];

// Reads a 422 Image
// *****************

int r;

for (r=0; r<HEIGHT; r++)
      fread(&(image_y[r][0]),sizeof(unsigned char),WIDTH,fin);
for (r=0; r<HEIGHT; r++) 
      fread(&(image_cb422[r][0]),sizeof(unsigned char),WIDTH/2,fin);
for (r=0; r<HEIGHT; r++) 
      fread(&(image_cr422[r][0]),sizeof(unsigned char),WIDTH/2,fin);

// 422 to 444 Conversion
// *********************

for(int j=0; j<HEIGHT; j++){
	for(int i=0; i<WIDTH/2-1; i++){
		image_cb444[j][i*2]=image_cb422[j][i];
		image_cb444[j][i*2+1]=(image_cb422[j][i]+image_cb422[j][i+1])/2;
		image_cr444[j][i*2]=image_cr422[j][i];
		image_cr444[j][i*2+1]=(image_cr422[j][i]+image_cr422[j][i+1])/2;
		}
	image_cb444[j][WIDTH-2]=image_cb422[j][WIDTH/2-1];
	image_cb444[j][WIDTH-1]=image_cb422[j][WIDTH/2-1];
	image_cr444[j][WIDTH-2]=image_cr422[j][WIDTH/2-1];
	image_cr444[j][WIDTH-1]=image_cr422[j][WIDTH/2-1];
	}

// Converting 444 into RGB and saving into colour ras
// **************************************************

unsigned long sun_header[8];
int x,y;
unsigned char rgb[3];

sun_header[0]=0x59a66a95;
sun_header[1]=WIDTH;
sun_header[2]=HEIGHT;
sun_header[3]=24;
sun_header[4]=3*WIDTH*HEIGHT;
sun_header[5]=1;
sun_header[6]=1;
sun_header[7]=0;

fwrite((char *) &sun_header,8,sizeof(long),fout);

for(y=0; y<HEIGHT; y++)
for(x=0; x<WIDTH; x++){

	y_cb_cr2rgb(image_y[y][x],image_cb444[y][x],image_cr444[y][x],rgb);	

	fwrite(&(rgb[2]),1,sizeof(unsigned char),fout);
	fwrite(&(rgb[1]),1,sizeof(unsigned char),fout);
	fwrite(&(rgb[0]),1,sizeof(unsigned char),fout);

	}
		
fclose(fin);
fclose(fout);		

}

