⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pngtest.c

📁 Linux下的基于X11的图形开发环境。
💻 C
📖 第 1 页 / 共 3 页
字号:
/* pngtest.c - a simple test program to test libpng * * libpng 1.2.5 - October 3, 2002 * For conditions of distribution and use, see copyright notice in png.h * Copyright (c) 1998-2002 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This program reads in a PNG image, writes it out again, and then * compares the two files.  If the files are identical, this shows that * the basic chunk handling, filtering, and (de)compression code is working * properly.  It does not currently test all of the transforms, although * it probably should. * * The program will report "FAIL" in certain legitimate cases: * 1) when the compression level or filter selection method is changed. * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192. * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks *    exist in the input file. * 4) others not listed here... * In these cases, it is best to check with another tool such as "pngcheck" * to see what the differences between the two files are. * * If a filename is given on the command-line, then this file is used * for the input, rather than the default "pngtest.png".  This allows * testing a wide variety of files easily.  You can also test a number * of files at once by typing "pngtest -m file1.png file2.png ..." */#if defined(_WIN32_WCE)#  if _WIN32_WCE < 211     __error__ (f|w)printf functions are not supported on old WindowsCE.;#  endif#  include <windows.h>#  include <stdlib.h>#  define READFILE(file, data, length, check) \     if (ReadFile(file, data, length, &check,NULL)) check = 0#  define WRITEFILE(file, data, length, check)) \     if (WriteFile(file, data, length, &check, NULL)) check = 0#  define FCLOSE(file) CloseHandle(file)#else#  include <stdio.h>#  include <stdlib.h>#  include <assert.h>#  define READFILE(file, data, length, check) \     check=(png_size_t)fread(data,(png_size_t)1,length,file)#  define WRITEFILE(file, data, length, check) \     check=(png_size_t)fwrite(data,(png_size_t)1, length, file)#  define FCLOSE(file) fclose(file)#endif#if defined(PNG_NO_STDIO)#  if defined(_WIN32_WCE)     typedef HANDLE                png_FILE_p;#  else     typedef FILE                * png_FILE_p;#  endif#endif/* Makes pngtest verbose so we can find problems (needs to be before png.h) */#ifndef PNG_DEBUG#  define PNG_DEBUG 0#endif#if !PNG_DEBUG#  define SINGLE_ROWBUF_ALLOC  /* makes buffer overruns easier to nail */#endif/* Turn on CPU timing#define PNGTEST_TIMING*/#ifdef PNG_NO_FLOATING_POINT_SUPPORTED#undef PNGTEST_TIMING#endif#ifdef PNGTEST_TIMINGstatic float t_start, t_stop, t_decode, t_encode, t_misc;#include <time.h>#endif#include "png.h"/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */#ifndef png_jmpbuf#  define png_jmpbuf(png_ptr) png_ptr->jmpbuf#endif#ifdef PNGTEST_TIMINGstatic float t_start, t_stop, t_decode, t_encode, t_misc;#if !defined(PNG_tIME_SUPPORTED)#include <time.h>#endif#endif#if defined(PNG_TIME_RFC1123_SUPPORTED)static int tIME_chunk_present=0;static char tIME_string[30] = "no tIME chunk present in file";#endifstatic int verbose = 0;int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));#ifdef __TURBOC__#include <mem.h>#endif/* defined so I can write to a file on gui/windowing platforms *//*  #define STDERR stderr  */#define STDERR stdout   /* for DOS *//* example of using row callbacks to make a simple progress meter */static int status_pass=1;static int status_dots_requested=0;static int status_dots=1;void#ifdef PNG_1_0_XPNGAPI#endifread_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);void#ifdef PNG_1_0_XPNGAPI#endifread_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass){    if(png_ptr == NULL || row_number > PNG_MAX_UINT) return;    if(status_pass != pass)    {       fprintf(stdout,"\n Pass %d: ",pass);       status_pass = pass;       status_dots = 31;    }    status_dots--;    if(status_dots == 0)    {       fprintf(stdout, "\n         ");       status_dots=30;    }    fprintf(stdout, "r");}void#ifdef PNG_1_0_XPNGAPI#endifwrite_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);void#ifdef PNG_1_0_XPNGAPI#endifwrite_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass){    if(png_ptr == NULL || row_number > PNG_MAX_UINT || pass > 7) return;    fprintf(stdout, "w");}#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)/* Example of using user transform callback (we don't transform anything,   but merely examine the row filters.  We set this to 256 rather than   5 in case illegal filter values are present.) */static png_uint_32 filters_used[256];void#ifdef PNG_1_0_XPNGAPI#endifcount_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);void#ifdef PNG_1_0_XPNGAPI#endifcount_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data){    if(png_ptr != NULL && row_info != NULL)      ++filters_used[*(data-1)];}#endif#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)/* example of using user transform callback (we don't transform anything,   but merely count the zero samples) */static png_uint_32 zero_samples;void#ifdef PNG_1_0_XPNGAPI#endifcount_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);void#ifdef PNG_1_0_XPNGAPI#endifcount_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data){   png_bytep dp = data;   if(png_ptr == NULL)return;   /* contents of row_info:    *  png_uint_32 width      width of row    *  png_uint_32 rowbytes   number of bytes in row    *  png_byte color_type    color type of pixels    *  png_byte bit_depth     bit depth of samples    *  png_byte channels      number of channels (1-4)    *  png_byte pixel_depth   bits per pixel (depth*channels)    */    /* counts the number of zero samples (or zero pixels if color_type is 3 */    if(row_info->color_type == 0 || row_info->color_type == 3)    {       int pos=0;       png_uint_32 n, nstop;       for (n=0, nstop=row_info->width; n<nstop; n++)       {          if(row_info->bit_depth == 1)          {             if(((*dp << pos++ ) & 0x80) == 0) zero_samples++;             if(pos == 8)             {                pos = 0;                dp++;             }          }          if(row_info->bit_depth == 2)          {             if(((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;             if(pos == 8)             {                pos = 0;                dp++;             }          }          if(row_info->bit_depth == 4)          {             if(((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;             if(pos == 8)             {                pos = 0;                dp++;             }          }          if(row_info->bit_depth == 8)             if(*dp++ == 0) zero_samples++;          if(row_info->bit_depth == 16)          {             if((*dp | *(dp+1)) == 0) zero_samples++;             dp+=2;          }       }    }    else /* other color types */    {       png_uint_32 n, nstop;       int channel;       int color_channels = row_info->channels;       if(row_info->color_type > 3)color_channels--;       for (n=0, nstop=row_info->width; n<nstop; n++)       {          for (channel = 0; channel < color_channels; channel++)          {             if(row_info->bit_depth == 8)                if(*dp++ == 0) zero_samples++;             if(row_info->bit_depth == 16)             {                if((*dp | *(dp+1)) == 0) zero_samples++;                dp+=2;             }          }          if(row_info->color_type > 3)          {             dp++;             if(row_info->bit_depth == 16)dp++;          }       }    }}#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */static int wrote_question = 0;#if defined(PNG_NO_STDIO)/* START of code to validate stdio-free compilation *//* These copies of the default read/write functions come from pngrio.c and *//* pngwio.c.  They allow "don't include stdio" testing of the library. *//* This is the function that does the actual reading of data.  If you are   not reading from a standard C stream, you should create a replacement   read_data function and use it at run time with png_set_read_fn(), rather   than changing the library. */#ifndef USE_FAR_KEYWORDstatic voidpngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length){   png_size_t check;   /* fread() returns 0 on error, so it is OK to store this in a png_size_t    * instead of an int, which is what fread() actually returns.    */   READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);   if (check != length)   {      png_error(png_ptr, "Read Error!");   }}#else/* this is the model-independent version. Since the standard I/O library   can't handle far buffers in the medium and small models, we have to copy   the data.*/#define NEAR_BUF_SIZE 1024#define MIN(a,b) (a <= b ? a : b)static voidpngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length){   int check;   png_byte *n_data;   png_FILE_p io_ptr;   /* Check if data really is near. If so, use usual code. */   n_data = (png_byte *)CVT_PTR_NOCHECK(data);   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);   if ((png_bytep)n_data == data)   {      READFILE(io_ptr, n_data, length, check);   }   else   {      png_byte buf[NEAR_BUF_SIZE];      png_size_t read, remaining, err;      check = 0;      remaining = length;      do      {         read = MIN(NEAR_BUF_SIZE, remaining);         READFILE(io_ptr, buf, 1, err);         png_memcpy(data, buf, read); /* copy far buffer to near buffer */         if(err != read)            break;         else            check += err;         data += read;         remaining -= read;      }      while (remaining != 0);   }   if (check != length)   {      png_error(png_ptr, "read Error");   }}#endif /* USE_FAR_KEYWORD */#if defined(PNG_WRITE_FLUSH_SUPPORTED)static voidpngtest_flush(png_structp png_ptr){#if !defined(_WIN32_WCE)   png_FILE_p io_ptr;   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));   if (io_ptr != NULL)      fflush(io_ptr);#endif}#endif/* This is the function that does the actual writing of data.  If you are   not writing to a standard C stream, you should create a replacement   write_data function and use it at run time with png_set_write_fn(), rather   than changing the library. */#ifndef USE_FAR_KEYWORDstatic voidpngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length){   png_uint_32 check;   WRITEFILE((png_FILE_p)png_ptr->io_ptr,  data, length, check);   if (check != length)   {      png_error(png_ptr, "Write Error");   }}#else/* this is the model-independent version. Since the standard I/O library   can't handle far buffers in the medium and small models, we have to copy   the data.*/#define NEAR_BUF_SIZE 1024#define MIN(a,b) (a <= b ? a : b)static voidpngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length){   png_uint_32 check;   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */   png_FILE_p io_ptr;   /* Check if data really is near. If so, use usual code. */   near_data = (png_byte *)CVT_PTR_NOCHECK(data);   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);   if ((png_bytep)near_data == data)   {      WRITEFILE(io_ptr, near_data, length, check);   }   else   {      png_byte buf[NEAR_BUF_SIZE];      png_size_t written, remaining, err;      check = 0;      remaining = length;      do      {         written = MIN(NEAR_BUF_SIZE, remaining);         png_memcpy(buf, data, written); /* copy far buffer to near buffer */         WRITEFILE(io_ptr, buf, written, err);         if (err != written)            break;         else            check += err;         data += written;         remaining -= written;      }      while (remaining != 0);   }   if (check != length)   {      png_error(png_ptr, "Write Error");   }}#endif /* USE_FAR_KEYWORD *//* This function is called when there is a warning, but the library thinks * it can continue anyway.  Replacement functions don't have to do anything * here if you don't want to.  In the default configuration, png_ptr is * not used, but it is passed in case it may be useful. */static voidpngtest_warning(png_structp png_ptr, png_const_charp message){   PNG_CONST char *name = "UNKNOWN (ERROR!)";   if (png_ptr != NULL && png_ptr->error_ptr != NULL)      name = png_ptr->error_ptr;   fprintf(STDERR, "%s: libpng warning: %s\n", name, message);}/* This is the default error handling function.  Note that replacements for * this function MUST NOT RETURN, or the program will likely crash.  This * function is used by default, or if the program supplies NULL for the * error function pointer in png_set_error_fn(). */static voidpngtest_error(png_structp png_ptr, png_const_charp message){   pngtest_warning(png_ptr, message);   /* We can return because png_error calls the default handler, which is    * actually OK in this case. */}#endif /* PNG_NO_STDIO *//* END of code to validate stdio-free compilation *//* START of code to validate memory allocation and deallocation */#ifdef PNG_USER_MEM_SUPPORTED/* Allocate memory.  For reasonable files, size should never exceed   64K.  However, zlib may allocate more then 64K if you don't tell   it not to.  See zconf.h and png.h for more information.  zlib does   need to allocate exactly 64K, so whatever you call here must   have the ability to do that.   This piece of code can be compiled to validate max 64K allocations   by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */typedef struct memory_information{   png_uint_32               size;   png_voidp                 pointer;   struct memory_information FAR *next;} memory_information;typedef memory_information FAR *memory_infop;static memory_infop pinformation = NULL;static int current_allocation = 0;static int maximum_allocation = 0;static int total_allocation = 0;static int num_allocations = 0;png_voidp png_debug_malloc PNGARG((png_structp png_ptr, png_uint_32 size));void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));png_voidppng_debug_malloc(png_structp png_ptr, png_uint_32 size){   /* png_malloc has already tested for NULL; png_create_struct calls      png_debug_malloc directly, with png_ptr == NULL which is OK */   if (size == 0)      return (NULL);   /* This calls the library allocator twice, once to get the requested      buffer and once to get a new free list entry. */   {      memory_infop pinfo = (memory_infop)png_malloc_default(png_ptr,         (png_uint_32)sizeof *pinfo);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -