equal
deleted
inserted
replaced
1 |
1 |
2 /* pngwio.c - functions for data output |
2 /* pngwio.c - functions for data output |
3 * |
3 * |
4 * Last changed in libpng 1.2.37 [June 4, 2009] |
4 * Last changed in libpng 1.4.0 [January 3, 2010] |
5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson |
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
8 * |
8 * |
9 * This code is released under the libpng license. |
9 * This code is released under the libpng license. |
10 * For conditions of distribution and use, see the disclaimer |
10 * For conditions of distribution and use, see the disclaimer |
16 * use different output methods. Note that you shouldn't change these |
16 * use different output methods. Note that you shouldn't change these |
17 * functions, but rather write replacement functions and then change |
17 * functions, but rather write replacement functions and then change |
18 * them at run time with png_set_write_fn(...). |
18 * them at run time with png_set_write_fn(...). |
19 */ |
19 */ |
20 |
20 |
21 #define PNG_INTERNAL |
21 #define PNG_NO_PEDANTIC_WARNINGS |
22 #include "png.h" |
22 #include "png.h" |
23 #ifdef PNG_WRITE_SUPPORTED |
23 #ifdef PNG_WRITE_SUPPORTED |
|
24 #include "pngpriv.h" |
24 |
25 |
25 /* Write the data to whatever output you are using. The default routine |
26 /* Write the data to whatever output you are using. The default routine |
26 * writes to a file pointer. Note that this routine sometimes gets called |
27 * 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 * 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 * buffering if you are using unbuffered writes. This should never be asked |
36 (*(png_ptr->write_data_fn))(png_ptr, data, length); |
37 (*(png_ptr->write_data_fn))(png_ptr, data, length); |
37 else |
38 else |
38 png_error(png_ptr, "Call to NULL write function"); |
39 png_error(png_ptr, "Call to NULL write function"); |
39 } |
40 } |
40 |
41 |
41 #if !defined(PNG_NO_STDIO) |
42 #ifdef PNG_STDIO_SUPPORTED |
42 /* This is the function that does the actual writing of data. If you are |
43 /* This is the function that does the actual writing of data. If you are |
43 * not writing to a standard C stream, you should create a replacement |
44 * 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 * write_data function and use it at run time with png_set_write_fn(), rather |
45 * than changing the library. |
46 * than changing the library. |
46 */ |
47 */ |
50 { |
51 { |
51 png_uint_32 check; |
52 png_uint_32 check; |
52 |
53 |
53 if (png_ptr == NULL) |
54 if (png_ptr == NULL) |
54 return; |
55 return; |
55 #if defined(_WIN32_WCE) |
|
56 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) |
|
57 check = 0; |
|
58 #else |
|
59 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); |
56 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); |
60 #endif |
|
61 if (check != length) |
57 if (check != length) |
62 png_error(png_ptr, "Write Error"); |
58 png_error(png_ptr, "Write Error"); |
63 } |
59 } |
64 #else |
60 #else |
65 /* This is the model-independent version. Since the standard I/O library |
61 /* This is the model-independent version. Since the standard I/O library |
82 /* Check if data really is near. If so, use usual code. */ |
78 /* Check if data really is near. If so, use usual code. */ |
83 near_data = (png_byte *)CVT_PTR_NOCHECK(data); |
79 near_data = (png_byte *)CVT_PTR_NOCHECK(data); |
84 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); |
80 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); |
85 if ((png_bytep)near_data == data) |
81 if ((png_bytep)near_data == data) |
86 { |
82 { |
87 #if defined(_WIN32_WCE) |
|
88 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) |
|
89 check = 0; |
|
90 #else |
|
91 check = fwrite(near_data, 1, length, io_ptr); |
83 check = fwrite(near_data, 1, length, io_ptr); |
92 #endif |
|
93 } |
84 } |
94 else |
85 else |
95 { |
86 { |
96 png_byte buf[NEAR_BUF_SIZE]; |
87 png_byte buf[NEAR_BUF_SIZE]; |
97 png_size_t written, remaining, err; |
88 png_size_t written, remaining, err; |
99 remaining = length; |
90 remaining = length; |
100 do |
91 do |
101 { |
92 { |
102 written = MIN(NEAR_BUF_SIZE, remaining); |
93 written = MIN(NEAR_BUF_SIZE, remaining); |
103 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ |
94 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ |
104 #if defined(_WIN32_WCE) |
|
105 if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) |
|
106 err = 0; |
|
107 #else |
|
108 err = fwrite(buf, 1, written, io_ptr); |
95 err = fwrite(buf, 1, written, io_ptr); |
109 #endif |
|
110 if (err != written) |
96 if (err != written) |
111 break; |
97 break; |
112 |
98 |
113 else |
99 else |
114 check += err; |
100 check += err; |
127 |
113 |
128 /* This function is called to output any data pending writing (normally |
114 /* This function is called to output any data pending writing (normally |
129 * to disk). After png_flush is called, there should be no data pending |
115 * to disk). After png_flush is called, there should be no data pending |
130 * writing in any buffers. |
116 * writing in any buffers. |
131 */ |
117 */ |
132 #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
118 #ifdef PNG_WRITE_FLUSH_SUPPORTED |
133 void /* PRIVATE */ |
119 void /* PRIVATE */ |
134 png_flush(png_structp png_ptr) |
120 png_flush(png_structp png_ptr) |
135 { |
121 { |
136 if (png_ptr->output_flush_fn != NULL) |
122 if (png_ptr->output_flush_fn != NULL) |
137 (*(png_ptr->output_flush_fn))(png_ptr); |
123 (*(png_ptr->output_flush_fn))(png_ptr); |
138 } |
124 } |
139 |
125 |
140 #if !defined(PNG_NO_STDIO) |
126 #ifdef PNG_STDIO_SUPPORTED |
141 void PNGAPI |
127 void PNGAPI |
142 png_default_flush(png_structp png_ptr) |
128 png_default_flush(png_structp png_ptr) |
143 { |
129 { |
144 #if !defined(_WIN32_WCE) |
|
145 png_FILE_p io_ptr; |
130 png_FILE_p io_ptr; |
146 #endif |
131 if (png_ptr == NULL) |
147 if (png_ptr == NULL) |
132 return; |
148 return; |
|
149 #if !defined(_WIN32_WCE) |
|
150 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); |
133 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); |
151 fflush(io_ptr); |
134 fflush(io_ptr); |
152 #endif |
|
153 } |
135 } |
154 #endif |
136 #endif |
155 #endif |
137 #endif |
156 |
138 |
157 /* This function allows the application to supply new output functions for |
139 /* This function allows the application to supply new output functions for |
190 if (png_ptr == NULL) |
172 if (png_ptr == NULL) |
191 return; |
173 return; |
192 |
174 |
193 png_ptr->io_ptr = io_ptr; |
175 png_ptr->io_ptr = io_ptr; |
194 |
176 |
195 #if !defined(PNG_NO_STDIO) |
177 #ifdef PNG_STDIO_SUPPORTED |
196 if (write_data_fn != NULL) |
178 if (write_data_fn != NULL) |
197 png_ptr->write_data_fn = write_data_fn; |
179 png_ptr->write_data_fn = write_data_fn; |
198 |
180 |
199 else |
181 else |
200 png_ptr->write_data_fn = png_default_write_data; |
182 png_ptr->write_data_fn = png_default_write_data; |
201 #else |
183 #else |
202 png_ptr->write_data_fn = write_data_fn; |
184 png_ptr->write_data_fn = write_data_fn; |
203 #endif |
185 #endif |
204 |
186 |
205 #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
187 #ifdef PNG_WRITE_FLUSH_SUPPORTED |
206 #if !defined(PNG_NO_STDIO) |
188 #ifdef PNG_STDIO_SUPPORTED |
207 if (output_flush_fn != NULL) |
189 if (output_flush_fn != NULL) |
208 png_ptr->output_flush_fn = output_flush_fn; |
190 png_ptr->output_flush_fn = output_flush_fn; |
209 |
191 |
210 else |
192 else |
211 png_ptr->output_flush_fn = png_default_flush; |
193 png_ptr->output_flush_fn = png_default_flush; |
219 { |
201 { |
220 png_ptr->read_data_fn = NULL; |
202 png_ptr->read_data_fn = NULL; |
221 png_warning(png_ptr, |
203 png_warning(png_ptr, |
222 "Attempted to set both read_data_fn and write_data_fn in"); |
204 "Attempted to set both read_data_fn and write_data_fn in"); |
223 png_warning(png_ptr, |
205 png_warning(png_ptr, |
224 "the same structure. Resetting read_data_fn to NULL."); |
206 "the same structure. Resetting read_data_fn to NULL"); |
225 } |
207 } |
226 } |
208 } |
227 |
209 |
228 #if defined(USE_FAR_KEYWORD) |
210 #ifdef USE_FAR_KEYWORD |
229 #if defined(_MSC_VER) |
211 #ifdef _MSC_VER |
230 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) |
212 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) |
231 { |
213 { |
232 void *near_ptr; |
214 void *near_ptr; |
233 void FAR *far_ptr; |
215 void FAR *far_ptr; |
234 FP_OFF(near_ptr) = FP_OFF(ptr); |
216 FP_OFF(near_ptr) = FP_OFF(ptr); |