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)  

pngwio.c

Go to the documentation of this file.
00001 
00002 /* pngwio.c - functions for data output
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 output.  Users who need
00014  * special handling are expected to write functions that have the same
00015  * arguments as these and perform similar functions, but that possibly
00016  * use different output methods.  Note that you shouldn't change these
00017  * functions, but rather write replacement functions and then change
00018  * them at run time with png_set_write_fn(...).
00019  */
00020 
00021 #define PNG_INTERNAL
00022 #include "png.h"
00023 #ifdef PNG_WRITE_SUPPORTED
00024 
00025 /* Write the data to whatever output you are using.  The default routine
00026  * writes to 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 writes.  This should never be asked
00029  * to write more than 64K on a 16 bit machine.
00030  */
00031 
00032 void /* PRIVATE */
00033 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00034 {
00035    if (png_ptr->write_data_fn != NULL )
00036       (*(png_ptr->write_data_fn))(png_ptr, data, length);
00037    else
00038       png_error(png_ptr, "Call to NULL write function");
00039 }
00040 
00041 #if !defined(PNG_NO_STDIO)
00042 /* This is the function that does the actual writing of data.  If you are
00043  * not writing to a standard C stream, you should create a replacement
00044  * write_data function and use it at run time with png_set_write_fn(), rather
00045  * than changing the library.
00046  */
00047 #ifndef USE_FAR_KEYWORD
00048 void PNGAPI
00049 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00050 {
00051    png_uint_32 check;
00052 
00053    if (png_ptr == NULL)
00054       return;
00055 #if defined(_WIN32_WCE)
00056    if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00057       check = 0;
00058 #else
00059    check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
00060 #endif
00061    if (check != length)
00062       png_error(png_ptr, "Write Error");
00063 }
00064 #else
00065 /* This is the model-independent version. Since the standard I/O library
00066  * can't handle far buffers in the medium and small models, we have to copy
00067  * the data.
00068  */
00069 
00070 #define NEAR_BUF_SIZE 1024
00071 #define MIN(a,b) (a <= b ? a : b)
00072 
00073 void PNGAPI
00074 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00075 {
00076    png_uint_32 check;
00077    png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
00078    png_FILE_p io_ptr;
00079 
00080    if (png_ptr == NULL)
00081       return;
00082    /* Check if data really is near. If so, use usual code. */
00083    near_data = (png_byte *)CVT_PTR_NOCHECK(data);
00084    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
00085    if ((png_bytep)near_data == data)
00086    {
00087 #if defined(_WIN32_WCE)
00088       if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
00089          check = 0;
00090 #else
00091       check = fwrite(near_data, 1, length, io_ptr);
00092 #endif
00093    }
00094    else
00095    {
00096       png_byte buf[NEAR_BUF_SIZE];
00097       png_size_t written, remaining, err;
00098       check = 0;
00099       remaining = length;
00100       do
00101       {
00102          written = MIN(NEAR_BUF_SIZE, remaining);
00103          png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
00104 #if defined(_WIN32_WCE)
00105          if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
00106             err = 0;
00107 #else
00108          err = fwrite(buf, 1, written, io_ptr);
00109 #endif
00110          if (err != written)
00111             break;
00112 
00113          else
00114             check += err;
00115 
00116          data += written;
00117          remaining -= written;
00118       }
00119       while (remaining != 0);
00120    }
00121    if (check != length)
00122       png_error(png_ptr, "Write Error");
00123 }
00124 
00125 #endif
00126 #endif
00127 
00128 /* This function is called to output any data pending writing (normally
00129  * to disk).  After png_flush is called, there should be no data pending
00130  * writing in any buffers.
00131  */
00132 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00133 void /* PRIVATE */
00134 png_flush(png_structp png_ptr)
00135 {
00136    if (png_ptr->output_flush_fn != NULL)
00137       (*(png_ptr->output_flush_fn))(png_ptr);
00138 }
00139 
00140 #if !defined(PNG_NO_STDIO)
00141 void PNGAPI
00142 png_default_flush(png_structp png_ptr)
00143 {
00144 #if !defined(_WIN32_WCE)
00145    png_FILE_p io_ptr;
00146 #endif
00147    if (png_ptr == NULL)
00148       return;
00149 #if !defined(_WIN32_WCE)
00150    io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
00151    fflush(io_ptr);
00152 #endif
00153 }
00154 #endif
00155 #endif
00156 
00157 /* This function allows the application to supply new output functions for
00158  * libpng if standard C streams aren't being used.
00159  *
00160  * This function takes as its arguments:
00161  * png_ptr       - pointer to a png output data structure
00162  * io_ptr        - pointer to user supplied structure containing info about
00163  *                 the output functions.  May be NULL.
00164  * write_data_fn - pointer to a new output function that takes as its
00165  *                 arguments a pointer to a png_struct, a pointer to
00166  *                 data to be written, and a 32-bit unsigned int that is
00167  *                 the number of bytes to be written.  The new write
00168  *                 function should call png_error(png_ptr, "Error msg")
00169  *                 to exit and output any fatal error messages.  May be
00170  *                 NULL, in which case libpng's default function will
00171  *                 be used.
00172  * flush_data_fn - pointer to a new flush function that takes as its
00173  *                 arguments a pointer to a png_struct.  After a call to
00174  *                 the flush function, there should be no data in any buffers
00175  *                 or pending transmission.  If the output method doesn't do
00176  *                 any buffering of ouput, a function prototype must still be
00177  *                 supplied although it doesn't have to do anything.  If
00178  *                 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
00179  *                 time, output_flush_fn will be ignored, although it must be
00180  *                 supplied for compatibility.  May be NULL, in which case
00181  *                 libpng's default function will be used, if
00182  *                 PNG_WRITE_FLUSH_SUPPORTED is defined.  This is not
00183  *                 a good idea if io_ptr does not point to a standard
00184  *                 *FILE structure.
00185  */
00186 void PNGAPI
00187 png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
00188    png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
00189 {
00190    if (png_ptr == NULL)
00191       return;
00192 
00193    png_ptr->io_ptr = io_ptr;
00194 
00195 #if !defined(PNG_NO_STDIO)
00196    if (write_data_fn != NULL)
00197       png_ptr->write_data_fn = write_data_fn;
00198 
00199    else
00200       png_ptr->write_data_fn = png_default_write_data;
00201 #else
00202    png_ptr->write_data_fn = write_data_fn;
00203 #endif
00204 
00205 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00206 #if !defined(PNG_NO_STDIO)
00207    if (output_flush_fn != NULL)
00208       png_ptr->output_flush_fn = output_flush_fn;
00209 
00210    else
00211       png_ptr->output_flush_fn = png_default_flush;
00212 #else
00213    png_ptr->output_flush_fn = output_flush_fn;
00214 #endif
00215 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
00216 
00217    /* It is an error to read while writing a png file */
00218    if (png_ptr->read_data_fn != NULL)
00219    {
00220       png_ptr->read_data_fn = NULL;
00221       png_warning(png_ptr,
00222          "Attempted to set both read_data_fn and write_data_fn in");
00223       png_warning(png_ptr,
00224          "the same structure.  Resetting read_data_fn to NULL.");
00225    }
00226 }
00227 
00228 #if defined(USE_FAR_KEYWORD)
00229 #if defined(_MSC_VER)
00230 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
00231 {
00232    void *near_ptr;
00233    void FAR *far_ptr;
00234    FP_OFF(near_ptr) = FP_OFF(ptr);
00235    far_ptr = (void FAR *)near_ptr;
00236 
00237    if (check != 0)
00238       if (FP_SEG(ptr) != FP_SEG(far_ptr))
00239          png_error(png_ptr, "segment lost in conversion");
00240 
00241    return(near_ptr);
00242 }
00243 #  else
00244 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
00245 {
00246    void *near_ptr;
00247    void FAR *far_ptr;
00248    near_ptr = (void FAR *)ptr;
00249    far_ptr = (void FAR *)near_ptr;
00250 
00251    if (check != 0)
00252       if (far_ptr != ptr)
00253          png_error(png_ptr, "segment lost in conversion");
00254 
00255    return(near_ptr);
00256 }
00257 #   endif
00258 #   endif
00259 #endif /* PNG_WRITE_SUPPORTED */