/*------------------------------------------------------------------------------
 src/bin/djvlut/widget.cxx

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

//#define DJ_DBG
#include "widget.h"
#include <djImageOpGen.h>
#include <djImageOpXform.h>
#include <FL/Fl_Image.H>

namespace djvlut {

VlutWidget::VlutWidget() :
  _image(0)
{}

void VlutWidget::set(const Image * in)
{
  _image = in;
  redraw(); 
}

const Vec2i VlutWidget::sizeHint() const
{
  return Vec2i(0, layoutThumbnailSize());
}

void VlutWidget::draw()
{
  DJ_DBG_FNC("VlutWidget::draw");
  
  Image image = ImageOpGradient::work(
    Vec2i(w(), 1), Pixel(PIXEL_RGB, PIXEL_U8),
    Vec2i(0, 0), Vec2i(w() - 1, 0),
    Color(0.0), Color(1.0)
  );
  DJ_DBG_PRN("image = " << image);
  
  if (_image) {
    
    Image tmp = ImageOpScale::work(
      *_image, Vec2i(pixelU8Range, 1),
      ImageOpScale::AUTO, ImageOpScale::AUTO,
      Image::EDGE_CLIP, Image::EDGE_CLIP
    );
  
    Image lut(tmp.size(), Pixel(PIXEL_RGB, PIXEL_U8));
    pixelPlanar(
      tmp.data(), lut.data(), lut.size(), 
      PIXEL_RGB, tmp.type(), lut.type()
    );
    DJ_DBG_PRN("lut = " << lut);
    
    pixelLut(
      image.data(), image.data(), image.width(),
      lut.data(), lut.width(),
      PIXEL_RGB, image.type(), image.type()
    );
  }
  
  Fl_RGB_Image imageFl(image.data(), image.width(), image.height());
  for (int i = 0; i < h(); ++i)
    imageFl.draw(x(), y() + i);
}

}
