Blame gtkmm-osx/libpng-1.2.5/pngwio.c

Carlos Lopez a09598
Carlos Lopez a09598
/* pngwio.c - functions for data output
Carlos Lopez a09598
 *
Carlos Lopez a09598
 * libpng 1.2.5 - October 3, 2002
Carlos Lopez a09598
 * For conditions of distribution and use, see copyright notice in png.h
Carlos Lopez a09598
 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
Carlos Lopez a09598
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Carlos Lopez a09598
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Carlos Lopez a09598
 *
Carlos Lopez a09598
 * This file provides a location for all output.  Users who need
Carlos Lopez a09598
 * special handling are expected to write functions that have the same
Carlos Lopez a09598
 * arguments as these and perform similar functions, but that possibly
Carlos Lopez a09598
 * use different output methods.  Note that you shouldn't change these
Carlos Lopez a09598
 * functions, but rather write replacement functions and then change
Carlos Lopez a09598
 * them at run time with png_set_write_fn(...).
Carlos Lopez a09598
 */
Carlos Lopez a09598
Carlos Lopez a09598
#define PNG_INTERNAL
Carlos Lopez a09598
#include "png.h"
Carlos Lopez a09598
#ifdef PNG_WRITE_SUPPORTED
Carlos Lopez a09598
Carlos Lopez a09598
/* Write the data to whatever output you are using.  The default routine
Carlos Lopez a09598
   writes to a file pointer.  Note that this routine sometimes gets called
Carlos Lopez a09598
   with very small lengths, so you should implement some kind of simple
Carlos Lopez a09598
   buffering if you are using unbuffered writes.  This should never be asked
Carlos Lopez a09598
   to write more than 64K on a 16 bit machine.  */
