pngtest.c 61 KB

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