fukasawa e60969
fukasawa e60969
/* pngwio.c - functions for data output
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.15 [November 20, 2014]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * This file provides a location for all output.  Users who need
fukasawa e60969
 * special handling are expected to write functions that have the same
fukasawa e60969
 * arguments as these and perform similar functions, but that possibly
fukasawa e60969
 * use different output methods.  Note that you shouldn't change these
fukasawa e60969
 * functions, but rather write replacement functions and then change
fukasawa e60969
 * them at run time with png_set_write_fn(...).
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include "pngpriv.h"
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
fukasawa e60969
/* Write the data to whatever output you are using.  The default routine
fukasawa e60969
 * writes to a file pointer.  Note that this routine sometimes gets called
fukasawa e60969
 * with very small lengths, so you should implement some kind of simple
fukasawa e60969
 * buffering if you are using unbuffered writes.  This should never be asked
fukasawa e60969
 * to write more than 64K on a 16-bit machine.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   /* NOTE: write_data_fn must not change the buffer! */
fukasawa e60969
   if (png_ptr->write_data_fn != NULL )
fukasawa e60969
      (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
fukasawa e60969
         length);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_error(png_ptr, "Call to NULL write function");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
/* This is the function that does the actual writing of data.  If you are
fukasawa e60969
 * not writing to a standard C stream, you should create a replacement
fukasawa e60969
 * write_data function and use it at run time with png_set_write_fn(), rather
fukasawa e60969
 * than changing the library.
fukasawa e60969
 */
fukasawa e60969
void PNGCBAPI
fukasawa e60969
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_size_t check;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
fukasawa e60969
fukasawa e60969
   if (check != length)
fukasawa e60969
      png_error(png_ptr, "Write Error");
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* This function is called to output any data pending writing (normally
fukasawa e60969
 * to disk).  After png_flush is called, there should be no data pending
fukasawa e60969
 * writing in any buffers.
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_flush(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->output_flush_fn != NULL)
fukasawa e60969
      (*(png_ptr->output_flush_fn))(png_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#  ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
void PNGCBAPI
fukasawa e60969
png_default_flush(png_structp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_FILE_p io_ptr;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));
fukasawa e60969
   fflush(io_ptr);
fukasawa e60969
}
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* This function allows the application to supply new output functions for
fukasawa e60969
 * libpng if standard C streams aren't being used.
fukasawa e60969
 *
fukasawa e60969
 * This function takes as its arguments:
fukasawa e60969
 * png_ptr       - pointer to a png output data structure
fukasawa e60969
 * io_ptr        - pointer to user supplied structure containing info about
fukasawa e60969
 *                 the output functions.  May be NULL.
fukasawa e60969
 * write_data_fn - pointer to a new output function that takes as its
fukasawa e60969
 *                 arguments a pointer to a png_struct, a pointer to
fukasawa e60969
 *                 data to be written, and a 32-bit unsigned int that is
fukasawa e60969
 *                 the number of bytes to be written.  The new write
fukasawa e60969
 *                 function should call png_error(png_ptr, "Error msg")
fukasawa e60969
 *                 to exit and output any fatal error messages.  May be
fukasawa e60969
 *                 NULL, in which case libpng's default function will
fukasawa e60969
 *                 be used.
fukasawa e60969
 * flush_data_fn - pointer to a new flush function that takes as its
fukasawa e60969
 *                 arguments a pointer to a png_struct.  After a call to
fukasawa e60969
 *                 the flush function, there should be no data in any buffers
fukasawa e60969
 *                 or pending transmission.  If the output method doesn't do
fukasawa e60969
 *                 any buffering of output, a function prototype must still be
fukasawa e60969
 *                 supplied although it doesn't have to do anything.  If
fukasawa e60969
 *                 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
fukasawa e60969
 *                 time, output_flush_fn will be ignored, although it must be
fukasawa e60969
 *                 supplied for compatibility.  May be NULL, in which case
fukasawa e60969
 *                 libpng's default function will be used, if
fukasawa e60969
 *                 PNG_WRITE_FLUSH_SUPPORTED is defined.  This is not
fukasawa e60969
 *                 a good idea if io_ptr does not point to a standard
fukasawa e60969
 *                 *FILE structure.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
fukasawa e60969
    png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->io_ptr = io_ptr;
fukasawa e60969
fukasawa e60969
#ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
   if (write_data_fn != NULL)
fukasawa e60969
      png_ptr->write_data_fn = write_data_fn;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_ptr->write_data_fn = png_default_write_data;
fukasawa e60969
#else
fukasawa e60969
   png_ptr->write_data_fn = write_data_fn;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
#  ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
fukasawa e60969
   if (output_flush_fn != NULL)
fukasawa e60969
      png_ptr->output_flush_fn = output_flush_fn;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_ptr->output_flush_fn = png_default_flush;
fukasawa e60969
fukasawa e60969
#  else
fukasawa e60969
   png_ptr->output_flush_fn = output_flush_fn;
fukasawa e60969
#  endif
fukasawa e60969
#else
fukasawa e60969
   PNG_UNUSED(output_flush_fn)
fukasawa e60969
#endif /* WRITE_FLUSH */
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_SUPPORTED
fukasawa e60969
   /* It is an error to read while writing a png file */
fukasawa e60969
   if (png_ptr->read_data_fn != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->read_data_fn = NULL;
fukasawa e60969
fukasawa e60969
      png_warning(png_ptr,
fukasawa e60969
          "Can't set both read_data_fn and write_data_fn in the"
fukasawa e60969
          " same structure");
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE */