pngread.c 46 KB

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