Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "../compatibility/tnz4.h"
Toshihiro Shimizu 890ddd
#include "../compatibility/inforegion.h"
Toshihiro Shimizu 890ddd
#include "filebmp.h"
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
typedef unsigned long  ULONG;
Toshihiro Shimizu 890ddd
typedef unsigned short USHORT;
Toshihiro Shimizu 890ddd
typedef unsigned char  UCHAR;
Toshihiro Shimizu 890ddd
typedef unsigned int   UINT;
Toshihiro Shimizu 890ddd
typedef void IMAGERGB;
Toshihiro Shimizu 890ddd
typedef struct {USHORT m,b,g,r;} SPIXEL;
Toshihiro Shimizu 890ddd
#define CASE break; case
Toshihiro Shimizu 890ddd
#define __OR      ; case
Toshihiro Shimizu 890ddd
#define DEFAULT break; default
Toshihiro Shimizu 890ddd
#define TRUE 1
Toshihiro Shimizu 890ddd
#define FALSE 0
Toshihiro Shimizu 890ddd
#define NIL (void*)0
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef enum {
Toshihiro Shimizu 890ddd
	BMP_NONE,
Toshihiro Shimizu 890ddd
	BMP_BW,
Toshihiro Shimizu 890ddd
	BMP_GREY16,
Toshihiro Shimizu 890ddd
	BMP_GREY16C,
Toshihiro Shimizu 890ddd
	BMP_GREY256,
Toshihiro Shimizu 890ddd
	BMP_GREY256C,
Toshihiro Shimizu 890ddd
	BMP_CMAPPED16,
Toshihiro Shimizu 890ddd
	BMP_CMAPPED16C,
Toshihiro Shimizu 890ddd
	BMP_CMAPPED256,
Toshihiro Shimizu 890ddd
	BMP_CMAPPED256C,
Toshihiro Shimizu 890ddd
	BMP_RGB
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} BMP_SUBTYPE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
typedef struct bmpimage {
Toshihiro Shimizu 890ddd
	int xsize,ysize;
Toshihiro Shimizu 890ddd
	LPIXEL *buffer;
Toshihiro Shimizu 890ddd
  BMP_SUBTYPE type;
Toshihiro Shimizu 890ddd
	} IMAGE;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define UNUSED_REDUCE_COLORS
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*-- Defines ----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_BI_RGB 0
Toshihiro Shimizu 890ddd
#define BMP_BI_RLE8 1
Toshihiro Shimizu 890ddd
#define BMP_BI_RLE4 2
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_WIN_OS2_OLD 12
Toshihiro Shimizu 890ddd
#define BMP_WIN_NEW 40
Toshihiro Shimizu 890ddd
#define BMP_OS2_NEW 64
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_READ_INFO 1
Toshihiro Shimizu 890ddd
#define BMP_READ_IMAGE 2
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_FERROR(fp) (ferror(fp) || feof(fp))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_RMAP(x) (x & 0xe0) | ((x & 0xe0) >> 3) | ((x & 0xc0) >> 6)
Toshihiro Shimizu 890ddd
#define BMP_GMAP(x) ((x & 0x1c) << 3) | (x & 0x1c) | ((x & 0x18) >> 3)
Toshihiro Shimizu 890ddd
#define BMP_BMAP(x) ((x & 0x03) << 6) | ((x & 0x03) << 4) | ((x & 0x03) << 2) | (x & 0x03)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_RMAP16(x) ((x & 0xc0) << 0) | ((x & 0xc0) >> 2) | ((x & 0xc0) >> 4) | ((x & 0xc0) >> 6)
Toshihiro Shimizu 890ddd
#define BMP_GMAP16(x) ((x & 0x60) << 1) | ((x & 0x60) >> 1) | ((x & 0x60) >> 3) | ((x & 0x60) >> 5)
Toshihiro Shimizu 890ddd
#define BMP_BMAP16(x) ((x & 0x30) << 2) | ((x & 0x30) << 0) | ((x & 0x30) >> 2) | ((x & 0x30) >> 4)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_CUT(a, b, c) \
Toshihiro Shimizu 890ddd
	{                    \
Toshihiro Shimizu 890ddd
		if (a < b)       \
Toshihiro Shimizu 890ddd
			a = b;       \
Toshihiro Shimizu 890ddd
		else if (a > c)  \
Toshihiro Shimizu 890ddd
			a = c;       \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_REDUCE_COLORS(r, g, b) ((r & 0xe0) | ((g & 0xe0) >> 3) | ((b & 0xc0) >> 6))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_ADD_ERROR(pix, weight)            \
Toshihiro Shimizu 890ddd
	{                                         \
Toshihiro Shimizu 890ddd
		tmp = (pix).r + ((r1 * weight) >> 4); \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);                 \
Toshihiro Shimizu 890ddd
		(pix).r = tmp;                        \
Toshihiro Shimizu 890ddd
		tmp = (pix).g + ((g1 * weight) >> 4); \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);                 \
Toshihiro Shimizu 890ddd
		(pix).g = tmp;                        \
Toshihiro Shimizu 890ddd
		tmp = (pix).b + ((b1 * weight) >> 4); \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);                 \
Toshihiro Shimizu 890ddd
		(pix).b = tmp;                        \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_ADD_ERROR_BW(pix, error) \
Toshihiro Shimizu 890ddd
	{                                \
Toshihiro Shimizu 890ddd
		tmp = (pix).r + (error);     \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);        \
Toshihiro Shimizu 890ddd
		(pix).r = tmp;               \
Toshihiro Shimizu 890ddd
		tmp = (pix).g + (error);     \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);        \
Toshihiro Shimizu 890ddd
		(pix).g = tmp;               \
Toshihiro Shimizu 890ddd
		tmp = (pix).b + (error);     \
Toshihiro Shimizu 890ddd
		BMP_CUT(tmp, 0, 255);        \
Toshihiro Shimizu 890ddd
		(pix).b = tmp;               \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*-- Structures and Enums ---------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef struct
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	UINT bfSize;
Toshihiro Shimizu 890ddd
	UINT bfOffBits;
Toshihiro Shimizu 890ddd
	UINT biSize;
Toshihiro Shimizu 890ddd
	UINT biWidth;
Toshihiro Shimizu 890ddd
	UINT biHeight;
Toshihiro Shimizu 890ddd
	UINT biPlanes;
Toshihiro Shimizu 890ddd
	UINT biBitCount;
Toshihiro Shimizu 890ddd
	UINT biCompression;
Toshihiro Shimizu 890ddd
	UINT biSizeImage;
Toshihiro Shimizu 890ddd
	UINT biXPelsPerMeter;
Toshihiro Shimizu 890ddd
	UINT biYPelsPerMeter;
Toshihiro Shimizu 890ddd
	UINT biClrUsed;
Toshihiro Shimizu 890ddd
	UINT biClrImportant;
Toshihiro Shimizu 890ddd
	UINT biFilesize;
Toshihiro Shimizu 890ddd
	UINT biPad;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} BMP_HEADER;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*-- Prototypes -------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int load_bmp_header(FILE *fp, BMP_HEADER **pHd);
