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

 Supported:
 
 * Channels: L, LA, RGB, RGBA.
 * Bit depths: U8, U16.
 * Attributes: data compression.

 References:

 * Paul Haeberli, "The SGI Image File Format, Version 1.00"
------------------------------------------------------------------------------*/

#ifndef DJ_IMAGE_IO_SGI_H
#define DJ_IMAGE_IO_SGI_H
#include <djFileIo.h>
#include <djImageIo.h>

namespace dj {
namespace ImageIoSgi {

// Plugin name.
//
inline const Str name() { return "SGI"; }

// File extensions.
//
static const StrList extensionList()
  { return StrList() + Str(".sgi") + Str(".rgba") + Str(".rgb") + Str(".bw"); }

// File header.
//
class Header {
public:

  Header();
  ~Header();

  uint8_t storage, bytes;
  uint16_t dimension, width, height, channels;
  uint32_t pixelMin, pixelMax;
  uint32_t * rleOffsets, * rleSizes;
};

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

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

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

private:

  FileInfo _fileInfo;
  FileIo _f;
  Header _header;
};

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

  Save();
  
  const Str name() const { return ImageIoSgi::name(); }
  Plugin * copy() const { return new Save(*this); }
  const StrList extensionList() const { return ImageIoSgi::extensionList(); }
  
  enum COMPRESS { COMPRESS_NONE, COMPRESS_RLE };
  enum ATTR { ATTR_COMPRESS };
  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 &);
  bool imageBegin(const Image &, int frame);
  bool imageSet(const PixelData &);

private:

  PixelDataInfo _info;
  COMPRESS _compress;

  FileInfo _fileInfo;
  FileIo _f;
  Header _header;
};

// Labels.
//
inline const StrList compressStr()
  { return StrList() + Str("None") + Str("RLE"); }
inline const StrList saveAttrStr()
  { return StrList() + Str("Compress"); }

// Debugging.
//
void dbg(const Header &);

}
}

#endif