Carlos Lopez a09598
Carlos Lopez a09598
void /* PRIVATE */
Carlos Lopez a09598
png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Carlos Lopez a09598
{
Carlos Lopez a09598
   if (png_ptr->write_data_fn != NULL )
Carlos Lopez a09598
      (*(png_ptr->write_data_fn))(png_ptr, data, length);
Carlos Lopez a09598
   else
Carlos Lopez a09598
      png_error(png_ptr, "Call to NULL write function");
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
#if !defined(PNG_NO_STDIO)
Carlos Lopez a09598
/* This is the function that does the actual writing of data.  If you are
Carlos Lopez a09598
   not writing to a standard C stream, you should create a replacement
Carlos Lopez a09598
   write_data function and use it at run time with png_set_write_fn(), rather
Carlos Lopez a09598
   than changing the library. */
Carlos Lopez a09598
#ifndef USE_FAR_KEYWORD
Carlos Lopez a09598
void PNGAPI
Carlos Lopez a09598
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Carlos Lopez a09598
{
Carlos Lopez a09598
   png_uint_32 check;
Carlos Lopez a09598
Carlos Lopez a09598
#if defined(_WIN32_WCE)
Carlos Lopez a09598
   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
Carlos Lopez a09598
      check = 0;
Carlos Lopez a09598
#else
Carlos Lopez a09598
   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
Carlos Lopez a09598
#endif
Carlos Lopez a09598
   if (check != length)
Carlos Lopez a09598
      png_error(png_ptr, "Write Error");
Carlos Lopez a09598
}
Carlos Lopez a09598
#else
Carlos Lopez a09598
/* this is the model-independent version. Since the standard I/O library
Carlos Lopez a09598
   can't handle far buffers in the medium and small models, we have to copy
Carlos Lopez a09598
   the data.
Carlos Lopez a09598
*/
Carlos Lopez a09598
Carlos Lopez a09598
#define NEAR_BUF_SIZE 1024
Carlos Lopez a09598
#define MIN(a,b) (a <= b ? a : b)
Carlos Lopez a09598
Carlos Lopez a09598
void PNGAPI
Carlos Lopez a09598
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Carlos Lopez a09598
{
Carlos Lopez a09598
   png_uint_32 check;
Carlos Lopez a09598
   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
Carlos Lopez a09598
   png_FILE_p io_ptr;
Carlos Lopez a09598
Carlos Lopez a09598
   /* Check if data really is near. If so, use usual code. */
Carlos Lopez a09598
   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Carlos Lopez a09598
   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Carlos Lopez a09598
   if ((png_bytep)near_data == data)
Carlos Lopez a09598
   {
Carlos Lopez a09598
#if defined(_WIN32_WCE)
Carlos Lopez a09598
      if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
Carlos Lopez a09598
         check = 0;
Carlos Lopez a09598
#else
Carlos Lopez a09598
      check = fwrite(near_data, 1, length, io_ptr);
Carlos Lopez a09598
#endif
Carlos Lopez a09598
   }
Carlos Lopez a09598
   else
Carlos Lopez a09598
   {
Carlos Lopez a09598
      png_byte buf[NEAR_BUF_SIZE];
Carlos Lopez a09598
      png_size_t written, remaining, err;
Carlos Lopez a09598
      check = 0;
Carlos Lopez a09598
      remaining = length;
Carlos Lopez a09598
      do
Carlos Lopez a09598
      {
Carlos Lopez a09598
         written = MIN(NEAR_BUF_SIZE, remaining);
Carlos Lopez a09598
         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
Carlos Lopez a09598
#if defined(_WIN32_WCE)
Carlos Lopez a09598
         if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
Carlos Lopez a09598
            err = 0;
Carlos Lopez a09598
#else
Carlos Lopez a09598
         err = fwrite(buf, 1, written, io_ptr);
Carlos Lopez a09598
#endif
Carlos Lopez a09598
         if (err != written)
Carlos Lopez a09598
            break;
Carlos Lopez a09598
         else
Carlos Lopez a09598
            check += err;
Carlos Lopez a09598
         data += written;
Carlos Lopez a09598
         remaining -= written;
Carlos Lopez a09598
      }
Carlos Lopez a09598
      while (remaining != 0);
Carlos Lopez a09598
   }
Carlos Lopez a09598
   if (check != length)
Carlos Lopez a09598
      png_error(png_ptr, "Write Error");
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* This function is called to output any data pending writing (normally
Carlos Lopez a09598
   to disk).  After png_flush is called, there should be no data pending
Carlos Lopez a09598
   writing in any buffers. */
Carlos Lopez a09598
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Carlos Lopez a09598
void /* PRIVATE */
Carlos Lopez a09598
png_flush(png_structp png_ptr)
Carlos Lopez a09598
{
Carlos Lopez a09598
   if (png_ptr->output_flush_fn != NULL)
Carlos Lopez a09598
      (*(png_ptr->output_flush_fn))(png_ptr);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
#if !defined(PNG_NO_STDIO)
Carlos Lopez a09598
void PNGAPI
Carlos Lopez a09598
png_default_flush(png_structp png_ptr)
Carlos Lopez a09598
{
Carlos Lopez a09598
#if !defined(_WIN32_WCE)
Carlos Lopez a09598
   png_FILE_p io_ptr;
Carlos Lopez a09598
   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
Carlos Lopez a09598
   if (io_ptr != NULL)
Carlos Lopez a09598
      fflush(io_ptr);
Carlos Lopez a09598
#endif
Carlos Lopez a09598
}
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* This function allows the application to supply new output functions for
Carlos Lopez a09598
   libpng if standard C streams aren't being used.
Carlos Lopez a09598
Carlos Lopez a09598
   This function takes as its arguments:
Carlos Lopez a09598
   png_ptr       - pointer to a png output data structure
Carlos Lopez a09598
   io_ptr        - pointer to user supplied structure containing info about
Carlos Lopez a09598
                   the output functions.  May be NULL.
Carlos Lopez a09598
   write_data_fn - pointer to a new output function that takes as its
Carlos Lopez a09598
                   arguments a pointer to a png_struct, a pointer to
Carlos Lopez a09598
                   data to be written, and a 32-bit unsigned int that is
Carlos Lopez a09598
                   the number of bytes to be written.  The new write
Carlos Lopez a09598
                   function should call png_error(png_ptr, "Error msg")
Carlos Lopez a09598
                   to exit and output any fatal error messages.
Carlos Lopez a09598
   flush_data_fn - pointer to a new flush function that takes as its
Carlos Lopez a09598
                   arguments a pointer to a png_struct.  After a call to
Carlos Lopez a09598
                   the flush function, there should be no data in any buffers
Carlos Lopez a09598
                   or pending transmission.  If the output method doesn't do
Carlos Lopez a09598
                   any buffering of ouput, a function prototype must still be
Carlos Lopez a09598
                   supplied although it doesn't have to do anything.  If
Carlos Lopez a09598
                   PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
Carlos Lopez a09598
                   time, output_flush_fn will be ignored, although it must be
Carlos Lopez a09598
                   supplied for compatibility. */
Carlos Lopez a09598
void PNGAPI
Carlos Lopez a09598
png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
Carlos Lopez a09598
   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
Carlos Lopez a09598
{
Carlos Lopez a09598
   png_ptr->io_ptr = io_ptr;
Carlos Lopez a09598
Carlos Lopez a09598
#if !defined(PNG_NO_STDIO)
Carlos Lopez a09598
   if (write_data_fn != NULL)
Carlos Lopez a09598
      png_ptr->write_data_fn = write_data_fn;
Carlos Lopez a09598
   else
Carlos Lopez a09598
      png_ptr->write_data_fn = png_default_write_data;
Carlos Lopez a09598
#else
Carlos Lopez a09598
   png_ptr->write_data_fn = write_data_fn;
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Carlos Lopez a09598
#if !defined(PNG_NO_STDIO)
Carlos Lopez a09598
   if (output_flush_fn != NULL)
Carlos Lopez a09598
      png_ptr->output_flush_fn = output_flush_fn;
Carlos Lopez a09598
   else
Carlos Lopez a09598
      png_ptr->output_flush_fn = png_default_flush;
Carlos Lopez a09598
#else
Carlos Lopez a09598
   png_ptr->output_flush_fn = output_flush_fn;
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Carlos Lopez a09598
Carlos Lopez a09598
   /* It is an error to read while writing a png file */
Carlos Lopez a09598
   if (png_ptr->read_data_fn != NULL)
Carlos Lopez a09598
   {
Carlos Lopez a09598
      png_ptr->read_data_fn = NULL;
Carlos Lopez a09598
      png_warning(png_ptr,
Carlos Lopez a09598
         "Attempted to set both read_data_fn and write_data_fn in");
Carlos Lopez a09598
      png_warning(png_ptr,
Carlos Lopez a09598
         "the same structure.  Resetting read_data_fn to NULL.");
Carlos Lopez a09598
   }
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
#if defined(USE_FAR_KEYWORD)
Carlos Lopez a09598
#if defined(_MSC_VER)
Carlos Lopez a09598
void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
Carlos Lopez a09598
{
Carlos Lopez a09598
   void *near_ptr;
Carlos Lopez a09598
   void FAR *far_ptr;
Carlos Lopez a09598
   FP_OFF(near_ptr) = FP_OFF(ptr);
Carlos Lopez a09598
   far_ptr = (void FAR *)near_ptr;
Carlos Lopez a09598
   if(check != 0)
Carlos Lopez a09598
      if(FP_SEG(ptr) != FP_SEG(far_ptr))
Carlos Lopez a09598
         png_error(png_ptr,"segment lost in conversion");
Carlos Lopez a09598
   return(near_ptr);
Carlos Lopez a09598
}
Carlos Lopez a09598
#  else
Carlos Lopez a09598
void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
Carlos Lopez a09598
{
Carlos Lopez a09598
   void *near_ptr;
Carlos Lopez a09598
   void FAR *far_ptr;
Carlos Lopez a09598
   near_ptr = (void FAR *)ptr;
Carlos Lopez a09598
   far_ptr = (void FAR *)near_ptr;
Carlos Lopez a09598
   if(check != 0)
Carlos Lopez a09598
      if(far_ptr != ptr)
Carlos Lopez a09598
         png_error(png_ptr,"segment lost in conversion");
Carlos Lopez a09598
   return(near_ptr);
Carlos Lopez a09598
}
Carlos Lopez a09598
#   endif
Carlos Lopez a09598
#   endif
Carlos Lopez a09598
#endif /* PNG_WRITE_SUPPORTED */