//=======================================================================================
//
// template for still image processing
//
// templ_ppm.c
//
// developer: Henry Guennadi Levkin
//
// templ_ppm file.ppm
//
// result in res.ppm
//
//=======================================================================================

#include "templ_ppm.h"

//=======================================================================================
// global variables

FILE* gFp; // logging file

int gW;      // RGB image width
int gH;      // RGB image height

byte* gpInp; // input RGB 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 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);

 free(gpInp);
}

//=======================================================================================
int main(int nArgs,char** ppArgs)
{
 char filename[256];

 puts("======================================================");
 puts("Still image processing:");
 gFp = fopen("log.txt", "w");

 strcpy(filename, ppArgs[1]);

 // read ppm image file
 PpmImageFileRead(filename, &gpInp, &gW, &gH);

 //-------------
 AllocateMem();

 //-----------------------------------------------------------
 // get R,G,B components  from  (r,g,b) image
 GetComponentsFromRGB(gpInp, gpRed, gpGreen, gpBlue, gW, gH);

 //---------------------------------------------------------------------
 // 4:4:4
 TransformRGBtoLuCbCr(gpRed, gpGreen, gpBlue, gpLu, gpCb, gpCr, gW, gH);
 
 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 // Lu,Cb,Cr image processing and save in gpLuRes, gpCbRes, gpCrRes


 //---------------------------------------------------------------------
 // 4:4:4
 TransformLuCbCrToRGB(gpLuRes, gpCbRes, gpCrRes, gpRed, gpGreen, gpBlue, gW, gH);

 //-----------------------------------------------------------
 // use same gpRed, gpGreen, gpBlue for saving
 MakeRGBfromComponents(gpInp, gpRed, gpGreen, gpBlue, gW, gH);

 // write result ppm image file
 PpmImageFileWrite("res.ppm", gpInp, gW, gH);

 //-------------
 FreeMem();
 
 fclose(gFp);

 return 0;
}

