pngread.c 38 KB

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