3dphotoshoot

annotate libs/libpng/pngwio.c @ 14:06dc8b9b4f89

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