~random-stuff/libpng/libpng-1.6.x

« back to all changes in this revision

Viewing changes to pngwio.c

  • Committer: Sérgio Benjamim
  • Date: 2015-10-10 23:00:20 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20151010230020-gdtmmv30zn25396n
Update to 1.6.18.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngwio.c - functions for data output
3
3
 *
4
 
 * Last changed in libpng 1.4.0 [January 3, 2010]
5
 
 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
 
4
 * Last changed in libpng 1.6.15 [November 20, 2014]
 
5
 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
6
6
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
7
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
8
 *
18
18
 * them at run time with png_set_write_fn(...).
19
19
 */
20
20
 
21
 
#define PNG_NO_PEDANTIC_WARNINGS
22
 
#include "png.h"
 
21
#include "pngpriv.h"
 
22
 
23
23
#ifdef PNG_WRITE_SUPPORTED
24
 
#include "pngpriv.h"
25
24
 
26
25
/* Write the data to whatever output you are using.  The default routine
27
26
 * writes to a file pointer.  Note that this routine sometimes gets called
31
30
 */
32
31
 
33
32
void /* PRIVATE */
34
 
png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
 
33
png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
35
34
{
 
35
   /* NOTE: write_data_fn must not change the buffer! */
36
36
   if (png_ptr->write_data_fn != NULL )
37
 
      (*(png_ptr->write_data_fn))(png_ptr, data, length);
 
37
      (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
 
38
         length);
 
39
 
38
40
   else
39
41
      png_error(png_ptr, "Call to NULL write function");
40
42
}
45
47
 * write_data function and use it at run time with png_set_write_fn(), rather
46
48
 * than changing the library.
47
49
 */
48
 
#ifndef USE_FAR_KEYWORD
49
 
void PNGAPI
 
50
void PNGCBAPI
50
51
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
51
52
{
52
 
   png_uint_32 check;
 
53
   png_size_t check;
53
54
 
54
55
   if (png_ptr == NULL)
55
56
      return;
 
57
 
56
58
   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
57
 
   if (check != length)
58
 
      png_error(png_ptr, "Write Error");
59
 
}
60
 
#else
61
 
/* This is the model-independent version. Since the standard I/O library
62
 
 * can't handle far buffers in the medium and small models, we have to copy
63
 
 * the data.
64
 
 */
65
 
 
66
 
#define NEAR_BUF_SIZE 1024
67
 
#define MIN(a,b) (a <= b ? a : b)
68
 
 
69
 
void PNGAPI
70
 
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
71
 
{
72
 
   png_uint_32 check;
73
 
   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
74
 
   png_FILE_p io_ptr;
75
 
 
76
 
   if (png_ptr == NULL)
77
 
      return;
78
 
   /* Check if data really is near. If so, use usual code. */
79
 
   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
80
 
   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
81
 
   if ((png_bytep)near_data == data)
82
 
   {
83
 
      check = fwrite(near_data, 1, length, io_ptr);
84
 
   }
85
 
   else
86
 
   {
87
 
      png_byte buf[NEAR_BUF_SIZE];
88
 
      png_size_t written, remaining, err;
89
 
      check = 0;
90
 
      remaining = length;
91
 
      do
92
 
      {
93
 
         written = MIN(NEAR_BUF_SIZE, remaining);
94
 
         png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
95
 
         err = fwrite(buf, 1, written, io_ptr);
96
 
         if (err != written)
97
 
            break;
98
 
 
99
 
         else
100
 
            check += err;
101
 
 
102
 
         data += written;
103
 
         remaining -= written;
104
 
      }
105
 
      while (remaining != 0);
106
 
   }
107
 
   if (check != length)
108
 
      png_error(png_ptr, "Write Error");
109
 
}
110
 
 
111
 
#endif
 
59
 
 
60
   if (check != length)
 
61
      png_error(png_ptr, "Write Error");
 
62
}
112
63
#endif
113
64
 
114
65
/* This function is called to output any data pending writing (normally
117
68
 */
118
69
#ifdef PNG_WRITE_FLUSH_SUPPORTED
119
70
void /* PRIVATE */
120
 
