00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define PNG_INTERNAL
00019 #include "png.h"
00020 #ifdef PNG_WRITE_SUPPORTED
00021
00022
00023
00024
00025
00026
00027
00028 void
00029 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00030 {
00031 if (png_ptr->write_data_fn != NULL )
00032 (*(png_ptr->write_data_fn))(png_ptr, data, length);
00033 else
00034 png_error(png_ptr, "Call to NULL write function");
00035 }
00036
00037 #if !defined(PNG_NO_STDIO)
00038
00039
00040
00041
00042 #ifndef USE_FAR_KEYWORD
00043 void PNGAPI
00044 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00045 {
00046 png_uint_32 check;
00047
00048 if (png_ptr == NULL) return;
00049 #if defined(_WIN32_WCE)
00050 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00051 check = 0;
00052 #else
00053 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
00054 #endif
00055 if (check != length)
00056 png_error(png_ptr, "Write Error");
00057 }
00058 #else
00059
00060
00061
00062
00063
00064 #define NEAR_BUF_SIZE 1024
00065 #define MIN(a,b) (a <= b ? a : b)
00066
00067 void PNGAPI
00068 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
00069 {
00070 png_uint_32 check;
00071 png_byte *near_data;
00072 png_FILE_p io_ptr;
00073
00074 if (png_ptr == NULL) return;
00075
00076 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
00077 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
00078 if ((png_bytep)near_data == data)
00079 {
00080 #if defined(_WIN32_WCE)
00081 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
00082 check = 0;
00083 #else
00084 check = fwrite(near_data, 1, length, io_ptr);
00085 #endif
00086 }
00087 else
00088 {
00089 png_byte buf[NEAR_BUF_SIZE];
00090 png_size_t written, remaining, err;
00091 check = 0;
00092 remaining = length;
00093 do
00094 {
00095 written = MIN(NEAR_BUF_SIZE, remaining);
00096 png_memcpy(buf, data, written);
00097 #if defined(_WIN32_WCE)
00098 if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
00099 err = 0;
00100 #else
00101 err = fwrite(buf, 1, written, io_ptr);
00102 #endif
00103 if (err != written)
00104 break;
00105 else
00106 check += err;
00107 data += written;
00108 remaining -= written;
00109 }
00110 while (remaining != 0);
00111 }
00112 if (check != length)
00113 png_error(png_ptr, "Write Error");
00114 }
00115
00116 #endif
00117 #endif
00118
00119
00120
00121
00122 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00123 void
00124 png_flush(png_structp png_ptr)
00125 {
00126 if (png_ptr->output_flush_fn != NULL)
00127 (*(png_ptr->output_flush_fn))(png_ptr);
00128 }
00129
00130 #if !defined(PNG_NO_STDIO)
00131 void PNGAPI
00132 png_default_flush(png_structp png_ptr)
00133 {
00134 #if !defined(_WIN32_WCE)
00135 png_FILE_p io_ptr;
00136 #endif
00137 if (png_ptr == NULL) return;
00138 #if !defined(_WIN32_WCE)
00139 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
00140 if (io_ptr != NULL)
00141 fflush(io_ptr);
00142 #endif
00143 }
00144 #endif
00145 #endif
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 void PNGAPI
00170 png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
00171 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
00172 {
00173 if (png_ptr == NULL) return;
00174 png_ptr->io_ptr = io_ptr;
00175
00176 #if !defined(PNG_NO_STDIO)
00177 if (write_data_fn != NULL)
00178 png_ptr->write_data_fn = write_data_fn;
00179 else
00180 png_ptr->write_data_fn = png_default_write_data;
00181 #else
00182 png_ptr->write_data_fn = write_data_fn;
00183 #endif
00184
00185 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00186 #if !defined(PNG_NO_STDIO)
00187 if (output_flush_fn != NULL)
00188 png_ptr->output_flush_fn = output_flush_fn;
00189 else
00190 png_ptr->output_flush_fn = png_default_flush;
00191 #else
00192 png_ptr->output_flush_fn = output_flush_fn;
00193 #endif
00194 #endif
00195
00196
00197 if (png_ptr->read_data_fn != NULL)
00198 {
00199 png_ptr->read_data_fn = NULL;
00200 png_warning(png_ptr,
00201 "Attempted to set both read_data_fn and write_data_fn in");
00202 png_warning(png_ptr,
00203 "the same structure. Resetting read_data_fn to NULL.");
00204 }
00205 }
00206
00207 #if defined(USE_FAR_KEYWORD)
00208 #if defined(_MSC_VER)
00209 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
00210 {
00211 void *near_ptr;
00212 void FAR *far_ptr;
00213 FP_OFF(near_ptr) = FP_OFF(ptr);
00214 far_ptr = (void FAR *)near_ptr;
00215 if (check != 0)
00216 if (FP_SEG(ptr) != FP_SEG(far_ptr))
00217 png_error(png_ptr, "segment lost in conversion");
00218 return(near_ptr);
00219 }
00220 # else
00221 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
00222 {
00223 void *near_ptr;
00224 void FAR *far_ptr;
00225 near_ptr = (void FAR *)ptr;
00226 far_ptr = (void FAR *)near_ptr;
00227 if (check != 0)
00228 if (far_ptr != ptr)
00229 png_error(png_ptr, "segment lost in conversion");
00230 return(near_ptr);
00231 }
00232 # endif
00233 # endif
00234 #endif