4
* Implements the I/O and support for the GD2 format.
6
* Changing the definition of GD2_DBG (below) will cause copious messages
7
* to be displayed while it processes requests.
9
* Designed, Written & Copyright 1999, Philip Warner.
20
#include "gdhelpers.h"
25
/* Use this for commenting out debug-print statements. */
26
/* Just use the first '#define' to allow all the prints... */
27
/*#define GD2_DBG(s) (s) */
37
extern int _gdGetColors (gdIOCtx * in, gdImagePtr im, int gd2xFlag);
38
extern void _gdPutColors (gdImagePtr im, gdIOCtx * out);
41
/* Read the extra info in the gd2 header. */
45
_gd2GetHeader (gdIOCtxPtr in, int *sx, int *sy,
46
int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** chunkIdx)
55
GD2_DBG (printf ("Reading gd2 header info\n"));
57
for (i = 0; i < 4; i++)
68
GD2_DBG (printf ("Got file code: %s\n", id));
70
/* Equiv. of 'magick'. */
71
if (strcmp (id, GD2_ID) != 0)
73
GD2_DBG (printf ("Not a valid gd2 file\n"));
78
if (gdGetWord (vers, in) != 1)
82
GD2_DBG (printf ("Version: %d\n", *vers));
84
if ((*vers != 1) && (*vers != 2))
86
GD2_DBG (printf ("Bad version: %d\n", *vers));
91
if (!gdGetWord (sx, in))
93
GD2_DBG (printf ("Could not get x-size\n"));
96
if (!gdGetWord (sy, in))
98
GD2_DBG (printf ("Could not get y-size\n"));
101
GD2_DBG (printf ("Image is %dx%d\n", *sx, *sy));
103
/* Chunk Size (pixels, not bytes!) */
104
if (gdGetWord (cs, in) != 1)
108
GD2_DBG (printf ("ChunkSize: %d\n", *cs));
110
if ((*cs < GD2_CHUNKSIZE_MIN) || (*cs > GD2_CHUNKSIZE_MAX))
112
GD2_DBG (printf ("Bad chunk size: %d\n", *cs));
117
if (gdGetWord (fmt, in) != 1)
121
GD2_DBG (printf ("Format: %d\n", *fmt));
123
if ((*fmt != GD2_FMT_RAW) && (*fmt != GD2_FMT_COMPRESSED))
125
GD2_DBG (printf ("Bad data format: %d\n", *fmt));
130
/* # of chunks wide */
131
if (gdGetWord (ncx, in) != 1)
135
GD2_DBG (printf ("%d Chunks Wide\n", *ncx));
137
/* # of chunks high */
138
if (gdGetWord (ncy, in) != 1)
142
GD2_DBG (printf ("%d Chunks vertically\n", *ncy));
144
if ((*fmt) == GD2_FMT_COMPRESSED)
146
nc = (*ncx) * (*ncy);
147
GD2_DBG (printf ("Reading %d chunk index entries\n", nc));
148
sidx = sizeof (t_chunk_info) * nc;
149
cidx = gdCalloc (sidx, 1);
150
for (i = 0; i < nc; i++)
152
if (gdGetInt (&cidx[i].offset, in) != 1)
156
if (gdGetInt (&cidx[i].size, in) != 1)
164
GD2_DBG (printf ("gd2 header complete\n"));
174
_gd2CreateFromFile (gdIOCtxPtr in, int *sx, int *sy,
175
int *cs, int *vers, int *fmt,
176
int *ncx, int *ncy, t_chunk_info ** cidx)
180
if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1)
182
GD2_DBG (printf ("Bad GD2 header\n"));
186
im = gdImageCreate (*sx, *sy);
189
GD2_DBG (printf ("Could not create gdImage\n"));
193
if (!_gdGetColors (in, im, (*vers) == 2))
195
GD2_DBG (printf ("Could not read color palette\n"));
198
GD2_DBG (printf ("Image palette completed: %d colours\n", im->colorsTotal));
213
_gd2ReadChunk (int offset, char *compBuf, int compSize, char *chunkBuf, uLongf * chunkLen, gdIOCtx * in)
217
if (gdTell (in) != offset)
219
GD2_DBG (printf ("Positioning in file to %d\n", offset));
224
GD2_DBG (printf ("Already Positioned in file to %d\n", offset));
227
/* Read and uncompress an entire chunk. */
228
GD2_DBG (printf ("Reading file\n"));
229
if (gdGetBuf (compBuf, compSize, in) != compSize)
233
GD2_DBG (printf ("Got %d bytes. Uncompressing into buffer of %d bytes\n", compSize, *chunkLen));
234
zerr = uncompress ((unsigned char *) chunkBuf, chunkLen,
235
(unsigned char *) compBuf, compSize);
238
GD2_DBG (printf ("Error %d from uncompress\n", zerr));
241
GD2_DBG (printf ("Got chunk\n"));
246
gdImageCreateFromGd2 (FILE * inFile)
248
gdIOCtx *in = gdNewFileCtx (inFile);
251
im = gdImageCreateFromGd2Ctx (in);
259
gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
263
int ncx, ncy, nc, cs, cx, cy;
264
int x, y, ylo, yhi, xlo, xhi;
266
t_chunk_info *chunkIdx = NULL; /* So we can gdFree it with impunity. */
267
unsigned char *chunkBuf = NULL; /* So we can gdFree it with impunity. */
274
char *compBuf = NULL; /* So we can gdFree it with impunity. */
279
im = _gd2CreateFromFile (in, &sx, &sy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx);
285
bytesPerPixel = im->trueColor ? 4 : 1;
288
if (fmt == GD2_FMT_COMPRESSED)
290
/* Find the maximum compressed chunk size. */
292
for (i = 0; (i < nc); i++)
294
if (chunkIdx[i].size > compMax)
296
compMax = chunkIdx[i].size;
301
/* Allocate buffers */
302
chunkMax = cs * bytesPerPixel * cs;
303
chunkBuf = gdCalloc (chunkMax, 1);
304
compBuf = gdCalloc (compMax, 1);
305
GD2_DBG (printf ("Largest compressed chunk is %d bytes\n", compMax));
308
/* if ( (ncx != sx / cs) || (ncy != sy / cs)) { */
312
/* Read the data... */
313
for (cy = 0; (cy < ncy); cy++)
315
for (cx = 0; (cx < ncx); cx++)
325
GD2_DBG (printf ("Processing Chunk %d (%d, %d), y from %d to %d\n", chunkNum, cx, cy, ylo, yhi));
327
if (fmt == GD2_FMT_COMPRESSED)
332
if (!_gd2ReadChunk (chunkIdx[chunkNum].offset,
334
chunkIdx[chunkNum].size,
335
chunkBuf, &chunkLen, in))
337
GD2_DBG (printf ("Error reading comproessed chunk\n"));
344
for (y = ylo; (y < yhi); y++)
353
/*GD2_DBG(printf("y=%d: ",y)); */
354
if (fmt == GD2_FMT_RAW)
356
for (x = xlo; x < xhi; x++)
361
if (!gdGetInt (&im->tpixels[y][x], in))
363
/*printf("EOF while reading\n"); */
364
/*gdImageDestroy(im); */
366
im->tpixels[y][x] = 0;
372
if (!gdGetByte (&ch, in))
374
/*printf("EOF while reading\n"); */
375
/*gdImageDestroy(im); */
379
im->pixels[y][x] = ch;
385
for (x = xlo; x < xhi; x++)
389
/* 2.0.1: work around a gcc bug by being verbose.
391
int a = chunkBuf[chunkPos++] << 24;
392
int r = chunkBuf[chunkPos++] << 16;
393
int g = chunkBuf[chunkPos++] << 8;
394
int b = chunkBuf[chunkPos++];
395
im->pixels[y][x] = a + r + g + b;
399
im->pixels[y][x] = chunkBuf[chunkPos++];
403
/*GD2_DBG(printf("\n")); */
409
GD2_DBG (printf ("Freeing memory\n"));
415
GD2_DBG (printf ("Done\n"));
429
gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h)
432
gdIOCtx *in = gdNewFileCtx (inFile);
434
im = gdImageCreateFromGd2PartCtx (in, srcx, srcy, w, h);
442
gdImageCreateFromGd2PartCtx (gdIOCtx * in, int srcx, int srcy, int w, int h)
444
int scx, scy, ecx, ecy, fsx, fsy;
445
int nc, ncx, ncy, cs, cx, cy;
446
int x, y, ylo, yhi, xlo, xhi;
450
t_chunk_info *chunkIdx = NULL;
451
char *chunkBuf = NULL;
457
char *compBuf = NULL;
462
/* The next few lines are basically copied from gd2CreateFromFile */
463
/* - we change the file size, so don't want to use the code directly. */
464
/* but we do need to know the file size. */
466
if (_gd2GetHeader (in, &fsx, &fsy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx) != 1)
471
GD2_DBG (printf ("File size is %dx%d\n", fsx, fsy));
473
/* This is the difference - make a file based on size of chunks. */
474
im = gdImageCreate (w, h);
480
if (!_gdGetColors (in, im, vers == 2))
484
GD2_DBG (printf ("Image palette completed: %d colours\n", im->colorsTotal));
486
/* Process the header info */
489
if (fmt == GD2_FMT_COMPRESSED)
491
/* Find the maximum compressed chunk size. */
493
for (i = 0; (i < nc); i++)
495
if (chunkIdx[i].size > compMax)
497
compMax = chunkIdx[i].size;
504
chunkMax = cs * cs * 4;
510
chunkBuf = gdCalloc (chunkMax, 1);
511
compBuf = gdCalloc (compMax, 1);
514
/* Don't bother with this... */
515
/* if ( (ncx != sx / cs) || (ncy != sy / cs)) { */
520
/* Work out start/end chunks */
532
ecx = (srcx + w) / cs;
533
ecy = (srcy + h) / cs;
543
/* Remember file position of image data. */
544
dstart = gdTell (in);
545
GD2_DBG (printf ("Data starts at %d\n", dstart));
547
/* Loop through the chunks. */
548
for (cy = scy; (cy <= ecy); cy++)
558
for (cx = scx; (cx <= ecx); cx++)
568
GD2_DBG (printf ("Processing Chunk (%d, %d), from %d to %d\n", cx, cy, ylo, yhi));
570
if (fmt == GD2_FMT_RAW)
572
GD2_DBG (printf ("Using raw format data\n"));
575
dpos = (cy * (cs * fsx) + cx * cs * (yhi - ylo) * 4) + dstart;
579
dpos = cy * (cs * fsx) + cx * cs * (yhi - ylo) + dstart;
582
if (gdSeek (in, dpos) != 0)
584
printf ("Error from seek: %d\n", errno);
587
GD2_DBG (printf ("Reading (%d, %d) from position %d\n", cx, cy, dpos - dstart));
591
chunkNum = cx + cy * ncx;
594
if (!_gd2ReadChunk (chunkIdx[chunkNum].offset,
596
chunkIdx[chunkNum].size,
597
chunkBuf, &chunkLen, in))
599
printf ("Error reading comproessed chunk\n");
603
GD2_DBG (printf ("Reading (%d, %d) from chunk %d\n", cx, cy, chunkNum));
606
GD2_DBG (printf (" into (%d, %d) - (%d, %d)\n", xlo, ylo, xhi, yhi));
607
for (y = ylo; (y < yhi); y++)
610
for (x = xlo; x < xhi; x++)
612
if (fmt == GD2_FMT_RAW)
616
if (!gdGetInt (&ch, in))
619
/*printf("EOF while reading file\n"); */
629
/*printf("EOF while reading file\n"); */
638
ch = chunkBuf[chunkPos++] << 24 +
639
chunkBuf[chunkPos++] << 16 +
640
chunkBuf[chunkPos++] << 8 +
641
chunkBuf[chunkPos++];
645
ch = chunkBuf[chunkPos++];
649
/* Only use a point that is in the image. */
650
if ((x >= srcx) && (x < (srcx + w)) && (x < fsx) && (x >= 0)
651
&& (y >= srcy) && (y < (srcy + h)) && (y < fsy) && (y >= 0)
654
im->pixels[y - srcy][x - srcx] = ch;
680
_gd2PutHeader (gdImagePtr im, gdIOCtx * out, int cs, int fmt, int cx, int cy)
684
/* Send the gd2 id, to verify file format. */
685
for (i = 0; i < 4; i++)
687
gdPutC ((unsigned char) (GD2_ID[i]), out);
691
/* We put the version info first, so future versions can easily change header info. */
693
gdPutWord (GD2_VERS, out);
694
gdPutWord (im->sx, out);
695
gdPutWord (im->sy, out);
697
gdPutWord (fmt, out);
704
_gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt)
706
int ncx, ncy, cx, cy;
707
int x, y, ylo, yhi, xlo, xhi;
710
char *chunkData = NULL; /* So we can gdFree it with impunity. */
711
char *compData = NULL; /* So we can gdFree it with impunity. */
715
t_chunk_info *chunkIdx = NULL;
717
int bytesPerPixel = im->trueColor ? 4 : 1;
720
/*printf("Trying to write GD2 file\n"); */
723
/* Force fmt to a valid value since we don't return anything. */
725
if ((fmt == 0) || ((fmt != GD2_FMT_RAW) && (fmt != GD2_FMT_COMPRESSED)))
727
fmt = GD2_FMT_COMPRESSED;
731
/* Make sure chunk size is valid. These are arbitrary values; 64 because it seems */
732
/* a little silly to expect performance improvements on a 64x64 bit scale, and */
733
/* 4096 because we buffer one chunk, and a 16MB buffer seems a little largei - it may be */
734
/* OK for one user, but for another to read it, they require the buffer. */
740
else if (cs < GD2_CHUNKSIZE_MIN)
742
cs = GD2_CHUNKSIZE_MIN;
744
else if (cs > GD2_CHUNKSIZE_MAX)
746
cs = GD2_CHUNKSIZE_MAX;
749
/* Work out number of chunks. */
750
ncx = im->sx / cs + 1;
751
ncy = im->sy / cs + 1;
753
/* Write the standard header. */
754
_gd2PutHeader (im, out, cs, fmt, ncx, ncy);
756
if (fmt == GD2_FMT_COMPRESSED)
759
/* Work out size of buffer for compressed data, If CHUNKSIZE is large, */
760
/* then these will be large! */
762
/* The zlib notes say output buffer size should be (input size) * 1.01 * 12 */
763
/* - we'll use 1.02 to be paranoid. */
765
compMax = cs * bytesPerPixel * cs * 1.02 + 12;
768
/* Allocate the buffers. */
770
chunkData = gdCalloc (cs * bytesPerPixel * cs, 1);
771
compData = gdCalloc (compMax, 1);
774
/* Save the file position of chunk index, and allocate enough space for */
775
/* each chunk_info block . */
777
idxPos = gdTell (out);
778
idxSize = ncx * ncy * sizeof (t_chunk_info);
779
GD2_DBG (printf ("Index size is %d\n", idxSize));
780
gdSeek (out, idxPos + idxSize);
782
chunkIdx = gdCalloc (idxSize * sizeof (t_chunk_info), 1);
785
_gdPutColors (im, out);
787
GD2_DBG (printf ("Size: %dx%d\n", im->sx, im->sy));
788
GD2_DBG (printf ("Chunks: %dx%d\n", ncx, ncy));
790
for (cy = 0; (cy < ncy); cy++)
792
for (cx = 0; (cx < ncx); cx++)
802
GD2_DBG (printf ("Processing Chunk (%dx%d), y from %d to %d\n", cx, cy, ylo, yhi));
804
for (y = ylo; (y < yhi); y++)
807
/*GD2_DBG(printf("y=%d: ",y)); */
816
if (fmt == GD2_FMT_COMPRESSED)
818
for (x = xlo; x < xhi; x++)
820
int p = im->pixels[y][x];
821
/*GD2_DBG(printf("%d...",x)); */
824
chunkData[chunkLen++] = gdTrueColorGetAlpha (p);
825
chunkData[chunkLen++] = gdTrueColorGetRed (p);
826
chunkData[chunkLen++] = gdTrueColorGetGreen (p);
827
chunkData[chunkLen++] = gdTrueColorGetBlue (p);
831
chunkData[chunkLen++] = p;
837
for (x = xlo; x < xhi; x++)
839
/*GD2_DBG(printf("%d, ",x)); */
843
gdPutInt (im->tpixels[y][x], out);
847
gdPutC ((unsigned char) im->pixels[y][x], out);
851
/*GD2_DBG(printf("y=%d done.\n",y)); */
853
if (fmt == GD2_FMT_COMPRESSED)
856
if (compress ((unsigned char *)
857
&compData[0], &compLen,
858
(unsigned char *) &chunkData[0],
861
printf ("Error from compressing\n");
865
chunkIdx[chunkNum].offset = gdTell (out);
866
chunkIdx[chunkNum++].size = compLen;
867
GD2_DBG (printf ("Chunk %d size %d offset %d\n", chunkNum, chunkIdx[chunkNum - 1].size, chunkIdx[chunkNum - 1].offset));
869
if (gdPutBuf (compData, compLen, out) <= 0)
871
/* Any alternate suggestions for handling this? */
872
printf ("Error %d on write\n", errno);
878
if (fmt == GD2_FMT_COMPRESSED)
880
/* Save the position, write the index, restore position (paranoia). */
881
GD2_DBG (printf ("Seeking %d to write index\n", idxPos));
882
posSave = gdTell (out);
883
gdSeek (out, idxPos);
884
GD2_DBG (printf ("Writing index\n"));
885
for (x = 0; x < chunkNum; x++)
887
GD2_DBG (printf ("Chunk %d size %d offset %d\n", x, chunkIdx[x].size, chunkIdx[x].offset));
888
gdPutInt (chunkIdx[x].offset, out);
889
gdPutInt (chunkIdx[x].size, out);
891
/* We don't use fwrite for *endian reasons. */
892
/*fwrite(chunkIdx, sizeof(int)*2, chunkNum, out); */
893
gdSeek (out, posSave);
896
GD2_DBG (printf ("Freeing memory\n"));
900
GD2_DBG (printf ("Done\n"));
902
/*printf("Memory block size is %d\n",gdTell(out)); */
907
gdImageGd2 (gdImagePtr im, FILE * outFile, int cs, int fmt)
909
gdIOCtx *out = gdNewFileCtx (outFile);
910
_gdImageGd2 (im, out, cs, fmt);
915
gdImageGd2Ptr (gdImagePtr im, int cs, int fmt, int *size)
918
gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
919
_gdImageGd2 (im, out, cs, fmt);
920
rv = gdDPExtractData (out, size);