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

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

namespace dj {

ImageOpCrop::ImageOpCrop() :
  _area(0, 0, 500, 500)
{}

const StrList ImageOpCrop::attrNameList() const { return imageOpCropAttrStr(); }

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

const StrList ImageOpCrop::attr(const Str & in) const
{
  StrList out;
  if (compareCase(attrNameList()[ATTR_AREA], in))
    out += deflateStr(_area);
  return out;
}

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

const PixelDataInfo ImageOpCrop::info(
  const PixelDataInfo & in, const Box2i & area
) {
  return PixelDataInfo(area.size(), in.pixel());
}

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

const Image ImageOpCrop::work(const Image & in, const Box2i & area)
{
  DJ_DBG_FNC("dj::ImageOpCrop::work");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("area = " << area);

  // Setup.
  //
  Image out(info(in, area));
  out.proxy(in.proxy());
  out.tagList(in.tagList());
  out.zero();
  DJ_DBG_PRN("out = " << out);
  
  // Work.
  //
  pixelCopy(in, &out, -area.position());

  return out;
}

}
