//=======================================================================================
//
// template for RGB bitmap image processing
//
// templ_ppm.c
//
// developer: Henry Guennadi Levkin
//
// How to use:
// templ_bmp file.bmp
//
// result in res.ppm
//
//=======================================================================================

#include "templ_bmp.h"

//=======================================================================================
// global variables

FILE* gFp; // logging file

int gW;      // RGB image width
int gH;      // RGB image height

// max bmp-file size 8MB
byte gpInp[8000000]; // input RGB bitmap image (from file)

byte* gpRed;
byte* gpGreen;
byte* gpBlue;

byte* gpLu;  // luma frame
byte* gpCb;  // chroma Cb frame
byte* gpCr;  // chroma Cr frame

byte* gpLuRes;  // luma frame after processing
byte* gpCbRes;  // chroma Cb frame after processing
byte* gpCrRes;  // chroma Cr frame after processing

//=======================================================================================
void WritePGM(char* file_name, byte* pImg, int nWidth, int nHeight)
{
    FILE* img_file= fopen(file_name, "wb");

    fprintf( img_file, "P5\n#\n%d %d\n255\n", nWidth, nHeight );

    fwrite( pImg, 1, nWidth * nHeight, img_file);
    fclose(img_file);
}
//=======================================================================================
void AllocateMem()
{
 gpRed   = malloc(gW*gH);
 gpGreen = malloc(gW*gH);
 gpBlue  = malloc(gW*gH);

 gpLu = malloc(gW * gH);  // luma frame
 gpCb = malloc(gW * gH);  // chroma Cb frame
 gpCr = malloc(gW * gH);  // chroma Cr frame

 gpLuRes = malloc(gW * gH);
 gpCbRes = malloc(gW * gH);
 gpCrRes = malloc(gW * gH);
}

//=======================================================================================
void FreeMem()
{
 free(gpCrRes);
 free(gpCbRes);
 free(gpLuRes);

 free(gpCr);
 free(gpCb);
 free(gpLu);

 free(gpBlue);
 free(gpGreen);
 free(gpRed);

}

//=======================================================================================
int main(int nArgs,char** ppArgs)
{
 char filename[256];

 puts("======================================================");
 puts("Still RGB bitmap image processing:");
 gFp = fopen("log.txt", "w");

 strcpy(filename, ppArgs[1]);

 // read RGB bitmap image file
 ReadBmpFile(filename, gpInp);
 BmpWidthHeight(gpInp, &gW, &gH);

 //-------------
 AllocateMem();

 //-----------------------------------------------------------
 // get R,G,B components  from  (r,g,b) image
 GetColorComponentFromBmp(gpInp, gpRed, gpGreen, gpBlue);

 //---------------------------------------------------------------------
 // 4:4:4
 TransformRGBtoLuCbCr(gpRed, gpGreen, gpBlue, gpLu, gpCb, gpCr, gW, gH);

 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 // Lu,Cb,Cr image processing and save in gpLuRes, gpCbRes, gpCrRes
 // here is just coping!!!
 {
   int i;
   for(i=0; i<gW*gH; i++)
   {
     gpLuRes[i] = gpLu[i];
     gpCbRes[i] = gpCb[i];
     gpCrRes[i] = gpCr[i];
   }
 }

 //---------------------------------------------------------------------
 // 4:4:4
 TransformLuCbCrToRGB(gpLuRes, gpCbRes, gpCrRes, gpRed, gpGreen, gpBlue, gW, gH);

 //-----------------------------------------------------------
 // use same gpRed, gpGreen, gpBlue for saving
 MakeRGBfromComponents(gpInp+14+40, gpRed, gpGreen, gpBlue, gW, gH);

 // write result ppm image file
 BitmapImageFileWrite("result.bmp", gpInp, gW, gH);

 //-------------
 FreeMem();
 
 fclose(gFp);

 return 0;
}