png_flush(png_structp png_ptr)
 
71
png_flush(png_structrp png_ptr)
121
72
{
122
73
   if (png_ptr->output_flush_fn != NULL)
123
74
      (*(png_ptr->output_flush_fn))(png_ptr);
124
75
}
125
76
 
126
 
#ifdef PNG_STDIO_SUPPORTED
127
 
void PNGAPI
 
77
#  ifdef PNG_STDIO_SUPPORTED
 
78
void PNGCBAPI
128
79
png_default_flush(png_structp png_ptr)
129
80
{
130
81
   png_FILE_p io_ptr;
 
82
 
131
83
   if (png_ptr == NULL)
132
84
      return;
133
 
   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
 
85
 
 
86
   io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));
134
87
   fflush(io_ptr);
135
88
}
136
 
#endif
 
89
#  endif
137
90
#endif
138
91
 
139
92
/* This function allows the application to supply new output functions for
166
119
 *                 *FILE structure.
167
120
 */
168
121
void PNGAPI
169
 
png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
170
 
   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
 
122
png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
 
123
    png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
171
124
{
172
125
   if (png_ptr == NULL)
173
126
      return;
185
138
#endif
186
139
 
187
140
#ifdef PNG_WRITE_FLUSH_SUPPORTED
188
 
#ifdef PNG_STDIO_SUPPORTED
 
141
#  ifdef PNG_STDIO_SUPPORTED
 
142
 
189
143
   if (output_flush_fn != NULL)
190
144
      png_ptr->output_flush_fn = output_flush_fn;
191
145
 
192
146
   else
193
147
      png_ptr->output_flush_fn = png_default_flush;
194
 
#else
 
148
 
 
149
#  else
195
150
   png_ptr->output_flush_fn = output_flush_fn;
196
 
#endif
197
 
#endif /* PNG_WRITE_FLUSH_SUPPORTED */
 
151
#  endif
 
152
#else
 
153
   PNG_UNUSED(output_flush_fn)
 
154
#endif /* WRITE_FLUSH */
198
155
 
 
156
#ifdef PNG_READ_SUPPORTED
199
157
   /* It is an error to read while writing a png file */
200
158
   if (png_ptr->read_data_fn != NULL)
201
159
   {
202
160
      png_ptr->read_data_fn = NULL;
203
 
      png_warning(png_ptr,
204
 
         "Attempted to set both read_data_fn and write_data_fn in");
205
 
      png_warning(png_ptr,
206
 
         "the same structure.  Resetting read_data_fn to NULL");
 
161
 
 
162
      png_warning(png_ptr,
 
163
          "Can't set both read_data_fn and write_data_fn in the"
 
164
          " same structure");
207
165
   }
208
 
}
209
 
 
210
 
#ifdef USE_FAR_KEYWORD
211
 
#ifdef _MSC_VER
212
 
void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
213
 
{
214
 
   void *near_ptr;
215
 
   void FAR *far_ptr;
216
 
   FP_OFF(near_ptr) = FP_OFF(ptr);
217
 
   far_ptr = (void FAR *)near_ptr;
218
 
 
219
 
   if (check != 0)
220
 
      if (FP_SEG(ptr) != FP_SEG(far_ptr))
221
 
         png_error(png_ptr, "segment lost in conversion");
222
 
 
223
 
   return(near_ptr);
224
 
}
225
 
#  else
226
 
void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
227
 
{
228
 
   void *near_ptr;
229
 
   void FAR *far_ptr;
230
 
   near_ptr = (void FAR *)ptr;
231
 
   far_ptr = (void FAR *)near_ptr;
232
 
 
233
 
   if (check != 0)
234
 
      if (far_ptr != ptr)
235
 
         png_error(png_ptr, "segment lost in conversion");
236
 
 
237
 
   return(near_ptr);
238
 
}
239
 
#   endif
240
 
#   endif
241
 
#endif /* PNG_WRITE_SUPPORTED */
 
166
#endif
 
167
}
 
168
#endif /* WRITE */