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


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:56