fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

pngrio.c

Go to the documentation of this file.
00001 
00002 /* pngrio.c - functions for data input
00003  *
00004  * Last changed in libpng 1.2.37 [June 4, 2009]
00005  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
00006  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00007  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00008  *
00009  * This code is released under the libpng license.
00010  * For conditions of distribution and use, see the disclaimer
00011  * and license in png.h
00012  *
00013  * This file provides a location for all input.  Users who need
00014  * special handling are expected to write a function that has the same
00015  * arguments as this and performs a similar function, but that possibly
00016  * has a different input method.  Note that you shouldn't change this
00017  * function, but rather write a replacement function and then make
00018  * libpng use it at run time with png_set_read_fn(...).
00019  */
00020 
00021 #define PNG_INTERNAL
00022 #include "png.h"
00023 #if defined(PNG_READ_SUPPORTED)
00024 
00025 /* Read the data from whatever input you are using.  The default routine
00026  * reads from a file pointer.  Note that this routine sometimes gets called
00027  * with very small lengths, so you should implement some kind of simple
00028  * buffering if you are using unbuffered reads.  This should never be asked
00029  * to read more then 64K on a 16 bit machine.
00030  */
00031 void /* PRIVATE */
00032 png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00033 {
00034    png_debug1(4, "reading %d bytes", (int)length);
00035    if (png_ptr->read_data_fn != NULL)
00036       (*(png_ptr->read_data_fn))(png_ptr, data, length);
00037    else
00038       png_error(png_ptr, "Call to NULL read function");
00039 }
00040 
00041 #if !defined(PNG_NO_STDIO)
00042 /* This is the function that does the actual reading of data.  If you are
00043  * not reading from a standard C stream, you should create a replacement
00044  * read_data function and use it at run time with png_set_read_fn(), rather
00045  * than changing the library.
00046  */
00047 #ifndef USE_FAR_KEYWORD
00048 void PNGAPI
00049 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00050 {
00051    png_size_t check;
00052 
00053    if (png_ptr == NULL)
00054       return;
00055    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
00056     * instead of an int, which is what fread() actually returns.
00057     */
00058 #if defined(_WIN32_WCE)
00059    if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00060       check = 0;
00061 #else
00062    check = (png_size_t)fread(data, (png_size_t)1, length,
00063       (png_FILE_p)png_ptr->io_ptr);
00064 #endif
00065 
00066    if (check != length)
00067       png_error(png_ptr, "Read Error");
00068 }
00069 #else
00070 /* This is the model-independent version. Since the standard I/O library
00071    can't handle far buffers in the medium and small models, we have to copy
00072    the data.
00073 */
00074 
00075 #define NEAR_BUF_SIZE 1024
00076 #define MIN(a,b) (a <= b ? a : b)
00077 
00078 static void PNGAPI
00079 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00080 {
00081    int check;
00082    png_byte *n_data;
00083    png_FILE_p io_ptr;
00084 
00085    if (png_ptr == NULL)
00086       return;
00087    /* Check if data really is near. If so, use usual code. */
00088    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
00089    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
00090    if ((png_bytep)n_data == data)
00091    {
00092 #if defined(_WIN32_WCE)
00093       if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00094          check = 0;
00095 #else
00096       check = fread(n_data, 1, length, io_ptr);
00097 #endif
00098    }
00099    else
00100    {
00101       png_byte buf[NEAR_BUF_SIZE];
00102       png_size_t read, remaining, err;
00103       check = 0;
00104       remaining = length;
00105       do
00106       {
00107          read = MIN(NEAR_BUF_SIZE, remaining);
00108 #if defined(_WIN32_WCE)
00109          if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
00110             err = 0;
00111 #else
00112          err = fread(buf, (png_size_t)1, read, io_ptr);
00113 #endif
00114          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
00115          if (err != read)
00116             break;
00117          else
00118             check += err;
00119          data += read;
00120          remaining -= read;
00121       }
00122       while (remaining != 0);
00123    }
00124    if ((png_uint_32)check != (png_uint_32)length)
00125       png_error(png_ptr, "read Error");
00126 }
00127 #endif
00128 #endif
00129 
00130 /* This function allows the application to supply a new input function
00131  * for libpng if standard C streams aren't being used.
00132  *
00133  * This function takes as its arguments:
00134  * png_ptr      - pointer to a png input data structure
00135  * io_ptr       - pointer to user supplied structure containing info about
00136  *                the input functions.  May be NULL.
00137  * read_data_fn - pointer to a new input function that takes as its
00138  *                arguments a pointer to a png_struct, a pointer to
00139  *                a location where input data can be stored, and a 32-bit
00140  *                unsigned int that is the number of bytes to be read.
00141  *                To exit and output any fatal error messages the new write
00142  *                function should call png_error(png_ptr, "Error msg").
00143  *                May be NULL, in which case libpng's default function will
00144  *                be used.
00145  */
00146 void PNGAPI
00147 png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
00148    png_rw_ptr read_data_fn)
00149 {
00150    if (png_ptr == NULL)
00151       return;
00152    png_ptr->io_ptr = io_ptr;
00153 
00154 #if !defined(PNG_NO_STDIO)
00155    if (read_data_fn != NULL)
00156       png_ptr->read_data_fn = read_data_fn;
00157    else
00158       png_ptr->read_data_fn = png_default_read_data;
00159 #else
00160    png_ptr->read_data_fn = read_data_fn;
00161 #endif
00162 
00163    /* It is an error to write to a read device */
00164    if (png_ptr->write_data_fn != NULL)
00165    {
00166       png_ptr->write_data_fn = NULL;
00167       png_warning(png_ptr,
00168          "It's an error to set both read_data_fn and write_data_fn in the ");
00169       png_warning(png_ptr,
00170          "same structure.  Resetting write_data_fn to NULL.");
00171    }
00172 
00173 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00174    png_ptr->output_flush_fn = NULL;
00175 #endif
00176 }
00177 #endif /* PNG_READ_SUPPORTED */