/*********************************************************************
 * This file is part of the PRAPI library.
 *
 * Copyright (C) 2001 Topi Mäenpää and Jaakko Viertola
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.7 $
 *********************************************************************/

#ifndef _RASTERCODEC_H
#define _RASTERCODEC_H

#include <iostream>
#include <Matrix.h>
#include <MatrixCodec.h>
#include "ColorMap.h"
#include "rasterfile.h"

namespace prapi
{
	typedef struct rasterfile RasterHeader;

	/**
	 * RasterCodec reads and writes gray-scale images or integer-valued
	 * color images with a palette (colormap) in sunras format.
	 **/
	class RasterCodec : public util::MatrixCodec<int>
	{
	public:
		RasterCodec();

		/**
		 * Set the packed flag.
		 * @param pack if true, the output produced by this codec will be
		 *        rle-encoded.
		 **/
		void setPacked(bool pack) { _bPacked = pack; }
		/**
		 * See whether this codec going to pack its output.
		 **/
		bool isPacked(void) const { return _bPacked; }

		/**
		 * Turn the colormap on or off.
		 * @param useClrMap if true, then a colormap will be written.
		 **/
		void setUseColorMap(bool useClrMap) { _bUseColorMap = useClrMap; }
		/**
		 * See whether this codec is going to write the color map.
		 **/
		bool isUsingColorMap(void) { return _bUseColorMap; }

		void decodeMatrix(std::istream& in, util::Matrix<int>& mat) throw (util::MatrixException&, util::io::IOException&);
		void encodeMatrix(std::ostream& out, const util::Matrix<int>& mat) throw (util::MatrixException&, util::io::IOException&);

		/**
		 * Set the color map. This will turn the colormap flag on.
		 * @param clrMap the colormap to be used. Each value in the image
		 *        is treated as an index to this map when reading.
		 **/
		void setColorMap(const ColorMap<int>& clrMap) { _colorMap = clrMap; _bUseColorMap = true; }
		ColorMap<int> getColorMap(void) const { return _colorMap; }
		ColorMap<int>& colorMap(void) { return _colorMap; }

	private:
		void writeClrMap(std::ostream& out);
		
		bool _bPacked, _bUseColorMap;
		ColorMap<int> _colorMap;
	};
}

#endif
