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

//#define DJ_DBG
#include "djImageOpGen.h"
#include <djImageIo.h>

namespace dj {

ImageOpInput::ImageOpInput() :
  _proxy(0), _format(-1), _type(-1), _frame(-1),
  _load(0)
{}

ImageOpInput::~ImageOpInput()
{
  if (_load) delete _load;
}

const StrList ImageOpInput::attrNameList() const
  { return imageOpInputAttrStr(); }

bool ImageOpInput::attr(const Str & in, const StrList & data)
{
  StrList tmp = data;
  if (compareCase(attrNameList()[ATTR_FILE_NAME], in)) {
    if (! inflateStr(tmp, &_fileName)) return false;
    if (_load) {
      delete _load;
      _load = 0;
    }
  } else if (compareCase(attrNameList()[ATTR_PROXY], in)) {
    if (! inflateStr(tmp, &_proxy)) return false;
  } else if (compareCase(attrNameList()[ATTR_FORMAT], in)) {
    if (! inflateStr(tmp, &_format)) return false;
  } else if (compareCase(attrNameList()[ATTR_TYPE], in)) {
    if (! inflateStr(tmp, &_type)) return false;
  } else if (compareCase(attrNameList()[ATTR_FRAME], in)) {
    if (! inflateStr(tmp, &_frame)) return false;
  } else 
    return false;
  return true;
}

const StrList ImageOpInput::attr(const Str & in) const
{
  StrList out;
  if (compareCase(attrNameList()[ATTR_FILE_NAME], in))
    out += deflateStr(_fileName);
  else if (compareCase(attrNameList()[ATTR_PROXY], in))
    out += deflateStr(_proxy);
  else if (compareCase(attrNameList()[ATTR_FORMAT], in))
    out += deflateStr(_format);
  else if (compareCase(attrNameList()[ATTR_TYPE], in))
    out += deflateStr(_type);
  else if (compareCase(attrNameList()[ATTR_FRAME], in))
    out += deflateStr(_frame);
  return out;
}

const PixelDataInfo ImageOpInput::info(const InfoList &) const
{
  if (! _load) {
    ImageOpInput * that = const_cast<ImageOpInput *>(this);
    ImageIoInfo info;
    that->_load = imageLoad(_fileName, &info);
    that->_info.size(info.size() / imageProxyScale(_proxy));
    that->_info.pixel(info.pixel());
  }
  
  return _info;
}

const Image ImageOpInput::work(const InputList &) const
{
  DJ_DBG_FNC("dj::ImageOpInput::work");
  DJ_DBG_PRN("file name = " << _fileName);
  DJ_DBG_PRN("proxy = " << _proxy);
  DJ_DBG_PRN("format = " << _format);
  DJ_DBG_PRN("type = " << _type);
  DJ_DBG_PRN("frame = " << _frame);
  
  if (! _load) {
    ImageOpInput * that = const_cast<ImageOpInput *>(this);
    that->_load = imageLoad(_fileName);
  }

  Image out;
  if (_load) _load->get(&out, _proxy, _format, _type, _frame);
  return out;
}

const Image ImageOpInput::work(
  const Str & fileName, int proxy, int format, int type, int frame
) {
  DJ_DBG_FNC("dj::ImageOpSolid::work");
  DJ_DBG_PRN("file name = " << fileName);
  DJ_DBG_PRN("proxy = " << proxy);
  DJ_DBG_PRN("format = " << format);
  DJ_DBG_PRN("type = " << type);
  DJ_DBG_PRN("frame = " << frame);
  
  ImageLoad * load = imageLoad(fileName);
  if (! load) return Image();
  
  Image out;
  load->get(&out, proxy, format, type, frame);
  delete load;
  
  return out;
}

}
