pngtest.c 55 KB

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