rev |
line source |
nuclear@2
|
1
|
nuclear@2
|
2 /* pngwio.c - functions for data output
|
nuclear@2
|
3 *
|
nuclear@2
|
4 * Last changed in libpng 1.2.30 [August 15, 2008]
|
nuclear@2
|
5 * For conditions of distribution and use, see copyright notice in png.h
|
nuclear@2
|
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
|
nuclear@2
|
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
nuclear@2
|
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
nuclear@2
|
9 *
|
nuclear@2
|
10 * This file provides a location for all output. Users who need
|
nuclear@2
|
11 * special handling are expected to write functions that have the same
|
nuclear@2
|
12 * arguments as these and perform similar functions, but that possibly
|
nuclear@2
|
13 * use different output methods. Note that you shouldn't change these
|
nuclear@2
|
14 * functions, but rather write replacement functions and then change
|
nuclear@2
|
15 * them at run time with png_set_write_fn(...).
|
nuclear@2
|
16 */
|
nuclear@2
|
17
|
nuclear@2
|
18 #define PNG_INTERNAL
|
nuclear@2
|
19 #include "png.h"
|
nuclear@2
|
20 #ifdef PNG_WRITE_SUPPORTED
|
nuclear@2
|
21
|
nuclear@2
|
22 /* Write the data to whatever output you are using. The default routine
|
nuclear@2
|
23 writes to a file pointer. Note that this routine sometimes gets called
|
nuclear@2
|
24 with very small lengths, so you should implement some kind of simple
|
nuclear@2
|
25 buffering if you are using unbuffered writes. This should never be asked
|
nuclear@2
|
26 to write more than 64K on a 16 bit machine. */
|
nuclear@2
|
27
|
nuclear@2
|
28 void /* PRIVATE */
|
nuclear@2
|
29 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
nuclear@2
|
30 {
|
nuclear@2
|
31 if (png_ptr->write_data_fn != NULL )
|
nuclear@2
|
32 (*(png_ptr->write_data_fn))(png_ptr, data, length);
|
nuclear@2
|
33 else
|
nuclear@2
|
34 png_error(png_ptr, "Call to NULL write function");
|
nuclear@2
|
35 }
|
nuclear@2
|
36
|
nuclear@2
|
37 #if !defined(PNG_NO_STDIO)
|
nuclear@2
|
38 /* This is the function that does the actual writing of data. If you are
|
nuclear@2
|
39 not writing to a standard C stream, you should create a replacement
|
nuclear@2
|
40 write_data function and use it at run time with png_set_write_fn(), rather
|
nuclear@2
|
41 than changing the library. */
|
nuclear@2
|
42 #ifndef USE_FAR_KEYWORD
|
nuclear@2
|
43 void PNGAPI
|
nuclear@2
|
44 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
nuclear@2
|
45 {
|
nuclear@2
|
46 png_uint_32 check;
|
nuclear@2
|
47
|
nuclear@2
|
48 if (png_ptr == NULL) return;
|
nuclear@2
|
49 #if defined(_WIN32_WCE)
|
nuclear@2
|
50 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
|
nuclear@2
|
51 check = 0;
|
nuclear@2
|
52 #else
|
nuclear@2
|
53 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
|
nuclear@2
|
54 #endif
|
nuclear@2
|
55 if (check != length)
|
nuclear@2
|
56 png_error(png_ptr, "Write Error");
|
nuclear@2
|
57 }
|
nuclear@2
|
58 #else
|
nuclear@2
|
59 /* this is the model-independent version. Since the standard I/O library
|
nuclear@2
|
60 can't handle far buffers in the medium and small models, we have to copy
|
nuclear@2
|
61 the data.
|
nuclear@2
|
62 */
|
nuclear@2
|
63
|
nuclear@2
|
64 #define NEAR_BUF_SIZE 1024
|
nuclear@2
|
65 #define MIN(a,b) (a <= b ? a : b)
|
nuclear@2
|
66
|
nuclear@2
|
67 void PNGAPI
|
nuclear@2
|
68 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
nuclear@2
|
69 {
|
nuclear@2
|
70 png_uint_32 check;
|
nuclear@2
|
71 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
|
nuclear@2
|
72 png_FILE_p io_ptr;
|
nuclear@2
|
73
|
nuclear@2
|
74 if (png_ptr == NULL) return;
|
nuclear@2
|
75 /* Check if data really is near. If so, use usual code. */
|
nuclear@2
|
76 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
|
nuclear@2
|
77 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
|
nuclear@2
|
78 if ((png_bytep)near_data == data)
|
nuclear@2
|
79 {
|
nuclear@2
|
80 #if defined(_WIN32_WCE)
|
nuclear@2
|
81 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
|
nuclear@2
|
82 check = 0;
|
nuclear@2
|
83 #else
|
nuclear@2
|
84 check = fwrite(near_data, 1, length, io_ptr);
|
nuclear@2
|
85 #endif
|
nuclear@2
|
86 }
|
nuclear@2
|
87 else
|
nuclear@2
|
88 {
|
nuclear@2
|
89 png_byte buf[NEAR_BUF_SIZE];
|
nuclear@2
|
90 png_size_t written, remaining, err;
|
nuclear@2
|
91 check = 0;
|
nuclear@2
|
92 remaining = length;
|
nuclear@2
|
93 do
|
nuclear@2
|
94 {
|
nuclear@2
|
95 written = MIN(NEAR_BUF_SIZE, remaining);
|
nuclear@2
|
96 png_memcpy(buf, data, written); /* copy far buffer to near buffer */
|
nuclear@2
|
97 #if defined(_WIN32_WCE)
|
nuclear@2
|
98 if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
|
nuclear@2
|
99 err = 0;
|
nuclear@2
|
100 #else
|
nuclear@2
|
101 err = fwrite(buf, 1, written, io_ptr);
|
nuclear@2
|
102 #endif
|
nuclear@2
|
103 if (err != written)
|
nuclear@2
|
104 break;
|
nuclear@2
|
105 else
|
nuclear@2
|
106 check += err;
|
nuclear@2
|
107 data += written;
|
nuclear@2
|
108 remaining -= written;
|
nuclear@2
|
109 }
|
nuclear@2
|
110 while (remaining != 0);
|
nuclear@2
|
111 }
|
nuclear@2
|
112 if (check != length)
|
nuclear@2
|
113 png_error(png_ptr, "Write Error");
|
nuclear@2
|
114 }
|
nuclear@2
|
115
|
nuclear@2
|
116 #endif
|
nuclear@2
|
117 #endif
|
nuclear@2
|
118
|
nuclear@2
|
119 /* This function is called to output any data pending writing (normally
|
nuclear@2
|
120 to disk). After png_flush is called, there should be no data pending
|
nuclear@2
|
121 writing in any buffers. */
|
nuclear@2
|
122 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
|
nuclear@2
|
123 void /* PRIVATE */
|
nuclear@2
|
124 png_flush(png_structp png_ptr)
|
nuclear@2
|
125 {
|
nuclear@2
|
126 if (png_ptr->output_flush_fn != NULL)
|
nuclear@2
|
127 (*(png_ptr->output_flush_fn))(png_ptr);
|
nuclear@2
|
128 }
|
nuclear@2
|
129
|
nuclear@2
|
130 #if !defined(PNG_NO_STDIO)
|
nuclear@2
|
131 void PNGAPI
|
nuclear@2
|
132 png_default_flush(png_structp png_ptr)
|
nuclear@2
|
133 {
|
nuclear@2
|
134 #if !defined(_WIN32_WCE)
|
nuclear@2
|
135 png_FILE_p io_ptr;
|
nuclear@2
|
136 #endif
|
nuclear@2
|
137 if (png_ptr == NULL) return;
|
nuclear@2
|
138 #if !defined(_WIN32_WCE)
|
nuclear@2
|
139 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
|
nuclear@2
|
140 if (io_ptr != NULL)
|
nuclear@2
|
141 fflush(io_ptr);
|
nuclear@2
|
142 #endif
|
nuclear@2
|
143 }
|
nuclear@2
|
144 #endif
|
nuclear@2
|
145 #endif
|
nuclear@2
|
146
|
nuclear@2
|
147 /* This function allows the application to supply new output functions for
|
nuclear@2
|
148 libpng if standard C streams aren't being used.
|
nuclear@2
|
149
|
nuclear@2
|
150 This function takes as its arguments:
|
nuclear@2
|
151 png_ptr - pointer to a png output data structure
|
nuclear@2
|
152 io_ptr - pointer to user supplied structure containing info about
|
nuclear@2
|
153 the output functions. May be NULL.
|
nuclear@2
|
154 write_data_fn - pointer to a new output function that takes as its
|
nuclear@2
|
155 arguments a pointer to a png_struct, a pointer to
|
nuclear@2
|
156 data to be written, and a 32-bit unsigned int that is
|
nuclear@2
|
157 the number of bytes to be written. The new write
|
nuclear@2
|
158 function should call png_error(png_ptr, "Error msg")
|
nuclear@2
|
159 to exit and output any fatal error messages.
|
nuclear@2
|
160 flush_data_fn - pointer to a new flush function that takes as its
|
nuclear@2
|
161 arguments a pointer to a png_struct. After a call to
|
nuclear@2
|
162 the flush function, there should be no data in any buffers
|
nuclear@2
|
163 or pending transmission. If the output method doesn't do
|
nuclear@2
|
164 any buffering of ouput, a function prototype must still be
|
nuclear@2
|
165 supplied although it doesn't have to do anything. If
|
nuclear@2
|
166 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
|
nuclear@2
|
167 time, output_flush_fn will be ignored, although it must be
|
nuclear@2
|
168 supplied for compatibility. */
|
nuclear@2
|
169 void PNGAPI
|
nuclear@2
|
170 png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
|
nuclear@2
|
171 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
|
nuclear@2
|
172 {
|
nuclear@2
|
173 if (png_ptr == NULL) return;
|
nuclear@2
|
174 png_ptr->io_ptr = io_ptr;
|
nuclear@2
|
175
|
nuclear@2
|
176 #if !defined(PNG_NO_STDIO)
|
nuclear@2
|
177 if (write_data_fn != NULL)
|
nuclear@2
|
178 png_ptr->write_data_fn = write_data_fn;
|
nuclear@2
|
179 else
|
nuclear@2
|
180 png_ptr->write_data_fn = png_default_write_data;
|
nuclear@2
|
181 #else
|
nuclear@2
|
182 png_ptr->write_data_fn = write_data_fn;
|
nuclear@2
|
183 #endif
|
nuclear@2
|
184
|
nuclear@2
|
185 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
|
nuclear@2
|
186 #if !defined(PNG_NO_STDIO)
|
nuclear@2
|
187 if (output_flush_fn != NULL)
|
nuclear@2
|
188 png_ptr->output_flush_fn = output_flush_fn;
|
nuclear@2
|
189 else
|
nuclear@2
|
190 png_ptr->output_flush_fn = png_default_flush;
|
nuclear@2
|
191 #else
|
nuclear@2
|
192 png_ptr->output_flush_fn = output_flush_fn;
|
nuclear@2
|
193 #endif
|
nuclear@2
|
194 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
|
nuclear@2
|
195
|
nuclear@2
|
196 /* It is an error to read while writing a png file */
|
nuclear@2
|
197 if (png_ptr->read_data_fn != NULL)
|
nuclear@2
|
198 {
|
nuclear@2
|
199 png_ptr->read_data_fn = NULL;
|
nuclear@2
|
200 png_warning(png_ptr,
|
nuclear@2
|
201 "Attempted to set both read_data_fn and write_data_fn in");
|
nuclear@2
|
202 png_warning(png_ptr,
|
nuclear@2
|
203 "the same structure. Resetting read_data_fn to NULL.");
|
nuclear@2
|
204 }
|
nuclear@2
|
205 }
|
nuclear@2
|
206
|
nuclear@2
|
207 #if defined(USE_FAR_KEYWORD)
|
nuclear@2
|
208 #if defined(_MSC_VER)
|
nuclear@2
|
209 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
|
nuclear@2
|
210 {
|
nuclear@2
|
211 void *near_ptr;
|
nuclear@2
|
212 void FAR *far_ptr;
|
nuclear@2
|
213 FP_OFF(near_ptr) = FP_OFF(ptr);
|
nuclear@2
|
214 far_ptr = (void FAR *)near_ptr;
|
nuclear@2
|
215 if (check != 0)
|
nuclear@2
|
216 if (FP_SEG(ptr) != FP_SEG(far_ptr))
|
nuclear@2
|
217 png_error(png_ptr, "segment lost in conversion");
|
nuclear@2
|
218 return(near_ptr);
|
nuclear@2
|
219 }
|
nuclear@2
|
220 # else
|
nuclear@2
|
221 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
|
nuclear@2
|
222 {
|
nuclear@2
|
223 void *near_ptr;
|
nuclear@2
|
224 void FAR *far_ptr;
|
nuclear@2
|
225 near_ptr = (void FAR *)ptr;
|
nuclear@2
|
226 far_ptr = (void FAR *)near_ptr;
|
nuclear@2
|
227 if (check != 0)
|
nuclear@2
|
228 if (far_ptr != ptr)
|
nuclear@2
|
229 png_error(png_ptr, "segment lost in conversion");
|
nuclear@2
|
230 return(near_ptr);
|
nuclear@2
|
231 }
|
nuclear@2
|
232 # endif
|
nuclear@2
|
233 # endif
|
nuclear@2
|
234 #endif /* PNG_WRITE_SUPPORTED */
|