Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libpng/pngwio.c
9833 views
1
/* pngwio.c - functions for data output
2
*
3
* Copyright (c) 2018-2025 Cosmin Truta
4
* Copyright (c) 1998-2002,2004,2006-2014,2016,2018 Glenn Randers-Pehrson
5
* Copyright (c) 1996-1997 Andreas Dilger
6
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
*
8
* This code is released under the libpng license.
9
* For conditions of distribution and use, see the disclaimer
10
* and license in png.h
11
*
12
* This file provides a location for all output. Users who need
13
* special handling are expected to write functions that have the same
14
* arguments as these and perform similar functions, but that possibly
15
* use different output methods. Note that you shouldn't change these
16
* functions, but rather write replacement functions and then change
17
* them at run time with png_set_write_fn(...).
18
*/
19
20
#include "pngpriv.h"
21
22
#ifdef PNG_WRITE_SUPPORTED
23
24
/* Write the data to whatever output you are using. The default routine
25
* writes to a file pointer. Note that this routine sometimes gets called
26
* with very small lengths, so you should implement some kind of simple
27
* buffering if you are using unbuffered writes. This should never be asked
28
* to write more than 64K on a 16-bit machine.
29
*/
30
31
void /* PRIVATE */
32
png_write_data(png_structrp png_ptr, png_const_bytep data, size_t length)
33
{
34
/* NOTE: write_data_fn must not change the buffer! */
35
if (png_ptr->write_data_fn != NULL )
36
(*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
37
length);
38
39
else
40
png_error(png_ptr, "Call to NULL write function");
41
}
42
43
#ifdef PNG_STDIO_SUPPORTED
44
/* This is the function that does the actual writing of data. If you are
45
* not writing to a standard C stream, you should create a replacement
46
* write_data function and use it at run time with png_set_write_fn(), rather
47
* than changing the library.
48
*/
49
void PNGCBAPI
50
png_default_write_data(png_structp png_ptr, png_bytep data, size_t length)
51
{
52
size_t check;
53
54
if (png_ptr == NULL)
55
return;
56
57
check = fwrite(data, 1, length, (FILE *)png_ptr->io_ptr);
58
59
if (check != length)
60
png_error(png_ptr, "Write Error");
61
}
62
#endif
63
64
/* This function is called to output any data pending writing (normally
65
* to disk). After png_flush is called, there should be no data pending
66
* writing in any buffers.
67
*/
68
#ifdef PNG_WRITE_FLUSH_SUPPORTED
69
void /* PRIVATE */
70
png_flush(png_structrp png_ptr)
71
{
72
if (png_ptr->output_flush_fn != NULL)
73
(*(png_ptr->output_flush_fn))(png_ptr);
74
}
75
76
# ifdef PNG_STDIO_SUPPORTED
77
void PNGCBAPI
78
png_default_flush(png_structp png_ptr)
79
{
80
FILE *io_ptr;
81
82
if (png_ptr == NULL)
83
return;
84
85
io_ptr = png_voidcast(FILE *, png_ptr->io_ptr);
86
fflush(io_ptr);
87
}
88
# endif
89
#endif
90
91
/* This function allows the application to supply new output functions for
92
* libpng if standard C streams aren't being used.
93
*
94
* This function takes as its arguments:
95
* png_ptr - pointer to a png output data structure
96
* io_ptr - pointer to user supplied structure containing info about
97
* the output functions. May be NULL.
98
* write_data_fn - pointer to a new output function that takes as its
99
* arguments a pointer to a png_struct, a pointer to
100
* data to be written, and a 32-bit unsigned int that is
101
* the number of bytes to be written. The new write
102
* function should call png_error(png_ptr, "Error msg")
103
* to exit and output any fatal error messages. May be
104
* NULL, in which case libpng's default function will
105
* be used.
106
* flush_data_fn - pointer to a new flush function that takes as its
107
* arguments a pointer to a png_struct. After a call to
108
* the flush function, there should be no data in any buffers
109
* or pending transmission. If the output method doesn't do
110
* any buffering of output, a function prototype must still be
111
* supplied although it doesn't have to do anything. If
112
* PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
113
* time, output_flush_fn will be ignored, although it must be
114
* supplied for compatibility. May be NULL, in which case
115
* libpng's default function will be used, if
116
* PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
117
* a good idea if io_ptr does not point to a standard
118
* *FILE structure.
119
*/
120
void PNGAPI
121
png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
122
png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
123
{
124
if (png_ptr == NULL)
125
return;
126
127
png_ptr->io_ptr = io_ptr;
128
129
#ifdef PNG_STDIO_SUPPORTED
130
if (write_data_fn != NULL)
131
png_ptr->write_data_fn = write_data_fn;
132
133
else
134
png_ptr->write_data_fn = png_default_write_data;
135
#else
136
png_ptr->write_data_fn = write_data_fn;
137
#endif
138
139
#ifdef PNG_WRITE_FLUSH_SUPPORTED
140
# ifdef PNG_STDIO_SUPPORTED
141
142
if (output_flush_fn != NULL)
143
png_ptr->output_flush_fn = output_flush_fn;
144
145
else
146
png_ptr->output_flush_fn = png_default_flush;
147
148
# else
149
png_ptr->output_flush_fn = output_flush_fn;
150
# endif
151
#else
152
PNG_UNUSED(output_flush_fn)
153
#endif /* WRITE_FLUSH */
154
155
#ifdef PNG_READ_SUPPORTED
156
/* It is an error to read while writing a png file */
157
if (png_ptr->read_data_fn != NULL)
158
{
159
png_ptr->read_data_fn = NULL;
160
161
png_warning(png_ptr,
162
"Can't set both read_data_fn and write_data_fn in the"
163
" same structure");
164
}
165
#endif
166
}
167
#endif /* WRITE */
168
169