pngtest.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /* pngtest.c - a simple test program to test libpng
  2. *
  3. * Last changed in libpng 1.6.9 [February 6, 2014]
  4. * Copyright (c) 1998-2014 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This program reads in a PNG image, writes it out again, and then
  13. * compares the two files. If the files are identical, this shows that
  14. * the basic chunk handling, filtering, and (de)compression code is working
  15. * properly. It does not currently test all of the transforms, although
  16. * it probably should.
  17. *
  18. * The program will report "FAIL" in certain legitimate cases:
  19. * 1) when the compression level or filter selection method is changed.
  20. * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
  21. * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
  22. * exist in the input file.
  23. * 4) others not listed here...
  24. * In these cases, it is best to check with another tool such as "pngcheck"
  25. * to see what the differences between the two files are.
  26. *
  27. * If a filename is given on the command-line, then this file is used
  28. * for the input, rather than the default "pngtest.png". This allows
  29. * testing a wide variety of files easily. You can also test a number
  30. * of files at once by typing "pngtest -m file1.png file2.png ..."
  31. */
  32. #define _POSIX_SOURCE 1
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. /* Defined so I can write to a file on gui/windowing platforms */
  37. /* #define STDERR stderr */
  38. #define STDERR stdout /* For DOS */
  39. #include "png.h"
  40. /* Known chunks that exist in pngtest.png must be supported or pngtest will fail
  41. * simply as a result of re-ordering them. This may be fixed in 1.7
  42. *
  43. * pngtest allocates a single row buffer for each row and overwrites it,
  44. * therefore if the write side doesn't support the writing of interlaced images
  45. * nothing can be done for an interlaced image (and the code below will fail
  46. * horribly trying to write extra data after writing garbage).
  47. */
  48. #if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
  49. defined PNG_READ_bKGD_SUPPORTED &&\
  50. defined PNG_READ_cHRM_SUPPORTED &&\
  51. defined PNG_READ_gAMA_SUPPORTED &&\
  52. defined PNG_READ_oFFs_SUPPORTED &&\
  53. defined PNG_READ_pCAL_SUPPORTED &&\
  54. defined PNG_READ_pHYs_SUPPORTED &&\
  55. defined PNG_READ_sBIT_SUPPORTED &&\
  56. defined PNG_READ_sCAL_SUPPORTED &&\
  57. defined PNG_READ_sRGB_SUPPORTED &&\
  58. defined PNG_READ_tEXt_SUPPORTED &&\
  59. defined PNG_READ_tIME_SUPPORTED &&\
  60. defined PNG_READ_zTXt_SUPPORTED &&\
  61. defined PNG_WRITE_INTERLACING_SUPPORTED
  62. #ifdef PNG_ZLIB_HEADER
  63. # include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
  64. #else
  65. # include "zlib.h"
  66. #endif
  67. /* Copied from pngpriv.h but only used in error messages below. */
  68. #ifndef PNG_ZBUF_SIZE
  69. # define PNG_ZBUF_SIZE 8192
  70. #endif
  71. #define FCLOSE(file) fclose(file)
  72. #ifndef PNG_STDIO_SUPPORTED
  73. typedef FILE * png_FILE_p;
  74. #endif
  75. /* Makes pngtest verbose so we can find problems. */
  76. #ifndef PNG_DEBUG
  77. # define PNG_DEBUG 0
  78. #endif
  79. #if PNG_DEBUG > 1
  80. # define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
  81. # define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
  82. # define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
  83. #else
  84. # define pngtest_debug(m) ((void)0)
  85. # define pngtest_debug1(m,p1) ((void)0)
  86. # define pngtest_debug2(m,p1,p2) ((void)0)
  87. #endif
  88. #if !PNG_DEBUG
  89. # define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
  90. #endif
  91. /* Turn on CPU timing
  92. #define PNGTEST_TIMING
  93. */
  94. #ifndef PNG_FLOATING_POINT_SUPPORTED
  95. #undef PNGTEST_TIMING
  96. #endif
  97. #ifdef PNGTEST_TIMING
  98. static float t_start, t_stop, t_decode, t_encode, t_misc;
  99. #include <time.h>
  100. #endif
  101. #ifdef PNG_TIME_RFC1123_SUPPORTED
  102. #define PNG_tIME_STRING_LENGTH 29
  103. static int tIME_chunk_present = 0;
  104. static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
  105. #endif
  106. static int verbose = 0;
  107. static int strict = 0;
  108. static int relaxed = 0;
  109. static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
  110. static int error_count = 0; /* count calls to png_error */
  111. static int warning_count = 0; /* count calls to png_warning */
  112. /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
  113. #ifndef png_jmpbuf
  114. # define png_jmpbuf(png_ptr) png_ptr->jmpbuf
  115. #endif
  116. /* Defines for unknown chunk handling if required. */
  117. #ifndef PNG_HANDLE_CHUNK_ALWAYS
  118. # define PNG_HANDLE_CHUNK_ALWAYS 3
  119. #endif
  120. #ifndef PNG_HANDLE_CHUNK_IF_SAFE
  121. # define PNG_HANDLE_CHUNK_IF_SAFE 2
  122. #endif
  123. /* Utility to save typing/errors, the argument must be a name */
  124. #define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
  125. /* Example of using row callbacks to make a simple progress meter */
  126. static int status_pass = 1;
  127. static int status_dots_requested = 0;
  128. static int status_dots = 1;
  129. static void PNGCBAPI
  130. read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
  131. {
  132. if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
  133. return;
  134. if (status_pass != pass)
  135. {
  136. fprintf(stdout, "\n Pass %d: ", pass);
  137. status_pass = pass;
  138. status_dots = 31;
  139. }
  140. status_dots--;
  141. if (status_dots == 0)
  142. {
  143. fprintf(stdout, "\n ");
  144. status_dots=30;
  145. }
  146. fprintf(stdout, "r");
  147. }
  148. #ifdef PNG_WRITE_SUPPORTED
  149. static void PNGCBAPI
  150. write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
  151. {
  152. if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
  153. return;
  154. fprintf(stdout, "w");
  155. }
  156. #endif
  157. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  158. /* Example of using user transform callback (we don't transform anything,
  159. * but merely examine the row filters. We set this to 256 rather than
  160. * 5 in case illegal filter values are present.)
  161. */
  162. static png_uint_32 filters_used[256];
  163. static void PNGCBAPI
  164. count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
  165. {
  166. if (png_ptr != NULL && row_info != NULL)
  167. ++filters_used[*(data - 1)];
  168. }
  169. #endif
  170. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  171. /* Example of using user transform callback (we don't transform anything,
  172. * but merely count the zero samples)
  173. */
  174. static png_uint_32 zero_samples;
  175. static void PNGCBAPI
  176. count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
  177. {
  178. png_bytep dp = data;
  179. if (png_ptr == NULL)
  180. return;
  181. /* Contents of row_info:
  182. * png_uint_32 width width of row
  183. * png_uint_32 rowbytes number of bytes in row
  184. * png_byte color_type color type of pixels
  185. * png_byte bit_depth bit depth of samples
  186. * png_byte channels number of channels (1-4)
  187. * png_byte pixel_depth bits per pixel (depth*channels)
  188. */
  189. /* Counts the number of zero samples (or zero pixels if color_type is 3 */
  190. if (row_info->color_type == 0 || row_info->color_type == 3)
  191. {
  192. int pos = 0;
  193. png_uint_32 n, nstop;
  194. for (n = 0, nstop=row_info->width; n<nstop; n++)
  195. {
  196. if (row_info->bit_depth == 1)
  197. {
  198. if (((*dp << pos++ ) & 0x80) == 0)
  199. zero_samples++;
  200. if (pos == 8)
  201. {
  202. pos = 0;
  203. dp++;
  204. }
  205. }
  206. if (row_info->bit_depth == 2)
  207. {
  208. if (((*dp << (pos+=2)) & 0xc0) == 0)
  209. zero_samples++;
  210. if (pos == 8)
  211. {
  212. pos = 0;
  213. dp++;
  214. }
  215. }
  216. if (row_info->bit_depth == 4)
  217. {
  218. if (((*dp << (pos+=4)) & 0xf0) == 0)
  219. zero_samples++;
  220. if (pos == 8)
  221. {
  222. pos = 0;
  223. dp++;
  224. }
  225. }
  226. if (row_info->bit_depth == 8)
  227. if (*dp++ == 0)
  228. zero_samples++;
  229. if (row_info->bit_depth == 16)
  230. {
  231. if ((*dp | *(dp+1)) == 0)
  232. zero_samples++;
  233. dp+=2;
  234. }
  235. }
  236. }
  237. else /* Other color types */
  238. {
  239. png_uint_32 n, nstop;
  240. int channel;
  241. int color_channels = row_info->channels;
  242. if (row_info->color_type > 3)color_channels--;
  243. for (n = 0, nstop=row_info->width; n<nstop; n++)
  244. {
  245. for (channel = 0; channel < color_channels; channel++)
  246. {
  247. if (row_info->bit_depth == 8)
  248. if (*dp++ == 0)
  249. zero_samples++;
  250. if (row_info->bit_depth == 16)
  251. {
  252. if ((*dp | *(dp+1)) == 0)
  253. zero_samples++;
  254. dp+=2;
  255. }
  256. }
  257. if (row_info->color_type > 3)
  258. {
  259. dp++;
  260. if (row_info->bit_depth == 16)
  261. dp++;
  262. }
  263. }
  264. }
  265. }
  266. #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
  267. #ifndef PNG_STDIO_SUPPORTED
  268. /* START of code to validate stdio-free compilation */
  269. /* These copies of the default read/write functions come from pngrio.c and
  270. * pngwio.c. They allow "don't include stdio" testing of the library.
  271. * This is the function that does the actual reading of data. If you are
  272. * not reading from a standard C stream, you should create a replacement
  273. * read_data function and use it at run time with png_set_read_fn(), rather
  274. * than changing the library.
  275. */
  276. #ifdef PNG_IO_STATE_SUPPORTED
  277. void
  278. pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
  279. png_uint_32 io_op);
  280. void
  281. pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
  282. png_uint_32 io_op)
  283. {
  284. png_uint_32 io_state = png_get_io_state(png_ptr);
  285. int err = 0;
  286. /* Check if the current operation (reading / writing) is as expected. */
  287. if ((io_state & PNG_IO_MASK_OP) != io_op)
  288. png_error(png_ptr, "Incorrect operation in I/O state");
  289. /* Check if the buffer size specific to the current location
  290. * (file signature / header / data / crc) is as expected.
  291. */
  292. switch (io_state & PNG_IO_MASK_LOC)
  293. {
  294. case PNG_IO_SIGNATURE:
  295. if (data_length > 8)
  296. err = 1;
  297. break;
  298. case PNG_IO_CHUNK_HDR:
  299. if (data_length != 8)
  300. err = 1;
  301. break;
  302. case PNG_IO_CHUNK_DATA:
  303. break; /* no restrictions here */
  304. case PNG_IO_CHUNK_CRC:
  305. if (data_length != 4)
  306. err = 1;
  307. break;
  308. default:
  309. err = 1; /* uninitialized */
  310. }
  311. if (err)
  312. png_error(png_ptr, "Bad I/O state or buffer size");
  313. }
  314. #endif
  315. static void PNGCBAPI
  316. pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  317. {
  318. png_size_t check = 0;
  319. png_voidp io_ptr;
  320. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  321. * instead of an int, which is what fread() actually returns.
  322. */
  323. io_ptr = png_get_io_ptr(png_ptr);
  324. if (io_ptr != NULL)
  325. {
  326. check = fread(data, 1, length, (png_FILE_p)io_ptr);
  327. }
  328. if (check != length)
  329. {
  330. png_error(png_ptr, "Read Error");
  331. }
  332. #ifdef PNG_IO_STATE_SUPPORTED
  333. pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
  334. #endif
  335. }
  336. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  337. static void PNGCBAPI
  338. pngtest_flush(png_structp png_ptr)
  339. {
  340. /* Do nothing; fflush() is said to be just a waste of energy. */
  341. PNG_UNUSED(png_ptr) /* Stifle compiler warning */
  342. }
  343. #endif
  344. /* This is the function that does the actual writing of data. If you are
  345. * not writing to a standard C stream, you should create a replacement
  346. * write_data function and use it at run time with png_set_write_fn(), rather
  347. * than changing the library.
  348. */
  349. static void PNGCBAPI
  350. pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  351. {
  352. png_size_t check;
  353. check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
  354. if (check != length)
  355. {
  356. png_error(png_ptr, "Write Error");
  357. }
  358. #ifdef PNG_IO_STATE_SUPPORTED
  359. pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
  360. #endif
  361. }
  362. #endif /* !PNG_STDIO_SUPPORTED */
  363. /* This function is called when there is a warning, but the library thinks
  364. * it can continue anyway. Replacement functions don't have to do anything
  365. * here if you don't want to. In the default configuration, png_ptr is
  366. * not used, but it is passed in case it may be useful.
  367. */
  368. typedef struct
  369. {
  370. PNG_CONST char *file_name;
  371. } pngtest_error_parameters;
  372. static void PNGCBAPI
  373. pngtest_warning(png_structp png_ptr, png_const_charp message)
  374. {
  375. PNG_CONST char *name = "UNKNOWN (ERROR!)";
  376. pngtest_error_parameters *test =
  377. (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
  378. ++warning_count;
  379. if (test != NULL && test->file_name != NULL)
  380. name = test->file_name;
  381. fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
  382. }
  383. /* This is the default error handling function. Note that replacements for
  384. * this function MUST NOT RETURN, or the program will likely crash. This
  385. * function is used by default, or if the program supplies NULL for the
  386. * error function pointer in png_set_error_fn().
  387. */
  388. static void PNGCBAPI
  389. pngtest_error(png_structp png_ptr, png_const_charp message)
  390. {
  391. ++error_count;
  392. pngtest_warning(png_ptr, message);
  393. /* We can return because png_error calls the default handler, which is
  394. * actually OK in this case.
  395. */
  396. }
  397. /* END of code to validate stdio-free compilation */
  398. /* START of code to validate memory allocation and deallocation */
  399. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  400. /* Allocate memory. For reasonable files, size should never exceed
  401. * 64K. However, zlib may allocate more then 64K if you don't tell
  402. * it not to. See zconf.h and png.h for more information. zlib does
  403. * need to allocate exactly 64K, so whatever you call here must
  404. * have the ability to do that.
  405. *
  406. * This piece of code can be compiled to validate max 64K allocations
  407. * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
  408. */
  409. typedef struct memory_information
  410. {
  411. png_alloc_size_t size;
  412. png_voidp pointer;
  413. struct memory_information *next;
  414. } memory_information;
  415. typedef memory_information *memory_infop;
  416. static memory_infop pinformation = NULL;
  417. static int current_allocation = 0;
  418. static int maximum_allocation = 0;
  419. static int total_allocation = 0;
  420. static int num_allocations = 0;
  421. png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
  422. png_alloc_size_t size));
  423. void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
  424. png_voidp
  425. PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
  426. {
  427. /* png_malloc has already tested for NULL; png_create_struct calls
  428. * png_debug_malloc directly, with png_ptr == NULL which is OK
  429. */
  430. if (size == 0)
  431. return (NULL);
  432. /* This calls the library allocator twice, once to get the requested
  433. buffer and once to get a new free list entry. */
  434. {
  435. /* Disable malloc_fn and free_fn */
  436. memory_infop pinfo;
  437. png_set_mem_fn(png_ptr, NULL, NULL, NULL);
  438. pinfo = (memory_infop)png_malloc(png_ptr,
  439. (sizeof *pinfo));
  440. pinfo->size = size;
  441. current_allocation += size;
  442. total_allocation += size;
  443. num_allocations ++;
  444. if (current_allocation > maximum_allocation)
  445. maximum_allocation = current_allocation;
  446. pinfo->pointer = png_malloc(png_ptr, size);
  447. /* Restore malloc_fn and free_fn */
  448. png_set_mem_fn(png_ptr,
  449. NULL, png_debug_malloc, png_debug_free);
  450. if (size != 0 && pinfo->pointer == NULL)
  451. {
  452. current_allocation -= size;
  453. total_allocation -= size;
  454. png_error(png_ptr,
  455. "out of memory in pngtest->png_debug_malloc");
  456. }
  457. pinfo->next = pinformation;
  458. pinformation = pinfo;
  459. /* Make sure the caller isn't assuming zeroed memory. */
  460. memset(pinfo->pointer, 0xdd, pinfo->size);
  461. if (verbose)
  462. printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
  463. pinfo->pointer);
  464. return (png_voidp)(pinfo->pointer);
  465. }
  466. }
  467. /* Free a pointer. It is removed from the list at the same time. */
  468. void PNGCBAPI
  469. png_debug_free(png_structp png_ptr, png_voidp ptr)
  470. {
  471. if (png_ptr == NULL)
  472. fprintf(STDERR, "NULL pointer to png_debug_free.\n");
  473. if (ptr == 0)
  474. {
  475. #if 0 /* This happens all the time. */
  476. fprintf(STDERR, "WARNING: freeing NULL pointer\n");
  477. #endif
  478. return;
  479. }
  480. /* Unlink the element from the list. */
  481. {
  482. memory_infop *ppinfo = &pinformation;
  483. for (;;)
  484. {
  485. memory_infop pinfo = *ppinfo;
  486. if (pinfo->pointer == ptr)
  487. {
  488. *ppinfo = pinfo->next;
  489. current_allocation -= pinfo->size;
  490. if (current_allocation < 0)
  491. fprintf(STDERR, "Duplicate free of memory\n");
  492. /* We must free the list element too, but first kill
  493. the memory that is to be freed. */
  494. memset(ptr, 0x55, pinfo->size);
  495. if (pinfo)
  496. free(pinfo);
  497. pinfo = NULL;
  498. break;
  499. }
  500. if (pinfo->next == NULL)
  501. {
  502. fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
  503. break;
  504. }
  505. ppinfo = &pinfo->next;
  506. }
  507. }
  508. /* Finally free the data. */
  509. if (verbose)
  510. printf("Freeing %p\n", ptr);
  511. if (ptr)
  512. free(ptr);
  513. ptr = NULL;
  514. }
  515. #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
  516. /* END of code to test memory allocation/deallocation */
  517. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  518. /* Demonstration of user chunk support of the sTER and vpAg chunks */
  519. /* (sTER is a public chunk not yet known by libpng. vpAg is a private
  520. chunk used in ImageMagick to store "virtual page" size). */
  521. static struct user_chunk_data
  522. {
  523. png_const_infop info_ptr;
  524. png_uint_32 vpAg_width, vpAg_height;
  525. png_byte vpAg_units;
  526. png_byte sTER_mode;
  527. int location[2];
  528. }
  529. user_chunk_data;
  530. /* Used for location and order; zero means nothing. */
  531. #define have_sTER 0x01
  532. #define have_vpAg 0x02
  533. #define before_PLTE 0x10
  534. #define before_IDAT 0x20
  535. #define after_IDAT 0x40
  536. static void
  537. init_callback_info(png_const_infop info_ptr)
  538. {
  539. MEMZERO(user_chunk_data);
  540. user_chunk_data.info_ptr = info_ptr;
  541. }
  542. static int
  543. set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
  544. {
  545. int location;
  546. if ((data->location[0] & what) || (data->location[1] & what))
  547. return 0; /* already have one of these */
  548. /* Find where we are (the code below zeros info_ptr to indicate that the
  549. * chunks before the first IDAT have been read.)
  550. */
  551. if (data->info_ptr == NULL) /* after IDAT */
  552. location = what | after_IDAT;
  553. else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE))
  554. location = what | before_IDAT;
  555. else
  556. location = what | before_PLTE;
  557. if (data->location[0] == 0)
  558. data->location[0] = location;
  559. else
  560. data->location[1] = location;
  561. return 1; /* handled */
  562. }
  563. static int PNGCBAPI
  564. read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
  565. {
  566. struct user_chunk_data *my_user_chunk_data =
  567. (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
  568. if (my_user_chunk_data == NULL)
  569. png_error(png_ptr, "lost user chunk pointer");
  570. /* Return one of the following:
  571. * return (-n); chunk had an error
  572. * return (0); did not recognize
  573. * return (n); success
  574. *
  575. * The unknown chunk structure contains the chunk data:
  576. * png_byte name[5];
  577. * png_byte *data;
  578. * png_size_t size;
  579. *
  580. * Note that libpng has already taken care of the CRC handling.
  581. */
  582. if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
  583. chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
  584. {
  585. /* Found sTER chunk */
  586. if (chunk->size != 1)
  587. return (-1); /* Error return */
  588. if (chunk->data[0] != 0 && chunk->data[0] != 1)
  589. return (-1); /* Invalid mode */
  590. if (set_location(png_ptr, my_user_chunk_data, have_sTER))
  591. {
  592. my_user_chunk_data->sTER_mode=chunk->data[0];
  593. return (1);
  594. }
  595. else
  596. return (0); /* duplicate sTER - give it to libpng */
  597. }
  598. if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
  599. chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
  600. return (0); /* Did not recognize */
  601. /* Found ImageMagick vpAg chunk */
  602. if (chunk->size != 9)
  603. return (-1); /* Error return */
  604. if (!set_location(png_ptr, my_user_chunk_data, have_vpAg))
  605. return (0); /* duplicate vpAg */
  606. my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
  607. my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
  608. my_user_chunk_data->vpAg_units = chunk->data[8];
  609. return (1);
  610. }
  611. #ifdef PNG_WRITE_SUPPORTED
  612. static void
  613. write_sTER_chunk(png_structp write_ptr)
  614. {
  615. png_byte sTER[5] = {115, 84, 69, 82, '\0'};
  616. if (verbose)
  617. fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
  618. png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
  619. }
  620. static void
  621. write_vpAg_chunk(png_structp write_ptr)
  622. {
  623. png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
  624. png_byte vpag_chunk_data[9];
  625. if (verbose)
  626. fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
  627. (unsigned long)user_chunk_data.vpAg_width,
  628. (unsigned long)user_chunk_data.vpAg_height,
  629. user_chunk_data.vpAg_units);
  630. png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
  631. png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
  632. vpag_chunk_data[8] = user_chunk_data.vpAg_units;
  633. png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
  634. }
  635. static void
  636. write_chunks(png_structp write_ptr, int location)
  637. {
  638. int i;
  639. /* Notice that this preserves the original chunk order, however chunks
  640. * intercepted by the callback will be written *after* chunks passed to
  641. * libpng. This will actually reverse a pair of sTER chunks or a pair of
  642. * vpAg chunks, resulting in an error later. This is not worth worrying
  643. * about - the chunks should not be duplicated!
  644. */
  645. for (i=0; i<2; ++i)
  646. {
  647. if (user_chunk_data.location[i] == (location | have_sTER))
  648. write_sTER_chunk(write_ptr);
  649. else if (user_chunk_data.location[i] == (location | have_vpAg))
  650. write_vpAg_chunk(write_ptr);
  651. }
  652. }
  653. #endif /* PNG_WRITE_SUPPORTED */
  654. #else /* !PNG_READ_USER_CHUNKS_SUPPORTED */
  655. # define write_chunks(pp,loc) ((void)0)
  656. #endif
  657. /* END of code to demonstrate user chunk support */
  658. /* START of code to check that libpng has the required text support; this only
  659. * checks for the write support because if read support is missing the chunk
  660. * will simply not be reported back to pngtest.
  661. */
  662. #ifdef PNG_TEXT_SUPPORTED
  663. static void
  664. pngtest_check_text_support(png_const_structp png_ptr, png_textp text_ptr,
  665. int num_text)
  666. {
  667. while (num_text > 0)
  668. {
  669. switch (text_ptr[--num_text].compression)
  670. {
  671. case PNG_TEXT_COMPRESSION_NONE:
  672. break;
  673. case PNG_TEXT_COMPRESSION_zTXt:
  674. # ifndef PNG_WRITE_zTXt_SUPPORTED
  675. ++unsupported_chunks;
  676. # endif
  677. break;
  678. case PNG_ITXT_COMPRESSION_NONE:
  679. case PNG_ITXT_COMPRESSION_zTXt:
  680. # ifndef PNG_WRITE_iTXt_SUPPORTED
  681. ++unsupported_chunks;
  682. # endif
  683. break;
  684. default:
  685. /* This is an error */
  686. png_error(png_ptr, "invalid text chunk compression field");
  687. break;
  688. }
  689. }
  690. }
  691. #endif
  692. /* END of code to check that libpng has the required text support */
  693. /* Test one file */
  694. static int
  695. test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
  696. {
  697. static png_FILE_p fpin;
  698. static png_FILE_p fpout; /* "static" prevents setjmp corruption */
  699. pngtest_error_parameters error_parameters;
  700. png_structp read_ptr;
  701. png_infop read_info_ptr, end_info_ptr;
  702. #ifdef PNG_WRITE_SUPPORTED
  703. png_structp write_ptr;
  704. png_infop write_info_ptr;
  705. png_infop write_end_info_ptr;
  706. int interlace_preserved = 1;
  707. #else
  708. png_structp write_ptr = NULL;
  709. png_infop write_info_ptr = NULL;
  710. png_infop write_end_info_ptr = NULL;
  711. #endif
  712. png_bytep row_buf;
  713. png_uint_32 y;
  714. png_uint_32 width, height;
  715. int num_pass = 1, pass;
  716. int bit_depth, color_type;
  717. row_buf = NULL;
  718. error_parameters.file_name = inname;
  719. if ((fpin = fopen(inname, "rb")) == NULL)
  720. {
  721. fprintf(STDERR, "Could not find input file %s\n", inname);
  722. return (1);
  723. }
  724. if ((fpout = fopen(outname, "wb")) == NULL)
  725. {
  726. fprintf(STDERR, "Could not open output file %s\n", outname);
  727. FCLOSE(fpin);
  728. return (1);
  729. }
  730. pngtest_debug("Allocating read and write structures");
  731. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  732. read_ptr =
  733. png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
  734. NULL, NULL, NULL, png_debug_malloc, png_debug_free);
  735. #else
  736. read_ptr =
  737. png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  738. #endif
  739. png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
  740. pngtest_warning);
  741. #ifdef PNG_WRITE_SUPPORTED
  742. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  743. write_ptr =
  744. png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
  745. NULL, NULL, NULL, png_debug_malloc, png_debug_free);
  746. #else
  747. write_ptr =
  748. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  749. #endif
  750. png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
  751. pngtest_warning);
  752. #endif
  753. pngtest_debug("Allocating read_info, write_info and end_info structures");
  754. read_info_ptr = png_create_info_struct(read_ptr);
  755. end_info_ptr = png_create_info_struct(read_ptr);
  756. #ifdef PNG_WRITE_SUPPORTED
  757. write_info_ptr = png_create_info_struct(write_ptr);
  758. write_end_info_ptr = png_create_info_struct(write_ptr);
  759. #endif
  760. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  761. init_callback_info(read_info_ptr);
  762. png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
  763. read_user_chunk_callback);
  764. #endif
  765. #ifdef PNG_SETJMP_SUPPORTED
  766. pngtest_debug("Setting jmpbuf for read struct");
  767. if (setjmp(png_jmpbuf(read_ptr)))
  768. {
  769. fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
  770. png_free(read_ptr, row_buf);
  771. row_buf = NULL;
  772. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  773. #ifdef PNG_WRITE_SUPPORTED
  774. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  775. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  776. #endif
  777. FCLOSE(fpin);
  778. FCLOSE(fpout);
  779. return (1);
  780. }
  781. #ifdef PNG_WRITE_SUPPORTED
  782. pngtest_debug("Setting jmpbuf for write struct");
  783. if (setjmp(png_jmpbuf(write_ptr)))
  784. {
  785. fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
  786. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  787. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  788. #ifdef PNG_WRITE_SUPPORTED
  789. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  790. #endif
  791. FCLOSE(fpin);
  792. FCLOSE(fpout);
  793. return (1);
  794. }
  795. #endif
  796. #endif
  797. if (strict)
  798. {
  799. /* Treat png_benign_error() as errors on read */
  800. png_set_benign_errors(read_ptr, 0);
  801. #ifdef PNG_WRITE_SUPPORTED
  802. /* Treat them as errors on write */
  803. png_set_benign_errors(write_ptr, 0);
  804. #endif
  805. /* if strict is not set, then app warnings and errors are treated as
  806. * warnings in release builds, but not in unstable builds; this can be
  807. * changed with '--relaxed'.
  808. */
  809. }
  810. else if (relaxed)
  811. {
  812. /* Allow application (pngtest) errors and warnings to pass */
  813. png_set_benign_errors(read_ptr, 1);
  814. #ifdef PNG_WRITE_SUPPORTED
  815. png_set_benign_errors(write_ptr, 1);
  816. #endif
  817. }
  818. pngtest_debug("Initializing input and output streams");
  819. #ifdef PNG_STDIO_SUPPORTED
  820. png_init_io(read_ptr, fpin);
  821. # ifdef PNG_WRITE_SUPPORTED
  822. png_init_io(write_ptr, fpout);
  823. # endif
  824. #else
  825. png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
  826. # ifdef PNG_WRITE_SUPPORTED
  827. png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
  828. # ifdef PNG_WRITE_FLUSH_SUPPORTED
  829. pngtest_flush);
  830. # else
  831. NULL);
  832. # endif
  833. # endif
  834. #endif
  835. if (status_dots_requested == 1)
  836. {
  837. #ifdef PNG_WRITE_SUPPORTED
  838. png_set_write_status_fn(write_ptr, write_row_callback);
  839. #endif
  840. png_set_read_status_fn(read_ptr, read_row_callback);
  841. }
  842. else
  843. {
  844. #ifdef PNG_WRITE_SUPPORTED
  845. png_set_write_status_fn(write_ptr, NULL);
  846. #endif
  847. png_set_read_status_fn(read_ptr, NULL);
  848. }
  849. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  850. {
  851. int i;
  852. for (i = 0; i<256; i++)
  853. filters_used[i] = 0;
  854. png_set_read_user_transform_fn(read_ptr, count_filters);
  855. }
  856. #endif
  857. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  858. zero_samples = 0;
  859. png_set_write_user_transform_fn(write_ptr, count_zero_samples);
  860. #endif
  861. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  862. /* Preserve all the unknown chunks, if possible. If this is disabled then,
  863. * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
  864. * libpng to *save* the unknown chunks on read (because we can't switch the
  865. * save option on!)
  866. *
  867. * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
  868. * unknown chunks and write will write them all.
  869. */
  870. #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  871. png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
  872. NULL, 0);
  873. #endif
  874. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  875. png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
  876. NULL, 0);
  877. #endif
  878. #endif
  879. pngtest_debug("Reading info struct");
  880. png_read_info(read_ptr, read_info_ptr);
  881. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  882. /* This is a bit of a hack; there is no obvious way in the callback function
  883. * to determine that the chunks before the first IDAT have been read, so
  884. * remove the info_ptr (which is only used to determine position relative to
  885. * PLTE) here to indicate that we are after the IDAT.
  886. */
  887. user_chunk_data.info_ptr = NULL;
  888. #endif
  889. pngtest_debug("Transferring info struct");
  890. {
  891. int interlace_type, compression_type, filter_type;
  892. if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
  893. &color_type, &interlace_type, &compression_type, &filter_type))
  894. {
  895. png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
  896. color_type, interlace_type, compression_type, filter_type);
  897. #ifndef PNG_READ_INTERLACING_SUPPORTED
  898. /* num_pass will not be set below, set it here if the image is
  899. * interlaced: what happens is that write interlacing is *not* turned
  900. * on an the partial interlaced rows are written directly.
  901. */
  902. switch (interlace_type)
  903. {
  904. case PNG_INTERLACE_NONE:
  905. num_pass = 1;
  906. break;
  907. case PNG_INTERLACE_ADAM7:
  908. num_pass = 7;
  909. break;
  910. default:
  911. png_error(read_ptr, "invalid interlace type");
  912. /*NOT REACHED*/
  913. }
  914. #endif
  915. }
  916. }
  917. #ifdef PNG_FIXED_POINT_SUPPORTED
  918. #ifdef PNG_cHRM_SUPPORTED
  919. {
  920. png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
  921. blue_y;
  922. if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
  923. &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
  924. {
  925. png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
  926. red_y, green_x, green_y, blue_x, blue_y);
  927. }
  928. }
  929. #endif
  930. #ifdef PNG_gAMA_SUPPORTED
  931. {
  932. png_fixed_point gamma;
  933. if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
  934. png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
  935. }
  936. #endif
  937. #else /* Use floating point versions */
  938. #ifdef PNG_FLOATING_POINT_SUPPORTED
  939. #ifdef PNG_cHRM_SUPPORTED
  940. {
  941. double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
  942. blue_y;
  943. if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
  944. &red_y, &green_x, &green_y, &blue_x, &blue_y))
  945. {
  946. png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
  947. red_y, green_x, green_y, blue_x, blue_y);
  948. }
  949. }
  950. #endif
  951. #ifdef PNG_gAMA_SUPPORTED
  952. {
  953. double gamma;
  954. if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
  955. png_set_gAMA(write_ptr, write_info_ptr, gamma);
  956. }
  957. #endif
  958. #endif /* Floating point */
  959. #endif /* Fixed point */
  960. #ifdef PNG_iCCP_SUPPORTED
  961. {
  962. png_charp name;
  963. png_bytep profile;
  964. png_uint_32 proflen;
  965. int compression_type;
  966. if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
  967. &profile, &proflen))
  968. {
  969. png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
  970. profile, proflen);
  971. }
  972. }
  973. #endif
  974. #ifdef PNG_sRGB_SUPPORTED
  975. {
  976. int intent;
  977. if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
  978. png_set_sRGB(write_ptr, write_info_ptr, intent);
  979. }
  980. #endif
  981. {
  982. png_colorp palette;
  983. int num_palette;
  984. if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
  985. png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
  986. }
  987. #ifdef PNG_bKGD_SUPPORTED
  988. {
  989. png_color_16p background;
  990. if (png_get_bKGD(read_ptr, read_info_ptr, &background))
  991. {
  992. png_set_bKGD(write_ptr, write_info_ptr, background);
  993. }
  994. }
  995. #endif
  996. #ifdef PNG_hIST_SUPPORTED
  997. {
  998. png_uint_16p hist;
  999. if (png_get_hIST(read_ptr, read_info_ptr, &hist))
  1000. png_set_hIST(write_ptr, write_info_ptr, hist);
  1001. }
  1002. #endif
  1003. #ifdef PNG_oFFs_SUPPORTED
  1004. {
  1005. png_int_32 offset_x, offset_y;
  1006. int unit_type;
  1007. if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
  1008. &unit_type))
  1009. {
  1010. png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
  1011. }
  1012. }
  1013. #endif
  1014. #ifdef PNG_pCAL_SUPPORTED
  1015. {
  1016. png_charp purpose, units;
  1017. png_charpp params;
  1018. png_int_32 X0, X1;
  1019. int type, nparams;
  1020. if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
  1021. &nparams, &units, &params))
  1022. {
  1023. png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
  1024. nparams, units, params);
  1025. }
  1026. }
  1027. #endif
  1028. #ifdef PNG_pHYs_SUPPORTED
  1029. {
  1030. png_uint_32 res_x, res_y;
  1031. int unit_type;
  1032. if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
  1033. png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
  1034. }
  1035. #endif
  1036. #ifdef PNG_sBIT_SUPPORTED
  1037. {
  1038. png_color_8p sig_bit;
  1039. if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
  1040. png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
  1041. }
  1042. #endif
  1043. #ifdef PNG_sCAL_SUPPORTED
  1044. #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
  1045. defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
  1046. {
  1047. int unit;
  1048. double scal_width, scal_height;
  1049. if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
  1050. &scal_height))
  1051. {
  1052. png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
  1053. }
  1054. }
  1055. #else
  1056. #ifdef PNG_FIXED_POINT_SUPPORTED
  1057. {
  1058. int unit;
  1059. png_charp scal_width, scal_height;
  1060. if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
  1061. &scal_height))
  1062. {
  1063. png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
  1064. scal_height);
  1065. }
  1066. }
  1067. #endif
  1068. #endif
  1069. #endif
  1070. #ifdef PNG_TEXT_SUPPORTED
  1071. {
  1072. png_textp text_ptr;
  1073. int num_text;
  1074. if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
  1075. {
  1076. pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1077. pngtest_check_text_support(read_ptr, text_ptr, num_text);
  1078. if (verbose)
  1079. {
  1080. int i;
  1081. printf("\n");
  1082. for (i=0; i<num_text; i++)
  1083. {
  1084. printf(" Text compression[%d]=%d\n",
  1085. i, text_ptr[i].compression);
  1086. }
  1087. }
  1088. png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
  1089. }
  1090. }
  1091. #endif
  1092. #ifdef PNG_tIME_SUPPORTED
  1093. {
  1094. png_timep mod_time;
  1095. if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
  1096. {
  1097. png_set_tIME(write_ptr, write_info_ptr, mod_time);
  1098. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1099. if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
  1100. tIME_string[(sizeof tIME_string) - 1] = '\0';
  1101. else
  1102. {
  1103. strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
  1104. tIME_string[(sizeof tIME_string) - 1] = '\0';
  1105. }
  1106. tIME_chunk_present++;
  1107. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1108. }
  1109. }
  1110. #endif
  1111. #ifdef PNG_tRNS_SUPPORTED
  1112. {
  1113. png_bytep trans_alpha;
  1114. int num_trans;
  1115. png_color_16p trans_color;
  1116. if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
  1117. &trans_color))
  1118. {
  1119. int sample_max = (1 << bit_depth);
  1120. /* libpng doesn't reject a tRNS chunk with out-of-range samples */
  1121. if (!((color_type == PNG_COLOR_TYPE_GRAY &&
  1122. (int)trans_color->gray > sample_max) ||
  1123. (color_type == PNG_COLOR_TYPE_RGB &&
  1124. ((int)trans_color->red > sample_max ||
  1125. (int)trans_color->green > sample_max ||
  1126. (int)trans_color->blue > sample_max))))
  1127. png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
  1128. trans_color);
  1129. }
  1130. }
  1131. #endif
  1132. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1133. {
  1134. png_unknown_chunkp unknowns;
  1135. int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
  1136. &unknowns);
  1137. if (num_unknowns)
  1138. {
  1139. png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
  1140. num_unknowns);
  1141. #if PNG_LIBPNG_VER < 10600
  1142. /* Copy the locations from the read_info_ptr. The automatically
  1143. * generated locations in write_end_info_ptr are wrong prior to 1.6.0
  1144. * because they are reset from the write pointer (removed in 1.6.0).
  1145. */
  1146. {
  1147. int i;
  1148. for (i = 0; i < num_unknowns; i++)
  1149. png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
  1150. unknowns[i].location);
  1151. }
  1152. #endif
  1153. }
  1154. }
  1155. #endif
  1156. #ifdef PNG_WRITE_SUPPORTED
  1157. pngtest_debug("Writing info struct");
  1158. /* Write the info in two steps so that if we write the 'unknown' chunks here
  1159. * they go to the correct place.
  1160. */
  1161. png_write_info_before_PLTE(write_ptr, write_info_ptr);
  1162. write_chunks(write_ptr, before_PLTE); /* before PLTE */
  1163. png_write_info(write_ptr, write_info_ptr);
  1164. write_chunks(write_ptr, before_IDAT); /* after PLTE */
  1165. #endif
  1166. #ifdef SINGLE_ROWBUF_ALLOC
  1167. pngtest_debug("Allocating row buffer...");
  1168. row_buf = (png_bytep)png_malloc(read_ptr,
  1169. png_get_rowbytes(read_ptr, read_info_ptr));
  1170. pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
  1171. #endif /* SINGLE_ROWBUF_ALLOC */
  1172. pngtest_debug("Writing row data");
  1173. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1174. num_pass = png_set_interlace_handling(read_ptr);
  1175. if (png_set_interlace_handling(write_ptr) != num_pass)
  1176. png_error(write_ptr, "png_set_interlace_handling: inconsistent num_pass");
  1177. #endif
  1178. #ifdef PNGTEST_TIMING
  1179. t_stop = (float)clock();
  1180. t_misc += (t_stop - t_start);
  1181. t_start = t_stop;
  1182. #endif
  1183. for (pass = 0; pass < num_pass; pass++)
  1184. {
  1185. pngtest_debug1("Writing row data for pass %d", pass);
  1186. for (y = 0; y < height; y++)
  1187. {
  1188. #ifndef SINGLE_ROWBUF_ALLOC
  1189. pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
  1190. row_buf = (png_bytep)png_malloc(read_ptr,
  1191. png_get_rowbytes(read_ptr, read_info_ptr));
  1192. pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
  1193. png_get_rowbytes(read_ptr, read_info_ptr));
  1194. #endif /* !SINGLE_ROWBUF_ALLOC */
  1195. png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
  1196. #ifdef PNG_WRITE_SUPPORTED
  1197. #ifdef PNGTEST_TIMING
  1198. t_stop = (float)clock();
  1199. t_decode += (t_stop - t_start);
  1200. t_start = t_stop;
  1201. #endif
  1202. png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
  1203. #ifdef PNGTEST_TIMING
  1204. t_stop = (float)clock();
  1205. t_encode += (t_stop - t_start);
  1206. t_start = t_stop;
  1207. #endif
  1208. #endif /* PNG_WRITE_SUPPORTED */
  1209. #ifndef SINGLE_ROWBUF_ALLOC
  1210. pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
  1211. png_free(read_ptr, row_buf);
  1212. row_buf = NULL;
  1213. #endif /* !SINGLE_ROWBUF_ALLOC */
  1214. }
  1215. }
  1216. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  1217. # ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  1218. png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
  1219. # endif
  1220. # ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1221. png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
  1222. # endif
  1223. #endif
  1224. pngtest_debug("Reading and writing end_info data");
  1225. png_read_end(read_ptr, end_info_ptr);
  1226. #ifdef PNG_TEXT_SUPPORTED
  1227. {
  1228. png_textp text_ptr;
  1229. int num_text;
  1230. if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
  1231. {
  1232. pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1233. pngtest_check_text_support(read_ptr, text_ptr, num_text);
  1234. if (verbose)
  1235. {
  1236. int i;
  1237. printf("\n");
  1238. for (i=0; i<num_text; i++)
  1239. {
  1240. printf(" Text compression[%d]=%d\n",
  1241. i, text_ptr[i].compression);
  1242. }
  1243. }
  1244. png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
  1245. }
  1246. }
  1247. #endif
  1248. #ifdef PNG_tIME_SUPPORTED
  1249. {
  1250. png_timep mod_time;
  1251. if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
  1252. {
  1253. png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
  1254. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1255. if (png_convert_to_rfc1123_buffer(tIME_string, mod_time))
  1256. tIME_string[(sizeof tIME_string) - 1] = '\0';
  1257. else
  1258. {
  1259. strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
  1260. tIME_string[(sizeof tIME_string)-1] = '\0';
  1261. }
  1262. tIME_chunk_present++;
  1263. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1264. }
  1265. }
  1266. #endif
  1267. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1268. {
  1269. png_unknown_chunkp unknowns;
  1270. int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
  1271. &unknowns);
  1272. if (num_unknowns)
  1273. {
  1274. png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
  1275. num_unknowns);
  1276. #if PNG_LIBPNG_VER < 10600
  1277. /* Copy the locations from the read_info_ptr. The automatically
  1278. * generated locations in write_end_info_ptr are wrong prior to 1.6.0
  1279. * because they are reset from the write pointer (removed in 1.6.0).
  1280. */
  1281. {
  1282. int i;
  1283. for (i = 0; i < num_unknowns; i++)
  1284. png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
  1285. unknowns[i].location);
  1286. }
  1287. #endif
  1288. }
  1289. }
  1290. #endif
  1291. #ifdef PNG_WRITE_SUPPORTED
  1292. #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  1293. /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
  1294. * This is here just to make pngtest replicate the results from libpng
  1295. * versions prior to 1.5.4, and to test this new API.
  1296. */
  1297. png_set_text_compression_strategy(write_ptr, Z_FILTERED);
  1298. #endif
  1299. /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
  1300. * do it is to write them *before* calling png_write_end. When unknown
  1301. * chunks are written by libpng, however, they are written just before IEND.
  1302. * There seems to be no way round this, however vpAg/sTER are not expected
  1303. * after IDAT.
  1304. */
  1305. write_chunks(write_ptr, after_IDAT);
  1306. png_write_end(write_ptr, write_end_info_ptr);
  1307. #endif
  1308. #ifdef PNG_EASY_ACCESS_SUPPORTED
  1309. if (verbose)
  1310. {
  1311. png_uint_32 iwidth, iheight;
  1312. iwidth = png_get_image_width(write_ptr, write_info_ptr);
  1313. iheight = png_get_image_height(write_ptr, write_info_ptr);
  1314. fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
  1315. (unsigned long)iwidth, (unsigned long)iheight);
  1316. }
  1317. #endif
  1318. pngtest_debug("Destroying data structs");
  1319. #ifdef SINGLE_ROWBUF_ALLOC
  1320. pngtest_debug("destroying row_buf for read_ptr");
  1321. png_free(read_ptr, row_buf);
  1322. row_buf = NULL;
  1323. #endif /* SINGLE_ROWBUF_ALLOC */
  1324. pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
  1325. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  1326. #ifdef PNG_WRITE_SUPPORTED
  1327. pngtest_debug("destroying write_end_info_ptr");
  1328. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  1329. pngtest_debug("destroying write_ptr, write_info_ptr");
  1330. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  1331. #endif
  1332. pngtest_debug("Destruction complete.");
  1333. FCLOSE(fpin);
  1334. FCLOSE(fpout);
  1335. /* Summarize any warnings or errors and in 'strict' mode fail the test.
  1336. * Unsupported chunks can result in warnings, in that case ignore the strict
  1337. * setting, otherwise fail the test on warnings as well as errors.
  1338. */
  1339. if (error_count > 0)
  1340. {
  1341. /* We don't really expect to get here because of the setjmp handling
  1342. * above, but this is safe.
  1343. */
  1344. fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
  1345. inname, error_count, warning_count);
  1346. if (strict != 0)
  1347. return (1);
  1348. }
  1349. # ifdef PNG_WRITE_SUPPORTED
  1350. /* If there we no write support nothing was written! */
  1351. else if (unsupported_chunks > 0)
  1352. {
  1353. fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
  1354. inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
  1355. }
  1356. # endif
  1357. else if (warning_count > 0)
  1358. {
  1359. fprintf(STDERR, "\n %s: %d libpng warnings found",
  1360. inname, warning_count);
  1361. if (strict != 0)
  1362. return (1);
  1363. }
  1364. pngtest_debug("Opening files for comparison");
  1365. if ((fpin = fopen(inname, "rb")) == NULL)
  1366. {
  1367. fprintf(STDERR, "Could not find file %s\n", inname);
  1368. return (1);
  1369. }
  1370. if ((fpout = fopen(outname, "rb")) == NULL)
  1371. {
  1372. fprintf(STDERR, "Could not find file %s\n", outname);
  1373. FCLOSE(fpin);
  1374. return (1);
  1375. }
  1376. #ifdef PNG_WRITE_SUPPORTED /* else nothing was written */
  1377. if (interlace_preserved) /* else the files will be changed */
  1378. {
  1379. for (;;)
  1380. {
  1381. static int wrote_question = 0;
  1382. png_size_t num_in, num_out;
  1383. char inbuf[256], outbuf[256];
  1384. num_in = fread(inbuf, 1, sizeof inbuf, fpin);
  1385. num_out = fread(outbuf, 1, sizeof outbuf, fpout);
  1386. if (num_in != num_out)
  1387. {
  1388. fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
  1389. inname, outname);
  1390. if (wrote_question == 0 && unsupported_chunks == 0)
  1391. {
  1392. fprintf(STDERR,
  1393. " Was %s written with the same maximum IDAT chunk size (%d bytes),",
  1394. inname, PNG_ZBUF_SIZE);
  1395. fprintf(STDERR,
  1396. "\n filtering heuristic (libpng default), compression");
  1397. fprintf(STDERR,
  1398. " level (zlib default),\n and zlib version (%s)?\n\n",
  1399. ZLIB_VERSION);
  1400. wrote_question = 1;
  1401. }
  1402. FCLOSE(fpin);
  1403. FCLOSE(fpout);
  1404. if (strict != 0 && unsupported_chunks == 0)
  1405. return (1);
  1406. else
  1407. return (0);
  1408. }
  1409. if (!num_in)
  1410. break;
  1411. if (memcmp(inbuf, outbuf, num_in))
  1412. {
  1413. fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
  1414. outname);
  1415. if (wrote_question == 0 && unsupported_chunks == 0)
  1416. {
  1417. fprintf(STDERR,
  1418. " Was %s written with the same maximum IDAT chunk size (%d bytes),",
  1419. inname, PNG_ZBUF_SIZE);
  1420. fprintf(STDERR,
  1421. "\n filtering heuristic (libpng default), compression");
  1422. fprintf(STDERR,
  1423. " level (zlib default),\n and zlib version (%s)?\n\n",
  1424. ZLIB_VERSION);
  1425. wrote_question = 1;
  1426. }
  1427. FCLOSE(fpin);
  1428. FCLOSE(fpout);
  1429. /* NOTE: the unsupported_chunks escape is permitted here because
  1430. * unsupported text chunk compression will result in the compression
  1431. * mode being changed (to NONE) yet, in the test case, the result
  1432. * can be exactly the same size!
  1433. */
  1434. if (strict != 0 && unsupported_chunks == 0)
  1435. return (1);
  1436. else
  1437. return (0);
  1438. }
  1439. }
  1440. }
  1441. #endif /* PNG_WRITE_SUPPORTED */
  1442. FCLOSE(fpin);
  1443. FCLOSE(fpout);
  1444. return (0);
  1445. }
  1446. /* Input and output filenames */
  1447. #ifdef RISCOS
  1448. static PNG_CONST char *inname = "pngtest/png";
  1449. static PNG_CONST char *outname = "pngout/png";
  1450. #else
  1451. static PNG_CONST char *inname = "pngtest.png";
  1452. static PNG_CONST char *outname = "pngout.png";
  1453. #endif
  1454. int
  1455. main(int argc, char *argv[])
  1456. {
  1457. int multiple = 0;
  1458. int ierror = 0;
  1459. fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
  1460. fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
  1461. fprintf(STDERR, "%s", png_get_copyright(NULL));
  1462. /* Show the version of libpng used in building the library */
  1463. fprintf(STDERR, " library (%lu):%s",
  1464. (unsigned long)png_access_version_number(),
  1465. png_get_header_version(NULL));
  1466. /* Show the version of libpng used in building the application */
  1467. fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
  1468. PNG_HEADER_VERSION_STRING);
  1469. /* Do some consistency checking on the memory allocation settings, I'm
  1470. * not sure this matters, but it is nice to know, the first of these
  1471. * tests should be impossible because of the way the macros are set
  1472. * in pngconf.h
  1473. */
  1474. #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
  1475. fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
  1476. #endif
  1477. /* I think the following can happen. */
  1478. #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
  1479. fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
  1480. #endif
  1481. if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
  1482. {
  1483. fprintf(STDERR,
  1484. "Warning: versions are different between png.h and png.c\n");
  1485. fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
  1486. fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
  1487. ++ierror;
  1488. }
  1489. if (argc > 1)
  1490. {
  1491. if (strcmp(argv[1], "-m") == 0)
  1492. {
  1493. multiple = 1;
  1494. status_dots_requested = 0;
  1495. }
  1496. else if (strcmp(argv[1], "-mv") == 0 ||
  1497. strcmp(argv[1], "-vm") == 0 )
  1498. {
  1499. multiple = 1;
  1500. verbose = 1;
  1501. status_dots_requested = 1;
  1502. }
  1503. else if (strcmp(argv[1], "-v") == 0)
  1504. {
  1505. verbose = 1;
  1506. status_dots_requested = 1;
  1507. inname = argv[2];
  1508. }
  1509. else if (strcmp(argv[1], "--strict") == 0)
  1510. {
  1511. status_dots_requested = 0;
  1512. verbose = 1;
  1513. inname = argv[2];
  1514. strict++;
  1515. relaxed = 0;
  1516. }
  1517. else if (strcmp(argv[1], "--relaxed") == 0)
  1518. {
  1519. status_dots_requested = 0;
  1520. verbose = 1;
  1521. inname = argv[2];
  1522. strict = 0;
  1523. relaxed++;
  1524. }
  1525. else
  1526. {
  1527. inname = argv[1];
  1528. status_dots_requested = 0;
  1529. }
  1530. }
  1531. if (!multiple && argc == 3 + verbose)
  1532. outname = argv[2 + verbose];
  1533. if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
  1534. {
  1535. fprintf(STDERR,
  1536. "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
  1537. argv[0], argv[0]);
  1538. fprintf(STDERR,
  1539. " reads/writes one PNG file (without -m) or multiple files (-m)\n");
  1540. fprintf(STDERR,
  1541. " with -m %s is used as a temporary file\n", outname);
  1542. exit(1);
  1543. }
  1544. if (multiple)
  1545. {
  1546. int i;
  1547. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1548. int allocation_now = current_allocation;
  1549. #endif
  1550. for (i=2; i<argc; ++i)
  1551. {
  1552. int kerror;
  1553. fprintf(STDERR, "\n Testing %s:", argv[i]);
  1554. kerror = test_one_file(argv[i], outname);
  1555. if (kerror == 0)
  1556. {
  1557. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1558. int k;
  1559. #endif
  1560. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1561. fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1562. (unsigned long)zero_samples);
  1563. #else
  1564. fprintf(STDERR, " PASS\n");
  1565. #endif
  1566. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1567. for (k = 0; k<256; k++)
  1568. if (filters_used[k])
  1569. fprintf(STDERR, " Filter %d was used %lu times\n",
  1570. k, (unsigned long)filters_used[k]);
  1571. #endif
  1572. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1573. if (tIME_chunk_present != 0)
  1574. fprintf(STDERR, " tIME = %s\n", tIME_string);
  1575. tIME_chunk_present = 0;
  1576. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1577. }
  1578. else
  1579. {
  1580. fprintf(STDERR, " FAIL\n");
  1581. ierror += kerror;
  1582. }
  1583. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1584. if (allocation_now != current_allocation)
  1585. fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
  1586. current_allocation - allocation_now);
  1587. if (current_allocation != 0)
  1588. {
  1589. memory_infop pinfo = pinformation;
  1590. fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
  1591. current_allocation);
  1592. while (pinfo != NULL)
  1593. {
  1594. fprintf(STDERR, " %lu bytes at %x\n",
  1595. (unsigned long)pinfo->size,
  1596. (unsigned int)pinfo->pointer);
  1597. pinfo = pinfo->next;
  1598. }
  1599. }
  1600. #endif
  1601. }
  1602. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1603. fprintf(STDERR, " Current memory allocation: %10d bytes\n",
  1604. current_allocation);
  1605. fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
  1606. maximum_allocation);
  1607. fprintf(STDERR, " Total memory allocation: %10d bytes\n",
  1608. total_allocation);
  1609. fprintf(STDERR, " Number of allocations: %10d\n",
  1610. num_allocations);
  1611. #endif
  1612. }
  1613. else
  1614. {
  1615. int i;
  1616. for (i = 0; i<3; ++i)
  1617. {
  1618. int kerror;
  1619. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1620. int allocation_now = current_allocation;
  1621. #endif
  1622. if (i == 1)
  1623. status_dots_requested = 1;
  1624. else if (verbose == 0)
  1625. status_dots_requested = 0;
  1626. if (i == 0 || verbose == 1 || ierror != 0)
  1627. fprintf(STDERR, "\n Testing %s:", inname);
  1628. kerror = test_one_file(inname, outname);
  1629. if (kerror == 0)
  1630. {
  1631. if (verbose == 1 || i == 2)
  1632. {
  1633. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1634. int k;
  1635. #endif
  1636. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1637. fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1638. (unsigned long)zero_samples);
  1639. #else
  1640. fprintf(STDERR, " PASS\n");
  1641. #endif
  1642. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1643. for (k = 0; k<256; k++)
  1644. if (filters_used[k])
  1645. fprintf(STDERR, " Filter %d was used %lu times\n",
  1646. k, (unsigned long)filters_used[k]);
  1647. #endif
  1648. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1649. if (tIME_chunk_present != 0)
  1650. fprintf(STDERR, " tIME = %s\n", tIME_string);
  1651. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1652. }
  1653. }
  1654. else
  1655. {
  1656. if (verbose == 0 && i != 2)
  1657. fprintf(STDERR, "\n Testing %s:", inname);
  1658. fprintf(STDERR, " FAIL\n");
  1659. ierror += kerror;
  1660. }
  1661. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1662. if (allocation_now != current_allocation)
  1663. fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
  1664. current_allocation - allocation_now);
  1665. if (current_allocation != 0)
  1666. {
  1667. memory_infop pinfo = pinformation;
  1668. fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
  1669. current_allocation);
  1670. while (pinfo != NULL)
  1671. {
  1672. fprintf(STDERR, " %lu bytes at %x\n",
  1673. (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
  1674. pinfo = pinfo->next;
  1675. }
  1676. }
  1677. #endif
  1678. }
  1679. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1680. fprintf(STDERR, " Current memory allocation: %10d bytes\n",
  1681. current_allocation);
  1682. fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
  1683. maximum_allocation);
  1684. fprintf(STDERR, " Total memory allocation: %10d bytes\n",
  1685. total_allocation);
  1686. fprintf(STDERR, " Number of allocations: %10d\n",
  1687. num_allocations);
  1688. #endif
  1689. }
  1690. #ifdef PNGTEST_TIMING
  1691. t_stop = (float)clock();
  1692. t_misc += (t_stop - t_start);
  1693. t_start = t_stop;
  1694. fprintf(STDERR, " CPU time used = %.3f seconds",
  1695. (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
  1696. fprintf(STDERR, " (decoding %.3f,\n",
  1697. t_decode/(float)CLOCKS_PER_SEC);
  1698. fprintf(STDERR, " encoding %.3f ,",
  1699. t_encode/(float)CLOCKS_PER_SEC);
  1700. fprintf(STDERR, " other %.3f seconds)\n\n",
  1701. t_misc/(float)CLOCKS_PER_SEC);
  1702. #endif
  1703. if (ierror == 0)
  1704. fprintf(STDERR, " libpng passes test\n");
  1705. else
  1706. fprintf(STDERR, " libpng FAILS test\n");
  1707. return (int)(ierror != 0);
  1708. }
  1709. #else
  1710. int
  1711. main(void)
  1712. {
  1713. fprintf(STDERR,
  1714. " test ignored because libpng was not built with read support\n");
  1715. /* And skip this test */
  1716. return PNG_LIBPNG_VER < 10600 ? 0 : 77;
  1717. }
  1718. #endif
  1719. /* Generate a compiler error if there is an old png.h in the search path. */
  1720. typedef png_libpng_version_1_6_10 Your_png_h_is_not_version_1_6_10;