xref: /illumos-gate/usr/src/common/pnglite/pnglite.h (revision 3ce5372277f4657ad0e52d36c979527c4ca22de2)
1 /*
2  *  pnglite.h - Interface for pnglite library
3  *	Copyright (c) 2007 Daniel Karling
4  *
5  *	This software is provided 'as-is', without any express or implied
6  *	warranty. In no event will the authors be held liable for any damages
7  *	arising from the use of this software.
8  *
9  *	Permission is granted to anyone to use this software for any purpose,
10  *	including commercial applications, and to alter it and redistribute it
11  *	freely, subject to the following restrictions:
12  *
13  *	1. The origin of this software must not be misrepresented; you must not
14  *	   claim that you wrote the original software. If you use this software
15  *	   in a product, an acknowledgment in the product documentation would be
16  *	   appreciated but is not required.
17  *
18  *	2. Altered source versions must be plainly marked as such, and must not
19  *	   be misrepresented as being the original software.
20  *
21  *	3. This notice may not be removed or altered from any source
22  *	   distribution.
23  *
24  *	Daniel Karling
25  *	daniel.karling@gmail.com
26  */
27 
28 
29 #ifndef _PNGLITE_H_
30 #define	_PNGLITE_H_
31 
32 #include <string.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  *	Enumerations for pnglite.
40  *	Negative numbers are error codes and 0 and up are okay responses.
41  */
42 
43 enum {
44 	PNG_DONE			= 1,
45 	PNG_NO_ERROR			= 0,
46 	PNG_FILE_ERROR			= -1,
47 	PNG_HEADER_ERROR		= -2,
48 	PNG_IO_ERROR			= -3,
49 	PNG_EOF_ERROR			= -4,
50 	PNG_CRC_ERROR			= -5,
51 	PNG_MEMORY_ERROR		= -6,
52 	PNG_ZLIB_ERROR			= -7,
53 	PNG_UNKNOWN_FILTER		= -8,
54 	PNG_NOT_SUPPORTED		= -9,
55 	PNG_WRONG_ARGUMENTS		= -10
56 };
57 
58 /*
59  *	The five different kinds of color storage in PNG files.
60  */
61 
62 enum {
63 	PNG_GREYSCALE			= 0,
64 	PNG_TRUECOLOR			= 2,
65 	PNG_INDEXED			= 3,
66 	PNG_GREYSCALE_ALPHA		= 4,
67 	PNG_TRUECOLOR_ALPHA		= 6
68 };
69 
70 typedef struct {
71 	void			*zs;		/* pointer to z_stream */
72 	int			fd;
73 	unsigned char		*image;
74 
75 	unsigned char		*png_data;
76 	unsigned		png_datalen;
77 
78 	unsigned		width;
79 	unsigned		height;
80 	unsigned char		depth;
81 	unsigned char		color_type;
82 	unsigned char		compression_method;
83 	unsigned char		filter_method;
84 	unsigned char		interlace_method;
85 	unsigned char		bpp;
86 
87 	unsigned char		*readbuf;
88 	unsigned		readbuflen;
89 } png_t;
90 
91 
92 /*
93  *	Function: png_open
94  *
95  *	This function is used to open a png file with the internal file
96  *	IO system.
97  *
98  *	Parameters:
99  *		png - Empty png_t struct.
100  *		filename - Filename of the file to be opened.
101  *
102  *	Returns:
103  *		PNG_NO_ERROR on success, otherwise an error code.
104  */
105 
106 int png_open(png_t *png, const char *filename);
107 
108 /*
109  *	Function: png_print_info
110  *
111  *	This function prints some info about the opened png file to stdout.
112  *
113  *	Parameters:
114  *		png - png struct to get info from.
115  */
116 
117 void png_print_info(png_t *png);
118 
119 /*
120  *	Function: png_error_string
121  *
122  *	This function translates an error code to a human readable string.
123  *
124  *	Parameters:
125  *		error - Error code.
126  *
127  *	Returns:
128  *		Pointer to string.
129  */
130 
131 char *png_error_string(int error);
132 
133 /*
134  *	Function: png_get_data
135  *
136  *	This function decodes the opened png file and stores the result in data.
137  *	data should be big enough to hold the decoded png.
138  *	Required size will be:
139  *
140  *	> width*height*(bytes per pixel)
141  *
142  *	Parameters:
143  *		data - Where to store result.
144  *
145  *	Returns:
146  *		PNG_NO_ERROR on success, otherwise an error code.
147  */
148 
149 int png_get_data(png_t *png, uint8_t *data);
150 
151 /*
152  *	Function: png_close
153  *
154  *	Closes an open png file pointer.
155  *
156  *	Parameters:
157  *		png - png to close.
158  *
159  *	Returns:
160  *		PNG_NO_ERROR
161  */
162 
163 int png_close(png_t *png);
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif /* _PNGLITE_H_ */
170