32#define GD2_FMT_TRUECOLOR_RAW 3
33#define GD2_FMT_TRUECOLOR_COMPRESSED 4
35#define gd2_compressed(fmt) (((fmt) == GD2_FMT_COMPRESSED) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))
36#define gd2_truecolor(fmt) (((fmt) == GD2_FMT_TRUECOLOR_RAW) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))
55static int _gd2GetHeader(
gdIOCtxPtr in,
int *sx,
int *sy,
int *cs,
int *vers,
int *fmt,
int *ncx,
int *ncy,
t_chunk_info ** chunkIdx)
66 for (i = 0; i < 4; i++) {
89 if ((*vers != 1) && (*vers != 2)) {
141 GD2_DBG(
printf (
"Illegal chunk counts: %d * %d\n", *ncx, *ncy));
144 nc = (*ncx) * (*ncy);
158 for (i = 0; i < nc; i++) {
187 if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1) {
218static int _gd2ReadChunk (
int offset,
char *compBuf,
int compSize,
char *chunkBuf, uLongf * chunkLen,
gdIOCtx * in)
231 if (
gdGetBuf(compBuf, compSize, in) != compSize) {
234 GD2_DBG(
gd_error(
"Got %d bytes. Uncompressing into buffer of %d bytes", compSize, (
int)*chunkLen));
235 zerr = uncompress((
unsigned char *) chunkBuf, chunkLen, (
unsigned char *) compBuf, compSize);
271 int ncx, ncy, nc, cs, cx, cy;
272 int x, y, ylo, yhi, xlo, xhi;
275 unsigned char *chunkBuf =
NULL;
282 char *compBuf =
NULL;
287 if (!(im = _gd2CreateFromFile(in, &sx, &sy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx))) {
297 for (i = 0; (i < nc); i++) {
298 if (chunkIdx[i].
size > compMax) {
299 compMax = chunkIdx[i].
size;
305 chunkMax = cs * bytesPerPixel * cs;
316 for (cy = 0; (cy < ncy); cy++) {
317 for (cx = 0; (cx < ncx); cx++) {
324 GD2_DBG(
gd_error(
"Processing Chunk %d (%d, %d), y from %d to %d", chunkNum, cx, cy, ylo, yhi));
329 if (!_gd2ReadChunk(chunkIdx[chunkNum].
offset, compBuf, chunkIdx[chunkNum].
size, (
char *) chunkBuf, &chunkLen, in)) {
337 for (y = ylo; (y < yhi); y++) {
345 for (x = xlo; x < xhi; x++) {
348 gd_error(
"gd2: EOF while reading\n");
355 gd_error(
"gd2: EOF while reading\n");
363 for (x = xlo; x < xhi; x++) {
366 int a = chunkBuf[chunkPos++] << 24;
367 int r = chunkBuf[chunkPos++] << 16;
368 int g = chunkBuf[chunkPos++] << 8;
369 int b = chunkBuf[chunkPos++];
372 im->
pixels[y][x] = chunkBuf[chunkPos++];
436 int scx, scy, ecx, ecy, fsx, fsy;
437 int nc, ncx, ncy, cs, cx, cy;
438 int x, y, ylo, yhi, xlo, xhi;
445 unsigned char *chunkBuf =
NULL;
451 char *compBuf =
NULL;
463 if (_gd2GetHeader(in, &fsx, &fsy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx) != 1) {
490 for (i = 0; (i < nc); i++) {
491 if (chunkIdx[i].
size > compMax) {
492 compMax = chunkIdx[i].
size;
498 chunkMax = cs * cs * 4;
520 ecx = (srcx + w) / cs;
521 ecy = (srcy + h) / cs;
534 for (cy = scy; (cy <= ecy); cy++) {
541 for (cx = scx; cx <= ecx; cx++) {
549 GD2_DBG(
gd_error(
"Processing Chunk (%d, %d), from %d to %d", cx, cy, ylo, yhi));
554 dpos = (cy * (cs * fsx) * 4 + cx * cs * (yhi - ylo) * 4) + dstart;
556 dpos = cy * (cs * fsx) + cx * cs * (yhi - ylo) + dstart;
564 GD2_DBG(
gd_error(
"Reading (%d, %d) from position %d", cx, cy, dpos - dstart));
566 chunkNum = cx + cy * ncx;
569 if (!_gd2ReadChunk (chunkIdx[chunkNum].
offset, compBuf, chunkIdx[chunkNum].
size, (
char *)chunkBuf, &chunkLen, in)) {
570 gd_error(
"Error reading comproessed chunk");
579 for (y = ylo; (y < yhi); y++) {
580 for (x = xlo; x < xhi; x++) {
588 if ((
int)
ch == EOF) {
594 ch = chunkBuf[chunkPos++];
595 ch = (
ch << 8) + chunkBuf[chunkPos++];
596 ch = (
ch << 8) + chunkBuf[chunkPos++];
597 ch = (
ch << 8) + chunkBuf[chunkPos++];
599 ch = chunkBuf[chunkPos++];
604 if ((x >= srcx) && (x < (srcx + w)) && (x < fsx) && (x >= 0) && (y >= srcy) && (y < (srcy + h)) && (y < fsy) && (y >= 0)) {
608 im->
pixels[y - srcy][x - srcx] =
ch;
649 for (i = 0; i < 4; i++) {
666 int ncx, ncy, cx, cy;
667 int x, y, ylo, yhi, xlo, xhi;
670 char *chunkData =
NULL;
671 char *compData =
NULL;
677 int bytesPerPixel = im->
trueColor ? 4 : 1;
701 ncx = (im->
sx + cs - 1) / cs;
702 ncy = (im->
sy + cs - 1) / cs;
705 _gd2PutHeader (im,
out, cs, fmt, ncx, ncy);
715 compMax = (int)(cs * bytesPerPixel * cs * 1.02f) + 12;
719 memset(chunkData, 0, cs * bytesPerPixel * cs);
742 for (cy = 0; (cy < ncy); cy++) {
743 for (cx = 0; (cx < ncx); cx++) {
750 GD2_DBG(
gd_error(
"Processing Chunk (%dx%d), y from %d to %d", cx, cy, ylo, yhi));
752 for (y = ylo; (y < yhi); y++) {
761 for (x = xlo; x < xhi; x++) {
770 chunkData[chunkLen++] = im->
pixels[y][x];
774 for (x = xlo; x < xhi; x++) {
789 if (
compress((
unsigned char *) &compData[0], &compLen, (
unsigned char *) &chunkData[0], chunkLen) != Z_OK) {
793 chunkIdx[chunkNum++].
size = compLen;
811 for (x = 0; x < chunkNum; x++) {
836 _gdImageGd2(im,
out, cs, fmt);
846 _gdImageGd2(im,
out, cs, fmt);
printf(string $format, mixed ... $values)
memset(ptr, 0, type->size)
int overflow2(int a, int b)
#define gdTrueColorGetBlue(c)
void * gdDPExtractData(struct gdIOCtx *ctx, int *size)
#define gdTrueColorGetGreen(c)
#define GD2_CHUNKSIZE_MAX
#define gdTrueColorGetAlpha(c)
#define GD2_CHUNKSIZE_MIN
#define gdTrueColorGetRed(c)
gdIOCtx * gdNewFileCtx(FILE *)
gdIOCtx * gdNewDynamicCtx(int, void *)
#define GD2_FMT_COMPRESSED
gdIOCtx * gdNewDynamicCtxEx(int size, void *data, int freeFlag)
#define gd2_truecolor(fmt)
gdImagePtr gdImageCreateFromGd2Ctx(gdIOCtxPtr in)
int _gdGetColors(gdIOCtx *in, gdImagePtr im, int gd2xFlag)
#define gd2_compressed(fmt)
gdImagePtr gdImageCreateFromGd2Ptr(int size, void *data)
gdImagePtr gdImageCreateFromGd2Part(FILE *inFile, int srcx, int srcy, int w, int h)
gdImagePtr gdImageCreateFromGd2(FILE *inFile)
void gdImageGd2(gdImagePtr im, FILE *outFile, int cs, int fmt)
gdImagePtr gdImageCreateFromGd2PartPtr(int size, void *data, int srcx, int srcy, int w, int h)
void _gdPutColors(gdImagePtr im, gdIOCtx *out)
#define GD2_FMT_TRUECOLOR_RAW
#define GD2_FMT_TRUECOLOR_COMPRESSED
gdImagePtr gdImageCreateFromGd2PartCtx(gdIOCtx *in, int srcx, int srcy, int w, int h)
void * gdImageGd2Ptr(gdImagePtr im, int cs, int fmt, int *size)
void gdPutInt(int w, gdIOCtx *ctx)
long gdTell(gdIOCtx *ctx)
int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
int gdSeek(gdIOCtx *ctx, const int pos)
int gdGetByte(int *result, gdIOCtx *ctx)
int gdGetInt(int *result, gdIOCtx *ctx)
int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
int gdGetWord(int *result, gdIOCtx *ctx)
void gdPutWord(int w, gdIOCtx *ctx)
void gdPutC(const unsigned char c, gdIOCtx *ctx)
struct gdIOCtx * gdIOCtxPtr
#define gdCalloc(nmemb, size)
gdImagePtr gdImageCreate(int sx, int sy)
void gdImageDestroy(gdImagePtr im)
void gd_error(const char *format,...)
void gd_error_ex(int priority, const char *format,...)
gdImagePtr gdImageCreateTrueColor(int sx, int sy)
void(* gd_free)(struct gdIOCtx *)
#define safe_emalloc(nmemb, size, offset)
strcmp(string $string1, string $string2)