/*------------------------------------------------------------------------------
 src/lib/djImage/pixelGradient.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djPixel.h"

namespace dj {

#define _PIXEL_GRADIENT(TYPE, MAX) \
  TYPE * p = reinterpret_cast<TYPE *>(in); \
  for (int i = 0; i < size; ++i, p += channels) { \
    p[0] = TYPE(MAX * i / size1); \
    DJ_DBG_PRN(i << " = " << p[0]); \
    for (int c = 1; c < channels; ++c) \
      p[c] = p[0]; \
  }

void pixelGradient(void * in, int size, const Pixel & pixel)
{
  DJ_DBG_FNC("dj::pixelGradient");
  DJ_DBG_PRN("size = " << size);
  DJ_DBG_PRN("pixel = " << pixel);
  
  const double size1 = size - 1;
  const int channels = pixelSize(pixel.format());
  switch (pixel.type()) {
    case PIXEL_U8: { _PIXEL_GRADIENT(PixelU8, pixelU8Max) } break;
    case PIXEL_U16: { _PIXEL_GRADIENT(PixelU16, pixelU16Max) } break;
    case PIXEL_F32: { _PIXEL_GRADIENT(PixelF32, 1.0) } break;
  }
}

}
