#ifndef _TGA_PRVT_H
#define _TGA_PRVT_H


#include <stdio.h>
#include <math.h>
#include <errno.h>

#include "fzrt_api.h"
#include "fzpl_plugin.h"
#include "fz_api.h"
#include "fz_math_api.h"
#include "fz_ffmt_api.h"
#include "fz_proj_api.h"
#include "fz_type_api.h"
#include "fz_iost_api.h"
#include "fz_fuim_api.h"
#include "fz_rndr_api.h"

#include "tga_pblc.h"


/************************************************************************/
/*				Translator defines										*/
/************************************************************************/

#define 	TGA_ICON				143

/* Resource file string list ID's */
#define		TGA_STRINGS				1
#define		TGA_MESSAGES			271

/* Resource file string ID's */
#define		WRITING_TGA_FILE		2
#define		TGA_COMPRESS_STR		3
#define		TGA_WRITE_ERR_STR		4
#define		TGA_READ_ERR_STR		5
#define		TGA_NAME_STR			6

#define		TGA_WRITE_ERR_ID		1
#define		TGA_READ_ERR_ID			2

/* Defines for custom export options */
#define		TGA_COMPRESS_BIT		1
#define		TGA_COMPRESS_FLAG		(1 << TGA_COMPRESS_BIT)



#define	TGA_MAX_READ_BUFFER			1048576		/* 1 MEG MAX READ BUFFER */


/* Defines for the version field */
#define TGA_VERSION_OLD				0
#define TGA_VERSION_20				1

/* Defines for the color map field */
#define	TGA_NO_COLOR_MAP  			0
#define	TGA_HAS_COLOR_MAP 			1

/* Defines for the image type field */
#define	TGA_IMAGE_TYPE_NONE			0
#define	TGA_IMAGE_TYPE_COLMAP		1
#define	TGA_IMAGE_TYPE_TRUECOL		2
#define	TGA_IMAGE_TYPE_BW			3
#define	TGA_IMAGE_TYPE_COLMAP_RLE	9
#define	TGA_IMAGE_TYPE_TRUECOL_RLE	10
#define	TGA_IMAGE_TYPE_BW_RLE		11

/* Targa error codes */
#define TGA_FORMAT_ERR	 			2		/* INVALID TARGA FORMAT			*/
#define TGA_NO_IMAGE	 			3		/* NO IMAGE STORED IN FILE		*/
#define	TGA_UNSUPPORTED_IMG_DEPTH	4		/* UNSUPPORTED IMAGE DEPTH		*/
#define	TGA_UNSUPPORTED_MAP_DEPTH	5		/* UNSUPPORTED COLOR MAP DEPTH	*/
#define TGA_MEM_ERR				    6		/* MEMORY ERROR					*/
#define TGA_ZERO_SIZE_ERR			7		/* WIDTH OR HEIGHT IS 0			*/
#define TGA_NO_EXPT_OPTS_ERR		8		/* COULD NOT GET EXPORT OPTIONS */
#define TGA_IMAGE_TOO_BIG_ERR		9		/* IMAGE IS TOO BIG TO EXPORT   */

#define TGA_END_OF_BUFFER			-1		/* UNEXPECTED END OF BUFFER  */


/* Byte swapping macros */
#if ( FZ_NATIVE_ENDIAN	== FZ_ENDIAN_SMALL )
	#define TGA_OUT_SWAP_LONG(a)
	#define TGA_OUT_SWAP_SHORT(a)
	#define TGA_IN_SWAP_LONG(a)
	#define TGA_IN_SWAP_SHORT(a)
#else
	#define TGA_OUT_SWAP_LONG(a)  _fzrt_mem_funcs.fzrt_endian_swap_4_bytes(&(a))
	#define TGA_OUT_SWAP_SHORT(a) _fzrt_mem_funcs.fzrt_endian_swap_2_bytes(&(a))
	#define TGA_IN_SWAP_LONG(a)   _fzrt_mem_funcs.fzrt_endian_swap_4_bytes(&(a))
	#define TGA_IN_SWAP_SHORT(a)  _fzrt_mem_funcs.fzrt_endian_swap_2_bytes(&(a))
