gd_gd2.c 20 KB

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