Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1787 lines
50 KiB

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