#define DJ_DBG
#include <djImageOpCore.h>
#include <djImageOpColor.h>
#include <djImageOpXform.h>
#include <djImageIo.h>
#include <djImageApp.h>

using namespace dj;

class App : public ImageApp {
public:
  App(int & argc, char ** argv);
  void work(const Str & input, const Str & output);
};

App::App(int & argc, char ** argv) :
  ImageApp("imageOp", argc, argv)
{
  if (argc != 3) {
    print("Usage: imageOp (input) (output)");
    exitValue(1);
    return;
  }
  do { work(argv[1], argv[2]); } while (0);
}

void App::work(const Str & input, const Str & output)
{
  DJ_DBG_FNC("imageCopy::work");
  ImageLoad * load = 0;
  ImageSave * save = 0;
do {

  // Open input.
  //
	ImageIoInfo info;
  load = dj::imageLoad(input, &info);
  if (! load) {
    error(Str("Cannot open input \"%1\".").arg(input));
    exitValue(1);
    break;
  }
  
  // Open output.
  //
  save = dj::imageSave(output, info);
  if (! save) {
    error(Str("Cannot open input \"%1\".").arg(output));
    exitValue(1);
    return;
  }
  
  // Work.
  //
  Image image;
  for (int i = info.timeStart(); i <= info.timeEnd(); ++i) {

    load->get(&image, 0, -1, -1, i);
    DJ_DBG_PRN("image = " << image);
    
    ImageOp::InputList inputs;
    inputs += &image;
    
    ImageOpSaturation op;
    op.attr("value", deflateStr(Color(0.1)));
    image = op.work(inputs);
    
    //ImageOpScale op;
    //op.attr("size", deflateStr(Vec2i(1024, 1024)));
    //image = op.work(inputs);
    
    if (! save->set(image, i)) {
      error(Str("Cannot save output \"%1\".").arg(output));
      exitValue(1);
      break;
    }
  }
  
} while (0);
  if (load) delete load;
  if (save) delete save;
}

int main(int argc, char ** argv)
{
  DJ_DBG_LOG();
  return App(argc, argv).exitValue();
}
