Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libpng17/pngpriv.h
3187 views
1
2
/* pngpriv.h - private declarations for use inside libpng
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
14
/* The symbols declared in this file (including the functions declared
15
* as extern) are PRIVATE. They are not part of the libpng public
16
* interface, and are not recommended for use by regular applications.
17
* Some of them may become public in the future; others may stay private,
18
* change in an incompatible way, or even disappear.
19
* Although the libpng users are not forbidden to include this header,
20
* they should be well aware of the issues that may arise from doing so.
21
*/
22
23
#ifndef PNGPRIV_H
24
#define PNGPRIV_H
25
26
/* Feature Test Macros. The following are defined here to ensure that correctly
27
* implemented libraries reveal the APIs libpng needs to build and hide those
28
* that are not needed and potentially damaging to the compilation.
29
*
30
* Feature Test Macros must be defined before any system header is included (see
31
* POSIX 1003.1 2.8.2 "POSIX Symbols."
32
*
33
* These macros only have an effect if the operating system supports either
34
* POSIX 1003.1 or C99, or both. On other operating systems (particularly
35
* Windows/Visual Studio) there is no effect; the OS specific tests below are
36
* still required (as of 2011-05-02.)
37
*/
38
#define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */
39
40
#ifndef PNG_VERSION_INFO_ONLY
41
/* Keep standard libraries at the top of this file */
42
43
/* Standard library headers not required by png.h: */
44
# include <stdlib.h>
45
# include <string.h>
46
47
/* For headers only required with some build configurations see the lines after
48
* pnglibconf.h is included!
49
*/
50
51
#endif /* VERSION_INFO_ONLY */
52
53
#define PNGLIB_BUILD /*libpng is being built, not used*/
54
55
/* If HAVE_CONFIG_H is defined during the build then the build system must
56
* provide an appropriate "config.h" file on the include path. The header file
57
* must provide definitions as required below (search for "HAVE_CONFIG_H");
58
* see configure.ac for more details of the requirements. The macro
59
* "PNG_NO_CONFIG_H" is provided for maintainers to test for dependencies on
60
* 'configure'; define this macro to prevent the configure build including the
61
* configure generated config.h. Libpng is expected to compile without *any*
62
* special build system support on a reasonably ANSI-C compliant system.
63
*/
64
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
65
# include <config.h>
66
67
/* Pick up the definition of 'restrict' from config.h if it was read: */
68
# define PNG_RESTRICT restrict
69
#endif
70
71
/* To support symbol prefixing it is necessary to know *before* including png.h
72
* whether the fixed point (and maybe other) APIs are exported, because if they
73
* are not internal definitions may be required. This is handled below just
74
* before png.h is included, but load the configuration now if it is available.
75
*/
76
#ifndef PNGLCONF_H
77
# include "pnglibconf.h"
78
#endif
79
80
/* Local renames may change non-exported API functions from png.h */
81
#if defined(PNG_PREFIX) && !defined(PNGPREFIX_H)
82
# include "pngprefix.h"
83
#endif
84
85
#ifdef PNG_USER_CONFIG
86
# include "pngusr.h"
87
/* These should have been defined in pngusr.h */
88
# ifndef PNG_USER_PRIVATEBUILD
89
# define PNG_USER_PRIVATEBUILD "Custom libpng build"
90
# endif
91
# ifndef PNG_USER_DLLFNAME_POSTFIX
92
# define PNG_USER_DLLFNAME_POSTFIX "Cb"
93
# endif
94
#endif
95
96
#ifndef PNG_VERSION_INFO_ONLY
97
/* Additional standard libaries required in certain cases, put only standard
98
* ANSI-C89 headers here. If not available, or non-functional, the problem
99
* should be fixed by writing a wrapper for the header and the file on your
100
* include path.
101
*/
102
#if defined(PNG_sCAL_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
103
/* png.c requires the following ANSI-C constants if the conversion of
104
* floating point to ASCII is implemented therein:
105
*
106
* DBL_MIN_10_EXP Minimum negative integer such that 10^integer is a
107
* normalized (double) value.
108
* DBL_DIG Maximum number of decimal digits (can be set to any constant)
109
* DBL_MIN Smallest normalized fp number (can be set to an arbitrary value)
110
* DBL_MAX Maximum floating point number (can be set to an arbitrary value)
111
*/
112
# include <float.h>
113
#endif /* sCAL && FLOATING_POINT */
114
115
#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) ||\
116
defined(PNG_FLOATING_POINT_SUPPORTED)
117
/* ANSI-C90 math functions are required. Full compliance with the standard
118
* is probably not a requirement, but the functions must exist and be
119
* declared in <math.h>
120
*/
121
# include <math.h>
122
#endif /* FLOATING_ARITHMETIC || FLOATING_POINT */
123
124
#endif /* VERSION_INFO_ONLY */
125
126
/* Is this a build of a DLL where compilation of the object modules requires
127
* different preprocessor settings to those required for a simple library? If
128
* so PNG_BUILD_DLL must be set.
129
*
130
* If libpng is used inside a DLL but that DLL does not export the libpng APIs
131
* PNG_BUILD_DLL must not be set. To avoid the code below kicking in build a
132
* static library of libpng then link the DLL against that.
133
*/
134
#ifndef PNG_BUILD_DLL
135
# ifdef DLL_EXPORT
136
/* This is set by libtool when files are compiled for a DLL; libtool
137
* always compiles twice, even on systems where it isn't necessary. Set
138
* PNG_BUILD_DLL in case it is necessary:
139
*/
140
# define PNG_BUILD_DLL
141
# else
142
# ifdef _WINDLL
143
/* This is set by the Microsoft Visual Studio IDE in projects that
144
* build a DLL. It can't easily be removed from those projects (it
145
* isn't visible in the Visual Studio UI) so it is a fairly reliable
146
* indication that PNG_IMPEXP needs to be set to the DLL export
147
* attributes.
148
*/
149
# define PNG_BUILD_DLL
150
# else
151
# ifdef __DLL__
152
/* This is set by the Borland C system when compiling for a DLL
153
* (as above.)
154
*/
155
# define PNG_BUILD_DLL
156
# else
157
/* Add additional compiler cases here. */
158
# endif
159
# endif
160
# endif
161
#endif /* Setting PNG_BUILD_DLL if required */
162
163
/* See pngconf.h for more details: the builder of the library may set this on
164
* the command line to the right thing for the specific compilation system or it
165
* may be automagically set above (at present we know of no system where it does
166
* need to be set on the command line.)
167
*
168
* PNG_IMPEXP must be set here when building the library to prevent pngconf.h
169
* setting it to the "import" setting for a DLL build.
170
*/
171
#ifndef PNG_IMPEXP
172
# ifdef PNG_BUILD_DLL
173
# define PNG_IMPEXP PNG_DLL_EXPORT
174
# else
175
/* Not building a DLL, or the DLL doesn't require specific export
176
* definitions.
177
*/
178
# define PNG_IMPEXP
179
# endif
180
#endif
181
182
/* No warnings for private or deprecated functions in the build: */
183
#ifndef PNG_DEPRECATED
184
# define PNG_DEPRECATED
185
#endif
186
#ifndef PNG_PRIVATE
187
# define PNG_PRIVATE
188
#endif
189
190
/* Symbol preprocessing support.
191
*
192
* To enable listing global, but internal, symbols the following macros should
193
* always be used to declare an extern data or function object in this file.
194
*/
195
#ifndef PNG_INTERNAL_DATA
196
# define PNG_INTERNAL_DATA(type, name, array) extern type name array
197
#endif
198
199
#ifndef PNG_INTERNAL_FUNCTION
200
# define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\
201
extern PNG_FUNCTION(type, name, args, PNG_EMPTY attributes)
202
#endif
203
204
#ifndef PNG_INTERNAL_CALLBACK
205
# define PNG_INTERNAL_CALLBACK(type, name, args, attributes)\
206
extern PNG_FUNCTION(type, (PNGCBAPI name), args, PNG_EMPTY attributes)
207
#endif
208
209
/* If floating or fixed point APIs are disabled they may still be compiled
210
* internally. To handle this make sure they are declared as the appropriate
211
* internal extern function (otherwise the symbol prefixing stuff won't work and
212
* the functions will be used without definitions.)
213
*
214
* NOTE: although all the API functions are declared here they are not all
215
* actually built! Because the declarations are still made it is necessary to
216
* fake out types that they depend on.
217
*/
218
#ifndef PNG_FP_EXPORT
219
# ifndef PNG_FLOATING_POINT_SUPPORTED
220
# define PNG_FP_EXPORT(ordinal, type, name, args)\
221
PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY);
222
# ifndef PNG_VERSION_INFO_ONLY
223
typedef struct png_incomplete png_double;
224
typedef png_double* png_doublep;
225
typedef const png_double* png_const_doublep;
226
typedef png_double** png_doublepp;
227
# endif
228
# endif
229
#endif
230
#ifndef PNG_FIXED_EXPORT
231
# ifndef PNG_FIXED_POINT_SUPPORTED
232
# define PNG_FIXED_EXPORT(ordinal, type, name, args)\
233
PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY);
234
# endif
235
#endif
236
237
/* Include png.h here to get the version info and other macros, pngstruct.h and
238
* pnginfo.h are included later under the protection of !PNG_VERSION_INFO_ONLY
239
*/
240
#include "png.h"
241
242
/* pngconf.h does not set PNG_DLL_EXPORT unless it is required, so: */
243
#ifndef PNG_DLL_EXPORT
244
# define PNG_DLL_EXPORT
245
#endif
246
247
/* This is a global switch to set the compilation for an installed system
248
* (a release build). It can be set for testing debug builds to ensure that
249
* they will compile when the build type is switched to RC or STABLE, the
250
* default is just to use PNG_LIBPNG_BUILD_BASE_TYPE. Set this in CPPFLAGS
251
* with either:
252
*
253
* -DPNG_RELEASE_BUILD Turns on the release compile path
254
* -DPNG_RELEASE_BUILD=0 Turns it off
255
* or in your pngusr.h with
256
* #define PNG_RELEASE_BUILD=1 Turns on the release compile path
257
* #define PNG_RELEASE_BUILD=0 Turns it off
258
*/
259
#ifndef PNG_RELEASE_BUILD
260
# define PNG_RELEASE_BUILD (PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC)
261
#endif
262
263
/* General purpose macros avoid the need to put #if PNG_RELEASE_BUILD
264
* macro blocks around function declarations and definitions when the
265
* parameter number varies. Using these results in slightly cleaner code.
266
*/
267
#if PNG_RELEASE_BUILD
268
# define only_rel(text) text
269
# define only_deb(text)
270
# define param_rel(param) param,
271
# define param_deb(param)
272
#else
273
# define only_rel(text)
274
# define only_deb(text) text
275
# define param_rel(param)
276
# define param_deb(param) param,
277
#endif
278
279
/* The affirm mechanism results in a minimal png_error() in released versions
280
* ('STABLE' versions) and a more descriptive PNG_ABORT in all other cases,
281
* when the "condition" is false (zero). If "condition" is true (nonzero),
282
* then the affirm mechanism does nothing.
283
*
284
* The PNG_RELEASE_BUILD macro, defined above, controls the behavior of
285
* 'affirm': if set to 1 affirm will call png_error (or png_err) rather than
286
* abort. The png_error text is the minimal (file location) text in this case,
287
* if it is produced. This flag indicates a STABLE (or RC) build.
288
*
289
* The macros rely on the naming convention throughout this code - png_ptr
290
* exists and is of type png_const_structrp or a compatible type - and the
291
* presence in each file of a uniquely defined macro PNG_SRC_FILE; a number
292
* indicating which file this is (this is to save space in released versions).
293
*
294
* 'affirm' is intended to look like the ANSI-C <assert.h> macro; note that
295
* this macro can coexist with the assert macro if <assert.h> is
296
* included.
297
*
298
* PNG_SRC_LINE is the position of the affirm macro. There are currently 15
299
* main source files (4 bits) and the biggest (pngrtran.c) has more than 4095
300
* lines (12 bits). However, to ensure the number will fit into 16-bits in the
301
* future and to allow hardware files to use affirm, the encoding is a bit-wise
302
* encoding based on the current number of lines.
303
*
304
* 'debug' is a version of 'affirm' that is completely removed from RELEASE
305
* builds. This is used when either an unexpected condition is completely
306
* handled or when it can't be handled even by png_error, for example after a
307
* memory overwrite.
308
*
309
* UNTESTED is used to mark code that has not been tested; it causes an assert
310
* if the code is executed and (therefore) tested. UNTESTED should not remain
311
* in release candidate code.
312
*
313
* PNG_AFFIRM_TEXT is set to 1 if affirm text should be produced, either
314
* the minimal text or, if PNG_RELEASE_BUILD is 0, the more verbose text
315
* including the 'condition' string. This value depends on whether the
316
* build supports an appropriate way of outputting the message.
317
*
318
* Note that PNG_AFFIRM_TEXT is not configurable but is worked out here: this
319
* is just the affirm code; there's no reason to allow configuration of this
320
* option.
321
*/
322
#if PNG_RELEASE_BUILD ?\
323
(defined PNG_ERROR_TEXT_SUPPORTED) :\
324
(defined PNG_WARNINGS_SUPPORTED) || (defined PNG_CONSOLE_IO_SUPPORTED)
325
# define PNG_AFFIRM_TEXT 1
326
#else
327
# define PNG_AFFIRM_TEXT 0
328
#endif /* PNG_AFFIRM_TEXT definition */
329
330
#define PNG_SRC_LINE (PNG_SRC_FILE + __LINE__)
331
332
/* png_affirmpp and png_impossiblepp are macros to make the correct call to the
333
* png_affirm function; these macros do not assume that the png_structp is
334
* called png_ptr.
335
*/
336
#if PNG_RELEASE_BUILD
337
# define png_affirmpp(pp, cond)\
338
do\
339
if (!(cond)) png_affirm(pp, PNG_SRC_LINE);\
340
while (0)
341
# define png_affirmexp(pp, cond)\
342
((cond) ? (void)0 : png_affirm(pp, PNG_SRC_LINE))
343
# define png_handled(pp, m) ((void)0)
344
# define png_impossiblepp(pp, reason) png_affirm(pp, PNG_SRC_LINE)
345
346
# define debug(cond) do {} while (0)
347
# define debug_handled(cond) do {} while (0)
348
# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
349
/* Make sure there are no 'UNTESTED' macros in released code: */
350
// # define UNTESTED libpng untested code
351
# define UNTESTED
352
# endif
353
# define NOT_REACHED do {} while (0)
354
#else
355
# define png_affirmpp(pp, cond)\
356
do\
357
if (!(cond)) png_affirm(pp, #cond, PNG_SRC_LINE);\
358
while (0)
359
# define png_affirmexp(pp, cond)\
360
((cond) ? (void)0 : png_affirm(pp, #cond, PNG_SRC_LINE))
361
# define png_handled(pp, m) (png_handled_affirm((pp), (m), PNG_SRC_LINE))
362
# define png_impossiblepp(pp, reason) png_affirm(pp, reason, PNG_SRC_LINE)
363
364
# define debug(cond) png_affirmpp(png_ptr, cond)
365
# define debug_handled(cond)\
366
do\
367
if (!(cond)) png_handled(png_ptr, #cond);\
368
while (0)
369
# define UNTESTED png_affirm(png_ptr, "untested code", PNG_SRC_LINE);
370
# define NOT_REACHED png_affirm(png_ptr, "NOT REACHED", PNG_SRC_LINE)
371
#endif
372
373
#define affirm(cond) png_affirmpp(png_ptr, cond)
374
#define affirmexp(cond) png_affirmexp(png_ptr, cond)
375
#define handled(m) png_handled(png_ptr, (m))
376
#define impossible(cond) png_impossiblepp(png_ptr, cond)
377
#define implies(a, b) debug(!(a) || (b))
378
379
/* The defines for PNG_SRC_FILE: */
380
#define PNG_SRC_FILE_(f,lines) PNG_SRC_FILE_ ## f + lines
381
382
#define PNG_SRC_FILE_png 0
383
#define PNG_SRC_FILE_pngerror (PNG_SRC_FILE_png +8192)
384
#define PNG_SRC_FILE_pngget (PNG_SRC_FILE_pngerror +2048)
385
#define PNG_SRC_FILE_pngmem (PNG_SRC_FILE_pngget +2048)
386
#define PNG_SRC_FILE_pngpread (PNG_SRC_FILE_pngmem +1024)
387
#define PNG_SRC_FILE_pngread (PNG_SRC_FILE_pngpread +2048)
388
#define PNG_SRC_FILE_pngrio (PNG_SRC_FILE_pngread +8192)
389
#define PNG_SRC_FILE_pngrtran (PNG_SRC_FILE_pngrio +1024)
390
#define PNG_SRC_FILE_pngrutil (PNG_SRC_FILE_pngrtran +8192)
391
#define PNG_SRC_FILE_pngset (PNG_SRC_FILE_pngrutil +8192)
392
#define PNG_SRC_FILE_pngtrans (PNG_SRC_FILE_pngset +2048)
393
#define PNG_SRC_FILE_pngwio (PNG_SRC_FILE_pngtrans +4096)
394
#define PNG_SRC_FILE_pngwrite (PNG_SRC_FILE_pngwio +1024)
395
#define PNG_SRC_FILE_pngwtran (PNG_SRC_FILE_pngwrite +4096)
396
#define PNG_SRC_FILE_pngwutil (PNG_SRC_FILE_pngwtran +1024)
397
398
#define PNG_SRC_FILE_arm_arm_init (PNG_SRC_FILE_pngwutil +8192)
399
#define PNG_SRC_FILE_arm_filter_neon_intrinsics\
400
(PNG_SRC_FILE_arm_arm_init +1024)
401
402
/* Add new files by changing the following line: */
403
#define PNG_SRC_FILE_LAST (PNG_SRC_FILE_arm_filter_neon_intrinsics +1024)
404
405
/* The following #define must list the files in exactly the same order as
406
* the above.
407
*/
408
#define PNG_FILES\
409
PNG_apply(png)\
410
PNG_apply(pngerror)\
411
PNG_apply(pngget)\
412
PNG_apply(pngmem)\
413
PNG_apply(pngpread)\
414
PNG_apply(pngread)\
415
PNG_apply(pngrio)\
416
PNG_apply(pngrtran)\
417
PNG_apply(pngrutil)\
418
PNG_apply(pngset)\
419
PNG_apply(pngtrans)\
420
PNG_apply(pngwio)\
421
PNG_apply(pngwrite)\
422
PNG_apply(pngwtran)\
423
PNG_apply(pngwutil)\
424
PNG_apply(arm_arm_init)\
425
PNG_apply(arm_filter_neon_intrinsics)\
426
PNG_end
427
428
/* SECURITY and SAFETY:
429
*
430
* libpng is built with support for internal limits on image dimensions and
431
* memory usage. These are documented in scripts/pnglibconf.dfa of the
432
* source and recorded in the machine generated header file pnglibconf.h.
433
*/
434
435
/* If you are running on a machine where you cannot allocate more
436
* than 64K of memory at once, uncomment this. While libpng will not
437
* normally need that much memory in a chunk (unless you load up a very
438
* large file), zlib needs to know how big of a chunk it can use, and
439
* libpng thus makes sure to check any memory allocation to verify it
440
* will fit into memory.
441
*
442
* zlib provides 'MAXSEG_64K' which, if defined, indicates the
443
* same limit and pngconf.h (already included) sets the limit
444
* if certain operating systems are detected.
445
*/
446
#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
447
# define PNG_MAX_MALLOC_64K
448
#endif
449
450
#ifndef PNG_UNUSED
451
/* Unused formal parameter warnings are silenced using the following macro
452
* which is expected to have no bad effects on performance (optimizing
453
* compilers will probably remove it entirely). Note that if you replace
454
* it with something other than whitespace, you must include the terminating
455
* semicolon.
456
*/
457
# define PNG_UNUSED(param) (void)param;
458
#endif
459
460
/* This is a convenience for parameters which are not used in release
461
* builds.
462
*/
463
#define PNG_UNUSEDRC(param) only_rel(PNG_UNUSED(param))
464
465
/* Just a little check that someone hasn't tried to define something
466
* contradictory.
467
*/
468
#if (PNG_ZBUF_SIZE > 32768) && defined(PNG_MAX_MALLOC_64K)
469
# undef PNG_ZBUF_SIZE
470
# define PNG_ZBUF_SIZE 32768
471
#endif
472
473
/* If warnings or errors are turned off the code is disabled or redirected here.
474
* From 1.5.4 functions have been added to allow very limited formatting of
475
* error and warning messages - this code will also be disabled here.
476
*/
477
#ifdef PNG_WARNINGS_SUPPORTED
478
# define PNG_WARNING_PARAMETERS(p) png_warning_parameters p;
479
#else
480
# define png_warning_parameter(p,number,string) ((void)0)
481
# define png_warning_parameter_unsigned(p,number,format,value) ((void)0)
482
# define png_warning_parameter_signed(p,number,format,value) ((void)0)
483
# define png_formatted_warning(pp,p,message) ((void)(pp))
484
# define PNG_WARNING_PARAMETERS(p)
485
#endif
486
#ifndef PNG_ERROR_TEXT_SUPPORTED
487
# define png_fixed_error(s1,s2) png_err(s1)
488
#endif
489
490
/* C allows up-casts from (void*) to any pointer and (const void*) to any
491
* pointer to a const object. C++ regards this as a type error and requires an
492
* explicit, static, cast and provides the static_cast<> rune to ensure that
493
* const is not cast away.
494
*/
495
#ifdef __cplusplus
496
# define png_voidcast(type, value) static_cast<type>(value)
497
# define png_upcast(type, value) static_cast<type>(value)
498
# define png_constcast(type, value) const_cast<type>(value)
499
# define png_aligncast(type, value) \
500
static_cast<type>(static_cast<void*>(value))
501
# define png_aligncastconst(type, value) \
502
static_cast<type>(static_cast<const void*>(value))
503
#else
504
# define png_voidcast(type, value) (value)
505
# define png_upcast(type, value) ((type)(value))
506
# define png_constcast(type, value) ((type)(value))
507
# define png_aligncast(type, value) ((void*)(value))
508
# define png_aligncastconst(type, value) ((const void*)(value))
509
#endif /* __cplusplus */
510
511
/* Some fixed point APIs are still required even if not exported because
512
* they get used by the corresponding floating point APIs. This magic
513
* deals with this:
514
*/
515
#ifdef PNG_FIXED_POINT_SUPPORTED
516
# define PNGFAPI PNGAPI
517
#else
518
# define PNGFAPI /* PRIVATE */
519
#endif
520
521
/* These macros may need to be architecture dependent. */
522
#define PNG_ALIGN_NONE 0 /* do not use data alignment */
523
#define PNG_ALIGN_ALWAYS 1 /* assume unaligned accesses are OK */
524
#ifdef offsetof
525
# define PNG_ALIGN_OFFSET 2 /* use offsetof to determine alignment */
526
#else
527
# define PNG_ALIGN_OFFSET -1 /* prevent the use of this */
528
#endif
529
#define PNG_ALIGN_SIZE 3 /* use sizeof to determine alignment */
530
531
#ifndef PNG_ALIGN_TYPE
532
/* Default to using aligned access optimizations and requiring alignment to a
533
* multiple of the data type size. Override in a compiler specific fashion
534
* if necessary by inserting tests here:
535
*/
536
# define PNG_ALIGN_TYPE PNG_ALIGN_SIZE
537
#endif
538
539
#if PNG_ALIGN_TYPE == PNG_ALIGN_SIZE
540
/* This is used because in some compiler implementations non-aligned
541
* structure members are supported, so the offsetof approach below fails.
542
* Set PNG_ALIGN_SIZE=0 for compiler combinations where unaligned access
543
* is good for performance. Do not do this unless you have tested the result
544
* and understand it.
545
*/
546
# define png_alignof(type) (sizeof (type))
547
#else
548
# if PNG_ALIGN_TYPE == PNG_ALIGN_OFFSET
549
# define png_alignof(type) offsetof(struct{char c; type t;}, t)
550
# else
551
# if PNG_ALIGN_TYPE == PNG_ALIGN_ALWAYS
552
# define png_alignof(type) (1)
553
# endif
554
/* Else leave png_alignof undefined to prevent use thereof */
555
# endif
556
#endif
557
558
/* This implicitly assumes alignment is always to a power of 2. */
559
#ifdef png_alignof
560
# define png_isaligned(ptr, type)\
561
((((const char*)ptr-(const char*)0) & (png_alignof(type)-1)) == 0)
562
#else
563
# define png_isaligned(ptr, type) 0
564
#endif
565
566
/* Buffer alignment control. These #defines control how the buffers used during
567
* read are aligned and how big they are.
568
*/
569
#ifndef PNG_ROW_BUFFER_ALIGN_TYPE
570
/* The absolute minimum alignment for a row buffer is that required for
571
* png_uint_32 direct access. The #define is of a legal C type that can be
572
* used as the type in the definition of the first member of a C union; give
573
* a typedef name if in doubt.
574
*/
575
# define PNG_ROW_BUFFER_ALIGN_TYPE png_uint_32
576
#endif /* !ROW_BUFFER_ALIGN_TYPE */
577
#ifndef PNG_ROW_BUFFER_BYTE_ALIGN
578
/* This is the minimum size in bytes of the buffer used while processing
579
* parts of row. Except at the end of the row pixels will always be
580
* processed in blocks such that the block size is a multiple of this number
581
*/
582
# define PNG_ROW_BUFFER_BYTE_ALIGN\
583
((unsigned int)/*SAFE*/(sizeof (PNG_ROW_BUFFER_ALIGN_TYPE)))
584
#endif /* !ROW_BUFFER_BYTE_ALIGN */
585
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
586
# define PNG_MAX_PIXEL_BYTES 16U /* 4x32-bit channels */
587
#else /* !READ_USER_TRANSFORM */
588
# define PNG_MAX_PIXEL_BYTES 8U /* 4x16-bit channels */
589
#endif /* !READ_USER_TRANSFORM */
590
/* PNG_ROW_BUFFER_SIZE is a compile time constant for the size of the row
591
* buffer. The minimum size of 2048 bytes is intended to allow the buffer to
592
* hold a complete 256 entry color map of 64-bit (8-byte) pixels. This is a
593
* requirement at some points of the colormap handling code.
594
*
595
* The maximum size is intended to allow (unsigned int) indexing of the buffer,
596
* it only affects systems with a 16-bit unsigned value where it limits the
597
* maximum to 4096 bytes.
598
*/
599
#define PNG_MIN_ROW_BUFFER_SIZE\
600
(PNG_MAX_PIXEL_BYTES * PNG_ROW_BUFFER_BYTE_ALIGN * 8U)
601
#define PNG_MAX_ROW_BUFFER_SIZE ((UINT_MAX / 16U) + 1U)
602
#ifndef PNG_ROW_BUFFER_SIZE
603
# define PNG_ROW_BUFFER_SIZE\
604
(PNG_MIN_ROW_BUFFER_SIZE < 2048U ? 2048U : PNG_MIN_ROW_BUFFER_SIZE)
605
#endif /* ROW_BUFFER_SIZE */
606
607
/* End of memory model/platform independent support */
608
/* End of 1.5.0beta36 move from pngconf.h */
609
610
/* CONSTANTS and UTILITY MACROS
611
* These are used internally by libpng and not exposed in the API
612
*/
613
614
/* Various modes of operation. Note that after an init, mode is set to
615
* zero automatically when the structure is created. Three of these
616
* are defined in png.h because they need to be visible to applications
617
* that call png_set_unknown_chunk().
618
*/
619
/* #define PNG_HAVE_IHDR 0x01 (defined as (int) in png.h) */
620
/* #define PNG_HAVE_PLTE 0x02 (defined as (int) in png.h) */
621
#define PNG_HAVE_IDAT 0x04U
622
/* #define PNG_AFTER_IDAT 0x08 (defined as (int) in png.h) */
623
#define PNG_HAVE_IEND 0x10U
624
#define PNG_HAVE_PNG_SIGNATURE 0x20U
625
626
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
627
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
628
/* See below for the definitions of the tables used in these macros */
629
#define PNG_sRGB_FROM_LINEAR(pp, linear) png_check_byte(pp,\
630
(png_sRGB_base[(linear)>>15] +\
631
((((linear)&0x7fffU)*png_sRGB_delta[(linear)>>15])>>12)) >> 8)
632
/* Given a value 'linear' in the range 0..255*65535 calculate the 8-bit sRGB
633
* encoded value with maximum error 0.646365. Note that the input is not a
634
* 16-bit value; it has been multiplied by 255! */
635
#endif /* PNG_SIMPLIFIED_READ/WRITE */
636
637
/* Added to libpng-1.6.0: scale a 16-bit value in the range 0..65535 to 0..255
638
* by dividing by 257 *with rounding*. This macro is exact for the given range.
639
* See the discourse in pngrtran.c png_do_scale_16_to_8. The values in the
640
* macro were established by experiment (modifying the added value). The macro
641
* has a second variant that takes a value already scaled by 255 and divides by
642
* 65535 - this has a maximum error of .502. Over the range 0..65535*65535 it
643
* only gives off-by-one errors and only for 0.5% (1 in 200) of the values.
644
*/
645
#define PNG_DIV65535(v24) (((v24) + 32895U) >> 16)
646
#define PNG_DIV257(v16) PNG_DIV65535((png_uint_32)(v16) * 255U)
647
648
/* Added to libpng-1.2.6 JB
649
* Modified in libpng-1.7.0 to avoid the intermediate calculation overflow
650
* when:
651
*
652
* pixel_bits == 4: any width over 0x3FFFFFFEU overflows
653
* pixel_bits == 2: any width over 0x7FFFFFFCU overflows
654
*
655
* In both these cases any width results in a rowbytes that fits in 32 bits.
656
* The problem arose in previous versions because the calculation used was
657
* simply ((width x pixel-bit-depth)+7)/8. At the cost of more calculations
658
* on pixel_depth this avoids the problem.
659
*/
660
#define PNG_SHIFTOF(pixel_bits/*<8*/) \
661
( (pixel_bits) == 1 ? 3 : \
662
( (pixel_bits) == 2 ? 2 : \
663
( (pixel_bits) == 4 ? 1 : \
664
0/*force bytes*/ ) ) )
665
#define PNG_ADDOF(pixel_bits/*<8*/) ((1U<<PNG_SHIFTOF(pixel_bits))-1)
666
#define PNG_ROWBYTES(pixel_bits, width) \
667
((pixel_bits) >= 8 ? \
668
((png_alloc_size_t)(width) * ((pixel_bits) >> 3)) : \
669
(((png_alloc_size_t)(width) + PNG_ADDOF(pixel_bits)) >> \
670
PNG_SHIFTOF(pixel_bits)) )
671
672
/* This macros, added in 1.7.0, makes it easy to deduce the number of channels
673
* and therefore the pixel depth from the color type. The PNG specification
674
* numbers are used in preference to the png.h constants to make it more clear
675
* why the macro works.
676
*/
677
#define PNG_COLOR_TYPE_CHANNELS(ct)\
678
(((ct) & PNG_COLOR_MASK_PALETTE) ?\
679
1U : 1U+((ct) & 2U/*COLOR*/)+(((ct)>>2)&1U/*ALPHA*/))
680
#define PNG_CHANNELS(ps) PNG_COLOR_TYPE_CHANNELS((ps).color_type)
681
#define PNG_PIXEL_DEPTH(ps) (PNG_CHANNELS(ps) * (ps).bit_depth)
682
683
/* PNG_OUT_OF_RANGE returns true if value is outside the range
684
* ideal-delta..ideal+delta. Each argument is evaluated twice.
685
* "ideal" and "delta" should be constants, normally simple
686
* integers, "value" a variable. Added to libpng-1.2.6 JB
687
*/
688
#define PNG_OUT_OF_RANGE(value, ideal, delta) \
689
( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
690
691
/* Handling of bit-field masks. Because the expression:
692
*
693
* bit_field & ~mask
694
*
695
* has implementation defined behavior in ANSI C-90 for many (int) values of
696
* 'mask' and because some of these are defined in png.h and passed in (int)
697
* parameters use of '~' has been expunged in libpng 1.7 and replaced by this
698
* macro, which is well defined in ANSI C-90 (there is a similar, 16-bit,
699
* version in pngstruct.h for the colorspace flags.)
700
*/
701
#define PNG_BIC_MASK(flags) (0xFFFFFFFFU - (flags))
702
703
/* Conversions between fixed and floating point, only defined if
704
* required (to make sure the code doesn't accidentally use float
705
* when it is supposedly disabled.)
706
*/
707
#ifdef PNG_FLOATING_POINT_SUPPORTED
708
/* The floating point conversion can't overflow, though it can and
709
* does lose accuracy relative to the original fixed point value.
710
* In practice this doesn't matter because png_fixed_point only
711
* stores numbers with very low precision. The png_ptr and s
712
* arguments are unused by default but are there in case error
713
* checking becomes a requirement.
714
*/
715
#define png_float(png_ptr, fixed, s) (.00001 * (fixed))
716
717
/* The fixed point conversion performs range checking and evaluates
718
* its argument multiple times, so must be used with care. The
719
* range checking uses the PNG specification values for a signed
720
* 32 bit fixed point value except that the values are deliberately
721
* rounded-to-zero to an integral value - 21474 (21474.83 is roughly
722
* (2^31-1) * 100000). 's' is a string that describes the value being
723
* converted.
724
*
725
* NOTE: this macro will raise a png_error if the range check fails,
726
* therefore it is normally only appropriate to use this on values
727
* that come from API calls or other sources where an out of range
728
* error indicates a programming error, not a data error!
729
*
730
* NOTE: by default this is off - the macro is not used - because the
731
* function call saves a lot of code.
732
*/
733
#ifdef PNG_FIXED_POINT_MACRO_SUPPORTED
734
#define png_fixed(png_ptr, fp, s) ((fp) <= 21474 && (fp) >= -21474 ?\
735
((png_fixed_point)(100000 * (fp))) : (png_fixed_error(png_ptr, s),0))
736
#endif
737
/* else the corresponding function is defined below, inside the scope of the
738
* cplusplus test.
739
*/
740
#endif
741
742
/* Gamma values (new at libpng-1.5.4): */
743
#define PNG_GAMMA_MAC_OLD 151724 /* Assume '1.8' is really 2.2/1.45! */
744
#define PNG_GAMMA_MAC_INVERSE 65909
745
#define PNG_GAMMA_sRGB_INVERSE 45455
746
747
/* Almost everything below is C specific; the #defines above can be used in
748
* non-C code (so long as it is C-preprocessed) the rest of this stuff cannot.
749
*/
750
#ifndef PNG_VERSION_INFO_ONLY
751
752
#include "pngstruct.h"
753
#include "pnginfo.h"
754
755
/* Validate the include paths - the include path used to generate pnglibconf.h
756
* must match that used in the build, or we must be using pnglibconf.h.prebuilt:
757
*/
758
#if PNG_ZLIB_VERNUM != 0 && PNG_ZLIB_VERNUM != ZLIB_VERNUM
759
# error ZLIB_VERNUM != PNG_ZLIB_VERNUM \
760
"-I (include path) error: see the notes in pngpriv.h"
761
/* This means that when pnglibconf.h was built the copy of zlib.h that it
762
* used is not the same as the one being used here. Because the build of
763
* libpng makes decisions to use inflateInit2 and inflateReset2 based on the
764
* zlib version number and because this affects handling of certain broken
765
* PNG files the -I directives must match.
766
*
767
* The most likely explanation is that you passed a -I in CFLAGS. This will
768
* not work; all the preprocessor directories and in particular all the -I
769
* directives must be in CPPFLAGS.
770
*/
771
#endif
772
773
/* This is used for 16 bit gamma tables -- only the top level pointers are
774
* const; this could be changed:
775
*/
776
typedef const png_uint_16p * png_const_uint_16pp;
777
778
/* Added to libpng-1.5.7: sRGB conversion tables */
779
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
780
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
781
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
782
PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_table, [256]);
783
/* Convert from an sRGB encoded value 0..255 to a 16-bit linear value,
784
* 0..65535. This table gives the closest 16-bit answers (no errors).
785
*/
786
#endif
787
788
PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_base, [512]);
789
PNG_INTERNAL_DATA(const png_byte, png_sRGB_delta, [512]);
790
#endif /* PNG_SIMPLIFIED_READ/WRITE */
791
792
793
/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
794
#ifdef __cplusplus
795
extern "C" {
796
#endif /* __cplusplus */
797
798
/* Internal functions; these are not exported from a DLL however because they
799
* are used within several of the C source files they have to be C extern.
800
*
801
* All of these functions must be declared with PNG_INTERNAL_FUNCTION.
802
*/
803
/* Affirm handling */
804
PNG_INTERNAL_FUNCTION(void, png_affirm,(png_const_structrp png_ptr,
805
param_deb(png_const_charp condition) unsigned int position), PNG_NORETURN);
806
807
#if !PNG_RELEASE_BUILD
808
PNG_INTERNAL_FUNCTION(void, png_handled_affirm,(png_const_structrp png_ptr,
809
png_const_charp message, unsigned int position), PNG_EMPTY);
810
/* This is not marked PNG_NORETURN because in PNG_RELEASE_BUILD it will
811
* disappear and control will pass through it.
812
*/
813
#endif /* !RELEASE_BUILD */
814
815
/* Character/byte range checking. */
816
/* GCC complains about assignments of an (int) expression to a (char) even when
817
* it can readily determine that the value is in range. This makes arithmetic
818
* on (char) or (png_byte) values tedious. The warning is not issued by
819
* default, but libpng coding rules require no warnings leading to excessive,
820
* ridiculous and dangerous expressions of the form:
821
*
822
* <char> = (char)(expression & 0xff)
823
*
824
* They are dangerous because they hide the warning, which might actually be
825
* valid, and therefore merely enable introduction of undetected overflows when
826
* code is modified.
827
*
828
* The following macros exist to reliably detect any overflow in non-release
829
* builds. The theory here is that we really want to know about overflows, not
830
* merely hide a basically flawed compiler warning by throwing unnecessary casts
831
* into the code. The warnings disappear in RC builds so that the released
832
* (STABLE) version just assigns the value (with, possibly, a warning if someone
833
* turns on the -Wconversion GCC warning.)
834
*
835
* Doing it this way ensures that the code meets two very important aims:
836
*
837
* 1) Overflows are detected in pre-release tests; previously versions of libpng
838
* have been released that really did have overflows in the RGB calculations.
839
* 2) In release builds GCC specific operations, which may reduce the ability
840
* of other compilers and even GCC to optimize the code, are avoided.
841
*
842
* There is one important extra consequence for pre-release code; it is
843
* performing a lot of checks in pixel arithmetic that the release code won't
844
* perform. As a consequence a build time option, RANGE_CHECK, is provided
845
* to allow the checks to be turned off in pre-release when building for
846
* performance testing. This is a standard "_SUPPORTED" option except that it
847
* cannot be set in the system configuration (pnglibconf.h, pnglibconf.dfa).
848
*
849
* A separate macro PNG_BYTE() is provided to safely convert an unsigned value
850
* to the PNG byte range 0..255. This handles the fact that, technically,
851
* an ANSI-C (unsigned char), hence a (png_byte), may be able to store values
852
* outside this range. Note that if you are building on a system where this is
853
* true libpng is almost certainly going to produce errors; it has never been
854
* tested on such a system. For the moment pngconf.h ensures that this will
855
* not happen.
856
*
857
* PNG_UINT_16 does the same thing for a 16-bit value passed in an (int) or
858
* (png_uint_32) (where checking is not expected.)
859
*/
860
#if !PNG_RELEASE_BUILD
861
# ifndef PNG_NO_RANGE_CHECK /* Turn off even in pre-release */
862
# define PNG_RANGE_CHECK_SUPPORTED
863
# endif
864
#endif
865
866
#ifdef PNG_RANGE_CHECK_SUPPORTED
867
PNG_INTERNAL_FUNCTION(unsigned int, png_bit_affirm,(png_const_structrp png_ptr,
868
unsigned int position, unsigned int u, unsigned int bits), PNG_EMPTY);
869
870
PNG_INTERNAL_FUNCTION(char, png_char_affirm,(png_const_structrp png_ptr,
871
unsigned int position, int c), PNG_EMPTY);
872
873
PNG_INTERNAL_FUNCTION(png_byte, png_byte_affirm,(png_const_structrp png_ptr,
874
unsigned int position, int b), PNG_EMPTY);
875
876
#if INT_MAX >= 65535
877
PNG_INTERNAL_FUNCTION(png_uint_16, png_u16_affirm,(png_const_structrp png_ptr,
878
unsigned int position, int u), PNG_EMPTY);
879
# define png_check_u16(pp, u) (png_u16_affirm((pp), PNG_SRC_LINE, (u)))
880
#else
881
/* (int) cannot hold a (png_uint_16) so the above function just won't
882
* compile correctly, for the moment just do this:
883
*/
884
# define png_check_u16(pp, u) (u)
885
#endif
886
887
# define png_check_bits(pp, u, bits)\
888
(((1U<<(bits))-1) & png_bit_affirm((pp), PNG_SRC_LINE, (u), (bits)))
889
# define png_check_char(pp, c) (png_char_affirm((pp), PNG_SRC_LINE, (c)))
890
# define png_check_byte(pp, b) (png_byte_affirm((pp), PNG_SRC_LINE, (b)))
891
# define PNG_BYTE(b) ((png_byte)((b) & 0xFFU))
892
# define PNG_UINT_16(u) ((png_uint_16)((u) & 0xFFFFU))
893
#elif !(defined PNG_REMOVE_CASTS) /* && !RANGE_CHECK */
894
# define png_check_bits(pp, u, bits) (((1U<<(bits))-1U) & (u))
895
# define png_check_char(pp, c) ((char)(c))
896
# define png_check_byte(pp, b) ((png_byte)(b))
897
# define png_check_u16(pp, u) ((png_uint_16)(u))
898
# define PNG_BYTE(b) ((png_byte)((b) & 0xFFU))
899
# define PNG_UINT_16(u) ((png_uint_16)((u) & 0xFFFFU))
900
#else /* !RANGE_CHECK */
901
/* This is somewhat trust-me-it-works: if PNG_REMOVE_CASTS is defined then
902
* the casts, which might otherwise change the values, are completely
903
* removed. Use this to test your compiler to see if it makes *any*
904
* difference (code size or speed.) Currently NOT SUPPORTED.
905
*
906
* It also makes the PNG_BYTE and PNG_UINT_16 macros do nothing either
907
* NOTE: this seems safe at present but might lead to unexpected results
908
* if someone writes code to depend on the truncation.
909
*/
910
# define png_check_bits(pp, u, bits) (u)
911
# define png_check_char(pp, c) (c)
912
# define png_check_byte(pp, b) (b)
913
# define png_check_u16(pp, u) (u)
914
# define PNG_BYTE(b) (b)
915
# define PNG_UINT_16(u) (u)
916
#endif /* RANGE_CHECK */
917
918
/* Safe calculation of a rowbytes value; does a png_error if the system limits
919
* are exceeded.
920
*/
921
PNG_INTERNAL_FUNCTION(png_alloc_size_t,png_calc_rowbytes,
922
(png_const_structrp png_ptr, unsigned int pixel_depth,
923
png_uint_32 row_width),PNG_EMPTY);
924
925
/* Common code to calculate the maximum number of pixels to transform or filter
926
* at one time; controlled by PNG_ROW_BUFFER_SIZE above:
927
*/
928
PNG_INTERNAL_FUNCTION(unsigned int,png_max_pixel_block,
929
(png_const_structrp png_ptr),PNG_EMPTY);
930
931
/* Copy the row in row_buffer; this is the non-interlaced copy used in both the
932
* read and write code. 'x_in_dest' specifies whether the 'x' applies to
933
* the destination (sp->dp[x], x_in_dest tru) or the source (sp[x]->dp,
934
* x_in_dest false).
935
*/
936
PNG_INTERNAL_FUNCTION(void, png_copy_row,(png_const_structrp png_ptr,
937
png_bytep dp, png_const_bytep sp, png_uint_32 x/*in INPUT*/,
938
png_uint_32 width/*of INPUT*/, unsigned int pixel_depth,
939
int clear/*clear the final byte*/, int x_in_dest),PNG_EMPTY);
940
941
/* Zlib support */
942
#define PNG_UNEXPECTED_ZLIB_RETURN (-7)
943
PNG_INTERNAL_FUNCTION(void, png_zstream_error,(z_stream *zstream, int ret),
944
PNG_EMPTY);
945
/* Used by the zlib handling functions to ensure that z_stream::msg is always
946
* set before they return.
947
*/
948
949
#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
950
!defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \
951
(defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \
952
defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \
953
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \
954
(defined(PNG_sCAL_SUPPORTED) && \
955
defined(PNG_FLOATING_ARITHMETIC_SUPPORTED))
956
PNG_INTERNAL_FUNCTION(png_fixed_point,png_fixed,(png_const_structrp png_ptr,
957
double fp, png_const_charp text),PNG_EMPTY);
958
#endif
959
960
/* Internal base allocator - no messages, NULL on failure to allocate. This
961
* does, however, call the application provided allocator and that could call
962
* png_error (although that would be a bug in the application implementation.)
963
*/
964
PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_base,(png_const_structrp png_ptr,
965
png_alloc_size_t size),PNG_ALLOCATED);
966
967
#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
968
defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
969
/* Internal array allocator, outputs no error or warning messages on failure,
970
* just returns NULL.
971
*/
972
PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_array,(png_const_structrp png_ptr,
973
int nelements, size_t element_size),PNG_ALLOCATED);
974
975
/* The same but an existing array is extended by add_elements. This function
976
* also memsets the new elements to 0 and copies the old elements. The old
977
* array is not freed or altered.
978
*/
979
PNG_INTERNAL_FUNCTION(png_voidp,png_realloc_array,(png_structrp png_ptr,
980
png_const_voidp array, int old_elements, int add_elements,
981
size_t element_size),PNG_ALLOCATED);
982
#endif /* text, sPLT or unknown chunks */
983
984
/* Magic to create a struct when there is no struct to call the user supplied
985
* memory allocators. Because error handling has not been set up the memory
986
* handlers can't safely call png_error, but this is an obscure and undocumented
987
* restriction so libpng has to assume that the 'free' handler, at least, might
988
* call png_error.
989
*/
990
PNG_INTERNAL_FUNCTION(png_structp,png_create_png_struct,
991
(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn,
992
png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn,
993
png_free_ptr free_fn),PNG_ALLOCATED);
994
995
/* Free memory from internal libpng struct */
996
PNG_INTERNAL_FUNCTION(void,png_destroy_png_struct,(png_structrp png_ptr),
997
PNG_EMPTY);
998
999
/* Free an allocated jmp_buf (always succeeds) */
1000
PNG_INTERNAL_FUNCTION(void,png_free_jmpbuf,(png_structrp png_ptr),PNG_EMPTY);
1001
1002
/* Function to allocate memory for zlib. PNGAPI is disallowed. */
1003
PNG_INTERNAL_FUNCTION(voidpf,png_zalloc,(voidpf png_ptr, uInt items, uInt size),
1004
PNG_ALLOCATED);
1005
1006
/* Function to free memory for zlib. PNGAPI is disallowed. */
1007
PNG_INTERNAL_FUNCTION(void,png_zfree,(voidpf png_ptr, voidpf ptr),PNG_EMPTY);
1008
1009
/* The next three functions are used by png_init_io to set the default
1010
* implementations for reading or writing to a stdio (png_FILE_p) stream.
1011
* They can't be static because in 1.7 png_init_io needs to reference them.
1012
*/
1013
#ifdef PNG_STDIO_SUPPORTED
1014
# ifdef PNG_READ_SUPPORTED
1015
PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_read_data,(png_structp png_ptr,
1016
png_bytep data, png_size_t length),PNG_EMPTY);
1017
# endif /* READ */
1018
1019
# ifdef PNG_WRITE_SUPPORTED
1020
PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_write_data,(png_structp png_ptr,
1021
png_bytep data, png_size_t length),PNG_EMPTY);
1022
1023
# ifdef PNG_WRITE_FLUSH_SUPPORTED
1024
PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_flush,(png_structp png_ptr),
1025
PNG_EMPTY);
1026
# endif /* WRITE_FLUSH */
1027
# endif /* WRITE */
1028
#endif /* STDIO */
1029
1030
/* Reset the CRC variable. The CRC is initialized with the chunk tag (4 bytes).
1031
* NOTE: at present png_struct::chunk_name MUST be set before this as well so
1032
* that png_struct::current_crc is initialized correctly!
1033
*/
1034
PNG_INTERNAL_FUNCTION(void,png_reset_crc,(png_structrp png_ptr,
1035
png_const_bytep chunk_tag), PNG_EMPTY);
1036
1037
/* Write the "data" buffer to whatever output you are using */
1038
PNG_INTERNAL_FUNCTION(void,png_write_data,(png_structrp png_ptr,
1039
png_const_voidp data, png_size_t length),PNG_EMPTY);
1040
1041
/* Read and check the PNG file signature */
1042
PNG_INTERNAL_FUNCTION(void,png_read_sig,(png_structrp png_ptr,
1043
png_inforp info_ptr),PNG_EMPTY);
1044
1045
/* Read data from whatever input you are using into the "data" buffer */
1046
PNG_INTERNAL_FUNCTION(void,png_read_data,(png_structrp png_ptr, png_voidp data,
1047
png_size_t length),PNG_EMPTY);
1048
1049
/* Read bytes into buf, and update png_ptr->crc */
1050
PNG_INTERNAL_FUNCTION(void,png_crc_read,(png_structrp png_ptr, png_voidp buf,
1051
png_uint_32 length),PNG_EMPTY);
1052
1053
/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */
1054
PNG_INTERNAL_FUNCTION(int,png_crc_finish,(png_structrp png_ptr,
1055
png_uint_32 skip),PNG_EMPTY);
1056
1057
/* Calculate the CRC over a section of data. Note that we are only
1058
* passing a maximum of 64K on systems that have this as a memory limit,
1059
* since this is the maximum buffer size we can specify.
1060
*/
1061
PNG_INTERNAL_FUNCTION(void,png_calculate_crc,(png_structrp png_ptr,
1062
png_const_voidp ptr, png_size_t length),PNG_EMPTY);
1063
1064
/* Write various chunks */
1065
1066
/* Write the IHDR chunk, and update the png_struct with the necessary
1067
* information.
1068
*/
1069
PNG_INTERNAL_FUNCTION(void,png_write_IHDR,(png_structrp png_ptr,
1070
png_uint_32 width, png_uint_32 height, int bit_depth, int color_type,
1071
int compression_method, int filter_method, int interlace_method),PNG_EMPTY);
1072
1073
PNG_INTERNAL_FUNCTION(void,png_write_PLTE,(png_structrp png_ptr,
1074
png_const_colorp palette, unsigned int num_pal),PNG_EMPTY);
1075
1076
PNG_INTERNAL_FUNCTION(void,png_write_IEND,(png_structrp png_ptr),PNG_EMPTY);
1077
1078
#ifdef PNG_WRITE_gAMA_SUPPORTED
1079
PNG_INTERNAL_FUNCTION(void,png_write_gAMA_fixed,(png_structrp png_ptr,
1080
png_fixed_point file_gamma),PNG_EMPTY);
1081
#endif
1082
1083
#ifdef PNG_WRITE_sBIT_SUPPORTED
1084
PNG_INTERNAL_FUNCTION(void,png_write_sBIT,(png_structrp png_ptr,
1085
png_const_color_8p sbit, int color_type),PNG_EMPTY);
1086
#endif
1087
1088
#ifdef PNG_WRITE_cHRM_SUPPORTED
1089
PNG_INTERNAL_FUNCTION(void,png_write_cHRM_fixed,(png_structrp png_ptr,
1090
const png_xy *xy), PNG_EMPTY);
1091
/* The xy value must have been previously validated */
1092
#endif
1093
1094
#ifdef PNG_WRITE_sRGB_SUPPORTED
1095
PNG_INTERNAL_FUNCTION(void,png_write_sRGB,(png_structrp png_ptr,
1096
int intent),PNG_EMPTY);
1097
#endif
1098
1099
#ifdef PNG_WRITE_iCCP_SUPPORTED
1100
PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr,
1101
png_const_charp name, png_const_voidp profile), PNG_EMPTY);
1102
/* The profile must have been previously validated for correctness, the
1103
* length comes from the first four bytes. Only the base, deflate,
1104
* compression is supported.
1105
*/
1106
#endif
1107
1108
#ifdef PNG_WRITE_sPLT_SUPPORTED
1109
PNG_INTERNAL_FUNCTION(void,png_write_sPLT,(png_structrp png_ptr,
1110
png_const_sPLT_tp palette),PNG_EMPTY);
1111
#endif
1112
1113
#ifdef PNG_WRITE_tRNS_SUPPORTED
1114
PNG_INTERNAL_FUNCTION(void,png_write_tRNS,(png_structrp png_ptr,
1115
png_const_bytep trans, png_const_color_16p values, int number,
1116
int color_type),PNG_EMPTY);
1117
#endif
1118
1119
#ifdef PNG_WRITE_bKGD_SUPPORTED
1120
PNG_INTERNAL_FUNCTION(void,png_write_bKGD,(png_structrp png_ptr,
1121
png_const_color_16p values, int color_type),PNG_EMPTY);
1122
#endif
1123
1124
#ifdef PNG_WRITE_hIST_SUPPORTED
1125
PNG_INTERNAL_FUNCTION(void,png_write_hIST,(png_structrp png_ptr,
1126
png_const_uint_16p hist, int num_hist),PNG_EMPTY);
1127
#endif
1128
1129
/* Chunks that have keywords */
1130
#ifdef PNG_WRITE_tEXt_SUPPORTED
1131
PNG_INTERNAL_FUNCTION(void,png_write_tEXt,(png_structrp png_ptr,
1132
png_const_charp key, png_const_charp text, png_size_t text_len),PNG_EMPTY);
1133
#endif
1134
1135
#ifdef PNG_WRITE_zTXt_SUPPORTED
1136
PNG_INTERNAL_FUNCTION(void,png_write_zTXt,(png_structrp png_ptr,
1137
png_const_charp key, png_const_charp text, int compression),PNG_EMPTY);
1138
#endif
1139
1140
#ifdef PNG_WRITE_iTXt_SUPPORTED
1141
PNG_INTERNAL_FUNCTION(void,png_write_iTXt,(png_structrp png_ptr,
1142
int compression, png_const_charp key, png_const_charp lang,
1143
png_const_charp lang_key, png_const_charp text),PNG_EMPTY);
1144
#endif
1145
1146
#ifdef PNG_TEXT_SUPPORTED /* Added at version 1.0.14 and 1.2.4 */
1147
PNG_INTERNAL_FUNCTION(int,png_set_text_2,(png_structrp png_ptr,
1148
png_inforp info_ptr, png_const_textp text_ptr, int num_text),PNG_EMPTY);
1149
#endif
1150
1151
#ifdef PNG_WRITE_oFFs_SUPPORTED
1152
PNG_INTERNAL_FUNCTION(void,png_write_oFFs,(png_structrp png_ptr,
1153
png_int_32 x_offset, png_int_32 y_offset, int unit_type),PNG_EMPTY);
1154
#endif
1155
1156
#ifdef PNG_WRITE_pCAL_SUPPORTED
1157
PNG_INTERNAL_FUNCTION(void,png_write_pCAL,(png_structrp png_ptr,
1158
png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
1159
png_const_charp units, png_charpp params),PNG_EMPTY);
1160
#endif
1161
1162
#ifdef PNG_WRITE_pHYs_SUPPORTED
1163
PNG_INTERNAL_FUNCTION(void,png_write_pHYs,(png_structrp png_ptr,
1164
png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
1165
int unit_type),PNG_EMPTY);
1166
#endif
1167
1168
#ifdef PNG_WRITE_tIME_SUPPORTED
1169
PNG_INTERNAL_FUNCTION(void,png_write_tIME,(png_structrp png_ptr,
1170
png_const_timep mod_time),PNG_EMPTY);
1171
#endif
1172
1173
#ifdef PNG_WRITE_sCAL_SUPPORTED
1174
PNG_INTERNAL_FUNCTION(void,png_write_sCAL_s,(png_structrp png_ptr,
1175
int unit, png_const_charp width, png_const_charp height),PNG_EMPTY);
1176
#endif
1177
1178
#ifdef PNG_WRITE_SUPPORTED
1179
PNG_INTERNAL_FUNCTION(void,png_write_start_IDAT,(png_structrp png_ptr),
1180
PNG_EMPTY);
1181
/* Do any required initialization before IDAT or row processing starts. */
1182
1183
/* Choose the best filter to use and filter the row data then write it out. If
1184
* WRITE_FILTERING is not supported this just writes the data out with a zero
1185
* (NONE) filter byte.
1186
*
1187
* This may be called multiple times per row, but calls must be in 'x' order;
1188
* first a call with x 0 to mark the start of the row and, at the end, one with
1189
* PNG_ROW_END set (this can be done in the same function call if the whole row
1190
* is passed.) The following flags are used internally to control pass
1191
* filtering and deflate:
1192
*/
1193
enum
1194
{
1195
png_pass_last =0x1U, /* This is the last pass in the image */
1196
png_pass_last_row =0x2U, /* This is the last row in a pass */
1197
png_pass_first_row =0x4U, /* This is the first row in a pass */
1198
png_row_end =0x8U, /* This is the last block in the row */
1199
png_no_row_info =0x0U /* Placeholder */
1200
1201
/* A useful macro; return true if this is the last block of the last row in
1202
* the image.
1203
*/
1204
# define PNG_IDAT_END(f) (((f) & ~png_pass_first_row) == \
1205
(png_row_end+png_pass_last_row+png_pass_last))
1206
};
1207
PNG_INTERNAL_FUNCTION(void,png_write_png_data,(png_structrp png_ptr,
1208
png_bytep prev_pixels, png_const_bytep unfiltered_row, png_uint_32 x,
1209
unsigned int width/*pixels*/, unsigned int row_info_flags),
1210
PNG_EMPTY);
1211
1212
PNG_INTERNAL_FUNCTION(void,png_write_png_rows,(png_structrp png_ptr,
1213
png_const_bytep *rows, png_uint_32 num_rows), PNG_EMPTY);
1214
/* As above but rows[num_rows] of correctly (PNG) formated but unfiltered
1215
* data are passed in. For an interlaced image the rows will be interlaced
1216
* rows and therefore may be narrower than the image width.
1217
*
1218
* This function advances png_structp::pass and png_structp::row_number as
1219
* required.
1220
*/
1221
1222
/* Release memory used by the deflate mechanism */
1223
PNG_INTERNAL_FUNCTION(void, png_deflate_destroy, (png_structp png_ptr),
1224
PNG_EMPTY);
1225
#endif /* WRITE */
1226
1227
#ifdef PNG_TRANSFORM_MECH_SUPPORTED
1228
PNG_INTERNAL_FUNCTION(void,png_transform_free,(png_const_structrp png_ptr,
1229
png_transformp *list),PNG_EMPTY);
1230
/* Free the entire transform list, from the given point on. the argument is
1231
* set to NULL.
1232
*/
1233
1234
PNG_INTERNAL_FUNCTION(void,png_init_transform_control,(
1235
png_transform_controlp out, png_structp png_ptr),PNG_EMPTY);
1236
/* Initialize a transform control for running the transform list forward (the
1237
* read case, and write initialization, but the write case is called within
1238
* pngtrans.c by the above function.)
1239
*/
1240
1241
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
1242
PNG_INTERNAL_FUNCTION(unsigned int,png_run_this_transform_list_forwards,
1243
(png_transform_controlp tc, png_transformp *start, png_transformp end),
1244
PNG_EMPTY);
1245
/* Used by the transform cache code to run a sub-list, from *start to the
1246
* transform end.
1247
*/
1248
#endif /* READ_TRANSFORMS */
1249
1250
#ifdef PNG_READ_SUPPORTED
1251
PNG_INTERNAL_FUNCTION(unsigned int,png_run_transform_list_forwards,
1252
(png_structp png_ptr, png_transform_controlp tc),PNG_EMPTY);
1253
/* Run the transform list in the forwards direction (from PNG format to
1254
* memory format). The transform control must be initialized completely by
1255
* the caller. This function takes account of transforms which delete
1256
* themselves during the run; it must be used.
1257
*/
1258
#endif /* READ */
1259
1260
#ifdef PNG_WRITE_SUPPORTED
1261
PNG_INTERNAL_FUNCTION(void,png_run_transform_list_backwards,
1262
(png_structp png_ptr, png_transform_controlp tc),PNG_EMPTY);
1263
/* Run the transform list in the backwards direction (from memory format to
1264
* PNG format). The transform control must be initialized completely by
1265
* the caller. This function takes account of transforms which delete
1266
* themselves during the run; it must be used.
1267
*/
1268
#endif /* WRITE */
1269
1270
PNG_INTERNAL_FUNCTION(png_transformp,png_add_transform,(png_structrp png_ptr,
1271
size_t size, png_transform_fn fn, unsigned int order),PNG_EMPTY);
1272
/* Add a transform, using the information in 'order' to control the position
1273
* of the transform in the list, returning a pointer to the transform. The
1274
* top 8 bits of 'order' control the position in the list. If a transform
1275
* does not already exist in the list with the given value a new transform
1276
* will be created and 'fn' and 'order' set. If there is a transform with
1277
* that value 'fn' must match and 'order' will be updated by combining the
1278
* new value in with a bitwise or (|). It is up to the function (fn) or the
1279
* caller of png_add_transform to determine whether the combination is valid.
1280
*
1281
* 'size' is used when creating a new transform, it may be larger than
1282
* (sizeof png_transform) if required to accomodate extra data.
1283
*
1284
* Prior to 1.7.0 transforms were executed in an order hard-wired into the
1285
* code that executed the transform functions. This was summarized in the
1286
* read case by the following comment from pngrtran.c
1287
* (png_init_read_transformations), note that this has been marked up to
1288
* indicate which PNG formats the transforms in the list apply to:
1289
*
1290
* *: applies to most formats
1291
* A: only formats with alpha
1292
* L: only low-bit-depth (less than 8 bits per component/pixel)
1293
* H: only high-bit-depth (16-bits per component)
1294
*
1295
> From the code of png_do_read_transformations the order is:
1296
*
1297
* GGRR For example column: . no action
1298
* AGG r acts on read
1299
* BB w acts on write
1300
* A B acts on both read and write
1301
>
1302
> r.r. 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1303
> .r.r 2) PNG_STRIP_ALPHA (if no compose)
1304
> ..rr 3) PNG_RGB_TO_GRAY
1305
> rr.. 4) PNG_GRAY_TO_RGB iff !PNG_FLAG_BACKGROUND_IS_GRAY
1306
> rrrr 5) PNG_COMPOSE
1307
> rrrr 6) PNG_GAMMA
1308
> .r.r 7) PNG_STRIP_ALPHA (if compose)
1309
> .r.r 8) PNG_ENCODE_ALPHA
1310
> rrrr 9) PNG_SCALE_16_TO_8
1311
> rrrr 10) PNG_16_TO_8
1312
> ..rr 11) PNG_QUANTIZE (converts to palette)
1313
> rrrr 12) PNG_EXPAND_16
1314
> rr.. 13) PNG_GRAY_TO_RGB iff PNG_FLAG_BACKGROUND_IS_GRAY
1315
> BB.. 14) PNG_INVERT_MONO
1316
> .B.B 15) PNG_INVERT_ALPHA
1317
> BBBB 16) PNG_SHIFT
1318
*
1319
* Note that transforms from this point on are used in 1.7.0 on palette
1320
* indices as well; a png_set_pack request (for example) packs the palette
1321
* index values if the output will be palettized and the grayscale values
1322
* if it will not be (if the output is low-bit-grayscale, not palette.)
1323
*
1324
> B... 17) PNG_PACK
1325
> ..BB 18) PNG_BGR
1326
> B... 19) PNG_PACKSWAP
1327
> rwrw 20) PNG_FILLER (includes PNG_ADD_ALPHA)
1328
> .B.B 21) PNG_SWAP_ALPHA
1329
> BBBB 22) PNG_SWAP_BYTES
1330
> BBBB 23) PNG_USER_TRANSFORM [must be last]
1331
*
1332
* Finally, outside the set of transforms prior to 1.7.0, the libpng
1333
* interlace handling required the pixels to be replicated to match the pixel
1334
* spacing in the image row; the first part the pre-1.7.0 interlace support,
1335
* this is still the case when reading, but for writing the interlace is now
1336
* a transform:
1337
*
1338
> BBBB 24) png_do_{read,write}_interlace (interlaced images only).
1339
*
1340
* First transforms are grouped according to basic function using the top 3
1341
* bits of the order code:
1342
*/
1343
# define PNG_TR_START 0x0000U /* initial ops on the PNG data */
1344
# define PNG_TR_ARITHMETIC 0x2000U /* arithmetic linear operations */
1345
# define PNG_TR_CHANNEL 0x4000U /* PNG conformant format changes */
1346
# define PNG_TR_QUANTIZE 0x6000U /* quantize and following operations */
1347
# define PNG_TR_ENCODING 0x8000U /* Row encoding transforms */
1348
# define PNG_TR_INTERLACE 0xA000U /* write interlace transform */
1349
/*
1350
* In libpng 1.7.0 the check on palette index values is moved to the start
1351
* (of read, end of write, which is where it was before) immediately after
1352
* the MNG filter handling
1353
*/
1354
# define PNG_TR_MNG_INTRAPIXEL (PNG_TR_START + 0x0100U)
1355
/* Perform intra-pixel differencing (write) or un-differencing on read. */
1356
# define PNG_TR_CHECK_PALETTE (PNG_TR_START + 0x0200U)
1357
/* Done before at the start on read, at the end on write to give a
1358
* consistent postion:
1359
*
1360
* PNG_RWTR_CHECK_PALETTE PI W11: happens in pngwrite.c last
1361
*/
1362
# define PNG_TR_START_CACHE (PNG_TR_START + 0x0300U)
1363
/* Not used on a transform; this is just a marker for the point at which
1364
* palette or low-bit-depth caching can start on read. (The previous
1365
* operations cannot be cached).
1366
*/
1367
# define PNG_TR_INIT_ALPHA (PNG_TR_START + 0x0400U)
1368
/* This just handles alpha/tRNS initialization issues to resolve the
1369
* inter-dependencies with tRNS expansion and background composition; it
1370
* doesn't do anything itself, just sets flags and pushes transforms.
1371
*/
1372
/*
1373
* Prior to 1.7 the arithmetic operations interleaved with the RGB-to-gray
1374
* and alpha strip byte level ops. This was done to reduce the amount of
1375
* data processed, i.e. it was an optimization not a requirement. These
1376
* operations were preceded by the 'expand' operations, which is the
1377
* opposite; it was done to simplify the code and actually slows things down
1378
* in the low bit depth gray case. The full list of operations after expand,
1379
* in the 1.6 order, is:
1380
*
1381
* PNG_TR_STRIP_ALPHA png_do_strip_channel (sometimes)
1382
* PNG_TR_RGB_TO_GRAY png_do_rgb_to_gray
1383
* PNG_TR_GRAY_TO_RGB png_do_gray_to_rgb (sometimes)
1384
* PNG_TR_COMPOSE png_do_compose
1385
* PNG_TR_GAMMA png_do_gamma (if no RGB_TO_GRAY)
1386
* PNG_TR_STRIP_ALPHA png_do_strip_channel (other times)
1387
* PNG_TR_ENCODE_ALPHA png_do_encode_alpha
1388
*
1389
* In 1.7 the operations are moved round somewhat, including moving alpha and
1390
* 16-to-8 bit reduction later. This leaves the following operations:
1391
*
1392
* PNG_TR_RGB_TO_GRAY png_do_rgb_to_gray
1393
* PNG_TR_COMPOSE png_do_compose
1394
* PNG_TR_GAMMA png_do_gamma (if no RGB_TO_GRAY)
1395
* PNG_TR_ENCODE_ALPHA png_do_encode_alpha
1396
*
1397
* Prior to 1.7 some combinations of transforms would do gamma correction
1398
* twice, the actual implementation in 1.7 is to use the following order and
1399
* rely on the cache code to optimize gray 1,2,4,8 and (of course) palette.
1400
*/
1401
# define PNG_TR_COMPOSE (PNG_TR_ARITHMETIC + 0x0100U)
1402
/* Handle background composition. This may need to push a gray-to-rgb
1403
* transform if the background is RGB for gray input. This precedes RGB
1404
* to gray convertion so that it can handle tRNS appropriately when the
1405
* background is in the PNG encoding however, typically, the processing
1406
* happens at PNG_TR_COMPOSE_ALPHA below.
1407
*
1408
* NOTE: this must be the first arithmetic transform because the code in
1409
* png_init_background relies on png_transform_control::gamma being the
1410
* original PNG gamma.
1411
*/
1412
# define PNG_TR_RGB_TO_GRAY (PNG_TR_ARITHMETIC + 0x0200U) /* to gray */
1413
/* Convert any RGB input (8/16 bit depth, RGB, RGBA) to linear gray
1414
* 16-bit. This happens first because it cannot be cached; the input data
1415
* has 24 or 48 bits of uncorrelated data so the transform has to happen
1416
* pixel-by-pixel. Internally the transform may maintain an 8 or 16-bit
1417
* gamma correction table (to 16-bit linear) to speed things up.
1418
*
1419
* NOTE: this transform must follow PNG_TR_COMPOSE with no intervening
1420
* transforms; see the code in png_init_background (pngrtran.c) which
1421
* relies on this during PNG_TC_INIT_FORMAT.
1422
*/
1423
# define PNG_TR_COMPOSE_ALPHA (PNG_TR_ARITHMETIC + 0x0300U)
1424
/* Compose alpha composition and tRNS handling when the background is a
1425
* screen color. Pushed by PNG_TR_COMPOSE as required.
1426
*/
1427
# define PNG_TR_GAMMA_ENCODE (PNG_TR_ARITHMETIC + 0x1F00U) /* last */
1428
/* Gamma encode the input. This encodes the gray or RGB channels to the
1429
* required bit depth and either scales the alpha channel or encodes it as
1430
* well, depending on the requested alpha encoding.
1431
*/
1432
/*
1433
* The 'expand' operations come after the arithmetic ones in libpng 1.7, this
1434
* forces the arithmetic stuff to do the expand, but since arithmetic is (in
1435
* 1.7) normally done in 16-bit linear this avoids spurious expands.
1436
*/
1437
# define PNG_TR_EXPAND (PNG_TR_CHANNEL + 0x0100U)
1438
/* Includes:
1439
*
1440
* PNG_TR_EXPAND_PALETTE palette images only, includes tRNS
1441
* PNG_TR_EXPAND_LBP_GRAY grayscale low-bit depth only
1442
* PNG_TR_EXPAND_tRNS non-palette images only
1443
*/
1444
# define PNG_TR_SCALE_16_TO_8 (PNG_TR_CHANNEL + 0x0200U)
1445
/* Comes after the expand and before the chop version; note that it works on
1446
* the pixel values directly, so it is a linear transform on a non-linear
1447
* value.
1448
*/
1449
/*
1450
* To handle transforms that affect the palette entries, not the palette
1451
* indices in the row data, libpng 1.7 reorders some of the post-quantize
1452
* transformations to put all the "PC" transforms ahead of all the "PI"
1453
* transforms. The "PC" transforms that came after png_do_quantize in libpng
1454
* 1.6 cannot be ordered to be before so they are included in the
1455
* PNG_TR_QUANTIZE section. The PI transforms are all in PNG_TR_ENCODING,
1456
* PNG_GRAY_TO_RGB is moved before PNG_TR_QUANTIZE to avoid the unpredictable
1457
* behavior of png_set_quantize that otherwise arises.
1458
*
1459
* The transforms in the PNG_TR_QUANTIZE section are:
1460
*
1461
* PNG_TR_EXPAND_16 !P !W
1462
* PNG_RWTR_INVERT_MONO !P W10: invert the gray channel
1463
* PNG_RWTR_INVERT_ALPHA PC W8: invert the alpha channel
1464
* PNG_RWTR_SHIFT PC W6: read: down, write: scale up
1465
* PNG_RWTR_BGR !P W9
1466
* PNG_RWTR_FILLER !P W2: add on R, remove on W
1467
* PNG_RWTR_SWAP_ALPHA !P W7
1468
* PNG_RWTR_SWAP_16 !P W5
1469
*
1470
* The ones in PNG_TR_ENCODING are:
1471
*
1472
* PNG_RWTR_PACK PI W4: R: unpack bytes, W: pack
1473
* PNG_RWTR_PIXEL_SWAP PI W3: Swap pixels in a byte
1474
* PNG_RWTR_USER PI W1
1475
*/
1476
1477
# define PNG_TR_CHANNEL_PREQ (PNG_TR_CHANNEL + 0x1F00U)
1478
/* The channel swap transforms that must happen before PNG_TR_QUANTIZE:
1479
*
1480
* PNG_TR_STRIP_ALPHA
1481
* PNG_TR_CHOP_16_TO_8
1482
* PNG_TR_GRAY_TO_RGB
1483
*/
1484
# define PNG_TR_CHANNEL_POSTQ (PNG_TR_QUANTIZE + 0x0100U)
1485
/* The post-quantize channel swap transforms:
1486
*
1487
* PNG_TR_EXPAND_16 !P !W
1488
* PNG_RWTR_BGR !P W9
1489
* PNG_RWTR_FILLER !P W2: (filler) add on R, remove on W
1490
* PNG_RWTR_SWAP_ALPHA !P W7
1491
* PNG_RWTR_SWAP_16 !P W5
1492
*
1493
* The 'CHANNEL' operation sets the transform_control channel_add flag for
1494
* use below.
1495
*/
1496
# define PNG_TR_INVERT (PNG_TR_QUANTIZE + 0x0200U)
1497
/* Invert MONO and ALPHA. If the channel_add flag is set in the transform
1498
* control INVERT_ALPHA will not be done; the png_add_alpha/filler APIs
1499
* happened after png_set_invert_alpha in earlier versions so the filler
1500
* value had to include the invert.
1501
*
1502
* PNG_RWTR_INVERT_MONO !P W10: invert the gray channel
1503
* PNG_RWTR_INVERT_ALPHA PC W8: invert the alpha channel
1504
*/
1505
# define PNG_TR_SHIFT (PNG_TR_QUANTIZE + 0x0300U)
1506
/* The channel shift, except that if the channel_add flag has been set the
1507
* alpha channel is not shifted.
1508
*
1509
* PNG_RWTR_SHIFT PC W6: read: down, write: scale up
1510
*/
1511
# define PNG_TR_PACK (PNG_TR_ENCODING + 0x0200U)
1512
/* PNG_RWTR_PACK PI W4: R: unpack bytes, W: pack */
1513
# define PNG_TR_PIXEL_SWAP (PNG_TR_ENCODING + 0x0300U)
1514
/* PNG_RWTR_PIXEL_SWAP PI W3: Swap pixels in a byte */
1515
# define PNG_TR_USER (PNG_TR_ENCODING + 0x1F00U)
1516
/* The user transform; must be last before the interlace handling because it
1517
* does unpredictable things to the format.
1518
*
1519
* PNG_RWTR_USER PI W1
1520
*/
1521
1522
PNG_INTERNAL_FUNCTION(png_transformp,png_push_transform,(png_structrp png_ptr,
1523
size_t size, png_transform_fn fn, png_transformp *transform,
1524
png_transform_controlp tc),PNG_EMPTY);
1525
/* As png_add_transform except that the new transform is inserted ahead of
1526
* the given transform (*transform). The new transform is returned, but it
1527
* will also invariably be in *transform. If 'tc' is not NULL the transform
1528
* callback will also be called; it needs to be called if this function is
1529
* called while transforms are being run.
1530
*
1531
* 'fn' must not be NULL.
1532
*
1533
* The transform is inserted with the same 'order' as the passed in
1534
* *transform, that transform and following transforms are moved up ('order'
1535
* is incremented) as required to make space. Consequently, unlike with
1536
* png_add_transform, the transform will always be new. To detect loops
1537
* (*transform)->fn must not be the same as the passed in 'fn'.
1538
*/
1539
1540
PNG_INTERNAL_FUNCTION(png_voidp,png_transform_cast_check,
1541
(png_const_structp png_ptr, unsigned int src_line, png_transformp tr,
1542
size_t size),PNG_EMPTY);
1543
/* Given a pointer to a transform, 'tr' validate that the underlying derived
1544
* class has size 'size' using the tr->size field and return the same
1545
* pointer. If there is a size mismatch the function does an affirm using
1546
* the given line number.
1547
*/
1548
#define png_transform_cast(type, pointer) png_voidcast(type*,\
1549
png_transform_cast_check(png_ptr, PNG_SRC_LINE, (pointer), sizeof (type)))
1550
/* This takes a pointer to a transform and safely returns a pointer to a
1551
* derived transform class (type); type must not have the pointer. It
1552
* validates the 'size' field. Derived classes start with a png_transform
1553
* as the first member called 'tr'.
1554
*/
1555
#endif /* TRANSFORM_MECH_SUPPORTED */
1556
1557
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
1558
/* Remove a transform from a list, moving the next transform down into
1559
* *transform.
1560
*/
1561
PNG_INTERNAL_FUNCTION(void,png_remove_transform,(png_const_structp png_ptr,
1562
png_transformp *transform),PNG_EMPTY);
1563
1564
/* Initializer for read transforms that handles caching, palette update and
1565
* palette expansion.
1566
*/
1567
PNG_INTERNAL_FUNCTION(unsigned int,png_read_init_transform_mech,
1568
(png_structp png_ptr, png_transform_control *tc),PNG_EMPTY);
1569
1570
/* Optional call to update the users info structure */
1571
PNG_INTERNAL_FUNCTION(void,png_read_transform_info,(png_structrp png_ptr,
1572
png_inforp info_ptr),PNG_EMPTY);
1573
#endif
1574
1575
/* APIs which do a tranform on both read and write but where the implementation
1576
* is separate for each; the read and write init functions are in pngrtran.c or
1577
* pngwtran.c, the API is in pngtrans.c
1578
*/
1579
#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED)
1580
PNG_INTERNAL_FUNCTION(void,png_init_read_pack,(png_transformp *transform,
1581
png_transform_controlp tc),PNG_EMPTY);
1582
#endif /* READ_PACK || READ_EXPAND */
1583
#ifdef PNG_WRITE_PACK_SUPPORTED
1584
PNG_INTERNAL_FUNCTION(void,png_init_write_pack,(png_transformp *transform,
1585
png_transform_controlp tc),PNG_EMPTY);
1586
#endif /* WRITE_PACK */
1587
1588
/* Shared transform functions, defined in pngtran.c */
1589
#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
1590
defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1591
PNG_INTERNAL_FUNCTION(void,png_do_strip_channel,(
1592
png_transform_controlp row_info, png_bytep row, int at_start),PNG_EMPTY);
1593
#endif /* FILLER */
1594
1595
#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
1596
PNG_INTERNAL_FUNCTION(void,png_do_invert,(png_transform_controlp row_info,
1597
png_bytep row),PNG_EMPTY);
1598
#endif /* INVERT */
1599
1600
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) ||\
1601
defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1602
PNG_INTERNAL_FUNCTION(void,png_do_invert_alpha,(png_transform_controlp row_info,
1603
png_bytep row),PNG_EMPTY);
1604
#endif /* INVERT_ALPHA */
1605
1606
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1607
PNG_INTERNAL_FUNCTION(void,png_add_rgb_to_gray_byte_ops,(png_structrp png_ptr,
1608
png_transform_controlp tc, unsigned int index, unsigned int order),
1609
PNG_EMPTY);
1610
/* This is an init-time utility to add appropriate byte ops to select a given
1611
* channel from R/G/B.
1612
*/
1613
#endif /* READ_RGB_TO_GRAY */
1614
1615
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) &&\
1616
defined(PNG_READ_BACKGROUND_SUPPORTED)
1617
PNG_INTERNAL_FUNCTION(void,png_push_gray_to_rgb_byte_ops,(png_transformp *tr,
1618
png_transform_controlp tc), PNG_EMPTY);
1619
/* This is an init-time utility to push appropriate byte ops to expand a
1620
* grayscale PNG data set to RGB. It calls the function callback so 'tc'
1621
* must be non-NULL.
1622
*/
1623
#endif /* GRAY_TO_RGB && READ_BACKGROUND */
1624
1625
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1626
PNG_INTERNAL_FUNCTION(void,png_add_strip_alpha_byte_ops,(png_structrp png_ptr),
1627
PNG_EMPTY);
1628
/* Called from pngrtran.c to add the relevant byte op. */
1629
#endif /* READ_STRIP_ALPHA */
1630
1631
/* The following decodes the appropriate chunks, and does error correction,
1632
* then calls the appropriate callback for the chunk if it is valid.
1633
*/
1634
1635
#ifdef PNG_READ_SUPPORTED
1636
PNG_INTERNAL_FUNCTION(png_bytep,png_read_buffer,(png_structrp png_ptr,
1637
png_alloc_size_t new_size, int warn),PNG_EMPTY);
1638
/* Manage the dynamically allocated read buffer */
1639
1640
/* Shared READ IDAT handling: */
1641
PNG_INTERNAL_FUNCTION(void,png_read_start_IDAT,(png_structrp png_ptr),
1642
PNG_EMPTY);
1643
/* Initialize the row buffers, etc. */
1644
1645
typedef enum
1646
{
1647
png_row_incomplete,
1648
/* more IDAT data needed for row */
1649
png_row_process,
1650
/* png_struct::row_buffer contains a complete row */
1651
png_row_repeat,
1652
/* row not in this pass, but the existing row may be used */
1653
png_row_skip
1654
/* row not in pass and no appropriate data; skip this row */
1655
} png_row_op;
1656
PNG_INTERNAL_FUNCTION(png_row_op,png_read_process_IDAT,(png_structrp png_ptr,
1657
png_bytep transformed_row, png_bytep display_row, int save_row),
1658
PNG_EMPTY);
1659
/* Process a block of IDAT data; the routine returns early if it has
1660
* obtained a row. It is valid to call this routine with no input data;
1661
* it will return png_row_incomplete if it needs input.
1662
*
1663
* transformed_row: The transformed pixels of the input are written here.
1664
* For interlaced images only the pixels in the pass will
1665
* be written, the other pixels will not be touched.
1666
*
1667
* display_row: The transformed pixels but replicated to that the entire
1668
* buffer will have been initialized. For passes after the
1669
* first the pixels written are determined by the 'block'
1670
* algorithm; only those *following* pixels which are
1671
* written by *later* passes are written (with a copy of the
1672
* pixel from the pass.)
1673
*
1674
* save_row: A boolean which indicates that the row (unexpanded)
1675
* should be saved in png_struct::transformed_row. This can
1676
* be used in a later call to png_combine_row.
1677
*
1678
* During reading the row is built up until png_row_process is returned. At
1679
* this point png_struct::row_buffer contains the original PNG row from the
1680
* file and, if save_row was set, png_struct::transformed_row contains the
1681
* row after the selected row transforms have been performed. For interlaced
1682
* images both are the width of the interlace pass.
1683
*
1684
* When png_row_repeat is returned the same is true, except that the buffers
1685
* still contain the contents of the preceding row (the one where this
1686
* funciton returned png_row_pricess).
1687
*
1688
* The row buffers should not be accessed if png_row_skip is returned; this
1689
* row is not modified in the current pass.
1690
*/
1691
1692
PNG_INTERNAL_FUNCTION(void,png_read_free_row_buffers,(png_structrp png_ptr),
1693
PNG_EMPTY);
1694
/* Free allocated row buffers; done as soon as possible to avoid carrying
1695
* around all the memory for longer than necessary.
1696
*/
1697
1698
PNG_INTERNAL_FUNCTION(int,png_read_finish_IDAT,(png_structrp png_ptr),
1699
PNG_EMPTY);
1700
/* Complete reading of the IDAT chunks. This returns 0 if more data is to
1701
* be read, 1 if the zlib stream has terminated. Call this routine with
1702
* zstream.avail_in greater than zero unless there is no more input data.
1703
* When zstream_avail_in is 0 on entry and the stream does not terminate
1704
* an "IDAT truncated" error will be output.
1705
*
1706
* ENTRY: png_ptr->zstream.{next,avail}_in points to more IDAT data, if
1707
* available, otherwise avail_in should be 0.
1708
* RET 0: the LZ stream is still active, more IDAT date is required, if
1709
* available, the routine *must* be called again.
1710
* RET 1: the LZ stream has been closed and an error may have been output;
1711
* png_ptr->zstream_error says whether it has. If not and there
1712
* is more IDAT data available the caller should output an
1713
* appropriate (too much IDAT) error message.
1714
*/
1715
1716
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1717
PNG_INTERNAL_FUNCTION(void,png_cache_known_unknown,(png_structrp png_ptr,
1718
png_const_bytep add, int keep),PNG_EMPTY);
1719
/* Update the png_struct::known_unknown bit cache which stores whether each
1720
* known chunk should be treated as unknown.
1721
*/
1722
#endif
1723
1724
typedef enum
1725
{
1726
png_chunk_skip = 0, /* Skip this chunk */
1727
png_chunk_unknown, /* Pass the chunk to png_handle_unknown */
1728
png_chunk_process_all, /* Process the chunk all at once */
1729
png_chunk_process_part /* Process the chunk in parts (for IDAT) */
1730
} png_chunk_op;
1731
1732
PNG_INTERNAL_FUNCTION(png_chunk_op,png_find_chunk_op,(png_structrp png_ptr),
1733
PNG_EMPTY);
1734
/* Given a chunk in png_struct::{chunk_name,chunk_length} validate the name
1735
* and work out how it should be handled. This function checks the chunk
1736
* location using png_struct::mode and will set the mode appropriately for
1737
* the known critical chunks but otherwise makes no changes to the stream
1738
* read state.
1739
*/
1740
1741
PNG_INTERNAL_FUNCTION(void,png_check_chunk_name,(png_const_structrp png_ptr,
1742
const png_uint_32 chunk_name),PNG_EMPTY);
1743
1744
PNG_INTERNAL_FUNCTION(void,png_check_chunk_length,(png_const_structrp png_ptr,
1745
const png_uint_32 chunk_length),PNG_EMPTY);
1746
1747
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1748
PNG_INTERNAL_FUNCTION(void,png_handle_unknown,(png_structrp png_ptr,
1749
png_inforp info_ptr, png_bytep chunk_data),PNG_EMPTY);
1750
/* Handle an unknown chunk that needs to be processed. It is only valid
1751
* to call this after png_find_chunk_op returns png_chunk_unknown. The
1752
* data argument points to the png_struct::chunk_length bytes of the chunk
1753
* data.
1754
*/
1755
#endif /* READ_UNKNOWN_CHUNKS */
1756
1757
PNG_INTERNAL_FUNCTION(void,png_handle_chunk,(png_structrp png_ptr,
1758
png_inforp info_ptr),PNG_EMPTY);
1759
/* The chunk to handle is in png_struct::chunk_name,chunk_length.
1760
*
1761
* NOTE: at present it is only valid to call this after png_find_chunk_op
1762
* has returned png_chunk_process_all and all the data is available for
1763
* png_handle_chunk (via the libpng read callback.)
1764
*/
1765
#endif /* READ */
1766
1767
PNG_INTERNAL_FUNCTION(void,png_init_row_info,(png_structrp png_ptr),PNG_EMPTY);
1768
/* Set the png_struct::row_ members from the PNG file information, running
1769
* transforms if required.
1770
*/
1771
1772
/* Added at libpng version 1.6.0 */
1773
#ifdef PNG_GAMMA_SUPPORTED
1774
PNG_INTERNAL_FUNCTION(void,png_colorspace_set_gamma,(png_const_structrp png_ptr,
1775
png_colorspacerp colorspace, png_fixed_point gAMA), PNG_EMPTY);
1776
/* Set the colorspace gamma with a value provided by the application or by
1777
* the gAMA chunk on read. The value will override anything set by an ICC
1778
* profile.
1779
*/
1780
1781
PNG_INTERNAL_FUNCTION(void,png_colorspace_sync_info,(png_const_structrp png_ptr,
1782
png_inforp info_ptr), PNG_EMPTY);
1783
/* Synchronize the info 'valid' flags with the colorspace */
1784
1785
PNG_INTERNAL_FUNCTION(void,png_colorspace_sync,(png_const_structrp png_ptr,
1786
png_inforp info_ptr), PNG_EMPTY);
1787
/* Copy the png_struct colorspace to the info_struct and call the above to
1788
* synchronize the flags. Checks for NULL info_ptr and does nothing.
1789
*/
1790
#endif
1791
1792
/* Added at libpng version 1.4.0 */
1793
#ifdef PNG_COLORSPACE_SUPPORTED
1794
/* These internal functions are for maintaining the colorspace structure within
1795
* a png_info or png_struct (or, indeed, both).
1796
*/
1797
PNG_INTERNAL_FUNCTION(int,png_colorspace_set_chromaticities,
1798
(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy,
1799
int preferred), PNG_EMPTY);
1800
1801
PNG_INTERNAL_FUNCTION(int,png_colorspace_set_endpoints,
1802
(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_XYZ *XYZ,
1803
int preferred), PNG_EMPTY);
1804
1805
#ifdef PNG_sRGB_SUPPORTED
1806
PNG_INTERNAL_FUNCTION(int,png_colorspace_set_sRGB,(png_const_structrp png_ptr,
1807
png_colorspacerp colorspace, int intent), PNG_EMPTY);
1808
/* This does set the colorspace gAMA and cHRM values too, but doesn't set the
1809
* flags to write them, if it returns false there was a problem and an error
1810
* message has already been output (but the colorspace may still need to be
1811
* synced to record the invalid flag).
1812
*/
1813
#endif /* sRGB */
1814
1815
#ifdef PNG_iCCP_SUPPORTED
1816
PNG_INTERNAL_FUNCTION(int,png_colorspace_set_ICC,(png_const_structrp png_ptr,
1817
png_colorspacerp colorspace, png_const_charp name,
1818
png_uint_32 profile_length, png_const_bytep profile, int is_color),
1819
PNG_EMPTY);
1820
/* The 'name' is used for information only */
1821
1822
/* Routines for checking parts of an ICC profile. */
1823
PNG_INTERNAL_FUNCTION(int,png_icc_check_length,(png_const_structrp png_ptr,
1824
png_colorspacerp colorspace, png_const_charp name,
1825
png_uint_32 profile_length), PNG_EMPTY);
1826
PNG_INTERNAL_FUNCTION(int,png_icc_check_header,(png_const_structrp png_ptr,
1827
png_colorspacerp colorspace, png_const_charp name,
1828
png_uint_32 profile_length,
1829
png_const_bytep profile /* first 132 bytes only */, int is_color),
1830
PNG_EMPTY);
1831
PNG_INTERNAL_FUNCTION(int,png_icc_check_tag_table,(png_const_structrp png_ptr,
1832
png_colorspacerp colorspace, png_const_charp name,
1833
png_uint_32 profile_length,
1834
png_const_bytep profile /* header plus whole tag table */), PNG_EMPTY);
1835
#ifdef PNG_sRGB_SUPPORTED
1836
PNG_INTERNAL_FUNCTION(void,png_icc_set_sRGB,(
1837
png_const_structrp png_ptr, png_colorspacerp colorspace,
1838
png_const_bytep profile, uLong adler), PNG_EMPTY);
1839
/* 'adler' is the Adler32 checksum of the uncompressed profile data. It may
1840
* be zero to indicate that it is not available. It is used, if provided,
1841
* as a fast check on the profile when checking to see if it is sRGB.
1842
*/
1843
#endif /* sRGB */
1844
#endif /* iCCP */
1845
#endif /* COLORSPACE */
1846
1847
/* Added at libpng version 1.4.0 */
1848
PNG_INTERNAL_FUNCTION(void,png_check_IHDR,(png_const_structrp png_ptr,
1849
png_uint_32 width, png_uint_32 height, int bit_depth,
1850
int color_type, int interlace_type, int compression_type,
1851
int filter_type),PNG_EMPTY);
1852
1853
#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
1854
PNG_INTERNAL_FUNCTION(void,png_fixed_error,(png_const_structrp png_ptr,
1855
png_const_charp name),PNG_NORETURN);
1856
#endif
1857
1858
/* Puts 'string' into 'buffer' at buffer[pos], taking care never to overwrite
1859
* the end. Always leaves the buffer nul terminated. Never errors out (and
1860
* there is no error code.)
1861
*/
1862
PNG_INTERNAL_FUNCTION(size_t,png_safecat,(png_charp buffer, size_t bufsize,
1863
size_t pos, png_const_charp string),PNG_EMPTY);
1864
1865
/* Various internal functions to handle formatted warning messages, currently
1866
* only implemented for warnings.
1867
*/
1868
#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
1869
/* Utility to dump an unsigned value into a buffer, given a start pointer and
1870
* and end pointer (which should point just *beyond* the end of the buffer!)
1871
* Returns the pointer to the start of the formatted string. This utility only
1872
* does unsigned values.
1873
*/
1874
PNG_INTERNAL_FUNCTION(png_charp,png_format_number,(png_const_charp start,
1875
png_charp end, int format, png_alloc_size_t number),PNG_EMPTY);
1876
1877
/* Convenience macro that takes an array: */
1878
#define PNG_FORMAT_NUMBER(buffer,format,number) \
1879
png_format_number(buffer, buffer + (sizeof buffer), format, number)
1880
1881
/* Suggested size for a number buffer (enough for 64 bits and a sign!) */
1882
#define PNG_NUMBER_BUFFER_SIZE 24
1883
1884
/* These are the integer formats currently supported, the name is formed from
1885
* the standard printf(3) format string.
1886
*/
1887
#define PNG_NUMBER_FORMAT_u 1 /* chose unsigned API! */
1888
#define PNG_NUMBER_FORMAT_02u 2
1889
#define PNG_NUMBER_FORMAT_d 1 /* chose signed API! */
1890
#define PNG_NUMBER_FORMAT_02d 2
1891
#define PNG_NUMBER_FORMAT_x 3
1892
#define PNG_NUMBER_FORMAT_02x 4
1893
#define PNG_NUMBER_FORMAT_fixed 5 /* choose the signed API */
1894
#endif
1895
1896
#ifdef PNG_WARNINGS_SUPPORTED
1897
/* New defines and members adding in libpng-1.5.4 */
1898
# define PNG_WARNING_PARAMETER_SIZE 32
1899
# define PNG_WARNING_PARAMETER_COUNT 8 /* Maximum 9; see pngerror.c */
1900
1901
/* An l-value of this type has to be passed to the APIs below to cache the
1902
* values of the parameters to a formatted warning message.
1903
*/
1904
typedef char png_warning_parameters[PNG_WARNING_PARAMETER_COUNT][
1905
PNG_WARNING_PARAMETER_SIZE];
1906
1907
PNG_INTERNAL_FUNCTION(void,png_warning_parameter,(png_warning_parameters p,
1908
int number, png_const_charp string),PNG_EMPTY);
1909
/* Parameters are limited in size to PNG_WARNING_PARAMETER_SIZE characters,
1910
* including the trailing '\0'.
1911
*/
1912
PNG_INTERNAL_FUNCTION(void,png_warning_parameter_unsigned,
1913
(png_warning_parameters p, int number, int format, png_alloc_size_t value),
1914
PNG_EMPTY);
1915
/* Use png_alloc_size_t because it is an unsigned type as big as any we
1916
* need to output. Use the following for a signed value.
1917
*/
1918
PNG_INTERNAL_FUNCTION(void,png_warning_parameter_signed,
1919
(png_warning_parameters p, int number, int format, png_int_32 value),
1920
PNG_EMPTY);
1921
1922
PNG_INTERNAL_FUNCTION(void,png_formatted_warning,(png_const_structrp png_ptr,
1923
png_warning_parameters p, png_const_charp message),PNG_EMPTY);
1924
/* 'message' follows the X/Open approach of using @1, @2 to insert
1925
* parameters previously supplied using the above functions. Errors in
1926
* specifying the parameters will simply result in garbage substitutions.
1927
*/
1928
#endif
1929
1930
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1931
/* Application errors (new in 1.6); use these functions (declared below) for
1932
* errors in the parameters or order of API function calls on read. The
1933
* 'warning' should be used for an error that can be handled completely; the
1934
* 'error' for one which can be handled safely but which may lose application
1935
* information or settings.
1936
*
1937
* By default these both result in a png_error call prior to release, while in a
1938
* released version the 'warning' is just a warning. However if the application
1939
* explicitly disables benign errors (explicitly permitting the code to lose
1940
* information) they both turn into warnings.
1941
*
1942
* If benign errors aren't supported they end up as the corresponding base call
1943
* (png_warning or png_error.)
1944
*/
1945
PNG_INTERNAL_FUNCTION(void,png_app_warning,(png_const_structrp png_ptr,
1946
png_const_charp message),PNG_EMPTY);
1947
/* The application provided invalid parameters to an API function or called
1948
* an API function at the wrong time, libpng can completely recover.
1949
*/
1950
1951
PNG_INTERNAL_FUNCTION(void,png_app_error,(png_const_structrp png_ptr,
1952
png_const_charp message),PNG_EMPTY);
1953
/* As above but libpng will ignore the call, or attempt some other partial
1954
* recovery from the error.
1955
*/
1956
#else
1957
# define png_app_warning(pp,s) png_warning(pp,s)
1958
# define png_app_error(pp,s) png_error(pp,s)
1959
#endif
1960
1961
PNG_INTERNAL_FUNCTION(void,png_chunk_report,(png_const_structrp png_ptr,
1962
png_const_charp message, int error),PNG_EMPTY);
1963
/* Report a recoverable issue in chunk data. On read this is used to report
1964
* a problem found while reading a particular chunk and the
1965
* png_chunk_benign_error or png_chunk_warning function is used as
1966
* appropriate. On write this is used to report an error that comes from
1967
* data set via an application call to a png_set_ API and png_app_error or
1968
* png_app_warning is used as appropriate.
1969
*
1970
* With PNG_CHUNK_FATAL an error can be marked as unrecoverable, and the
1971
* function will not return.
1972
*
1973
* The 'error' parameter must have one of the following values:
1974
*/
1975
#define PNG_CHUNK_WARNING 0 /* never an error */
1976
#define PNG_CHUNK_WRITE_ERROR 1 /* an error only on write */
1977
#define PNG_CHUNK_ERROR 2 /* always an error */
1978
#define PNG_CHUNK_FATAL 3 /* an unrecoverable error */
1979
1980
#ifndef PNG_ERROR_TEXT_SUPPORTED
1981
# define png_chunk_report(pp,e,v) png_chunk_report(pp,NULL,v)
1982
#endif
1983
1984
/* ASCII to FP interfaces, currently only implemented if sCAL
1985
* support is required.
1986
*/
1987
#if defined(PNG_sCAL_SUPPORTED)
1988
/* MAX_DIGITS is actually the maximum number of characters in an sCAL
1989
* width or height, derived from the precision (number of significant
1990
* digits - a build time settable option) and assumptions about the
1991
* maximum ridiculous exponent.
1992
*/
1993
#define PNG_sCAL_MAX_DIGITS (PNG_sCAL_PRECISION+1/*.*/+1/*E*/+10/*exponent*/)
1994
1995
#ifdef PNG_FLOATING_POINT_SUPPORTED
1996
PNG_INTERNAL_FUNCTION(void,png_ascii_from_fp,(png_const_structrp png_ptr,
1997
png_charp ascii, png_size_t size, double fp, unsigned int precision),
1998
PNG_EMPTY);
1999
#endif /* FLOATING_POINT */
2000
2001
#ifdef PNG_FIXED_POINT_SUPPORTED
2002
PNG_INTERNAL_FUNCTION(void,png_ascii_from_fixed,(png_const_structrp png_ptr,
2003
png_charp ascii, png_size_t size, png_fixed_point fp),PNG_EMPTY);
2004
#endif /* FIXED_POINT */
2005
#endif /* sCAL */
2006
2007
#if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED)
2008
/* An internal API to validate the format of a floating point number.
2009
* The result is the index of the next character. If the number is
2010
* not valid it will be the index of a character in the supposed number.
2011
*
2012
* The format of a number is defined in the PNG extensions specification
2013
* and this API is strictly conformant to that spec, not anyone elses!
2014
*
2015
* The format as a regular expression is:
2016
*
2017
* [+-]?[0-9]+.?([Ee][+-]?[0-9]+)?
2018
*
2019
* or:
2020
*
2021
* [+-]?.[0-9]+(.[0-9]+)?([Ee][+-]?[0-9]+)?
2022
*
2023
* The complexity is that either integer or fraction must be present and the
2024
* fraction is permitted to have no digits only if the integer is present.
2025
*
2026
* NOTE: The dangling E problem.
2027
* There is a PNG valid floating point number in the following:
2028
*
2029
* PNG floating point numbers are not greedy.
2030
*
2031
* Working this out requires *TWO* character lookahead (because of the
2032
* sign), the parser does not do this - it will fail at the 'r' - this
2033
* doesn't matter for PNG sCAL chunk values, but it requires more care
2034
* if the value were ever to be embedded in something more complex. Use
2035
* ANSI-C strtod if you need the lookahead.
2036
*/
2037
/* State table for the parser. */
2038
#define PNG_FP_INTEGER 0 /* before or in integer */
2039
#define PNG_FP_FRACTION 1 /* before or in fraction */
2040
#define PNG_FP_EXPONENT 2 /* before or in exponent */
2041
#define PNG_FP_STATE 3 /* mask for the above */
2042
#define PNG_FP_SAW_SIGN 4 /* Saw +/- in current state */
2043
#define PNG_FP_SAW_DIGIT 8 /* Saw a digit in current state */
2044
#define PNG_FP_SAW_DOT 16 /* Saw a dot in current state */
2045
#define PNG_FP_SAW_E 32 /* Saw an E (or e) in current state */
2046
#define PNG_FP_SAW_ANY 60 /* Saw any of the above 4 */
2047
2048
/* These three values don't affect the parser. They are set but not used.
2049
*/
2050
#define PNG_FP_WAS_VALID 64 /* Preceding substring is a valid fp number */
2051
#define PNG_FP_NEGATIVE 128 /* A negative number, including "-0" */
2052
#define PNG_FP_NONZERO 256 /* A non-zero value */
2053
#define PNG_FP_STICKY 448 /* The above three flags */
2054
2055
/* This is available for the caller to store in 'state' if required. Do not
2056
* call the parser after setting it (the parser sometimes clears it.)
2057
*/
2058
#define PNG_FP_INVALID 512 /* Available for callers as a distinct value */
2059
2060
/* Result codes for the parser (boolean - true meants ok, false means
2061
* not ok yet.)
2062
*/
2063
#define PNG_FP_MAYBE 0 /* The number may be valid in the future */
2064
#define PNG_FP_OK 1 /* The number is valid */
2065
2066
/* Tests on the sticky non-zero and negative flags. To pass these checks
2067
* the state must also indicate that the whole number is valid - this is
2068
* achieved by testing PNG_FP_SAW_DIGIT (see the implementation for why this
2069
* is equivalent to PNG_FP_OK above.)
2070
*/
2071
#define PNG_FP_NZ_MASK (PNG_FP_SAW_DIGIT | PNG_FP_NEGATIVE | PNG_FP_NONZERO)
2072
/* NZ_MASK: the string is valid and a non-zero negative value */
2073
#define PNG_FP_Z_MASK (PNG_FP_SAW_DIGIT | PNG_FP_NONZERO)
2074
/* Z MASK: the string is valid and a non-zero value. */
2075
/* PNG_FP_SAW_DIGIT: the string is valid. */
2076
#define PNG_FP_IS_ZERO(state) (((state) & PNG_FP_Z_MASK) == PNG_FP_SAW_DIGIT)
2077
#define PNG_FP_IS_POSITIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_Z_MASK)
2078
#define PNG_FP_IS_NEGATIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_NZ_MASK)
2079
2080
/* The actual parser. This can be called repeatedly. It updates
2081
* the index into the string and the state variable (which must
2082
* be initialized to 0). It returns a result code, as above. There
2083
* is no point calling the parser any more if it fails to advance to
2084
* the end of the string - it is stuck on an invalid character (or
2085
* terminated by '\0').
2086
*
2087
* Note that the pointer will consume an E or even an E+ and then leave
2088
* a 'maybe' state even though a preceding integer.fraction is valid.
2089
* The PNG_FP_WAS_VALID flag indicates that a preceding substring was
2090
* a valid number. It's possible to recover from this by calling
2091
* the parser again (from the start, with state 0) but with a string
2092
* that omits the last character (i.e. set the size to the index of
2093
* the problem character.) This has not been tested within libpng.
2094
*/
2095
PNG_INTERNAL_FUNCTION(int,png_check_fp_number,(png_const_charp string,
2096
png_size_t size, int *statep, png_size_tp whereami),PNG_EMPTY);
2097
2098
/* This is the same but it checks a complete string and returns true
2099
* only if it just contains a floating point number. As of 1.5.4 this
2100
* function also returns the state at the end of parsing the number if
2101
* it was valid (otherwise it returns 0.) This can be used for testing
2102
* for negative or zero values using the sticky flag.
2103
*/
2104
PNG_INTERNAL_FUNCTION(int,png_check_fp_string,(png_const_charp string,
2105
png_size_t size),PNG_EMPTY);
2106
#endif /* pCAL || sCAL */
2107
2108
#if defined(PNG_GAMMA_SUPPORTED) ||\
2109
defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED)
2110
/* Added at libpng version 1.5.0 */
2111
/* This is a utility to provide a*times/div (rounded) and indicate
2112
* if there is an overflow. The result is a boolean - false (0)
2113
* for overflow, true (1) if no overflow, in which case *res
2114
* holds the result.
2115
*/
2116
PNG_INTERNAL_FUNCTION(int,png_muldiv,(png_fixed_point_p res, png_fixed_point a,
2117
png_int_32 multiplied_by, png_int_32 divided_by),PNG_EMPTY);
2118
#endif /* GAMMA || INCH_CONVERSIONS || READ_pHYs */
2119
2120
#ifdef PNG_READ_GAMMA_SUPPORTED
2121
/* Internal fixed point gamma correction. These APIs are called as
2122
* required to convert single values - they don't need to be fast,
2123
* they are not used when processing image pixel values.
2124
*/
2125
PNG_INTERNAL_FUNCTION(unsigned int,png_gamma_nxmbit_correct,
2126
(unsigned int value, png_fixed_point gamma_val, unsigned int n/*input bits*/,
2127
unsigned int m/*output bits */),PNG_EMPTY);
2128
/* In this case the value must have 'n' bits and the output will have 'm'
2129
* bits.
2130
*/
2131
2132
#if !PNG_RELEASE_BUILD
2133
PNG_INTERNAL_FUNCTION(int,png_gamma_check,(png_const_structrp png_ptr,
2134
png_const_transform_controlp tc),PNG_EMPTY);
2135
/* Debugging only routine to repeat the test used above to determine if the
2136
* gamma was insignificant.
2137
*/
2138
#endif /* !RELEASE_BUILD */
2139
#endif /* READ_GAMMA */
2140
2141
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
2142
/* Internal check function to saw if the gamma of the PNG data is far enough
2143
* from the given screen gamma to require gamma correction (only needed for a
2144
* bug work-round in the simplified API).
2145
* TODO: it should be possible to remove the bug work-round in 1.7
2146
*/
2147
PNG_INTERNAL_FUNCTION(int,png_need_gamma_correction,(png_const_structrp png_ptr,
2148
png_fixed_point gamma, int sRGB_output),PNG_EMPTY);
2149
#endif /* SIMPLIFIED_READ */
2150
2151
/* This is a utility macro to say whether a gamma value is close enough to sRGB.
2152
* The test is now hardwired:
2153
*
2154
* API CHANGE: prior to 1.7 this would depend on the build-time
2155
* PNG_GAMMA_THRESHOLD_FIXED setting, which would cause inconsistent results
2156
* when the setting was changed. Since this setting can now be changed at
2157
* run-time it seems more sensible to have a single fixed definition of 'sRGB'.
2158
*
2159
* The test is approximately +/- 1%, it allows any decimal value from 0.45 (the
2160
* two digit rounded version of 1/2.2) to just under 0.46).
2161
*/
2162
#define PNG_GAMMA_IS_sRGB(g) ((g) >= 45000 && (g) < 46000)
2163
2164
/* SIMPLIFIED READ/WRITE SUPPORT */
2165
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
2166
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
2167
/* The internal structure that png_image::opaque points to. */
2168
typedef struct png_control
2169
{
2170
png_structp png_ptr;
2171
png_infop info_ptr;
2172
png_voidp error_buf; /* Always a jmp_buf at present. */
2173
2174
png_const_bytep memory; /* Memory buffer. */
2175
png_size_t size; /* Size of the memory buffer. */
2176
2177
unsigned int for_write :1; /* Otherwise it is a read structure */
2178
unsigned int owned_file :1; /* We own the file in io_ptr */
2179
} png_control;
2180
2181
/* Return the pointer to the jmp_buf from a png_control: necessary because C
2182
* does not reveal the type of the elements of jmp_buf.
2183
*/
2184
#ifdef __cplusplus
2185
# define png_control_jmp_buf(pc) (((jmp_buf*)((pc)->error_buf))[0])
2186
#else
2187
# define png_control_jmp_buf(pc) ((pc)->error_buf)
2188
#endif
2189
2190
/* Utility to safely execute a piece of libpng code catching and logging any
2191
* errors that might occur. Returns true on success, false on failure (either
2192
* of the function or as a result of a png_error.)
2193
*/
2194
PNG_INTERNAL_CALLBACK(void,png_safe_error,(png_structp png_ptr,
2195
png_const_charp error_message),PNG_NORETURN);
2196
2197
#ifdef PNG_WARNINGS_SUPPORTED
2198
PNG_INTERNAL_CALLBACK(void,png_safe_warning,(png_structp png_ptr,
2199
png_const_charp warning_message),PNG_EMPTY);
2200
#else
2201
# define png_safe_warning 0/*dummy argument*/
2202
#endif
2203
2204
PNG_INTERNAL_FUNCTION(int,png_safe_execute,(png_imagep image,
2205
int (*function)(png_voidp), png_voidp arg),PNG_EMPTY);
2206
2207
/* Utility to log an error; this also cleans up the png_image; the function
2208
* always returns 0 (false).
2209
*/
2210
PNG_INTERNAL_FUNCTION(int,png_image_error,(png_imagep image,
2211
png_const_charp error_message),PNG_EMPTY);
2212
2213
/* Safely initialize a stdio pointer - used by both the read and the write
2214
* code.
2215
*/
2216
#ifdef PNG_STDIO_SUPPORTED
2217
PNG_INTERNAL_FUNCTION(int,png_image_init_io,(png_imagep image, png_FILE_p file),
2218
PNG_EMPTY);
2219
#endif /* STDIO */
2220
2221
#ifndef PNG_SIMPLIFIED_READ_SUPPORTED
2222
/* png_image_free is used by the write code but not exported */
2223
PNG_INTERNAL_FUNCTION(void, png_image_free, (png_imagep image), PNG_EMPTY);
2224
#endif /* !SIMPLIFIED_READ */
2225
2226
#endif /* SIMPLIFIED READ/WRITE */
2227
2228
#ifdef PNG_READ_SUPPORTED
2229
PNG_INTERNAL_FUNCTION(png_int_32, png_read_setting, (png_structrp png_ptr,
2230
png_uint_32 setting, png_uint_32 parameter, png_int_32 value), PNG_EMPTY);
2231
#endif /* READ */
2232
#ifdef PNG_WRITE_SUPPORTED
2233
PNG_INTERNAL_FUNCTION(png_int_32, png_write_setting, (png_structrp png_ptr,
2234
png_uint_32 setting, png_uint_32 parameter, png_int_32 value), PNG_EMPTY);
2235
/* Implementations of read and write settings, in pngrutil.c and pngwutil.c
2236
* respectively.
2237
*/
2238
#endif /* WRITE */
2239
2240
/* Maintainer: Put new private prototypes here ^ */
2241
2242
/* These are initialization functions for hardware specific PNG filter
2243
* optimizations; list these here then select the appropriate one at compile
2244
* time using the macro PNG_FILTER_OPTIMIZATIONS. If the macro is not defined
2245
* the generic code is used.
2246
*/
2247
#ifdef PNG_FILTER_OPTIMIZATIONS
2248
PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structrp png_ptr,
2249
unsigned int bpp), PNG_EMPTY);
2250
/* Just declare the optimization that will be used */
2251
#else
2252
/* List *all* the possible optimizations here - this branch is required if
2253
* the builder of libpng passes the definition of PNG_FILTER_OPTIMIZATIONS in
2254
* CFLAGS in place of CPPFLAGS *and* uses symbol prefixing.
2255
*/
2256
PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_neon,
2257
(png_structp png_ptr, unsigned int bpp), PNG_EMPTY);
2258
#endif
2259
2260
#include "pngdebug.h"
2261
2262
/* EXTENSION SPECIFIC FUNCTIONS */
2263
#include "arm/neon.h"
2264
2265
#ifdef __cplusplus
2266
}
2267
#endif
2268
2269
#endif /* PNG_VERSION_INFO_ONLY */
2270
#endif /* PNGPRIV_H */
2271
2272