2 /* pngwio.c - functions for data output
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 * 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
14 * functions, but rather write replacement functions and then change
15 * them at run time with png_set_write_fn(...).
20 #ifdef PNG_WRITE_SUPPORTED
22 /* Write the data to whatever output you are using. The default routine
23 * 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.
30 png_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
32 if (png_ptr
->write_data_fn
!= NULL
)
33 (*(png_ptr
->write_data_fn
))(png_ptr
, data
, length
);
35 png_error(png_ptr
, "Call to NULL write function");
38 #if !defined(PNG_NO_STDIO)
39 /* This is the function that does the actual writing of data. If you are
40 * 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.
44 #ifndef USE_FAR_KEYWORD
46 png_default_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
52 #if defined(_WIN32_WCE)
53 if ( !WriteFile((HANDLE
)(png_ptr
->io_ptr
), data
, length
, &check
, NULL
) )
56 check
= fwrite(data
, 1, length
, (png_FILE_p
)(png_ptr
->io_ptr
));
59 png_error(png_ptr
, "Write Error");
62 /* This is the model-independent version. Since the standard I/O library
63 * can't handle far buffers in the medium and small models, we have to copy
67 #define NEAR_BUF_SIZE 1024
68 #define MIN(a,b) (a <= b ? a : b)
71 png_default_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
74 png_byte
*near_data
; /* Needs to be "png_byte *" instead of "png_bytep" */
79 /* Check if data really is near. If so, use usual code. */
80 near_data
= (png_byte
*)CVT_PTR_NOCHECK(data
);
81 io_ptr
= (png_FILE_p
)CVT_PTR(png_ptr
->io_ptr
);
82 if ((png_bytep
)near_data
== data
)
84 #if defined(_WIN32_WCE)
85 if ( !WriteFile(io_ptr
, near_data
, length
, &check
, NULL
) )
88 check
= fwrite(near_data
, 1, length
, io_ptr
);
93 png_byte buf
[NEAR_BUF_SIZE
];
94 png_size_t written
, remaining
, err
;
99 written
= MIN(NEAR_BUF_SIZE
, remaining
);
100 png_memcpy(buf
, data
, written
); /* Copy far buffer to near buffer */
101 #if defined(_WIN32_WCE)
102 if ( !WriteFile(io_ptr
, buf
, written
, &err
, NULL
) )
105 err
= fwrite(buf
, 1, written
, io_ptr
);
114 remaining
-= written
;
116 while (remaining
!= 0);
119 png_error(png_ptr
, "Write Error");
125 /* This function is called to output any data pending writing (normally
126 * to disk). After png_flush is called, there should be no data pending
127 * writing in any buffers.
129 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
131 png_flush(png_structp png_ptr
)
133 if (png_ptr
->output_flush_fn
!= NULL
)
134 (*(png_ptr
->output_flush_fn
))(png_ptr
);
137 #if !defined(PNG_NO_STDIO)
139 png_default_flush(png_structp png_ptr
)
141 #if !defined(_WIN32_WCE)
146 #if !defined(_WIN32_WCE)
147 io_ptr
= (png_FILE_p
)CVT_PTR((png_ptr
->io_ptr
));
154 /* This function allows the application to supply new output functions for
155 * libpng if standard C streams aren't being used.
157 * This function takes as its arguments:
158 * png_ptr - pointer to a png output data structure
159 * io_ptr - pointer to user supplied structure containing info about
160 * the output functions. May be NULL.
161 * write_data_fn - pointer to a new output function that takes as its
162 * arguments a pointer to a png_struct, a pointer to
163 * data to be written, and a 32-bit unsigned int that is
164 * the number of bytes to be written. The new write
165 * function should call png_error(png_ptr, "Error msg")
166 * to exit and output any fatal error messages. May be
167 * NULL, in which case libpng's default function will
169 * flush_data_fn - pointer to a new flush function that takes as its
170 * arguments a pointer to a png_struct. After a call to
171 * the flush function, there should be no data in any buffers
172 * or pending transmission. If the output method doesn't do
173 * any buffering of ouput, a function prototype must still be
174 * supplied although it doesn't have to do anything. If
175 * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
176 * time, output_flush_fn will be ignored, although it must be
177 * supplied for compatibility. May be NULL, in which case
178 * libpng's default function will be used, if
179 * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
180 * a good idea if io_ptr does not point to a standard
184 png_set_write_fn(png_structp png_ptr
, png_voidp io_ptr
,
185 png_rw_ptr write_data_fn
, png_flush_ptr output_flush_fn
)
190 png_ptr
->io_ptr
= io_ptr
;
192 #if !defined(PNG_NO_STDIO)
193 if (write_data_fn
!= NULL
)
194 png_ptr
->write_data_fn
= write_data_fn
;
197 png_ptr
->write_data_fn
= png_default_write_data
;
199 png_ptr
->write_data_fn
= write_data_fn
;
202 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
203 #if !defined(PNG_NO_STDIO)
204 if (output_flush_fn
!= NULL
)
205 png_ptr
->output_flush_fn
= output_flush_fn
;
208 png_ptr
->output_flush_fn
= png_default_flush
;
210 png_ptr
->output_flush_fn
= output_flush_fn
;
212 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
214 /* It is an error to read while writing a png file */
215 if (png_ptr
->read_data_fn
!= NULL
)
217 png_ptr
->read_data_fn
= NULL
;
219 "Attempted to set both read_data_fn and write_data_fn in");
221 "the same structure. Resetting read_data_fn to NULL.");
225 #if defined(USE_FAR_KEYWORD)
226 #if defined(_MSC_VER)
227 void *png_far_to_near(png_structp png_ptr
, png_voidp ptr
, int check
)
231 FP_OFF(near_ptr
) = FP_OFF(ptr
);
232 far_ptr
= (void FAR
*)near_ptr
;
235 if (FP_SEG(ptr
) != FP_SEG(far_ptr
))
236 png_error(png_ptr
, "segment lost in conversion");
241 void *png_far_to_near(png_structp png_ptr
, png_voidp ptr
, int check
)
245 near_ptr
= (void FAR
*)ptr
;
246 far_ptr
= (void FAR
*)near_ptr
;
250 png_error(png_ptr
, "segment lost in conversion");
256 #endif /* PNG_WRITE_SUPPORTED */