#include "imageUtils.h"
#include <stdio.h>

#if 0
unsigned char* transformToLuminance(int width, int height, int type, unsigned char* imgSrc, unsigned char* imgDest)
{
  if ((width <= 0) || (height <= 0))
    return NULL;

  unsigned char* result;
  if (!imgDest)
  {
    result = new unsigned char[3*width*height];
    if (!result)
      return NULL;
  }
  else
    result = imgDest;

  if (type == IMG_BGR)
  {
    for (int i = 0; i < 3*width*height; i=i+3)
    {
      result[i] = (unsigned char)(0.11f*(float)imgSrc[i] + 0.59f*(float)imgSrc[i+1] + 0.3f*(float)imgSrc[i+2]);
      result[i+1] = result[i];
      result[i+2] = result[i];
    }
  }
  else if (type == IMG_RGB)
  {
    for (int i = 0; i < 3*width*height; i=i+3)
    {
      result[i] = (unsigned char)(0.3f*(float)imgSrc[i] + 0.59f*(float)imgSrc[i+1] + 0.11f*(float)imgSrc[i+2]);
      result[i+1] = result[i];
      result[i+2] = result[i];
    }
  }
  else
  {
    if (!imgDest)
      delete result;
	return NULL;
  }

  return result;
}

#else

unsigned char* transformToLuminance(int width, int height, int type, unsigned char* imgSrc, unsigned char* imgDest)
{
  if ((width <= 0) || (height <= 0))
    return NULL;

  unsigned char* result;
  if (!imgDest)
  {
    result = new unsigned char[3*width*height];
    if (!result)
      return NULL;
  }
  else
    result = imgDest;

  if (type == IMG_BGR)
  {
    unsigned char* p = result;
	unsigned char* p2 = imgSrc;
    for (int i = 0; i < width*height; ++i)
    {
	  int red = *p2++;
	  int green = *p2++;
	  int blue = *p2++;
      int luminance = (unsigned char)(0.11f*(float)red + 0.59f*(float)green + 0.3f*(float)blue);
	  *p++ = luminance;
	  *p++ = luminance;
	  *p++ = luminance;
    }
  }
  else if (type == IMG_RGB)
  {
    unsigned char* p = result;
	unsigned char* p2 = imgSrc;
    for (int i = 0; i < width*height; ++i)
    {
	  int red = *p2++;
	  int green = *p2++;
	  int blue = *p2++;
      int luminance = (unsigned char)(0.3f*(float)red + 0.59f*(float)green + 0.11f*(float)blue);
	  *p++ = luminance;
	  *p++ = luminance;
	  *p++ = luminance;
    }
  }
  else
  {
    if (!imgDest)
      delete result;
	return NULL;
  }

  return result;
}

#endif

void packRGB(int width, int height, unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* rgbData)
{
  if (width <= 0 || height <= 0 || !rgbData || !r || !g || !b)
    return;
  for (int j = 0; j < height; j++)
  {
    int idx_j = j*width;
    for (int i = 0; i < width; i++)
	{
	  int idx_i = idx_j + i;
	  int three_idx_i = (idx_i << 1) + idx_i; // 3*idx_i

	  rgbData[three_idx_i]     = r[idx_i];
	  rgbData[three_idx_i + 1] = g[idx_i];
	  rgbData[three_idx_i + 2] = b[idx_i];
	}
  }
}

void packBGR(int width, int height, unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* rgbData)
{
  if (width <= 0 || height <= 0 || !rgbData || !r || !g || !b)
    return;
  for (int j = 0; j < height; j++)
  {
    int idx_j = j*width;
    for (int i = 0; i < width; i++)
	{
	  int idx_i = idx_j + i;
	  int three_idx_i = (idx_i << 1) + idx_i; // 3*idx_i

	  rgbData[three_idx_i]     = b[idx_i];
	  rgbData[three_idx_i + 1] = g[idx_i];
	  rgbData[three_idx_i + 2] = r[idx_i];
	}
  }
}

void unpackRGB(int width, int height, unsigned char* rgbData, unsigned char* r, unsigned char* g, unsigned char* b)
{
  if (width <= 0 || height <= 0 || !rgbData || !r || !g || !b)
    return;
  for (int j = 0; j < height; j++)
  {
    int idx_j = j*width;
    for (int i = 0; i < width; i++)
	{
	  int idx_i = idx_j + i;
	  int three_idx_i = (idx_i << 1) + idx_i; // 3*idx_i

	  r[idx_i] = rgbData[three_idx_i];
	  g[idx_i] = rgbData[three_idx_i + 1];
	  b[idx_i] = rgbData[three_idx_i + 2];
	}
  }
}

void unpackBGR(int width, int height, unsigned char* rgbData, unsigned char* r, unsigned char* g, unsigned char* b)
{
  if (width <= 0 || height <= 0 || !rgbData || !r || !g || !b)
    return;
  for (int j = 0; j < height; j++)
  {
    int idx_j = j*width;
    for (int i = 0; i < width; i++)
	{
	  int idx_i = idx_j + i;
	  int three_idx_i = (idx_i << 1) + idx_i; // 3*idx_i

	  b[idx_i] = rgbData[three_idx_i];
	  g[idx_i] = rgbData[three_idx_i + 1];
	  r[idx_i] = rgbData[three_idx_i + 2];
	}
  }
}
