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