/*------------------------------------------------------------------------------
 src/lib/djImage/djImageIoTga.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
 
 Supported:
 
 * TARGA v1.0.
 * Channels: L, LA, RGB, RGBA.
 * Bit depths: U8.
 * Attributes: data compression.
 
 References:

 * James D. Murray, William vanRyper, "Encyclopedia of Graphics File Formats,
   Second Edition"
------------------------------------------------------------------------------*/

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

namespace dj {
namespace ImageIoTga {

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

// File extensions.
//
static const StrList extensionList() { return StrList() + Str(".tga"); }

// File header.
//
struct Header {

  uint8_t idSize, cmapType, imageType;
  uint16_t cmapStart, cmapSize;
  uint8_t cmapDepth;
  uint16_t x, y, width, height;
  uint8_t pixelDepth, descriptor;
};

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

  const Str name() const { return ImageIoTga::name(); }
  Plugin * copy() const { return new Load(*this); }
  const StrList extensionList() const { return ImageIoTga::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;
  PIXEL_FORMAT _format;
};

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

  Save();
  
  const Str name() const { return ImageIoTga::name(); }
  Plugin * copy() const { return new Save(*this); }
  const StrList extensionList() const { return ImageIoTga::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
