pngread.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /* pngread.c - read a PNG file
  2. *
  3. * Last changed in libpng 1.5.7 [December 15, 2011]
  4. * Copyright (c) 1998-2011 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that an application calls directly to
  13. * read a PNG file or stream.
  14. */
  15. #include "pngpriv.h"
  16. #ifdef PNG_READ_SUPPORTED
  17. /* Create a PNG structure for reading, and allocate any memory needed. */
  18. PNG_FUNCTION(png_structp,PNGAPI
  19. png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  20. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  21. {
  22. #ifdef PNG_USER_MEM_SUPPORTED
  23. return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  24. warn_fn, NULL, NULL, NULL));
  25. }
  26. /* Alternate create PNG structure for reading, and allocate any memory
  27. * needed.
  28. */
  29. PNG_FUNCTION(png_structp,PNGAPI
  30. png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  31. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  32. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  33. {
  34. #endif /* PNG_USER_MEM_SUPPORTED */
  35. #ifdef PNG_SETJMP_SUPPORTED
  36. volatile
  37. #endif
  38. png_structp png_ptr;
  39. volatile int png_cleanup_needed = 0;
  40. #ifdef PNG_SETJMP_SUPPORTED
  41. #ifdef USE_FAR_KEYWORD
  42. jmp_buf tmp_jmpbuf;
  43. #endif
  44. #endif
  45. png_debug(1, "in png_create_read_struct");
  46. #ifdef PNG_USER_MEM_SUPPORTED
  47. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  48. malloc_fn, mem_ptr);
  49. #else
  50. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  51. #endif
  52. if (png_ptr == NULL)
  53. return (NULL);
  54. /* Added at libpng-1.2.6 */
  55. #ifdef PNG_USER_LIMITS_SUPPORTED
  56. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  57. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  58. # ifdef PNG_USER_CHUNK_CACHE_MAX
  59. /* Added at libpng-1.2.43 and 1.4.0 */
  60. png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
  61. # endif
  62. # ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
  63. /* Added at libpng-1.2.43 and 1.4.1 */
  64. png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
  65. # endif
  66. #endif
  67. #ifdef PNG_SETJMP_SUPPORTED
  68. /* Applications that neglect to set up their own setjmp() and then
  69. * encounter a png_error() will longjmp here. Since the jmpbuf is
  70. * then meaningless we abort instead of returning.
  71. */
  72. #ifdef USE_FAR_KEYWORD
  73. if (setjmp(tmp_jmpbuf))
  74. #else
  75. if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
  76. #endif
  77. PNG_ABORT();
  78. #ifdef USE_FAR_KEYWORD
  79. png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
  80. #endif
  81. #endif /* PNG_SETJMP_SUPPORTED */
  82. #ifdef PNG_USER_MEM_SUPPORTED
  83. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  84. #endif
  85. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  86. /* Call the general version checker (shared with read and write code): */
  87. if (!png_user_version_check(png_ptr, user_png_ver))
  88. png_cleanup_needed = 1;
  89. if (!png_cleanup_needed)
  90. {
  91. /* Initialize zbuf - compression buffer */
  92. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  93. png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
  94. if (png_ptr->zbuf == NULL)
  95. png_cleanup_needed = 1;
  96. }
  97. png_ptr->zstream.zalloc = png_zalloc;
  98. png_ptr->zstream.zfree = png_zfree;
  99. png_ptr->zstream.opaque = (voidpf)png_ptr;
  100. if (!png_cleanup_needed)
  101. {
  102. switch (inflateInit(&png_ptr->zstream))
  103. {
  104. case Z_OK:
  105. break; /* Do nothing */
  106. case Z_MEM_ERROR:
  107. png_warning(png_ptr, "zlib memory error");
  108. png_cleanup_needed = 1;
  109. break;
  110. case Z_STREAM_ERROR:
  111. png_warning(png_ptr, "zlib stream error");
  112. png_cleanup_needed = 1;
  113. break;
  114. case Z_VERSION_ERROR:
  115. png_warning(png_ptr, "zlib version error");
  116. png_cleanup_needed = 1;
  117. break;
  118. default: png_warning(png_ptr, "Unknown zlib error");
  119. png_cleanup_needed = 1;
  120. }
  121. }
  122. if (png_cleanup_needed)
  123. {
  124. /* Clean up PNG structure and deallocate any memory. */
  125. png_free(png_ptr, png_ptr->zbuf);
  126. png_ptr->zbuf = NULL;
  127. #ifdef PNG_USER_MEM_SUPPORTED
  128. png_destroy_struct_2((png_voidp)png_ptr,
  129. (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  130. #else
  131. png_destroy_struct((png_voidp)png_ptr);
  132. #endif
  133. return (NULL);
  134. }
  135. png_ptr->zstream.next_out = png_ptr->zbuf;
  136. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  137. png_set_read_fn(png_ptr, NULL, NULL);
  138. return (png_ptr);
  139. }
  140. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  141. /* Read the information before the actual image data. This has been
  142. * changed in v0.90 to allow reading a file that already has the magic
  143. * bytes read from the stream. You can tell libpng how many bytes have
  144. * been read from the beginning of the stream (up to the maximum of 8)
  145. * via png_set_sig_bytes(), and we will only check the remaining bytes
  146. * here. The application can then have access to the signature bytes we
  147. * read if it is determined that this isn't a valid PNG file.
  148. */
  149. void PNGAPI
  150. png_read_info(png_structp png_ptr, png_infop info_ptr)
  151. {
  152. png_debug(1, "in png_read_info");
  153. if (png_ptr == NULL || info_ptr == NULL)
  154. return;
  155. /* Read and check the PNG file signature. */
  156. png_read_sig(png_ptr, info_ptr);
  157. for (;;)
  158. {
  159. png_uint_32 length = png_read_chunk_header(png_ptr);
  160. png_uint_32 chunk_name = png_ptr->chunk_name;
  161. /* This should be a binary subdivision search or a hash for
  162. * matching the chunk name rather than a linear search.
  163. */
  164. if (chunk_name == png_IDAT)
  165. if (png_ptr->mode & PNG_AFTER_IDAT)
  166. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  167. if (chunk_name == png_IHDR)
  168. png_handle_IHDR(png_ptr, info_ptr, length);
  169. else if (chunk_name == png_IEND)
  170. png_handle_IEND(png_ptr, info_ptr, length);
  171. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  172. else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
  173. PNG_HANDLE_CHUNK_AS_DEFAULT)
  174. {
  175. if (chunk_name == png_IDAT)
  176. png_ptr->mode |= PNG_HAVE_IDAT;
  177. png_handle_unknown(png_ptr, info_ptr, length);
  178. if (chunk_name == png_PLTE)
  179. png_ptr->mode |= PNG_HAVE_PLTE;
  180. else if (chunk_name == png_IDAT)
  181. {
  182. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  183. png_error(png_ptr, "Missing IHDR before IDAT");
  184. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  185. !(png_ptr->mode & PNG_HAVE_PLTE))
  186. png_error(png_ptr, "Missing PLTE before IDAT");
  187. break;
  188. }
  189. }
  190. #endif
  191. else if (chunk_name == png_PLTE)
  192. png_handle_PLTE(png_ptr, info_ptr, length);
  193. else if (chunk_name == png_IDAT)
  194. {
  195. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  196. png_error(png_ptr, "Missing IHDR before IDAT");
  197. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  198. !(png_ptr->mode & PNG_HAVE_PLTE))
  199. png_error(png_ptr, "Missing PLTE before IDAT");
  200. png_ptr->idat_size = length;
  201. png_ptr->mode |= PNG_HAVE_IDAT;
  202. break;
  203. }
  204. #ifdef PNG_READ_bKGD_SUPPORTED
  205. else if (chunk_name == png_bKGD)
  206. png_handle_bKGD(png_ptr, info_ptr, length);
  207. #endif
  208. #ifdef PNG_READ_cHRM_SUPPORTED
  209. else if (chunk_name == png_cHRM)
  210. png_handle_cHRM(png_ptr, info_ptr, length);
  211. #endif
  212. #ifdef PNG_READ_gAMA_SUPPORTED
  213. else if (chunk_name == png_gAMA)
  214. png_handle_gAMA(png_ptr, info_ptr, length);
  215. #endif
  216. #ifdef PNG_READ_hIST_SUPPORTED
  217. else if (chunk_name == png_hIST)
  218. png_handle_hIST(png_ptr, info_ptr, length);
  219. #endif
  220. #ifdef PNG_READ_oFFs_SUPPORTED
  221. else if (chunk_name == png_oFFs)
  222. png_handle_oFFs(png_ptr, info_ptr, length);
  223. #endif
  224. #ifdef PNG_READ_pCAL_SUPPORTED
  225. else if (chunk_name == png_pCAL)
  226. png_handle_pCAL(png_ptr, info_ptr, length);
  227. #endif
  228. #ifdef PNG_READ_sCAL_SUPPORTED
  229. else if (chunk_name == png_sCAL)
  230. png_handle_sCAL(png_ptr, info_ptr, length);
  231. #endif
  232. #ifdef PNG_READ_pHYs_SUPPORTED
  233. else if (chunk_name == png_pHYs)
  234. png_handle_pHYs(png_ptr, info_ptr, length);
  235. #endif
  236. #ifdef PNG_READ_sBIT_SUPPORTED
  237. else if (chunk_name == png_sBIT)
  238. png_handle_sBIT(png_ptr, info_ptr, length);
  239. #endif
  240. #ifdef PNG_READ_sRGB_SUPPORTED
  241. else if (chunk_name == png_sRGB)
  242. png_handle_sRGB(png_ptr, info_ptr, length);
  243. #endif
  244. #ifdef PNG_READ_iCCP_SUPPORTED
  245. else if (chunk_name == png_iCCP)
  246. png_handle_iCCP(png_ptr, info_ptr, length);
  247. #endif
  248. #ifdef PNG_READ_sPLT_SUPPORTED
  249. else if (chunk_name == png_sPLT)
  250. png_handle_sPLT(png_ptr, info_ptr, length);
  251. #endif
  252. #ifdef PNG_READ_tEXt_SUPPORTED
  253. else if (chunk_name == png_tEXt)
  254. png_handle_tEXt(png_ptr, info_ptr, length);
  255. #endif
  256. #ifdef PNG_READ_tIME_SUPPORTED
  257. else if (chunk_name == png_tIME)
  258. png_handle_tIME(png_ptr, info_ptr, length);
  259. #endif
  260. #ifdef PNG_READ_tRNS_SUPPORTED
  261. else if (chunk_name == png_tRNS)
  262. png_handle_tRNS(png_ptr, info_ptr, length);
  263. #endif
  264. #ifdef PNG_READ_zTXt_SUPPORTED
  265. else if (chunk_name == png_zTXt)
  266. png_handle_zTXt(png_ptr, info_ptr, length);
  267. #endif
  268. #ifdef PNG_READ_iTXt_SUPPORTED
  269. else if (chunk_name == png_iTXt)
  270. png_handle_iTXt(png_ptr, info_ptr, length);
  271. #endif
  272. else
  273. png_handle_unknown(png_ptr, info_ptr, length);
  274. }
  275. }
  276. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  277. /* Optional call to update the users info_ptr structure */
  278. void PNGAPI
  279. png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  280. {
  281. png_debug(1, "in png_read_update_info");
  282. if (png_ptr == NULL)
  283. return;
  284. png_read_start_row(png_ptr);
  285. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  286. png_read_transform_info(png_ptr, info_ptr);
  287. #else
  288. PNG_UNUSED(info_ptr)
  289. #endif
  290. }
  291. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  292. /* Initialize palette, background, etc, after transformations
  293. * are set, but before any reading takes place. This allows
  294. * the user to obtain a gamma-corrected palette, for example.
  295. * If the user doesn't call this, we will do it ourselves.
  296. */
  297. void PNGAPI
  298. png_start_read_image(png_structp png_ptr)
  299. {
  300. png_debug(1, "in png_start_read_image");
  301. if (png_ptr != NULL)
  302. png_read_start_row(png_ptr);
  303. }
  304. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  305. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  306. void PNGAPI
  307. png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  308. {
  309. int ret;
  310. png_row_info row_info;
  311. if (png_ptr == NULL)
  312. return;
  313. png_debug2(1, "in png_read_row (row %lu, pass %d)",
  314. (unsigned long)png_ptr->row_number, png_ptr->pass);
  315. /* png_read_start_row sets the information (in particular iwidth) for this
  316. * interlace pass.
  317. */
  318. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  319. png_read_start_row(png_ptr);
  320. /* 1.5.6: row_info moved out of png_struct to a local here. */
  321. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  322. row_info.color_type = png_ptr->color_type;
  323. row_info.bit_depth = png_ptr->bit_depth;
  324. row_info.channels = png_ptr->channels;
  325. row_info.pixel_depth = png_ptr->pixel_depth;
  326. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  327. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  328. {
  329. /* Check for transforms that have been set but were defined out */
  330. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  331. if (png_ptr->transformations & PNG_INVERT_MONO)
  332. png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
  333. #endif
  334. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  335. if (png_ptr->transformations & PNG_FILLER)
  336. png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
  337. #endif
  338. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  339. !defined(PNG_READ_PACKSWAP_SUPPORTED)
  340. if (png_ptr->transformations & PNG_PACKSWAP)
  341. png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
  342. #endif
  343. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  344. if (png_ptr->transformations & PNG_PACK)
  345. png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
  346. #endif
  347. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  348. if (png_ptr->transformations & PNG_SHIFT)
  349. png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
  350. #endif
  351. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  352. if (png_ptr->transformations & PNG_BGR)
  353. png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
  354. #endif
  355. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  356. if (png_ptr->transformations & PNG_SWAP_BYTES)
  357. png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
  358. #endif
  359. }
  360. #ifdef PNG_READ_INTERLACING_SUPPORTED
  361. /* If interlaced and we do not need a new row, combine row and return.
  362. * Notice that the pixels we have from previous rows have been transformed
  363. * already; we can only combine like with like (transformed or
  364. * untransformed) and, because of the libpng API for interlaced images, this
  365. * means we must transform before de-interlacing.
  366. */
  367. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  368. {
  369. switch (png_ptr->pass)
  370. {
  371. case 0:
  372. if (png_ptr->row_number & 0x07)
  373. {
  374. if (dsp_row != NULL)
  375. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  376. png_read_finish_row(png_ptr);
  377. return;
  378. }
  379. break;
  380. case 1:
  381. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  382. {
  383. if (dsp_row != NULL)
  384. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  385. png_read_finish_row(png_ptr);
  386. return;
  387. }
  388. break;
  389. case 2:
  390. if ((png_ptr->row_number & 0x07) != 4)
  391. {
  392. if (dsp_row != NULL && (png_ptr->row_number & 4))
  393. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  394. png_read_finish_row(png_ptr);
  395. return;
  396. }
  397. break;
  398. case 3:
  399. if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  400. {
  401. if (dsp_row != NULL)
  402. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  403. png_read_finish_row(png_ptr);
  404. return;
  405. }
  406. break;
  407. case 4:
  408. if ((png_ptr->row_number & 3) != 2)
  409. {
  410. if (dsp_row != NULL && (png_ptr->row_number & 2))
  411. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  412. png_read_finish_row(png_ptr);
  413. return;
  414. }
  415. break;
  416. case 5:
  417. if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  418. {
  419. if (dsp_row != NULL)
  420. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  421. png_read_finish_row(png_ptr);
  422. return;
  423. }
  424. break;
  425. default:
  426. case 6:
  427. if (!(png_ptr->row_number & 1))
  428. {
  429. png_read_finish_row(png_ptr);
  430. return;
  431. }
  432. break;
  433. }
  434. }
  435. #endif
  436. if (!(png_ptr->mode & PNG_HAVE_IDAT))
  437. png_error(png_ptr, "Invalid attempt to read row data");
  438. png_ptr->zstream.next_out = png_ptr->row_buf;
  439. png_ptr->zstream.avail_out =
  440. (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  441. png_ptr->iwidth) + 1);
  442. do
  443. {
  444. if (!(png_ptr->zstream.avail_in))
  445. {
  446. while (!png_ptr->idat_size)
  447. {
  448. png_crc_finish(png_ptr, 0);
  449. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  450. if (png_ptr->chunk_name != png_IDAT)
  451. png_error(png_ptr, "Not enough image data");
  452. }
  453. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  454. png_ptr->zstream.next_in = png_ptr->zbuf;
  455. if (png_ptr->zbuf_size > png_ptr->idat_size)
  456. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  457. png_crc_read(png_ptr, png_ptr->zbuf,
  458. (png_size_t)png_ptr->zstream.avail_in);
  459. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  460. }
  461. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  462. if (ret == Z_STREAM_END)
  463. {
  464. if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  465. png_ptr->idat_size)
  466. png_benign_error(png_ptr, "Extra compressed data");
  467. png_ptr->mode |= PNG_AFTER_IDAT;
  468. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  469. break;
  470. }
  471. if (ret != Z_OK)
  472. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  473. "Decompression error");
  474. } while (png_ptr->zstream.avail_out);
  475. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  476. {
  477. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  478. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  479. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  480. else
  481. png_error(png_ptr, "bad adaptive filter value");
  482. }
  483. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  484. * 1.5.6, while the buffer really is this big in current versions of libpng
  485. * it may not be in the future, so this was changed just to copy the
  486. * interlaced count:
  487. */
  488. png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  489. #ifdef PNG_MNG_FEATURES_SUPPORTED
  490. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  491. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  492. {
  493. /* Intrapixel differencing */
  494. png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
  495. }
  496. #endif
  497. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  498. if (png_ptr->transformations)
  499. png_do_read_transformations(png_ptr, &row_info);
  500. #endif
  501. /* The transformed pixel depth should match the depth now in row_info. */
  502. if (png_ptr->transformed_pixel_depth == 0)
  503. {
  504. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  505. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  506. png_error(png_ptr, "sequential row overflow");
  507. }
  508. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  509. png_error(png_ptr, "internal sequential row size calculation error");
  510. #ifdef PNG_READ_INTERLACING_SUPPORTED
  511. /* Blow up interlaced rows to full size */
  512. if (png_ptr->interlaced &&
  513. (png_ptr->transformations & PNG_INTERLACE))
  514. {
  515. if (png_ptr->pass < 6)
  516. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  517. png_ptr->transformations);
  518. if (dsp_row != NULL)
  519. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  520. if (row != NULL)
  521. png_combine_row(png_ptr, row, 0/*row*/);
  522. }
  523. else
  524. #endif
  525. {
  526. if (row != NULL)
  527. png_combine_row(png_ptr, row, -1/*ignored*/);
  528. if (dsp_row != NULL)
  529. png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
  530. }
  531. png_read_finish_row(png_ptr);
  532. if (png_ptr->read_row_fn != NULL)
  533. (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  534. }
  535. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  536. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  537. /* Read one or more rows of image data. If the image is interlaced,
  538. * and png_set_interlace_handling() has been called, the rows need to
  539. * contain the contents of the rows from the previous pass. If the
  540. * image has alpha or transparency, and png_handle_alpha()[*] has been
  541. * called, the rows contents must be initialized to the contents of the
  542. * screen.
  543. *
  544. * "row" holds the actual image, and pixels are placed in it
  545. * as they arrive. If the image is displayed after each pass, it will
  546. * appear to "sparkle" in. "display_row" can be used to display a
  547. * "chunky" progressive image, with finer detail added as it becomes
  548. * available. If you do not want this "chunky" display, you may pass
  549. * NULL for display_row. If you do not want the sparkle display, and
  550. * you have not called png_handle_alpha(), you may pass NULL for rows.
  551. * If you have called png_handle_alpha(), and the image has either an
  552. * alpha channel or a transparency chunk, you must provide a buffer for
  553. * rows. In this case, you do not have to provide a display_row buffer
  554. * also, but you may. If the image is not interlaced, or if you have
  555. * not called png_set_interlace_handling(), the display_row buffer will
  556. * be ignored, so pass NULL to it.
  557. *
  558. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  559. */
  560. void PNGAPI
  561. png_read_rows(png_structp png_ptr, png_bytepp row,
  562. png_bytepp display_row, png_uint_32 num_rows)
  563. {
  564. png_uint_32 i;
  565. png_bytepp rp;
  566. png_bytepp dp;
  567. png_debug(1, "in png_read_rows");
  568. if (png_ptr == NULL)
  569. return;
  570. rp = row;
  571. dp = display_row;
  572. if (rp != NULL && dp != NULL)
  573. for (i = 0; i < num_rows; i++)
  574. {
  575. png_bytep rptr = *rp++;
  576. png_bytep dptr = *dp++;
  577. png_read_row(png_ptr, rptr, dptr);
  578. }
  579. else if (rp != NULL)
  580. for (i = 0; i < num_rows; i++)
  581. {
  582. png_bytep rptr = *rp;
  583. png_read_row(png_ptr, rptr, NULL);
  584. rp++;
  585. }
  586. else if (dp != NULL)
  587. for (i = 0; i < num_rows; i++)
  588. {
  589. png_bytep dptr = *dp;
  590. png_read_row(png_ptr, NULL, dptr);
  591. dp++;
  592. }
  593. }
  594. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  595. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  596. /* Read the entire image. If the image has an alpha channel or a tRNS
  597. * chunk, and you have called png_handle_alpha()[*], you will need to
  598. * initialize the image to the current image that PNG will be overlaying.
  599. * We set the num_rows again here, in case it was incorrectly set in
  600. * png_read_start_row() by a call to png_read_update_info() or
  601. * png_start_read_image() if png_set_interlace_handling() wasn't called
  602. * prior to either of these functions like it should have been. You can
  603. * only call this function once. If you desire to have an image for
  604. * each pass of a interlaced image, use png_read_rows() instead.
  605. *
  606. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  607. */
  608. void PNGAPI
  609. png_read_image(png_structp png_ptr, png_bytepp image)
  610. {
  611. png_uint_32 i, image_height;
  612. int pass, j;
  613. png_bytepp rp;
  614. png_debug(1, "in png_read_image");
  615. if (png_ptr == NULL)
  616. return;
  617. #ifdef PNG_READ_INTERLACING_SUPPORTED
  618. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  619. {
  620. pass = png_set_interlace_handling(png_ptr);
  621. /* And make sure transforms are initialized. */
  622. png_start_read_image(png_ptr);
  623. }
  624. else
  625. {
  626. if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
  627. {
  628. /* Caller called png_start_read_image or png_read_update_info without
  629. * first turning on the PNG_INTERLACE transform. We can fix this here,
  630. * but the caller should do it!
  631. */
  632. png_warning(png_ptr, "Interlace handling should be turned on when "
  633. "using png_read_image");
  634. /* Make sure this is set correctly */
  635. png_ptr->num_rows = png_ptr->height;
  636. }
  637. /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
  638. * the above error case.
  639. */
  640. pass = png_set_interlace_handling(png_ptr);
  641. }
  642. #else
  643. if (png_ptr->interlaced)
  644. png_error(png_ptr,
  645. "Cannot read interlaced image -- interlace handler disabled");
  646. pass = 1;
  647. #endif
  648. image_height=png_ptr->height;
  649. for (j = 0; j < pass; j++)
  650. {
  651. rp = image;
  652. for (i = 0; i < image_height; i++)
  653. {
  654. png_read_row(png_ptr, *rp, NULL);
  655. rp++;
  656. }
  657. }
  658. }
  659. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  660. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  661. /* Read the end of the PNG file. Will not read past the end of the
  662. * file, will verify the end is accurate, and will read any comments
  663. * or time information at the end of the file, if info is not NULL.
  664. */
  665. void PNGAPI
  666. png_read_end(png_structp png_ptr, png_infop info_ptr)
  667. {
  668. png_debug(1, "in png_read_end");
  669. if (png_ptr == NULL)
  670. return;
  671. png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  672. do
  673. {
  674. png_uint_32 length = png_read_chunk_header(png_ptr);
  675. png_uint_32 chunk_name = png_ptr->chunk_name;
  676. if (chunk_name == png_IHDR)
  677. png_handle_IHDR(png_ptr, info_ptr, length);
  678. else if (chunk_name == png_IEND)
  679. png_handle_IEND(png_ptr, info_ptr, length);
  680. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  681. else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
  682. PNG_HANDLE_CHUNK_AS_DEFAULT)
  683. {
  684. if (chunk_name == png_IDAT)
  685. {
  686. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  687. png_benign_error(png_ptr, "Too many IDATs found");
  688. }
  689. png_handle_unknown(png_ptr, info_ptr, length);
  690. if (chunk_name == png_PLTE)
  691. png_ptr->mode |= PNG_HAVE_PLTE;
  692. }
  693. #endif
  694. else if (chunk_name == png_IDAT)
  695. {
  696. /* Zero length IDATs are legal after the last IDAT has been
  697. * read, but not after other chunks have been read.
  698. */
  699. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  700. png_benign_error(png_ptr, "Too many IDATs found");
  701. png_crc_finish(png_ptr, length);
  702. }
  703. else if (chunk_name == png_PLTE)
  704. png_handle_PLTE(png_ptr, info_ptr, length);
  705. #ifdef PNG_READ_bKGD_SUPPORTED
  706. else if (chunk_name == png_bKGD)
  707. png_handle_bKGD(png_ptr, info_ptr, length);
  708. #endif
  709. #ifdef PNG_READ_cHRM_SUPPORTED
  710. else if (chunk_name == png_cHRM)
  711. png_handle_cHRM(png_ptr, info_ptr, length);
  712. #endif
  713. #ifdef PNG_READ_gAMA_SUPPORTED
  714. else if (chunk_name == png_gAMA)
  715. png_handle_gAMA(png_ptr, info_ptr, length);
  716. #endif
  717. #ifdef PNG_READ_hIST_SUPPORTED
  718. else if (chunk_name == png_hIST)
  719. png_handle_hIST(png_ptr, info_ptr, length);
  720. #endif
  721. #ifdef PNG_READ_oFFs_SUPPORTED
  722. else if (chunk_name == png_oFFs)
  723. png_handle_oFFs(png_ptr, info_ptr, length);
  724. #endif
  725. #ifdef PNG_READ_pCAL_SUPPORTED
  726. else if (chunk_name == png_pCAL)
  727. png_handle_pCAL(png_ptr, info_ptr, length);
  728. #endif
  729. #ifdef PNG_READ_sCAL_SUPPORTED
  730. else if (chunk_name == png_sCAL)
  731. png_handle_sCAL(png_ptr, info_ptr, length);
  732. #endif
  733. #ifdef PNG_READ_pHYs_SUPPORTED
  734. else if (chunk_name == png_pHYs)
  735. png_handle_pHYs(png_ptr, info_ptr, length);
  736. #endif
  737. #ifdef PNG_READ_sBIT_SUPPORTED
  738. else if (chunk_name == png_sBIT)
  739. png_handle_sBIT(png_ptr, info_ptr, length);
  740. #endif
  741. #ifdef PNG_READ_sRGB_SUPPORTED
  742. else if (chunk_name == png_sRGB)
  743. png_handle_sRGB(png_ptr, info_ptr, length);
  744. #endif
  745. #ifdef PNG_READ_iCCP_SUPPORTED
  746. else if (chunk_name == png_iCCP)
  747. png_handle_iCCP(png_ptr, info_ptr, length);
  748. #endif
  749. #ifdef PNG_READ_sPLT_SUPPORTED
  750. else if (chunk_name == png_sPLT)
  751. png_handle_sPLT(png_ptr, info_ptr, length);
  752. #endif
  753. #ifdef PNG_READ_tEXt_SUPPORTED
  754. else if (chunk_name == png_tEXt)
  755. png_handle_tEXt(png_ptr, info_ptr, length);
  756. #endif
  757. #ifdef PNG_READ_tIME_SUPPORTED
  758. else if (chunk_name == png_tIME)
  759. png_handle_tIME(png_ptr, info_ptr, length);
  760. #endif
  761. #ifdef PNG_READ_tRNS_SUPPORTED
  762. else if (chunk_name == png_tRNS)
  763. png_handle_tRNS(png_ptr, info_ptr, length);
  764. #endif
  765. #ifdef PNG_READ_zTXt_SUPPORTED
  766. else if (chunk_name == png_zTXt)
  767. png_handle_zTXt(png_ptr, info_ptr, length);
  768. #endif
  769. #ifdef PNG_READ_iTXt_SUPPORTED
  770. else if (chunk_name == png_iTXt)
  771. png_handle_iTXt(png_ptr, info_ptr, length);
  772. #endif
  773. else
  774. png_handle_unknown(png_ptr, info_ptr, length);
  775. } while (!(png_ptr->mode & PNG_HAVE_IEND));
  776. }
  777. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  778. /* Free all memory used by the read */
  779. void PNGAPI
  780. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  781. png_infopp end_info_ptr_ptr)
  782. {
  783. png_structp png_ptr = NULL;
  784. png_infop info_ptr = NULL, end_info_ptr = NULL;
  785. #ifdef PNG_USER_MEM_SUPPORTED
  786. png_free_ptr free_fn = NULL;
  787. png_voidp mem_ptr = NULL;
  788. #endif
  789. png_debug(1, "in png_destroy_read_struct");
  790. if (png_ptr_ptr != NULL)
  791. png_ptr = *png_ptr_ptr;
  792. if (png_ptr == NULL)
  793. return;
  794. #ifdef PNG_USER_MEM_SUPPORTED
  795. free_fn = png_ptr->free_fn;
  796. mem_ptr = png_ptr->mem_ptr;
  797. #endif
  798. if (info_ptr_ptr != NULL)
  799. info_ptr = *info_ptr_ptr;
  800. if (end_info_ptr_ptr != NULL)
  801. end_info_ptr = *end_info_ptr_ptr;
  802. png_read_destroy(png_ptr, info_ptr, end_info_ptr);
  803. if (info_ptr != NULL)
  804. {
  805. #ifdef PNG_TEXT_SUPPORTED
  806. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
  807. #endif
  808. #ifdef PNG_USER_MEM_SUPPORTED
  809. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  810. (png_voidp)mem_ptr);
  811. #else
  812. png_destroy_struct((png_voidp)info_ptr);
  813. #endif
  814. *info_ptr_ptr = NULL;
  815. }
  816. if (end_info_ptr != NULL)
  817. {
  818. #ifdef PNG_READ_TEXT_SUPPORTED
  819. png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
  820. #endif
  821. #ifdef PNG_USER_MEM_SUPPORTED
  822. png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
  823. (png_voidp)mem_ptr);
  824. #else
  825. png_destroy_struct((png_voidp)end_info_ptr);
  826. #endif
  827. *end_info_ptr_ptr = NULL;
  828. }
  829. if (png_ptr != NULL)
  830. {
  831. #ifdef PNG_USER_MEM_SUPPORTED
  832. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  833. (png_voidp)mem_ptr);
  834. #else
  835. png_destroy_struct((png_voidp)png_ptr);
  836. #endif
  837. *png_ptr_ptr = NULL;
  838. }
  839. }
  840. /* Free all memory used by the read (old method) */
  841. void /* PRIVATE */
  842. png_read_destroy(png_structp png_ptr, png_infop info_ptr,
  843. png_infop end_info_ptr)
  844. {
  845. #ifdef PNG_SETJMP_SUPPORTED
  846. jmp_buf tmp_jmp;
  847. #endif
  848. png_error_ptr error_fn;
  849. #ifdef PNG_WARNINGS_SUPPORTED
  850. png_error_ptr warning_fn;
  851. #endif
  852. png_voidp error_ptr;
  853. #ifdef PNG_USER_MEM_SUPPORTED
  854. png_free_ptr free_fn;
  855. #endif
  856. png_debug(1, "in png_read_destroy");
  857. if (info_ptr != NULL)
  858. png_info_destroy(png_ptr, info_ptr);
  859. if (end_info_ptr != NULL)
  860. png_info_destroy(png_ptr, end_info_ptr);
  861. #ifdef PNG_READ_GAMMA_SUPPORTED
  862. png_destroy_gamma_table(png_ptr);
  863. #endif
  864. png_free(png_ptr, png_ptr->zbuf);
  865. png_free(png_ptr, png_ptr->big_row_buf);
  866. png_free(png_ptr, png_ptr->big_prev_row);
  867. png_free(png_ptr, png_ptr->chunkdata);
  868. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  869. png_free(png_ptr, png_ptr->palette_lookup);
  870. png_free(png_ptr, png_ptr->quantize_index);
  871. #endif
  872. if (png_ptr->free_me & PNG_FREE_PLTE)
  873. png_zfree(png_ptr, png_ptr->palette);
  874. png_ptr->free_me &= ~PNG_FREE_PLTE;
  875. #if defined(PNG_tRNS_SUPPORTED) || \
  876. defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  877. if (png_ptr->free_me & PNG_FREE_TRNS)
  878. png_free(png_ptr, png_ptr->trans_alpha);
  879. png_ptr->free_me &= ~PNG_FREE_TRNS;
  880. #endif
  881. #ifdef PNG_READ_hIST_SUPPORTED
  882. if (png_ptr->free_me & PNG_FREE_HIST)
  883. png_free(png_ptr, png_ptr->hist);
  884. png_ptr->free_me &= ~PNG_FREE_HIST;
  885. #endif
  886. inflateEnd(&png_ptr->zstream);
  887. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  888. png_free(png_ptr, png_ptr->save_buffer);
  889. #endif
  890. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  891. #ifdef PNG_TEXT_SUPPORTED
  892. png_free(png_ptr, png_ptr->current_text);
  893. #endif /* PNG_TEXT_SUPPORTED */
  894. #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
  895. /* Save the important info out of the png_struct, in case it is
  896. * being used again.
  897. */
  898. #ifdef PNG_SETJMP_SUPPORTED
  899. png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
  900. #endif
  901. error_fn = png_ptr->error_fn;
  902. #ifdef PNG_WARNINGS_SUPPORTED
  903. warning_fn = png_ptr->warning_fn;
  904. #endif
  905. error_ptr = png_ptr->error_ptr;
  906. #ifdef PNG_USER_MEM_SUPPORTED
  907. free_fn = png_ptr->free_fn;
  908. #endif
  909. png_memset(png_ptr, 0, png_sizeof(png_struct));
  910. png_ptr->error_fn = error_fn;
  911. #ifdef PNG_WARNINGS_SUPPORTED
  912. png_ptr->warning_fn = warning_fn;
  913. #endif
  914. png_ptr->error_ptr = error_ptr;
  915. #ifdef PNG_USER_MEM_SUPPORTED
  916. png_ptr->free_fn = free_fn;
  917. #endif
  918. #ifdef PNG_SETJMP_SUPPORTED
  919. png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
  920. #endif
  921. }
  922. void PNGAPI
  923. png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
  924. {
  925. if (png_ptr == NULL)
  926. return;
  927. png_ptr->read_row_fn = read_row_fn;
  928. }
  929. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  930. #ifdef PNG_INFO_IMAGE_SUPPORTED
  931. void PNGAPI
  932. png_read_png(png_structp png_ptr, png_infop info_ptr,
  933. int transforms,
  934. voidp params)
  935. {
  936. int row;
  937. if (png_ptr == NULL || info_ptr == NULL)
  938. return;
  939. /* png_read_info() gives us all of the information from the
  940. * PNG file before the first IDAT (image data chunk).
  941. */
  942. png_read_info(png_ptr, info_ptr);
  943. if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
  944. png_error(png_ptr, "Image is too high to process with png_read_png()");
  945. /* -------------- image transformations start here ------------------- */
  946. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  947. /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
  948. */
  949. if (transforms & PNG_TRANSFORM_SCALE_16)
  950. {
  951. /* Added at libpng-1.5.4. "strip_16" produces the same result that it
  952. * did in earlier versions, while "scale_16" is now more accurate.
  953. */
  954. png_set_scale_16(png_ptr);
  955. }
  956. #endif
  957. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  958. /* If both SCALE and STRIP are required pngrtran will effectively cancel the
  959. * latter by doing SCALE first. This is ok and allows apps not to check for
  960. * which is supported to get the right answer.
  961. */
  962. if (transforms & PNG_TRANSFORM_STRIP_16)
  963. png_set_strip_16(png_ptr);
  964. #endif
  965. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  966. /* Strip alpha bytes from the input data without combining with
  967. * the background (not recommended).
  968. */
  969. if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
  970. png_set_strip_alpha(png_ptr);
  971. #endif
  972. #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
  973. /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  974. * byte into separate bytes (useful for paletted and grayscale images).
  975. */
  976. if (transforms & PNG_TRANSFORM_PACKING)
  977. png_set_packing(png_ptr);
  978. #endif
  979. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  980. /* Change the order of packed pixels to least significant bit first
  981. * (not useful if you are using png_set_packing).
  982. */
  983. if (transforms & PNG_TRANSFORM_PACKSWAP)
  984. png_set_packswap(png_ptr);
  985. #endif
  986. #ifdef PNG_READ_EXPAND_SUPPORTED
  987. /* Expand paletted colors into true RGB triplets
  988. * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  989. * Expand paletted or RGB images with transparency to full alpha
  990. * channels so the data will be available as RGBA quartets.
  991. */
  992. if (transforms & PNG_TRANSFORM_EXPAND)
  993. if ((png_ptr->bit_depth < 8) ||
  994. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  995. (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
  996. png_set_expand(png_ptr);
  997. #endif
  998. /* We don't handle background color or gamma transformation or quantizing.
  999. */
  1000. #ifdef PNG_READ_INVERT_SUPPORTED
  1001. /* Invert monochrome files to have 0 as white and 1 as black
  1002. */
  1003. if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1004. png_set_invert_mono(png_ptr);
  1005. #endif
  1006. #ifdef PNG_READ_SHIFT_SUPPORTED
  1007. /* If you want to shift the pixel values from the range [0,255] or
  1008. * [0,65535] to the original [0,7] or [0,31], or whatever range the
  1009. * colors were originally in:
  1010. */
  1011. if ((transforms & PNG_TRANSFORM_SHIFT)
  1012. && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
  1013. {
  1014. png_color_8p sig_bit;
  1015. png_get_sBIT(png_ptr, info_ptr, &sig_bit);
  1016. png_set_shift(png_ptr, sig_bit);
  1017. }
  1018. #endif
  1019. #ifdef PNG_READ_BGR_SUPPORTED
  1020. /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
  1021. if (transforms & PNG_TRANSFORM_BGR)
  1022. png_set_bgr(png_ptr);
  1023. #endif
  1024. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  1025. /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
  1026. if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1027. png_set_swap_alpha(png_ptr);
  1028. #endif
  1029. #ifdef PNG_READ_SWAP_SUPPORTED
  1030. /* Swap bytes of 16-bit files to least significant byte first */
  1031. if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1032. png_set_swap(png_ptr);
  1033. #endif
  1034. /* Added at libpng-1.2.41 */
  1035. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1036. /* Invert the alpha channel from opacity to transparency */
  1037. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1038. png_set_invert_alpha(png_ptr);
  1039. #endif
  1040. /* Added at libpng-1.2.41 */
  1041. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1042. /* Expand grayscale image to RGB */
  1043. if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
  1044. png_set_gray_to_rgb(png_ptr);
  1045. #endif
  1046. /* Added at libpng-1.5.4 */
  1047. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1048. if (transforms & PNG_TRANSFORM_EXPAND_16)
  1049. png_set_expand_16(png_ptr);
  1050. #endif
  1051. /* We don't handle adding filler bytes */
  1052. /* We use png_read_image and rely on that for interlace handling, but we also
  1053. * call png_read_update_info therefore must turn on interlace handling now:
  1054. */
  1055. (void)png_set_interlace_handling(png_ptr);
  1056. /* Optional call to gamma correct and add the background to the palette
  1057. * and update info structure. REQUIRED if you are expecting libpng to
  1058. * update the palette for you (i.e., you selected such a transform above).
  1059. */
  1060. png_read_update_info(png_ptr, info_ptr);
  1061. /* -------------- image transformations end here ------------------- */
  1062. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1063. if (info_ptr->row_pointers == NULL)
  1064. {
  1065. png_uint_32 iptr;
  1066. info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
  1067. info_ptr->height * png_sizeof(png_bytep));
  1068. for (iptr=0; iptr<info_ptr->height; iptr++)
  1069. info_ptr->row_pointers[iptr] = NULL;
  1070. info_ptr->free_me |= PNG_FREE_ROWS;
  1071. for (row = 0; row < (int)info_ptr->height; row++)
  1072. info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
  1073. png_get_rowbytes(png_ptr, info_ptr));
  1074. }
  1075. png_read_image(png_ptr, info_ptr->row_pointers);
  1076. info_ptr->valid |= PNG_INFO_IDAT;
  1077. /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1078. png_read_end(png_ptr, info_ptr);
  1079. PNG_UNUSED(transforms) /* Quiet compiler warnings */
  1080. PNG_UNUSED(params)
  1081. }
  1082. #endif /* PNG_INFO_IMAGE_SUPPORTED */
  1083. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  1084. #endif /* PNG_READ_SUPPORTED */