File:  [RetroPC.NET] / mkfont32 / accessories / mkres.c
Revision 1.1: download - view: text, annotated - select for diffs
Mon Jun 7 00:33:09 2004 JST (21 years, 4 months ago) by yui
Branches: MAIN
CVS tags: HEAD
initialize version (T.Yui)

#include	"compiler.h"
#include	"bmpdata.h"
#include	"dosio.h"
#include	"textout.h"


typedef struct {
	int		width;
	int		height;
	int		align;
	BYTE	*ptr;
} BMPRES;

static BMPRES *bmpload(const char *filename) {

	FILEH	fh;
	BMPFILE	bf;
	BMPINFO	bi;
	BMPDATA	bd;
	long	fptr;
	UINT	align;
	UINT	size;
	BMPRES	*ret;
	BYTE	*p;
	int		step;
	int		height;

	fh = file_open_rb(filename);
	if (fh == FILEH_INVALID) {
		goto bl_err1;
	}
	if ((file_read(fh, &bf, sizeof(bf)) != sizeof(bf)) ||
		(bf.bfType[0] != 'B') || (bf.bfType[1] != 'M')) {
		goto bl_err2;
	}
	if ((file_read(fh, &bi, sizeof(bi)) != sizeof(bi)) ||
		(bmpdata_getinfo(&bi, &bd) != SUCCESS) || (bd.bpp != 1)) {
		goto bl_err2;
	}
	fptr = LOADINTELDWORD(bf.bfOffBits);
	if (file_seek(fh, fptr, FSEEK_SET) != fptr) {
		goto bl_err2;
	}
	align = bmpdata_getalign(&bi);
	size = sizeof(BMPRES) + bmpdata_getdatasize(&bi);
	ret = (BMPRES *)_MALLOC(size, filename);
	if (ret == NULL) {
		goto bl_err2;
	}
	p = (BYTE *)(ret + 1);
	step = align;
	ret->width = bd.width;
	ret->align = align;
	ret->ptr = p;
	if (bd.height < 0) {
		height = 0 - bd.height;
	}
	else {
		height = bd.height;
		step = 0 - align;
		p += (height - 1) * align;
	}
	ret->height = height;
	while(height) {
		height--;
		if (file_read(fh, p, align) != align) {
			goto bl_err3;
		}
		p += step;
	}
	file_close(fh);
	return(ret);

bl_err3:
	_MFREE(ret);

bl_err2:
	file_close(fh);

bl_err1:
	return(NULL);
}


// ----

static void dump(const char *dstfile, const char *sym,
												const BYTE *ptr, UINT size) {

	void	*dst;
	char	work[256];
	UINT	r;
	UINT	i;

	dst = textout_open(dstfile, 256);
	if (sym == NULL) {
		sym = dstfile;
	}
	SPRINTF(work, "const unsigned char %s[%d] = {\n", sym, size);
	textout_write(dst, work);

	while(1) {
		r = min(size, 12);
		if (r == 0) {
			break;
		}
		for (i=0; i<r; i++) {
			SPRINTF(work + i*5, "0x%02x,", ptr[i]);
		}
		textout_write(dst, "\t\t\t");
		textout_write(dst, work);
		textout_write(dst, "\n");
		ptr += r;
		size -= r;
	}
	textout_write(dst, "};\n");
	textout_close(dst);
}


// ----

static void font8x8(const char *dstfile, const char *sym,
									const char *srcname, UINT sx, UINT sy) {

	BMPRES	*res;
	UINT	size;
	BYTE	*ptr;
	BYTE	*p;
	UINT	x;
	UINT	y;
	UINT	i;

	res = bmpload(srcname);
	if (res == NULL) {
		goto f_err1;
	}
	if ((res->width < (int)(sx * 8)) || (res->height < (int)(sy * 8))) {
		goto f_err2;
	}
	size = sx * sy * 8;
	ptr = (BYTE *)_MALLOC(size, srcname);
	if (ptr == NULL) {
		goto f_err2;
	}
	p = ptr;
	for (y=0; y<sy; y++) {
		for (x=0; x<sx; x++) {
			for (i=0; i<8; i++) {
				*p++ = res->ptr[x + (((y * 8) + i) * res->align)];
			}
		}
	}
	dump(dstfile, sym, ptr, size);

	_MFREE(ptr);

f_err2:
	_MFREE(res);

f_err1:
	return;
}

static void font8x16(const char *dstfile, const char *sym,
									const char *srcname, UINT sx, UINT sy) {

	BMPRES	*res;
	UINT	size;
	BYTE	*ptr;
	BYTE	*p;
	UINT	x;
	UINT	y;
	UINT	i;

	res = bmpload(srcname);
	if (res == NULL) {
		goto f_err1;
	}
	if ((res->width < (int)(sx * 8)) || (res->height < (int)(sy * 16))) {
		goto f_err2;
	}
	size = sx * sy * 16;
	ptr = (BYTE *)_MALLOC(size, srcname);
	if (ptr == NULL) {
		goto f_err2;
	}
	p = ptr;
	for (y=0; y<sy; y++) {
		for (x=0; x<sx; x++) {
			for (i=0; i<16; i++) {
				*p++ = res->ptr[x + (((y * 16) + i) * res->align)];
			}
		}
	}
	dump(dstfile, sym, ptr, size);

	_MFREE(ptr);

f_err2:
	_MFREE(res);

f_err1:
	return;
}

static void font16x16(const char *dstfile, const char *sym,
									const char *srcname, UINT sx, UINT sy) {

	BMPRES	*res;
	UINT	size;
	BYTE	*ptr;
	BYTE	*p;
	UINT	x;
	UINT	y;
	UINT	i;

	res = bmpload(srcname);
	if (res == NULL) {
		goto f_err1;
	}
	if ((res->width < (int)(sx * 16)) || (res->height < (int)(sy * 16))) {
		goto f_err2;
	}
	size = sx * 2 * sy * 16;
	ptr = (BYTE *)_MALLOC(size, srcname);
	if (ptr == NULL) {
		goto f_err2;
	}
	p = ptr;
	for (y=0; y<sy; y++) {
		for (x=0; x<sx; x++) {
			for (i=0; i<16; i++) {
				*p++ = res->ptr[(x * 2) + 0 + (((y * 16) + i) * res->align)];
				*p++ = res->ptr[(x * 2) + 1 + (((y * 16) + i) * res->align)];
			}
		}
	}
	dump(dstfile, sym, ptr, size);

	_MFREE(ptr);

f_err2:
	_MFREE(res);

f_err1:
	return;
}

int main(int argc, char **argv) {

	font8x8("..\\cmn_8.res", "cmn_8", "cmn_8.bmp", 0x20, 5);
	font8x8("..\\pc98_8.res", "pc98_8", "pc98_8.bmp", 0x20, 3);
	font8x16("..\\pc98_16.res", "pc98_16", "pc98_16.bmp", 0x20, 3);
	font16x16("..\\jis28xx.res", "jis28xx", "jis28xx.bmp", 0x20, 1);
	font8x16("..\\jis29xx.res", "jis29xx", "jis29xx.bmp", 0x5e, 1);
	font8x16("..\\jis2axx.res", "jis2axx", "jis2axx.bmp", 0x5e, 1);
	font8x16("..\\jis2bxx.res", "jis2bxx", "jis2bxx.bmp", 0x5e, 1);
	font16x16("..\\jis2cxx.res", "jis2cxx", "jis2cxx.bmp", 0x4c, 1);
	return(0);
}


RetroPC.NET-CVS <cvs@retropc.net>