-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnglite.h
184 lines (157 loc) · 5.73 KB
/
pnglite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* pnglite.h - Interface for pnglite library
Copyright (c) 2007 Daniel Karling
Copyright (c) 2019 Alexander Sabourenkov
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Daniel Karling
*/
#ifndef _PNGLITE_H_
#define _PNGLITE_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Enumerations for pnglite. */
/* Return values.
Negative numbers are error codes and 0 and up are okay responses. */
enum
{
PNG_DONE = 1,
PNG_NO_ERROR = 0,
PNG_FILE_ERROR = -1,
PNG_HEADER_ERROR = -2,
PNG_IO_ERROR = -3,
PNG_EOF_ERROR = -4,
PNG_CRC_ERROR = -5,
PNG_MEMORY_ERROR = -6,
PNG_ZLIB_ERROR = -7,
PNG_UNKNOWN_FILTER = -8,
PNG_TRNS_WRONG_COLORTYPE = -9,
PNG_NOT_SUPPORTED_16 = -10,
PNG_CORRUPTED = -11,
PNG_WRONG_ARGUMENTS = -12,
PNG_IMAGE_TOO_BIG = -13,
PNG_OVERSIZE_CHUNK = -14
};
/* The five different kinds of color storage in PNG files. */
enum {
PNG_GREYSCALE = 0,
PNG_TRUECOLOR = 2,
PNG_INDEXED = 3,
PNG_GREYSCALE_ALPHA = 4,
PNG_TRUECOLOR_ALPHA = 6
};
/* PNG filter types */
enum {
PNG_FILTER_NONE = 0,
PNG_FILTER_SUB = 1,
PNG_FILTER_UP = 2,
PNG_FILTER_AVERAGE = 3,
PNG_FILTER_PAETH = 4
};
/* Typedefs for callbacks. */
typedef size_t (*pnglite_read_callback_t)(void* output, size_t size, size_t numel, void* user_pointer);
typedef size_t (*pnglite_write_callback_t)(void* input, size_t size, size_t numel, void* user_pointer);
typedef void * (*pnglite_alloc_t)(size_t s);
typedef void (*pnglite_free_t)(void* p);
typedef struct {
void* zs; /* pointer to z_stream */
int zerr; /* last zlib call status */
const char* zmsg; /* message for the last err or NULL */
pnglite_read_callback_t read;
pnglite_write_callback_t write;
pnglite_alloc_t alloc;
pnglite_free_t free;
size_t chunk_size_limit;
size_t image_data_limit;
void* user_pointer;
unsigned char* png_data;
unsigned png_datalen;
unsigned char palette[4*256];
unsigned char colorkey[6];
unsigned width;
unsigned height;
unsigned palette_size;
unsigned char depth;
unsigned char color_type;
unsigned char transparency_present;
unsigned char compression_method;
unsigned char filter_method;
unsigned char interlace_method;
unsigned char stride;
unsigned pitch;
} pnglite_t;
/**
* Initializes a png_t object.
*
* @param png - A png_t structure to init.
* @param user_pointer - what to pass to reader/writer
* @param read_fun - a reader . may be 0 if writing
* @param write_fun - a writer. may be 0 if reading
* @param pngalloc - Pointer to custom allocation routine.
* If 0 is passed, malloc from libc will be used.
* @param pngfree - Pointer to custom free routine. If 0 is passed,
* free from libc will be used.
* @param chunk_size_limit - reject PNGs with chunks sized over this limit 0 = (1<<31)-1
* @param image_data_limit - reject PNGs where resulting image is over this limit. same as above.
*
* @return PNG_WRONG_ARGUMENTS if both reader and writer are 0; PNG_NO_ERROR otherwise.
*/
int pnglite_init(pnglite_t *png, void* user_pointer,
pnglite_read_callback_t read_fun, pnglite_read_callback_t write_fun,
pnglite_alloc_t pngalloc, pnglite_free_t pngfree,
size_t chunk_size_limit, size_t image_data_limit);
/**
* Reads and checks a header from the stream.
*
* @param png png_t object set for reading.
* @return PNG_NO_ERROR on success, otherwise an error code.
*/
int pnglite_read_header(pnglite_t* png);
/**
* Writes decoded image data into given buffer.
*
* @param png the png_t object
* @param data the output buffer,
* not less than width*height*(bytes per pixel) bytes.
*
* @return PNG_NO_ERROR on success, otherwise an error code.
*/
int pnglite_read_image(pnglite_t* png, unsigned char* data);
/**
* Writes out given image data.
*
* @param width
* @param height
* @param depth
* @param color
* @param transparency
* @param data
*
* @return PNG_NO_ERROR on success, otherwise an error code.
*/
int pnglite_write_image(pnglite_t* png, unsigned width, unsigned height, char depth, int color, int transparency, unsigned char* data);
/**
* Returns a string representation of an error code
*
* @param error - Error code.
*
* @return pointer to string.
*/
const char* pnglite_error_string(int error);
#ifdef __cplusplus
}
#endif
#endif