CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libpng17/pngwio.c
Views: 1401
1
2
/* pngwio.c - functions for data output
3
*
4
* Last changed in libpng 1.7.0 [(PENDING RELEASE)]
5
* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
6
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
*
9
* This code is released under the libpng license.
10
* For conditions of distribution and use, see the disclaimer
11
* and license in png.h
12
*
13
* This file provides a location for all output. Users who need
14
* special handling are expected to write functions that have the same
15
* arguments as these and perform similar functions, but that possibly
16
* use different output methods. Note that you shouldn't change these
17
* functions, but rather write replacement functions and then change
18
* them at run time with png_set_write_fn(...).
19
*/
20
21
#include "pngpriv.h"
22
#define PNG_SRC_FILE PNG_SRC_FILE_pngwio
23
24
#ifdef PNG_WRITE_SUPPORTED
25
26
/* Write the data to whatever output you are using. The default routine
27
* writes to a file pointer. Note that this routine sometimes gets called
28
* with very small lengths, so you should implement some kind of simple
29
* buffering if you are using unbuffered writes. This should never be asked
30
* to write more than 64K on a 16 bit machine.
31
*/
32
33
void /* PRIVATE */
34
png_write_data(png_structrp png_ptr, png_const_voidp data, png_size_t length)
35
{
36
/* This was guaranteed by prior versions of libpng, so app callbacks may
37
* assume it even though it isn't documented to be the case.
38
*/
39
debug(length > 0U);
40
41
/* NOTE: write_data_fn must not change the buffer!
42
* This cast is required because of the API; changing the type of the
43
* callback would require every app to change the callback and that change
44
* would have to be conditional on the libpng version.
45
*/
46
if (png_ptr->rw_data_fn != NULL )
47
png_ptr->rw_data_fn(png_ptr,
48
png_constcast(png_bytep,png_voidcast(png_const_bytep,data)), length);
49
50
else
51
png_app_error(png_ptr, "No write function");
52
}
53
54
#ifdef PNG_STDIO_SUPPORTED
55
/* This is the function that does the actual writing of data. If you are
56
* not writing to a standard C stream, you should create a replacement
57
* write_data function and use it at run time with png_set_write_fn(), rather
58
* than changing the library.
59
*/
60
void PNGCBAPI
61
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
62
{
63
png_size_t check;
64
65
if (png_ptr == NULL)
66
return;
67
68
check = fwrite(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
69
70
if (check != length)
71
png_error(png_ptr, "Write Error");
72
}
73
#endif
74
75
#ifdef PNG_WRITE_FLUSH_SUPPORTED
76
# ifdef PNG_STDIO_SUPPORTED
77
void PNGCBAPI
78
png_default_flush(png_structp png_ptr)
79
{
80
if (png_ptr == NULL)
81
return;
82
83
fflush(png_voidcast(png_FILE_p, (png_ptr->io_ptr)));
84
}
85
# endif
86
#endif
87
88
/* This function allows the application to supply new output functions for
89
* libpng if standard C streams aren't being used.
90
*
91
* This function takes as its arguments:
92
* png_ptr - pointer to a png output data structure
93
* io_ptr - pointer to user supplied structure containing info about
94
* the output functions. May be NULL.
95
* write_data_fn - pointer to a new output function that takes as its
96
* arguments a pointer to a png_struct, a pointer to
97
* data to be written, and a 32-bit unsigned int that is
98
* the number of bytes to be written. The new write
99
* function should call png_error(png_ptr, "Error msg")
100
* to exit and output any fatal error messages. May be
101
* NULL, in which case libpng's default function will
102
* be used.
103
* flush_data_fn - pointer to a new flush function that takes as its
104
* arguments a pointer to a png_struct. After a call to
105
* the flush function, there should be no data in any buffers
106
* or pending transmission. If the output method doesn't do
107
* any buffering of output, a function prototype must still be
108
* supplied although it doesn't have to do anything. If
109
* PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
110
* time, output_flush_fn will be ignored, although it must be
111
* supplied for compatibility. May be NULL, in which case
112
* libpng's default function will be used, if
113
* PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
114
* a good idea if io_ptr does not point to a standard
115
* *FILE structure.
116
*/
117
void PNGAPI
118
png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
119
png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
120
{
121
if (png_ptr == NULL)
122
return;
123
124
if (png_ptr->read_struct)
125
{
126
png_app_error(png_ptr, "cannot set a write function on a read struct");
127
return;
128
}
129
130
if (write_data_fn == NULL)
131
{
132
png_app_error(png_ptr,
133
"API change: png_set_write_fn requires a function");
134
return;
135
}
136
137
png_ptr->io_ptr = io_ptr;
138
png_ptr->rw_data_fn = write_data_fn;
139
# ifdef PNG_WRITE_FLUSH_SUPPORTED
140
if (output_flush_fn != NULL)
141
png_ptr->output_flush_fn = output_flush_fn;
142
# else
143
PNG_UNUSED(output_flush_fn)
144
# endif /* WRITE_FLUSH */
145
}
146
#endif /* WRITE */
147
148