gd_gd2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * gd_gd2.c
  3. *
  4. * Implements the I/O and support for the GD2 format.
  5. *
  6. * Changing the definition of GD2_DBG (below) will cause copious messages
  7. * to be displayed while it processes requests.
  8. *
  9. * Designed, Written & Copyright 1999, Philip Warner.
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <math.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "gd.h"
  18. #include "gd_errors.h"
  19. #include "gdhelpers.h"
  20. #include <zlib.h>
  21. #define TRUE 1
  22. #define FALSE 0
  23. /* 2.11: not part of the API, as the save routine can figure it out
  24. * from im->trueColor, and the load routine doesn't need to tell
  25. * the end user the saved format. NOTE: adding 2 is assumed
  26. * to result in the correct format value for truecolor!
  27. */
  28. #define GD2_FMT_TRUECOLOR_RAW 3
  29. #define GD2_FMT_TRUECOLOR_COMPRESSED 4
  30. #define gd2_compressed(fmt) (((fmt) == GD2_FMT_COMPRESSED) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))
  31. #define gd2_truecolor(fmt) (((fmt) == GD2_FMT_TRUECOLOR_RAW) || ((fmt) == GD2_FMT_TRUECOLOR_COMPRESSED))
  32. /* Use this for commenting out debug-print statements. */
  33. /* Just use the first '#define' to allow all the prints... */
  34. /* #define GD2_DBG(s) (s) */
  35. #define GD2_DBG(s)
  36. typedef struct
  37. {
  38. int offset;
  39. int size;
  40. } t_chunk_info;
  41. extern int _gdGetColors(gdIOCtx * in, gdImagePtr im, int gd2xFlag);
  42. extern void _gdPutColors(gdImagePtr im, gdIOCtx * out);
  43. /* */
  44. /* Read the extra info in the gd2 header. */
  45. /* */
  46. static int _gd2GetHeader(gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** chunkIdx)
  47. {
  48. int i;
  49. int ch;
  50. char id[5];
  51. t_chunk_info *cidx;
  52. int sidx;
  53. int nc;
  54. GD2_DBG(gd_error("Reading gd2 header info"));
  55. for (i = 0; i < 4; i++) {
  56. ch = gdGetC(in);
  57. if (ch == EOF) {
  58. goto fail1;
  59. }
  60. id[i] = ch;
  61. }
  62. id[4] = 0;
  63. GD2_DBG(gd_error("Got file code: %s", id));
  64. /* Equiv. of 'magick'. */
  65. if (strcmp(id, GD2_ID) != 0) {
  66. GD2_DBG(gd_error("Not a valid gd2 file"));
  67. goto fail1;
  68. }
  69. /* Version */
  70. if (gdGetWord(vers, in) != 1) {
  71. goto fail1;
  72. }
  73. GD2_DBG(gd_error("Version: %d", *vers));
  74. if ((*vers != 1) && (*vers != 2)) {
  75. GD2_DBG(gd_error("Bad version: %d", *vers));
  76. goto fail1;
  77. }
  78. /* Image Size */
  79. if (!gdGetWord(sx, in)) {
  80. GD2_DBG(gd_error("Could not get x-size"));
  81. goto fail1;
  82. }
  83. if (!gdGetWord(sy, in)) {
  84. GD2_DBG(gd_error("Could not get y-size"));
  85. goto fail1;
  86. }
  87. GD2_DBG(gd_error("Image is %dx%d", *sx, *sy));
  88. /* Chunk Size (pixels, not bytes!) */
  89. if (gdGetWord(cs, in) != 1) {
  90. goto fail1;
  91. }
  92. GD2_DBG(gd_error("ChunkSize: %d", *cs));
  93. if ((*cs < GD2_CHUNKSIZE_MIN) || (*cs > GD2_CHUNKSIZE_MAX)) {
  94. GD2_DBG(gd_error("Bad chunk size: %d", *cs));
  95. goto fail1;
  96. }
  97. /* Data Format */
  98. if (gdGetWord(fmt, in) != 1) {
  99. goto fail1;
  100. }
  101. GD2_DBG(gd_error("Format: %d", *fmt));
  102. if ((*fmt != GD2_FMT_RAW) && (*fmt != GD2_FMT_COMPRESSED) && (*fmt != GD2_FMT_TRUECOLOR_RAW) && (*fmt != GD2_FMT_TRUECOLOR_COMPRESSED)) {
  103. GD2_DBG(gd_error("Bad data format: %d", *fmt));
  104. goto fail1;
  105. }
  106. /* # of chunks wide */
  107. if (gdGetWord(ncx, in) != 1) {
  108. goto fail1;
  109. }
  110. GD2_DBG(gd_error("%d Chunks Wide", *ncx));
  111. /* # of chunks high */
  112. if (gdGetWord(ncy, in) != 1) {
  113. goto fail1;
  114. }
  115. GD2_DBG(gd_error("%d Chunks vertically", *ncy));
  116. if (gd2_compressed(*fmt)) {
  117. if (*ncx <= 0 || *ncy <= 0 || *ncx > INT_MAX / *ncy) {
  118. GD2_DBG(printf ("Illegal chunk counts: %d * %d\n", *ncx, *ncy));
  119. goto fail1;
  120. }
  121. nc = (*ncx) * (*ncy);
  122. GD2_DBG(gd_error("Reading %d chunk index entries", nc));
  123. if (overflow2(sizeof(t_chunk_info), nc)) {
  124. goto fail1;
  125. }
  126. sidx = sizeof(t_chunk_info) * nc;
  127. if (sidx <= 0) {
  128. goto fail1;
  129. }
  130. cidx = gdCalloc(sidx, 1);
  131. if (cidx == NULL) {
  132. goto fail1;
  133. }
  134. for (i = 0; i < nc; i++) {
  135. if (gdGetInt(&cidx[i].offset, in) != 1) {
  136. gdFree(cidx);
  137. goto fail1;
  138. }
  139. if (gdGetInt(&cidx[i].size, in) != 1) {
  140. gdFree(cidx);
  141. goto fail1;
  142. }
  143. if (cidx[i].offset < 0 || cidx[i].size < 0) {
  144. gdFree(cidx);
  145. goto fail1;
  146. }
  147. }
  148. *chunkIdx = cidx;
  149. }
  150. GD2_DBG(gd_error("gd2 header complete"));
  151. return 1;
  152. fail1:
  153. return 0;
  154. }
  155. static gdImagePtr _gd2CreateFromFile (gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** cidx)
  156. {
  157. gdImagePtr im;
  158. if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1) {
  159. GD2_DBG(gd_error("Bad GD2 header"));
  160. goto fail1;
  161. }
  162. if (gd2_truecolor(*fmt)) {
  163. im = gdImageCreateTrueColor(*sx, *sy);
  164. } else {
  165. im = gdImageCreate(*sx, *sy);
  166. }
  167. if (im == NULL) {
  168. GD2_DBG(gd_error("Could not create gdImage"));
  169. goto fail2;
  170. }
  171. if (!_gdGetColors(in, im, (*vers) == 2)) {
  172. GD2_DBG(gd_error("Could not read color palette"));
  173. goto fail3;
  174. }
  175. GD2_DBG(gd_error("Image palette completed: %d colours", im->colorsTotal));
  176. return im;
  177. fail3:
  178. gdImageDestroy(im);
  179. fail2:
  180. gdFree(*cidx);
  181. fail1:
  182. return 0;
  183. }
  184. static int _gd2ReadChunk (int offset, char *compBuf, int compSize, char *chunkBuf, uLongf * chunkLen, gdIOCtx * in)
  185. {
  186. int zerr;
  187. if (gdTell(in) != offset) {
  188. GD2_DBG(gd_error("Positioning in file to %d", offset));
  189. gdSeek(in, offset);
  190. } else {
  191. GD2_DBG(gd_error("Already Positioned in file to %d", offset));
  192. }
  193. /* Read and uncompress an entire chunk. */
  194. GD2_DBG(gd_error("Reading file"));
  195. if (gdGetBuf(compBuf, compSize, in) != compSize) {
  196. return FALSE;
  197. }
  198. GD2_DBG(gd_error("Got %d bytes. Uncompressing into buffer of %d bytes", compSize, (int)*chunkLen));
  199. zerr = uncompress((unsigned char *) chunkBuf, chunkLen, (unsigned char *) compBuf, compSize);
  200. if (zerr != Z_OK) {
  201. GD2_DBG(gd_error("Error %d from uncompress", zerr));
  202. return FALSE;
  203. }
  204. GD2_DBG(gd_error("Got chunk"));
  205. return TRUE;
  206. }
  207. gdImagePtr gdImageCreateFromGd2 (FILE * inFile)
  208. {
  209. gdIOCtx *in = gdNewFileCtx(inFile);
  210. gdImagePtr im;
  211. im = gdImageCreateFromGd2Ctx(in);
  212. in->gd_free(in);
  213. return im;
  214. }
  215. gdImagePtr gdImageCreateFromGd2Ptr (int size, void *data)
  216. {
  217. gdImagePtr im;
  218. gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
  219. im = gdImageCreateFromGd2Ctx(in);
  220. in->gd_free(in);
  221. return im;
  222. }
  223. gdImagePtr gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
  224. {
  225. int sx, sy;
  226. int i;
  227. int ncx, ncy, nc, cs, cx, cy;
  228. int x, y, ylo, yhi, xlo, xhi;
  229. int vers, fmt;
  230. t_chunk_info *chunkIdx = NULL; /* So we can gdFree it with impunity. */
  231. unsigned char *chunkBuf = NULL; /* So we can gdFree it with impunity. */
  232. int chunkNum = 0;
  233. int chunkMax = 0;
  234. uLongf chunkLen;
  235. int chunkPos = 0;
  236. int compMax = 0;
  237. int bytesPerPixel;
  238. char *compBuf = NULL; /* So we can gdFree it with impunity. */
  239. gdImagePtr im;
  240. /* Get the header */
  241. if (!(im = _gd2CreateFromFile(in, &sx, &sy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx))) {
  242. return 0;
  243. }
  244. bytesPerPixel = im->trueColor ? 4 : 1;
  245. nc = ncx * ncy;
  246. if (gd2_compressed(fmt)) {
  247. /* Find the maximum compressed chunk size. */
  248. compMax = 0;
  249. for (i = 0; (i < nc); i++) {
  250. if (chunkIdx[i].size > compMax) {
  251. compMax = chunkIdx[i].size;
  252. }
  253. }
  254. compMax++;
  255. /* Allocate buffers */
  256. chunkMax = cs * bytesPerPixel * cs;
  257. if (chunkMax <= 0) {
  258. return 0;
  259. }
  260. chunkBuf = gdCalloc(chunkMax, 1);
  261. compBuf = gdCalloc(compMax, 1);
  262. GD2_DBG(gd_error("Largest compressed chunk is %d bytes", compMax));
  263. }
  264. /* Read the data... */
  265. for (cy = 0; (cy < ncy); cy++) {
  266. for (cx = 0; (cx < ncx); cx++) {
  267. ylo = cy * cs;
  268. yhi = ylo + cs;
  269. if (yhi > im->sy) {
  270. yhi = im->sy;
  271. }
  272. GD2_DBG(gd_error("Processing Chunk %d (%d, %d), y from %d to %d", chunkNum, cx, cy, ylo, yhi));
  273. if (gd2_compressed(fmt)) {
  274. chunkLen = chunkMax;
  275. if (!_gd2ReadChunk(chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, (char *) chunkBuf, &chunkLen, in)) {
  276. GD2_DBG(gd_error("Error reading comproessed chunk"));
  277. goto fail2;
  278. }
  279. chunkPos = 0;
  280. }
  281. for (y = ylo; (y < yhi); y++) {
  282. xlo = cx * cs;
  283. xhi = xlo + cs;
  284. if (xhi > im->sx) {
  285. xhi = im->sx;
  286. }
  287. if (!gd2_compressed(fmt)) {
  288. for (x = xlo; x < xhi; x++) {
  289. if (im->trueColor) {
  290. if (!gdGetInt(&im->tpixels[y][x], in)) {
  291. gd_error("gd2: EOF while reading\n");
  292. gdImageDestroy(im);
  293. return NULL;
  294. }
  295. } else {
  296. int ch;
  297. if (!gdGetByte(&ch, in)) {
  298. gd_error("gd2: EOF while reading\n");
  299. gdImageDestroy(im);
  300. return NULL;
  301. }
  302. im->pixels[y][x] = ch;
  303. }
  304. }
  305. } else {
  306. for (x = xlo; x < xhi; x++) {
  307. if (im->trueColor) {
  308. /* 2.0.1: work around a gcc bug by being verbose. TBB */
  309. int a = chunkBuf[chunkPos++] << 24;
  310. int r = chunkBuf[chunkPos++] << 16;
  311. int g = chunkBuf[chunkPos++] << 8;
  312. int b = chunkBuf[chunkPos++];
  313. im->tpixels[y][x] = a + r + g + b;
  314. } else {
  315. im->pixels[y][x] = chunkBuf[chunkPos++];
  316. }
  317. }
  318. }
  319. }
  320. chunkNum++;
  321. }
  322. }
  323. GD2_DBG(gd_error("Freeing memory"));
  324. if (chunkBuf) {
  325. gdFree(chunkBuf);
  326. }
  327. if (compBuf) {
  328. gdFree(compBuf);
  329. }
  330. if (chunkIdx) {
  331. gdFree(chunkIdx);
  332. }
  333. GD2_DBG(gd_error("Done"));
  334. return im;
  335. fail2:
  336. gdImageDestroy(im);
  337. if (chunkBuf) {
  338. gdFree(chunkBuf);
  339. }
  340. if (compBuf) {
  341. gdFree(compBuf);
  342. }
  343. if (chunkIdx) {
  344. gdFree(chunkIdx);
  345. }
  346. return 0;
  347. }
  348. gdImagePtr gdImageCreateFromGd2PartPtr (int size, void *data, int srcx, int srcy, int w, int h)
  349. {
  350. gdImagePtr im;
  351. gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
  352. im = gdImageCreateFromGd2PartCtx(in, srcx, srcy, w, h);
  353. in->gd_free(in);
  354. return im;
  355. }
  356. gdImagePtr gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h)
  357. {
  358. gdImagePtr im;
  359. gdIOCtx *in = gdNewFileCtx(inFile);
  360. im = gdImageCreateFromGd2PartCtx(in, srcx, srcy, w, h);
  361. in->gd_free(in);
  362. return im;
  363. }
  364. gdImagePtr gdImageCreateFromGd2PartCtx (gdIOCtx * in, int srcx, int srcy, int w, int h)
  365. {
  366. int scx, scy, ecx, ecy, fsx, fsy;
  367. int nc, ncx, ncy, cs, cx, cy;
  368. int x, y, ylo, yhi, xlo, xhi;
  369. int dstart, dpos;
  370. int i;
  371. /* 2.0.12: unsigned is correct; fixes problems with color munging. Thanks to Steven Brown. */
  372. unsigned int ch;
  373. int vers, fmt;
  374. t_chunk_info *chunkIdx = NULL;
  375. unsigned char *chunkBuf = NULL;
  376. int chunkNum;
  377. int chunkMax = 0;
  378. uLongf chunkLen;
  379. int chunkPos = 0;
  380. int compMax;
  381. char *compBuf = NULL;
  382. gdImagePtr im;
  383. if (w<1 || h <1) {
  384. return 0;
  385. }
  386. /* The next few lines are basically copied from gd2CreateFromFile
  387. * we change the file size, so don't want to use the code directly.
  388. * but we do need to know the file size.
  389. */
  390. if (_gd2GetHeader(in, &fsx, &fsy, &cs, &vers, &fmt, &ncx, &ncy, &chunkIdx) != 1) {
  391. goto fail1;
  392. }
  393. GD2_DBG(gd_error("File size is %dx%d", fsx, fsy));
  394. /* This is the difference - make a file based on size of chunks. */
  395. if (gd2_truecolor(fmt)) {
  396. im = gdImageCreateTrueColor(w, h);
  397. } else {
  398. im = gdImageCreate(w, h);
  399. }
  400. if (im == NULL) {
  401. goto fail1;
  402. }
  403. if (!_gdGetColors(in, im, vers == 2)) {
  404. goto fail2;
  405. }
  406. GD2_DBG(gd_error("Image palette completed: %d colours", im->colorsTotal));
  407. /* Process the header info */
  408. nc = ncx * ncy;
  409. if (gd2_compressed(fmt)) {
  410. /* Find the maximum compressed chunk size. */
  411. compMax = 0;
  412. for (i = 0; (i < nc); i++) {
  413. if (chunkIdx[i].size > compMax) {
  414. compMax = chunkIdx[i].size;
  415. }
  416. }
  417. compMax++;
  418. if (im->trueColor) {
  419. chunkMax = cs * cs * 4;
  420. } else {
  421. chunkMax = cs * cs;
  422. }
  423. if (chunkMax <= 0) {
  424. goto fail2;
  425. }
  426. chunkBuf = gdCalloc(chunkMax, 1);
  427. compBuf = gdCalloc(compMax, 1);
  428. }
  429. /* Work out start/end chunks */
  430. scx = srcx / cs;
  431. scy = srcy / cs;
  432. if (scx < 0) {
  433. scx = 0;
  434. }
  435. if (scy < 0) {
  436. scy = 0;
  437. }
  438. ecx = (srcx + w) / cs;
  439. ecy = (srcy + h) / cs;
  440. if (ecx >= ncx) {
  441. ecx = ncx - 1;
  442. }
  443. if (ecy >= ncy) {
  444. ecy = ncy - 1;
  445. }
  446. /* Remember file position of image data. */
  447. dstart = gdTell(in);
  448. GD2_DBG(gd_error("Data starts at %d", dstart));
  449. /* Loop through the chunks. */
  450. for (cy = scy; (cy <= ecy); cy++) {
  451. ylo = cy * cs;
  452. yhi = ylo + cs;
  453. if (yhi > fsy) {
  454. yhi = fsy;
  455. }
  456. for (cx = scx; cx <= ecx; cx++) {
  457. xlo = cx * cs;
  458. xhi = xlo + cs;
  459. if (xhi > fsx) {
  460. xhi = fsx;
  461. }
  462. GD2_DBG(gd_error("Processing Chunk (%d, %d), from %d to %d", cx, cy, ylo, yhi));
  463. if (!gd2_compressed(fmt)) {
  464. GD2_DBG(gd_error("Using raw format data"));
  465. if (im->trueColor) {
  466. dpos = (cy * (cs * fsx) * 4 + cx * cs * (yhi - ylo) * 4) + dstart;
  467. } else {
  468. dpos = cy * (cs * fsx) + cx * cs * (yhi - ylo) + dstart;
  469. }
  470. /* gd 2.0.11: gdSeek returns TRUE on success, not 0. Longstanding bug. 01/16/03 */
  471. if (!gdSeek(in, dpos)) {
  472. gd_error_ex(E_WARNING, "Error from seek: %d", errno);
  473. goto fail2;
  474. }
  475. GD2_DBG(gd_error("Reading (%d, %d) from position %d", cx, cy, dpos - dstart));
  476. } else {
  477. chunkNum = cx + cy * ncx;
  478. chunkLen = chunkMax;
  479. if (!_gd2ReadChunk (chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, (char *)chunkBuf, &chunkLen, in)) {
  480. gd_error("Error reading comproessed chunk");
  481. goto fail2;
  482. }
  483. chunkPos = 0;
  484. GD2_DBG(gd_error("Reading (%d, %d) from chunk %d", cx, cy, chunkNum));
  485. }
  486. GD2_DBG(gd_error(" into (%d, %d) - (%d, %d)", xlo, ylo, xhi, yhi));
  487. for (y = ylo; (y < yhi); y++) {
  488. for (x = xlo; x < xhi; x++) {
  489. if (!gd2_compressed(fmt)) {
  490. if (im->trueColor) {
  491. if (!gdGetInt((int *)&ch, in)) {
  492. ch = 0;
  493. }
  494. } else {
  495. ch = gdGetC(in);
  496. if ((int)ch == EOF) {
  497. ch = 0;
  498. }
  499. }
  500. } else {
  501. if (im->trueColor) {
  502. ch = chunkBuf[chunkPos++];
  503. ch = (ch << 8) + chunkBuf[chunkPos++];
  504. ch = (ch << 8) + chunkBuf[chunkPos++];
  505. ch = (ch << 8) + chunkBuf[chunkPos++];
  506. } else {
  507. ch = chunkBuf[chunkPos++];
  508. }
  509. }
  510. /* Only use a point that is in the image. */
  511. if ((x >= srcx) && (x < (srcx + w)) && (x < fsx) && (x >= 0) && (y >= srcy) && (y < (srcy + h)) && (y < fsy) && (y >= 0)) {
  512. if (im->trueColor) {
  513. im->tpixels[y - srcy][x - srcx] = ch;
  514. } else {
  515. im->pixels[y - srcy][x - srcx] = ch;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. if (chunkBuf) {
  523. gdFree(chunkBuf);
  524. }
  525. if (compBuf) {
  526. gdFree(compBuf);
  527. }
  528. if (chunkIdx) {
  529. gdFree(chunkIdx);
  530. }
  531. return im;
  532. fail2:
  533. gdImageDestroy(im);
  534. fail1:
  535. if (chunkBuf) {
  536. gdFree(chunkBuf);
  537. }
  538. if (compBuf) {
  539. gdFree(compBuf);
  540. }
  541. if (chunkIdx) {
  542. gdFree(chunkIdx);
  543. }
  544. return 0;
  545. }
  546. static void _gd2PutHeader (gdImagePtr im, gdIOCtx * out, int cs, int fmt, int cx, int cy)
  547. {
  548. int i;
  549. /* Send the gd2 id, to verify file format. */
  550. for (i = 0; i < 4; i++) {
  551. gdPutC((unsigned char) (GD2_ID[i]), out);
  552. }
  553. /* We put the version info first, so future versions can easily change header info. */
  554. gdPutWord(GD2_VERS, out);
  555. gdPutWord(im->sx, out);
  556. gdPutWord(im->sy, out);
  557. gdPutWord(cs, out);
  558. gdPutWord(fmt, out);
  559. gdPutWord(cx, out);
  560. gdPutWord(cy, out);
  561. }
  562. static void _gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt)
  563. {
  564. int ncx, ncy, cx, cy;
  565. int x, y, ylo, yhi, xlo, xhi;
  566. int chunkLen;
  567. int chunkNum = 0;
  568. char *chunkData = NULL; /* So we can gdFree it with impunity. */
  569. char *compData = NULL; /* So we can gdFree it with impunity. */
  570. uLongf compLen;
  571. int idxPos = 0;
  572. int idxSize;
  573. t_chunk_info *chunkIdx = NULL; /* So we can gdFree it with impunity. */
  574. int posSave;
  575. int bytesPerPixel = im->trueColor ? 4 : 1;
  576. int compMax = 0;
  577. /* Force fmt to a valid value since we don't return anything. */
  578. if ((fmt != GD2_FMT_RAW) && (fmt != GD2_FMT_COMPRESSED)) {
  579. fmt = GD2_FMT_COMPRESSED;
  580. }
  581. if (im->trueColor) {
  582. fmt += 2;
  583. }
  584. /* Make sure chunk size is valid. These are arbitrary values; 64 because it seems
  585. * a little silly to expect performance improvements on a 64x64 bit scale, and
  586. * 4096 because we buffer one chunk, and a 16MB buffer seems a little large - it may be
  587. * OK for one user, but for another to read it, they require the buffer.
  588. */
  589. if (cs == 0) {
  590. cs = GD2_CHUNKSIZE;
  591. } else if (cs < GD2_CHUNKSIZE_MIN) {
  592. cs = GD2_CHUNKSIZE_MIN;
  593. } else if (cs > GD2_CHUNKSIZE_MAX) {
  594. cs = GD2_CHUNKSIZE_MAX;
  595. }
  596. /* Work out number of chunks. */
  597. ncx = (im->sx + cs - 1) / cs;
  598. ncy = (im->sy + cs - 1) / cs;
  599. /* Write the standard header. */
  600. _gd2PutHeader (im, out, cs, fmt, ncx, ncy);
  601. if (gd2_compressed(fmt)) {
  602. /* Work out size of buffer for compressed data, If CHUNKSIZE is large,
  603. * then these will be large!
  604. */
  605. /* The zlib notes say output buffer size should be (input size) * 1.01 * 12
  606. * - we'll use 1.02 to be paranoid.
  607. */
  608. compMax = (int)(cs * bytesPerPixel * cs * 1.02f) + 12;
  609. /* Allocate the buffers. */
  610. chunkData = safe_emalloc(cs * bytesPerPixel, cs, 0);
  611. memset(chunkData, 0, cs * bytesPerPixel * cs);
  612. if (compMax <= 0) {
  613. goto fail;
  614. }
  615. compData = gdCalloc(compMax, 1);
  616. /* Save the file position of chunk index, and allocate enough space for
  617. * each chunk_info block .
  618. */
  619. idxPos = gdTell(out);
  620. idxSize = ncx * ncy * sizeof(t_chunk_info);
  621. GD2_DBG(gd_error("Index size is %d", idxSize));
  622. gdSeek(out, idxPos + idxSize);
  623. chunkIdx = safe_emalloc(idxSize, sizeof(t_chunk_info), 0);
  624. memset(chunkIdx, 0, idxSize * sizeof(t_chunk_info));
  625. }
  626. _gdPutColors (im, out);
  627. GD2_DBG(gd_error("Size: %dx%d", im->sx, im->sy));
  628. GD2_DBG(gd_error("Chunks: %dx%d", ncx, ncy));
  629. for (cy = 0; (cy < ncy); cy++) {
  630. for (cx = 0; (cx < ncx); cx++) {
  631. ylo = cy * cs;
  632. yhi = ylo + cs;
  633. if (yhi > im->sy) {
  634. yhi = im->sy;
  635. }
  636. GD2_DBG(gd_error("Processing Chunk (%dx%d), y from %d to %d", cx, cy, ylo, yhi));
  637. chunkLen = 0;
  638. for (y = ylo; (y < yhi); y++) {
  639. GD2_DBG(gd_error("y=%d: ",y));
  640. xlo = cx * cs;
  641. xhi = xlo + cs;
  642. if (xhi > im->sx) {
  643. xhi = im->sx;
  644. }
  645. if (gd2_compressed(fmt)) {
  646. for (x = xlo; x < xhi; x++) {
  647. GD2_DBG(gd_error("%d...",x));
  648. if (im->trueColor) {
  649. int p = im->tpixels[y][x];
  650. chunkData[chunkLen++] = gdTrueColorGetAlpha(p);
  651. chunkData[chunkLen++] = gdTrueColorGetRed(p);
  652. chunkData[chunkLen++] = gdTrueColorGetGreen(p);
  653. chunkData[chunkLen++] = gdTrueColorGetBlue(p);
  654. } else {
  655. chunkData[chunkLen++] = im->pixels[y][x];
  656. }
  657. }
  658. } else {
  659. for (x = xlo; x < xhi; x++) {
  660. GD2_DBG(gd_error("%d, ",x));
  661. if (im->trueColor) {
  662. gdPutInt(im->tpixels[y][x], out);
  663. } else {
  664. gdPutC((unsigned char) im->pixels[y][x], out);
  665. }
  666. }
  667. }
  668. GD2_DBG(gd_error("y=%d done.",y));
  669. }
  670. if (gd2_compressed(fmt)) {
  671. compLen = compMax;
  672. if (compress((unsigned char *) &compData[0], &compLen, (unsigned char *) &chunkData[0], chunkLen) != Z_OK) {
  673. gd_error("Error from compressing");
  674. } else {
  675. chunkIdx[chunkNum].offset = gdTell(out);
  676. chunkIdx[chunkNum++].size = compLen;
  677. GD2_DBG(gd_error("Chunk %d size %d offset %d", chunkNum, chunkIdx[chunkNum - 1].size, chunkIdx[chunkNum - 1].offset));
  678. if (gdPutBuf (compData, compLen, out) <= 0) {
  679. /* Any alternate suggestions for handling this? */
  680. gd_error_ex(E_WARNING, "Error %d on write", errno);
  681. }
  682. }
  683. }
  684. }
  685. }
  686. if (gd2_compressed(fmt)) {
  687. /* Save the position, write the index, restore position (paranoia). */
  688. GD2_DBG(gd_error("Seeking %d to write index", idxPos));
  689. posSave = gdTell(out);
  690. gdSeek(out, idxPos);
  691. GD2_DBG(gd_error("Writing index"));
  692. for (x = 0; x < chunkNum; x++) {
  693. GD2_DBG(gd_error("Chunk %d size %d offset %d", x, chunkIdx[x].size, chunkIdx[x].offset));
  694. gdPutInt(chunkIdx[x].offset, out);
  695. gdPutInt(chunkIdx[x].size, out);
  696. }
  697. gdSeek(out, posSave);
  698. }
  699. fail:
  700. GD2_DBG(gd_error("Freeing memory"));
  701. if (chunkData) {
  702. gdFree(chunkData);
  703. }
  704. if (compData) {
  705. gdFree(compData);
  706. }
  707. if (chunkIdx) {
  708. gdFree(chunkIdx);
  709. }
  710. GD2_DBG(gd_error("Done"));
  711. }
  712. void gdImageGd2 (gdImagePtr im, FILE * outFile, int cs, int fmt)
  713. {
  714. gdIOCtx *out = gdNewFileCtx(outFile);
  715. _gdImageGd2(im, out, cs, fmt);
  716. out->gd_free(out);
  717. }
  718. void *gdImageGd2Ptr (gdImagePtr im, int cs, int fmt, int *size)
  719. {
  720. void *rv;
  721. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  722. _gdImageGd2(im, out, cs, fmt);
  723. rv = gdDPExtractData(out, size);
  724. out->gd_free(out);
  725. return rv;
  726. }