src/3rdparty/libpng/pngtest.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 
     1 
     2 /* pngtest.c - a simple test program to test libpng
     2 /* pngtest.c - a simple test program to test libpng
     3  *
     3  *
     4  * Last changed in libpng 1.2.37 [June 4, 2009]
     4  * Last changed in libpng 1.4.0 [January 3, 2010]
     5  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
     5  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     8  *
     8  *
     9  * This code is released under the libpng license.
     9  * This code is released under the libpng license.
    10  * For conditions of distribution and use, see the disclaimer
    10  * For conditions of distribution and use, see the disclaimer
    30  * testing a wide variety of files easily.  You can also test a number
    30  * testing a wide variety of files easily.  You can also test a number
    31  * of files at once by typing "pngtest -m file1.png file2.png ..."
    31  * of files at once by typing "pngtest -m file1.png file2.png ..."
    32  */
    32  */
    33 
    33 
    34 #include "png.h"
    34 #include "png.h"
    35 
    35 #include "pngpriv.h"
    36 #if defined(_WIN32_WCE)
    36 
    37 #  if _WIN32_WCE < 211
       
    38      __error__ (f|w)printf functions are not supported on old WindowsCE.;
       
    39 #  endif
       
    40 #  include <windows.h>
       
    41 #  include <stdlib.h>
       
    42 #  define READFILE(file, data, length, check) \
       
    43      if (ReadFile(file, data, length, &check, NULL)) check = 0
       
    44 #  define WRITEFILE(file, data, length, check)) \
       
    45      if (WriteFile(file, data, length, &check, NULL)) check = 0
       
    46 #  define FCLOSE(file) CloseHandle(file)
       
    47 #else
       
    48 #  include <stdio.h>
    37 #  include <stdio.h>
    49 #  include <stdlib.h>
    38 #  include <stdlib.h>
    50 #  define READFILE(file, data, length, check) \
       
    51      check=(png_size_t)fread(data, (png_size_t)1, length, file)
       
    52 #  define WRITEFILE(file, data, length, check) \
       
    53      check=(png_size_t)fwrite(data, (png_size_t)1, length, file)
       
    54 #  define FCLOSE(file) fclose(file)
    39 #  define FCLOSE(file) fclose(file)
    55 #endif
    40 
    56 
    41 #ifndef PNG_STDIO_SUPPORTED
    57 #if defined(PNG_NO_STDIO)
       
    58 #  if defined(_WIN32_WCE)
       
    59      typedef HANDLE                png_FILE_p;
       
    60 #  else
       
    61      typedef FILE                * png_FILE_p;
    42      typedef FILE                * png_FILE_p;
    62 #  endif
       
    63 #endif
    43 #endif
    64 
    44 
    65 /* Makes pngtest verbose so we can find problems (needs to be before png.h) */
    45 /* Makes pngtest verbose so we can find problems (needs to be before png.h) */
    66 #ifndef PNG_DEBUG
    46 #ifndef PNG_DEBUG
    67 #  define PNG_DEBUG 0
    47 #  define PNG_DEBUG 0
    73 
    53 
    74 /* Turn on CPU timing
    54 /* Turn on CPU timing
    75 #define PNGTEST_TIMING
    55 #define PNGTEST_TIMING
    76 */
    56 */
    77 
    57 
    78 #ifdef PNG_NO_FLOATING_POINT_SUPPORTED
    58 #ifndef PNG_FLOATING_POINT_SUPPORTED
    79 #undef PNGTEST_TIMING
    59 #undef PNGTEST_TIMING
    80 #endif
    60 #endif
    81 
    61 
    82 #ifdef PNGTEST_TIMING
    62 #ifdef PNGTEST_TIMING
    83 static float t_start, t_stop, t_decode, t_encode, t_misc;
    63 static float t_start, t_stop, t_decode, t_encode, t_misc;
    84 #include <time.h>
    64 #include <time.h>
    85 #endif
    65 #endif
    86 
    66 
    87 #if defined(PNG_TIME_RFC1123_SUPPORTED)
    67 #ifdef PNG_TIME_RFC1123_SUPPORTED
    88 #define PNG_tIME_STRING_LENGTH 29
    68 #define PNG_tIME_STRING_LENGTH 29
    89 static int tIME_chunk_present = 0;
    69 static int tIME_chunk_present = 0;
    90 static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
    70 static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
    91 #endif
    71 #endif
    92 
    72 
   116 static int status_pass = 1;
    96 static int status_pass = 1;
   117 static int status_dots_requested = 0;
    97 static int status_dots_requested = 0;
   118 static int status_dots = 1;
    98 static int status_dots = 1;
   119 
    99 
   120 void
   100 void
   121 #ifdef PNG_1_0_X
       
   122 PNGAPI
       
   123 #endif
       
   124 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
   101 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
   125 void
   102 void
   126 #ifdef PNG_1_0_X
       
   127 PNGAPI
       
   128 #endif
       
   129 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
   103 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
   130 {
   104 {
   131    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
   105    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
   132       return;
   106       return;
   133    if (status_pass != pass)
   107    if (status_pass != pass)
   144    }
   118    }
   145    fprintf(stdout, "r");
   119    fprintf(stdout, "r");
   146 }
   120 }
   147 
   121 
   148 void
   122 void
   149 #ifdef PNG_1_0_X
       
   150 PNGAPI
       
   151 #endif
       
   152 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
   123 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
   153 void
   124 void
   154 #ifdef PNG_1_0_X
       
   155 PNGAPI
       
   156 #endif
       
   157 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
   125 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
   158 {
   126 {
   159    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
   127    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
   160       return;
   128       return;
   161    fprintf(stdout, "w");
   129    fprintf(stdout, "w");
   162 }
   130 }
   163 
   131 
   164 
   132 
   165 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   133 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
   166 /* Example of using user transform callback (we don't transform anything,
   134 /* Example of using user transform callback (we don't transform anything,
   167  * but merely examine the row filters.  We set this to 256 rather than
   135  * but merely examine the row filters.  We set this to 256 rather than
   168  * 5 in case illegal filter values are present.)
   136  * 5 in case illegal filter values are present.)
   169  */
   137  */
   170 static png_uint_32 filters_used[256];
   138 static png_uint_32 filters_used[256];
   171 void
   139 void
   172 #ifdef PNG_1_0_X
       
   173 PNGAPI
       
   174 #endif
       
   175 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
   140 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
   176 void
   141 void
   177 #ifdef PNG_1_0_X
       
   178 PNGAPI
       
   179 #endif
       
   180 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
   142 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
   181 {
   143 {
   182    if (png_ptr != NULL && row_info != NULL)
   144    if (png_ptr != NULL && row_info != NULL)
   183       ++filters_used[*(data - 1)];
   145       ++filters_used[*(data - 1)];
   184 }
   146 }
   185 #endif
   147 #endif
   186 
   148 
   187 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   149 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
   188 /* Example of using user transform callback (we don't transform anything,
   150 /* Example of using user transform callback (we don't transform anything,
   189  * but merely count the zero samples)
   151  * but merely count the zero samples)
   190  */
   152  */
   191 
   153 
   192 static png_uint_32 zero_samples;
   154 static png_uint_32 zero_samples;
   193 
   155 
   194 void
   156 void
   195 #ifdef PNG_1_0_X
       
   196 PNGAPI
       
   197 #endif
       
   198 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
   157 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
   199 void
   158 void
   200 #ifdef PNG_1_0_X
       
   201 PNGAPI
       
   202 #endif
       
   203 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
   159 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
   204 {
   160 {
   205    png_bytep dp = data;
   161    png_bytep dp = data;
   206    if (png_ptr == NULL)return;
   162    if (png_ptr == NULL)return;
   207 
   163 
   295 }
   251 }
   296 #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
   252 #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
   297 
   253 
   298 static int wrote_question = 0;
   254 static int wrote_question = 0;
   299 
   255 
   300 #if defined(PNG_NO_STDIO)
   256 #ifndef PNG_STDIO_SUPPORTED
   301 /* START of code to validate stdio-free compilation */
   257 /* START of code to validate stdio-free compilation */
   302 /* These copies of the default read/write functions come from pngrio.c and
   258 /* These copies of the default read/write functions come from pngrio.c and
   303  * pngwio.c.  They allow "don't include stdio" testing of the library.
   259  * pngwio.c.  They allow "don't include stdio" testing of the library.
   304  * This is the function that does the actual reading of data.  If you are
   260  * This is the function that does the actual reading of data.  If you are
   305  * not reading from a standard C stream, you should create a replacement
   261  * not reading from a standard C stream, you should create a replacement
   309 
   265 
   310 #ifndef USE_FAR_KEYWORD
   266 #ifndef USE_FAR_KEYWORD
   311 static void
   267 static void
   312 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   268 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   313 {
   269 {
   314    png_size_t check;
   270    png_size_t check = 0;
       
   271    png_voidp io_ptr;
   315 
   272 
   316    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
   273    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
   317     * instead of an int, which is what fread() actually returns.
   274     * instead of an int, which is what fread() actually returns.
   318     */
   275     */
   319    READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
   276    io_ptr = png_get_io_ptr(png_ptr);
       
   277    if (io_ptr != NULL)
       
   278    {
       
   279       check = fread(data, 1, length, (png_FILE_p)io_ptr);
       
   280    }
   320 
   281 
   321    if (check != length)
   282    if (check != length)
   322    {
   283    {
   323       png_error(png_ptr, "Read Error!");
   284       png_error(png_ptr, "Read Error!");
   324    }
   285    }
   333 #define MIN(a,b) (a <= b ? a : b)
   294 #define MIN(a,b) (a <= b ? a : b)
   334 
   295 
   335 static void
   296 static void
   336 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   297 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   337 {
   298 {
   338    int check;
   299    png_size_t check;
   339    png_byte *n_data;
   300    png_byte *n_data;
   340    png_FILE_p io_ptr;
   301    png_FILE_p io_ptr;
   341 
   302 
   342    /* Check if data really is near. If so, use usual code. */
   303    /* Check if data really is near. If so, use usual code. */
   343    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   304    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   344    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   305    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   345    if ((png_bytep)n_data == data)
   306    if ((png_bytep)n_data == data)
   346    {
   307    {
   347       READFILE(io_ptr, n_data, length, check);
   308       check = fread(n_data, 1, length, io_ptr);
   348    }
   309    }
   349    else
   310    else
   350    {
   311    {
   351       png_byte buf[NEAR_BUF_SIZE];
   312       png_byte buf[NEAR_BUF_SIZE];
   352       png_size_t read, remaining, err;
   313       png_size_t read, remaining, err;
   353       check = 0;
   314       check = 0;
   354       remaining = length;
   315       remaining = length;
   355       do
   316       do
   356       {
   317       {
   357          read = MIN(NEAR_BUF_SIZE, remaining);
   318          read = MIN(NEAR_BUF_SIZE, remaining);
   358          READFILE(io_ptr, buf, 1, err);
   319          err = fread(buf, 1, 1, io_ptr);
   359          png_memcpy(data, buf, read); /* Copy far buffer to near buffer */
   320          png_memcpy(data, buf, read); /* Copy far buffer to near buffer */
   360          if (err != read)
   321          if (err != read)
   361             break;
   322             break;
   362          else
   323          else
   363             check += err;
   324             check += err;
   369    if (check != length)
   330    if (check != length)
   370       png_error(png_ptr, "read Error");
   331       png_error(png_ptr, "read Error");
   371 }
   332 }
   372 #endif /* USE_FAR_KEYWORD */
   333 #endif /* USE_FAR_KEYWORD */
   373 
   334 
   374 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
   335 #ifdef PNG_WRITE_FLUSH_SUPPORTED
   375 static void
   336 static void
   376 pngtest_flush(png_structp png_ptr)
   337 pngtest_flush(png_structp png_ptr)
   377 {
   338 {
   378    /* Do nothing; fflush() is said to be just a waste of energy. */
   339    /* Do nothing; fflush() is said to be just a waste of energy. */
   379    png_ptr = png_ptr;  /* Stifle compiler warning */
   340    png_ptr = png_ptr;  /* Stifle compiler warning */
   387  */
   348  */
   388 #ifndef USE_FAR_KEYWORD
   349 #ifndef USE_FAR_KEYWORD
   389 static void
   350 static void
   390 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   351 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   391 {
   352 {
   392    png_uint_32 check;
   353    png_size_t check;
   393 
   354 
   394    WRITEFILE((png_FILE_p)png_ptr->io_ptr,  data, length, check);
   355    check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
   395    if (check != length)
   356    if (check != length)
   396    {
   357    {
   397       png_error(png_ptr, "Write Error");
   358       png_error(png_ptr, "Write Error");
   398    }
   359    }
   399 }
   360 }
   407 #define MIN(a,b) (a <= b ? a : b)
   368 #define MIN(a,b) (a <= b ? a : b)
   408 
   369 
   409 static void
   370 static void
   410 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   371 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   411 {
   372 {
   412    png_uint_32 check;
   373    png_size_t check;
   413    png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   374    png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   414    png_FILE_p io_ptr;
   375    png_FILE_p io_ptr;
   415 
   376 
   416    /* Check if data really is near. If so, use usual code. */
   377    /* Check if data really is near. If so, use usual code. */
   417    near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   378    near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   418    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   379    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   419    if ((png_bytep)near_data == data)
   380    if ((png_bytep)near_data == data)
   420    {
   381    {
   421       WRITEFILE(io_ptr, near_data, length, check);
   382       check = fwrite(near_data, 1, length, io_ptr);
   422    }
   383    }
   423    else
   384    else
   424    {
   385    {
   425       png_byte buf[NEAR_BUF_SIZE];
   386       png_byte buf[NEAR_BUF_SIZE];
   426       png_size_t written, remaining, err;
   387       png_size_t written, remaining, err;
   428       remaining = length;
   389       remaining = length;
   429       do
   390       do
   430       {
   391       {
   431          written = MIN(NEAR_BUF_SIZE, remaining);
   392          written = MIN(NEAR_BUF_SIZE, remaining);
   432          png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
   393          png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
   433          WRITEFILE(io_ptr, buf, written, err);
   394          err = fwrite(buf, 1, written, io_ptr);
   434          if (err != written)
   395          if (err != written)
   435             break;
   396             break;
   436          else
   397          else
   437             check += err;
   398             check += err;
   438          data += written;
   399          data += written;
   454  */
   415  */
   455 static void
   416 static void
   456 pngtest_warning(png_structp png_ptr, png_const_charp message)
   417 pngtest_warning(png_structp png_ptr, png_const_charp message)
   457 {
   418 {
   458    PNG_CONST char *name = "UNKNOWN (ERROR!)";
   419    PNG_CONST char *name = "UNKNOWN (ERROR!)";
   459    if (png_ptr != NULL && png_ptr->error_ptr != NULL)
   420    char *test;
   460       name = png_ptr->error_ptr;
   421    test = png_get_error_ptr(png_ptr);
   461    fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
   422    if (test == NULL)
       
   423      fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
       
   424    else
       
   425      fprintf(STDERR, "%s: libpng warning: %s\n", test, message);
   462 }
   426 }
   463 
   427 
   464 /* This is the default error handling function.  Note that replacements for
   428 /* This is the default error handling function.  Note that replacements for
   465  * this function MUST NOT RETURN, or the program will likely crash.  This
   429  * this function MUST NOT RETURN, or the program will likely crash.  This
   466  * function is used by default, or if the program supplies NULL for the
   430  * function is used by default, or if the program supplies NULL for the
   472    pngtest_warning(png_ptr, message);
   436    pngtest_warning(png_ptr, message);
   473    /* We can return because png_error calls the default handler, which is
   437    /* We can return because png_error calls the default handler, which is
   474     * actually OK in this case.
   438     * actually OK in this case.
   475     */
   439     */
   476 }
   440 }
   477 #endif /* PNG_NO_STDIO */
   441 #endif /* !PNG_STDIO_SUPPORTED */
   478 /* END of code to validate stdio-free compilation */
   442 /* END of code to validate stdio-free compilation */
   479 
   443 
   480 /* START of code to validate memory allocation and deallocation */
   444 /* START of code to validate memory allocation and deallocation */
   481 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   445 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   482 
   446 
   489  * This piece of code can be compiled to validate max 64K allocations
   453  * This piece of code can be compiled to validate max 64K allocations
   490  * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
   454  * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
   491  */
   455  */
   492 typedef struct memory_information
   456 typedef struct memory_information
   493 {
   457 {
   494    png_uint_32               size;
   458    png_alloc_size_t          size;
   495    png_voidp                 pointer;
   459    png_voidp                 pointer;
   496    struct memory_information FAR *next;
   460    struct memory_information FAR *next;
   497 } memory_information;
   461 } memory_information;
   498 typedef memory_information FAR *memory_infop;
   462 typedef memory_information FAR *memory_infop;
   499 
   463 
   501 static int current_allocation = 0;
   465 static int current_allocation = 0;
   502 static int maximum_allocation = 0;
   466 static int maximum_allocation = 0;
   503 static int total_allocation = 0;
   467 static int total_allocation = 0;
   504 static int num_allocations = 0;
   468 static int num_allocations = 0;
   505 
   469 
   506 png_voidp png_debug_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
   470 png_voidp png_debug_malloc
       
   471    PNGARG((png_structp png_ptr, png_alloc_size_t size));
   507 void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
   472 void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
   508 
   473 
   509 png_voidp
   474 png_voidp
   510 png_debug_malloc(png_structp png_ptr, png_uint_32 size)
   475 png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
   511 {
   476 {
   512 
   477 
   513    /* png_malloc has already tested for NULL; png_create_struct calls
   478    /* png_malloc has already tested for NULL; png_create_struct calls
   514     * png_debug_malloc directly, with png_ptr == NULL which is OK
   479     * png_debug_malloc directly, with png_ptr == NULL which is OK
   515     */
   480     */
   522    {
   487    {
   523       /* Disable malloc_fn and free_fn */
   488       /* Disable malloc_fn and free_fn */
   524       memory_infop pinfo;
   489       memory_infop pinfo;
   525       png_set_mem_fn(png_ptr, NULL, NULL, NULL);
   490       png_set_mem_fn(png_ptr, NULL, NULL, NULL);
   526       pinfo = (memory_infop)png_malloc(png_ptr,
   491       pinfo = (memory_infop)png_malloc(png_ptr,
   527          (png_uint_32)png_sizeof(*pinfo));
   492          png_sizeof(*pinfo));
   528       pinfo->size = size;
   493       pinfo->size = size;
   529       current_allocation += size;
   494       current_allocation += size;
   530       total_allocation += size;
   495       total_allocation += size;
   531       num_allocations ++;
   496       num_allocations ++;
   532       if (current_allocation > maximum_allocation)
   497       if (current_allocation > maximum_allocation)
   533          maximum_allocation = current_allocation;
   498          maximum_allocation = current_allocation;
   534       pinfo->pointer = (png_voidp)png_malloc(png_ptr, size);
   499       pinfo->pointer = png_malloc(png_ptr, size);
   535       /* Restore malloc_fn and free_fn */
   500       /* Restore malloc_fn and free_fn */
   536       png_set_mem_fn(png_ptr,
   501       png_set_mem_fn(png_ptr,
   537           png_voidp_NULL, (png_malloc_ptr)png_debug_malloc,
   502           NULL, png_debug_malloc, png_debug_free);
   538           (png_free_ptr)png_debug_free);
       
   539       if (size != 0 && pinfo->pointer == NULL)
   503       if (size != 0 && pinfo->pointer == NULL)
   540       {
   504       {
   541          current_allocation -= size;
   505          current_allocation -= size;
   542          total_allocation -= size;
   506          total_allocation -= size;
   543          png_error(png_ptr,
   507          png_error(png_ptr,
   544            "out of memory in pngtest->png_debug_malloc.");
   508            "out of memory in pngtest->png_debug_malloc");
   545       }
   509       }
   546       pinfo->next = pinformation;
   510       pinfo->next = pinformation;
   547       pinformation = pinfo;
   511       pinformation = pinfo;
   548       /* Make sure the caller isn't assuming zeroed memory. */
   512       /* Make sure the caller isn't assuming zeroed memory. */
   549       png_memset(pinfo->pointer, 0xdd, pinfo->size);
   513       png_memset(pinfo->pointer, 0xdd, pinfo->size);
   605 #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
   569 #endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
   606 /* END of code to test memory allocation/deallocation */
   570 /* END of code to test memory allocation/deallocation */
   607 
   571 
   608 
   572 
   609 /* Demonstration of user chunk support of the sTER and vpAg chunks */
   573 /* Demonstration of user chunk support of the sTER and vpAg chunks */
   610 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   574 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
   611 
   575 
   612 /* (sTER is a public chunk not yet known by libpng.  vpAg is a private
   576 /* (sTER is a public chunk not yet known by libpng.  vpAg is a private
   613 chunk used in ImageMagick to store "virtual page" size).  */
   577 chunk used in ImageMagick to store "virtual page" size).  */
   614 
   578 
   615 static png_uint_32 user_chunk_data[4];
   579 static png_uint_32 user_chunk_data[4];
   699 #ifdef USE_FAR_KEYWORD
   663 #ifdef USE_FAR_KEYWORD
   700    jmp_buf jmpbuf;
   664    jmp_buf jmpbuf;
   701 #endif
   665 #endif
   702 #endif
   666 #endif
   703 
   667 
   704 #if defined(_WIN32_WCE)
       
   705    TCHAR path[MAX_PATH];
       
   706 #endif
       
   707    char inbuf[256], outbuf[256];
   668    char inbuf[256], outbuf[256];
   708 
   669 
   709    row_buf = NULL;
   670    row_buf = NULL;
   710 
   671 
   711 #if defined(_WIN32_WCE)
       
   712    MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
       
   713    if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
       
   714 #else
       
   715    if ((fpin = fopen(inname, "rb")) == NULL)
   672    if ((fpin = fopen(inname, "rb")) == NULL)
   716 #endif
       
   717    {
   673    {
   718       fprintf(STDERR, "Could not find input file %s\n", inname);
   674       fprintf(STDERR, "Could not find input file %s\n", inname);
   719       return (1);
   675       return (1);
   720    }
   676    }
   721 
   677 
   722 #if defined(_WIN32_WCE)
       
   723    MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
       
   724    if ((fpout = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
       
   725 #else
       
   726    if ((fpout = fopen(outname, "wb")) == NULL)
   678    if ((fpout = fopen(outname, "wb")) == NULL)
   727 #endif
       
   728    {
   679    {
   729       fprintf(STDERR, "Could not open output file %s\n", outname);
   680       fprintf(STDERR, "Could not open output file %s\n", outname);
   730       FCLOSE(fpin);
   681       FCLOSE(fpin);
   731       return (1);
   682       return (1);
   732    }
   683    }
   733 
   684 
   734    png_debug(0, "Allocating read and write structures");
   685    png_debug(0, "Allocating read and write structures");
   735 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   686 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   736    read_ptr =
   687    read_ptr =
   737       png_create_read_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
   688       png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
   738       png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
   689       NULL, NULL, NULL,
   739       (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
   690       (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
   740 #else
   691 #else
   741    read_ptr =
   692    read_ptr =
   742       png_create_read_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
   693       png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   743       png_error_ptr_NULL, png_error_ptr_NULL);
   694 #endif
   744 #endif
   695 #ifndef PNG_STDIO_SUPPORTED
   745 #if defined(PNG_NO_STDIO)
       
   746    png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
   696    png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
   747        pngtest_warning);
   697        pngtest_warning);
   748 #endif
   698 #endif
   749 
   699 
   750 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   700 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
   751    user_chunk_data[0] = 0;
   701    user_chunk_data[0] = 0;
   752    user_chunk_data[1] = 0;
   702    user_chunk_data[1] = 0;
   753    user_chunk_data[2] = 0;
   703    user_chunk_data[2] = 0;
   754    user_chunk_data[3] = 0;
   704    user_chunk_data[3] = 0;
   755    png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
   705    png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
   757 
   707 
   758 #endif
   708 #endif
   759 #ifdef PNG_WRITE_SUPPORTED
   709 #ifdef PNG_WRITE_SUPPORTED
   760 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   710 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
   761    write_ptr =
   711    write_ptr =
   762       png_create_write_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
   712       png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
   763       png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
   713       NULL, NULL, NULL, png_debug_malloc, png_debug_free);
   764       (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
       
   765 #else
   714 #else
   766    write_ptr =
   715    write_ptr =
   767       png_create_write_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
   716       png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   768       png_error_ptr_NULL, png_error_ptr_NULL);
   717 #endif
   769 #endif
   718 #ifndef PNG_STDIO_SUPPORTED
   770 #if defined(PNG_NO_STDIO)
       
   771    png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
   719    png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
   772        pngtest_warning);
   720        pngtest_warning);
   773 #endif
   721 #endif
   774 #endif
   722 #endif
   775    png_debug(0, "Allocating read_info, write_info and end_info structures");
   723    png_debug(0, "Allocating read_info, write_info and end_info structures");
   827 #endif
   775 #endif
   828 #endif
   776 #endif
   829 #endif
   777 #endif
   830 
   778 
   831    png_debug(0, "Initializing input and output streams");
   779    png_debug(0, "Initializing input and output streams");
   832 #if !defined(PNG_NO_STDIO)
   780 #ifdef PNG_STDIO_SUPPORTED
   833    png_init_io(read_ptr, fpin);
   781    png_init_io(read_ptr, fpin);
   834 #  ifdef PNG_WRITE_SUPPORTED
   782 #  ifdef PNG_WRITE_SUPPORTED
   835    png_init_io(write_ptr, fpout);
   783    png_init_io(write_ptr, fpout);
   836 #  endif
   784 #  endif
   837 #else
   785 #else
   838    png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
   786    png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
   839 #  ifdef PNG_WRITE_SUPPORTED
   787 #  ifdef PNG_WRITE_SUPPORTED
   840    png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
   788    png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
   841 #    if defined(PNG_WRITE_FLUSH_SUPPORTED)
   789 #    ifdef PNG_WRITE_FLUSH_SUPPORTED
   842       pngtest_flush);
   790       pngtest_flush);
   843 #    else
   791 #    else
   844       NULL);
   792       NULL);
   845 #    endif
   793 #    endif
   846 #  endif
   794 #  endif
   853       png_set_read_status_fn(read_ptr, read_row_callback);
   801       png_set_read_status_fn(read_ptr, read_row_callback);
   854    }
   802    }
   855    else
   803    else
   856    {
   804    {
   857 #ifdef PNG_WRITE_SUPPORTED
   805 #ifdef PNG_WRITE_SUPPORTED
   858       png_set_write_status_fn(write_ptr, png_write_status_ptr_NULL);
   806       png_set_write_status_fn(write_ptr, NULL);
   859 #endif
   807 #endif
   860       png_set_read_status_fn(read_ptr, png_read_status_ptr_NULL);
   808       png_set_read_status_fn(read_ptr, NULL);
   861    }
   809    }
   862 
   810 
   863 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   811 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
   864    {
   812    {
   865       int i;
   813       int i;
   866       for (i = 0; i<256; i++)
   814       for (i = 0; i<256; i++)
   867          filters_used[i] = 0;
   815          filters_used[i] = 0;
   868       png_set_read_user_transform_fn(read_ptr, count_filters);
   816       png_set_read_user_transform_fn(read_ptr, count_filters);
   869    }
   817    }
   870 #endif
   818 #endif
   871 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   819 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
   872    zero_samples = 0;
   820    zero_samples = 0;
   873    png_set_write_user_transform_fn(write_ptr, count_zero_samples);
   821    png_set_write_user_transform_fn(write_ptr, count_zero_samples);
   874 #endif
   822 #endif
   875 
   823 
   876 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
   824 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
   877 #  ifndef PNG_HANDLE_CHUNK_ALWAYS
   825 #  ifndef PNG_HANDLE_CHUNK_ALWAYS
   878 #    define PNG_HANDLE_CHUNK_ALWAYS       3
   826 #    define PNG_HANDLE_CHUNK_ALWAYS       3
   879 #  endif
   827 #  endif
   880    png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
   828    png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
   881       png_bytep_NULL, 0);
   829       NULL, 0);
   882 #endif
   830 #endif
   883 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
   831 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
   884 #  ifndef PNG_HANDLE_CHUNK_IF_SAFE
   832 #  ifndef PNG_HANDLE_CHUNK_IF_SAFE
   885 #    define PNG_HANDLE_CHUNK_IF_SAFE      2
   833 #    define PNG_HANDLE_CHUNK_IF_SAFE      2
   886 #  endif
   834 #  endif
   887    png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
   835    png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
   888       png_bytep_NULL, 0);
   836       NULL, 0);
   889 #endif
   837 #endif
   890 
   838 
   891    png_debug(0, "Reading info struct");
   839    png_debug(0, "Reading info struct");
   892    png_read_info(read_ptr, read_info_ptr);
   840    png_read_info(read_ptr, read_info_ptr);
   893 
   841 
   897 
   845 
   898       if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
   846       if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
   899           &color_type, &interlace_type, &compression_type, &filter_type))
   847           &color_type, &interlace_type, &compression_type, &filter_type))
   900       {
   848       {
   901          png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
   849          png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
   902 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   850 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
   903             color_type, interlace_type, compression_type, filter_type);
   851             color_type, interlace_type, compression_type, filter_type);
   904 #else
   852 #else
   905             color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
   853             color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
   906 #endif
   854 #endif
   907       }
   855       }
   908    }
   856    }
   909 #if defined(PNG_FIXED_POINT_SUPPORTED)
   857 #ifdef PNG_FIXED_POINT_SUPPORTED
   910 #if defined(PNG_cHRM_SUPPORTED)
   858 #ifdef PNG_cHRM_SUPPORTED
   911    {
   859    {
   912       png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
   860       png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
   913          blue_y;
   861          blue_y;
   914       if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
   862       if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
   915          &red_y, &green_x, &green_y, &blue_x, &blue_y))
   863          &red_y, &green_x, &green_y, &blue_x, &blue_y))
   917          png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
   865          png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
   918             red_y, green_x, green_y, blue_x, blue_y);
   866             red_y, green_x, green_y, blue_x, blue_y);
   919       }
   867       }
   920    }
   868    }
   921 #endif
   869 #endif
   922 #if defined(PNG_gAMA_SUPPORTED)
   870 #ifdef PNG_gAMA_SUPPORTED
   923    {
   871    {
   924       png_fixed_point gamma;
   872       png_fixed_point gamma;
   925 
   873 
   926       if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
   874       if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
   927          png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
   875          png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
   928    }
   876    }
   929 #endif
   877 #endif
   930 #else /* Use floating point versions */
   878 #else /* Use floating point versions */
   931 #if defined(PNG_FLOATING_POINT_SUPPORTED)
   879 #ifdef PNG_FLOATING_POINT_SUPPORTED
   932 #if defined(PNG_cHRM_SUPPORTED)
   880 #ifdef PNG_cHRM_SUPPORTED
   933    {
   881    {
   934       double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
   882       double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
   935          blue_y;
   883          blue_y;
   936       if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
   884       if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
   937          &red_y, &green_x, &green_y, &blue_x, &blue_y))
   885          &red_y, &green_x, &green_y, &blue_x, &blue_y))
   939          png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
   887          png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
   940             red_y, green_x, green_y, blue_x, blue_y);
   888             red_y, green_x, green_y, blue_x, blue_y);
   941       }
   889       }
   942    }
   890    }
   943 #endif
   891 #endif
   944 #if defined(PNG_gAMA_SUPPORTED)
   892 #ifdef PNG_gAMA_SUPPORTED
   945    {
   893    {
   946       double gamma;
   894       double gamma;
   947 
   895 
   948       if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
   896       if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
   949          png_set_gAMA(write_ptr, write_info_ptr, gamma);
   897          png_set_gAMA(write_ptr, write_info_ptr, gamma);
   950    }
   898    }
   951 #endif
   899 #endif
   952 #endif /* Floating point */
   900 #endif /* Floating point */
   953 #endif /* Fixed point */
   901 #endif /* Fixed point */
   954 #if defined(PNG_iCCP_SUPPORTED)
   902 #ifdef PNG_iCCP_SUPPORTED
   955    {
   903    {
   956       png_charp name;
   904       png_charp name;
   957       png_charp profile;
   905       png_charp profile;
   958       png_uint_32 proflen;
   906       png_uint_32 proflen;
   959       int compression_type;
   907       int compression_type;
   964          png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
   912          png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
   965                       profile, proflen);
   913                       profile, proflen);
   966       }
   914       }
   967    }
   915    }
   968 #endif
   916 #endif
   969 #if defined(PNG_sRGB_SUPPORTED)
   917 #ifdef PNG_sRGB_SUPPORTED
   970    {
   918    {
   971       int intent;
   919       int intent;
   972 
   920 
   973       if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
   921       if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
   974          png_set_sRGB(write_ptr, write_info_ptr, intent);
   922          png_set_sRGB(write_ptr, write_info_ptr, intent);
   979       int num_palette;
   927       int num_palette;
   980 
   928 
   981       if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
   929       if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
   982          png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
   930          png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
   983    }
   931    }
   984 #if defined(PNG_bKGD_SUPPORTED)
   932 #ifdef PNG_bKGD_SUPPORTED
   985    {
   933    {
   986       png_color_16p background;
   934       png_color_16p background;
   987 
   935 
   988       if (png_get_bKGD(read_ptr, read_info_ptr, &background))
   936       if (png_get_bKGD(read_ptr, read_info_ptr, &background))
   989       {
   937       {
   990          png_set_bKGD(write_ptr, write_info_ptr, background);
   938          png_set_bKGD(write_ptr, write_info_ptr, background);
   991       }
   939       }
   992    }
   940    }
   993 #endif
   941 #endif
   994 #if defined(PNG_hIST_SUPPORTED)
   942 #ifdef PNG_hIST_SUPPORTED
   995    {
   943    {
   996       png_uint_16p hist;
   944       png_uint_16p hist;
   997 
   945 
   998       if (png_get_hIST(read_ptr, read_info_ptr, &hist))
   946       if (png_get_hIST(read_ptr, read_info_ptr, &hist))
   999          png_set_hIST(write_ptr, write_info_ptr, hist);
   947          png_set_hIST(write_ptr, write_info_ptr, hist);
  1000    }
   948    }
  1001 #endif
   949 #endif
  1002 #if defined(PNG_oFFs_SUPPORTED)
   950 #ifdef PNG_oFFs_SUPPORTED
  1003    {
   951    {
  1004       png_int_32 offset_x, offset_y;
   952       png_int_32 offset_x, offset_y;
  1005       int unit_type;
   953       int unit_type;
  1006 
   954 
  1007       if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
   955       if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
  1009       {
   957       {
  1010          png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
   958          png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
  1011       }
   959       }
  1012    }
   960    }
  1013 #endif
   961 #endif
  1014 #if defined(PNG_pCAL_SUPPORTED)
   962 #ifdef PNG_pCAL_SUPPORTED
  1015    {
   963    {
  1016       png_charp purpose, units;
   964       png_charp purpose, units;
  1017       png_charpp params;
   965       png_charpp params;
  1018       png_int_32 X0, X1;
   966       png_int_32 X0, X1;
  1019       int type, nparams;
   967       int type, nparams;
  1024          png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
   972          png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
  1025             nparams, units, params);
   973             nparams, units, params);
  1026       }
   974       }
  1027    }
   975    }
  1028 #endif
   976 #endif
  1029 #if defined(PNG_pHYs_SUPPORTED)
   977 #ifdef PNG_pHYs_SUPPORTED
  1030    {
   978    {
  1031       png_uint_32 res_x, res_y;
   979       png_uint_32 res_x, res_y;
  1032       int unit_type;
   980       int unit_type;
  1033 
   981 
  1034       if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
   982       if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
  1035          png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
   983          png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
  1036    }
   984    }
  1037 #endif
   985 #endif
  1038 #if defined(PNG_sBIT_SUPPORTED)
   986 #ifdef PNG_sBIT_SUPPORTED
  1039    {
   987    {
  1040       png_color_8p sig_bit;
   988       png_color_8p sig_bit;
  1041 
   989 
  1042       if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
   990       if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
  1043          png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
   991          png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
  1044    }
   992    }
  1045 #endif
   993 #endif
  1046 #if defined(PNG_sCAL_SUPPORTED)
   994 #ifdef PNG_sCAL_SUPPORTED
  1047 #ifdef PNG_FLOATING_POINT_SUPPORTED
   995 #ifdef PNG_FLOATING_POINT_SUPPORTED
  1048    {
   996    {
  1049       int unit;
   997       int unit;
  1050       double scal_width, scal_height;
   998       double scal_width, scal_height;
  1051 
   999 
  1068       }
  1016       }
  1069    }
  1017    }
  1070 #endif
  1018 #endif
  1071 #endif
  1019 #endif
  1072 #endif
  1020 #endif
  1073 #if defined(PNG_TEXT_SUPPORTED)
  1021 #ifdef PNG_TEXT_SUPPORTED
  1074    {
  1022    {
  1075       png_textp text_ptr;
  1023       png_textp text_ptr;
  1076       int num_text;
  1024       int num_text;
  1077 
  1025 
  1078       if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
  1026       if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
  1080          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1028          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1081          png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
  1029          png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
  1082       }
  1030       }
  1083    }
  1031    }
  1084 #endif
  1032 #endif
  1085 #if defined(PNG_tIME_SUPPORTED)
  1033 #ifdef PNG_tIME_SUPPORTED
  1086    {
  1034    {
  1087       png_timep mod_time;
  1035       png_timep mod_time;
  1088 
  1036 
  1089       if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
  1037       if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
  1090       {
  1038       {
  1091          png_set_tIME(write_ptr, write_info_ptr, mod_time);
  1039          png_set_tIME(write_ptr, write_info_ptr, mod_time);
  1092 #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1040 #ifdef PNG_TIME_RFC1123_SUPPORTED
  1093          /* We have to use png_memcpy instead of "=" because the string
  1041          /* We have to use png_memcpy instead of "=" because the string
  1094           * pointed to by png_convert_to_rfc1123() gets free'ed before
  1042           * pointed to by png_convert_to_rfc1123() gets free'ed before
  1095           * we use it.
  1043           * we use it.
  1096           */
  1044           */
  1097          png_memcpy(tIME_string,
  1045          png_memcpy(tIME_string,
  1101          tIME_chunk_present++;
  1049          tIME_chunk_present++;
  1102 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1050 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1103       }
  1051       }
  1104    }
  1052    }
  1105 #endif
  1053 #endif
  1106 #if defined(PNG_tRNS_SUPPORTED)
  1054 #ifdef PNG_tRNS_SUPPORTED
  1107    {
  1055    {
  1108       png_bytep trans;
  1056       png_bytep trans_alpha;
  1109       int num_trans;
  1057       int num_trans;
  1110       png_color_16p trans_values;
  1058       png_color_16p trans_color;
  1111 
  1059 
  1112       if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
  1060       if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
  1113          &trans_values))
  1061          &trans_color))
  1114       {
  1062       {
  1115          int sample_max = (1 << read_info_ptr->bit_depth);
  1063          int sample_max = (1 << bit_depth);
  1116          /* libpng doesn't reject a tRNS chunk with out-of-range samples */
  1064          /* libpng doesn't reject a tRNS chunk with out-of-range samples */
  1117          if (!((read_info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  1065          if (!((color_type == PNG_COLOR_TYPE_GRAY &&
  1118              (int)trans_values->gray > sample_max) ||
  1066              (int)trans_color->gray > sample_max) ||
  1119              (read_info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  1067              (color_type == PNG_COLOR_TYPE_RGB &&
  1120              ((int)trans_values->red > sample_max ||
  1068              ((int)trans_color->red > sample_max ||
  1121              (int)trans_values->green > sample_max ||
  1069              (int)trans_color->green > sample_max ||
  1122              (int)trans_values->blue > sample_max))))
  1070              (int)trans_color->blue > sample_max))))
  1123             png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
  1071             png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
  1124                trans_values);
  1072                trans_color);
  1125       }
  1073       }
  1126    }
  1074    }
  1127 #endif
  1075 #endif
  1128 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  1076 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1129    {
  1077    {
  1130       png_unknown_chunkp unknowns;
  1078       png_unknown_chunkp unknowns;
  1131       int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
  1079       int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
  1132          &unknowns);
  1080          &unknowns);
  1133       if (num_unknowns)
  1081       if (num_unknowns)
  1152 /* If we wanted, we could write info in two steps:
  1100 /* If we wanted, we could write info in two steps:
  1153  * png_write_info_before_PLTE(write_ptr, write_info_ptr);
  1101  * png_write_info_before_PLTE(write_ptr, write_info_ptr);
  1154  */
  1102  */
  1155    png_write_info(write_ptr, write_info_ptr);
  1103    png_write_info(write_ptr, write_info_ptr);
  1156 
  1104 
  1157 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  1105 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  1158    if (user_chunk_data[0] != 0)
  1106    if (user_chunk_data[0] != 0)
  1159    {
  1107    {
  1160       png_byte png_sTER[5] = {115,  84,  69,  82, '\0'};
  1108       png_byte png_sTER[5] = {115,  84,  69,  82, '\0'};
  1161 
  1109 
  1162       unsigned char
  1110       unsigned char
  1222          row_buf = (png_bytep)png_malloc(read_ptr,
  1170          row_buf = (png_bytep)png_malloc(read_ptr,
  1223             png_get_rowbytes(read_ptr, read_info_ptr));
  1171             png_get_rowbytes(read_ptr, read_info_ptr));
  1224          png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf,
  1172          png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf,
  1225             png_get_rowbytes(read_ptr, read_info_ptr));
  1173             png_get_rowbytes(read_ptr, read_info_ptr));
  1226 #endif /* !SINGLE_ROWBUF_ALLOC */
  1174 #endif /* !SINGLE_ROWBUF_ALLOC */
  1227          png_read_rows(read_ptr, (png_bytepp)&row_buf, png_bytepp_NULL, 1);
  1175          png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
  1228 
  1176 
  1229 #ifdef PNG_WRITE_SUPPORTED
  1177 #ifdef PNG_WRITE_SUPPORTED
  1230 #ifdef PNGTEST_TIMING
  1178 #ifdef PNGTEST_TIMING
  1231          t_stop = (float)clock();
  1179          t_stop = (float)clock();
  1232          t_decode += (t_stop - t_start);
  1180          t_decode += (t_stop - t_start);
  1246          row_buf = NULL;
  1194          row_buf = NULL;
  1247 #endif /* !SINGLE_ROWBUF_ALLOC */
  1195 #endif /* !SINGLE_ROWBUF_ALLOC */
  1248       }
  1196       }
  1249    }
  1197    }
  1250 
  1198 
  1251 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  1199 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  1252    png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
  1200    png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
  1253 #endif
  1201 #endif
  1254 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  1202 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1255    png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
  1203    png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
  1256 #endif
  1204 #endif
  1257 
  1205 
  1258    png_debug(0, "Reading and writing end_info data");
  1206    png_debug(0, "Reading and writing end_info data");
  1259 
  1207 
  1260    png_read_end(read_ptr, end_info_ptr);
  1208    png_read_end(read_ptr, end_info_ptr);
  1261 #if defined(PNG_TEXT_SUPPORTED)
  1209 #ifdef PNG_TEXT_SUPPORTED
  1262    {
  1210    {
  1263       png_textp text_ptr;
  1211       png_textp text_ptr;
  1264       int num_text;
  1212       int num_text;
  1265 
  1213 
  1266       if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
  1214       if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
  1268          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1216          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);
  1269          png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
  1217          png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
  1270       }
  1218       }
  1271    }
  1219    }
  1272 #endif
  1220 #endif
  1273 #if defined(PNG_tIME_SUPPORTED)
  1221 #ifdef PNG_tIME_SUPPORTED
  1274    {
  1222    {
  1275       png_timep mod_time;
  1223       png_timep mod_time;
  1276 
  1224 
  1277       if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
  1225       if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
  1278       {
  1226       {
  1279          png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
  1227          png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
  1280 #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1228 #ifdef PNG_TIME_RFC1123_SUPPORTED
  1281          /* We have to use png_memcpy instead of "=" because the string
  1229          /* We have to use png_memcpy instead of "=" because the string
  1282             pointed to by png_convert_to_rfc1123() gets free'ed before
  1230             pointed to by png_convert_to_rfc1123() gets free'ed before
  1283             we use it */
  1231             we use it */
  1284          png_memcpy(tIME_string,
  1232          png_memcpy(tIME_string,
  1285                     png_convert_to_rfc1123(read_ptr, mod_time),
  1233                     png_convert_to_rfc1123(read_ptr, mod_time),
  1288          tIME_chunk_present++;
  1236          tIME_chunk_present++;
  1289 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1237 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1290       }
  1238       }
  1291    }
  1239    }
  1292 #endif
  1240 #endif
  1293 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  1241 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  1294    {
  1242    {
  1295       png_unknown_chunkp unknowns;
  1243       png_unknown_chunkp unknowns;
  1296       int num_unknowns;
  1244       int num_unknowns;
  1297       num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
  1245       num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
  1298          &unknowns);
  1246          &unknowns);
  1344 
  1292 
  1345    FCLOSE(fpin);
  1293    FCLOSE(fpin);
  1346    FCLOSE(fpout);
  1294    FCLOSE(fpout);
  1347 
  1295 
  1348    png_debug(0, "Opening files for comparison");
  1296    png_debug(0, "Opening files for comparison");
  1349 #if defined(_WIN32_WCE)
       
  1350    MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
       
  1351    if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
       
  1352 #else
       
  1353    if ((fpin = fopen(inname, "rb")) == NULL)
  1297    if ((fpin = fopen(inname, "rb")) == NULL)
  1354 #endif
       
  1355    {
  1298    {
  1356       fprintf(STDERR, "Could not find file %s\n", inname);
  1299       fprintf(STDERR, "Could not find file %s\n", inname);
  1357       return (1);
  1300       return (1);
  1358    }
  1301    }
  1359 
  1302 
  1360 #if defined(_WIN32_WCE)
       
  1361    MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
       
  1362    if ((fpout = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
       
  1363 #else
       
  1364    if ((fpout = fopen(outname, "rb")) == NULL)
  1303    if ((fpout = fopen(outname, "rb")) == NULL)
  1365 #endif
       
  1366    {
  1304    {
  1367       fprintf(STDERR, "Could not find file %s\n", outname);
  1305       fprintf(STDERR, "Could not find file %s\n", outname);
  1368       FCLOSE(fpin);
  1306       FCLOSE(fpin);
  1369       return (1);
  1307       return (1);
  1370    }
  1308    }
  1371 
  1309 
  1372    for (;;)
  1310    for (;;)
  1373    {
  1311    {
  1374       png_size_t num_in, num_out;
  1312       png_size_t num_in, num_out;
  1375 
  1313 
  1376          READFILE(fpin, inbuf, 1, num_in);
  1314          num_in = fread(inbuf, 1, 1, fpin);
  1377          READFILE(fpout, outbuf, 1, num_out);
  1315          num_out = fread(outbuf, 1, 1, fpout);
  1378 
  1316 
  1379       if (num_in != num_out)
  1317       if (num_in != num_out)
  1380       {
  1318       {
  1381          fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
  1319          fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
  1382                  inname, outname);
  1320                  inname, outname);
  1525 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1463 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
  1526       int allocation_now = current_allocation;
  1464       int allocation_now = current_allocation;
  1527 #endif
  1465 #endif
  1528       for (i=2; i<argc; ++i)
  1466       for (i=2; i<argc; ++i)
  1529       {
  1467       {
  1530 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1468 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1531          int k;
  1469          int k;
  1532 #endif
  1470 #endif
  1533          int kerror;
  1471          int kerror;
  1534          fprintf(STDERR, "\n Testing %s:", argv[i]);
  1472          fprintf(STDERR, "\n Testing %s:", argv[i]);
  1535          kerror = test_one_file(argv[i], outname);
  1473          kerror = test_one_file(argv[i], outname);
  1536          if (kerror == 0)
  1474          if (kerror == 0)
  1537          {
  1475          {
  1538 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  1476 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1539             fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1477             fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1540                (unsigned long)zero_samples);
  1478                (unsigned long)zero_samples);
  1541 #else
  1479 #else
  1542             fprintf(STDERR, " PASS\n");
  1480             fprintf(STDERR, " PASS\n");
  1543 #endif
  1481 #endif
  1544 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1482 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1545             for (k = 0; k<256; k++)
  1483             for (k = 0; k<256; k++)
  1546                if (filters_used[k])
  1484                if (filters_used[k])
  1547                   fprintf(STDERR, " Filter %d was used %lu times\n",
  1485                   fprintf(STDERR, " Filter %d was used %lu times\n",
  1548                      k, (unsigned long)filters_used[k]);
  1486                      k, (unsigned long)filters_used[k]);
  1549 #endif
  1487 #endif
  1550 #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1488 #ifdef PNG_TIME_RFC1123_SUPPORTED
  1551          if (tIME_chunk_present != 0)
  1489          if (tIME_chunk_present != 0)
  1552             fprintf(STDERR, " tIME = %s\n", tIME_string);
  1490             fprintf(STDERR, " tIME = %s\n", tIME_string);
  1553          tIME_chunk_present = 0;
  1491          tIME_chunk_present = 0;
  1554 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1492 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1555          }
  1493          }
  1605          kerror = test_one_file(inname, outname);
  1543          kerror = test_one_file(inname, outname);
  1606          if (kerror == 0)
  1544          if (kerror == 0)
  1607          {
  1545          {
  1608             if (verbose == 1 || i == 2)
  1546             if (verbose == 1 || i == 2)
  1609             {
  1547             {
  1610 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1548 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1611                 int k;
  1549                 int k;
  1612 #endif
  1550 #endif
  1613 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  1551 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1614                 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1552                 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
  1615                    (unsigned long)zero_samples);
  1553                    (unsigned long)zero_samples);
  1616 #else
  1554 #else
  1617                 fprintf(STDERR, " PASS\n");
  1555                 fprintf(STDERR, " PASS\n");
  1618 #endif
  1556 #endif
  1619 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1557 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1620                 for (k = 0; k<256; k++)
  1558                 for (k = 0; k<256; k++)
  1621                    if (filters_used[k])
  1559                    if (filters_used[k])
  1622                       fprintf(STDERR, " Filter %d was used %lu times\n",
  1560                       fprintf(STDERR, " Filter %d was used %lu times\n",
  1623                          k,
  1561                          k,
  1624                          (unsigned long)filters_used[k]);
  1562                          (unsigned long)filters_used[k]);
  1625 #endif
  1563 #endif
  1626 #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1564 #ifdef PNG_TIME_RFC1123_SUPPORTED
  1627              if (tIME_chunk_present != 0)
  1565              if (tIME_chunk_present != 0)
  1628                 fprintf(STDERR, " tIME = %s\n", tIME_string);
  1566                 fprintf(STDERR, " tIME = %s\n", tIME_string);
  1629 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1567 #endif /* PNG_TIME_RFC1123_SUPPORTED */
  1630             }
  1568             }
  1631          }
  1569          }
  1687       fprintf(STDERR, " libpng FAILS test\n");
  1625       fprintf(STDERR, " libpng FAILS test\n");
  1688    return (int)(ierror != 0);
  1626    return (int)(ierror != 0);
  1689 }
  1627 }
  1690 
  1628 
  1691 /* Generate a compiler error if there is an old png.h in the search path. */
  1629 /* Generate a compiler error if there is an old png.h in the search path. */
  1692 typedef version_1_2_40 your_png_h_is_not_version_1_2_40;
  1630 typedef version_1_4_0 your_png_h_is_not_version_1_4_0;