Toshihiro Shimizu 890ddd
int write_bmp_header(FILE *fp, BMP_HEADER *hd);
Toshihiro Shimizu 890ddd
void release_bmp_header(BMP_HEADER *hd);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int write_bmp_palette(FILE *fp, int nc, UCHAR *b, UCHAR *g, UCHAR *r);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int make_bmp_palette(int colors, int grey, UCHAR *r, UCHAR *g, UCHAR *b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
BMP_SUBTYPE bmp_get_colorstyle(IMAGE *img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int error_checking_bmp(BMP_HEADER *hd);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int read_bmp_line(FILE *fp, void *line,
Toshihiro Shimizu 890ddd
				  UINT w, UINT h, UCHAR **map, BMP_SUBTYPE type);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int write_bmp_line(FILE *fp, void *line_buffer,
Toshihiro Shimizu 890ddd
				   UINT w, UINT row, UCHAR *map, BMP_SUBTYPE type);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int skip_bmp_lines(FILE *fp, UINT w, UINT rows, int whence, BMP_SUBTYPE type);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*-- Local Prototypes -------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static UINT getshort(FILE *fp);
Toshihiro Shimizu 890ddd
static UINT getint(FILE *fp);
Toshihiro Shimizu 890ddd
static void putshort(FILE *fp, int value);
Toshihiro Shimizu 890ddd
static void putint(FILE *fp, int value);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int img_read_bmp_generic(const MYSTRING fname, int type, IMAGE **);
Toshihiro Shimizu 890ddd
#ifndef UNUSED_REDUCE_COLORS
Toshihiro Shimizu 890ddd
static UCHAR *reduce_colors(UCHAR *buffin, int xsize, int ysize,
Toshihiro Shimizu 890ddd
							UCHAR *rmap, UCHAR *gmap, UCHAR *bmap, int nc);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int loadBMP1(FILE *fp, LPIXEL *pic, UINT w, UINT h, UCHAR *r, UCHAR *g, UCHAR *b);
Toshihiro Shimizu 890ddd
static int loadBMP4(FILE *fp, LPIXEL *pic, UINT w, UINT h, UINT comp, UCHAR *r, UCHAR *g, UCHAR *b);
Toshihiro Shimizu 890ddd
static int loadBMP8(FILE *fp, LPIXEL *pic, UINT w, UINT h, UINT comp, UCHAR *r, UCHAR *g, UCHAR *b);
Toshihiro Shimizu 890ddd
static int loadBMP24(FILE *fp, LPIXEL *pic, UINT w, UINT h);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int writeBMP1(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
static int writeBMP4(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
static int writeBMPC4(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
static int writeBMP8(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
static int writeBMPC8(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
static int writeBMP24(FILE *fp, UCHAR *pic24, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int load_lineBMP1(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map);
Toshihiro Shimizu 890ddd
static int load_lineBMP4(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map);
Toshihiro Shimizu 890ddd
static int load_lineBMPC4(FILE *fp, LPIXEL *pic, UINT w, UINT y, UCHAR **map);
Toshihiro Shimizu 890ddd
static int load_lineBMP8(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map);
Toshihiro Shimizu 890ddd
static int load_lineBMPC8(FILE *fp, LPIXEL *pic, UINT w, UINT y, UCHAR **map);
Toshihiro Shimizu 890ddd
static int load_lineBMP24(FILE *fp, LPIXEL *pic, UINT w, UINT padb);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int line_writeBMP1(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc);
Toshihiro Shimizu 890ddd
static int line_writeBMP4(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc);
Toshihiro Shimizu 890ddd
static int line_writeBMPC4(FILE *fp, UCHAR *pic8, UINT w, UINT row, UCHAR *pc2nc);
Toshihiro Shimizu 890ddd
static int line_writeBMP8(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc);
Toshihiro Shimizu 890ddd
static int line_writeBMPC8(FILE *fp, UCHAR *pic8, UINT w, UINT row, UCHAR *pc2nc);
Toshihiro Shimizu 890ddd
static int line_writeBMP24(FILE *fp, LPIXEL *pp, UINT w, UINT padb);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int skip_rowsBMP1(FILE *fp, UINT w, UINT pad, UINT rows, int whence);
Toshihiro Shimizu 890ddd
static int skip_rowsBMP4(FILE *fp, UINT w, UINT pad, UINT rows, int whence);
Toshihiro Shimizu 890ddd
static int skip_rowsBMPC4(FILE *fp, UINT rows);
Toshihiro Shimizu 890ddd
static int skip_rowsBMP8(FILE *fp, UINT w, UINT pad, UINT rows, int whence);
Toshihiro Shimizu 890ddd
static int skip_rowsBMPC8(FILE *fp, UINT rows);
Toshihiro Shimizu 890ddd
static int skip_rowsBMP24(FILE *fp, UINT w, UINT pad, UINT rows, int whence);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BMP_DEBUG 0
Toshihiro Shimizu 890ddd
#define __BMP_WRITE_LINE_BY_LINE
Toshihiro Shimizu 890ddd
#define __BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int read_bmp_line(FILE *fp, void *line_buffer,
Toshihiro Shimizu 890ddd
				  UINT w, UINT row, UCHAR **map, BMP_SUBTYPE type)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	LPIXEL *pic = (LPIXEL *)line_buffer;
Toshihiro Shimizu 890ddd
	unsigned int pad;
Toshihiro Shimizu 890ddd
	int rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (type) {
Toshihiro Shimizu 890ddd
	case BMP_BW:
Toshihiro Shimizu 890ddd
		pad = ((w + 31) / 32) * 32;
Toshihiro Shimizu 890ddd
		rv = load_lineBMP1(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16 : __OR BMP_CMAPPED16 : pad = ((w + 7) / 8) * 8;
Toshihiro Shimizu 890ddd
		rv = load_lineBMP4(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16C : __OR BMP_CMAPPED16C : rv = load_lineBMPC4(fp, pic, w, row, map);
Toshihiro Shimizu 890ddd
		if (rv == -1)
Toshihiro Shimizu 890ddd
			rv = 1;
Toshihiro Shimizu 890ddd
		else if (rv == -2)
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		else if (rv == -3)
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256 : __OR BMP_CMAPPED256 : pad = ((w + 3) / 4) * 4;
Toshihiro Shimizu 890ddd
		rv = load_lineBMP8(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256C : __OR BMP_CMAPPED256C : rv = load_lineBMPC8(fp, pic, w, row, map);
Toshihiro Shimizu 890ddd
		if (rv == -1)
Toshihiro Shimizu 890ddd
			rv = 1;
Toshihiro Shimizu 890ddd
		else if (rv == -2)
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		else if (rv == -3)
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			rv = 0;
Toshihiro Shimizu 890ddd
		CASE BMP_RGB : pad = (4 - ((w * 3) % 4)) & 0x03;
Toshihiro Shimizu 890ddd
		rv = load_lineBMP24(fp, pic, w, pad);
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return !rv; /* return 0 for unsuccess */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int write_bmp_line(FILE *fp, void *line_buffer,
Toshihiro Shimizu 890ddd
				   UINT w, UINT row, UCHAR *map, BMP_SUBTYPE type)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pic = (UCHAR *)line_buffer;
Toshihiro Shimizu 890ddd
	LPIXEL *p24 = (LPIXEL *)line_buffer;
Toshihiro Shimizu 890ddd
	unsigned int pad;
Toshihiro Shimizu 890ddd
	int rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (type) {
Toshihiro Shimizu 890ddd
	case BMP_BW:
Toshihiro Shimizu 890ddd
		pad = ((w + 31) / 32) * 32;
Toshihiro Shimizu 890ddd
		rv = line_writeBMP1(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16 : __OR BMP_CMAPPED16 : pad = ((w + 7) / 8) * 8;
Toshihiro Shimizu 890ddd
		rv = line_writeBMP4(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16C : __OR BMP_CMAPPED16C : rv = line_writeBMPC4(fp, pic, w, row, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256 : __OR BMP_CMAPPED256 : pad = ((w + 3) / 4) * 4;
Toshihiro Shimizu 890ddd
		rv = line_writeBMP8(fp, pic, w, pad, map);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256C : __OR BMP_CMAPPED256C : rv = line_writeBMPC8(fp, pic, w, row, map);
Toshihiro Shimizu 890ddd
		CASE BMP_RGB : pad = (4 - ((w * 3) % 4)) & 0x03;
Toshihiro Shimizu 890ddd
		rv = line_writeBMP24(fp, p24, w, pad);
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv; /* 0 for unsuccess */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int skip_bmp_lines(FILE *fp, UINT w, UINT rows, int whence, BMP_SUBTYPE type)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	unsigned int pad;
Toshihiro Shimizu 890ddd
	int rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (type) {
Toshihiro Shimizu 890ddd
	case BMP_BW:
Toshihiro Shimizu 890ddd
		pad = ((w + 31) / 32) * 32;
Toshihiro Shimizu 890ddd
		rv = skip_rowsBMP1(fp, w, pad, rows, whence);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16 : __OR BMP_CMAPPED16 : pad = ((w + 7) / 8) * 8;
Toshihiro Shimizu 890ddd
		rv = skip_rowsBMP4(fp, w, pad, rows, whence);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY16C : __OR BMP_CMAPPED16C : rv = skip_rowsBMPC4(fp, rows);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256 : __OR BMP_CMAPPED256 : pad = ((w + 3) / 4) * 4;
Toshihiro Shimizu 890ddd
		rv = skip_rowsBMP8(fp, w, pad, rows, whence);
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256C : __OR BMP_CMAPPED256C : rv = skip_rowsBMPC8(fp, rows);
Toshihiro Shimizu 890ddd
		CASE BMP_RGB : pad = (4 - ((w * 3) % 4)) & 0x03;
Toshihiro Shimizu 890ddd
		rv = skip_rowsBMP24(fp, w, pad, rows, whence);
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return !rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int error_checking_bmp(BMP_HEADER *hd)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if ((hd->biBitCount != 1 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 4 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 8 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 24) ||
Toshihiro Shimizu 890ddd
		hd->biPlanes != 1 || hd->biCompression > BMP_BI_RLE4) {
Toshihiro Shimizu 890ddd
		return UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if (((hd->biBitCount == 1 || hd->biBitCount == 24) &&
Toshihiro Shimizu 890ddd
		 hd->biCompression != BMP_BI_RGB) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 4 && hd->biCompression == BMP_BI_RLE8) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 8 && hd->biCompression == BMP_BI_RLE4)) {
Toshihiro Shimizu 890ddd
		return UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_bmp_header(FILE *fp, BMP_HEADER **header)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	BMP_HEADER *hd = NULL;
Toshihiro Shimizu 890ddd
	int c, c1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*header = NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hd = (BMP_HEADER *)calloc((size_t)1, sizeof(BMP_HEADER));
Toshihiro Shimizu 890ddd
	if (!hd)
Toshihiro Shimizu 890ddd
		return OUT_OF_MEMORY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* figure out the file size */
Toshihiro Shimizu 890ddd
	fseek(fp, 0L, SEEK_END);
Toshihiro Shimizu 890ddd
	hd->biFilesize = ftell(fp);
Toshihiro Shimizu 890ddd
	fseek(fp, 0L, 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* read the file type (first two bytes) */
Toshihiro Shimizu 890ddd
	c = getc(fp);
Toshihiro Shimizu 890ddd
	c1 = getc(fp);
Toshihiro Shimizu 890ddd
	if (c != 'B' || c1 != 'M') {
Toshihiro Shimizu 890ddd
		free(hd);
Toshihiro Shimizu 890ddd
		return UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hd->bfSize = getint(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* reserved and ignored */
Toshihiro Shimizu 890ddd
	getshort(fp);
Toshihiro Shimizu 890ddd
	getshort(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hd->bfOffBits = getint(fp);
Toshihiro Shimizu 890ddd
	hd->biSize = getint(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (hd->biSize == BMP_WIN_NEW || hd->biSize == BMP_OS2_NEW) {
Toshihiro Shimizu 890ddd
		hd->biWidth = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biHeight = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biPlanes = getshort(fp);
Toshihiro Shimizu 890ddd
		hd->biBitCount = getshort(fp);
Toshihiro Shimizu 890ddd
		hd->biCompression = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biSizeImage = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biXPelsPerMeter = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biYPelsPerMeter = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biClrUsed = getint(fp);
Toshihiro Shimizu 890ddd
		hd->biClrImportant = getint(fp);
Toshihiro Shimizu 890ddd
	} else /* old bitmap format */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		hd->biWidth = getshort(fp);
Toshihiro Shimizu 890ddd
		hd->biHeight = getshort(fp);
Toshihiro Shimizu 890ddd
		hd->biPlanes = getshort(fp);
Toshihiro Shimizu 890ddd
		hd->biBitCount = getshort(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* Not in old versions so have to compute them */
Toshihiro Shimizu 890ddd
		hd->biSizeImage = (((hd->biPlanes *
Toshihiro Shimizu 890ddd
							 hd->biBitCount *
Toshihiro Shimizu 890ddd
							 hd->biWidth) +
Toshihiro Shimizu 890ddd
							31) /
Toshihiro Shimizu 890ddd
						   32) *
Toshihiro Shimizu 890ddd
						  4 * hd->biHeight;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		hd->biCompression = BMP_BI_RGB;
Toshihiro Shimizu 890ddd
		hd->biXPelsPerMeter = 0;
Toshihiro Shimizu 890ddd
		hd->biYPelsPerMeter = 0;
Toshihiro Shimizu 890ddd
		hd->biClrUsed = 0;
Toshihiro Shimizu 890ddd
		hd->biClrImportant = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_DEBUG) {
Toshihiro Shimizu 890ddd
		printf("\nLoadBMP:\tbfSize=%u, bfOffBits=%u\n", hd->bfSize,
Toshihiro Shimizu 890ddd
			   hd->bfOffBits);
Toshihiro Shimizu 890ddd
		printf("\t\tbiSize=%u, biWidth=%u, biHeight=%u, biPlanes=%u\n",
Toshihiro Shimizu 890ddd
			   hd->biSize,
Toshihiro Shimizu 890ddd
			   hd->biWidth,
Toshihiro Shimizu 890ddd
			   hd->biHeight,
Toshihiro Shimizu 890ddd
			   hd->biPlanes);
Toshihiro Shimizu 890ddd
		printf("\t\tbiBitCount=%u, biCompression=%u, biSizeImage=%u\n",
Toshihiro Shimizu 890ddd
			   hd->biBitCount,
Toshihiro Shimizu 890ddd
			   hd->biCompression,
Toshihiro Shimizu 890ddd
			   hd->biSizeImage);
Toshihiro Shimizu 890ddd
		printf("\t\tbiX,YPelsPerMeter=%u,%u  biClrUsed=%u, biClrImp=%u\n",
Toshihiro Shimizu 890ddd
			   hd->biXPelsPerMeter,
Toshihiro Shimizu 890ddd
			   hd->biYPelsPerMeter,
Toshihiro Shimizu 890ddd
			   hd->biClrUsed,
Toshihiro Shimizu 890ddd
			   hd->biClrImportant);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp)) {
Toshihiro Shimizu 890ddd
		free(hd);
Toshihiro Shimizu 890ddd
		return UNEXPECTED_EOF;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*header = hd;
Toshihiro Shimizu 890ddd
	return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void release_bmp_header(BMP_HEADER *hd)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (hd)
Toshihiro Shimizu 890ddd
		free(hd);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __LIBSIMAGE__
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int img_read_bmp(const MYSTRING fname, IMAGE **pimg)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return img_read_bmp_generic(fname, BMP_READ_IMAGE, pimg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int img_read_bmp_generic(const MYSTRING fname, int type, IMAGE **pimg)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int retCode = OK;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR r[256], g[256], b[256];
Toshihiro Shimizu 890ddd
	BMP_HEADER *hd = NULL;
Toshihiro Shimizu 890ddd
	IMAGE *img = NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int rv, c, i;
Toshihiro Shimizu 890ddd
	LPIXEL *pic;
Toshihiro Shimizu 890ddd
	FILE *fp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*pimg = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* returns  'NULL' on unsuccess */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pic = (LPIXEL *)NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* open image file */
Toshihiro Shimizu 890ddd
	fp = _wfopen(fname, L"rb");
Toshihiro Shimizu 890ddd
	if (!fp)
Toshihiro Shimizu 890ddd
		return CANT_OPEN_FILE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* load up the image header */
Toshihiro Shimizu 890ddd
	retCode = load_bmp_header(fp, &hd);
Toshihiro Shimizu 890ddd
	if (retCode != OK)
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if ((hd->biBitCount != 1 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 4 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 8 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 24) ||
Toshihiro Shimizu 890ddd
		hd->biPlanes != 1 || hd->biCompression > BMP_BI_RLE4) {
Toshihiro Shimizu 890ddd
		retCode = UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if (((hd->biBitCount == 1 || hd->biBitCount == 24) &&
Toshihiro Shimizu 890ddd
		 hd->biCompression != BMP_BI_RGB) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 4 && hd->biCompression == BMP_BI_RLE8) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 8 && hd->biCompression == BMP_BI_RLE4)) {
Toshihiro Shimizu 890ddd
		retCode = UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img = new_img();
Toshihiro Shimizu 890ddd
	img->type = TOONZRGB;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (type == BMP_READ_INFO) {
Toshihiro Shimizu 890ddd
		fclose(fp);
Toshihiro Shimizu 890ddd
		img->xSBsize = img->xsize = hd->biWidth;
Toshihiro Shimizu 890ddd
		img->ySBsize = img->ysize = hd->biHeight;
Toshihiro Shimizu 890ddd
		release_bmp_header(hd);
Toshihiro Shimizu 890ddd
		*pimg = img;
Toshihiro Shimizu 890ddd
		return OK;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	allocate_pixmap(img, (int)hd->biWidth, (int)hd->biHeight);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* hd->biPad; */
Toshihiro Shimizu 890ddd
	if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
		/* skip ahead to colormap, using biSize */
Toshihiro Shimizu 890ddd
		c = hd->biSize - 40; /* 40 bytes read from biSize to biClrImportant */
Toshihiro Shimizu 890ddd
		for (i = 0; i < c; i++)
Toshihiro Shimizu 890ddd
			getc(fp);
Toshihiro Shimizu 890ddd
		hd->biPad = hd->bfOffBits - (hd->biSize + 14);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* load up colormap, if any */
Toshihiro Shimizu 890ddd
	if (hd->biBitCount != 24) {
Toshihiro Shimizu 890ddd
		int i, cmaplen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*cmaplen = (hd->biClrUsed) ? hd->biClrUsed : 1 << hd->biBitCount;*/
Toshihiro Shimizu 890ddd
		if (hd->biClrUsed)
Toshihiro Shimizu 890ddd
			cmaplen = hd->biClrUsed;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			cmaplen = 1 << hd->biBitCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (i = 0; i < cmaplen; i++) {
Toshihiro Shimizu 890ddd
			b[i] = getc(fp);
Toshihiro Shimizu 890ddd
			g[i] = getc(fp);
Toshihiro Shimizu 890ddd
			r[i] = getc(fp);
Toshihiro Shimizu 890ddd
			if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
				getc(fp);
Toshihiro Shimizu 890ddd
				hd->biPad -= 4;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp)) {
Toshihiro Shimizu 890ddd
			retCode = UNEXPECTED_EOF;
Toshihiro Shimizu 890ddd
			goto ERROR;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (BMP_DEBUG) {
Toshihiro Shimizu 890ddd
			printf("LoadBMP:  BMP colormap:  (RGB order)\n");
Toshihiro Shimizu 890ddd
			for (i = 0; i < cmaplen; i++)
Toshihiro Shimizu 890ddd
				printf("%02x%02x%02x  ", r[i], g[i], b[i]);
Toshihiro Shimizu 890ddd
			printf("\n\n");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
		/* Waste any unused bytes between the colour map (if present)
Toshihiro Shimizu 890ddd
         and the start of the actual bitmap data. 
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (hd->biPad > 0) {
Toshihiro Shimizu 890ddd
			(void)getc(fp);
Toshihiro Shimizu 890ddd
			hd->biPad--;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* create 32 bit image buffer */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pic = (LPIXEL *)img->buffer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* load up the image */
Toshihiro Shimizu 890ddd
	switch (hd->biBitCount) {
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		rv = loadBMP1(fp, pic, hd->biWidth, hd->biHeight, r, g, b);
Toshihiro Shimizu 890ddd
		CASE 4 : rv = loadBMP4(fp, pic, hd->biWidth, hd->biHeight, hd->biCompression, r, g, b);
Toshihiro Shimizu 890ddd
		CASE 8 : rv = loadBMP8(fp, pic, hd->biWidth, hd->biHeight, hd->biCompression, r, g, b);
Toshihiro Shimizu 890ddd
		CASE 24 : rv = loadBMP24(fp, pic, hd->biWidth, hd->biHeight);
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		retCode = UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rv) {
Toshihiro Shimizu 890ddd
		retCode = UNEXPECTED_EOF;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	release_bmp_header(hd);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*pimg = img;
Toshihiro Shimizu 890ddd
	return OK;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ERROR:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	release_bmp_header(hd);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (img) {
Toshihiro Shimizu 890ddd
		TFREE(img->buffer)
Toshihiro Shimizu 890ddd
		TFREE(img)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return retCode;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif /* __LIBSIMAGE__ */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int img_read_bmp_region(const MYSTRING fname, IMAGE **pimg, int x1, int y1, int x2, int y2, int scale)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR r[256], g[256], b[256] /*  ,*map[3]  */;
Toshihiro Shimizu 890ddd
	LPIXEL *line = NULL;
Toshihiro Shimizu 890ddd
	UINT line_size = 0;
Toshihiro Shimizu 890ddd
	BMP_HEADER *hd = NULL;
Toshihiro Shimizu 890ddd
	EXT_INFO_REGION info;
Toshihiro Shimizu 890ddd
	BMP_SUBTYPE subtype;
Toshihiro Shimizu 890ddd
	LPIXEL *pic, *appo;
Toshihiro Shimizu 890ddd
	IMAGE *img = NULL;
Toshihiro Shimizu 890ddd
	UINT start_offset;
Toshihiro Shimizu 890ddd
	UINT c, i, j;
Toshihiro Shimizu 890ddd
	char buf[512];
Toshihiro Shimizu 890ddd
	FILE *fp;
Toshihiro Shimizu 890ddd
	UINT pad;
Toshihiro Shimizu 890ddd
	enum BMP_ERROR_CODE bmp_error = OK;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* initialize some variables */
Toshihiro Shimizu 890ddd
	i = pad = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* returns  'NULL' on unsuccess */
Toshihiro Shimizu 890ddd
	pic = (LPIXEL *)NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* open image file */
Toshihiro Shimizu 890ddd
	fp = _wfopen(fname, L"rb");
Toshihiro Shimizu 890ddd
	if (!fp) {
Toshihiro Shimizu 890ddd
		return CANT_OPEN_FILE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* load up the image header */
Toshihiro Shimizu 890ddd
	load_bmp_header(fp, &hd);
Toshihiro Shimizu 890ddd
	if (!hd)
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if ((hd->biBitCount != 1 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 4 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 8 &&
Toshihiro Shimizu 890ddd
		 hd->biBitCount != 24) ||
Toshihiro Shimizu 890ddd
		hd->biPlanes != 1 || hd->biCompression > BMP_BI_RLE4) {
Toshihiro Shimizu 890ddd
		sprintf(buf, "Bogus BMP File! (bitCount=%d, Planes=%d, Compression=%d)",
Toshihiro Shimizu 890ddd
				hd->biBitCount, hd->biPlanes, hd->biCompression);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bmp_error = UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* error checking */
Toshihiro Shimizu 890ddd
	if (((hd->biBitCount == 1 || hd->biBitCount == 24) &&
Toshihiro Shimizu 890ddd
		 hd->biCompression != BMP_BI_RGB) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 4 && hd->biCompression == BMP_BI_RLE8) ||
Toshihiro Shimizu 890ddd
		(hd->biBitCount == 8 && hd->biCompression == BMP_BI_RLE4)) {
Toshihiro Shimizu 890ddd
		sprintf(buf, "Bogus BMP File!  (bitCount=%d, Compression=%d)",
Toshihiro Shimizu 890ddd
				hd->biBitCount, hd->biCompression);
Toshihiro Shimizu 890ddd
		bmp_error = UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img = new_img();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->type = TOONZRGB;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->xsize = hd->biWidth;
Toshihiro Shimizu 890ddd
	img->ysize = hd->biHeight;
Toshihiro Shimizu 890ddd
	img->xSBsize = hd->biWidth;
Toshihiro Shimizu 890ddd
	img->ySBsize = hd->biHeight;
Toshihiro Shimizu 890ddd
	img->x_dpi = (double)(hd->biXPelsPerMeter / 39);
Toshihiro Shimizu 890ddd
	img->y_dpi = (double)(hd->biYPelsPerMeter / 39);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hd->biPad = 0;
Toshihiro Shimizu 890ddd
	if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
		/* skip ahead to colormap, using biSize */
Toshihiro Shimizu 890ddd
		c = hd->biSize - 40; /* 40 bytes read from biSize to biClrImportant */
Toshihiro Shimizu 890ddd
		for (i = 0; i < c; i++)
Toshihiro Shimizu 890ddd
			getc(fp);
Toshihiro Shimizu 890ddd
		hd->biPad = hd->bfOffBits - (hd->biSize + 14);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* load up colormap, if any */
Toshihiro Shimizu 890ddd
	if (hd->biBitCount != 24) {
Toshihiro Shimizu 890ddd
		int i, cmaplen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*cmaplen = (hd->biClrUsed) ? hd->biClrUsed : 1 << hd->biBitCount;*/
Toshihiro Shimizu 890ddd
		if (hd->biClrUsed)
Toshihiro Shimizu 890ddd
			cmaplen = hd->biClrUsed;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			cmaplen = hd->biBitCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (i = 0; i < cmaplen; i++) {
Toshihiro Shimizu 890ddd
			b[i] = getc(fp);
Toshihiro Shimizu 890ddd
			g[i] = getc(fp);
Toshihiro Shimizu 890ddd
			r[i] = getc(fp);
Toshihiro Shimizu 890ddd
			if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
				getc(fp);
Toshihiro Shimizu 890ddd
				hd->biPad -= 4;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp)) {
Toshihiro Shimizu 890ddd
			bmp_error = UNEXPECTED_EOF;
Toshihiro Shimizu 890ddd
			goto ERROR;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (BMP_DEBUG) {
Toshihiro Shimizu 890ddd
			printf("LoadBMP:  BMP colormap:  (RGB order)\n");
Toshihiro Shimizu 890ddd
			for (i = 0; i < cmaplen; i++)
Toshihiro Shimizu 890ddd
				printf("%02x%02x%02x  ", r[i], g[i], b[i]);
Toshihiro Shimizu 890ddd
			printf("\n\n");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (hd->biSize != BMP_WIN_OS2_OLD) {
Toshihiro Shimizu 890ddd
		/* Waste any unused bytes between the colour map (if present)
Toshihiro Shimizu 890ddd
         and the start of the actual bitmap data. 
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (hd->biPad > 0) {
Toshihiro Shimizu 890ddd
			(void)getc(fp);
Toshihiro Shimizu 890ddd
			hd->biPad--;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* get information about the portion of the image to load */
Toshihiro Shimizu 890ddd
	get_info_region(&info, x1, y1, x2, y2, scale,
Toshihiro Shimizu 890ddd
					(int)hd->biWidth, (int)hd->biHeight, TNZ_BOTLEFT);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* create 32 bit image buffer */
Toshihiro Shimizu 890ddd
	if (!allocate_pixmap(img, info.xsize, info.ysize)) {
Toshihiro Shimizu 890ddd
		bmp_error = OUT_OF_MEMORY;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	start_offset = info.y_offset * info.xsize + info.x_offset;
Toshihiro Shimizu 890ddd
	pic = ((LPIXEL *)img->buffer) + start_offset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (line_size < hd->biWidth + 32) {
Toshihiro Shimizu 890ddd
		line_size = hd->biWidth + 32;
Toshihiro Shimizu 890ddd
		if (!line)
Toshihiro Shimizu 890ddd
			TCALLOC((LPIXEL *)line, (size_t)line_size)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			TREALLOC(line, line_size)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!line) {
Toshihiro Shimizu 890ddd
		bmp_error = OUT_OF_MEMORY;
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (hd->biBitCount) {
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		pad = ((hd->biWidth + 31) / 32) * 32;
Toshihiro Shimizu 890ddd
		CASE 4 : pad = ((hd->biWidth + 7) / 8) * 8;
Toshihiro Shimizu 890ddd
		CASE 8 : pad = ((hd->biWidth + 3) / 4) * 4;
Toshihiro Shimizu 890ddd
		CASE 24 : pad = (4 - ((hd->biWidth * 3) % 4)) & 0x03;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		/* segnala errore ed esci */
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	subtype = bmp_get_colorstyle(img);
Toshihiro Shimizu 890ddd
	if (subtype == BMP_NONE)
Toshihiro Shimizu 890ddd
		goto ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (info.y_offset > 0)
Toshihiro Shimizu 890ddd
		info.scanNrow++;
Toshihiro Shimizu 890ddd
	if (info.x_offset > 0)
Toshihiro Shimizu 890ddd
		info.scanNcol++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*  print_info_region(&info);      */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (info.startScanRow > 0)
Toshihiro Shimizu 890ddd
		skip_bmp_lines(fp, hd->biWidth, (unsigned int)(info.startScanRow - 1), (unsigned int)SEEK_CUR, subtype);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < (UINT)info.scanNrow; i++) {
Toshihiro Shimizu 890ddd
		if (load_lineBMP24(fp, line, hd->biWidth, pad))
Toshihiro Shimizu 890ddd
			goto ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*  QUESTO SWITCH VA AGGIUSTATO!
Toshihiro Shimizu 890ddd
      switch (subtype)
Toshihiro Shimizu 890ddd
       {
Toshihiro Shimizu 890ddd
	 CASE BMP_BW:
Toshihiro Shimizu 890ddd
	     if (load_lineBMP1(fp, line, hd->biWidth, pad, map))
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
	 CASE BMP_GREY16:
Toshihiro Shimizu 890ddd
	 __OR BMP_CMAPPED16:
Toshihiro Shimizu 890ddd
	     if (load_lineBMP4(fp, line, hd->biWidth, pad, map))
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
	 CASE BMP_GREY16C:
Toshihiro Shimizu 890ddd
	 __OR BMP_CMAPPED16C:
Toshihiro Shimizu 890ddd
	     if (load_lineBMPC4(fp, line, hd->biWidth, i, map)==-1)
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
	 CASE BMP_GREY256:
Toshihiro Shimizu 890ddd
	 __OR BMP_CMAPPED256:
Toshihiro Shimizu 890ddd
	     if (load_lineBMP8(fp, line, hd->biWidth, pad, map))
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
	 CASE BMP_GREY256C:
Toshihiro Shimizu 890ddd
	 __OR BMP_CMAPPED256C:
Toshihiro Shimizu 890ddd
	     if (load_lineBMPC8(fp, line, hd->biWidth, i, map)==-1)
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
	 CASE BMP_RGB:
Toshihiro Shimizu 890ddd
	     if (load_lineBMP24(fp, line, hd->biWidth, pad))
Toshihiro Shimizu 890ddd
	        goto ERROR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
		for (appo = pic, j = c = 0; j < (UINT)info.scanNcol; j++, c += info.step)
Toshihiro Shimizu 890ddd
			*appo++ = *(line + info.startScanCol + c);
Toshihiro Shimizu 890ddd
		pic += info.xsize;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		skip_bmp_lines(fp, hd->biWidth, (unsigned int)(info.step - 1), (unsigned int)SEEK_CUR, subtype);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  if (BMP_FERROR(fp)) 
Toshihiro Shimizu 890ddd
   {
Toshihiro Shimizu 890ddd
     bmp_error(fname, "File appears truncated.  Winging it.\n");
Toshihiro Shimizu 890ddd
     goto ERROR;
Toshihiro Shimizu 890ddd
   }
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	release_bmp_header(hd);
Toshihiro Shimizu 890ddd
	TFREE(line);
Toshihiro Shimizu 890ddd
	*pimg = img;
Toshihiro Shimizu 890ddd
	return OK;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ERROR:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	printf("error: (row=%d)\n", i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	release_bmp_header(hd);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (img)
Toshihiro Shimizu 890ddd
		free_img(img);
Toshihiro Shimizu 890ddd
	TFREE(line);
Toshihiro Shimizu 890ddd
	return bmp_error;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int loadBMP1(FILE *fp, LPIXEL *pic, UINT w, UINT h, UCHAR *r, UCHAR *g, UCHAR *b)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, c, bitnum, padw, rv;
Toshihiro Shimizu 890ddd
	UCHAR byte;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
	UCHAR *map[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map[0] = r;
Toshihiro Shimizu 890ddd
	map[1] = g;
Toshihiro Shimizu 890ddd
	map[2] = b;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rv = c = 0;
Toshihiro Shimizu 890ddd
	padw = ((w + 31) / 32) * 32; /* 'w', padded to be a multiple of 32 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		pp = pic + (i * w);
Toshihiro Shimizu 890ddd
		rv = load_lineBMP1(fp, pp, w, padw, map);
Toshihiro Shimizu 890ddd
		if (rv)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		pp = pic + (i * w);
Toshihiro Shimizu 890ddd
		for (j = bitnum = 0; j < padw; j++, bitnum++) {
Toshihiro Shimizu 890ddd
			if ((bitnum & 7) == 0) /* read the next byte */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				bitnum = 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (j < w) {
Toshihiro Shimizu 890ddd
				byte = (c & 0x80) ? 1 : 0;
Toshihiro Shimizu 890ddd
				c <<= 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				pp->r = r[byte];
Toshihiro Shimizu 890ddd
				pp->g = g[byte];
Toshihiro Shimizu 890ddd
				pp->b = b[byte];
Toshihiro Shimizu 890ddd
				pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				pp++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMP1(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT j, c, bitnum;
Toshihiro Shimizu 890ddd
	UCHAR byte;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (c = 0, pp = pic, j = bitnum = 0; j < padw; j++, bitnum++) {
Toshihiro Shimizu 890ddd
		if ((bitnum & 7) == 0) /* read the next byte */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			bitnum = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (j < w) {
Toshihiro Shimizu 890ddd
			byte = (c & 0x80) ? 1 : 0;
Toshihiro Shimizu 890ddd
			c <<= 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp->r = map[0][byte];
Toshihiro Shimizu 890ddd
			pp->g = map[1][byte];
Toshihiro Shimizu 890ddd
			pp->b = map[2][byte];
Toshihiro Shimizu 890ddd
			pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMP1(FILE *fp, UINT w, UINT pad, UINT rows, int whence)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT offset = pad * rows;
Toshihiro Shimizu 890ddd
	UINT i, bitnum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = bitnum = 0; i < offset; i++, bitnum++) {
Toshihiro Shimizu 890ddd
		if ((bitnum & 7) == 0) {
Toshihiro Shimizu 890ddd
			getc(fp);
Toshihiro Shimizu 890ddd
			bitnum = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int loadBMP4(FILE *fp,
Toshihiro Shimizu 890ddd
					LPIXEL *pic, UINT w, UINT h, UINT comp, UCHAR *r, UCHAR *g, UCHAR *b)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, c, c1, x, y, nybnum, padw, rv;
Toshihiro Shimizu 890ddd
	UCHAR byte;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
	UCHAR *map[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map[0] = r;
Toshihiro Shimizu 890ddd
	map[1] = g;
Toshihiro Shimizu 890ddd
	map[2] = b;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rv = 0;
Toshihiro Shimizu 890ddd
	c = c1 = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (comp == BMP_BI_RGB) /* read uncompressed data */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		padw = ((w + 7) / 8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */
Toshihiro Shimizu 890ddd
		for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
			pp = pic + (i * w);
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
			rv = load_lineBMP4(fp, pp, w, padw, map);
Toshihiro Shimizu 890ddd
			if (rv)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			for (j = nybnum = 0; j < padw; j++, nybnum++) {
Toshihiro Shimizu 890ddd
				if ((nybnum & 1) == 0) /* read next byte */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					c = getc(fp);
Toshihiro Shimizu 890ddd
					nybnum = 0;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (j < w) {
Toshihiro Shimizu 890ddd
					byte = (c & 0xf0) >> 4;
Toshihiro Shimizu 890ddd
					c <<= 4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					pp->r = r[byte];
Toshihiro Shimizu 890ddd
					pp->g = g[byte];
Toshihiro Shimizu 890ddd
					pp->b = b[byte];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					pp++;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (comp == BMP_BI_RLE4) /* read RLE4 compressed data */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		x = y = 0;
Toshihiro Shimizu 890ddd
		pp = pic + x + (y)*w;
Toshihiro Shimizu 890ddd
		while (y < h) {
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			rv = load_lineBMPC4(fp, pp, w, y, map);
Toshihiro Shimizu 890ddd
			if (rv == -1) {
Toshihiro Shimizu 890ddd
				rv = 1;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			} else if (rv == -2) {
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
				y++;
Toshihiro Shimizu 890ddd
				pp = pic + y * w;
Toshihiro Shimizu 890ddd
			} else if (rv == -3) {
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				y += (rv / w);
Toshihiro Shimizu 890ddd
				pp = pic + rv;
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			if ((int)c == EOF) {
Toshihiro Shimizu 890ddd
				rv = 1;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (c) /* encoded mode */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c1 = getc(fp);
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
					byte = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
Toshihiro Shimizu 890ddd
					pp->r = r[byte];
Toshihiro Shimizu 890ddd
					pp->g = g[byte];
Toshihiro Shimizu 890ddd
					pp->b = b[byte];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else /* c==0x00  :  escape codes */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				if ((int)c == EOF) {
Toshihiro Shimizu 890ddd
					rv = 1;
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (c == 0x00) /* end of line */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					x = 0;
Toshihiro Shimizu 890ddd
					y++;
Toshihiro Shimizu 890ddd
					if (y < h)
Toshihiro Shimizu 890ddd
						pp = pic + x + (y)*w;
Toshihiro Shimizu 890ddd
				} else if (c == 0x01)
Toshihiro Shimizu 890ddd
					break;			/* end of  pic */
Toshihiro Shimizu 890ddd
				else if (c == 0x02) /* delta */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					c = getc(fp);
Toshihiro Shimizu 890ddd
					x += c;
Toshihiro Shimizu 890ddd
					c = getc(fp);
Toshihiro Shimizu 890ddd
					y += c;
Toshihiro Shimizu 890ddd
					if (y < h)
Toshihiro Shimizu 890ddd
						pp = pic + x + (y)*w;
Toshihiro Shimizu 890ddd
				} else /* absolute mode */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
						if ((i & 1) == 0)
Toshihiro Shimizu 890ddd
							c1 = getc(fp);
Toshihiro Shimizu 890ddd
						byte = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
Toshihiro Shimizu 890ddd
						pp->r = r[byte];
Toshihiro Shimizu 890ddd
						pp->g = g[byte];
Toshihiro Shimizu 890ddd
						pp->b = b[byte];
Toshihiro Shimizu 890ddd
						pp->m = 255;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (((c & 3) == 1) || ((c & 3) == 2)) /* read pad byte */
Toshihiro Shimizu 890ddd
						getc(fp);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMP4(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT nybnum, j, c;
Toshihiro Shimizu 890ddd
	UCHAR byte;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (c = 0, pp = pic, j = nybnum = 0; j < padw; j++, nybnum++) {
Toshihiro Shimizu 890ddd
		if ((nybnum & 1) == 0) /* read next byte */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			nybnum = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (j < w) {
Toshihiro Shimizu 890ddd
			byte = (c & 0xf0) >> 4;
Toshihiro Shimizu 890ddd
			c <<= 4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp->r = map[0][byte];
Toshihiro Shimizu 890ddd
			pp->g = map[1][byte];
Toshihiro Shimizu 890ddd
			pp->b = map[2][byte];
Toshihiro Shimizu 890ddd
			pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMP4(FILE *fp, UINT w, UINT pad, UINT rows, int whence)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT offset = pad * rows;
Toshihiro Shimizu 890ddd
	UINT i, nybnum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = nybnum = 0; i < offset; i++, nybnum++) {
Toshihiro Shimizu 890ddd
		if ((nybnum & 1) == 0) {
Toshihiro Shimizu 890ddd
			getc(fp);
Toshihiro Shimizu 890ddd
			nybnum = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMPC4(FILE *fp, LPIXEL *pic, UINT w, UINT y, UCHAR **map)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, c, c1, x;
Toshihiro Shimizu 890ddd
	UCHAR byte;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
   *  Codici di ritorno:
Toshihiro Shimizu 890ddd
   * 
Toshihiro Shimizu 890ddd
   *     -1:   incontrata la file del file       (EOF)
Toshihiro Shimizu 890ddd
   *     -2:   incontrata la fine della linea    (Escape code 0x00 0x00)
Toshihiro Shimizu 890ddd
   *     -3:   incontrata la fine dell' immagine (Escape code 0x00 0x01)
Toshihiro Shimizu 890ddd
   *    altro:   incontrato un delta               (Escape code 0x00 0x02)
Toshihiro Shimizu 890ddd
   */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* initialize some variables */
Toshihiro Shimizu 890ddd
	x = 0;
Toshihiro Shimizu 890ddd
	pp = pic;
Toshihiro Shimizu 890ddd
	c = c1 = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (1) {
Toshihiro Shimizu 890ddd
		c = getc(fp);
Toshihiro Shimizu 890ddd
		if ((int)c == EOF)
Toshihiro Shimizu 890ddd
			return -1;
Toshihiro Shimizu 890ddd
		if (c) { /* encoded mode */
Toshihiro Shimizu 890ddd
			c1 = getc(fp);
Toshihiro Shimizu 890ddd
			for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
				byte = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
Toshihiro Shimizu 890ddd
				pp->r = map[0][byte];
Toshihiro Shimizu 890ddd
				pp->g = map[1][byte];
Toshihiro Shimizu 890ddd
				pp->b = map[2][byte];
Toshihiro Shimizu 890ddd
				pp->m = 255;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else /* c==0x00  :  escape codes */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			if ((int)c == EOF)
Toshihiro Shimizu 890ddd
				return -1;
Toshihiro Shimizu 890ddd
			if (c == 0x00) /* end of line */
Toshihiro Shimizu 890ddd
				return -2;
Toshihiro Shimizu 890ddd
			else if (c == 0x01) /* end of pic */
Toshihiro Shimizu 890ddd
				return -3;
Toshihiro Shimizu 890ddd
			else if (c == 0x02) /* delta */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				x += c;
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				y += c;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				return (x + y * w);
Toshihiro Shimizu 890ddd
			} else /* absolute mode */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
					if ((i & 1) == 0)
Toshihiro Shimizu 890ddd
						c1 = getc(fp);
Toshihiro Shimizu 890ddd
					byte = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
Toshihiro Shimizu 890ddd
					pp->r = map[0][byte];
Toshihiro Shimizu 890ddd
					pp->g = map[1][byte];
Toshihiro Shimizu 890ddd
					pp->b = map[2][byte];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (((c & 3) == 1) || ((c & 3) == 2)) /* read pad byte */
Toshihiro Shimizu 890ddd
					getc(fp);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMPC4(FILE *fp, UINT rows)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, c, c1, rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (rows > 0) {
Toshihiro Shimizu 890ddd
		c = getc(fp);
Toshihiro Shimizu 890ddd
		switch (c) {
Toshihiro Shimizu 890ddd
		case 0x00:
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			switch (c) {
Toshihiro Shimizu 890ddd
			case 0x00:
Toshihiro Shimizu 890ddd
				rows--;
Toshihiro Shimizu 890ddd
				CASE 0x01 : rows = 0;
Toshihiro Shimizu 890ddd
				CASE 0x02 : c1 = getc(fp); /* x buffer offest */
Toshihiro Shimizu 890ddd
				c1 = getc(fp);			   /* y buffer offest */
Toshihiro Shimizu 890ddd
				rows -= c1;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++) {
Toshihiro Shimizu 890ddd
					if ((i & 1) == 0)
Toshihiro Shimizu 890ddd
						getc(fp);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (((c & 3) == 1) || ((c & 3) == 2))
Toshihiro Shimizu 890ddd
					getc(fp);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			c1 = getc(fp);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int loadBMP8(FILE *fp,
Toshihiro Shimizu 890ddd
					LPIXEL *pic, UINT w, UINT h, UINT comp, UCHAR *r, UCHAR *g, UCHAR *b)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, c, c1, padw, x, y, rv;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
	UCHAR *map[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map[0] = r;
Toshihiro Shimizu 890ddd
	map[1] = g;
Toshihiro Shimizu 890ddd
	map[2] = b;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (comp == BMP_BI_RGB) /* read uncompressed data */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		padw = ((w + 3) / 4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */
Toshihiro Shimizu 890ddd
		for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
			pp = pic + (i * w);
Toshihiro Shimizu 890ddd
			rv = load_lineBMP8(fp, pp, w, padw, map);
Toshihiro Shimizu 890ddd
			if (rv)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			pp = pic + (i * w);
Toshihiro Shimizu 890ddd
			for (j = 0; j < padw; j++) {
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				if ((int)c == EOF)
Toshihiro Shimizu 890ddd
					rv = 1;
Toshihiro Shimizu 890ddd
				if (j < w) {
Toshihiro Shimizu 890ddd
					pp->r = r[c];
Toshihiro Shimizu 890ddd
					pp->g = g[c];
Toshihiro Shimizu 890ddd
					pp->b = b[c];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					pp++;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (comp == BMP_BI_RLE8) /* read RLE8 compressed data */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		x = y = 0;
Toshihiro Shimizu 890ddd
		pp = pic + x + y * w;
Toshihiro Shimizu 890ddd
		while (y < h) {
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			rv = load_lineBMPC8(fp, pp, w, y, map);
Toshihiro Shimizu 890ddd
			if (rv == -1) {
Toshihiro Shimizu 890ddd
				rv = 1;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			} else if (rv == -2) {
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
				y++;
Toshihiro Shimizu 890ddd
				pp = pic + y * w;
Toshihiro Shimizu 890ddd
			} else if (rv == -3) {
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				y += (rv / w);
Toshihiro Shimizu 890ddd
				pp = pic + rv;
Toshihiro Shimizu 890ddd
				rv = 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			if ((int)c == EOF) {
Toshihiro Shimizu 890ddd
				rv = 1;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (c) { /* encoded mode */
Toshihiro Shimizu 890ddd
				c1 = getc(fp);
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
					pp->r = r[c1];
Toshihiro Shimizu 890ddd
					pp->g = g[c1];
Toshihiro Shimizu 890ddd
					pp->b = b[c1];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else /* c==0x00  :  escape codes */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				if ((int)c == EOF) {
Toshihiro Shimizu 890ddd
					rv = 1;
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (c == 0x00) /* end of line */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					x = 0;
Toshihiro Shimizu 890ddd
					y++;
Toshihiro Shimizu 890ddd
					pp = pic + x + y * w;
Toshihiro Shimizu 890ddd
				} else if (c == 0x01)
Toshihiro Shimizu 890ddd
					break;			/* end of pic */
Toshihiro Shimizu 890ddd
				else if (c == 0x02) /* delta */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					c = getc(fp);
Toshihiro Shimizu 890ddd
					x += c;
Toshihiro Shimizu 890ddd
					c = getc(fp);
Toshihiro Shimizu 890ddd
					y += c;
Toshihiro Shimizu 890ddd
					pp = pic + x + y * w;
Toshihiro Shimizu 890ddd
				} else /* absolute mode */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
						c1 = getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						pp->r = r[c1];
Toshihiro Shimizu 890ddd
						pp->g = g[c1];
Toshihiro Shimizu 890ddd
						pp->b = b[c1];
Toshihiro Shimizu 890ddd
						pp->m = 255;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (c & 1) /* odd length run: read an extra pad byte */
Toshihiro Shimizu 890ddd
						getc(fp);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMP8(FILE *fp, LPIXEL *pic, UINT w, UINT padw, UCHAR **map)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT j, c, rv = 0;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (pp = pic, j = 0; j < padw; j++) {
Toshihiro Shimizu 890ddd
		c = getc(fp);
Toshihiro Shimizu 890ddd
		if ((int)c == EOF) {
Toshihiro Shimizu 890ddd
			rv = 1;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (j < w) {
Toshihiro Shimizu 890ddd
			pp->r = map[0][c];
Toshihiro Shimizu 890ddd
			pp->g = map[1][c];
Toshihiro Shimizu 890ddd
			pp->b = map[2][c];
Toshihiro Shimizu 890ddd
			pp->m = 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMP8(FILE *fp, UINT w, UINT pad, UINT rows, int whence)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT offset = pad * rows;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fseek(fp, (long)offset, whence);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMPC8(FILE *fp, LPIXEL *pic, UINT w, UINT y, UCHAR **map)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, c, c1, x;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
   *  Codici di ritorno:
Toshihiro Shimizu 890ddd
   * 
Toshihiro Shimizu 890ddd
   *     -1:   incontrata la file del file       (EOF)
Toshihiro Shimizu 890ddd
   *     -2:   incontrata la fine della linea    (Escape code 0x00 0x00)
Toshihiro Shimizu 890ddd
   *     -3:   incontrata la fine dell' immagine (Escape code 0x00 0x01)
Toshihiro Shimizu 890ddd
   *  altro:   incontrato un delta               (Escape code 0x00 0x02)
Toshihiro Shimizu 890ddd
   */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = 0;
Toshihiro Shimizu 890ddd
	pp = pic;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (1) {
Toshihiro Shimizu 890ddd
		c = getc(fp);
Toshihiro Shimizu 890ddd
		if (c == EOF)
Toshihiro Shimizu 890ddd
			return -1;
Toshihiro Shimizu 890ddd
		if (c) { /* encoded mode */
Toshihiro Shimizu 890ddd
			c1 = getc(fp);
Toshihiro Shimizu 890ddd
			for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
				pp->r = map[0][c1];
Toshihiro Shimizu 890ddd
				pp->g = map[1][c1];
Toshihiro Shimizu 890ddd
				pp->b = map[2][c1];
Toshihiro Shimizu 890ddd
				pp->m = 255;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else /* c==0x00  :  escape codes */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			if (c == EOF)
Toshihiro Shimizu 890ddd
				return -1;
Toshihiro Shimizu 890ddd
			if (c == 0x00) /* end of line */
Toshihiro Shimizu 890ddd
				return -2;
Toshihiro Shimizu 890ddd
			else if (c == 0x01) /* end of pic */
Toshihiro Shimizu 890ddd
				return -3;
Toshihiro Shimizu 890ddd
			else if (c == 0x02) /* delta */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				x += c;
Toshihiro Shimizu 890ddd
				c = getc(fp);
Toshihiro Shimizu 890ddd
				y += c;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				return (x + y * w);
Toshihiro Shimizu 890ddd
			} else /* absolute mode */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++, x++, pp++) {
Toshihiro Shimizu 890ddd
					c1 = getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					pp->r = map[0][c1];
Toshihiro Shimizu 890ddd
					pp->g = map[1][c1];
Toshihiro Shimizu 890ddd
					pp->b = map[2][c1];
Toshihiro Shimizu 890ddd
					pp->m = 255;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (c & 1) /* odd length run: read an extra pad byte */
Toshihiro Shimizu 890ddd
					getc(fp);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMPC8(FILE *fp, UINT rows)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, c, c1, rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (rows > 0) {
Toshihiro Shimizu 890ddd
		c = getc(fp);
Toshihiro Shimizu 890ddd
		switch (c) {
Toshihiro Shimizu 890ddd
		case 0x00:
Toshihiro Shimizu 890ddd
			c = getc(fp);
Toshihiro Shimizu 890ddd
			switch (c) {
Toshihiro Shimizu 890ddd
			case 0x00:
Toshihiro Shimizu 890ddd
				rows--;
Toshihiro Shimizu 890ddd
				CASE 0x01 : rows = 0;
Toshihiro Shimizu 890ddd
				CASE 0x02 : c1 = getc(fp); /* x buffer offest */
Toshihiro Shimizu 890ddd
				c1 = getc(fp);			   /* y buffer offest */
Toshihiro Shimizu 890ddd
				rows -= c1;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				for (i = 0; i < c; i++)
Toshihiro Shimizu 890ddd
					getc(fp);
Toshihiro Shimizu 890ddd
				if (c & 1)
Toshihiro Shimizu 890ddd
					getc(fp);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			c1 = getc(fp);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		rv = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int loadBMP24(FILE *fp, LPIXEL *pic, UINT w, UINT h)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, padb, rv;
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rv = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	padb = (4 - ((w * 3) % 4)) & 0x03; /* # of pad bytes to read at EOscanline */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
#ifdef BMP_READ_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		pp = pic + i * w;
Toshihiro Shimizu 890ddd
		rv = load_lineBMP24(fp, pp, w, padb);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		for (pp = pic + i * w, j = 0; j < w; j++, pp++) {
Toshihiro Shimizu 890ddd
			pp->b = getc(fp); /* blue  */
Toshihiro Shimizu 890ddd
			pp->g = getc(fp); /* green */
Toshihiro Shimizu 890ddd
			pp->r = getc(fp); /* red   */
Toshihiro Shimizu 890ddd
			pp->m = 255;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		for (j = 0; j < padb; j++)
Toshihiro Shimizu 890ddd
			getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		rv = (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		if (rv)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rv;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int load_lineBMP24(FILE *fp, LPIXEL *pic, UINT w, UINT padb)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	LPIXEL *pp;
Toshihiro Shimizu 890ddd
	UINT j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (pp = pic, j = 0; j < w; j++, pp++) {
Toshihiro Shimizu 890ddd
		pp->b = getc(fp); /* blue  */
Toshihiro Shimizu 890ddd
		pp->g = getc(fp); /* green */
Toshihiro Shimizu 890ddd
		pp->r = getc(fp); /* red   */
Toshihiro Shimizu 890ddd
		pp->m = 255;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (j = 0; j < padb; j++)
Toshihiro Shimizu 890ddd
		getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int skip_rowsBMP24(FILE *fp, UINT w, UINT pad, UINT rows, int whence)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT offset = (w * 3 + pad) * rows;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fseek(fp, (long)offset, whence);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (BMP_FERROR(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*-- BMP WRITE --------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
BMP_SUBTYPE bmp_get_colorstyle(IMAGE *img)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return img->type;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int write_bmp_header(FILE *fp, BMP_HEADER *hd)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	putc('B', fp);
Toshihiro Shimizu 890ddd
	putc('M', fp); /* BMP file magic number           */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->bfSize);
Toshihiro Shimizu 890ddd
	putshort(fp, 0); /* reserved1                       */
Toshihiro Shimizu 890ddd
	putshort(fp, 0); /* reserved2                       */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->bfOffBits); /* offset from BOfile to BObitmap */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biSize);		  /* size of bitmap info header     */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biWidth);		  /* width                          */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biHeight);		  /* height                         */
Toshihiro Shimizu 890ddd
	putshort(fp, (int)hd->biPlanes);	  /* must be '1'                    */
Toshihiro Shimizu 890ddd
	putshort(fp, (int)hd->biBitCount);	/* 1,4,8, or 24                   */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biCompression);   /* BMP_BI_RGB, BMP_BI_RLE8 or BMP_BI_RLE4     */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biSizeImage);	 /* size of raw image data         */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biXPelsPerMeter); /* dpi * 39" per meter            */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biYPelsPerMeter); /* dpi * 39" per meter            */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biClrUsed);		  /* colors used in cmap            */
Toshihiro Shimizu 890ddd
	putint(fp, (int)hd->biClrImportant);  /* same as above                  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int write_bmp_palette(FILE *fp, int nc, UCHAR *b, UCHAR *g, UCHAR *r)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < nc; i++) {
Toshihiro Shimizu 890ddd
		putc(b[i], fp);
Toshihiro Shimizu 890ddd
		putc(g[i], fp);
Toshihiro Shimizu 890ddd
		putc(r[i], fp);
Toshihiro Shimizu 890ddd
		putc(0, fp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __LIBSIMAGE__
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int img_write_bmp(const MYSTRING fname, IMAGE *img)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int (*write_function)(FILE *fp, UCHAR *pic, UINT w, UINT h, UCHAR *map);
Toshihiro Shimizu 890ddd
	int h, w, i, nc, nbits, bperlin, comp;
Toshihiro Shimizu 890ddd
	UCHAR val;
Toshihiro Shimizu 890ddd
	UCHAR pc2nc[256], r1[256], g1[256], b1[256];
Toshihiro Shimizu 890ddd
	UCHAR *pic, *graypic;
Toshihiro Shimizu 890ddd
	BMP_SUBTYPE subtype;
Toshihiro Shimizu 890ddd
	BMP_HEADER hd;
Toshihiro Shimizu 890ddd
	FILE *fp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	subtype = bmp_get_colorstyle(img);
Toshihiro Shimizu 890ddd
	if (subtype == BMP_NONE)
Toshihiro Shimizu 890ddd
		return UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fp = _wfopen(fname, L"wb");
Toshihiro Shimizu 890ddd
	if (!fp)
Toshihiro Shimizu 890ddd
		return CANT_OPEN_FILE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	graypic = NULL;
Toshihiro Shimizu 890ddd
	nc = 0;
Toshihiro Shimizu 890ddd
	nbits = 0;
Toshihiro Shimizu 890ddd
	comp = 0;
Toshihiro Shimizu 890ddd
	h = img->ysize;
Toshihiro Shimizu 890ddd
	w = img->xsize;
Toshihiro Shimizu 890ddd
	pic = (UCHAR *)img->buffer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (subtype) {
Toshihiro Shimizu 890ddd
	case BMP_BW:
Toshihiro Shimizu 890ddd
		__OR BMP_GREY16 : __OR BMP_GREY16C :
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						  __OR BMP_CMAPPED256 : __OR BMP_CMAPPED256C : return UNSUPPORTED_BMP_FORMAT;
Toshihiro Shimizu 890ddd
		CASE BMP_GREY256 : __OR BMP_GREY256C : nbits = 8;
Toshihiro Shimizu 890ddd
		CASE BMP_RGB : nbits = 24;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		goto BMP_WRITE_ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* number bytes written per line */
Toshihiro Shimizu 890ddd
	bperlin = ((w * nbits + 31) / 32) * 4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* compute filesize and write it */
Toshihiro Shimizu 890ddd
	i = 14 +		 /* size of bitmap file header */
Toshihiro Shimizu 890ddd
		40 +		 /* size of bitmap info header */
Toshihiro Shimizu 890ddd
		(nc * 4) +   /* size of colormap */
Toshihiro Shimizu 890ddd
		bperlin * h; /* size of image data */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (nbits) {
Toshihiro Shimizu 890ddd
	case 4:
Toshihiro Shimizu 890ddd
		comp = (comp == TRUE) ? BMP_BI_RLE4 : BMP_BI_RGB;
Toshihiro Shimizu 890ddd
		CASE 8 : comp = (comp == TRUE) ? BMP_BI_RLE8 : BMP_BI_RGB;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		comp = BMP_BI_RGB;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* fill image header */
Toshihiro Shimizu 890ddd
	hd.bfSize = i;
Toshihiro Shimizu 890ddd
	hd.bfOffBits = 14 + 40 + (nc * 4);
Toshihiro Shimizu 890ddd
	hd.biSize = 40;
Toshihiro Shimizu 890ddd
	hd.biWidth = w;
Toshihiro Shimizu 890ddd
	hd.biHeight = h;
Toshihiro Shimizu 890ddd
	hd.biPlanes = 1;
Toshihiro Shimizu 890ddd
	hd.biBitCount = nbits;
Toshihiro Shimizu 890ddd
	hd.biCompression = comp;
Toshihiro Shimizu 890ddd
	hd.biSizeImage = bperlin * h;
Toshihiro Shimizu 890ddd
	hd.biXPelsPerMeter = 0 * 39;
Toshihiro Shimizu 890ddd
	hd.biYPelsPerMeter = 0 * 39;
Toshihiro Shimizu 890ddd
	hd.biClrUsed = nc;
Toshihiro Shimizu 890ddd
	hd.biClrImportant = nc;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!write_bmp_header(fp, &hd))
Toshihiro Shimizu 890ddd
		goto BMP_WRITE_ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	write_bmp_palette(fp, nc, b1, g1, r1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (nbits) {
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		write_function = writeBMP1;
Toshihiro Shimizu 890ddd
		CASE 4 : if (comp == BMP_BI_RGB) write_function = writeBMP4;
Toshihiro Shimizu 890ddd
		else write_function = writeBMPC4;
Toshihiro Shimizu 890ddd
		CASE 8 : if (comp == BMP_BI_RGB) write_function = writeBMP8;
Toshihiro Shimizu 890ddd
		else write_function = writeBMPC8;
Toshihiro Shimizu 890ddd
		CASE 24 : write_function = writeBMP24;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		goto BMP_WRITE_ERROR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* write out the image */
Toshihiro Shimizu 890ddd
	val = write_function(fp, pic, (unsigned int)w, (unsigned int)h, pc2nc);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (graypic)
Toshihiro Shimizu 890ddd
		free(graypic);
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 0 failed , 1 ok */
Toshihiro Shimizu 890ddd
	return val == 1 ? OK : WRITE_ERROR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
BMP_WRITE_ERROR:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	if (graypic)
Toshihiro Shimizu 890ddd
		free(graypic);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	_wremove(fname);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return WRITE_ERROR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif /* __LIBSIMAGE__ */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMP1(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, c, bitnum, padw;
Toshihiro Shimizu 890ddd
	UCHAR *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	padw = ((w + 31) / 32) * 32; /* 'w', padded to be a multiple of 32 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp = pic8 + (i * w);
Toshihiro Shimizu 890ddd
#ifdef BMP_WRITE_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		if (line_writeBMP1(fp, pp, w, padw, pc2nc) == FALSE)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		for (j = bitnum = c = 0; j <= padw; j++, bitnum++) {
Toshihiro Shimizu 890ddd
			if (bitnum == 8) /* write the next byte */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				putc((int)c, fp);
Toshihiro Shimizu 890ddd
				bitnum = c = 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			c <<= 1;
Toshihiro Shimizu 890ddd
			if (j < w) {
Toshihiro Shimizu 890ddd
				c |= (pc2nc[*pp++] & 0x01);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMP1(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp = pic8;
Toshihiro Shimizu 890ddd
	UINT j, c, bitnum;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = bitnum = c = 0; j <= padw; j++, bitnum++) {
Toshihiro Shimizu 890ddd
		if (bitnum == 8) /* write the next byte */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			putc((int)c, fp);
Toshihiro Shimizu 890ddd
			bitnum = c = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		c <<= 1;
Toshihiro Shimizu 890ddd
		if (j < w) {
Toshihiro Shimizu 890ddd
			c |= (pc2nc[*pp++] & 0x01);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMP4(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, c, nybnum, padw;
Toshihiro Shimizu 890ddd
	UCHAR *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	padw = ((w + 7) / 8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp = pic8 + (i * w);
Toshihiro Shimizu 890ddd
#ifdef BMP_WRITE_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		if (line_writeBMP4(fp, pp, w, padw, pc2nc) == FALSE)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		for (j = nybnum = c = 0; j <= padw; j++, nybnum++) {
Toshihiro Shimizu 890ddd
			if (nybnum == 2) /* write next byte */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				putc((int)(c & 0xff), fp);
Toshihiro Shimizu 890ddd
				nybnum = c = 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			c <<= 4;
Toshihiro Shimizu 890ddd
			if (j < w) {
Toshihiro Shimizu 890ddd
				c |= (pc2nc[*pp] & 0x0f);
Toshihiro Shimizu 890ddd
				pp++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMP4(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT j, c, nybnum;
Toshihiro Shimizu 890ddd
	UCHAR *pp = pic8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = nybnum = c = 0; j <= padw; j++, nybnum++) {
Toshihiro Shimizu 890ddd
		if (nybnum == 2) /* write next byte */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			putc((int)(c & 0xff), fp);
Toshihiro Shimizu 890ddd
			nybnum = c = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		c <<= 4;
Toshihiro Shimizu 890ddd
		if (j < w) {
Toshihiro Shimizu 890ddd
			c |= (pc2nc[*pp] & 0x0f);
Toshihiro Shimizu 890ddd
			pp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMPC4(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp1, *pp2, *pp3, byte1, byte2;
Toshihiro Shimizu 890ddd
	UINT i, cnt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp1 = pic8 + i * w;
Toshihiro Shimizu 890ddd
		pp2 = pp1 + 2;
Toshihiro Shimizu 890ddd
		pp3 = pp1 + w + 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (; pp2 < pp3; pp2 += 2) {
Toshihiro Shimizu 890ddd
			cnt = 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			byte1 = ((pc2nc[*pp1] << 4) & 0xf0) | (pc2nc[*(pp1 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
			byte2 = ((pc2nc[*pp2] << 4) & 0xf0) | (pc2nc[*(pp2 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (byte1 != byte2) {
Toshihiro Shimizu 890ddd
				putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
				putc(byte1, fp);
Toshihiro Shimizu 890ddd
				pp1 = pp2;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				while (cnt <= 254 && pp2 < pp3) {
Toshihiro Shimizu 890ddd
					cnt += 2;
Toshihiro Shimizu 890ddd
					pp2 += 2;
Toshihiro Shimizu 890ddd
					byte2 = ((pc2nc[*pp2] << 4) & 0xf0) | (pc2nc[*(pp2 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
					if (byte1 != byte2 || cnt >= 254 || pp2 + 2 > pp3) {
Toshihiro Shimizu 890ddd
						if (pp2 + 2 > pp3)
Toshihiro Shimizu 890ddd
							cnt -= 2;
Toshihiro Shimizu 890ddd
						putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
						putc(byte1, fp);
Toshihiro Shimizu 890ddd
						break;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				pp1 = pp2;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		putc(0x00, fp);
Toshihiro Shimizu 890ddd
		putc(0x00, fp);
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
	putc(0x01, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMPC4(FILE *fp, UCHAR *pic8, UINT w, UINT row, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp1, *pp2, *pp3, byte1, byte2;
Toshihiro Shimizu 890ddd
	UINT cnt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pp1 = pic8 + row * w;
Toshihiro Shimizu 890ddd
	pp2 = pp1 + 2;
Toshihiro Shimizu 890ddd
	pp3 = pp1 + w + 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; pp2 < pp3; pp2 += 2) {
Toshihiro Shimizu 890ddd
		cnt = 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		byte1 = ((pc2nc[*pp1] << 4) & 0xf0) | (pc2nc[*(pp1 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
		byte2 = ((pc2nc[*pp2] << 4) & 0xf0) | (pc2nc[*(pp2 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (byte1 != byte2) {
Toshihiro Shimizu 890ddd
			putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
			putc(byte1, fp);
Toshihiro Shimizu 890ddd
			pp1 = pp2;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			while (cnt <= 254 && pp2 < pp3) {
Toshihiro Shimizu 890ddd
				cnt += 2;
Toshihiro Shimizu 890ddd
				pp2 += 2;
Toshihiro Shimizu 890ddd
				byte2 = ((pc2nc[*pp2] << 4) & 0xf0) | (pc2nc[*(pp2 + 1)] & 0x0f);
Toshihiro Shimizu 890ddd
				if (byte1 != byte2 || cnt >= 254 || pp2 + 2 > pp3) {
Toshihiro Shimizu 890ddd
					if (pp2 + 2 > pp3)
Toshihiro Shimizu 890ddd
						cnt -= 2;
Toshihiro Shimizu 890ddd
					putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
					putc(byte1, fp);
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pp1 = pp2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMP8(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, padw;
Toshihiro Shimizu 890ddd
	UCHAR *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	padw = ((w + 3) / 4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp = pic8 + (i * w);
Toshihiro Shimizu 890ddd
#ifdef BMP_WRITE_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		if (line_writeBMP8(fp, pp, w, padw, pc2nc) == FALSE)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		/* for (j=0; j
Toshihiro Shimizu 890ddd
		for (j = 0; j < w; j++) {
Toshihiro Shimizu 890ddd
			putc(*pp, fp);
Toshihiro Shimizu 890ddd
			pp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		for (; j < padw; j++)
Toshihiro Shimizu 890ddd
			putc(0, fp);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMP8(FILE *fp, UCHAR *pic8, UINT w, UINT padw, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp = pic8;
Toshihiro Shimizu 890ddd
	UINT j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = 0; j < w; j++)
Toshihiro Shimizu 890ddd
		putc(pc2nc[*pp++], fp);
Toshihiro Shimizu 890ddd
	for (; j < padw; j++)
Toshihiro Shimizu 890ddd
		putc(0, fp);
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMPC8(FILE *fp, UCHAR *pic8, UINT w, UINT h, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp1, *pp2, *pp3, byte1, byte2;
Toshihiro Shimizu 890ddd
	UINT i, cnt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp1 = pic8 + i * w;
Toshihiro Shimizu 890ddd
		pp2 = pp1 + 1;
Toshihiro Shimizu 890ddd
		pp3 = pp1 + w + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (; pp2 < pp3; pp2++) {
Toshihiro Shimizu 890ddd
			cnt = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			byte1 = pc2nc[*pp1];
Toshihiro Shimizu 890ddd
			byte2 = pc2nc[*pp2];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (byte1 != byte2) {
Toshihiro Shimizu 890ddd
				putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
				putc(byte1, fp);
Toshihiro Shimizu 890ddd
				pp1 = pp2;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				while (cnt <= 254 && pp2 < pp3) {
Toshihiro Shimizu 890ddd
					cnt++;
Toshihiro Shimizu 890ddd
					pp2++;
Toshihiro Shimizu 890ddd
					byte2 = pc2nc[*pp2];
Toshihiro Shimizu 890ddd
					if (byte1 != byte2 || cnt >= 254 || pp2 + 1 > pp3) {
Toshihiro Shimizu 890ddd
						if (pp2 + 1 > pp3)
Toshihiro Shimizu 890ddd
							cnt--;
Toshihiro Shimizu 890ddd
						putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
						putc(byte1, fp);
Toshihiro Shimizu 890ddd
						break;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				pp1 = pp2;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		putc(0x00, fp);
Toshihiro Shimizu 890ddd
		putc(0x00, fp);
Toshihiro Shimizu 890ddd
		if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
	putc(0x01, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMPC8(FILE *fp, UCHAR *pic8, UINT w, UINT row, UCHAR *pc2nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *pp1, *pp2, *pp3, byte1, byte2;
Toshihiro Shimizu 890ddd
	UINT cnt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pp1 = pic8 + row * w;
Toshihiro Shimizu 890ddd
	pp2 = pp1 + 1;
Toshihiro Shimizu 890ddd
	pp3 = pp1 + w + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; pp2 < pp3; pp2++) {
Toshihiro Shimizu 890ddd
		cnt = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		byte1 = pc2nc[*pp1];
Toshihiro Shimizu 890ddd
		byte2 = pc2nc[*pp2];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (byte1 != byte2) {
Toshihiro Shimizu 890ddd
			putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
			putc(byte1, fp);
Toshihiro Shimizu 890ddd
			pp1 = pp2;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			while (cnt <= 254 && pp2 < pp3) {
Toshihiro Shimizu 890ddd
				cnt++;
Toshihiro Shimizu 890ddd
				pp2++;
Toshihiro Shimizu 890ddd
				byte2 = pc2nc[*pp2];
Toshihiro Shimizu 890ddd
				if (byte1 != byte2 || cnt >= 254 || pp2 + 1 > pp3) {
Toshihiro Shimizu 890ddd
					if (pp2 + 1 > pp3)
Toshihiro Shimizu 890ddd
						cnt--;
Toshihiro Shimizu 890ddd
					putc((int)cnt, fp);
Toshihiro Shimizu 890ddd
					putc(byte1, fp);
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pp1 = pp2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
	putc(0x00, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int writeBMP24(FILE *fp, UCHAR *pic24, UINT w, UINT h, UCHAR *whence)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT i, j, padb;
Toshihiro Shimizu 890ddd
	LPIXEL *pixel;
Toshihiro Shimizu 890ddd
	UCHAR *pp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* pc2nc not used */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	padb = (4 - ((w * 3) % 4)) & 0x03; /* # of pad bytes to write at EOscanline */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < h; i++) {
Toshihiro Shimizu 890ddd
		pp = pic24 + (i * w * 4);
Toshihiro Shimizu 890ddd
		pixel = (LPIXEL *)pp;
Toshihiro Shimizu 890ddd
#ifdef BMP_WRITE_LINE_BY_LINE
Toshihiro Shimizu 890ddd
		if (line_writeBMP24(fp, pixel, w, padb) == FALSE)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		for (j = 0; j < w; j++) {
Toshihiro Shimizu 890ddd
			putc(pixel->b, fp);
Toshihiro Shimizu 890ddd
			putc(pixel->g, fp);
Toshihiro Shimizu 890ddd
			putc(pixel->r, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pixel++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		for (j = 0; j < padb; j++)
Toshihiro Shimizu 890ddd
			putc(0, fp);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static int line_writeBMP24(FILE *fp, LPIXEL *pp, UINT w, UINT padb)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = 0; j < w; j++) {
Toshihiro Shimizu 890ddd
		putc(pp->b, fp);
Toshihiro Shimizu 890ddd
		putc(pp->g, fp);
Toshihiro Shimizu 890ddd
		putc(pp->r, fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pp++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (j = 0; j < padb; j++)
Toshihiro Shimizu 890ddd
		putc(0, fp);
Toshihiro Shimizu 890ddd
	if (BMP_FERROR(fp))
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __LIBSIMAGE__
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef UNUSED_REDUCE_COLORS
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static UCHAR *reduce_colors(UCHAR *buffin, int xsize, int ysize,
Toshihiro Shimizu 890ddd
							UCHAR *rmap, UCHAR *gmap, UCHAR *bmap, int nc)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	LPIXEL *curr_pix, *next_pix, *prev_pix, *buffer;
Toshihiro Shimizu 890ddd
	static LPIXEL *mbuffer = NULL;
Toshihiro Shimizu 890ddd
	static UCHAR *ret_buf = NULL;
Toshihiro Shimizu 890ddd
	static int outbuf_size = 0;
Toshihiro Shimizu 890ddd
	static int buffin_size = 0;
Toshihiro Shimizu 890ddd
	int r1, g1, b1, dim;
Toshihiro Shimizu 890ddd
	int i, j, tmp;
Toshihiro Shimizu 890ddd
	int imax, jmax;
Toshihiro Shimizu 890ddd
	UCHAR *outbuf;
Toshihiro Shimizu 890ddd
	USHORT val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dim = xsize * ysize;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dim > outbuf_size) {
Toshihiro Shimizu 890ddd
		if (!ret_buf)
Toshihiro Shimizu 890ddd
			TCALLOC(ret_buf, dim)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			TREALLOC(ret_buf, dim);
Toshihiro Shimizu 890ddd
		if (!ret_buf)
Toshihiro Shimizu 890ddd
			return NULL;
Toshihiro Shimizu 890ddd
		outbuf_size = dim;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dim > buffin_size) {
Toshihiro Shimizu 890ddd
		if (!mbuffer)
Toshihiro Shimizu 890ddd
			TCALLOC(mbuffer, dim)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			TREALLOC(mbuffer, dim);
Toshihiro Shimizu 890ddd
		if (!ret_buf)
Toshihiro Shimizu 890ddd
			return NULL;
Toshihiro Shimizu 890ddd
		buffin_size = dim;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	memcpy(mbuffer, buffin, dim * sizeof(LPIXEL));
Toshihiro Shimizu 890ddd
	buffer = mbuffer;
Toshihiro Shimizu 890ddd
	outbuf = ret_buf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	imax = ysize - 1;
Toshihiro Shimizu 890ddd
	jmax = xsize - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < ysize; i++) {
Toshihiro Shimizu 890ddd
		curr_pix = buffer;
Toshihiro Shimizu 890ddd
		buffer += xsize;
Toshihiro Shimizu 890ddd
		next_pix = buffer;
Toshihiro Shimizu 890ddd
		prev_pix = NIL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (j = 0; j < xsize; j++) {
Toshihiro Shimizu 890ddd
			r1 = curr_pix->r;
Toshihiro Shimizu 890ddd
			g1 = curr_pix->g;
Toshihiro Shimizu 890ddd
			b1 = curr_pix->b;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			val = BMP_REDUCE_COLORS(r1, g1, b1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			*(outbuf++) = (unsigned char)val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* errors on colors */
Toshihiro Shimizu 890ddd
			r1 -= rmap[val];
Toshihiro Shimizu 890ddd
			g1 -= gmap[val];
Toshihiro Shimizu 890ddd
			b1 -= bmap[val];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (j != jmax)
Toshihiro Shimizu 890ddd
				BMP_ADD_ERROR(curr_pix[1], 7) /*  RIGHT   */
Toshihiro Shimizu 890ddd
			if (i != imax)					  /*  UP      */
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				BMP_ADD_ERROR(*next_pix, 5)
Toshihiro Shimizu 890ddd
				if (j > 0)
Toshihiro Shimizu 890ddd
					BMP_ADD_ERROR(*prev_pix, 3) /* UP LEFT  */
Toshihiro Shimizu 890ddd
				if (j != jmax)
Toshihiro Shimizu 890ddd
					BMP_ADD_ERROR(next_pix[1], 1) /* UP RIGHT */
Toshihiro Shimizu 890ddd
				prev_pix = next_pix;
Toshihiro Shimizu 890ddd
				next_pix++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			curr_pix++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ret_buf;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif /* __LIBSIMAGE__ */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
int make_bmp_palette(int colors, int grey, UCHAR *r, UCHAR *g, UCHAR *b)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j, ind, val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (colors) {
Toshihiro Shimizu 890ddd
	case 2:
Toshihiro Shimizu 890ddd
		for (i = 0; i < 2; i++)
Toshihiro Shimizu 890ddd
			r[i] = g[i] = b[i] = i * 255;
Toshihiro Shimizu 890ddd
		CASE 16 : for (i = 0; i < 16; i++)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			for (j = 0; j < 16; j++) {
Toshihiro Shimizu 890ddd
				ind = i * 16 + j;
Toshihiro Shimizu 890ddd
				val = i * 16;
Toshihiro Shimizu 890ddd
				r[ind] = g[ind] = b[ind] = val;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		CASE 256 : if (grey)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			for (i = 0; i < 256; i++)
Toshihiro Shimizu 890ddd
				r[i] = g[i] = b[i] = i;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			for (i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
				r[i] = BMP_RMAP(i);
Toshihiro Shimizu 890ddd
				g[i] = BMP_GMAP(i);
Toshihiro Shimizu 890ddd
				b[i] = BMP_BMAP(i);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static UINT getshort(FILE *fp)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int c = getc(fp),
Toshihiro Shimizu 890ddd
		c1 = getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ((UINT)c) + (((UINT)c1) << 8);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static UINT getint(FILE *fp)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int c = getc(fp),
Toshihiro Shimizu 890ddd
		c1 = getc(fp),
Toshihiro Shimizu 890ddd
		c2 = getc(fp),
Toshihiro Shimizu 890ddd
		c3 = getc(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (((UINT)c) << 0) +
Toshihiro Shimizu 890ddd
		   (((UINT)c1) << 8) +
Toshihiro Shimizu 890ddd
		   (((UINT)c2) << 16) +
Toshihiro Shimizu 890ddd
		   (((UINT)c3) << 24);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static void putshort(FILE *fp, int i)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int c = (((UINT)i)) & 0xff,
Toshihiro Shimizu 890ddd
		c1 = (((UINT)i) >> 8) & 0xff;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	putc(c, fp);
Toshihiro Shimizu 890ddd
	putc(c1, fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
static void putint(FILE *fp, int i)
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int c = ((UINT)i) & 0xff,
Toshihiro Shimizu 890ddd
		c1 = (((UINT)i) >> 8) & 0xff,
Toshihiro Shimizu 890ddd
		c2 = (((UINT)i) >> 16) & 0xff,
Toshihiro Shimizu 890ddd
		c3 = (((UINT)i) >> 24) & 0xff;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	putc(c, fp);
Toshihiro Shimizu 890ddd
	putc(c1, fp);
Toshihiro Shimizu 890ddd
	putc(c2, fp);
Toshihiro Shimizu 890ddd
	putc(c3, fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int writebmp(const MYSTRING filename, int xsize, int ysize, void *buffer, int bpp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	IMAGE img;
Toshihiro Shimizu 890ddd
	img.xsize = xsize;
Toshihiro Shimizu 890ddd
	img.ysize = ysize;
Toshihiro Shimizu 890ddd
	img.buffer = buffer;
Toshihiro Shimizu 890ddd
	switch (bpp) {
Toshihiro Shimizu 890ddd
	case 8:
Toshihiro Shimizu 890ddd
		img.type = BMP_GREY256C;
Toshihiro Shimizu 890ddd
		CASE 32 : img.type = BMP_RGB;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return img_write_bmp(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int readbmp(const MYSTRING filename, int *xsize, int *ysize, void **buffer)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	IMAGE *img;
Toshihiro Shimizu 890ddd
	int retCode = img_read_bmp(filename, &img);
Toshihiro Shimizu 890ddd
	if (retCode != OK) {
Toshihiro Shimizu 890ddd
		*xsize = *ysize = 0;
Toshihiro Shimizu 890ddd
		*buffer = 0;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*xsize = img->xsize;
Toshihiro Shimizu 890ddd
		*ysize = img->ysize;
Toshihiro Shimizu 890ddd
		*buffer = img->buffer;
Toshihiro Shimizu 890ddd
		img->buffer = 0;
Toshihiro Shimizu 890ddd
		free_img(img);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return retCode;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int readbmpregion(const MYSTRING filename, void **pimg, int x1, int y1, int x2, int y2, int scale)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	IMAGE *img;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int retCode = img_read_bmp_region(filename, &img, x1, y1, x2, y2, scale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (retCode != OK) {
Toshihiro Shimizu 890ddd
		*pimg = 0;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*pimg = img->buffer;
Toshihiro Shimizu 890ddd
		free(img);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return retCode;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int readbmp_size(const MYSTRING fname, int *lx, int *ly)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	IMAGE *img;
Toshihiro Shimizu 890ddd
	int retCode = img_read_bmp_generic(fname, BMP_READ_INFO, &img);
Toshihiro Shimizu 890ddd
	if (retCode == OK) {
Toshihiro Shimizu 890ddd
		*lx = img->xsize;
Toshihiro Shimizu 890ddd
		*ly = img->ysize;
Toshihiro Shimizu 890ddd
		free(img);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return retCode;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int readbmp_bbox(const MYSTRING fname, int *x0, int *y0, int *x1, int *y1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	IMAGE *img;
Toshihiro Shimizu 890ddd
	int retCode = img_read_bmp_generic(fname, BMP_READ_INFO, &img);
Toshihiro Shimizu 890ddd
	if (retCode == OK) {
Toshihiro Shimizu 890ddd
		*x0 = 0;
Toshihiro Shimizu 890ddd
		*x1 = 0;
Toshihiro Shimizu 890ddd
		*x1 = img->xsize - 1;
Toshihiro Shimizu 890ddd
		*y1 = img->ysize - 1;
Toshihiro Shimizu 890ddd
		free(img);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return retCode;
Toshihiro Shimizu 890ddd
}