⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gd_gd2.c

📁 PHP v6.0 For Linux 运行环境:Win9X/ WinME/ WinNT/ Win2K/ WinXP
💻 C
📖 第 1 页 / 共 2 页
字号:
/*   * gd_gd2.c   *   * Implements the I/O and support for the GD2 format.   *   * Changing the definition of GD2_DBG (below) will cause copious messages   * to be displayed while it processes requests.   *   * Designed, Written & Copyright 1999, Philip Warner.   * */#include <stdio.h>#include <errno.h>#include <math.h>#include <string.h>#include <stdlib.h>#include "gd.h"#include "gdhelpers.h"#include <zlib.h>#define TRUE 1#define FALSE 0/* 2.11: not part of the API, as the save routine can figure it out *	from im->trueColor, and the load routine doesn't need to tell *	the end user the saved format. NOTE: adding 2 is assumed *	to result in the correct format value for truecolor!*/#define GD2_FMT_TRUECOLOR_RAW 3#define GD2_FMT_TRUECOLOR_COMPRESSED 4#define gd2_compressed(fmt) (((fmt) == GD2_FMT_COMPRESSED) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))#define gd2_truecolor(fmt) (((fmt) == GD2_FMT_TRUECOLOR_RAW) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))/* Use this for commenting out debug-print statements. *//* Just use the first '#define' to allow all the prints... *//* #define GD2_DBG(s) (s) */#define GD2_DBG(s)typedef struct{	int offset;	int size;} t_chunk_info;extern int _gdGetColors(gdIOCtx * in, gdImagePtr im, int gd2xFlag);extern void _gdPutColors(gdImagePtr im, gdIOCtx * out);/* *//* Read the extra info in the gd2 header. *//* */static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** chunkIdx){	int i;	int ch;	char id[5];	t_chunk_info *cidx;	int sidx;	int nc;	GD2_DBG(php_gd_error("Reading gd2 header info"));	for (i = 0; i < 4; i++) {		ch = gdGetC(in);		if (ch == EOF) {			goto fail1;		}		id[i] = ch;	}	id[4] = 0;	GD2_DBG(php_gd_error("Got file code: %s", id));	/* Equiv. of 'magick'.  */	if (strcmp(id, GD2_ID) != 0) {		GD2_DBG(php_gd_error("Not a valid gd2 file"));		goto fail1;	}	/* Version */	if (gdGetWord(vers, in) != 1) {		goto fail1;	}	GD2_DBG(php_gd_error("Version: %d", *vers));	if ((*vers != 1) && (*vers != 2)) {		GD2_DBG(php_gd_error("Bad version: %d", *vers));		goto fail1;	}	/* Image Size */	if (!gdGetWord(sx, in)) {		GD2_DBG(php_gd_error("Could not get x-size"));		goto fail1;	}	if (!gdGetWord(sy, in)) {		GD2_DBG(php_gd_error("Could not get y-size"));		goto fail1;	}	GD2_DBG(php_gd_error("Image is %dx%d", *sx, *sy));	/* Chunk Size (pixels, not bytes!) */	if (gdGetWord(cs, in) != 1) {		goto fail1;	}	GD2_DBG(php_gd_error("ChunkSize: %d", *cs));	if ((*cs < GD2_CHUNKSIZE_MIN) || (*cs > GD2_CHUNKSIZE_MAX)) {		GD2_DBG(php_gd_error("Bad chunk size: %d", *cs));		goto fail1;	}	/* Data Format */	if (gdGetWord(fmt, in) != 1) {		goto fail1;	}	GD2_DBG(php_gd_error("Format: %d", *fmt));	if ((*fmt != GD2_FMT_RAW) && (*fmt != GD2_FMT_COMPRESSED) && (*fmt != GD2_FMT_TRUECOLOR_RAW) && (*fmt != GD2_FMT_TRUECOLOR_COMPRESSED)) {		GD2_DBG(php_gd_error("Bad data format: %d", *fmt));		goto fail1;	}	/* # of chunks wide */	if (gdGetWord(ncx, in) != 1) {		goto fail1;	}	GD2_DBG(php_gd_error("%d Chunks Wide", *ncx));	/* # of chunks high */	if (gdGetWord(ncy, in) != 1) {		goto fail1;	}	GD2_DBG(php_gd_error("%d Chunks vertically", *ncy));	if (gd2_compressed(*fmt)) {		nc = (*ncx) * (*ncy);		GD2_DBG(php_gd_error("Reading %d chunk index entries", nc));		sidx = sizeof(t_chunk_info) * nc;		if (sidx <= 0) {			goto fail1;		}		cidx = gdCalloc(sidx, 1);		for (i = 0; i < nc; i++) {			if (gdGetInt(&cidx[i].offset, in) != 1) {				goto fail1;			}			if (gdGetInt(&cidx[i].size, in) != 1) {				goto fail1;			}		}		*chunkIdx = cidx;	}	GD2_DBG(php_gd_error("gd2 header complete"));	return 1;fail1:	return 0;}static gdImagePtr _gd2CreateFromFile (gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** cidx){	gdImagePtr im;	if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1) {		GD2_DBG(php_gd_error("Bad GD2 header"));		goto fail1;	}	if (gd2_truecolor(*fmt)) {		im = gdImageCreateTrueColor(*sx, *sy);	} else {		im = gdImageCreate(*sx, *sy);	}	if (im == NULL) {		GD2_DBG(php_gd_error("Could not create gdImage"));		goto fail1;	}	if (!_gdGetColors(in, im, (*vers) == 2)) {		GD2_DBG(php_gd_error("Could not read color palette"));		goto fail2;	}	GD2_DBG(php_gd_error("Image palette completed: %d colours", im->colorsTotal));	return im;fail2:	gdImageDestroy(im);	return 0;fail1:	return 0;}static int _gd2ReadChunk (int offset, char *compBuf, int compSize, char *chunkBuf, uLongf * chunkLen, gdIOCtx * in){	int zerr;	if (gdTell(in) != offset) {		GD2_DBG(php_gd_error("Positioning in file to %d", offset));		gdSeek(in, offset);	} else {		GD2_DBG(php_gd_error("Already Positioned in file to %d", offset));	}	/* Read and uncompress an entire chunk. */	GD2_DBG(php_gd_error("Reading file"));	if (gdGetBuf(compBuf, compSize, in) != compSize) {		return FALSE;	}	GD2_DBG(php_gd_error("Got %d bytes. Uncompressing into buffer of %d bytes", compSize, (int)*chunkLen));	zerr = uncompress((unsigned char *) chunkBuf, chunkLen, (unsigned char *) compBuf, compSize);	if (zerr != Z_OK) {		GD2_DBG(php_gd_error("Error %d from uncompress", zerr));		return FALSE;	}	GD2_DBG(php_gd_error("Got chunk"));	return TRUE;}gdImagePtr gdImageCreateFromGd2 (FILE * inFile){	gdIOCtx *in = gdNewFileCtx(inFile);	gdImagePtr im;	im = gdImageCreateFromGd2Ctx(in);	in->gd_free(in);	return im;}gdImagePtr gdImageCreateFromGd2Ptr (int size, void *data){	gdImagePtr im;	gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);	im = gdImageCreateFromGd2Ctx(in);	in->gd_free(in);	return im;}gdImagePtr gdImageCreateFromGd2Ctx (gdIOCtxPtr in){	int sx, sy;	int i;	int ncx, ncy, nc, cs, cx, cy;	int x, y, ylo, yhi, xlo, xhi;	int vers, fmt;	t_chunk_info *chunkIdx = NULL;	/* So we can gdFree it with impunity. */	unsigned char *chunkBuf = NULL;	/* So we can gdFree it with impunity. */	int chunkNum = 0;	int chunkMax = 0;	uLongf chunkLen;	int chunkPos = 0;	int compMax = 0;	int bytesPerPixel;	char *compBuf = NULL;		/* So we can gdFree it with impunity. */	gdImagePtr im;	/* Get the header */	if (!(im = _gd2CreateFromFile(in, &sx, &sy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx))) {		 return 0;	}	bytesPerPixel = im->trueColor ? 4 : 1;	nc = ncx * ncy;	if (gd2_compressed(fmt)) {		/* Find the maximum compressed chunk size. */		compMax = 0;		for (i = 0; (i < nc); i++) {			if (chunkIdx[i].size > compMax) {				compMax = chunkIdx[i].size;			}		}		compMax++;		/* Allocate buffers */		chunkMax = cs * bytesPerPixel * cs;		if (chunkMax <= 0) {			return 0;		}		chunkBuf = gdCalloc(chunkMax, 1);		compBuf = gdCalloc(compMax, 1);		GD2_DBG(php_gd_error("Largest compressed chunk is %d bytes", compMax));	}	/* Read the data... */	for (cy = 0; (cy < ncy); cy++) {		for (cx = 0; (cx < ncx); cx++) {			ylo = cy * cs;			yhi = ylo + cs;			if (yhi > im->sy) {				yhi = im->sy;			}			GD2_DBG(php_gd_error("Processing Chunk %d (%d, %d), y from %d to %d", chunkNum, cx, cy, ylo, yhi));			if (gd2_compressed(fmt)) {				chunkLen = chunkMax;				if (!_gd2ReadChunk(chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, (char *) chunkBuf, &chunkLen, in)) {					GD2_DBG(php_gd_error("Error reading comproessed chunk"));					goto fail2;				}				chunkPos = 0;			}			for (y = ylo; (y < yhi); y++) {				xlo = cx * cs;				xhi = xlo + cs;				if (xhi > im->sx) {					xhi = im->sx;				}				if (!gd2_compressed(fmt)) {					for (x = xlo; x < xhi; x++) {						if (im->trueColor) {							if (!gdGetInt(&im->tpixels[y][x], in)) {								im->tpixels[y][x] = 0;							}						} else {							int ch;							if (!gdGetByte(&ch, in)) {								ch = 0;							}							im->pixels[y][x] = ch;						}					}				} else {					for (x = xlo; x < xhi; x++) {						if (im->trueColor) {							/* 2.0.1: work around a gcc bug by being verbose. TBB */							int a = chunkBuf[chunkPos++] << 24;							int r = chunkBuf[chunkPos++] << 16;							int g = chunkBuf[chunkPos++] << 8;							int b = chunkBuf[chunkPos++];							im->tpixels[y][x] = a + r + g + b;						} else {							im->pixels[y][x] = chunkBuf[chunkPos++];						}					}				}			}			chunkNum++;		}	}	GD2_DBG(php_gd_error("Freeing memory"));	if (chunkBuf) {		gdFree(chunkBuf);	}	if (compBuf) {		gdFree(compBuf);	}	if (chunkIdx) {		gdFree(chunkIdx);	}	GD2_DBG(php_gd_error("Done"));	return im;fail2:	gdImageDestroy(im);	if (chunkBuf) {		gdFree(chunkBuf);	}	if (compBuf) {		gdFree(compBuf);	}	if (chunkIdx) {		gdFree(chunkIdx);	}	return 0;}gdImagePtr gdImageCreateFromGd2PartPtr (int size, void *data, int srcx, int srcy, int w, int h){	gdImagePtr im;	gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);	im = gdImageCreateFromGd2PartCtx(in, srcx, srcy, w, h);	in->gd_free(in);	return im;}gdImagePtr gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h){	gdImagePtr im;	gdIOCtx *in = gdNewFileCtx(inFile);	im = gdImageCreateFromGd2PartCtx(in, srcx, srcy, w, h);	in->gd_free(in);	return im;}gdImagePtr gdImageCreateFromGd2PartCtx (gdIOCtx * in, int srcx, int srcy, int w, int h){	int scx, scy, ecx, ecy, fsx, fsy;	int nc, ncx, ncy, cs, cx, cy;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -