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