#endif


/************************************************************************/
/*				Translator data types									*/
/************************************************************************/

/* Fixed point value */
typedef fzrt_int	tga_fixed_td;

/* Import working data structure */
typedef struct	tga_in_prvt_td
{
	/* Header parameters */
	char			id_length;					/* LENGTH OF IMAGE ID FIELD					*/
	char			col_map_type;				/* COLOR MAP TYPE						 	*/
	char			img_type;					/* IMAGE TYPE							 	*/
	
	unsigned short	col_map_eindex;				/* INDEX OF FIRST ENTRY USED IN COLOR MAP	*/
	unsigned short	col_map_length;				/* LENGTH OF COLOR MAP						*/
	char			col_map_esize;				/* SIZE OF ENTRY IN COLOR MAP (# OF BITS)	*/
	fzrt_int			col_map_size;				/* TOTAL SIZE IN BYTES OF COLOR MAP 		*/
	char			map_bytes_per_entry;		/* # OF BYTES PER ENTRY IN COLOR MAP		*/
	
	unsigned short	img_width;					/* PIXEL WIDTH  OF IMAGE 					*/
	unsigned short	img_height;					/* PIXEL HEIGHT OF IMAGE 					*/
	char			img_pixel_depth;			/* # OF BITS PER PIXEL						*/
	char			img_desc;					/* IMAGE DESCRIPTION						*/
	char			img_bytes_per_pixel;		/* # OF BYTES PER PIXEL						*/
	char			attr_bits;					/* # OF ATTRIBUTE BITS PER PIXEL			*/
	char			top_2_bottom;				/* IS IMAGE STORED TOP TO BOTTOM ?			*/
	char			left_2_right;				/* IS IMAGE STORED LEFT TO RIGHT	*/
	
	/* Color map */
	unsigned char	*col_map;					/* COLOR MAP								*/
	
	/* Footer parameters */
	fzrt_int			version;					/* FILE VERSION. 0 = OLD, 1 = SPECIFICATION 2.0			*/
	fzrt_int			ext_offset;					/* EXTENSION AREA OFFSET FROM START OF FILE				*/
	fzrt_int			dev_offset;					/* DEVELOPER DIRECTORY OFFSET FROM START OF FILE		*/

	/* File parameters */
	fzrt_int			buff_end_pos;				/* MAX EXTENT OF IMAGE DATA IN FILE 		*/
	
	float			gamma;						/* gamma encoding of pixels */	/* MAH 07/10/98 */

}	tga_in_prvt_td;

/* Export working data structure */
typedef struct tga_out_prvt_td
{
	/* File variables */
	short			bytes_per_pixel;			/* BYTES PER PIXEL */
	fzrt_int			scan_size;					/* # OF BYTES IN SCANLINE */
	short			compressed;					/* USE RLE ? */
	
	unsigned char	*rle_buffer;				/* RLE BUFFER */
	
	/* Image variables */
	char			shift;
	char			im_init;
	unsigned short	mask;
	short			ncolms;
	short			nrows;
	short			nbits;
	short			pixsize;
	fzrt_rgb_color_td		back_color;
	unsigned char	*scanline;
	FILE*			fp;
	
	float			gamma;

} tga_out_prvt_td;





/************************************************************************/
/*				Plugin and function set stuff							*/
/************************************************************************/

	/* application provided function sets to hold on to */
extern const fzpl_fset_glue_fset	*_fset_glue;
extern fz_ffmt_fset					_ffmt_funcs;
extern fz_ffmt_image_fset			_ffmt_2d_funcs;
extern fz_proj_fset					_proj_funcs;
extern fz_math_fset					_math_funcs;
extern fz_type_fset					_type_funcs;
extern fz_iost_fset					_iost_funcs;
extern fz_fuim_fset					_fuim_funcs;
extern fz_rndr_mngr_fset			_rndr_funcs;
extern fz_fzrt_base_fset			_fzrt_base_funcs;
extern fz_fzrt_mem_fset				_fzrt_mem_funcs;
extern fz_fzrt_file_fset			_fzrt_file_funcs;
extern fz_fzrt_graf_fset			_fzrt_graf_funcs;
extern fzpl_plugin_query_fset		_plugin_query_funcs;

	/* plugin's runtime id */
