blob: 0e7e8d0bba48ab50c9c7d3663cb32fef977a03fc [file] [log] [blame]
Guy Schalnate5a37791996-06-05 15:50:50 -05001
2/* pngwio.c - functions for data output
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05009 * This code is released under the zlib-libpng license.
10 * For conditions of distribution and use, see copyright notice in png.h
11 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050012 * This file provides a location for all output. Users who need
13 * special handling are expected to write functions that have the same
14 * arguments as these and perform similar functions, but that possibly
15 * use different output methods. Note that you shouldn't change these
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060016 * functions, but rather write replacement functions and then change
17 * them at run time with png_set_write_fn(...).
18 */
Guy Schalnate5a37791996-06-05 15:50:50 -050019
Guy Schalnate5a37791996-06-05 15:50:50 -050020#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050021#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050022#include "pngpriv.h"
Guy Schalnate5a37791996-06-05 15:50:50 -050023
24/* Write the data to whatever output you are using. The default routine
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050025 * writes to a file pointer. Note that this routine sometimes gets called
26 * with very small lengths, so you should implement some kind of simple
27 * buffering if you are using unbuffered writes. This should never be asked
28 * to write more than 64K on a 16 bit machine.
29 */
Guy Schalnate5a37791996-06-05 15:50:50 -050030
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050031void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050032png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050033{
Andreas Dilger47a0c421997-05-16 02:46:07 -050034 if (png_ptr->write_data_fn != NULL )
Guy Schalnate5a37791996-06-05 15:50:50 -050035 (*(png_ptr->write_data_fn))(png_ptr, data, length);
36 else
37 png_error(png_ptr, "Call to NULL write function");
38}
39
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060040#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050041/* This is the function that does the actual writing of data. If you are
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050042 * not writing to a standard C stream, you should create a replacement
43 * write_data function and use it at run time with png_set_write_fn(), rather
44 * than changing the library.
45 */
Guy Schalnate5a37791996-06-05 15:50:50 -050046#ifndef USE_FAR_KEYWORD
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050047void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050048png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050049{
50 png_uint_32 check;
51
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050052 if (png_ptr == NULL)
53 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050054 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
Guy Schalnate5a37791996-06-05 15:50:50 -050055 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050056 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050057}
58#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050059/* This is the model-independent version. Since the standard I/O library
60 * can't handle far buffers in the medium and small models, we have to copy
61 * the data.
62 */
Guy Schalnate5a37791996-06-05 15:50:50 -050063
64#define NEAR_BUF_SIZE 1024
65#define MIN(a,b) (a <= b ? a : b)
66
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050067void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050068png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050069{
70 png_uint_32 check;
Andreas Dilger47a0c421997-05-16 02:46:07 -050071 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050072 png_FILE_p io_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050073
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050074 if (png_ptr == NULL)
75 return;
Guy Schalnate5a37791996-06-05 15:50:50 -050076 /* Check if data really is near. If so, use usual code. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050077 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050078 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 if ((png_bytep)near_data == data)
Guy Schalnate5a37791996-06-05 15:50:50 -050080 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050081 check = fwrite(near_data, 1, length, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050082 }
83 else
84 {
85 png_byte buf[NEAR_BUF_SIZE];
86 png_size_t written, remaining, err;
87 check = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 remaining = length;
Guy Schalnate5a37791996-06-05 15:50:50 -050089 do
90 {
91 written = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050092 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060093 err = fwrite(buf, 1, written, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050094 if (err != written)
95 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050096
Guy Schalnate5a37791996-06-05 15:50:50 -050097 else
98 check += err;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050099
Guy Schalnate5a37791996-06-05 15:50:50 -0500100 data += written;
101 remaining -= written;
102 }
103 while (remaining != 0);
104 }
105 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -0500106 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -0500107}
108
109#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600110#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500111
112/* This function is called to output any data pending writing (normally
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500113 * to disk). After png_flush is called, there should be no data pending
114 * writing in any buffers.
115 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500116#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500117void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500118png_flush(png_structp png_ptr)
119{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 if (png_ptr->output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500121 (*(png_ptr->output_flush_fn))(png_ptr);
122}
123
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600124#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500125void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500126png_default_flush(png_structp png_ptr)
127{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500128 png_FILE_p io_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 if (png_ptr == NULL)
130 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500131 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500132 fflush(io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500133}
134#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600135#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500136
137/* This function allows the application to supply new output functions for
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500138 * libpng if standard C streams aren't being used.
139 *
140 * This function takes as its arguments:
141 * png_ptr - pointer to a png output data structure
142 * io_ptr - pointer to user supplied structure containing info about
143 * the output functions. May be NULL.
144 * write_data_fn - pointer to a new output function that takes as its
145 * arguments a pointer to a png_struct, a pointer to
146 * data to be written, and a 32-bit unsigned int that is
147 * the number of bytes to be written. The new write
148 * function should call png_error(png_ptr, "Error msg")
149 * to exit and output any fatal error messages. May be
150 * NULL, in which case libpng's default function will
151 * be used.
152 * flush_data_fn - pointer to a new flush function that takes as its
153 * arguments a pointer to a png_struct. After a call to
154 * the flush function, there should be no data in any buffers
155 * or pending transmission. If the output method doesn't do
156 * any buffering of ouput, a function prototype must still be
157 * supplied although it doesn't have to do anything. If
158 * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
159 * time, output_flush_fn will be ignored, although it must be
160 * supplied for compatibility. May be NULL, in which case
161 * libpng's default function will be used, if
162 * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
163 * a good idea if io_ptr does not point to a standard
164 * *FILE structure.
165 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500166void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500167png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
168 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
169{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500170 if (png_ptr == NULL)
171 return;
172
Guy Schalnate5a37791996-06-05 15:50:50 -0500173 png_ptr->io_ptr = io_ptr;
174
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600175#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 if (write_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 png_ptr->write_data_fn = write_data_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500178
Guy Schalnate5a37791996-06-05 15:50:50 -0500179 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500180 png_ptr->write_data_fn = png_default_write_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600181#else
182 png_ptr->write_data_fn = write_data_fn;
183#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500184
185#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600186#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 if (output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500188 png_ptr->output_flush_fn = output_flush_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189
Guy Schalnate5a37791996-06-05 15:50:50 -0500190 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500191 png_ptr->output_flush_fn = png_default_flush;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600192#else
193 png_ptr->output_flush_fn = output_flush_fn;
194#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500195#endif /* PNG_WRITE_FLUSH_SUPPORTED */
196
197 /* It is an error to read while writing a png file */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500198 if (png_ptr->read_data_fn != NULL)
199 {
200 png_ptr->read_data_fn = NULL;
201 png_warning(png_ptr,
202 "Attempted to set both read_data_fn and write_data_fn in");
203 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500204 "the same structure. Resetting read_data_fn to NULL");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500205 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500206}
207
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600208#if defined(USE_FAR_KEYWORD)
209#if defined(_MSC_VER)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500210void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600211{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600212 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600213 void FAR *far_ptr;
214 FP_OFF(near_ptr) = FP_OFF(ptr);
215 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500216
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500217 if (check != 0)
218 if (FP_SEG(ptr) != FP_SEG(far_ptr))
219 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500220
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221 return(near_ptr);
222}
223# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500224void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600226 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 void FAR *far_ptr;
228 near_ptr = (void FAR *)ptr;
229 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500230
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500231 if (check != 0)
232 if (far_ptr != ptr)
233 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500234
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 return(near_ptr);
236}
237# endif
238# endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500239#endif /* PNG_WRITE_SUPPORTED */