pngtest.c 59 KB

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