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

#ifndef DJ_IMAGE_IO_JPEG_IO_H
#define DJ_IMAGE_IO_JPEG_IO_H
#include "base.h"
#include <djImageIo.h>

namespace djImageIoJpeg {

// Image load plugin.
//
class Load : public ImageLoad {
public:

  Load();
  ~Load();
  
  const Str name() const { return djImageIoJpeg::name(); }
  Plugin * copy() const { return new Load(*this); }
  const StrList extensionList() const { return djImageIoJpeg::extensionList(); }
  
  bool open(const Str & fileName, ImageIoInfo *);

protected:

  bool imageOpen(const Str & fileName, ImageIoInfo *);
  void imageClose();
  bool imageBegin(int frame, ImageIoInfo *);
  bool imageGet(PixelData *, int proxyScale);

private:

  FileInfo _fileInfo;
  FILE * _f;
  jpeg_decompress_struct _jpeg;
  bool _jpegInit;
  PIXEL_FORMAT _format;
  Error _error;
};

// Image save plugin.
//
class Save : public ImageSave {
public:

  Save();
  ~Save();
  
  const Str name() const { return djImageIoJpeg::name(); }
  Plugin * copy() const { return new Save(*this); }
  const StrList extensionList() const { return djImageIoJpeg::extensionList(); }

  enum ATTR { ATTR_QUALITY };
  const StrList attrNameList() const;
  bool attr(const Str &, const StrList &);
  const StrList attr(const Str &) const;
  
  bool open(const Str & fileName, const ImageIoInfo &);

protected:

  bool imageOpen(const Str & fileName, const Image &);
  void imageClose();
  bool imageBegin(const Image &, int frame);
  bool imageSet(const PixelData &);

private:

  PixelDataInfo _info;
  int _quality;
  
  FileInfo _fileInfo;
  FILE * _f;
  jpeg_compress_struct _jpeg;
  bool _jpegInit;
  Error _error;
};

// Labels.
//
inline const StrList saveAttrStr() { return StrList() + Str("Quality"); }

}

#endif
