blob: e3d48a90b9b889980462483f604d5d1b419c2968 [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-Pehrsondbd40142009-08-31 08:42:02 -05004 * Last changed in libpng 1.4.0 [August 31, 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-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050013 * This file provides a location for all output. Users who need
14 * special handling are expected to write functions that have the same
15 * arguments as these and perform similar functions, but that possibly
16 * use different output methods. Note that you shouldn't change these
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060017 * functions, but rather write replacement functions and then change
18 * them at run time with png_set_write_fn(...).
19 */
Guy Schalnate5a37791996-06-05 15:50:50 -050020
Guy Schalnate5a37791996-06-05 15:50:50 -050021#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050022#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050023#include "pngpriv.h"
Guy Schalnate5a37791996-06-05 15:50:50 -050024
25/* Write the data to whatever output you are using. The default routine
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050026 * writes to a file pointer. Note that this routine sometimes gets called
27 * with very small lengths, so you should implement some kind of simple
28 * buffering if you are using unbuffered writes. This should never be asked
29 * to write more than 64K on a 16 bit machine.
30 */
Guy Schalnate5a37791996-06-05 15:50:50 -050031
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050034{
Andreas Dilger47a0c421997-05-16 02:46:07 -050035 if (png_ptr->write_data_fn != NULL )
Guy Schalnate5a37791996-06-05 15:50:50 -050036 (*(png_ptr->write_data_fn))(png_ptr, data, length);
37 else
38 png_error(png_ptr, "Call to NULL write function");
39}
40
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050041#ifdef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050042/* 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 -050043 * not writing to a standard C stream, you should create a replacement
44 * write_data function and use it at run time with png_set_write_fn(), rather
45 * than changing the library.
46 */
Guy Schalnate5a37791996-06-05 15:50:50 -050047#ifndef USE_FAR_KEYWORD
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050048void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050049png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050050{
51 png_uint_32 check;
52
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050053 if (png_ptr == NULL)
54 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050055 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
Guy Schalnate5a37791996-06-05 15:50:50 -050056 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050057 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050058}
59#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050060/* This is the model-independent version. Since the standard I/O library
61 * can't handle far buffers in the medium and small models, we have to copy
62 * the data.
63 */
Guy Schalnate5a37791996-06-05 15:50:50 -050064
65#define NEAR_BUF_SIZE 1024
66#define MIN(a,b) (a <= b ? a : b)
67
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050068void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050069png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050070{
71 png_uint_32 check;
Andreas Dilger47a0c421997-05-16 02:46:07 -050072 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050073 png_FILE_p io_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050074
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050075 if (png_ptr == NULL)
76 return;
Guy Schalnate5a37791996-06-05 15:50:50 -050077 /* Check if data really is near. If so, use usual code. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050079 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 if ((png_bytep)near_data == data)
Guy Schalnate5a37791996-06-05 15:50:50 -050081 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 check = fwrite(near_data, 1, length, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050083 }
84 else
85 {
86 png_byte buf[NEAR_BUF_SIZE];
87 png_size_t written, remaining, err;
88 check = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050089 remaining = length;
Guy Schalnate5a37791996-06-05 15:50:50 -050090 do
91 {
92 written = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050093 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060094 err = fwrite(buf, 1, written, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050095 if (err != written)
96 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050097
Guy Schalnate5a37791996-06-05 15:50:50 -050098 else
99 check += err;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500100
Guy Schalnate5a37791996-06-05 15:50:50 -0500101 data += written;
102 remaining -= written;
103 }
104 while (remaining != 0);
105 }
106 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -0500107 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -0500108}
109
110#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600111#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500112
113/* This function is called to output any data pending writing (normally
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500114 * to disk). After png_flush is called, there should be no data pending
115 * writing in any buffers.
116 */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500117#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500118void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500119png_flush(png_structp png_ptr)
120{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121 if (png_ptr->output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500122 (*(png_ptr->output_flush_fn))(png_ptr);
123}
124
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500125#ifdef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500126void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500127png_default_flush(png_structp png_ptr)
128{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500129 png_FILE_p io_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 if (png_ptr == NULL)
131 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500132 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500133 fflush(io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500134}
135#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600136#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500137
138/* This function allows the application to supply new output functions for
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500139 * libpng if standard C streams aren't being used.
140 *
141 * This function takes as its arguments:
142 * png_ptr - pointer to a png output data structure
143 * io_ptr - pointer to user supplied structure containing info about
144 * the output functions. May be NULL.
145 * write_data_fn - pointer to a new output function that takes as its
146 * arguments a pointer to a png_struct, a pointer to
147 * data to be written, and a 32-bit unsigned int that is
148 * the number of bytes to be written. The new write
149 * function should call png_error(png_ptr, "Error msg")
150 * to exit and output any fatal error messages. May be
151 * NULL, in which case libpng's default function will
152 * be used.
153 * flush_data_fn - pointer to a new flush function that takes as its
154 * arguments a pointer to a png_struct. After a call to
155 * the flush function, there should be no data in any buffers
156 * or pending transmission. If the output method doesn't do
157 * any buffering of ouput, a function prototype must still be
158 * supplied although it doesn't have to do anything. If
159 * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
160 * time, output_flush_fn will be ignored, although it must be
161 * supplied for compatibility. May be NULL, in which case
162 * libpng's default function will be used, if
163 * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
164 * a good idea if io_ptr does not point to a standard
165 * *FILE structure.
166 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500167void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500168png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
169 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
170{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500171 if (png_ptr == NULL)
172 return;
173
Guy Schalnate5a37791996-06-05 15:50:50 -0500174 png_ptr->io_ptr = io_ptr;
175
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500176#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 if (write_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 png_ptr->write_data_fn = write_data_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500179
Guy Schalnate5a37791996-06-05 15:50:50 -0500180 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500181 png_ptr->write_data_fn = png_default_write_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600182#else
183 png_ptr->write_data_fn = write_data_fn;
184#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500185
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500186#ifdef PNG_WRITE_FLUSH_SUPPORTED
187#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 if (output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500189 png_ptr->output_flush_fn = output_flush_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500190
Guy Schalnate5a37791996-06-05 15:50:50 -0500191 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500192 png_ptr->output_flush_fn = png_default_flush;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600193#else
194 png_ptr->output_flush_fn = output_flush_fn;
195#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500196#endif /* PNG_WRITE_FLUSH_SUPPORTED */
197
198 /* It is an error to read while writing a png file */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500199 if (png_ptr->read_data_fn != NULL)
200 {
201 png_ptr->read_data_fn = NULL;
202 png_warning(png_ptr,
203 "Attempted to set both read_data_fn and write_data_fn in");
204 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500205 "the same structure. Resetting read_data_fn to NULL");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500206 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500207}
208
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500209#ifdef USE_FAR_KEYWORD
210#ifdef _MSC_VER
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500211void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600212{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600213 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 void FAR *far_ptr;
215 FP_OFF(near_ptr) = FP_OFF(ptr);
216 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500217
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500218 if (check != 0)
219 if (FP_SEG(ptr) != FP_SEG(far_ptr))
220 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500221
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600222 return(near_ptr);
223}
224# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500225void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600227 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228 void FAR *far_ptr;
229 near_ptr = (void FAR *)ptr;
230 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500231
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500232 if (check != 0)
233 if (far_ptr != ptr)
234 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500235
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 return(near_ptr);
237}
238# endif
239# endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500240#endif /* PNG_WRITE_SUPPORTED */