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