PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/compiler/android/jni/ftk/pngtest.c

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