pngread.c 46 KB

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