extern fzrt_unsigned_int					_tga_plugin_runtime_id;

	/* translator's format reference id */
extern fz_ffmt_ref_td					_tga_ref;

	/* translator's resource file id */
extern fzrt_fzr_ref_td					_fz_rsrc_ref;
	
	/* export custom options */
extern fzrt_int								_write_flags;





/************************************************************************/
/*							FUNCTION PROTOTYPES							*/
/************************************************************************/

/* Function prototypes for reading */
extern fzrt_error_td tga_read_info(fzrt_int windex, fz_ffmt_ref_td ffmt_id,  fzrt_floc_ptr floc,
				fzrt_int *lwidth, fzrt_int *lheight, short *pixel_depth, fzrt_boolean *has_alpha, double *dpi);
extern fzrt_error_td tga_read_image(fzrt_int windex, fz_ffmt_ref_td ffmt_id,  fzrt_floc_ptr floc, fz_ffmt_image_channel_enum which_channel, fzrt_pbuf_ptr *pbuf);

/* Function prototypes for translator options */
extern fzrt_error_td tga_opts_io(fz_iost_ptr iost, fz_iost_dir_td_enum dir, fzpl_vers_td * const version, fzrt_unsigned_int size);
extern fzrt_error_td tga_write_opts_default(fz_ffmt_ref_td ffmt_id);
extern fzrt_error_td tga_dlog_cust_func(fz_fuim_tmpl_ptr fuim_tmpl, short parent, fz_ffmt_ref_td ref_id);
extern fzrt_error_td tga_read_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags);
extern fzrt_error_td tga_write_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags);

/* Function prototypes for writing */
extern fzrt_error_td tga_write_err_lable(fzrt_int windex, fz_ffmt_ref_td ffmt_id, char *str, fzrt_int max_len, short *err_id);
extern fzrt_error_td tga_write_progress_str(fzrt_int windex, fz_ffmt_ref_td ffmt_id, char *str, fzrt_int max_len);
extern fzrt_error_td tga_export_init(fzrt_int windex, fz_ffmt_ref_td ffmt_id, void **inst,  fzrt_floc_ptr floc);
extern fzrt_error_td tga_export_finit(fzrt_int windex, fz_ffmt_ref_td ffmt_id, void **inst,  fzrt_floc_ptr floc, fzrt_error_td err);
extern fzrt_error_td tga_out_init_image(fzrt_int windex, fz_ffmt_ref_td ffmt_id, void *inst, fzrt_int width, fzrt_int height, fzrt_boolean has_alpha);
extern fzrt_error_td tga_out_buffer_2_file_image(fzrt_int windex, fz_ffmt_ref_td ffmt_id, void *inst, fz_rgb_uchar_td *pixels, unsigned char *alpha, fzrt_int row);
extern fzrt_error_td tga_out_finit_image(fzrt_int windex, fz_ffmt_ref_td ffmt_id, void *inst);

/* Function prototypes for translator info */
extern fzrt_error_td tga_get_name(char *fmt_name, fzrt_int fmt_name_max_len);
extern fzrt_error_td tga_get_id(fzrt_UUID_td fmt_id);
extern fzrt_error_td tga_get_info(
			char			*sortby,
			fzrt_int			sortby_max_len,
			char			*file_ext,
			fzrt_int			file_ext_max_len,
			char			*keywd,
			fzrt_int			keywd_max_len,
			fzrt_int			*attr_flags
			);
extern fzrt_error_td tga_get_ftype(
			fzrt_ostype		*type,
			fzrt_ostype		*creator
			);
extern fzrt_error_td tga_get_icon(
			fzrt_icon_ptr 	*icon
			);
extern fzrt_boolean tga_error_str_func(fzrt_int err, char *str, short str_len);


#endif /* _TGA_PRVT_H */