pngpread.c 36 KB

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