pngpread.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /* pngpread.c - read a png file in push mode
  2. *
  3. * Last changed in libpng 1.6.24 [August 4, 2016]
  4. * Copyright (c) 1998-2002,2004,2006-2016 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. #include "pngpriv.h"
  13. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  14. /* Push model modes */
  15. #define PNG_READ_SIG_MODE 0
  16. #define PNG_READ_CHUNK_MODE 1
  17. #define PNG_READ_IDAT_MODE 2
  18. #define PNG_READ_tEXt_MODE 4
  19. #define PNG_READ_zTXt_MODE 5
  20. #define PNG_READ_DONE_MODE 6
  21. #define PNG_READ_iTXt_MODE 7
  22. #define PNG_ERROR_MODE 8
  23. #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
  24. if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
  25. { png_push_save_buffer(png_ptr); return; }
  26. #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
  27. if (png_ptr->buffer_size < N) \
  28. { png_push_save_buffer(png_ptr); return; }
  29. void PNGAPI
  30. png_process_data(png_structrp png_ptr, png_inforp info_ptr,
  31. png_bytep buffer, png_size_t buffer_size)
  32. {
  33. if (png_ptr == NULL || info_ptr == NULL)
  34. return;
  35. png_push_restore_buffer(png_ptr, buffer, buffer_size);
  36. while (png_ptr->buffer_size)
  37. {
  38. png_process_some_data(png_ptr, info_ptr);
  39. }
  40. }
  41. png_size_t PNGAPI
  42. png_process_data_pause(png_structrp png_ptr, int save)
  43. {
  44. if (png_ptr != NULL)
  45. {
  46. /* It's easiest for the caller if we do the save; then the caller doesn't
  47. * have to supply the same data again:
  48. */
  49. if (save != 0)
  50. png_push_save_buffer(png_ptr);
  51. else
  52. {
  53. /* This includes any pending saved bytes: */
  54. png_size_t remaining = png_ptr->buffer_size;
  55. png_ptr->buffer_size = 0;
  56. /* So subtract the saved buffer size, unless all the data
  57. * is actually 'saved', in which case we just return 0
  58. */
  59. if (png_ptr->save_buffer_size < remaining)
  60. return remaining - png_ptr->save_buffer_size;
  61. }
  62. }
  63. return 0;
  64. }
  65. png_uint_32 PNGAPI
  66. png_process_data_skip(png_structrp png_ptr)
  67. {
  68. /* TODO: Deprecate and remove this API.
  69. * Somewhere the implementation of this seems to have been lost,
  70. * or abandoned. It was only to support some internal back-door access
  71. * to png_struct) in libpng-1.4.x.
  72. */
  73. png_app_warning(png_ptr,
  74. "png_process_data_skip is not implemented in any current version of libpng");
  75. return 0;
  76. }
  77. /* What we do with the incoming data depends on what we were previously
  78. * doing before we ran out of data...
  79. */
  80. void /* PRIVATE */
  81. png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
  82. {
  83. if (png_ptr == NULL)
  84. return;
  85. switch (png_ptr->process_mode)
  86. {
  87. case PNG_READ_SIG_MODE:
  88. {
  89. png_push_read_sig(png_ptr, info_ptr);
  90. break;
  91. }
  92. case PNG_READ_CHUNK_MODE:
  93. {
  94. png_push_read_chunk(png_ptr, info_ptr);
  95. break;
  96. }
  97. case PNG_READ_IDAT_MODE:
  98. {
  99. png_push_read_IDAT(png_ptr);
  100. break;
  101. }
  102. default:
  103. {
  104. png_ptr->buffer_size = 0;
  105. break;
  106. }
  107. }
  108. }
  109. /* Read any remaining signature bytes from the stream and compare them with
  110. * the correct PNG signature. It is possible that this routine is called
  111. * with bytes already read from the signature, either because they have been
  112. * checked by the calling application, or because of multiple calls to this
  113. * routine.
  114. */
  115. void /* PRIVATE */
  116. png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
  117. {
  118. png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
  119. num_to_check = 8 - num_checked;
  120. if (png_ptr->buffer_size < num_to_check)
  121. {
  122. num_to_check = png_ptr->buffer_size;
  123. }
  124. png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
  125. num_to_check);
  126. png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
  127. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  128. {
  129. if (num_checked < 4 &&
  130. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  131. png_error(png_ptr, "Not a PNG file");
  132. else
  133. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  134. }
  135. else
  136. {
  137. if (png_ptr->sig_bytes >= 8)
  138. {
  139. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  140. }
  141. }
  142. }
  143. void /* PRIVATE */
  144. png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
  145. {
  146. png_uint_32 chunk_name;
  147. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  148. int keep; /* unknown handling method */
  149. #endif
  150. /* First we make sure we have enough data for the 4-byte chunk name
  151. * and the 4-byte chunk length before proceeding with decoding the
  152. * chunk data. To fully decode each of these chunks, we also make
  153. * sure we have enough data in the buffer for the 4-byte CRC at the
  154. * end of every chunk (except IDAT, which is handled separately).
  155. */
  156. if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
  157. {
  158. png_byte chunk_length[4];
  159. png_byte chunk_tag[4];
  160. PNG_PUSH_SAVE_BUFFER_IF_LT(8)
  161. png_push_fill_buffer(png_ptr, chunk_length, 4);
  162. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  163. png_reset_crc(png_ptr);
  164. png_crc_read(png_ptr, chunk_tag, 4);
  165. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  166. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  167. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  168. }
  169. chunk_name = png_ptr->chunk_name;
  170. #ifdef PNG_READ_APNG_SUPPORTED
  171. if (png_ptr->num_frames_read > 0 &&
  172. png_ptr->num_frames_read < info_ptr->num_frames)
  173. {
  174. if (chunk_name == png_IDAT)
  175. {
  176. /* Discard trailing IDATs for the first frame */
  177. if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
  178. png_ptr->num_frames_read > 1)
  179. png_error(png_ptr, "out of place IDAT");
  180. PNG_PUSH_SAVE_BUFFER_IF_FULL
  181. png_crc_finish(png_ptr, png_ptr->push_length);
  182. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  183. }
  184. else if (chunk_name == png_fdAT)
  185. {
  186. PNG_PUSH_SAVE_BUFFER_IF_LT(4)
  187. png_ensure_sequence_number(png_ptr, 4);
  188. if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
  189. {
  190. /* Discard trailing fdATs for frames other than the first */
  191. if (png_ptr->num_frames_read < 2)
  192. png_error(png_ptr, "out of place fdAT");
  193. PNG_PUSH_SAVE_BUFFER_IF_FULL
  194. png_crc_finish(png_ptr, png_ptr->push_length);
  195. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  196. }
  197. else
  198. {
  199. /* frame data follows */
  200. png_ptr->idat_size = png_ptr->push_length - 4;
  201. png_ptr->mode |= PNG_HAVE_IDAT;
  202. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  203. }
  204. }
  205. else if (chunk_name == png_fcTL)
  206. {
  207. PNG_PUSH_SAVE_BUFFER_IF_FULL
  208. png_read_reset(png_ptr);
  209. png_ptr->mode &= ~PNG_HAVE_fcTL;
  210. png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
  211. if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
  212. png_error(png_ptr, "missing required fcTL chunk");
  213. png_read_reinit(png_ptr, info_ptr);
  214. png_progressive_read_reset(png_ptr);
  215. if (png_ptr->frame_info_fn != NULL)
  216. (*(png_ptr->frame_info_fn))(png_ptr, png_ptr->num_frames_read);
  217. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  218. }
  219. else if (chunk_name == png_IEND)
  220. {
  221. PNG_PUSH_SAVE_BUFFER_IF_FULL
  222. png_warning(png_ptr, "Number of actual frames fewer than expected");
  223. png_crc_finish(png_ptr, png_ptr->push_length);
  224. png_ptr->process_mode = PNG_READ_DONE_MODE;
  225. png_push_have_end(png_ptr, info_ptr);
  226. }
  227. else
  228. {
  229. PNG_PUSH_SAVE_BUFFER_IF_FULL
  230. png_warning(png_ptr, "Skipped (ignored) a chunk "
  231. "between APNG chunks");
  232. png_crc_finish(png_ptr, png_ptr->push_length);
  233. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  234. }
  235. return;
  236. }
  237. #endif /* READ_APNG */
  238. if (chunk_name == png_IDAT)
  239. {
  240. if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  241. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  242. /* If we reach an IDAT chunk, this means we have read all of the
  243. * header chunks, and we can start reading the image (or if this
  244. * is called after the image has been read - we have an error).
  245. */
  246. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  247. png_error(png_ptr, "Missing IHDR before IDAT");
  248. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  249. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  250. png_error(png_ptr, "Missing PLTE before IDAT");
  251. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  252. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  253. if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
  254. if (png_ptr->push_length == 0)
  255. return;
  256. png_ptr->mode |= PNG_HAVE_IDAT;
  257. if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  258. png_benign_error(png_ptr, "Too many IDATs found");
  259. }
  260. if (chunk_name == png_IHDR)
  261. {
  262. if (png_ptr->push_length != 13)
  263. png_error(png_ptr, "Invalid IHDR length");
  264. PNG_PUSH_SAVE_BUFFER_IF_FULL
  265. png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
  266. }
  267. else if (chunk_name == png_IEND)
  268. {
  269. PNG_PUSH_SAVE_BUFFER_IF_FULL
  270. png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
  271. png_ptr->process_mode = PNG_READ_DONE_MODE;
  272. png_push_have_end(png_ptr, info_ptr);
  273. }
  274. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  275. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  276. {
  277. PNG_PUSH_SAVE_BUFFER_IF_FULL
  278. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
  279. if (chunk_name == png_PLTE)
  280. png_ptr->mode |= PNG_HAVE_PLTE;
  281. }
  282. #endif
  283. else if (chunk_name == png_PLTE)
  284. {
  285. PNG_PUSH_SAVE_BUFFER_IF_FULL
  286. png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
  287. }
  288. else if (chunk_name == png_IDAT)
  289. {
  290. #ifdef PNG_READ_APNG_SUPPORTED
  291. png_have_info(png_ptr, info_ptr);
  292. #endif
  293. png_ptr->idat_size = png_ptr->push_length;
  294. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  295. png_push_have_info(png_ptr, info_ptr);
  296. png_ptr->zstream.avail_out =
  297. (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
  298. png_ptr->iwidth) + 1;
  299. png_ptr->zstream.next_out = png_ptr->row_buf;
  300. return;
  301. }
  302. #ifdef PNG_READ_gAMA_SUPPORTED
  303. else if (png_ptr->chunk_name == png_gAMA)
  304. {
  305. PNG_PUSH_SAVE_BUFFER_IF_FULL
  306. png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
  307. }
  308. #endif
  309. #ifdef PNG_READ_sBIT_SUPPORTED
  310. else if (png_ptr->chunk_name == png_sBIT)
  311. {
  312. PNG_PUSH_SAVE_BUFFER_IF_FULL
  313. png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
  314. }
  315. #endif
  316. #ifdef PNG_READ_cHRM_SUPPORTED
  317. else if (png_ptr->chunk_name == png_cHRM)
  318. {
  319. PNG_PUSH_SAVE_BUFFER_IF_FULL
  320. png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
  321. }
  322. #endif
  323. #ifdef PNG_READ_sRGB_SUPPORTED
  324. else if (chunk_name == png_sRGB)
  325. {
  326. PNG_PUSH_SAVE_BUFFER_IF_FULL
  327. png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
  328. }
  329. #endif
  330. #ifdef PNG_READ_iCCP_SUPPORTED
  331. else if (png_ptr->chunk_name == png_iCCP)
  332. {
  333. PNG_PUSH_SAVE_BUFFER_IF_FULL
  334. png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
  335. }
  336. #endif
  337. #ifdef PNG_READ_sPLT_SUPPORTED
  338. else if (chunk_name == png_sPLT)
  339. {
  340. PNG_PUSH_SAVE_BUFFER_IF_FULL
  341. png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
  342. }
  343. #endif
  344. #ifdef PNG_READ_tRNS_SUPPORTED
  345. else if (chunk_name == png_tRNS)
  346. {
  347. PNG_PUSH_SAVE_BUFFER_IF_FULL
  348. png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
  349. }
  350. #endif
  351. #ifdef PNG_READ_bKGD_SUPPORTED
  352. else if (chunk_name == png_bKGD)
  353. {
  354. PNG_PUSH_SAVE_BUFFER_IF_FULL
  355. png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
  356. }
  357. #endif
  358. #ifdef PNG_READ_hIST_SUPPORTED
  359. else if (chunk_name == png_hIST)
  360. {
  361. PNG_PUSH_SAVE_BUFFER_IF_FULL
  362. png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
  363. }
  364. #endif
  365. #ifdef PNG_READ_pHYs_SUPPORTED
  366. else if (chunk_name == png_pHYs)
  367. {
  368. PNG_PUSH_SAVE_BUFFER_IF_FULL
  369. png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
  370. }
  371. #endif
  372. #ifdef PNG_READ_oFFs_SUPPORTED
  373. else if (chunk_name == png_oFFs)
  374. {
  375. PNG_PUSH_SAVE_BUFFER_IF_FULL
  376. png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
  377. }
  378. #endif
  379. #ifdef PNG_READ_pCAL_SUPPORTED
  380. else if (chunk_name == png_pCAL)
  381. {
  382. PNG_PUSH_SAVE_BUFFER_IF_FULL
  383. png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
  384. }
  385. #endif
  386. #ifdef PNG_READ_sCAL_SUPPORTED
  387. else if (chunk_name == png_sCAL)
  388. {
  389. PNG_PUSH_SAVE_BUFFER_IF_FULL
  390. png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
  391. }
  392. #endif
  393. #ifdef PNG_READ_tIME_SUPPORTED
  394. else if (chunk_name == png_tIME)
  395. {
  396. PNG_PUSH_SAVE_BUFFER_IF_FULL
  397. png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
  398. }
  399. #endif
  400. #ifdef PNG_READ_tEXt_SUPPORTED
  401. else if (chunk_name == png_tEXt)
  402. {
  403. PNG_PUSH_SAVE_BUFFER_IF_FULL
  404. png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
  405. }
  406. #endif
  407. #ifdef PNG_READ_zTXt_SUPPORTED
  408. else if (chunk_name == png_zTXt)
  409. {
  410. PNG_PUSH_SAVE_BUFFER_IF_FULL
  411. png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
  412. }
  413. #endif
  414. #ifdef PNG_READ_iTXt_SUPPORTED
  415. else if (chunk_name == png_iTXt)
  416. {
  417. PNG_PUSH_SAVE_BUFFER_IF_FULL
  418. png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
  419. }
  420. #endif
  421. #ifdef PNG_READ_APNG_SUPPORTED
  422. else if (chunk_name == png_acTL)
  423. {
  424. PNG_PUSH_SAVE_BUFFER_IF_FULL
  425. png_handle_acTL(png_ptr, info_ptr, png_ptr->push_length);
  426. }
  427. else if (chunk_name == png_fcTL)
  428. {
  429. PNG_PUSH_SAVE_BUFFER_IF_FULL
  430. png_handle_fcTL(png_ptr, info_ptr, png_ptr->push_length);
  431. }
  432. #endif /* READ_APNG */
  433. else
  434. {
  435. PNG_PUSH_SAVE_BUFFER_IF_FULL
  436. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
  437. PNG_HANDLE_CHUNK_AS_DEFAULT);
  438. }
  439. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  440. }
  441. void PNGCBAPI
  442. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
  443. {
  444. png_bytep ptr;
  445. if (png_ptr == NULL)
  446. return;
  447. ptr = buffer;
  448. if (png_ptr->save_buffer_size != 0)
  449. {
  450. png_size_t save_size;
  451. if (length < png_ptr->save_buffer_size)
  452. save_size = length;
  453. else
  454. save_size = png_ptr->save_buffer_size;
  455. memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  456. length -= save_size;
  457. ptr += save_size;
  458. png_ptr->buffer_size -= save_size;
  459. png_ptr->save_buffer_size -= save_size;
  460. png_ptr->save_buffer_ptr += save_size;
  461. }
  462. if (length != 0 && png_ptr->current_buffer_size != 0)
  463. {
  464. png_size_t save_size;
  465. if (length < png_ptr->current_buffer_size)
  466. save_size = length;
  467. else
  468. save_size = png_ptr->current_buffer_size;
  469. memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  470. png_ptr->buffer_size -= save_size;
  471. png_ptr->current_buffer_size -= save_size;
  472. png_ptr->current_buffer_ptr += save_size;
  473. }
  474. }
  475. void /* PRIVATE */
  476. png_push_save_buffer(png_structrp png_ptr)
  477. {
  478. if (png_ptr->save_buffer_size != 0)
  479. {
  480. if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  481. {
  482. png_size_t i, istop;
  483. png_bytep sp;
  484. png_bytep dp;
  485. istop = png_ptr->save_buffer_size;
  486. for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  487. i < istop; i++, sp++, dp++)
  488. {
  489. *dp = *sp;
  490. }
  491. }
  492. }
  493. if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  494. png_ptr->save_buffer_max)
  495. {
  496. png_size_t new_max;
  497. png_bytep old_buffer;
  498. if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  499. (png_ptr->current_buffer_size + 256))
  500. {
  501. png_error(png_ptr, "Potential overflow of save_buffer");
  502. }
  503. new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  504. old_buffer = png_ptr->save_buffer;
  505. png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
  506. (png_size_t)new_max);
  507. if (png_ptr->save_buffer == NULL)
  508. {
  509. png_free(png_ptr, old_buffer);
  510. png_error(png_ptr, "Insufficient memory for save_buffer");
  511. }
  512. if (old_buffer)
  513. memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  514. else if (png_ptr->save_buffer_size)
  515. png_error(png_ptr, "save_buffer error");
  516. png_free(png_ptr, old_buffer);
  517. png_ptr->save_buffer_max = new_max;
  518. }
  519. if (png_ptr->current_buffer_size)
  520. {
  521. memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  522. png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  523. png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  524. png_ptr->current_buffer_size = 0;
  525. }
  526. png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  527. png_ptr->buffer_size = 0;
  528. }
  529. void /* PRIVATE */
  530. png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
  531. png_size_t buffer_length)
  532. {
  533. png_ptr->current_buffer = buffer;
  534. png_ptr->current_buffer_size = buffer_length;
  535. png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  536. png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  537. }
  538. void /* PRIVATE */
  539. png_push_read_IDAT(png_structrp png_ptr)
  540. {
  541. if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
  542. {
  543. png_byte chunk_length[4];
  544. png_byte chunk_tag[4];
  545. /* TODO: this code can be commoned up with the same code in push_read */
  546. #ifdef PNG_READ_APNG_SUPPORTED
  547. PNG_PUSH_SAVE_BUFFER_IF_LT(12)
  548. #else
  549. PNG_PUSH_SAVE_BUFFER_IF_LT(8)
  550. #endif
  551. png_push_fill_buffer(png_ptr, chunk_length, 4);
  552. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  553. png_reset_crc(png_ptr);
  554. png_crc_read(png_ptr, chunk_tag, 4);
  555. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  556. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  557. #ifdef PNG_READ_APNG_SUPPORTED
  558. if (png_ptr->chunk_name != png_fdAT && png_ptr->num_frames_read > 0)
  559. {
  560. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) != 0)
  561. {
  562. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  563. if (png_ptr->frame_end_fn != NULL)
  564. (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
  565. png_ptr->num_frames_read++;
  566. return;
  567. }
  568. else
  569. {
  570. if (png_ptr->chunk_name == png_IEND)
  571. png_error(png_ptr, "Not enough image data");
  572. PNG_PUSH_SAVE_BUFFER_IF_FULL
  573. png_warning(png_ptr, "Skipping (ignoring) a chunk between "
  574. "APNG chunks");
  575. png_crc_finish(png_ptr, png_ptr->push_length);
  576. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  577. return;
  578. }
  579. }
  580. else
  581. #endif
  582. #ifdef PNG_READ_APNG_SUPPORTED
  583. if (png_ptr->chunk_name != png_IDAT && png_ptr->num_frames_read == 0)
  584. #else
  585. if (png_ptr->chunk_name != png_IDAT)
  586. #endif
  587. {
  588. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  589. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  590. png_error(png_ptr, "Not enough compressed data");
  591. #ifdef PNG_READ_APNG_SUPPORTED
  592. if (png_ptr->frame_end_fn != NULL)
  593. (*(png_ptr->frame_end_fn))(png_ptr, png_ptr->num_frames_read);
  594. png_ptr->num_frames_read++;
  595. #endif
  596. return;
  597. }
  598. png_ptr->idat_size = png_ptr->push_length;
  599. #ifdef PNG_READ_APNG_SUPPORTED
  600. if (png_ptr->num_frames_read > 0)
  601. {
  602. png_ensure_sequence_number(png_ptr, 4);
  603. png_ptr->idat_size -= 4;
  604. }
  605. #endif
  606. }
  607. if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
  608. {
  609. png_size_t save_size = png_ptr->save_buffer_size;
  610. png_uint_32 idat_size = png_ptr->idat_size;
  611. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  612. * are of different types and we don't know which variable has the fewest
  613. * bits. Carefully select the smaller and cast it to the type of the
  614. * larger - this cannot overflow. Do not cast in the following test - it
  615. * will break on either 16-bit or 64-bit platforms.
  616. */
  617. if (idat_size < save_size)
  618. save_size = (png_size_t)idat_size;
  619. else
  620. idat_size = (png_uint_32)save_size;
  621. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  622. png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  623. png_ptr->idat_size -= idat_size;
  624. png_ptr->buffer_size -= save_size;
  625. png_ptr->save_buffer_size -= save_size;
  626. png_ptr->save_buffer_ptr += save_size;
  627. }
  628. if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
  629. {
  630. png_size_t save_size = png_ptr->current_buffer_size;
  631. png_uint_32 idat_size = png_ptr->idat_size;
  632. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  633. * are of different types and we don't know which variable has the fewest
  634. * bits. Carefully select the smaller and cast it to the type of the
  635. * larger - this cannot overflow.
  636. */
  637. if (idat_size < save_size)
  638. save_size = (png_size_t)idat_size;
  639. else
  640. idat_size = (png_uint_32)save_size;
  641. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  642. png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  643. png_ptr->idat_size -= idat_size;
  644. png_ptr->buffer_size -= save_size;
  645. png_ptr->current_buffer_size -= save_size;
  646. png_ptr->current_buffer_ptr += save_size;
  647. }
  648. if (png_ptr->idat_size == 0)
  649. {
  650. PNG_PUSH_SAVE_BUFFER_IF_LT(4)
  651. png_crc_finish(png_ptr, 0);
  652. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  653. png_ptr->mode |= PNG_AFTER_IDAT;
  654. png_ptr->zowner = 0;
  655. }
  656. }
  657. void /* PRIVATE */
  658. png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
  659. png_size_t buffer_length)
  660. {
  661. /* The caller checks for a non-zero buffer length. */
  662. if (!(buffer_length > 0) || buffer == NULL)
  663. png_error(png_ptr, "No IDAT data (internal error)");
  664. #ifdef PNG_READ_APNG_SUPPORTED
  665. /* If the app is not APNG-aware, decode only the first frame */
  666. if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
  667. png_ptr->num_frames_read > 0)
  668. {
  669. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  670. return;
  671. }
  672. #endif
  673. /* This routine must process all the data it has been given
  674. * before returning, calling the row callback as required to
  675. * handle the uncompressed results.
  676. */
  677. png_ptr->zstream.next_in = buffer;
  678. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  679. png_ptr->zstream.avail_in = (uInt)buffer_length;
  680. /* Keep going until the decompressed data is all processed
  681. * or the stream marked as finished.
  682. */
  683. while (png_ptr->zstream.avail_in > 0 &&
  684. (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  685. {
  686. int ret;
  687. /* We have data for zlib, but we must check that zlib
  688. * has someplace to put the results. It doesn't matter
  689. * if we don't expect any results -- it may be the input
  690. * data is just the LZ end code.
  691. */
  692. if (!(png_ptr->zstream.avail_out > 0))
  693. {
  694. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  695. png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  696. png_ptr->iwidth) + 1);
  697. png_ptr->zstream.next_out = png_ptr->row_buf;
  698. }
  699. /* Using Z_SYNC_FLUSH here means that an unterminated
  700. * LZ stream (a stream with a missing end code) can still
  701. * be handled, otherwise (Z_NO_FLUSH) a future zlib
  702. * implementation might defer output and therefore
  703. * change the current behavior (see comments in inflate.c
  704. * for why this doesn't happen at present with zlib 1.2.5).
  705. */
  706. ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
  707. /* Check for any failure before proceeding. */
  708. if (ret != Z_OK && ret != Z_STREAM_END)
  709. {
  710. /* Terminate the decompression. */
  711. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  712. png_ptr->zowner = 0;
  713. /* This may be a truncated stream (missing or
  714. * damaged end code). Treat that as a warning.
  715. */
  716. if (png_ptr->row_number >= png_ptr->num_rows ||
  717. png_ptr->pass > 6)
  718. png_warning(png_ptr, "Truncated compressed data in IDAT");
  719. else
  720. {
  721. if (ret == Z_DATA_ERROR)
  722. png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
  723. else
  724. png_error(png_ptr, "Decompression error in IDAT");
  725. }
  726. /* Skip the check on unprocessed input */
  727. return;
  728. }
  729. /* Did inflate output any data? */
  730. if (png_ptr->zstream.next_out != png_ptr->row_buf)
  731. {
  732. /* Is this unexpected data after the last row?
  733. * If it is, artificially terminate the LZ output
  734. * here.
  735. */
  736. if (png_ptr->row_number >= png_ptr->num_rows ||
  737. png_ptr->pass > 6)
  738. {
  739. /* Extra data. */
  740. png_warning(png_ptr, "Extra compressed data in IDAT");
  741. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  742. png_ptr->zowner = 0;
  743. /* Do no more processing; skip the unprocessed
  744. * input check below.
  745. */
  746. return;
  747. }
  748. /* Do we have a complete row? */
  749. if (png_ptr->zstream.avail_out == 0)
  750. png_push_process_row(png_ptr);
  751. }
  752. /* And check for the end of the stream. */
  753. if (ret == Z_STREAM_END)
  754. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  755. }
  756. /* All the data should have been processed, if anything
  757. * is left at this point we have bytes of IDAT data
  758. * after the zlib end code.
  759. */
  760. if (png_ptr->zstream.avail_in > 0)
  761. png_warning(png_ptr, "Extra compression data in IDAT");
  762. }
  763. void /* PRIVATE */
  764. png_push_process_row(png_structrp png_ptr)
  765. {
  766. /* 1.5.6: row_info moved out of png_struct to a local here. */
  767. png_row_info row_info;
  768. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  769. row_info.color_type = png_ptr->color_type;
  770. row_info.bit_depth = png_ptr->bit_depth;
  771. row_info.channels = png_ptr->channels;
  772. row_info.pixel_depth = png_ptr->pixel_depth;
  773. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  774. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  775. {
  776. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  777. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  778. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  779. else
  780. png_error(png_ptr, "bad adaptive filter value");
  781. }
  782. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  783. * 1.5.6, while the buffer really is this big in current versions of libpng
  784. * it may not be in the future, so this was changed just to copy the
  785. * interlaced row count:
  786. */
  787. memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  788. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  789. if (png_ptr->transformations != 0)
  790. png_do_read_transformations(png_ptr, &row_info);
  791. #endif
  792. /* The transformed pixel depth should match the depth now in row_info. */
  793. if (png_ptr->transformed_pixel_depth == 0)
  794. {
  795. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  796. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  797. png_error(png_ptr, "progressive row overflow");
  798. }
  799. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  800. png_error(png_ptr, "internal progressive row size calculation error");
  801. #ifdef PNG_READ_INTERLACING_SUPPORTED
  802. /* Expand interlaced rows to full size */
  803. if (png_ptr->interlaced != 0 &&
  804. (png_ptr->transformations & PNG_INTERLACE) != 0)
  805. {
  806. if (png_ptr->pass < 6)
  807. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  808. png_ptr->transformations);
  809. switch (png_ptr->pass)
  810. {
  811. case 0:
  812. {
  813. int i;
  814. for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  815. {
  816. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  817. png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
  818. }
  819. if (png_ptr->pass == 2) /* Pass 1 might be empty */
  820. {
  821. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  822. {
  823. png_push_have_row(png_ptr, NULL);
  824. png_read_push_finish_row(png_ptr);
  825. }
  826. }
  827. if (png_ptr->pass == 4 && png_ptr->height <= 4)
  828. {
  829. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  830. {
  831. png_push_have_row(png_ptr, NULL);
  832. png_read_push_finish_row(png_ptr);
  833. }
  834. }
  835. if (png_ptr->pass == 6 && png_ptr->height <= 4)
  836. {
  837. png_push_have_row(png_ptr, NULL);
  838. png_read_push_finish_row(png_ptr);
  839. }
  840. break;
  841. }
  842. case 1:
  843. {
  844. int i;
  845. for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  846. {
  847. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  848. png_read_push_finish_row(png_ptr);
  849. }
  850. if (png_ptr->pass == 2) /* Skip top 4 generated rows */
  851. {
  852. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  853. {
  854. png_push_have_row(png_ptr, NULL);
  855. png_read_push_finish_row(png_ptr);
  856. }
  857. }
  858. break;
  859. }
  860. case 2:
  861. {
  862. int i;
  863. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  864. {
  865. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  866. png_read_push_finish_row(png_ptr);
  867. }
  868. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  869. {
  870. png_push_have_row(png_ptr, NULL);
  871. png_read_push_finish_row(png_ptr);
  872. }
  873. if (png_ptr->pass == 4) /* Pass 3 might be empty */
  874. {
  875. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  876. {
  877. png_push_have_row(png_ptr, NULL);
  878. png_read_push_finish_row(png_ptr);
  879. }
  880. }
  881. break;
  882. }
  883. case 3:
  884. {
  885. int i;
  886. for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  887. {
  888. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  889. png_read_push_finish_row(png_ptr);
  890. }
  891. if (png_ptr->pass == 4) /* Skip top two generated rows */
  892. {
  893. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  894. {
  895. png_push_have_row(png_ptr, NULL);
  896. png_read_push_finish_row(png_ptr);
  897. }
  898. }
  899. break;
  900. }
  901. case 4:
  902. {
  903. int i;
  904. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  905. {
  906. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  907. png_read_push_finish_row(png_ptr);
  908. }
  909. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  910. {
  911. png_push_have_row(png_ptr, NULL);
  912. png_read_push_finish_row(png_ptr);
  913. }
  914. if (png_ptr->pass == 6) /* Pass 5 might be empty */
  915. {
  916. png_push_have_row(png_ptr, NULL);
  917. png_read_push_finish_row(png_ptr);
  918. }
  919. break;
  920. }
  921. case 5:
  922. {
  923. int i;
  924. for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  925. {
  926. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  927. png_read_push_finish_row(png_ptr);
  928. }
  929. if (png_ptr->pass == 6) /* Skip top generated row */
  930. {
  931. png_push_have_row(png_ptr, NULL);
  932. png_read_push_finish_row(png_ptr);
  933. }
  934. break;
  935. }
  936. default:
  937. case 6:
  938. {
  939. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  940. png_read_push_finish_row(png_ptr);
  941. if (png_ptr->pass != 6)
  942. break;
  943. png_push_have_row(png_ptr, NULL);
  944. png_read_push_finish_row(png_ptr);
  945. }
  946. }
  947. }
  948. else
  949. #endif
  950. {
  951. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  952. png_read_push_finish_row(png_ptr);
  953. }
  954. }
  955. void /* PRIVATE */
  956. png_read_push_finish_row(png_structrp png_ptr)
  957. {
  958. #ifdef PNG_READ_INTERLACING_SUPPORTED
  959. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  960. /* Start of interlace block */
  961. static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  962. /* Offset to next interlace block */
  963. static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  964. /* Start of interlace block in the y direction */
  965. static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  966. /* Offset to next interlace block in the y direction */
  967. static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  968. /* Height of interlace block. This is not currently used - if you need
  969. * it, uncomment it here and in png.h
  970. static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  971. */
  972. #endif
  973. png_ptr->row_number++;
  974. if (png_ptr->row_number < png_ptr->num_rows)
  975. return;
  976. #ifdef PNG_READ_INTERLACING_SUPPORTED
  977. if (png_ptr->interlaced != 0)
  978. {
  979. png_ptr->row_number = 0;
  980. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  981. do
  982. {
  983. png_ptr->pass++;
  984. if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
  985. (png_ptr->pass == 3 && png_ptr->width < 3) ||
  986. (png_ptr->pass == 5 && png_ptr->width < 2))
  987. png_ptr->pass++;
  988. if (png_ptr->pass > 7)
  989. png_ptr->pass--;
  990. if (png_ptr->pass >= 7)
  991. break;
  992. png_ptr->iwidth = (png_ptr->width +
  993. png_pass_inc[png_ptr->pass] - 1 -
  994. png_pass_start[png_ptr->pass]) /
  995. png_pass_inc[png_ptr->pass];
  996. if ((png_ptr->transformations & PNG_INTERLACE) != 0)
  997. break;
  998. png_ptr->num_rows = (png_ptr->height +
  999. png_pass_yinc[png_ptr->pass] - 1 -
  1000. png_pass_ystart[png_ptr->pass]) /
  1001. png_pass_yinc[png_ptr->pass];
  1002. } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
  1003. }
  1004. #endif /* READ_INTERLACING */
  1005. }
  1006. void /* PRIVATE */
  1007. png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
  1008. {
  1009. if (png_ptr->info_fn != NULL)
  1010. (*(png_ptr->info_fn))(png_ptr, info_ptr);
  1011. }
  1012. void /* PRIVATE */
  1013. png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
  1014. {
  1015. if (png_ptr->end_fn != NULL)
  1016. (*(png_ptr->end_fn))(png_ptr, info_ptr);
  1017. }
  1018. void /* PRIVATE */
  1019. png_push_have_row(png_structrp png_ptr, png_bytep row)
  1020. {
  1021. if (png_ptr->row_fn != NULL)
  1022. (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
  1023. (int)png_ptr->pass);
  1024. }
  1025. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1026. void PNGAPI
  1027. png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
  1028. png_const_bytep new_row)
  1029. {
  1030. if (png_ptr == NULL)
  1031. return;
  1032. /* new_row is a flag here - if it is NULL then the app callback was called
  1033. * from an empty row (see the calls to png_struct::row_fn below), otherwise
  1034. * it must be png_ptr->row_buf+1
  1035. */
  1036. if (new_row != NULL)
  1037. png_combine_row(png_ptr, old_row, 1/*blocky display*/);
  1038. }
  1039. #endif /* READ_INTERLACING */
  1040. void PNGAPI
  1041. png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
  1042. png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
  1043. png_progressive_end_ptr end_fn)
  1044. {
  1045. if (png_ptr == NULL)
  1046. return;
  1047. png_ptr->info_fn = info_fn;
  1048. png_ptr->row_fn = row_fn;
  1049. png_ptr->end_fn = end_fn;
  1050. png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
  1051. }
  1052. #ifdef PNG_READ_APNG_SUPPORTED
  1053. void PNGAPI
  1054. png_set_progressive_frame_fn(png_structp png_ptr,
  1055. png_progressive_frame_ptr frame_info_fn,
  1056. png_progressive_frame_ptr frame_end_fn)
  1057. {
  1058. png_ptr->frame_info_fn = frame_info_fn;
  1059. png_ptr->frame_end_fn = frame_end_fn;
  1060. png_ptr->apng_flags |= PNG_APNG_APP;
  1061. }
  1062. #endif
  1063. png_voidp PNGAPI
  1064. png_get_progressive_ptr(png_const_structrp png_ptr)
  1065. {
  1066. if (png_ptr == NULL)
  1067. return (NULL);
  1068. return png_ptr->io_ptr;
  1069. }
  1070. #endif /* PROGRESSIVE_READ */