| OLD | NEW |
| 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.2.41 [December 3, 2009] |
| 5 * For conditions of distribution and use, see copyright notice in png.h | |
| 6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson |
| 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 * (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.) |
| 9 * | 8 * |
| 9 * This code is released under the libpng license. |
| 10 * For conditions of distribution and use, see the disclaimer |
| 11 * and license in png.h |
| 12 * |
| 10 * This file provides a location for all output. Users who need | 13 * This file provides a location for all output. Users who need |
| 11 * special handling are expected to write functions that have the same | 14 * special handling are expected to write functions that have the same |
| 12 * arguments as these and perform similar functions, but that possibly | 15 * arguments as these and perform similar functions, but that possibly |
| 13 * use different output methods. Note that you shouldn't change these | 16 * use different output methods. Note that you shouldn't change these |
| 14 * functions, but rather write replacement functions and then change | 17 * functions, but rather write replacement functions and then change |
| 15 * them at run time with png_set_write_fn(...). | 18 * them at run time with png_set_write_fn(...). |
| 16 */ | 19 */ |
| 17 | 20 |
| 18 #define PNG_INTERNAL | 21 #define PNG_INTERNAL |
| 22 #define PNG_NO_PEDANTIC_WARNINGS |
| 19 #include "png.h" | 23 #include "png.h" |
| 20 #ifdef PNG_WRITE_SUPPORTED | 24 #ifdef PNG_WRITE_SUPPORTED |
| 21 | 25 |
| 22 /* 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 |
| 23 * 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 |
| 24 * 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 |
| 25 * 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 |
| 26 * to write more than 64K on a 16 bit machine. | 30 * to write more than 64K on a 16 bit machine. |
| 27 */ | 31 */ |
| 28 | 32 |
| 29 void /* PRIVATE */ | 33 void /* PRIVATE */ |
| 30 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) | 34 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 31 { | 35 { |
| 32 if (png_ptr->write_data_fn != NULL ) | 36 if (png_ptr->write_data_fn != NULL ) |
| 33 (*(png_ptr->write_data_fn))(png_ptr, data, length); | 37 (*(png_ptr->write_data_fn))(png_ptr, data, length); |
| 34 else | 38 else |
| 35 png_error(png_ptr, "Call to NULL write function"); | 39 png_error(png_ptr, "Call to NULL write function"); |
| 36 } | 40 } |
| 37 | 41 |
| 38 #if !defined(PNG_NO_STDIO) | 42 #ifdef PNG_STDIO_SUPPORTED |
| 39 /* 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 |
| 40 * 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 |
| 41 * 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 |
| 42 * than changing the library. | 46 * than changing the library. |
| 43 */ | 47 */ |
| 44 #ifndef USE_FAR_KEYWORD | 48 #ifndef USE_FAR_KEYWORD |
| 45 void PNGAPI | 49 void PNGAPI |
| 46 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) | 50 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 47 { | 51 { |
| 48 png_uint_32 check; | 52 png_uint_32 check; |
| 49 | 53 |
| 50 if (png_ptr == NULL) | 54 if (png_ptr == NULL) |
| 51 return; | 55 return; |
| 52 #if defined(_WIN32_WCE) | 56 #ifdef _WIN32_WCE |
| 53 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) | 57 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) |
| 54 check = 0; | 58 check = 0; |
| 55 #else | 59 #else |
| 56 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); | 60 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); |
| 57 #endif | 61 #endif |
| 58 if (check != length) | 62 if (check != length) |
| 59 png_error(png_ptr, "Write Error"); | 63 png_error(png_ptr, "Write Error"); |
| 60 } | 64 } |
| 61 #else | 65 #else |
| 62 /* This is the model-independent version. Since the standard I/O library | 66 /* This is the model-independent version. Since the standard I/O library |
| (...skipping 11 matching lines...) Expand all Loading... |
| 74 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ | 78 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ |
| 75 png_FILE_p io_ptr; | 79 png_FILE_p io_ptr; |
| 76 | 80 |
| 77 if (png_ptr == NULL) | 81 if (png_ptr == NULL) |
| 78 return; | 82 return; |
| 79 /* Check if data really is near. If so, use usual code. */ | 83 /* Check if data really is near. If so, use usual code. */ |
| 80 near_data = (png_byte *)CVT_PTR_NOCHECK(data); | 84 near_data = (png_byte *)CVT_PTR_NOCHECK(data); |
| 81 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); | 85 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); |
| 82 if ((png_bytep)near_data == data) | 86 if ((png_bytep)near_data == data) |
| 83 { | 87 { |
| 84 #if defined(_WIN32_WCE) | 88 #ifdef _WIN32_WCE |
| 85 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) | 89 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) |
| 86 check = 0; | 90 check = 0; |
| 87 #else | 91 #else |
| 88 check = fwrite(near_data, 1, length, io_ptr); | 92 check = fwrite(near_data, 1, length, io_ptr); |
| 89 #endif | 93 #endif |
| 90 } | 94 } |
| 91 else | 95 else |
| 92 { | 96 { |
| 93 png_byte buf[NEAR_BUF_SIZE]; | 97 png_byte buf[NEAR_BUF_SIZE]; |
| 94 png_size_t written, remaining, err; | 98 png_size_t written, remaining, err; |
| 95 check = 0; | 99 check = 0; |
| 96 remaining = length; | 100 remaining = length; |
| 97 do | 101 do |
| 98 { | 102 { |
| 99 written = MIN(NEAR_BUF_SIZE, remaining); | 103 written = MIN(NEAR_BUF_SIZE, remaining); |
| 100 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ | 104 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ |
| 101 #if defined(_WIN32_WCE) | 105 #ifdef _WIN32_WCE |
| 102 if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) | 106 if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) |
| 103 err = 0; | 107 err = 0; |
| 104 #else | 108 #else |
| 105 err = fwrite(buf, 1, written, io_ptr); | 109 err = fwrite(buf, 1, written, io_ptr); |
| 106 #endif | 110 #endif |
| 107 if (err != written) | 111 if (err != written) |
| 108 break; | 112 break; |
| 109 | 113 |
| 110 else | 114 else |
| 111 check += err; | 115 check += err; |
| 112 | 116 |
| 113 data += written; | 117 data += written; |
| 114 remaining -= written; | 118 remaining -= written; |
| 115 } | 119 } |
| 116 while (remaining != 0); | 120 while (remaining != 0); |
| 117 } | 121 } |
| 118 if (check != length) | 122 if (check != length) |
| 119 png_error(png_ptr, "Write Error"); | 123 png_error(png_ptr, "Write Error"); |
| 120 } | 124 } |
| 121 | 125 |
| 122 #endif | 126 #endif |
| 123 #endif | 127 #endif |
| 124 | 128 |
| 125 /* This function is called to output any data pending writing (normally | 129 /* 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 | 130 * to disk). After png_flush is called, there should be no data pending |
| 127 * writing in any buffers. | 131 * writing in any buffers. |
| 128 */ | 132 */ |
| 129 #if defined(PNG_WRITE_FLUSH_SUPPORTED) | 133 #ifdef PNG_WRITE_FLUSH_SUPPORTED |
| 130 void /* PRIVATE */ | 134 void /* PRIVATE */ |
| 131 png_flush(png_structp png_ptr) | 135 png_flush(png_structp png_ptr) |
| 132 { | 136 { |
| 133 if (png_ptr->output_flush_fn != NULL) | 137 if (png_ptr->output_flush_fn != NULL) |
| 134 (*(png_ptr->output_flush_fn))(png_ptr); | 138 (*(png_ptr->output_flush_fn))(png_ptr); |
| 135 } | 139 } |
| 136 | 140 |
| 137 #if !defined(PNG_NO_STDIO) | 141 #ifdef PNG_STDIO_SUPPORTED |
| 138 void PNGAPI | 142 void PNGAPI |
| 139 png_default_flush(png_structp png_ptr) | 143 png_default_flush(png_structp png_ptr) |
| 140 { | 144 { |
| 141 #if !defined(_WIN32_WCE) | 145 #ifndef _WIN32_WCE |
| 142 png_FILE_p io_ptr; | 146 png_FILE_p io_ptr; |
| 143 #endif | 147 #endif |
| 144 if (png_ptr == NULL) | 148 if (png_ptr == NULL) |
| 145 return; | 149 return; |
| 146 #if !defined(_WIN32_WCE) | 150 #ifndef _WIN32_WCE |
| 147 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); | 151 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); |
| 148 fflush(io_ptr); | 152 fflush(io_ptr); |
| 149 #endif | 153 #endif |
| 150 } | 154 } |
| 151 #endif | 155 #endif |
| 152 #endif | 156 #endif |
| 153 | 157 |
| 154 /* This function allows the application to supply new output functions for | 158 /* This function allows the application to supply new output functions for |
| 155 * libpng if standard C streams aren't being used. | 159 * libpng if standard C streams aren't being used. |
| 156 * | 160 * |
| (...skipping 25 matching lines...) Expand all Loading... |
| 182 */ | 186 */ |
| 183 void PNGAPI | 187 void PNGAPI |
| 184 png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, | 188 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) | 189 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) |
| 186 { | 190 { |
| 187 if (png_ptr == NULL) | 191 if (png_ptr == NULL) |
| 188 return; | 192 return; |
| 189 | 193 |
| 190 png_ptr->io_ptr = io_ptr; | 194 png_ptr->io_ptr = io_ptr; |
| 191 | 195 |
| 192 #if !defined(PNG_NO_STDIO) | 196 #ifdef PNG_STDIO_SUPPORTED |
| 193 if (write_data_fn != NULL) | 197 if (write_data_fn != NULL) |
| 194 png_ptr->write_data_fn = write_data_fn; | 198 png_ptr->write_data_fn = write_data_fn; |
| 195 | 199 |
| 196 else | 200 else |
| 197 png_ptr->write_data_fn = png_default_write_data; | 201 png_ptr->write_data_fn = png_default_write_data; |
| 198 #else | 202 #else |
| 199 png_ptr->write_data_fn = write_data_fn; | 203 png_ptr->write_data_fn = write_data_fn; |
| 200 #endif | 204 #endif |
| 201 | 205 |
| 202 #if defined(PNG_WRITE_FLUSH_SUPPORTED) | 206 #ifdef PNG_WRITE_FLUSH_SUPPORTED |
| 203 #if !defined(PNG_NO_STDIO) | 207 #ifdef PNG_STDIO_SUPPORTED |
| 204 if (output_flush_fn != NULL) | 208 if (output_flush_fn != NULL) |
| 205 png_ptr->output_flush_fn = output_flush_fn; | 209 png_ptr->output_flush_fn = output_flush_fn; |
| 206 | 210 |
| 207 else | 211 else |
| 208 png_ptr->output_flush_fn = png_default_flush; | 212 png_ptr->output_flush_fn = png_default_flush; |
| 209 #else | 213 #else |
| 210 png_ptr->output_flush_fn = output_flush_fn; | 214 png_ptr->output_flush_fn = output_flush_fn; |
| 211 #endif | 215 #endif |
| 212 #endif /* PNG_WRITE_FLUSH_SUPPORTED */ | 216 #endif /* PNG_WRITE_FLUSH_SUPPORTED */ |
| 213 | 217 |
| 214 /* It is an error to read while writing a png file */ | 218 /* It is an error to read while writing a png file */ |
| 215 if (png_ptr->read_data_fn != NULL) | 219 if (png_ptr->read_data_fn != NULL) |
| 216 { | 220 { |
| 217 png_ptr->read_data_fn = NULL; | 221 png_ptr->read_data_fn = NULL; |
| 218 png_warning(png_ptr, | 222 png_warning(png_ptr, |
| 219 "Attempted to set both read_data_fn and write_data_fn in"); | 223 "Attempted to set both read_data_fn and write_data_fn in"); |
| 220 png_warning(png_ptr, | 224 png_warning(png_ptr, |
| 221 "the same structure. Resetting read_data_fn to NULL."); | 225 "the same structure. Resetting read_data_fn to NULL."); |
| 222 } | 226 } |
| 223 } | 227 } |
| 224 | 228 |
| 225 #if defined(USE_FAR_KEYWORD) | 229 #ifdef USE_FAR_KEYWORD |
| 226 #if defined(_MSC_VER) | 230 #ifdef _MSC_VER |
| 227 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) | 231 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) |
| 228 { | 232 { |
| 229 void *near_ptr; | 233 void *near_ptr; |
| 230 void FAR *far_ptr; | 234 void FAR *far_ptr; |
| 231 FP_OFF(near_ptr) = FP_OFF(ptr); | 235 FP_OFF(near_ptr) = FP_OFF(ptr); |
| 232 far_ptr = (void FAR *)near_ptr; | 236 far_ptr = (void FAR *)near_ptr; |
| 233 | 237 |
| 234 if (check != 0) | 238 if (check != 0) |
| 235 if (FP_SEG(ptr) != FP_SEG(far_ptr)) | 239 if (FP_SEG(ptr) != FP_SEG(far_ptr)) |
| 236 png_error(png_ptr, "segment lost in conversion"); | 240 png_error(png_ptr, "segment lost in conversion"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 247 | 251 |
| 248 if (check != 0) | 252 if (check != 0) |
| 249 if (far_ptr != ptr) | 253 if (far_ptr != ptr) |
| 250 png_error(png_ptr, "segment lost in conversion"); | 254 png_error(png_ptr, "segment lost in conversion"); |
| 251 | 255 |
| 252 return(near_ptr); | 256 return(near_ptr); |
| 253 } | 257 } |
| 254 # endif | 258 # endif |
| 255 # endif | 259 # endif |
| 256 #endif /* PNG_WRITE_SUPPORTED */ | 260 #endif /* PNG_WRITE_SUPPORTED */ |
| OLD | NEW |