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

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

namespace dj {

ImageOpChannel::ImageOpChannel() :
  _channel(0)
{}

const StrList ImageOpChannel::attrNameList() const
  { return imageOpChannelAttrStr(); }

bool ImageOpChannel::attr(const Str & in, const StrList & data)
{
  StrList tmp = data;
  if (compareCase(attrNameList()[ATTR_CHANNEL], in)) {
    if (! inflateStr(tmp, &_channel)) return false;
  } else
    return false;
  return true;
}

const StrList ImageOpChannel::attr(const Str & in) const
{
  StrList out;
  if (compareCase(attrNameList()[ATTR_CHANNEL], in))
    out += deflateStr(_channel);
  return out;
}

const PixelDataInfo ImageOpChannel::info(const InfoList & in) const
{
  return in.size() ? info(*in[0]) : PixelDataInfo();
}

const PixelDataInfo ImageOpChannel::info(const PixelDataInfo & in)
{
  return PixelDataInfo(in.size(), Pixel(PIXEL_L, in.type()));
}

const Image ImageOpChannel::work(const InputList & in) const
{
  return in.size() ? work(*in[0], _channel) : Image();
}

const Image ImageOpChannel::work(const Image & in, int channel)
{
  DJ_DBG_FNC("dj::ImageOpChannel::work");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("channel = " << channel);

  // Setup.
  //
  Image out(info(in));
  out.proxy(in.proxy());
  out.tagList(in.tagList());
  channel = clamp(channel, 0, pixelSize(in.format()) - 1);
  
  // Work.
  //
  pixelCopy(
    in.data() + channel * pixelSize(in.type()), out.data(),
    area(in.size()), in.pixel(), out.pixel(),
    1, false, -1
  );

  return out;
}

}
