blob: 59a2841f979f6e37850488774bd367cd9b41bf8f [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-Pehrsond8eb62f2009-05-30 20:19:20 -05004 * Last changed in libpng 1.4.0 [May 31, 2009]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050010 * This file provides a location for all output. Users who need
11 * special handling are expected to write functions that have the same
12 * arguments as these and perform similar functions, but that possibly
13 * use different output methods. Note that you shouldn't change these
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060014 * functions, but rather write replacement functions and then change
15 * them at run time with png_set_write_fn(...).
16 */
Guy Schalnate5a37791996-06-05 15:50:50 -050017
Guy Schalnate5a37791996-06-05 15:50:50 -050018#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050019#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050020#include "pngpriv.h"
Guy Schalnate5a37791996-06-05 15:50:50 -050021
22/* Write the data to whatever output you are using. The default routine
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050023 * writes to a file pointer. Note that this routine sometimes gets called
24 * with very small lengths, so you should implement some kind of simple
25 * buffering if you are using unbuffered writes. This should never be asked
26 * to write more than 64K on a 16 bit machine.
27 */
Guy Schalnate5a37791996-06-05 15:50:50 -050028
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050029void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050030png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050031{
Andreas Dilger47a0c421997-05-16 02:46:07 -050032 if (png_ptr->write_data_fn != NULL )
Guy Schalnate5a37791996-06-05 15:50:50 -050033 (*(png_ptr->write_data_fn))(png_ptr, data, length);
34 else
35 png_error(png_ptr, "Call to NULL write function");
36}
37
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060038#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050039/* 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 -050040 * not writing to a standard C stream, you should create a replacement
41 * write_data function and use it at run time with png_set_write_fn(), rather
42 * than changing the library.
43 */
Guy Schalnate5a37791996-06-05 15:50:50 -050044#ifndef USE_FAR_KEYWORD
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050045void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050046png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050047{
48 png_uint_32 check;
49
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050050 if (png_ptr == NULL)
51 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050052 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
Guy Schalnate5a37791996-06-05 15:50:50 -050053 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050054 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050055}
56#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050057/* This is the model-independent version. Since the standard I/O library
58 * can't handle far buffers in the medium and small models, we have to copy
59 * the data.
60 */
Guy Schalnate5a37791996-06-05 15:50:50 -050061
62#define NEAR_BUF_SIZE 1024
63#define MIN(a,b) (a <= b ? a : b)
64
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050065void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050066png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050067{
68 png_uint_32 check;
Andreas Dilger47a0c421997-05-16 02:46:07 -050069 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050070 png_FILE_p io_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050071
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050072 if (png_ptr == NULL)
73 return;
Guy Schalnate5a37791996-06-05 15:50:50 -050074 /* Check if data really is near. If so, use usual code. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050075 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050076 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050077 if ((png_bytep)near_data == data)
Guy Schalnate5a37791996-06-05 15:50:50 -050078 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 check = fwrite(near_data, 1, length, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050080 }
81 else
82 {
83 png_byte buf[NEAR_BUF_SIZE];
84 png_size_t written, remaining, err;
85 check = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 remaining = length;
Guy Schalnate5a37791996-06-05 15:50:50 -050087 do
88 {
89 written = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050090 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060091 err = fwrite(buf, 1, written, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050092 if (err != written)
93 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050094
Guy Schalnate5a37791996-06-05 15:50:50 -050095 else
96 check += err;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050097
Guy Schalnate5a37791996-06-05 15:50:50 -050098 data += written;
99 remaining -= written;
100 }
101 while (remaining != 0);
102 }
103 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -0500104 png_error(png_ptr, "Write Error");
Guy Schalnate5a37791996-06-05 15:50:50 -0500105}
106
107#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600108#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500109
110/* This function is called to output any data pending writing (normally
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500111 * to disk). After png_flush is called, there should be no data pending
112 * writing in any buffers.
113 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500114#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500115void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500116png_flush(png_structp png_ptr)
117{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118 if (png_ptr->output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500119 (*(png_ptr->output_flush_fn))(png_ptr);
120}
121
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600122#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500123void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500124png_default_flush(png_structp png_ptr)
125{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500126 png_FILE_p io_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 if (png_ptr == NULL)
128 return;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500129 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500130 fflush(io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500131}
132#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600133#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500134
135/* This function allows the application to supply new output functions for
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500136 * libpng if standard C streams aren't being used.
137 *
138 * This function takes as its arguments:
139 * png_ptr - pointer to a png output data structure
140 * io_ptr - pointer to user supplied structure containing info about
141 * the output functions. May be NULL.
142 * write_data_fn - pointer to a new output function that takes as its
143 * arguments a pointer to a png_struct, a pointer to
144 * data to be written, and a 32-bit unsigned int that is
145 * the number of bytes to be written. The new write
146 * function should call png_error(png_ptr, "Error msg")
147 * to exit and output any fatal error messages. May be
148 * NULL, in which case libpng's default function will
149 * be used.
150 * flush_data_fn - pointer to a new flush function that takes as its
151 * arguments a pointer to a png_struct. After a call to
152 * the flush function, there should be no data in any buffers
153 * or pending transmission. If the output method doesn't do
154 * any buffering of ouput, a function prototype must still be
155 * supplied although it doesn't have to do anything. If
156 * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
157 * time, output_flush_fn will be ignored, although it must be
158 * supplied for compatibility. May be NULL, in which case
159 * libpng's default function will be used, if
160 * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
161 * a good idea if io_ptr does not point to a standard
162 * *FILE structure.
163 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500164void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500165png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
166 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
167{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500168 if (png_ptr == NULL)
169 return;
170
Guy Schalnate5a37791996-06-05 15:50:50 -0500171 png_ptr->io_ptr = io_ptr;
172
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600173#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 if (write_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500175 png_ptr->write_data_fn = write_data_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500176
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500178 png_ptr->write_data_fn = png_default_write_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600179#else
180 png_ptr->write_data_fn = write_data_fn;
181#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500182
183#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600184#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185 if (output_flush_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500186 png_ptr->output_flush_fn = output_flush_fn;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500187
Guy Schalnate5a37791996-06-05 15:50:50 -0500188 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500189 png_ptr->output_flush_fn = png_default_flush;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600190#else
191 png_ptr->output_flush_fn = output_flush_fn;
192#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500193#endif /* PNG_WRITE_FLUSH_SUPPORTED */
194
195 /* It is an error to read while writing a png file */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500196 if (png_ptr->read_data_fn != NULL)
197 {
198 png_ptr->read_data_fn = NULL;
199 png_warning(png_ptr,
200 "Attempted to set both read_data_fn and write_data_fn in");
201 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500202 "the same structure. Resetting read_data_fn to NULL");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500203 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500204}
205
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600206#if defined(USE_FAR_KEYWORD)
207#if defined(_MSC_VER)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500208void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600209{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600210 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600211 void FAR *far_ptr;
212 FP_OFF(near_ptr) = FP_OFF(ptr);
213 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500214
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500215 if (check != 0)
216 if (FP_SEG(ptr) != FP_SEG(far_ptr))
217 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500218
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219 return(near_ptr);
220}
221# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500222void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600223{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600224 void *near_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 void FAR *far_ptr;
226 near_ptr = (void FAR *)ptr;
227 far_ptr = (void FAR *)near_ptr;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500228
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500229 if (check != 0)
230 if (far_ptr != ptr)
231 png_error(png_ptr, "segment lost in conversion");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500232
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600233 return(near_ptr);
234}
235# endif
236# endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500237#endif /* PNG_WRITE_SUPPORTED */