//=====================================================================
//
// ipicture.h - general picture file operation
//
// NOTE: require ibitmap.h, ibitmapm.h, idcolor.h
// for more information, please see the readme file
//
//=====================================================================

#ifndef __I_PICTURE_H__
#define __I_PICTURE_H__

#include "ibitmap.h"
#include "ibmcols.h"

#ifdef __cplusplus
extern "C" {
#endif


//=====================================================================
//
// PICTURE DATA INTERFACE
//
// IRWCHARD - Media Data Input/Output Interface
// PIXELS   - Pixel Operation for Bitmaps
// FIXUP    - Pixel Format Fixing Up Interface
//
//=====================================================================

//---------------------------------------------------------------------
// Media Data Input/Output Interface
//---------------------------------------------------------------------
typedef struct IRWCHARD
{
	int (*getc)(struct IRWCHARD *stream);
	int (*putc)(struct IRWCHARD *stream, int c);
	long _code, _pos, _len, _ch, _bitc, _bitd, _cnt;
	void *data;
}	IRWCHARD;

int is_getc(IRWCHARD *stream);
int is_putc(IRWCHARD *stream, int c);

long is_reader(IRWCHARD *stream, void *buffer, long size);
long is_writer(IRWCHARD *stream, const void *buffer, long size);

int is_igetw(IRWCHARD *stream);
int is_iputw(IRWCHARD *stream, int w);
long is_igetl(IRWCHARD *stream);
long is_iputl(IRWCHARD *stream, long l);

int is_mgetw(IRWCHARD *stream);
int is_mputw(IRWCHARD *stream, int w);
long is_mgetl(IRWCHARD *stream);
long is_mputl(IRWCHARD *stream, long l);

long is_igetbits(IRWCHARD *stream, int count);
long is_iputbits(IRWCHARD *stream, long data, int count);
void is_iflushbits(IRWCHARD *stream, int isflushwrite);

long is_mgetbits(IRWCHARD *stream, int count);
long is_mputbits(IRWCHARD *stream, long data, int count);
void is_mflushbits(IRWCHARD *stream, int isflushwrite);

void is_seekcur(IRWCHARD *stream, long skip);


//---------------------------------------------------------------------
// stream operation
//---------------------------------------------------------------------
int is_init_mem(IRWCHARD *stream, const void *lptr, long size);
int is_open_file(IRWCHARD *stream, const char *filename, const char *rw);
int is_close_file(IRWCHARD *stream);

void _is_perrno_set(long v);
long _is_perrno_get(void);

//---------------------------------------------------------------------
// BITMAP PIXEL OPERATION
//---------------------------------------------------------------------
unsigned long _is_get3b(const void *p);
unsigned long _is_put3b(void *p, unsigned long c);

// bitmap getpixel / putpixel interface
unsigned long _is_getpx(const struct IBITMAP *bmp, int x, int y);
unsigned long _is_putpx(struct IBITMAP *bmp, int x, int y, unsigned long c);

// ibitmap_fixup - [B,G,R,A] order is used if bpp < 0
struct IBITMAP *ibitmap_fixup(const struct IBITMAP*b, int bpp, const IRGB*v);


//=====================================================================
//
// PICTURE MEDIA OPERATION
//
// BMP - Windows/OS2 IBITMAP Format, load/save supported
// TGA - Tagged Graphics (Truevision Inc) Format, load/save supported
// GIF - Graphics Interchange Format (CompuServe Inc), load supported
//
//=====================================================================


//---------------------------------------------------------------------
// Picture Loading / Saving - Original Interface
//---------------------------------------------------------------------
// load bmp picture from stream
struct IBITMAP *iload_bmp_stream(IRWCHARD *stream, IRGB *pal);

// load bmp picture from stream
struct IBITMAP *iload_tga_stream(IRWCHARD *stream, IRGB *pal);

// load single gif frame from stream
struct IBITMAP *iload_gif_stream(IRWCHARD *stream, IRGB *pal);

// save bmp picture to stream
int isave_bmp_stream(IRWCHARD *stream, struct IBITMAP *bmp, const IRGB *pal);

// save tga picture to stream
int isave_tga_stream(IRWCHARD *stream, struct IBITMAP *bmp, const IRGB *pal);


//---------------------------------------------------------------------
// Picture Loading / Saving - Standard Interface
//---------------------------------------------------------------------
// load bmp picture from file
struct IBITMAP *iload_bmp_file(const char *file, IRGB *pal, long pos);

// load tga picture from file
struct IBITMAP *iload_tga_file(const char *file, IRGB *pal, long pos);

// load gif picture from file
struct IBITMAP *iload_gif_file(const char *file, IRGB *pal, long pos);

// load tga picture from memory
struct IBITMAP *iload_bmp_mem(const void *memory, IRGB *pal);

// load tga picture from memory
struct IBITMAP *iload_tga_mem(const void *memory, IRGB *pal);

// load gif picture from memory
struct IBITMAP *iload_gif_mem(const void *memory, IRGB *pal);

// save bmp picture to file
int isave_bmp_file(const char *file, struct IBITMAP *bmp, const IRGB *pal);

// save tga picture to file
int isave_tga_file(const char *file, struct IBITMAP *bmp, const IRGB *pal);


//---------------------------------------------------------------------
// ORIGINAL OPERATION
//---------------------------------------------------------------------
struct IBITMAP *ipic_load(const char *file, long pos);

struct IBITMAP *ipic_load_bmp(const char *file, long pos);
struct IBITMAP *ipic_load_tga(const char *file, long pos);
struct IBITMAP *ipic_load_gif(const char *file, long pos);

struct IBITMAP *ipic_convert(const struct IBITMAP *src, int bpp, void *data);


//---------------------------------------------------------------------
// GIF INTERFACE (LZW PATENT HAS EXPIRED NOW)
//---------------------------------------------------------------------
struct IGIFDESC			/* gif descriptor */
{
	int width;			/* screen width */
	int height;			/* screen height */
	int bpp;			/* color depth */
	int state;			/* decode state */
	int aspect;			/* aspect */
	int error;			/* decode/encode error */
	int empty_string;	/* lzw - empty_string pointer */
	int curr_bit_size;	/* lzw - current bit size */
	int bit_overflow;	/* lzw - is table size greater than 4096 */
	int bit_pos;		/* lzw - loading bit position */
	int data_pos;		/* lzw - loading data position */
	int data_len;		/* lzw - loading data length */
	int entire;			/* lzw - loading data entire */
	int code;			/* lzw - code loaded */
	int cc;				/* lzw - table size */
	int string_length;	/* lzw - string length */
	int bit_size;		/* lzw - bit size */
	int image_x;		/* image descriptor - image x */
	int image_y;		/* image descriptor - image y */
	int image_w;		/* image descriptor - image width */
	int image_h;		/* image descriptor - image height */
	int interlace;		/* image descriptor - interlace */
	int x;				/* image descriptor - current x */
	int y;				/* image descriptor - current y */
	int customer_bmp;	/* use customer bmp */
	int transparent;	/* extension - color transparent */
	int method;			/* extension - rander method */
	long delay;			/* extension - time delay */
	long frame;			/* extension - frame count */
	IRGB *pal;			/* current palette */
	IRWCHARD *stream;	/* input / output stream */
	IBITMAP *bitmap;	/* bitmap */
	unsigned char background;		/* background color */
	unsigned char string[4096];		/* decode string */
	unsigned char newc[4096];		/* dict new char */
	struct { short base; short length; } str[4096];   /* lzw table */
};

typedef struct IGIFDESC IGIFDESC;

// load gif header, allocate bitmap if customer is false
int ipic_gif_open(IGIFDESC *gif, IRWCHARD *stream, IRGB *pal, int customer);

// close gif and free bitmap
int ipic_gif_close(IGIFDESC *gif);

// returns delay time, below zero for eof or error
int ipic_gif_read_frame(IGIFDESC *gif);



#ifdef __cplusplus
}
#endif

#endif


