pngtest.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /* pngtest.c - a simple test program to test libpng
  2. *
  3. * libpng 1.2.8 - December 3, 2004
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2004 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. *
  9. * This program reads in a PNG image, writes it out again, and then
  10. * compares the two files. If the files are identical, this shows that
  11. * the basic chunk handling, filtering, and (de)compression code is working
  12. * properly. It does not currently test all of the transforms, although
  13. * it probably should.
  14. *
  15. * The program will report "FAIL" in certain legitimate cases:
  16. * 1) when the compression level or filter selection method is changed.
  17. * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
  18. * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
  19. * exist in the input file.
  20. * 4) others not listed here...
  21. * In these cases, it is best to check with another tool such as "pngcheck"
  22. * to see what the differences between the two files are.
  23. *
  24. * If a filename is given on the command-line, then this file is used
  25. * for the input, rather than the default "pngtest.png". This allows
  26. * testing a wide variety of files easily. You can also test a number
  27. * of files at once by typing "pngtest -m file1.png file2.png ..."
  28. */
  29. #include "png.h"
  30. #if defined(_WIN32_WCE)
  31. # if _WIN32_WCE < 211
  32. __error__ (f|w)printf functions are not supported on old WindowsCE.;
  33. # endif
  34. # include <windows.h>
  35. # include <stdlib.h>
  36. # define READFILE(file, data, length, check) \
  37. if (ReadFile(file, data, length, &check,NULL)) check = 0
  38. # define WRITEFILE(file, data, length, check)) \
  39. if (WriteFile(file, data, length, &check, NULL)) check = 0
  40. # define FCLOSE(file) CloseHandle(file)
  41. #else
  42. # include <stdio.h>
  43. # include <stdlib.h>
  44. # include <assert.h>
  45. # define READFILE(file, data, length, check) \
  46. check=(png_size_t)fread(data,(png_size_t)1,length,file)
  47. # define WRITEFILE(file, data, length, check) \
  48. check=(png_size_t)fwrite(data,(png_size_t)1, length, file)
  49. # define FCLOSE(file) fclose(file)
  50. #endif
  51. #if defined(PNG_NO_STDIO)
  52. # if defined(_WIN32_WCE)
  53. typedef HANDLE png_FILE_p;
  54. # else
  55. typedef FILE * png_FILE_p;
  56. # endif
  57. #endif
  58. /* Makes pngtest verbose so we can find problems (needs to be before png.h) */
  59. #ifndef PNG_DEBUG
  60. # define PNG_DEBUG 0
  61. #endif
  62. #if !PNG_DEBUG
  63. # define SINGLE_ROWBUF_ALLOC /* makes buffer overruns easier to nail */
  64. #endif
  65. /* Turn on CPU timing
  66. #define PNGTEST_TIMING
  67. */
  68. #ifdef PNG_NO_FLOATING_POINT_SUPPORTED
  69. #undef PNGTEST_TIMING
  70. #endif
  71. #ifdef PNGTEST_TIMING
  72. static float t_start, t_stop, t_decode, t_encode, t_misc;
  73. #include <time.h>
  74. #endif
  75. /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
  76. #ifndef png_jmpbuf
  77. # define png_jmpbuf(png_ptr) png_ptr->jmpbuf
  78. #endif
  79. #ifdef PNGTEST_TIMING
  80. static float t_start, t_stop, t_decode, t_encode, t_misc;
  81. #if !defined(PNG_tIME_SUPPORTED)
  82. #include <time.h>
  83. #endif
  84. #endif
  85. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  86. static int tIME_chunk_present=0;
  87. static char tIME_string[30] = "no tIME chunk present in file";
  88. #endif
  89. static int verbose = 0;
  90. int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
  91. #ifdef __TURBOC__
  92. #include <mem.h>
  93. #endif
  94. /* defined so I can write to a file on gui/windowing platforms */
  95. /* #define STDERR stderr */
  96. #define STDERR stdout /* for DOS */
  97. /* example of using row callbacks to make a simple progress meter */
  98. static int status_pass=1;
  99. static int status_dots_requested=0;
  100. static int status_dots=1;
  101. void
  102. #ifdef PNG_1_0_X
  103. PNGAPI
  104. #endif
  105. read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
  106. void
  107. #ifdef PNG_1_0_X
  108. PNGAPI
  109. #endif
  110. read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
  111. {
  112. if(png_ptr == NULL || row_number > PNG_UINT_31_MAX) return;
  113. if(status_pass != pass)
  114. {
  115. fprintf(stdout,"\n Pass %d: ",pass);
  116. status_pass = pass;
  117. status_dots = 31;
  118. }
  119. status_dots--;
  120. if(status_dots == 0)
  121. {
  122. fprintf(stdout, "\n ");
  123. status_dots=30;
  124. }
  125. fprintf(stdout, "r");
  126. }
  127. void
  128. #ifdef PNG_1_0_X
  129. PNGAPI
  130. #endif
  131. write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
  132. void
  133. #ifdef PNG_1_0_X
  134. PNGAPI
  135. #endif
  136. write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
  137. {
  138. if(png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) return;
  139. fprintf(stdout, "w");
  140. }
  141. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  142. /* Example of using user transform callback (we don't transform anything,
  143. but merely examine the row filters. We set this to 256 rather than
  144. 5 in case illegal filter values are present.) */
  145. static png_uint_32 filters_used[256];
  146. void
  147. #ifdef PNG_1_0_X
  148. PNGAPI
  149. #endif
  150. count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
  151. void
  152. #ifdef PNG_1_0_X
  153. PNGAPI
  154. #endif
  155. count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
  156. {
  157. if(png_ptr != NULL && row_info != NULL)
  158. ++filters_used[*(data-1)];
  159. }
  160. #endif
  161. #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  162. /* example of using user transform callback (we don't transform anything,
  163. but merely count the zero samples) */
  164. static png_uint_32 zero_samples;
  165. void
  166. #ifdef PNG_1_0_X
  167. PNGAPI
  168. #endif
  169. count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
  170. void
  171. #ifdef PNG_1_0_X
  172. PNGAPI
  173. #endif
  174. count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
  175. {
  176. png_bytep dp = data;
  177. if(png_ptr == NULL)return;
  178. /* contents of row_info:
  179. * png_uint_32 width width of row
  180. * png_uint_32 rowbytes number of bytes in row
  181. * png_byte color_type color type of pixels
  182. * png_byte bit_depth bit depth of samples
  183. * png_byte channels number of channels (1-4)
  184. * png_byte pixel_depth bits per pixel (depth*channels)
  185. */
  186. /* counts the number of zero samples (or zero pixels if color_type is 3 */
  187. if(row_info->color_type == 0 || row_info->color_type == 3)
  188. {
  189. int pos=0;
  190. png_uint_32 n, nstop;
  191. for (n=0, nstop=row_info->width; n<nstop; n++)
  192. {
  193. if(row_info->bit_depth == 1)
  194. {
  195. if(((*dp << pos++ ) & 0x80) == 0) zero_samples++;
  196. if(pos == 8)
  197. {
  198. pos = 0;
  199. dp++;
  200. }
  201. }
  202. if(row_info->bit_depth == 2)
  203. {
  204. if(((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;
  205. if(pos == 8)
  206. {
  207. pos = 0;
  208. dp++;
  209. }
  210. }
  211. if(row_info->bit_depth == 4)
  212. {
  213. if(((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;
  214. if(pos == 8)
  215. {
  216. pos = 0;
  217. dp++;
  218. }
  219. }
  220. if(row_info->bit_depth == 8)
  221. if(*dp++ == 0) zero_samples++;
  222. if(row_info->bit_depth == 16)
  223. {
  224. if((*dp | *(dp+1)) == 0) zero_samples++;
  225. dp+=2;
  226. }
  227. }
  228. }
  229. else /* other color types */
  230. {
  231. png_uint_32 n, nstop;
  232. int channel;
  233. int color_channels = row_info->channels;
  234. if(row_info->color_type > 3)color_channels--;
  235. for (n=0, nstop=row_info->width; n<nstop; n++)
  236. {
  237. for (channel = 0; channel < color_channels; channel++)
  238. {
  239. if(row_info->bit_depth == 8)
  240. if(*dp++ == 0) zero_samples++;
  241. if(row_info->bit_depth == 16)
  242. {
  243. if((*dp | *(dp+1)) == 0) zero_samples++;
  244. dp+=2;
  245. }
  246. }
  247. if(row_info->color_type > 3)
  248. {
  249. dp++;
  250. if(row_info->bit_depth == 16)dp++;
  251. }
  252. }
  253. }
  254. }
  255. #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
  256. static int wrote_question = 0;
  257. #if defined(PNG_NO_STDIO)
  258. /* START of code to validate stdio-free compilation */
  259. /* These copies of the default read/write functions come from pngrio.c and */
  260. /* pngwio.c. They allow "don't include stdio" testing of the library. */
  261. /* This is the function that does the actual reading of data. If you are
  262. not reading from a standard C stream, you should create a replacement
  263. read_data function and use it at run time with png_set_read_fn(), rather
  264. than changing the library. */
  265. #ifndef USE_FAR_KEYWORD
  266. static void
  267. pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  268. {
  269. png_size_t check;
  270. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  271. * instead of an int, which is what fread() actually returns.
  272. */
  273. READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
  274. if (check != length)
  275. {
  276. png_error(png_ptr, "Read Error!");
  277. }
  278. }
  279. #else
  280. /* this is the model-independent version. Since the standard I/O library
  281. can't handle far buffers in the medium and small models, we have to copy
  282. the data.
  283. */
  284. #define NEAR_BUF_SIZE 1024
  285. #define MIN(a,b) (a <= b ? a : b)
  286. static void
  287. pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  288. {
  289. int check;
  290. png_byte *n_data;
  291. png_FILE_p io_ptr;
  292. /* Check if data really is near. If so, use usual code. */
  293. n_data = (png_byte *)CVT_PTR_NOCHECK(data);
  294. io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
  295. if ((png_bytep)n_data == data)
  296. {
  297. READFILE(io_ptr, n_data, length, check);
  298. }
  299. else
  300. {
  301. png_byte buf[NEAR_BUF_SIZE];
  302. png_size_t read, remaining, err;
  303. check = 0;
  304. remaining = length;
  305. do
  306. {
  307. read = MIN(NEAR_BUF_SIZE, remaining);
  308. READFILE(io_ptr, buf, 1, err);
  309. png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  310. if(err != read)
  311. break;
  312. else
  313. check += err;
  314. data += read;
  315. remaining -= read;
  316. }
  317. while (remaining != 0);
  318. }
  319. if (check != length)
  320. {
  321. png_error(png_ptr, "read Error");
  322. }
  323. }
  324. #endif /* USE_FAR_KEYWORD */
  325. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  326. static void
  327. pngtest_flush(png_structp png_ptr)
  328. {
  329. #if !defined(_WIN32_WCE)
  330. png_FILE_p io_ptr;
  331. io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
  332. if (io_ptr != NULL)
  333. fflush(io_ptr);
  334. #endif
  335. }
  336. #endif
  337. /* This is the function that does the actual writing of data. If you are
  338. not writing to a standard C stream, you should create a replacement
  339. write_data function and use it at run time with png_set_write_fn(), rather
  340. than changing the library. */
  341. #ifndef USE_FAR_KEYWORD
  342. static void
  343. pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  344. {
  345. png_uint_32 check;
  346. WRITEFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
  347. if (check != length)
  348. {
  349. png_error(png_ptr, "Write Error");
  350. }
  351. }
  352. #else
  353. /* this is the model-independent version. Since the standard I/O library
  354. can't handle far buffers in the medium and small models, we have to copy
  355. the data.
  356. */
  357. #define NEAR_BUF_SIZE 1024
  358. #define MIN(a,b) (a <= b ? a : b)
  359. static void
  360. pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  361. {
  362. png_uint_32 check;
  363. png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
  364. png_FILE_p io_ptr;
  365. /* Check if data really is near. If so, use usual code. */
  366. near_data = (png_byte *)CVT_PTR_NOCHECK(data);
  367. io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
  368. if ((png_bytep)near_data == data)
  369. {
  370. WRITEFILE(io_ptr, near_data, length, check);
  371. }
  372. else
  373. {
  374. png_byte buf[NEAR_BUF_SIZE];
  375. png_size_t written, remaining, err;
  376. check = 0;
  377. remaining = length;
  378. do
  379. {
  380. written = MIN(NEAR_BUF_SIZE, remaining);
  381. png_memcpy(buf, data, written); /* copy far buffer to near buffer */
  382. WRITEFILE(io_ptr, buf, written, err);
  383. if (err != written)
  384. break;
  385. else
  386. check += err;
  387. data += written;
  388. remaining -= written;
  389. }
  390. while (remaining != 0);
  391. }
  392. if (check != length)
  393. {
  394. png_error(png_ptr, "Write Error");
  395. }
  396. }
  397. #endif /* USE_FAR_KEYWORD */
  398. /* This function is called when there is a warning, but the library thinks
  399. * it can continue anyway. Replacement functions don't have to do anything
  400. * here if you don't want to. In the default configuration, png_ptr is
  401. * not used, but it is passed in case it may be useful.
  402. */
  403. static void
  404. pngtest_warning(png_structp png_ptr, png_const_charp message)
  405. {
  406. PNG_CONST char *name = "UNKNOWN (ERROR!)";
  407. if (png_ptr != NULL && png_ptr->error_ptr != NULL)
  408. name = png_ptr->error_ptr;
  409. fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
  410. }
  411. /* This is the default error handling function. Note that replacements for
  412. * this function MUST NOT RETURN, or the program will likely crash. This
  413. * function is used by default, or if the program supplies NULL for the
  414. * error function pointer in png_set_error_fn().
  415. */
  416. static void
  417. pngtest_error(png_structp png_ptr, png_const_charp message)
  418. {
  419. pngtest_warning(png_ptr, message);
  420. /* We can return because png_error calls the default handler, which is
  421. * actually OK in this case. */
  422. }
  423. #endif /* PNG_NO_STDIO */
  424. /* END of code to validate stdio-free compilation */
  425. /* START of code to validate memory allocation and deallocation */
  426. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  427. /* Allocate memory. For reasonable files, size should never exceed
  428. 64K. However, zlib may allocate more then 64K if you don't tell
  429. it not to. See zconf.h and png.h for more information. zlib does
  430. need to allocate exactly 64K, so whatever you call here must
  431. have the ability to do that.
  432. This piece of code can be compiled to validate max 64K allocations
  433. by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
  434. typedef struct memory_information
  435. {
  436. png_uint_32 size;
  437. png_voidp pointer;
  438. struct memory_information FAR *next;
  439. } memory_information;
  440. typedef memory_information FAR *memory_infop;
  441. static memory_infop pinformation = NULL;
  442. static int current_allocation = 0;
  443. static int maximum_allocation = 0;
  444. static int total_allocation = 0;
  445. static int num_allocations = 0;
  446. png_voidp png_debug_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
  447. void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
  448. png_voidp
  449. png_debug_malloc(png_structp png_ptr, png_uint_32 size)
  450. {
  451. /* png_malloc has already tested for NULL; png_create_struct calls
  452. png_debug_malloc directly, with png_ptr == NULL which is OK */
  453. if (size == 0)
  454. return (NULL);
  455. /* This calls the library allocator twice, once to get the requested
  456. buffer and once to get a new free list entry. */
  457. {
  458. /* Disable malloc_fn and free_fn */
  459. memory_infop pinfo;
  460. png_set_mem_fn(png_ptr, NULL, NULL, NULL);
  461. pinfo = (memory_infop)png_malloc(png_ptr,
  462. (png_uint_32)png_sizeof (*pinfo));
  463. pinfo->size = size;
  464. current_allocation += size;
  465. total_allocation += size;
  466. num_allocations ++;
  467. if (current_allocation > maximum_allocation)
  468. maximum_allocation = current_allocation;
  469. pinfo->pointer = (png_voidp)png_malloc(png_ptr, size);
  470. /* Restore malloc_fn and free_fn */
  471. png_set_mem_fn(png_ptr, png_voidp_NULL, (png_malloc_ptr)png_debug_malloc,
  472. (png_free_ptr)png_debug_free);
  473. if (size != 0 && pinfo->pointer == NULL)
  474. {
  475. current_allocation -= size;
  476. total_allocation -= size;
  477. png_error(png_ptr,
  478. "out of memory in pngtest->png_debug_malloc.");
  479. }
  480. pinfo->next = pinformation;
  481. pinformation = pinfo;
  482. /* Make sure the caller isn't assuming zeroed memory. */
  483. png_memset(pinfo->pointer, 0xdd, pinfo->size);
  484. if(verbose)
  485. printf("png_malloc %lu bytes at %x\n",size,pinfo->pointer);
  486. assert(pinfo->size != 12345678);
  487. return (png_voidp)(pinfo->pointer);
  488. }
  489. }
  490. /* Free a pointer. It is removed from the list at the same time. */
  491. void
  492. png_debug_free(png_structp png_ptr, png_voidp ptr)
  493. {
  494. if (png_ptr == NULL)
  495. fprintf(STDERR, "NULL pointer to png_debug_free.\n");
  496. if (ptr == 0)
  497. {
  498. #if 0 /* This happens all the time. */
  499. fprintf(STDERR, "WARNING: freeing NULL pointer\n");
  500. #endif
  501. return;
  502. }
  503. /* Unlink the element from the list. */
  504. {
  505. memory_infop FAR *ppinfo = &pinformation;
  506. for (;;)
  507. {
  508. memory_infop pinfo = *ppinfo;
  509. if (pinfo->pointer == ptr)
  510. {
  511. *ppinfo = pinfo->next;
  512. current_allocation -= pinfo->size;
  513. if (current_allocation < 0)
  514. fprintf(STDERR, "Duplicate free of memory\n");
  515. /* We must free the list element too, but first kill
  516. the memory that is to be freed. */
  517. png_memset(ptr, 0x55, pinfo->size);
  518. png_free_default(png_ptr, pinfo);
  519. pinfo=NULL;
  520. break;
  521. }
  522. if (pinfo->next == NULL)
  523. {
  524. fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
  525. break;
  526. }
  527. ppinfo = &pinfo->next;
  528. }
  529. }
  530. /* Finally free the data. */
  531. if(verbose)
  532. printf("Freeing %x\n",ptr);
  533. png_free_default(png_ptr, ptr);
  534. ptr=NULL;
  535. }
  536. #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
  537. /* END of code to test memory allocation/deallocation */
  538. /* Test one file */
  539. int
  540. test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
  541. {
  542. static png_FILE_p fpin;
  543. static png_FILE_p fpout; /* "static" prevents setjmp corruption */
  544. png_structp read_ptr;
  545. png_infop read_info_ptr, end_info_ptr;
  546. #ifdef PNG_WRITE_SUPPORTED
  547. png_structp write_ptr;
  548. png_infop write_info_ptr;
  549. png_infop write_end_info_ptr;
  550. #else
  551. png_structp write_ptr = NULL;
  552. png_infop write_info_ptr = NULL;
  553. png_infop write_end_info_ptr = NULL;
  554. #endif
  555. png_bytep row_buf;
  556. png_uint_32 y;
  557. png_uint_32 width, height;
  558. int num_pass, pass;
  559. int bit_depth, color_type;
  560. #ifdef PNG_SETJMP_SUPPORTED
  561. #ifdef USE_FAR_KEYWORD
  562. jmp_buf jmpbuf;
  563. #endif
  564. #endif
  565. #if defined(_WIN32_WCE)
  566. TCHAR path[MAX_PATH];
  567. #endif
  568. char inbuf[256], outbuf[256];
  569. row_buf = NULL;
  570. #if defined(_WIN32_WCE)
  571. MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
  572. if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
  573. #else
  574. if ((fpin = fopen(inname, "rb")) == NULL)
  575. #endif
  576. {
  577. fprintf(STDERR, "Could not find input file %s\n", inname);
  578. return (1);
  579. }
  580. #if defined(_WIN32_WCE)
  581. MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
  582. if ((fpout = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
  583. #else
  584. if ((fpout = fopen(outname, "wb")) == NULL)
  585. #endif
  586. {
  587. fprintf(STDERR, "Could not open output file %s\n", outname);
  588. FCLOSE(fpin);
  589. return (1);
  590. }
  591. png_debug(0, "Allocating read and write structures\n");
  592. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  593. read_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
  594. png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
  595. (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
  596. #else
  597. read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
  598. png_error_ptr_NULL, png_error_ptr_NULL);
  599. #endif
  600. #if defined(PNG_NO_STDIO)
  601. png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
  602. pngtest_warning);
  603. #endif
  604. #ifdef PNG_WRITE_SUPPORTED
  605. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  606. write_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
  607. png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
  608. (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
  609. #else
  610. write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
  611. png_error_ptr_NULL, png_error_ptr_NULL);
  612. #endif
  613. #if defined(PNG_NO_STDIO)
  614. png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
  615. pngtest_warning);
  616. #endif
  617. #endif
  618. png_debug(0, "Allocating read_info, write_info and end_info structures\n");
  619. read_info_ptr = png_create_info_struct(read_ptr);
  620. end_info_ptr = png_create_info_struct(read_ptr);
  621. #ifdef PNG_WRITE_SUPPORTED
  622. write_info_ptr = png_create_info_struct(write_ptr);
  623. write_end_info_ptr = png_create_info_struct(write_ptr);
  624. #endif
  625. #ifdef PNG_SETJMP_SUPPORTED
  626. png_debug(0, "Setting jmpbuf for read struct\n");
  627. #ifdef USE_FAR_KEYWORD
  628. if (setjmp(jmpbuf))
  629. #else
  630. if (setjmp(png_jmpbuf(read_ptr)))
  631. #endif
  632. {
  633. fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
  634. if (row_buf)
  635. png_free(read_ptr, row_buf);
  636. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  637. #ifdef PNG_WRITE_SUPPORTED
  638. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  639. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  640. #endif
  641. FCLOSE(fpin);
  642. FCLOSE(fpout);
  643. return (1);
  644. }
  645. #ifdef USE_FAR_KEYWORD
  646. png_memcpy(png_jmpbuf(read_ptr),jmpbuf,png_sizeof(jmp_buf));
  647. #endif
  648. #ifdef PNG_WRITE_SUPPORTED
  649. png_debug(0, "Setting jmpbuf for write struct\n");
  650. #ifdef USE_FAR_KEYWORD
  651. if (setjmp(jmpbuf))
  652. #else
  653. if (setjmp(png_jmpbuf(write_ptr)))
  654. #endif
  655. {
  656. fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
  657. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  658. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  659. #ifdef PNG_WRITE_SUPPORTED
  660. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  661. #endif
  662. FCLOSE(fpin);
  663. FCLOSE(fpout);
  664. return (1);
  665. }
  666. #ifdef USE_FAR_KEYWORD
  667. png_memcpy(png_jmpbuf(write_ptr),jmpbuf,png_sizeof(jmp_buf));
  668. #endif
  669. #endif
  670. #endif
  671. png_debug(0, "Initializing input and output streams\n");
  672. #if !defined(PNG_NO_STDIO)
  673. png_init_io(read_ptr, fpin);
  674. # ifdef PNG_WRITE_SUPPORTED
  675. png_init_io(write_ptr, fpout);
  676. # endif
  677. #else
  678. png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
  679. # ifdef PNG_WRITE_SUPPORTED
  680. png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
  681. # if defined(PNG_WRITE_FLUSH_SUPPORTED)
  682. pngtest_flush);
  683. # else
  684. NULL);
  685. # endif
  686. # endif
  687. #endif
  688. if(status_dots_requested == 1)
  689. {
  690. #ifdef PNG_WRITE_SUPPORTED
  691. png_set_write_status_fn(write_ptr, write_row_callback);
  692. #endif
  693. png_set_read_status_fn(read_ptr, read_row_callback);
  694. }
  695. else
  696. {
  697. #ifdef PNG_WRITE_SUPPORTED
  698. png_set_write_status_fn(write_ptr, png_write_status_ptr_NULL);
  699. #endif
  700. png_set_read_status_fn(read_ptr, png_read_status_ptr_NULL);
  701. }
  702. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  703. {
  704. int i;
  705. for(i=0; i<256; i++)
  706. filters_used[i]=0;
  707. png_set_read_user_transform_fn(read_ptr, count_filters);
  708. }
  709. #endif
  710. #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  711. zero_samples=0;
  712. png_set_write_user_transform_fn(write_ptr, count_zero_samples);
  713. #endif
  714. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  715. # ifndef PNG_HANDLE_CHUNK_ALWAYS
  716. # define PNG_HANDLE_CHUNK_ALWAYS 3
  717. # endif
  718. png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
  719. png_bytep_NULL, 0);
  720. #endif
  721. #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  722. # ifndef PNG_HANDLE_CHUNK_IF_SAFE
  723. # define PNG_HANDLE_CHUNK_IF_SAFE 2
  724. # endif
  725. png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
  726. png_bytep_NULL, 0);
  727. #endif
  728. png_debug(0, "Reading info struct\n");
  729. png_read_info(read_ptr, read_info_ptr);
  730. png_debug(0, "Transferring info struct\n");
  731. {
  732. int interlace_type, compression_type, filter_type;
  733. if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
  734. &color_type, &interlace_type, &compression_type, &filter_type))
  735. {
  736. png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
  737. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  738. color_type, interlace_type, compression_type, filter_type);
  739. #else
  740. color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
  741. #endif
  742. }
  743. }
  744. #if defined(PNG_FIXED_POINT_SUPPORTED)
  745. #if defined(PNG_cHRM_SUPPORTED)
  746. {
  747. png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
  748. blue_y;
  749. if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
  750. &red_y, &green_x, &green_y, &blue_x, &blue_y))
  751. {
  752. png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
  753. red_y, green_x, green_y, blue_x, blue_y);
  754. }
  755. }
  756. #endif
  757. #if defined(PNG_gAMA_SUPPORTED)
  758. {
  759. png_fixed_point gamma;
  760. if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
  761. {
  762. png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
  763. }
  764. }
  765. #endif
  766. #else /* Use floating point versions */
  767. #if defined(PNG_FLOATING_POINT_SUPPORTED)
  768. #if defined(PNG_cHRM_SUPPORTED)
  769. {
  770. double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
  771. blue_y;
  772. if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
  773. &red_y, &green_x, &green_y, &blue_x, &blue_y))
  774. {
  775. png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
  776. red_y, green_x, green_y, blue_x, blue_y);
  777. }
  778. }
  779. #endif
  780. #if defined(PNG_gAMA_SUPPORTED)
  781. {
  782. double gamma;
  783. if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
  784. {
  785. png_set_gAMA(write_ptr, write_info_ptr, gamma);
  786. }
  787. }
  788. #endif
  789. #endif /* floating point */
  790. #endif /* fixed point */
  791. #if defined(PNG_iCCP_SUPPORTED)
  792. {
  793. png_charp name;
  794. png_charp profile;
  795. png_uint_32 proflen;
  796. int compression_type;
  797. if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
  798. &profile, &proflen))
  799. {
  800. png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
  801. profile, proflen);
  802. }
  803. }
  804. #endif
  805. #if defined(PNG_sRGB_SUPPORTED)
  806. {
  807. int intent;
  808. if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
  809. {
  810. png_set_sRGB(write_ptr, write_info_ptr, intent);
  811. }
  812. }
  813. #endif
  814. {
  815. png_colorp palette;
  816. int num_palette;
  817. if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
  818. {
  819. png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
  820. }
  821. }
  822. #if defined(PNG_bKGD_SUPPORTED)
  823. {
  824. png_color_16p background;
  825. if (png_get_bKGD(read_ptr, read_info_ptr, &background))
  826. {
  827. png_set_bKGD(write_ptr, write_info_ptr, background);
  828. }
  829. }
  830. #endif
  831. #if defined(PNG_hIST_SUPPORTED)
  832. {
  833. png_uint_16p hist;
  834. if (png_get_hIST(read_ptr, read_info_ptr, &hist))
  835. {
  836. png_set_hIST(write_ptr, write_info_ptr, hist);
  837. }
  838. }
  839. #endif
  840. #if defined(PNG_oFFs_SUPPORTED)
  841. {
  842. png_int_32 offset_x, offset_y;
  843. int unit_type;
  844. if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
  845. {
  846. png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
  847. }
  848. }
  849. #endif
  850. #if defined(PNG_pCAL_SUPPORTED)
  851. {
  852. png_charp purpose, units;
  853. png_charpp params;
  854. png_int_32 X0, X1;
  855. int type, nparams;
  856. if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
  857. &nparams, &units, &params))
  858. {
  859. png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
  860. nparams, units, params);
  861. }
  862. }
  863. #endif
  864. #if defined(PNG_pHYs_SUPPORTED)
  865. {
  866. png_uint_32 res_x, res_y;
  867. int unit_type;
  868. if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
  869. {
  870. png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
  871. }
  872. }
  873. #endif
  874. #if defined(PNG_sBIT_SUPPORTED)
  875. {
  876. png_color_8p sig_bit;
  877. if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
  878. {
  879. png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
  880. }
  881. }
  882. #endif
  883. #if defined(PNG_sCAL_SUPPORTED)
  884. #ifdef PNG_FLOATING_POINT_SUPPORTED
  885. {
  886. int unit;
  887. double scal_width, scal_height;
  888. if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
  889. &scal_height))
  890. {
  891. png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
  892. }
  893. }
  894. #else
  895. #ifdef PNG_FIXED_POINT_SUPPORTED
  896. {
  897. int unit;
  898. png_charp scal_width, scal_height;
  899. if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
  900. &scal_height))
  901. {
  902. png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
  903. }
  904. }
  905. #endif
  906. #endif
  907. #endif
  908. #if defined(PNG_TEXT_SUPPORTED)
  909. {
  910. png_textp text_ptr;
  911. int num_text;
  912. if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
  913. {
  914. png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
  915. png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
  916. }
  917. }
  918. #endif
  919. #if defined(PNG_tIME_SUPPORTED)
  920. {
  921. png_timep mod_time;
  922. if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
  923. {
  924. png_set_tIME(write_ptr, write_info_ptr, mod_time);
  925. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  926. /* we have to use png_strcpy instead of "=" because the string
  927. pointed to by png_convert_to_rfc1123() gets free'ed before
  928. we use it */
  929. png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
  930. tIME_chunk_present++;
  931. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  932. }
  933. }
  934. #endif
  935. #if defined(PNG_tRNS_SUPPORTED)
  936. {
  937. png_bytep trans;
  938. int num_trans;
  939. png_color_16p trans_values;
  940. if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
  941. &trans_values))
  942. {
  943. png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
  944. trans_values);
  945. }
  946. }
  947. #endif
  948. #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  949. {
  950. png_unknown_chunkp unknowns;
  951. int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
  952. &unknowns);
  953. if (num_unknowns)
  954. {
  955. png_size_t i;
  956. png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
  957. num_unknowns);
  958. /* copy the locations from the read_info_ptr. The automatically
  959. generated locations in write_info_ptr are wrong because we
  960. haven't written anything yet */
  961. for (i = 0; i < (png_size_t)num_unknowns; i++)
  962. png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
  963. unknowns[i].location);
  964. }
  965. }
  966. #endif
  967. #ifdef PNG_WRITE_SUPPORTED
  968. png_debug(0, "\nWriting info struct\n");
  969. /* If we wanted, we could write info in two steps:
  970. png_write_info_before_PLTE(write_ptr, write_info_ptr);
  971. */
  972. png_write_info(write_ptr, write_info_ptr);
  973. #endif
  974. #ifdef SINGLE_ROWBUF_ALLOC
  975. png_debug(0, "\nAllocating row buffer...");
  976. row_buf = (png_bytep)png_malloc(read_ptr,
  977. png_get_rowbytes(read_ptr, read_info_ptr));
  978. png_debug1(0, "0x%08lx\n\n", (unsigned long)row_buf);
  979. #endif /* SINGLE_ROWBUF_ALLOC */
  980. png_debug(0, "Writing row data\n");
  981. #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
  982. defined(PNG_WRITE_INTERLACING_SUPPORTED)
  983. num_pass = png_set_interlace_handling(read_ptr);
  984. # ifdef PNG_WRITE_SUPPORTED
  985. png_set_interlace_handling(write_ptr);
  986. # endif
  987. #else
  988. num_pass=1;
  989. #endif
  990. #ifdef PNGTEST_TIMING
  991. t_stop = (float)clock();
  992. t_misc += (t_stop - t_start);
  993. t_start = t_stop;
  994. #endif
  995. for (pass = 0; pass < num_pass; pass++)
  996. {
  997. png_debug1(0, "Writing row data for pass %d\n",pass);
  998. for (y = 0; y < height; y++)
  999. {
  1000. #ifndef SINGLE_ROWBUF_ALLOC
  1001. png_debug2(0, "\nAllocating row buffer (pass %d, y = %ld)...", pass,y);
  1002. row_buf = (png_bytep)png_malloc(read_ptr,
  1003. png_get_rowbytes(read_ptr, read_info_ptr));
  1004. png_debug2(0, "0x%08lx (%ld bytes)\n", (unsigned long)row_buf,
  1005. png_get_rowbytes(read_ptr, read_info_ptr));
  1006. #endif /* !SINGLE_ROWBUF_ALLOC */
  1007. png_read_rows(read_ptr, (png_bytepp)&row_buf, png_bytepp_NULL, 1);
  1008. #ifdef PNG_WRITE_SUPPORTED
  1009. #ifdef PNGTEST_TIMING
  1010. t_stop = (float)clock();
  1011. t_decode += (t_stop - t_start);
  1012. t_start = t_stop;
  1013. #endif
  1014. png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
  1015. #ifdef PNGTEST_TIMING
  1016. t_stop = (float)clock();
  1017. t_encode += (t_stop - t_start);
  1018. t_start = t_stop;
  1019. #endif
  1020. #endif /* PNG_WRITE_SUPPORTED */
  1021. #ifndef SINGLE_ROWBUF_ALLOC
  1022. png_debug2(0, "Freeing row buffer (pass %d, y = %ld)\n\n", pass, y);
  1023. png_free(read_ptr, row_buf);
  1024. #endif /* !SINGLE_ROWBUF_ALLOC */
  1025. }
  1026. }
  1027. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1028. png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
  1029. #endif
  1030. #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  1031. png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
  1032. #endif
  1033. png_debug(0, "Reading and writing end_info data\n");
  1034. png_read_end(read_ptr, end_info_ptr);
  1035. #if defined(PNG_TEXT_SUPPORTED)
  1036. {
  1037. png_textp text_ptr;
  1038. int num_text;
  1039. if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
  1040. {
  1041. png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
  1042. png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
  1043. }
  1044. }
  1045. #endif
  1046. #if defined(PNG_tIME_SUPPORTED)
  1047. {
  1048. png_timep mod_time;
  1049. if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
  1050. {
  1051. png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
  1052. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1053. /* we have to use png_strcpy instead of "=" because the string
  1054. pointed to by png_convert_to_rfc1123() gets free'ed before
  1055. we use it */
  1056. png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
  1057. tIME_chunk_present++;
  1058. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1059. }
  1060. }
  1061. #endif
  1062. #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  1063. {
  1064. png_unknown_chunkp unknowns;
  1065. int num_unknowns;
  1066. num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
  1067. &unknowns);
  1068. if (num_unknowns)
  1069. {
  1070. png_size_t i;
  1071. png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
  1072. num_unknowns);
  1073. /* copy the locations from the read_info_ptr. The automatically
  1074. generated locations in write_end_info_ptr are wrong because we
  1075. haven't written the end_info yet */
  1076. for (i = 0; i < (png_size_t)num_unknowns; i++)
  1077. png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
  1078. unknowns[i].location);
  1079. }
  1080. }
  1081. #endif
  1082. #ifdef PNG_WRITE_SUPPORTED
  1083. png_write_end(write_ptr, write_end_info_ptr);
  1084. #endif
  1085. #ifdef PNG_EASY_ACCESS_SUPPORTED
  1086. if(verbose)
  1087. {
  1088. png_uint_32 iwidth, iheight;
  1089. iwidth = png_get_image_width(write_ptr, write_info_ptr);
  1090. iheight = png_get_image_height(write_ptr, write_info_ptr);
  1091. fprintf(STDERR, "Image width = %lu, height = %lu\n",
  1092. iwidth, iheight);
  1093. }
  1094. #endif
  1095. png_debug(0, "Destroying data structs\n");
  1096. #ifdef SINGLE_ROWBUF_ALLOC
  1097. png_debug(1, "destroying row_buf for read_ptr\n");
  1098. png_free(read_ptr, row_buf);
  1099. row_buf=NULL;
  1100. #endif /* SINGLE_ROWBUF_ALLOC */
  1101. png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr\n");
  1102. png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
  1103. #ifdef PNG_WRITE_SUPPORTED
  1104. png_debug(1, "destroying write_end_info_ptr\n");
  1105. png_destroy_info_struct(write_ptr, &write_end_info_ptr);
  1106. png_debug(1, "destroying write_ptr, write_info_ptr\n");
  1107. png_destroy_write_struct(&write_ptr, &write_info_ptr);
  1108. #endif
  1109. png_debug(0, "Destruction complete.\n");
  1110. FCLOSE(fpin);
  1111. FCLOSE(fpout);
  1112. png_debug(0, "Opening files for comparison\n");
  1113. #if defined(_WIN32_WCE)
  1114. MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
  1115. if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
  1116. #else
  1117. if ((fpin = fopen(inname, "rb")) == NULL)
  1118. #endif
  1119. {
  1120. fprintf(STDERR, "Could not find file %s\n", inname);
  1121. return (1);
  1122. }
  1123. #if defined(_WIN32_WCE)
  1124. MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
  1125. if ((fpout = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
  1126. #else
  1127. if ((fpout = fopen(outname, "rb")) == NULL)
  1128. #endif
  1129. {
  1130. fprintf(STDERR, "Could not find file %s\n", outname);
  1131. FCLOSE(fpin);
  1132. return (1);
  1133. }
  1134. for(;;)
  1135. {
  1136. png_size_t num_in, num_out;
  1137. READFILE(fpin, inbuf, 1, num_in);
  1138. READFILE(fpout, outbuf, 1, num_out);
  1139. if (num_in != num_out)
  1140. {
  1141. fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
  1142. inname, outname);
  1143. if(wrote_question == 0)
  1144. {
  1145. fprintf(STDERR,
  1146. " Was %s written with the same maximum IDAT chunk size (%d bytes),",
  1147. inname,PNG_ZBUF_SIZE);
  1148. fprintf(STDERR,
  1149. "\n filtering heuristic (libpng default), compression");
  1150. fprintf(STDERR,
  1151. " level (zlib default),\n and zlib version (%s)?\n\n",
  1152. ZLIB_VERSION);
  1153. wrote_question=1;
  1154. }
  1155. FCLOSE(fpin);
  1156. FCLOSE(fpout);
  1157. return (0);
  1158. }
  1159. if (!num_in)
  1160. break;
  1161. if (png_memcmp(inbuf, outbuf, num_in))
  1162. {
  1163. fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
  1164. if(wrote_question == 0)
  1165. {
  1166. fprintf(STDERR,
  1167. " Was %s written with the same maximum IDAT chunk size (%d bytes),",
  1168. inname,PNG_ZBUF_SIZE);
  1169. fprintf(STDERR,
  1170. "\n filtering heuristic (libpng default), compression");
  1171. fprintf(STDERR,
  1172. " level (zlib default),\n and zlib version (%s)?\n\n",
  1173. ZLIB_VERSION);
  1174. wrote_question=1;
  1175. }
  1176. FCLOSE(fpin);
  1177. FCLOSE(fpout);
  1178. return (0);
  1179. }
  1180. }
  1181. FCLOSE(fpin);
  1182. FCLOSE(fpout);
  1183. return (0);
  1184. }
  1185. /* input and output filenames */
  1186. #ifdef RISCOS
  1187. static PNG_CONST char *inname = "pngtest/png";
  1188. static PNG_CONST char *outname = "pngout/png";
  1189. #else
  1190. static PNG_CONST char *inname = "pngtest.png";
  1191. static PNG_CONST char *outname = "pngout.png";
  1192. #endif
  1193. int
  1194. main(int argc, char *argv[])
  1195. {
  1196. int multiple = 0;
  1197. int ierror = 0;
  1198. fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
  1199. fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
  1200. fprintf(STDERR,"%s",png_get_copyright(NULL));
  1201. /* Show the version of libpng used in building the library */
  1202. fprintf(STDERR," library (%lu):%s", png_access_version_number(),
  1203. png_get_header_version(NULL));
  1204. /* Show the version of libpng used in building the application */
  1205. fprintf(STDERR," pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
  1206. PNG_HEADER_VERSION_STRING);
  1207. fprintf(STDERR," png_sizeof(png_struct)=%ld, png_sizeof(png_info)=%ld\n",
  1208. (long)png_sizeof(png_struct), (long)png_sizeof(png_info));
  1209. /* Do some consistency checking on the memory allocation settings, I'm
  1210. not sure this matters, but it is nice to know, the first of these
  1211. tests should be impossible because of the way the macros are set
  1212. in pngconf.h */
  1213. #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
  1214. fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
  1215. #endif
  1216. /* I think the following can happen. */
  1217. #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
  1218. fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
  1219. #endif
  1220. if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
  1221. {
  1222. fprintf(STDERR,
  1223. "Warning: versions are different between png.h and png.c\n");
  1224. fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
  1225. fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
  1226. ++ierror;
  1227. }
  1228. if (argc > 1)
  1229. {
  1230. if (strcmp(argv[1], "-m") == 0)
  1231. {
  1232. multiple = 1;
  1233. status_dots_requested = 0;
  1234. }
  1235. else if (strcmp(argv[1], "-mv") == 0 ||
  1236. strcmp(argv[1], "-vm") == 0 )
  1237. {
  1238. multiple = 1;
  1239. verbose = 1;
  1240. status_dots_requested = 1;
  1241. }
  1242. else if (strcmp(argv[1], "-v") == 0)
  1243. {
  1244. verbose = 1;
  1245. status_dots_requested = 1;
  1246. inname = argv[2];
  1247. }
  1248. else
  1249. {
  1250. inname = argv[1];
  1251. status_dots_requested = 0;
  1252. }
  1253. }
  1254. if (!multiple && argc == 3+verbose)
  1255. outname = argv[2+verbose];
  1256. if ((!multiple && argc > 3+verbose) || (multiple && argc < 2))
  1257. {
  1258. fprintf(STDERR,
  1259. "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
  1260. argv[0], argv[0]);
  1261. fprintf(STDERR,
  1262. " reads/writes one PNG file (without -m) or multiple files (-m)\n");
  1263. fprintf(STDERR,
  1264. " with -m %s is used as a temporary file\n", outname);
  1265. exit(1);
  1266. }
  1267. if (multiple)
  1268. {
  1269. int i;
  1270. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1271. int allocation_now = current_allocation;
  1272. #endif
  1273. for (i=2; i<argc; ++i)
  1274. {
  1275. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1276. int k;
  1277. #endif
  1278. int kerror;
  1279. fprintf(STDERR, "Testing %s:",argv[i]);
  1280. kerror = test_one_file(argv[i], outname);
  1281. if (kerror == 0)
  1282. {
  1283. #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  1284. fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
  1285. #else
  1286. fprintf(STDERR, " PASS\n");
  1287. #endif
  1288. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1289. for (k=0; k<256; k++)
  1290. if(filters_used[k])
  1291. fprintf(STDERR, " Filter %d was used %lu times\n",
  1292. k,filters_used[k]);
  1293. #endif
  1294. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1295. if(tIME_chunk_present != 0)
  1296. fprintf(STDERR, " tIME = %s\n",tIME_string);
  1297. tIME_chunk_present = 0;
  1298. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1299. }
  1300. else
  1301. {
  1302. fprintf(STDERR, " FAIL\n");
  1303. ierror += kerror;
  1304. }
  1305. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1306. if (allocation_now != current_allocation)
  1307. fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
  1308. current_allocation-allocation_now);
  1309. if (current_allocation != 0)
  1310. {
  1311. memory_infop pinfo = pinformation;
  1312. fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
  1313. current_allocation);
  1314. while (pinfo != NULL)
  1315. {
  1316. fprintf(STDERR, " %lu bytes at %x\n", pinfo->size,
  1317. (unsigned int) pinfo->pointer);
  1318. pinfo = pinfo->next;
  1319. }
  1320. }
  1321. #endif
  1322. }
  1323. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1324. fprintf(STDERR, " Current memory allocation: %10d bytes\n",
  1325. current_allocation);
  1326. fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
  1327. maximum_allocation);
  1328. fprintf(STDERR, " Total memory allocation: %10d bytes\n",
  1329. total_allocation);
  1330. fprintf(STDERR, " Number of allocations: %10d\n",
  1331. num_allocations);
  1332. #endif
  1333. }
  1334. else
  1335. {
  1336. int i;
  1337. for (i=0; i<3; ++i)
  1338. {
  1339. int kerror;
  1340. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1341. int allocation_now = current_allocation;
  1342. #endif
  1343. if (i == 1) status_dots_requested = 1;
  1344. else if(verbose == 0)status_dots_requested = 0;
  1345. if (i == 0 || verbose == 1 || ierror != 0)
  1346. fprintf(STDERR, "Testing %s:",inname);
  1347. kerror = test_one_file(inname, outname);
  1348. if(kerror == 0)
  1349. {
  1350. if(verbose == 1 || i == 2)
  1351. {
  1352. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1353. int k;
  1354. #endif
  1355. #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  1356. fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
  1357. #else
  1358. fprintf(STDERR, " PASS\n");
  1359. #endif
  1360. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1361. for (k=0; k<256; k++)
  1362. if(filters_used[k])
  1363. fprintf(STDERR, " Filter %d was used %lu times\n",
  1364. k,filters_used[k]);
  1365. #endif
  1366. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1367. if(tIME_chunk_present != 0)
  1368. fprintf(STDERR, " tIME = %s\n",tIME_string);
  1369. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1370. }
  1371. }
  1372. else
  1373. {
  1374. if(verbose == 0 && i != 2)
  1375. fprintf(STDERR, "Testing %s:",inname);
  1376. fprintf(STDERR, " FAIL\n");
  1377. ierror += kerror;
  1378. }
  1379. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1380. if (allocation_now != current_allocation)
  1381. fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
  1382. current_allocation-allocation_now);
  1383. if (current_allocation != 0)
  1384. {
  1385. memory_infop pinfo = pinformation;
  1386. fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
  1387. current_allocation);
  1388. while (pinfo != NULL)
  1389. {
  1390. fprintf(STDERR," %lu bytes at %x\n",
  1391. pinfo->size, (unsigned int)pinfo->pointer);
  1392. pinfo = pinfo->next;
  1393. }
  1394. }
  1395. #endif
  1396. }
  1397. #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1398. fprintf(STDERR, " Current memory allocation: %10d bytes\n",
  1399. current_allocation);
  1400. fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
  1401. maximum_allocation);
  1402. fprintf(STDERR, " Total memory allocation: %10d bytes\n",
  1403. total_allocation);
  1404. fprintf(STDERR, " Number of allocations: %10d\n",
  1405. num_allocations);
  1406. #endif
  1407. }
  1408. #ifdef PNGTEST_TIMING
  1409. t_stop = (float)clock();
  1410. t_misc += (t_stop - t_start);
  1411. t_start = t_stop;
  1412. fprintf(STDERR," CPU time used = %.3f seconds",
  1413. (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
  1414. fprintf(STDERR," (decoding %.3f,\n",
  1415. t_decode/(float)CLOCKS_PER_SEC);
  1416. fprintf(STDERR," encoding %.3f ,",
  1417. t_encode/(float)CLOCKS_PER_SEC);
  1418. fprintf(STDERR," other %.3f seconds)\n\n",
  1419. t_misc/(float)CLOCKS_PER_SEC);
  1420. #endif
  1421. if (ierror == 0)
  1422. fprintf(STDERR, "libpng passes test\n");
  1423. else
  1424. fprintf(STDERR, "libpng FAILS test\n");
  1425. return (int)(ierror != 0);
  1426. }
  1427. /* Generate a compiler error if there is an old png.h in the search path. */
  1428. typedef version_1_2_8 your_png_h_is_not_version_1_2_8;