pngread.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /* pngread.c - read a PNG file
  2. *
  3. * Last changed in libpng 1.2.48 [March 8, 2012]
  4. * Copyright (c) 1998-2012 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. #define PNG_INTERNAL
  16. #define PNG_NO_PEDANTIC_WARNINGS
  17. #include "png.h"
  18. #ifdef PNG_READ_SUPPORTED
  19. /* Create a PNG structure for reading, and allocate any memory needed. */
  20. png_structp PNGAPI
  21. png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
  22. png_error_ptr error_fn, png_error_ptr warn_fn)
  23. {
  24. #ifdef PNG_USER_MEM_SUPPORTED
  25. return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  26. warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
  27. }
  28. /* Alternate create PNG structure for reading, and allocate any memory
  29. * needed.
  30. */
  31. png_structp PNGAPI
  32. png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
  33. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  34. png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  35. {
  36. #endif /* PNG_USER_MEM_SUPPORTED */
  37. #ifdef PNG_SETJMP_SUPPORTED
  38. volatile
  39. #endif
  40. png_structp png_ptr;
  41. #ifdef PNG_SETJMP_SUPPORTED
  42. #ifdef USE_FAR_KEYWORD
  43. jmp_buf jmpbuf;
  44. #endif
  45. #endif
  46. int i;
  47. png_debug(1, "in png_create_read_struct");
  48. #ifdef PNG_USER_MEM_SUPPORTED
  49. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  50. (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  51. #else
  52. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  53. #endif
  54. if (png_ptr == NULL)
  55. return (NULL);
  56. /* Added at libpng-1.2.6 */
  57. #ifdef PNG_USER_LIMITS_SUPPORTED
  58. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  59. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  60. /* Added at libpng-1.2.43 and 1.4.0 */
  61. png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
  62. #endif
  63. #ifdef PNG_SETJMP_SUPPORTED
  64. #ifdef USE_FAR_KEYWORD
  65. if (setjmp(jmpbuf))
  66. #else
  67. if (setjmp(png_ptr->jmpbuf))
  68. #endif
  69. {
  70. png_free(png_ptr, png_ptr->zbuf);
  71. png_ptr->zbuf = NULL;
  72. #ifdef PNG_USER_MEM_SUPPORTED
  73. png_destroy_struct_2((png_voidp)png_ptr,
  74. (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  75. #else
  76. png_destroy_struct((png_voidp)png_ptr);
  77. #endif
  78. return (NULL);
  79. }
  80. #ifdef USE_FAR_KEYWORD
  81. png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  82. #endif
  83. #endif /* PNG_SETJMP_SUPPORTED */
  84. #ifdef PNG_USER_MEM_SUPPORTED
  85. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  86. #endif
  87. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  88. if (user_png_ver)
  89. {
  90. i = 0;
  91. do
  92. {
  93. if (user_png_ver[i] != png_libpng_ver[i])
  94. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  95. } while (png_libpng_ver[i++]);
  96. }
  97. else
  98. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  99. if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  100. {
  101. /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  102. * we must recompile any applications that use any older library version.
  103. * For versions after libpng 1.0, we will be compatible, so we need
  104. * only check the first digit.
  105. */
  106. if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  107. (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  108. (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  109. {
  110. #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
  111. char msg[80];
  112. if (user_png_ver)
  113. {
  114. png_snprintf(msg, 80,
  115. "Application was compiled with png.h from libpng-%.20s",
  116. user_png_ver);
  117. png_warning(png_ptr, msg);
  118. }
  119. png_snprintf(msg, 80,
  120. "Application is running with png.c from libpng-%.20s",
  121. png_libpng_ver);
  122. png_warning(png_ptr, msg);
  123. #endif
  124. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  125. png_ptr->flags = 0;
  126. #endif
  127. png_error(png_ptr,
  128. "Incompatible libpng version in application and library");
  129. }
  130. }
  131. /* Initialize zbuf - compression buffer */
  132. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  133. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  134. (png_uint_32)png_ptr->zbuf_size);
  135. png_ptr->zstream.zalloc = png_zalloc;
  136. png_ptr->zstream.zfree = png_zfree;
  137. png_ptr->zstream.opaque = (voidpf)png_ptr;
  138. switch (inflateInit(&png_ptr->zstream))
  139. {
  140. case Z_OK: /* Do nothing */ break;
  141. case Z_MEM_ERROR:
  142. case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error");
  143. break;
  144. case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error");
  145. break;
  146. default: png_error(png_ptr, "Unknown zlib error");
  147. }
  148. png_ptr->zstream.next_out = png_ptr->zbuf;
  149. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  150. png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  151. #ifdef PNG_SETJMP_SUPPORTED
  152. /* Applications that neglect to set up their own setjmp() and then
  153. encounter a png_error() will longjmp here. Since the jmpbuf is
  154. then meaningless we abort instead of returning. */
  155. #ifdef USE_FAR_KEYWORD
  156. if (setjmp(jmpbuf))
  157. PNG_ABORT();
  158. png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  159. #else
  160. if (setjmp(png_ptr->jmpbuf))
  161. PNG_ABORT();
  162. #endif
  163. #endif /* PNG_SETJMP_SUPPORTED */
  164. return (png_ptr);
  165. }
  166. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  167. /* Initialize PNG structure for reading, and allocate any memory needed.
  168. * This interface is deprecated in favour of the png_create_read_struct(),
  169. * and it will disappear as of libpng-1.3.0.
  170. */
  171. #undef png_read_init
  172. void PNGAPI
  173. png_read_init(png_structp png_ptr)
  174. {
  175. /* We only come here via pre-1.0.7-compiled applications */
  176. png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  177. }
  178. void PNGAPI
  179. png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  180. png_size_t png_struct_size, png_size_t png_info_size)
  181. {
  182. /* We only come here via pre-1.0.12-compiled applications */
  183. if (png_ptr == NULL)
  184. return;
  185. #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
  186. if (png_sizeof(png_struct) > png_struct_size ||
  187. png_sizeof(png_info) > png_info_size)
  188. {
  189. char msg[80];
  190. png_ptr->warning_fn = NULL;
  191. if (user_png_ver)
  192. {
  193. png_snprintf(msg, 80,
  194. "Application was compiled with png.h from libpng-%.20s",
  195. user_png_ver);
  196. png_warning(png_ptr, msg);
  197. }
  198. png_snprintf(msg, 80,
  199. "Application is running with png.c from libpng-%.20s",
  200. png_libpng_ver);
  201. png_warning(png_ptr, msg);
  202. }
  203. #endif
  204. if (png_sizeof(png_struct) > png_struct_size)
  205. {
  206. png_ptr->error_fn = NULL;
  207. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  208. png_ptr->flags = 0;
  209. #endif
  210. png_error(png_ptr,
  211. "The png struct allocated by the application for reading is"
  212. " too small.");
  213. }
  214. if (png_sizeof(png_info) > png_info_size)
  215. {
  216. png_ptr->error_fn = NULL;
  217. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  218. png_ptr->flags = 0;
  219. #endif
  220. png_error(png_ptr,
  221. "The info struct allocated by application for reading is"
  222. " too small.");
  223. }
  224. png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
  225. }
  226. #endif /* PNG_1_0_X || PNG_1_2_X */
  227. void PNGAPI
  228. png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  229. png_size_t png_struct_size)
  230. {
  231. #ifdef PNG_SETJMP_SUPPORTED
  232. jmp_buf tmp_jmp; /* to save current jump buffer */
  233. #endif
  234. int i = 0;
  235. png_structp png_ptr=*ptr_ptr;
  236. if (png_ptr == NULL)
  237. return;
  238. do
  239. {
  240. if (user_png_ver[i] != png_libpng_ver[i])
  241. {
  242. #ifdef PNG_LEGACY_SUPPORTED
  243. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  244. #else
  245. png_ptr->warning_fn = NULL;
  246. png_warning(png_ptr,
  247. "Application uses deprecated png_read_init() and should be"
  248. " recompiled.");
  249. break;
  250. #endif
  251. }
  252. } while (png_libpng_ver[i++]);
  253. png_debug(1, "in png_read_init_3");
  254. #ifdef PNG_SETJMP_SUPPORTED
  255. /* Save jump buffer and error functions */
  256. png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  257. #endif
  258. if (png_sizeof(png_struct) > png_struct_size)
  259. {
  260. png_destroy_struct(png_ptr);
  261. *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  262. png_ptr = *ptr_ptr;
  263. }
  264. /* Reset all variables to 0 */
  265. png_memset(png_ptr, 0, png_sizeof(png_struct));
  266. #ifdef PNG_SETJMP_SUPPORTED
  267. /* Restore jump buffer */
  268. png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  269. #endif
  270. /* Added at libpng-1.2.6 */
  271. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  272. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  273. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  274. #endif
  275. /* Initialize zbuf - compression buffer */
  276. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  277. png_ptr->zstream.zalloc = png_zalloc;
  278. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  279. (png_uint_32)png_ptr->zbuf_size);
  280. png_ptr->zstream.zalloc = png_zalloc;
  281. png_ptr->zstream.zfree = png_zfree;
  282. png_ptr->zstream.opaque = (voidpf)png_ptr;
  283. switch (inflateInit(&png_ptr->zstream))
  284. {
  285. case Z_OK: /* Do nothing */ break;
  286. case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
  287. case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error");
  288. break;
  289. default: png_error(png_ptr, "Unknown zlib error");
  290. }
  291. png_ptr->zstream.next_out = png_ptr->zbuf;
  292. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  293. png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  294. }
  295. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  296. /* Read the information before the actual image data. This has been
  297. * changed in v0.90 to allow reading a file that already has the magic
  298. * bytes read from the stream. You can tell libpng how many bytes have
  299. * been read from the beginning of the stream (up to the maximum of 8)
  300. * via png_set_sig_bytes(), and we will only check the remaining bytes
  301. * here. The application can then have access to the signature bytes we
  302. * read if it is determined that this isn't a valid PNG file.
  303. */
  304. void PNGAPI
  305. png_read_info(png_structp png_ptr, png_infop info_ptr)
  306. {
  307. png_debug(1, "in png_read_info");
  308. if (png_ptr == NULL || info_ptr == NULL)
  309. return;
  310. /* If we haven't checked all of the PNG signature bytes, do so now. */
  311. if (png_ptr->sig_bytes < 8)
  312. {
  313. png_size_t num_checked = png_ptr->sig_bytes,
  314. num_to_check = 8 - num_checked;
  315. png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  316. png_ptr->sig_bytes = 8;
  317. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  318. {
  319. if (num_checked < 4 &&
  320. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  321. png_error(png_ptr, "Not a PNG file");
  322. else
  323. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  324. }
  325. if (num_checked < 3)
  326. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  327. }
  328. for (;;)
  329. {
  330. #ifdef PNG_USE_LOCAL_ARRAYS
  331. PNG_CONST PNG_IHDR;
  332. PNG_CONST PNG_IDAT;
  333. PNG_CONST PNG_IEND;
  334. PNG_CONST PNG_PLTE;
  335. #ifdef PNG_READ_bKGD_SUPPORTED
  336. PNG_CONST PNG_bKGD;
  337. #endif
  338. #ifdef PNG_READ_cHRM_SUPPORTED
  339. PNG_CONST PNG_cHRM;
  340. #endif
  341. #ifdef PNG_READ_gAMA_SUPPORTED
  342. PNG_CONST PNG_gAMA;
  343. #endif
  344. #ifdef PNG_READ_hIST_SUPPORTED
  345. PNG_CONST PNG_hIST;
  346. #endif
  347. #ifdef PNG_READ_iCCP_SUPPORTED
  348. PNG_CONST PNG_iCCP;
  349. #endif
  350. #ifdef PNG_READ_iTXt_SUPPORTED
  351. PNG_CONST PNG_iTXt;
  352. #endif
  353. #ifdef PNG_READ_oFFs_SUPPORTED
  354. PNG_CONST PNG_oFFs;
  355. #endif
  356. #ifdef PNG_READ_pCAL_SUPPORTED
  357. PNG_CONST PNG_pCAL;
  358. #endif
  359. #ifdef PNG_READ_pHYs_SUPPORTED
  360. PNG_CONST PNG_pHYs;
  361. #endif
  362. #ifdef PNG_READ_sBIT_SUPPORTED
  363. PNG_CONST PNG_sBIT;
  364. #endif
  365. #ifdef PNG_READ_sCAL_SUPPORTED
  366. PNG_CONST PNG_sCAL;
  367. #endif
  368. #ifdef PNG_READ_sPLT_SUPPORTED
  369. PNG_CONST PNG_sPLT;
  370. #endif
  371. #ifdef PNG_READ_sRGB_SUPPORTED
  372. PNG_CONST PNG_sRGB;
  373. #endif
  374. #ifdef PNG_READ_tEXt_SUPPORTED
  375. PNG_CONST PNG_tEXt;
  376. #endif
  377. #ifdef PNG_READ_tIME_SUPPORTED
  378. PNG_CONST PNG_tIME;
  379. #endif
  380. #ifdef PNG_READ_tRNS_SUPPORTED
  381. PNG_CONST PNG_tRNS;
  382. #endif
  383. #ifdef PNG_READ_zTXt_SUPPORTED
  384. PNG_CONST PNG_zTXt;
  385. #endif
  386. #endif /* PNG_USE_LOCAL_ARRAYS */
  387. png_uint_32 length = png_read_chunk_header(png_ptr);
  388. PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  389. /* This should be a binary subdivision search or a hash for
  390. * matching the chunk name rather than a linear search.
  391. */
  392. if (!png_memcmp(chunk_name, png_IDAT, 4))
  393. if (png_ptr->mode & PNG_AFTER_IDAT)
  394. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  395. if (!png_memcmp(chunk_name, png_IHDR, 4))
  396. png_handle_IHDR(png_ptr, info_ptr, length);
  397. else if (!png_memcmp(chunk_name, png_IEND, 4))
  398. png_handle_IEND(png_ptr, info_ptr, length);
  399. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  400. else if (png_handle_as_unknown(png_ptr, chunk_name))
  401. {
  402. if (!png_memcmp(chunk_name, png_IDAT, 4))
  403. png_ptr->mode |= PNG_HAVE_IDAT;
  404. png_handle_unknown(png_ptr, info_ptr, length);
  405. if (!png_memcmp(chunk_name, png_PLTE, 4))
  406. png_ptr->mode |= PNG_HAVE_PLTE;
  407. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  408. {
  409. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  410. png_error(png_ptr, "Missing IHDR before IDAT");
  411. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  412. !(png_ptr->mode & PNG_HAVE_PLTE))
  413. png_error(png_ptr, "Missing PLTE before IDAT");
  414. break;
  415. }
  416. }
  417. #endif
  418. else if (!png_memcmp(chunk_name, png_PLTE, 4))
  419. png_handle_PLTE(png_ptr, info_ptr, length);
  420. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  421. {
  422. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  423. png_error(png_ptr, "Missing IHDR before IDAT");
  424. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  425. !(png_ptr->mode & PNG_HAVE_PLTE))
  426. png_error(png_ptr, "Missing PLTE before IDAT");
  427. png_ptr->idat_size = length;
  428. png_ptr->mode |= PNG_HAVE_IDAT;
  429. break;
  430. }
  431. #ifdef PNG_READ_bKGD_SUPPORTED
  432. else if (!png_memcmp(chunk_name, png_bKGD, 4))
  433. png_handle_bKGD(png_ptr, info_ptr, length);
  434. #endif
  435. #ifdef PNG_READ_cHRM_SUPPORTED
  436. else if (!png_memcmp(chunk_name, png_cHRM, 4))
  437. png_handle_cHRM(png_ptr, info_ptr, length);
  438. #endif
  439. #ifdef PNG_READ_gAMA_SUPPORTED
  440. else if (!png_memcmp(chunk_name, png_gAMA, 4))
  441. png_handle_gAMA(png_ptr, info_ptr, length);
  442. #endif
  443. #ifdef PNG_READ_hIST_SUPPORTED
  444. else if (!png_memcmp(chunk_name, png_hIST, 4))
  445. png_handle_hIST(png_ptr, info_ptr, length);
  446. #endif
  447. #ifdef PNG_READ_oFFs_SUPPORTED
  448. else if (!png_memcmp(chunk_name, png_oFFs, 4))
  449. png_handle_oFFs(png_ptr, info_ptr, length);
  450. #endif
  451. #ifdef PNG_READ_pCAL_SUPPORTED
  452. else if (!png_memcmp(chunk_name, png_pCAL, 4))
  453. png_handle_pCAL(png_ptr, info_ptr, length);
  454. #endif
  455. #ifdef PNG_READ_sCAL_SUPPORTED
  456. else if (!png_memcmp(chunk_name, png_sCAL, 4))
  457. png_handle_sCAL(png_ptr, info_ptr, length);
  458. #endif
  459. #ifdef PNG_READ_pHYs_SUPPORTED
  460. else if (!png_memcmp(chunk_name, png_pHYs, 4))
  461. png_handle_pHYs(png_ptr, info_ptr, length);
  462. #endif
  463. #ifdef PNG_READ_sBIT_SUPPORTED
  464. else if (!png_memcmp(chunk_name, png_sBIT, 4))
  465. png_handle_sBIT(png_ptr, info_ptr, length);
  466. #endif
  467. #ifdef PNG_READ_sRGB_SUPPORTED
  468. else if (!png_memcmp(chunk_name, png_sRGB, 4))
  469. png_handle_sRGB(png_ptr, info_ptr, length);
  470. #endif
  471. #ifdef PNG_READ_iCCP_SUPPORTED
  472. else if (!png_memcmp(chunk_name, png_iCCP, 4))
  473. png_handle_iCCP(png_ptr, info_ptr, length);
  474. #endif
  475. #ifdef PNG_READ_sPLT_SUPPORTED
  476. else if (!png_memcmp(chunk_name, png_sPLT, 4))
  477. png_handle_sPLT(png_ptr, info_ptr, length);
  478. #endif
  479. #ifdef PNG_READ_tEXt_SUPPORTED
  480. else if (!png_memcmp(chunk_name, png_tEXt, 4))
  481. png_handle_tEXt(png_ptr, info_ptr, length);
  482. #endif
  483. #ifdef PNG_READ_tIME_SUPPORTED
  484. else if (!png_memcmp(chunk_name, png_tIME, 4))
  485. png_handle_tIME(png_ptr, info_ptr, length);
  486. #endif
  487. #ifdef PNG_READ_tRNS_SUPPORTED
  488. else if (!png_memcmp(chunk_name, png_tRNS, 4))
  489. png_handle_tRNS(png_ptr, info_ptr, length);
  490. #endif
  491. #ifdef PNG_READ_zTXt_SUPPORTED
  492. else if (!png_memcmp(chunk_name, png_zTXt, 4))
  493. png_handle_zTXt(png_ptr, info_ptr, length);
  494. #endif
  495. #ifdef PNG_READ_iTXt_SUPPORTED
  496. else if (!png_memcmp(chunk_name, png_iTXt, 4))
  497. png_handle_iTXt(png_ptr, info_ptr, length);
  498. #endif
  499. else
  500. png_handle_unknown(png_ptr, info_ptr, length);
  501. }
  502. }
  503. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  504. /* Optional call to update the users info_ptr structure */
  505. void PNGAPI
  506. png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  507. {
  508. png_debug(1, "in png_read_update_info");
  509. if (png_ptr == NULL)
  510. return;
  511. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  512. png_read_start_row(png_ptr);
  513. else
  514. png_warning(png_ptr,
  515. "Ignoring extra png_read_update_info() call; row buffer not reallocated");
  516. png_read_transform_info(png_ptr, info_ptr);
  517. }
  518. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  519. /* Initialize palette, background, etc, after transformations
  520. * are set, but before any reading takes place. This allows
  521. * the user to obtain a gamma-corrected palette, for example.
  522. * If the user doesn't call this, we will do it ourselves.
  523. */
  524. void PNGAPI
  525. png_start_read_image(png_structp png_ptr)
  526. {
  527. png_debug(1, "in png_start_read_image");
  528. if (png_ptr == NULL)
  529. return;
  530. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  531. png_read_start_row(png_ptr);
  532. }
  533. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  534. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  535. void PNGAPI
  536. png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  537. {
  538. PNG_CONST PNG_IDAT;
  539. PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  540. 0xff};
  541. PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  542. int ret;
  543. if (png_ptr == NULL)
  544. return;
  545. png_debug2(1, "in png_read_row (row %lu, pass %d)",
  546. png_ptr->row_number, png_ptr->pass);
  547. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  548. png_read_start_row(png_ptr);
  549. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  550. {
  551. /* Check for transforms that have been set but were defined out */
  552. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  553. if (png_ptr->transformations & PNG_INVERT_MONO)
  554. png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
  555. #endif
  556. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  557. if (png_ptr->transformations & PNG_FILLER)
  558. png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
  559. #endif
  560. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  561. !defined(PNG_READ_PACKSWAP_SUPPORTED)
  562. if (png_ptr->transformations & PNG_PACKSWAP)
  563. png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
  564. #endif
  565. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  566. if (png_ptr->transformations & PNG_PACK)
  567. png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
  568. #endif
  569. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  570. if (png_ptr->transformations & PNG_SHIFT)
  571. png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
  572. #endif
  573. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  574. if (png_ptr->transformations & PNG_BGR)
  575. png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
  576. #endif
  577. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  578. if (png_ptr->transformations & PNG_SWAP_BYTES)
  579. png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
  580. #endif
  581. }
  582. #ifdef PNG_READ_INTERLACING_SUPPORTED
  583. /* If interlaced and we do not need a new row, combine row and return */
  584. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  585. {
  586. switch (png_ptr->pass)
  587. {
  588. case 0:
  589. if (png_ptr->row_number & 0x07)
  590. {
  591. if (dsp_row != NULL)
  592. png_combine_row(png_ptr, dsp_row,
  593. png_pass_dsp_mask[png_ptr->pass]);
  594. png_read_finish_row(png_ptr);
  595. return;
  596. }
  597. break;
  598. case 1:
  599. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  600. {
  601. if (dsp_row != NULL)
  602. png_combine_row(png_ptr, dsp_row,
  603. png_pass_dsp_mask[png_ptr->pass]);
  604. png_read_finish_row(png_ptr);
  605. return;
  606. }
  607. break;
  608. case 2:
  609. if ((png_ptr->row_number & 0x07) != 4)
  610. {
  611. if (dsp_row != NULL && (png_ptr->row_number & 4))
  612. png_combine_row(png_ptr, dsp_row,
  613. png_pass_dsp_mask[png_ptr->pass]);
  614. png_read_finish_row(png_ptr);
  615. return;
  616. }
  617. break;
  618. case 3:
  619. if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  620. {
  621. if (dsp_row != NULL)
  622. png_combine_row(png_ptr, dsp_row,
  623. png_pass_dsp_mask[png_ptr->pass]);
  624. png_read_finish_row(png_ptr);
  625. return;
  626. }
  627. break;
  628. case 4:
  629. if ((png_ptr->row_number & 3) != 2)
  630. {
  631. if (dsp_row != NULL && (png_ptr->row_number & 2))
  632. png_combine_row(png_ptr, dsp_row,
  633. png_pass_dsp_mask[png_ptr->pass]);
  634. png_read_finish_row(png_ptr);
  635. return;
  636. }
  637. break;
  638. case 5:
  639. if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  640. {
  641. if (dsp_row != NULL)
  642. png_combine_row(png_ptr, dsp_row,
  643. png_pass_dsp_mask[png_ptr->pass]);
  644. png_read_finish_row(png_ptr);
  645. return;
  646. }
  647. break;
  648. case 6:
  649. if (!(png_ptr->row_number & 1))
  650. {
  651. png_read_finish_row(png_ptr);
  652. return;
  653. }
  654. break;
  655. }
  656. }
  657. #endif
  658. if (!(png_ptr->mode & PNG_HAVE_IDAT))
  659. png_error(png_ptr, "Invalid attempt to read row data");
  660. png_ptr->zstream.next_out = png_ptr->row_buf;
  661. png_ptr->zstream.avail_out =
  662. (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  663. png_ptr->iwidth) + 1);
  664. do
  665. {
  666. if (!(png_ptr->zstream.avail_in))
  667. {
  668. while (!png_ptr->idat_size)
  669. {
  670. png_crc_finish(png_ptr, 0);
  671. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  672. if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  673. png_error(png_ptr, "Not enough image data");
  674. }
  675. png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  676. png_ptr->zstream.next_in = png_ptr->zbuf;
  677. if (png_ptr->zbuf_size > png_ptr->idat_size)
  678. png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  679. png_crc_read(png_ptr, png_ptr->zbuf,
  680. (png_size_t)png_ptr->zstream.avail_in);
  681. png_ptr->idat_size -= png_ptr->zstream.avail_in;
  682. }
  683. ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  684. if (ret == Z_STREAM_END)
  685. {
  686. if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  687. png_ptr->idat_size)
  688. png_error(png_ptr, "Extra compressed data");
  689. png_ptr->mode |= PNG_AFTER_IDAT;
  690. png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  691. break;
  692. }
  693. if (ret != Z_OK)
  694. png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  695. "Decompression error");
  696. } while (png_ptr->zstream.avail_out);
  697. png_ptr->row_info.color_type = png_ptr->color_type;
  698. png_ptr->row_info.width = png_ptr->iwidth;
  699. png_ptr->row_info.channels = png_ptr->channels;
  700. png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  701. png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  702. png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  703. png_ptr->row_info.width);
  704. if (png_ptr->row_buf[0])
  705. png_read_filter_row(png_ptr, &(png_ptr->row_info),
  706. png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  707. (int)(png_ptr->row_buf[0]));
  708. png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  709. png_ptr->rowbytes + 1);
  710. #ifdef PNG_MNG_FEATURES_SUPPORTED
  711. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  712. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  713. {
  714. /* Intrapixel differencing */
  715. png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  716. }
  717. #endif
  718. if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
  719. png_do_read_transformations(png_ptr);
  720. #ifdef PNG_READ_INTERLACING_SUPPORTED
  721. /* Blow up interlaced rows to full size */
  722. if (png_ptr->interlaced &&
  723. (png_ptr->transformations & PNG_INTERLACE))
  724. {
  725. if (png_ptr->pass < 6)
  726. /* Old interface (pre-1.0.9):
  727. * png_do_read_interlace(&(png_ptr->row_info),
  728. * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  729. */
  730. png_do_read_interlace(png_ptr);
  731. if (dsp_row != NULL)
  732. png_combine_row(png_ptr, dsp_row,
  733. png_pass_dsp_mask[png_ptr->pass]);
  734. if (row != NULL)
  735. png_combine_row(png_ptr, row,
  736. png_pass_mask[png_ptr->pass]);
  737. }
  738. else
  739. #endif
  740. {
  741. if (row != NULL)
  742. png_combine_row(png_ptr, row, 0xff);
  743. if (dsp_row != NULL)
  744. png_combine_row(png_ptr, dsp_row, 0xff);
  745. }
  746. png_read_finish_row(png_ptr);
  747. if (png_ptr->read_row_fn != NULL)
  748. (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  749. }
  750. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  751. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  752. /* Read one or more rows of image data. If the image is interlaced,
  753. * and png_set_interlace_handling() has been called, the rows need to
  754. * contain the contents of the rows from the previous pass. If the
  755. * image has alpha or transparency, and png_handle_alpha()[*] has been
  756. * called, the rows contents must be initialized to the contents of the
  757. * screen.
  758. *
  759. * "row" holds the actual image, and pixels are placed in it
  760. * as they arrive. If the image is displayed after each pass, it will
  761. * appear to "sparkle" in. "display_row" can be used to display a
  762. * "chunky" progressive image, with finer detail added as it becomes
  763. * available. If you do not want this "chunky" display, you may pass
  764. * NULL for display_row. If you do not want the sparkle display, and
  765. * you have not called png_handle_alpha(), you may pass NULL for rows.
  766. * If you have called png_handle_alpha(), and the image has either an
  767. * alpha channel or a transparency chunk, you must provide a buffer for
  768. * rows. In this case, you do not have to provide a display_row buffer
  769. * also, but you may. If the image is not interlaced, or if you have
  770. * not called png_set_interlace_handling(), the display_row buffer will
  771. * be ignored, so pass NULL to it.
  772. *
  773. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  774. */
  775. void PNGAPI
  776. png_read_rows(png_structp png_ptr, png_bytepp row,
  777. png_bytepp display_row, png_uint_32 num_rows)
  778. {
  779. png_uint_32 i;
  780. png_bytepp rp;
  781. png_bytepp dp;
  782. png_debug(1, "in png_read_rows");
  783. if (png_ptr == NULL)
  784. return;
  785. rp = row;
  786. dp = display_row;
  787. if (rp != NULL && dp != NULL)
  788. for (i = 0; i < num_rows; i++)
  789. {
  790. png_bytep rptr = *rp++;
  791. png_bytep dptr = *dp++;
  792. png_read_row(png_ptr, rptr, dptr);
  793. }
  794. else if (rp != NULL)
  795. for (i = 0; i < num_rows; i++)
  796. {
  797. png_bytep rptr = *rp;
  798. png_read_row(png_ptr, rptr, png_bytep_NULL);
  799. rp++;
  800. }
  801. else if (dp != NULL)
  802. for (i = 0; i < num_rows; i++)
  803. {
  804. png_bytep dptr = *dp;
  805. png_read_row(png_ptr, png_bytep_NULL, dptr);
  806. dp++;
  807. }
  808. }
  809. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  810. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  811. /* Read the entire image. If the image has an alpha channel or a tRNS
  812. * chunk, and you have called png_handle_alpha()[*], you will need to
  813. * initialize the image to the current image that PNG will be overlaying.
  814. * We set the num_rows again here, in case it was incorrectly set in
  815. * png_read_start_row() by a call to png_read_update_info() or
  816. * png_start_read_image() if png_set_interlace_handling() wasn't called
  817. * prior to either of these functions like it should have been. You can
  818. * only call this function once. If you desire to have an image for
  819. * each pass of a interlaced image, use png_read_rows() instead.
  820. *
  821. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  822. */
  823. void PNGAPI
  824. png_read_image(png_structp png_ptr, png_bytepp image)
  825. {
  826. png_uint_32 i, image_height;
  827. int pass, j;
  828. png_bytepp rp;
  829. png_debug(1, "in png_read_image");
  830. if (png_ptr == NULL)
  831. return;
  832. #ifdef PNG_READ_INTERLACING_SUPPORTED
  833. pass = png_set_interlace_handling(png_ptr);
  834. #else
  835. if (png_ptr->interlaced)
  836. png_error(png_ptr,
  837. "Cannot read interlaced image -- interlace handler disabled.");
  838. pass = 1;
  839. #endif
  840. image_height=png_ptr->height;
  841. png_ptr->num_rows = image_height; /* Make sure this is set correctly */
  842. for (j = 0; j < pass; j++)
  843. {
  844. rp = image;
  845. for (i = 0; i < image_height; i++)
  846. {
  847. png_read_row(png_ptr, *rp, png_bytep_NULL);
  848. rp++;
  849. }
  850. }
  851. }
  852. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  853. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  854. /* Read the end of the PNG file. Will not read past the end of the
  855. * file, will verify the end is accurate, and will read any comments
  856. * or time information at the end of the file, if info is not NULL.
  857. */
  858. void PNGAPI
  859. png_read_end(png_structp png_ptr, png_infop info_ptr)
  860. {
  861. png_debug(1, "in png_read_end");
  862. if (png_ptr == NULL)
  863. return;
  864. png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  865. do
  866. {
  867. #ifdef PNG_USE_LOCAL_ARRAYS
  868. PNG_CONST PNG_IHDR;
  869. PNG_CONST PNG_IDAT;
  870. PNG_CONST PNG_IEND;
  871. PNG_CONST PNG_PLTE;
  872. #ifdef PNG_READ_bKGD_SUPPORTED
  873. PNG_CONST PNG_bKGD;
  874. #endif
  875. #ifdef PNG_READ_cHRM_SUPPORTED
  876. PNG_CONST PNG_cHRM;
  877. #endif
  878. #ifdef PNG_READ_gAMA_SUPPORTED
  879. PNG_CONST PNG_gAMA;
  880. #endif
  881. #ifdef PNG_READ_hIST_SUPPORTED
  882. PNG_CONST PNG_hIST;
  883. #endif
  884. #ifdef PNG_READ_iCCP_SUPPORTED
  885. PNG_CONST PNG_iCCP;
  886. #endif
  887. #ifdef PNG_READ_iTXt_SUPPORTED
  888. PNG_CONST PNG_iTXt;
  889. #endif
  890. #ifdef PNG_READ_oFFs_SUPPORTED
  891. PNG_CONST PNG_oFFs;
  892. #endif
  893. #ifdef PNG_READ_pCAL_SUPPORTED
  894. PNG_CONST PNG_pCAL;
  895. #endif
  896. #ifdef PNG_READ_pHYs_SUPPORTED
  897. PNG_CONST PNG_pHYs;
  898. #endif
  899. #ifdef PNG_READ_sBIT_SUPPORTED
  900. PNG_CONST PNG_sBIT;
  901. #endif
  902. #ifdef PNG_READ_sCAL_SUPPORTED
  903. PNG_CONST PNG_sCAL;
  904. #endif
  905. #ifdef PNG_READ_sPLT_SUPPORTED
  906. PNG_CONST PNG_sPLT;
  907. #endif
  908. #ifdef PNG_READ_sRGB_SUPPORTED
  909. PNG_CONST PNG_sRGB;
  910. #endif
  911. #ifdef PNG_READ_tEXt_SUPPORTED
  912. PNG_CONST PNG_tEXt;
  913. #endif
  914. #ifdef PNG_READ_tIME_SUPPORTED
  915. PNG_CONST PNG_tIME;
  916. #endif
  917. #ifdef PNG_READ_tRNS_SUPPORTED
  918. PNG_CONST PNG_tRNS;
  919. #endif
  920. #ifdef PNG_READ_zTXt_SUPPORTED
  921. PNG_CONST PNG_zTXt;
  922. #endif
  923. #endif /* PNG_USE_LOCAL_ARRAYS */
  924. png_uint_32 length = png_read_chunk_header(png_ptr);
  925. PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  926. if (!png_memcmp(chunk_name, png_IHDR, 4))
  927. png_handle_IHDR(png_ptr, info_ptr, length);
  928. else if (!png_memcmp(chunk_name, png_IEND, 4))
  929. png_handle_IEND(png_ptr, info_ptr, length);
  930. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  931. else if (png_handle_as_unknown(png_ptr, chunk_name))
  932. {
  933. if (!png_memcmp(chunk_name, png_IDAT, 4))
  934. {
  935. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  936. png_error(png_ptr, "Too many IDAT's found");
  937. }
  938. png_handle_unknown(png_ptr, info_ptr, length);
  939. if (!png_memcmp(chunk_name, png_PLTE, 4))
  940. png_ptr->mode |= PNG_HAVE_PLTE;
  941. }
  942. #endif
  943. else if (!png_memcmp(chunk_name, png_IDAT, 4))
  944. {
  945. /* Zero length IDATs are legal after the last IDAT has been
  946. * read, but not after other chunks have been read.
  947. */
  948. if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  949. png_error(png_ptr, "Too many IDAT's found");
  950. png_crc_finish(png_ptr, length);
  951. }
  952. else if (!png_memcmp(chunk_name, png_PLTE, 4))
  953. png_handle_PLTE(png_ptr, info_ptr, length);
  954. #ifdef PNG_READ_bKGD_SUPPORTED
  955. else if (!png_memcmp(chunk_name, png_bKGD, 4))
  956. png_handle_bKGD(png_ptr, info_ptr, length);
  957. #endif
  958. #ifdef PNG_READ_cHRM_SUPPORTED
  959. else if (!png_memcmp(chunk_name, png_cHRM, 4))
  960. png_handle_cHRM(png_ptr, info_ptr, length);
  961. #endif
  962. #ifdef PNG_READ_gAMA_SUPPORTED
  963. else if (!png_memcmp(chunk_name, png_gAMA, 4))
  964. png_handle_gAMA(png_ptr, info_ptr, length);
  965. #endif
  966. #ifdef PNG_READ_hIST_SUPPORTED
  967. else if (!png_memcmp(chunk_name, png_hIST, 4))
  968. png_handle_hIST(png_ptr, info_ptr, length);
  969. #endif
  970. #ifdef PNG_READ_oFFs_SUPPORTED
  971. else if (!png_memcmp(chunk_name, png_oFFs, 4))
  972. png_handle_oFFs(png_ptr, info_ptr, length);
  973. #endif
  974. #ifdef PNG_READ_pCAL_SUPPORTED
  975. else if (!png_memcmp(chunk_name, png_pCAL, 4))
  976. png_handle_pCAL(png_ptr, info_ptr, length);
  977. #endif
  978. #ifdef PNG_READ_sCAL_SUPPORTED
  979. else if (!png_memcmp(chunk_name, png_sCAL, 4))
  980. png_handle_sCAL(png_ptr, info_ptr, length);
  981. #endif
  982. #ifdef PNG_READ_pHYs_SUPPORTED
  983. else if (!png_memcmp(chunk_name, png_pHYs, 4))
  984. png_handle_pHYs(png_ptr, info_ptr, length);
  985. #endif
  986. #ifdef PNG_READ_sBIT_SUPPORTED
  987. else if (!png_memcmp(chunk_name, png_sBIT, 4))
  988. png_handle_sBIT(png_ptr, info_ptr, length);
  989. #endif
  990. #ifdef PNG_READ_sRGB_SUPPORTED
  991. else if (!png_memcmp(chunk_name, png_sRGB, 4))
  992. png_handle_sRGB(png_ptr, info_ptr, length);
  993. #endif
  994. #ifdef PNG_READ_iCCP_SUPPORTED
  995. else if (!png_memcmp(chunk_name, png_iCCP, 4))
  996. png_handle_iCCP(png_ptr, info_ptr, length);
  997. #endif
  998. #ifdef PNG_READ_sPLT_SUPPORTED
  999. else if (!png_memcmp(chunk_name, png_sPLT, 4))
  1000. png_handle_sPLT(png_ptr, info_ptr, length);
  1001. #endif
  1002. #ifdef PNG_READ_tEXt_SUPPORTED
  1003. else if (!png_memcmp(chunk_name, png_tEXt, 4))
  1004. png_handle_tEXt(png_ptr, info_ptr, length);
  1005. #endif
  1006. #ifdef PNG_READ_tIME_SUPPORTED
  1007. else if (!png_memcmp(chunk_name, png_tIME, 4))
  1008. png_handle_tIME(png_ptr, info_ptr, length);
  1009. #endif
  1010. #ifdef PNG_READ_tRNS_SUPPORTED
  1011. else if (!png_memcmp(chunk_name, png_tRNS, 4))
  1012. png_handle_tRNS(png_ptr, info_ptr, length);
  1013. #endif
  1014. #ifdef PNG_READ_zTXt_SUPPORTED
  1015. else if (!png_memcmp(chunk_name, png_zTXt, 4))
  1016. png_handle_zTXt(png_ptr, info_ptr, length);
  1017. #endif
  1018. #ifdef PNG_READ_iTXt_SUPPORTED
  1019. else if (!png_memcmp(chunk_name, png_iTXt, 4))
  1020. png_handle_iTXt(png_ptr, info_ptr, length);
  1021. #endif
  1022. else
  1023. png_handle_unknown(png_ptr, info_ptr, length);
  1024. } while (!(png_ptr->mode & PNG_HAVE_IEND));
  1025. }
  1026. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  1027. /* Free all memory used by the read */
  1028. void PNGAPI
  1029. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  1030. png_infopp end_info_ptr_ptr)
  1031. {
  1032. png_structp png_ptr = NULL;
  1033. png_infop info_ptr = NULL, end_info_ptr = NULL;
  1034. #ifdef PNG_USER_MEM_SUPPORTED
  1035. png_free_ptr free_fn = NULL;
  1036. png_voidp mem_ptr = NULL;
  1037. #endif
  1038. png_debug(1, "in png_destroy_read_struct");
  1039. if (png_ptr_ptr != NULL)
  1040. png_ptr = *png_ptr_ptr;
  1041. if (png_ptr == NULL)
  1042. return;
  1043. #ifdef PNG_USER_MEM_SUPPORTED
  1044. free_fn = png_ptr->free_fn;
  1045. mem_ptr = png_ptr->mem_ptr;
  1046. #endif
  1047. if (info_ptr_ptr != NULL)
  1048. info_ptr = *info_ptr_ptr;
  1049. if (end_info_ptr_ptr != NULL)
  1050. end_info_ptr = *end_info_ptr_ptr;
  1051. png_read_destroy(png_ptr, info_ptr, end_info_ptr);
  1052. if (info_ptr != NULL)
  1053. {
  1054. #ifdef PNG_TEXT_SUPPORTED
  1055. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
  1056. #endif
  1057. #ifdef PNG_USER_MEM_SUPPORTED
  1058. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  1059. (png_voidp)mem_ptr);
  1060. #else
  1061. png_destroy_struct((png_voidp)info_ptr);
  1062. #endif
  1063. *info_ptr_ptr = NULL;
  1064. }
  1065. if (end_info_ptr != NULL)
  1066. {
  1067. #ifdef PNG_READ_TEXT_SUPPORTED
  1068. png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
  1069. #endif
  1070. #ifdef PNG_USER_MEM_SUPPORTED
  1071. png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
  1072. (png_voidp)mem_ptr);
  1073. #else
  1074. png_destroy_struct((png_voidp)end_info_ptr);
  1075. #endif
  1076. *end_info_ptr_ptr = NULL;
  1077. }
  1078. if (png_ptr != NULL)
  1079. {
  1080. #ifdef PNG_USER_MEM_SUPPORTED
  1081. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  1082. (png_voidp)mem_ptr);
  1083. #else
  1084. png_destroy_struct((png_voidp)png_ptr);
  1085. #endif
  1086. *png_ptr_ptr = NULL;
  1087. }
  1088. }
  1089. /* Free all memory used by the read (old method) */
  1090. void /* PRIVATE */
  1091. png_read_destroy(png_structp png_ptr, png_infop info_ptr,
  1092. png_infop end_info_ptr)
  1093. {
  1094. #ifdef PNG_SETJMP_SUPPORTED
  1095. jmp_buf tmp_jmp;
  1096. #endif
  1097. png_error_ptr error_fn;
  1098. png_error_ptr warning_fn;
  1099. png_voidp error_ptr;
  1100. #ifdef PNG_USER_MEM_SUPPORTED
  1101. png_free_ptr free_fn;
  1102. #endif
  1103. png_debug(1, "in png_read_destroy");
  1104. if (info_ptr != NULL)
  1105. png_info_destroy(png_ptr, info_ptr);
  1106. if (end_info_ptr != NULL)
  1107. png_info_destroy(png_ptr, end_info_ptr);
  1108. png_free(png_ptr, png_ptr->zbuf);
  1109. png_free(png_ptr, png_ptr->big_row_buf);
  1110. png_free(png_ptr, png_ptr->prev_row);
  1111. png_free(png_ptr, png_ptr->chunkdata);
  1112. #ifdef PNG_READ_DITHER_SUPPORTED
  1113. png_free(png_ptr, png_ptr->palette_lookup);
  1114. png_free(png_ptr, png_ptr->dither_index);
  1115. #endif
  1116. #ifdef PNG_READ_GAMMA_SUPPORTED
  1117. png_free(png_ptr, png_ptr->gamma_table);
  1118. #endif
  1119. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1120. png_free(png_ptr, png_ptr->gamma_from_1);
  1121. png_free(png_ptr, png_ptr->gamma_to_1);
  1122. #endif
  1123. #ifdef PNG_FREE_ME_SUPPORTED
  1124. if (png_ptr->free_me & PNG_FREE_PLTE)
  1125. png_zfree(png_ptr, png_ptr->palette);
  1126. png_ptr->free_me &= ~PNG_FREE_PLTE;
  1127. #else
  1128. if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
  1129. png_zfree(png_ptr, png_ptr->palette);
  1130. png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  1131. #endif
  1132. #if defined(PNG_tRNS_SUPPORTED) || \
  1133. defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  1134. #ifdef PNG_FREE_ME_SUPPORTED
  1135. if (png_ptr->free_me & PNG_FREE_TRNS)
  1136. png_free(png_ptr, png_ptr->trans);
  1137. png_ptr->free_me &= ~PNG_FREE_TRNS;
  1138. #else
  1139. if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
  1140. png_free(png_ptr, png_ptr->trans);
  1141. png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  1142. #endif
  1143. #endif
  1144. #ifdef PNG_READ_hIST_SUPPORTED
  1145. #ifdef PNG_FREE_ME_SUPPORTED
  1146. if (png_ptr->free_me & PNG_FREE_HIST)
  1147. png_free(png_ptr, png_ptr->hist);
  1148. png_ptr->free_me &= ~PNG_FREE_HIST;
  1149. #else
  1150. if (png_ptr->flags & PNG_FLAG_FREE_HIST)
  1151. png_free(png_ptr, png_ptr->hist);
  1152. png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  1153. #endif
  1154. #endif
  1155. #ifdef PNG_READ_GAMMA_SUPPORTED
  1156. if (png_ptr->gamma_16_table != NULL)
  1157. {
  1158. int i;
  1159. int istop = (1 << (8 - png_ptr->gamma_shift));
  1160. for (i = 0; i < istop; i++)
  1161. {
  1162. png_free(png_ptr, png_ptr->gamma_16_table[i]);
  1163. }
  1164. png_free(png_ptr, png_ptr->gamma_16_table);
  1165. }
  1166. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1167. if (png_ptr->gamma_16_from_1 != NULL)
  1168. {
  1169. int i;
  1170. int istop = (1 << (8 - png_ptr->gamma_shift));
  1171. for (i = 0; i < istop; i++)
  1172. {
  1173. png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
  1174. }
  1175. png_free(png_ptr, png_ptr->gamma_16_from_1);
  1176. }
  1177. if (png_ptr->gamma_16_to_1 != NULL)
  1178. {
  1179. int i;
  1180. int istop = (1 << (8 - png_ptr->gamma_shift));
  1181. for (i = 0; i < istop; i++)
  1182. {
  1183. png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
  1184. }
  1185. png_free(png_ptr, png_ptr->gamma_16_to_1);
  1186. }
  1187. #endif
  1188. #endif
  1189. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1190. png_free(png_ptr, png_ptr->time_buffer);
  1191. #endif
  1192. inflateEnd(&png_ptr->zstream);
  1193. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1194. png_free(png_ptr, png_ptr->save_buffer);
  1195. #endif
  1196. /* Save the important info out of the png_struct, in case it is
  1197. * being used again.
  1198. */
  1199. #ifdef PNG_SETJMP_SUPPORTED
  1200. png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  1201. #endif
  1202. error_fn = png_ptr->error_fn;
  1203. warning_fn = png_ptr->warning_fn;
  1204. error_ptr = png_ptr->error_ptr;
  1205. #ifdef PNG_USER_MEM_SUPPORTED
  1206. free_fn = png_ptr->free_fn;
  1207. #endif
  1208. png_memset(png_ptr, 0, png_sizeof(png_struct));
  1209. png_ptr->error_fn = error_fn;
  1210. png_ptr->warning_fn = warning_fn;
  1211. png_ptr->error_ptr = error_ptr;
  1212. #ifdef PNG_USER_MEM_SUPPORTED
  1213. png_ptr->free_fn = free_fn;
  1214. #endif
  1215. #ifdef PNG_SETJMP_SUPPORTED
  1216. png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  1217. #endif
  1218. }
  1219. void PNGAPI
  1220. png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
  1221. {
  1222. if (png_ptr == NULL)
  1223. return;
  1224. png_ptr->read_row_fn = read_row_fn;
  1225. }
  1226. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  1227. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1228. void PNGAPI
  1229. png_read_png(png_structp png_ptr, png_infop info_ptr,
  1230. int transforms,
  1231. voidp params)
  1232. {
  1233. int row;
  1234. if (png_ptr == NULL)
  1235. return;
  1236. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1237. /* Invert the alpha channel from opacity to transparency
  1238. */
  1239. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1240. png_set_invert_alpha(png_ptr);
  1241. #endif
  1242. /* png_read_info() gives us all of the information from the
  1243. * PNG file before the first IDAT (image data chunk).
  1244. */
  1245. png_read_info(png_ptr, info_ptr);
  1246. if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
  1247. png_error(png_ptr, "Image is too high to process with png_read_png()");
  1248. /* -------------- image transformations start here ------------------- */
  1249. #ifdef PNG_READ_16_TO_8_SUPPORTED
  1250. /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
  1251. */
  1252. if (transforms & PNG_TRANSFORM_STRIP_16)
  1253. png_set_strip_16(png_ptr);
  1254. #endif
  1255. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1256. /* Strip alpha bytes from the input data without combining with
  1257. * the background (not recommended).
  1258. */
  1259. if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
  1260. png_set_strip_alpha(png_ptr);
  1261. #endif
  1262. #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
  1263. /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  1264. * byte into separate bytes (useful for paletted and grayscale images).
  1265. */
  1266. if (transforms & PNG_TRANSFORM_PACKING)
  1267. png_set_packing(png_ptr);
  1268. #endif
  1269. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  1270. /* Change the order of packed pixels to least significant bit first
  1271. * (not useful if you are using png_set_packing).
  1272. */
  1273. if (transforms & PNG_TRANSFORM_PACKSWAP)
  1274. png_set_packswap(png_ptr);
  1275. #endif
  1276. #ifdef PNG_READ_EXPAND_SUPPORTED
  1277. /* Expand paletted colors into true RGB triplets
  1278. * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  1279. * Expand paletted or RGB images with transparency to full alpha
  1280. * channels so the data will be available as RGBA quartets.
  1281. */
  1282. if (transforms & PNG_TRANSFORM_EXPAND)
  1283. if ((png_ptr->bit_depth < 8) ||
  1284. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  1285. (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
  1286. png_set_expand(png_ptr);
  1287. #endif
  1288. /* We don't handle background color or gamma transformation or dithering.
  1289. */
  1290. #ifdef PNG_READ_INVERT_SUPPORTED
  1291. /* Invert monochrome files to have 0 as white and 1 as black
  1292. */
  1293. if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1294. png_set_invert_mono(png_ptr);
  1295. #endif
  1296. #ifdef PNG_READ_SHIFT_SUPPORTED
  1297. /* If you want to shift the pixel values from the range [0,255] or
  1298. * [0,65535] to the original [0,7] or [0,31], or whatever range the
  1299. * colors were originally in:
  1300. */
  1301. if ((transforms & PNG_TRANSFORM_SHIFT)
  1302. && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
  1303. {
  1304. png_color_8p sig_bit;
  1305. png_get_sBIT(png_ptr, info_ptr, &sig_bit);
  1306. png_set_shift(png_ptr, sig_bit);
  1307. }
  1308. #endif
  1309. #ifdef PNG_READ_BGR_SUPPORTED
  1310. /* Flip the RGB pixels to BGR (or RGBA to BGRA)
  1311. */
  1312. if (transforms & PNG_TRANSFORM_BGR)
  1313. png_set_bgr(png_ptr);
  1314. #endif
  1315. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  1316. /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
  1317. */
  1318. if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1319. png_set_swap_alpha(png_ptr);
  1320. #endif
  1321. #ifdef PNG_READ_SWAP_SUPPORTED
  1322. /* Swap bytes of 16 bit files to least significant byte first
  1323. */
  1324. if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1325. png_set_swap(png_ptr);
  1326. #endif
  1327. /* Added at libpng-1.2.41 */
  1328. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1329. /* Invert the alpha channel from opacity to transparency
  1330. */
  1331. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1332. png_set_invert_alpha(png_ptr);
  1333. #endif
  1334. /* Added at libpng-1.2.41 */
  1335. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1336. /* Expand grayscale image to RGB
  1337. */
  1338. if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
  1339. png_set_gray_to_rgb(png_ptr);
  1340. #endif
  1341. /* We don't handle adding filler bytes */
  1342. /* Optional call to gamma correct and add the background to the palette
  1343. * and update info structure. REQUIRED if you are expecting libpng to
  1344. * update the palette for you (i.e., you selected such a transform above).
  1345. */
  1346. png_read_update_info(png_ptr, info_ptr);
  1347. /* -------------- image transformations end here ------------------- */
  1348. #ifdef PNG_FREE_ME_SUPPORTED
  1349. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1350. #endif
  1351. if (info_ptr->row_pointers == NULL)
  1352. {
  1353. info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
  1354. info_ptr->height * png_sizeof(png_bytep));
  1355. png_memset(info_ptr->row_pointers, 0, info_ptr->height
  1356. * png_sizeof(png_bytep));
  1357. #ifdef PNG_FREE_ME_SUPPORTED
  1358. info_ptr->free_me |= PNG_FREE_ROWS;
  1359. #endif
  1360. for (row = 0; row < (int)info_ptr->height; row++)
  1361. info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
  1362. png_get_rowbytes(png_ptr, info_ptr));
  1363. }
  1364. png_read_image(png_ptr, info_ptr->row_pointers);
  1365. info_ptr->valid |= PNG_INFO_IDAT;
  1366. /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1367. png_read_end(png_ptr, info_ptr);
  1368. transforms = transforms; /* Quiet compiler warnings */
  1369. params = params;
  1370. }
  1371. #endif /* PNG_INFO_IMAGE_SUPPORTED */
  1372. #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
  1373. #endif /* PNG_READ_SUPPORTED */