Gamma and other image manipulation commands
[TeXnicard.git] / lodepng / lodepng.h
blob03f2a68056b7b5705ba134dde19ba917ffb65a00
1 /*
2 LodePNG version 20110221
4 Copyright (c) 2005-2011 Lode Vandevenne
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
22 3. This notice may not be removed or altered from any source
23 distribution.
26 #ifndef LODEPNG_H
27 #define LODEPNG_H
29 #include <string.h> /*for size_t*/
31 #ifdef __cplusplus
32 #include <vector>
33 #include <string>
34 #endif /*__cplusplus*/
36 /* ////////////////////////////////////////////////////////////////////////// */
37 /* Code Sections */
38 /* ////////////////////////////////////////////////////////////////////////// */
41 The following #defines are used to create code sections. They can be disabled
42 to disable code sections, which can give faster compile time and smaller binary.
43 Also, some text editors allow expanding/collapsing #ifdef sections.
46 #define LODEPNG_COMPILE_ZLIB /*deflate&zlib encoder and deflate&zlib decoder*/
47 #define LODEPNG_COMPILE_PNG /*png encoder and png decoder*/
48 #define LODEPNG_COMPILE_DECODER /*deflate&zlib decoder and png decoder*/
49 #define LODEPNG_COMPILE_ENCODER /*deflate&zlib encoder and png encoder*/
50 #define LODEPNG_COMPILE_DISK /*the optional built in harddisk file loading and saving functions*/
51 //#define LODEPNG_COMPILE_ANCILLARY_CHUNKS /*any code or struct datamember related to chunks other than IHDR, IDAT, PLTE, tRNS, IEND*/
52 //#define LODEPNG_COMPILE_UNKNOWN_CHUNKS /*handling of unknown chunks*/
53 //#define LODEPNG_COMPILE_ERROR_TEXT /*ability to convert error numerical codes to English text string*/
55 /* ////////////////////////////////////////////////////////////////////////// */
56 /* Simple Functions */
57 /* ////////////////////////////////////////////////////////////////////////// */
60 This are the simple functions, they can be used directly to convert raw data
61 to/from PNG data. Both the C and C++ simple functions are declared here.
63 If more flexibility and settings are required, then the more advanced interface
64 below this "simple" part has to be used.
67 #ifdef LODEPNG_COMPILE_PNG
68 #ifdef LODEPNG_COMPILE_DECODER
71 LodePNG_decode
72 Converts PNG data in memory to raw pixel data.
73 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
74 Its size is w * h * (bytes per pixel), bytes per pixel depends on colorType and bitDepth.
75 Must be freed after usage with free(*out).
76 w: Output parameter. Pointer to width of pixel data.
77 h: Output parameter. Pointer to height of pixel data.
78 in: Memory buffer with the PNG file.
79 insize: size of the in buffer.
80 colorType: the desired color type for the raw output image. See explanation on PNG color types.
81 bitDepth: the desired bit depth for the raw output image. See explanation on PNG color types.
82 Return value: LodePNG error code (0 means no error).
84 unsigned LodePNG_decode(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize, unsigned colorType, unsigned bitDepth); /*return value is error*/
87 LodePNG_decode32
88 Converts PNG data in memory to 32-bit raw pixel data.
89 Same as LodePNG_decode, but uses colorType = 6 and bitDepth = 8 by default.
90 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
91 Its size is w * h * 4 bytes.
92 Must be freed after usage with free(*out).
93 w: Output parameter. Pointer to width of pixel data.
94 h: Output parameter. Pointer to height of pixel data.
95 in: Memory buffer with the PNG file.
96 insize: size of the in buffer.
97 Return value: LodePNG error code (0 means no error).
99 unsigned LodePNG_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize); /*return value is error*/
101 #ifdef LODEPNG_COMPILE_DISK
104 LodePNG_decode_file
105 Load PNG from disk, from file with given name.
106 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
107 Its size is w * h * (bytes per pixel), bytes per pixel depends on colorType and bitDepth.
108 Must be freed after usage with free(*out).
109 w: Output parameter. Pointer to width of pixel data.
110 h: Output parameter. Pointer to height of pixel data.
111 filename: Path on disk of the PNG file.
112 colorType: the desired color type for the raw output image. See explanation on PNG color types.
113 bitDepth: the desired bit depth for the raw output image. See explanation on PNG color types.
114 Return value: LodePNG error code (0 means no error).
116 unsigned LodePNG_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename, unsigned colorType, unsigned bitDepth);
119 LodePNG_decode32_file
120 Load PNG from disk to 32-bit RGBA pixel buffer, from file with given name.
121 Same as LodePNG_decode_file, but uses colorType = 6 and bitDepth = 8 by default.
122 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
123 Its size is w * h * 4 bytes.
124 Must be freed after usage with free(*out).
125 w: Output parameter. Pointer to width of pixel data.
126 h: Output parameter. Pointer to height of pixel data.
127 in: Memory buffer with the PNG file.
128 insize: size of the in buffer.
129 Return value: LodePNG error code (0 means no error).
131 unsigned LodePNG_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename);
133 #endif /*LODEPNG_COMPILE_DISK*/
134 #endif /*LODEPNG_COMPILE_DECODER*/
135 #ifdef LODEPNG_COMPILE_ENCODER
138 LodePNG_encode
139 Converts raw pixel data into a PNG image in memory. The colorType and bitDepth
140 of the output PNG image cannot be chosen, they are automatically determined
141 by the colorType, bitDepth and content of the input pixel data.
142 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
143 Must be freed after usage with free(*out).
144 outsize: Output parameter. Pointer to the size in bytes of the out buffer.
145 image: The raw pixel data to encode. The size of this buffer should be
146 w * h * (bytes per pixel), bytes per pixel depends on colorType and bitDepth.
147 w: width of the raw pixel data in pixels.
148 h: height of the raw pixel data in pixels.
149 colorType: the color type of the raw input image. See explanation on PNG color types.
150 bitDepth: the bit depth of the raw input image. See explanation on PNG color types.
151 Return value: LodePNG error code (0 means no error).
153 unsigned LodePNG_encode(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h, unsigned colorType, unsigned bitDepth); /*return value is error*/
156 LodePNG_encode32
157 Converts 32-bit RGBA raw pixel data into a PNG image in memory.
158 Same as LodePNG_encode, but uses colorType = 6 and bitDepth = 8 by default.
159 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
160 Must be freed after usage with free(*out).
161 outsize: Output parameter. Pointer to the size in bytes of the out buffer.
162 image: The raw pixel data to encode. The size of this buffer should be w * h * 4 bytes.
163 w: width of the raw pixel data in pixels.
164 h: height of the raw pixel data in pixels.
165 Return value: LodePNG error code (0 means no error).
167 unsigned LodePNG_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h); /*return value is error*/
169 #ifdef LODEPNG_COMPILE_DISK
172 LodePNG_encode_file
173 Converts raw pixel data into a PNG file on disk. Same as LodePNG_encode, but
174 outputs to disk instead of memory buffer.
175 filename: path to file on disk to write the PNG image to.
176 image: The raw pixel data to encode. The size of this buffer should be
177 w * h * (bytes per pixel), bytes per pixel depends on colorType and bitDepth.
178 w: width of the raw pixel data in pixels.
179 h: height of the raw pixel data in pixels.
180 colorType: the color type of the raw input image. See explanation on PNG color types.
181 bitDepth: the bit depth of the raw input image. See explanation on PNG color types.
182 Return value: LodePNG error code (0 means no error).
184 unsigned LodePNG_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h, unsigned colorType, unsigned bitDepth);
187 LodePNG_encode_file
188 Converts 32-bit RGBA raw pixel data into a PNG file on disk. Same as LodePNG_encode_file,
189 but uses colorType = 6 and bitDepth = 8 by default.
190 filename: path to file on disk to write the PNG image to.
191 image: The raw pixel data to encode. The size of this buffer should be w * h * 4 bytes.
192 w: width of the raw pixel data in pixels.
193 h: height of the raw pixel data in pixels.
194 Return value: LodePNG error code (0 means no error).
196 unsigned LodePNG_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h);
197 #endif /*LODEPNG_COMPILE_DISK*/
198 #endif /*LODEPNG_COMPILE_ENCODER*/
201 #ifdef __cplusplus
202 namespace LodePNG
204 #ifdef LODEPNG_COMPILE_DECODER
207 LodePNG::decode
208 Converts PNG data in memory to raw pixel data.
209 out: Output parameter, std::vector containing the raw pixel data. Its size
210 will be w * h * (bytes per pixel), where bytes per pixel is 4 if the default
211 colorType=6 and bitDepth=8 is used. The pixels are 32-bit RGBA bit in that case.
212 w: Output parameter, width of the image in pixels.
213 h: Output parameter, height of the image in pixels.
214 in: Memory buffer with the PNG file.
215 insize: size of the in buffer.
216 colorType: the desired color type for the raw output image. See explanation on PNG color types.
217 bitDepth: the desired bit depth for the raw output image. See explanation on PNG color types.
218 Return value: LodePNG error code (0 means no error).
220 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const unsigned char* in, size_t insize, unsigned colorType = 6, unsigned bitDepth = 8);
223 LodePNG::decode
224 Exactly the same as the decode function that takes a unsigned char buffer, but instead of giving
225 a pointer and a size, this takes the input buffer as an std::vector.
227 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::vector<unsigned char>& in, unsigned colorType = 6, unsigned bitDepth = 8);
228 #ifdef LODEPNG_COMPILE_DISK
230 LodePNG::decode
231 Converts PNG file from disk to raw pixel data in memory.
232 out: Output parameter, std::vector containing the raw pixel data. Its size
233 will be w * h * (bytes per pixel), where bytes per pixel is 4 if the default
234 colorType=6 and bitDepth=8 is used. The pixels are 32-bit RGBA bit in that case.
235 w: Output parameter, width of the image in pixels.
236 h: Output parameter, height of the image in pixels.
237 filename: Path to PNG file on disk.
238 colorType: the desired color type for the raw output image. See explanation on PNG color types.
239 bitDepth: the desired bit depth for the raw output image. See explanation on PNG color types.
240 Return value: LodePNG error code (0 means no error).
242 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename, unsigned colorType = 6, unsigned bitDepth = 8);
243 #endif //LODEPNG_COMPILE_DISK
244 #endif //LODEPNG_COMPILE_DECODER
246 #ifdef LODEPNG_COMPILE_ENCODER
249 LodePNG::encode
250 Converts 32-bit RGBA raw pixel data into a PNG image in memory.
251 out: Output parameter, std::vector containing the PNG image data.
252 in: Memory buffer with raw pixel data. The size of this buffer should be
253 w * h * (bytes per pixel), With the default colorType=6 and bitDepth=8, bytes
254 per pixel should be 4 and the data is a 32-bit RGBA pixel buffer.
255 w: Width of the image in pixels.
256 h: Height of the image in pixels.
257 colorType: the color type of the raw input image. See explanation on PNG color types.
258 bitDepth: the bit depth of the raw input image. See explanation on PNG color types.
259 Return value: LodePNG error code (0 means no error).
261 unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h, unsigned colorType = 6, unsigned bitDepth = 8);
264 LodePNG::encode
265 Exactly the same as the encode function that takes a unsigned char buffer, but instead of giving
266 a pointer and a size, this takes the input buffer as an std::vector.
268 unsigned encode(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, unsigned w, unsigned h, unsigned colorType = 6, unsigned bitDepth = 8);
269 #ifdef LODEPNG_COMPILE_DISK
271 LodePNG::encode
272 Converts 32-bit RGBA raw pixel data into a PNG file on disk.
273 filename: Path to the file to write the PNG image to.
274 in: Memory buffer with raw pixel data. The size of this buffer should be
275 w * h * (bytes per pixel), With the default colorType=6 and bitDepth=8, bytes
276 per pixel should be 4 and the data is a 32-bit RGBA pixel buffer.
277 w: Width of the image in pixels.
278 h: Height of the image in pixels.
279 colorType: the color type of the raw input image. See explanation on PNG color types.
280 bitDepth: the bit depth of the raw input image. See explanation on PNG color types.
281 Return value: LodePNG error code (0 means no error).
283 unsigned encode(const std::string& filename, const unsigned char* in, unsigned w, unsigned h, unsigned colorType = 6, unsigned bitDepth = 8);
286 LodePNG::encode
287 Exactly the same as the encode function that takes a unsigned char buffer, but instead of giving
288 a pointer and a size, this takes the input buffer as an std::vector.
290 unsigned encode(const std::string& filename, const std::vector<unsigned char>& in, unsigned w, unsigned h, unsigned colorType = 6, unsigned bitDepth = 8);
291 #endif //LODEPNG_COMPILE_DISK
292 #endif //LODEPNG_COMPILE_ENCODER
293 } //namespace LodePNG
294 #endif /*__cplusplus*/
295 #endif /*LODEPNG_COMPILE_PNG*/
297 #ifdef LODEPNG_COMPILE_ERROR_TEXT
300 error_text: returns a textual description of the error code, in English. The
301 numerical value of the code itself is not included in this description.
303 const char* LodePNG_error_text(unsigned code);
305 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/
307 /* ////////////////////////////////////////////////////////////////////////// */
308 /* Inflate & Deflate Setting Structs */
309 /* ////////////////////////////////////////////////////////////////////////// */
312 These structs contain settings for the decompression and compression of the
313 PNG files. Typically you won't need these directly.
316 #ifdef LODEPNG_COMPILE_DECODER
317 typedef struct LodeZlib_DecompressSettings
319 unsigned ignoreAdler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/
320 } LodeZlib_DecompressSettings;
322 extern const LodeZlib_DecompressSettings LodeZlib_defaultDecompressSettings;
323 void LodeZlib_DecompressSettings_init(LodeZlib_DecompressSettings* settings);
324 #endif /*LODEPNG_COMPILE_DECODER*/
326 #ifdef LODEPNG_COMPILE_ENCODER
328 LodeZlib_CompressSettings
329 Compression settings. Tweaking these settings tweaks the balance between
330 speed and compression ratio.
332 typedef struct LodeZlib_CompressSettings /*deflate = compress*/
334 /*LZ77 related settings*/
335 unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard)*/
336 unsigned useLZ77; /*whether or not to use LZ77. Should be 1 for good compression.*/
337 unsigned windowSize; /*the maximum is 32768, higher gives more compression but is slower*/
338 } LodeZlib_CompressSettings;
340 extern const LodeZlib_CompressSettings LodeZlib_defaultCompressSettings;
341 void LodeZlib_CompressSettings_init(LodeZlib_CompressSettings* settings);
342 #endif /*LODEPNG_COMPILE_ENCODER*/
344 #ifdef LODEPNG_COMPILE_PNG
346 /* ////////////////////////////////////////////////////////////////////////// */
347 /* PNG and Raw Image Information Structs */
348 /* ////////////////////////////////////////////////////////////////////////// */
351 LodePNG_InfoColor
352 Info about the color type of an image.
353 The same LodePNG_InfoColor struct is used for both the PNG and raw image type,
354 even though they are two totally different things.
356 typedef struct LodePNG_InfoColor
358 /*header (IHDR)*/
359 unsigned colorType; /*color type, see PNG standard or documentation further in this header file*/
360 unsigned bitDepth; /*bits per sample, see PNG standard or documentation further in this header file*/
363 palette (PLTE)
365 This is a dynamically allocated unsigned char array with the colors of the palette. The value palettesize
366 indicates the amount of colors in the palette. The allocated size of the buffer is 4 * palettesize bytes,
367 because there are 4 values per color: R, G, B and A. Even if less color channels are used, the palette
368 is always in RGBA format, in the order RGBARGBARGBA.....
370 When encoding a PNG, to store your colors in the palette of the LodePNG_InfoRaw, first use
371 LodePNG_InfoColor_clearPalette, then for each color use LodePNG_InfoColor_addPalette.
372 In the C++ version the Encoder class also has the above functions available directly in its interface.
374 The palette information from the tRNS chunk is also already included in this palette vector.
376 If you encode an image with palette, don't forget that you have to set the alpha channels (A) of the palette
377 too, set them to 255 for an opaque palette. If you leave them at zero, the image will be encoded as
378 fully invisible. This both for the palette in the infoRaw and the infoPng if the png is to have a palette.
380 When decoding, by default you can ignore this information, since LodePNG gives the
381 raw output as RGBA pixels by default and already fills the palette values in them.
383 unsigned char* palette; /*palette in RGBARGBA... order*/
384 size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/
387 transparent color key (tRNS)
388 This color is 8-bit for 8-bit PNGs, 16-bit for 16-bit per channel PNGs.
389 For greyscale PNGs, r, g and b will all 3 be set to the same.
391 When decoding, by default you can ignore this information, since LodePNG sets
392 pixels with this key to transparent already in the raw RGBA output.
394 unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/
395 unsigned key_r; /*red/greyscale component of color key*/
396 unsigned key_g; /*green component of color key*/
397 unsigned key_b; /*blue component of color key*/
398 } LodePNG_InfoColor;
400 /*init, cleanup and copy functions to use with this struct*/
401 void LodePNG_InfoColor_init(LodePNG_InfoColor* info);
402 void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info);
403 unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor* dest, const LodePNG_InfoColor* source); /*return value is error code (0 means no error)*/
405 /*Use these functions instead of allocating palette manually*/
406 void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor* info);
407 unsigned LodePNG_InfoColor_addPalette(LodePNG_InfoColor* info, unsigned char r, unsigned char g, unsigned char b, unsigned char a); /*add 1 color to the palette*/
409 /*additional color info*/
410 unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor* info); /*get the total amount of bits per pixel, based on colorType and bitDepth in the struct*/
411 unsigned LodePNG_InfoColor_getChannels(const LodePNG_InfoColor* info); /*get the amount of color channels used, based on colorType in the struct. If a palette is used, it counts as 1 channel.*/
412 unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor* info); /*is it a greyscale type? (only colorType 0 or 4)*/
413 unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor* info); /*has it got an alpha channel? (only colorType 2 or 6)*/
414 unsigned LodePNG_InfoColor_isPaletteType(const LodePNG_InfoColor* info); /*has it got a palette? (only colorType 3)*/
415 unsigned LodePNG_InfoColor_hasPaletteAlpha(const LodePNG_InfoColor* info); /*only returns true if there is a palette and there is a value in the palette with alpha < 255. Loops through the palette to check this.*/
418 LodePNG_InfoColor_canHaveAlpha
419 Check if the given color info indicates the possibility of having non-opaque pixels in the PNG image.
420 Returns true if the image can have translucent or invisible pixels (it still be opaque if it doesn't use such pixels).
421 Returns false if the image can only have opaque pixels.
422 In detail, it returns true only if it's a color type with alpha, or has a palette with non-opaque values, or if "key_defined" is true.
424 unsigned LodePNG_InfoColor_canHaveAlpha(const LodePNG_InfoColor* info);
426 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
428 LodePNG_Time
429 The information of a Time chunk in PNG.
430 To make the encoder add a time chunk, set time_defined to 1 and fill in
431 the correct values in all the time parameters. LodePNG will not fill the current
432 time in these values itself, all it does is copy them over into the chunk bytes.
434 typedef struct LodePNG_Time
436 unsigned year; /*2 bytes used (0-65535)*/
437 unsigned char month; /*1-12*/
438 unsigned char day; /*1-31*/
439 unsigned char hour; /*0-23*/
440 unsigned char minute; /*0-59*/
441 unsigned char second; /*0-60 (to allow for leap seconds)*/
442 } LodePNG_Time;
445 LodePNG_Text
446 Info about text chunks in a PNG file. The arrays can contain multiple keys
447 and strings. The amount of keys and strings is the same. The amount of strings
448 ends when the pointer to the string is a null pointer.
450 They keyword of text chunks gives a short description what the actual text
451 represents. There are a few standard standard keywords recognised
452 by many programs: Title, Author, Description, Copyright, Creation Time,
453 Software, Disclaimer, Warning, Source, Comment. It's allowed to use other keys.
455 A keyword is minimum 1 character and maximum 79 characters long. It's
456 discouraged to use a single line length longer than 79 characters for texts.
458 typedef struct LodePNG_Text /*non-international text*/
460 /*Don't allocate these text buffers yourself. Use the init/cleanup functions
461 correctly and use LodePNG_Text_add and LodePNG_Text_clear.*/
462 size_t num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/
463 char** keys; /*the keyword of a text chunk (e.g. "Comment")*/
464 char** strings; /*the actual text*/
465 } LodePNG_Text;
467 /*init, cleanup and copy functions to use with this struct*/
468 void LodePNG_Text_init(LodePNG_Text* text);
469 void LodePNG_Text_cleanup(LodePNG_Text* text);
470 unsigned LodePNG_Text_copy(LodePNG_Text* dest, const LodePNG_Text* source); /*return value is error code (0 means no error)*/
472 /*Use these functions instead of allocating the char**s manually*/
473 void LodePNG_Text_clear(LodePNG_Text* text); /*use this to clear the texts again after you filled them in*/
474 unsigned LodePNG_Text_add(LodePNG_Text* text, const char* key, const char* str); /*push back both texts at once*/
477 LodePNG_IText
478 Info about international text chunks in a PNG file. The arrays can contain multiple keys
479 and strings. The amount of keys, lengtags, transkeys and strings is the same.
480 The amount of strings ends when the pointer to the string is a null pointer.
482 A keyword is minimum 1 character and maximum 79 characters long. It's
483 discouraged to use a single line length longer than 79 characters for texts.
485 typedef struct LodePNG_IText /*international text*/
487 /*Don't allocate these text buffers yourself. Use the init/cleanup functions
488 correctly and use LodePNG_IText_add and LodePNG_IText_clear.*/
489 size_t num; /*the amount of international texts in this PNG*/
490 char** keys; /*the English keyword of the text chunk (e.g. "Comment")*/
491 char** langtags; /*the language tag for this text's international language, ISO/IEC 646 string, e.g. ISO 639 language tag*/
492 char** transkeys; /*keyword translated to the international language - UTF-8 string*/
493 char** strings; /*the actual international text - UTF-8 string*/
494 } LodePNG_IText;
496 /*init, cleanup and copy functions to use with this struct*/
497 void LodePNG_IText_init(LodePNG_IText* text);
498 void LodePNG_IText_cleanup(LodePNG_IText* text);
499 unsigned LodePNG_IText_copy(LodePNG_IText* dest, const LodePNG_IText* source); /*return value is error code (0 means no error)*/
501 /*Use these functions instead of allocating the char**s manually*/
502 void LodePNG_IText_clear(LodePNG_IText* text); /*use this to clear the itexts again after you filled them in*/
503 unsigned LodePNG_IText_add(LodePNG_IText* text, const char* key, const char* langtag, const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/
504 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
506 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
508 LodePNG_UnknownChunks
509 Unknown chunks read from the PNG, or extra chunks the user wants to have added
510 in the encoded PNG.
512 typedef struct LodePNG_UnknownChunks
514 /*There are 3 buffers, one for each position in the PNG where unknown chunks can appear
515 each buffer contains all unknown chunks for that position consecutively
516 The 3 buffers are the unknown chunks between certain critical chunks:
517 0: IHDR-PLTE, 1: PLTE-IDAT, 2: IDAT-IEND
519 Do not allocate or traverse this data yourself. Use the chunk traversing functions declared
520 later, such as LodePNG_chunk_next and LodePNG_append_chunk, to read/write this struct.
522 unsigned char* data[3];
523 size_t datasize[3]; /*size in bytes of the unknown chunks, given for protection*/
525 } LodePNG_UnknownChunks;
527 /*init, cleanup and copy functions to use with this struct*/
528 void LodePNG_UnknownChunks_init(LodePNG_UnknownChunks* chunks);
529 void LodePNG_UnknownChunks_cleanup(LodePNG_UnknownChunks* chunks);
530 unsigned LodePNG_UnknownChunks_copy(LodePNG_UnknownChunks* dest, const LodePNG_UnknownChunks* src); /*return value is error code (0 means no error)*/
531 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
534 LodePNG_InfoPng
535 Information about the PNG image, except pixels and sometimes except width and height.
537 typedef struct LodePNG_InfoPng
539 /*header (IHDR), palette (PLTE) and transparency (tRNS)*/
542 Note: width and height are only used as information of a decoded PNG image. When encoding one, you don't have
543 to specify width and height in an LodePNG_Info struct, but you give them as parameters of the encode function.
544 The rest of the LodePNG_Info struct IS used by the encoder though!
546 unsigned width; /*width of the image in pixels (ignored by encoder, but filled in by decoder)*/
547 unsigned height; /*height of the image in pixels (ignored by encoder, but filled in by decoder)*/
548 unsigned compressionMethod; /*compression method of the original file. Always 0.*/
549 unsigned filterMethod; /*filter method of the original file*/
550 unsigned interlaceMethod; /*interlace method of the original file*/
551 LodePNG_InfoColor color; /*color type and bits, palette and transparency of the PNG file*/
553 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
556 suggested background color (bKGD)
557 This color is 8-bit for 8-bit PNGs, 16-bit for 16-bit PNGs
559 For greyscale PNGs, r, g and b will all 3 be set to the same. When encoding
560 the encoder writes the red one. For palette PNGs: When decoding, the RGB value
561 will be stored, not a palette index. But when encoding, specify the index of
562 the palette in background_r, the other two are then ignored.
564 The decoder does not use this background color to edit the color of pixels.
566 unsigned background_defined; /*is a suggested background color given?*/
567 unsigned background_r; /*red component of suggested background color*/
568 unsigned background_g; /*green component of suggested background color*/
569 unsigned background_b; /*blue component of suggested background color*/
571 /*non-international text chunks (tEXt and zTXt)*/
572 LodePNG_Text text;
574 /*international text chunks (iTXt)*/
575 LodePNG_IText itext;
577 /*time chunk (tIME)*/
578 unsigned char time_defined; /*if 0, no tIME chunk was or will be generated in the PNG image*/
579 LodePNG_Time time;
581 /*phys chunk (pHYs)*/
582 unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/
583 unsigned phys_x; /*pixels per unit in x direction*/
584 unsigned phys_y; /*pixels per unit in y direction*/
585 unsigned char phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/
587 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
589 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
590 /*unknown chunks*/
591 LodePNG_UnknownChunks unknown_chunks;
592 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
594 } LodePNG_InfoPng;
596 /*init, cleanup and copy functions to use with this struct*/
597 void LodePNG_InfoPng_init(LodePNG_InfoPng* info);
598 void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info);
599 unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng* dest, const LodePNG_InfoPng* source); /*return value is error code (0 means no error)*/
602 LodePNG_InfoRaw
603 Contains user-chosen information about the raw image data, which is independent of the PNG image
604 With raw images, I mean the image data in the form of the simple raw buffer to which the
605 compressed PNG data is decoded, or from which a PNG image can be encoded.
607 typedef struct LodePNG_InfoRaw
609 LodePNG_InfoColor color; /*color info of the raw image, note that the same struct as for PNG data is used.*/
610 } LodePNG_InfoRaw;
612 /*init, cleanup and copy functions to use with this struct*/
613 void LodePNG_InfoRaw_init(LodePNG_InfoRaw* info);
614 void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw* info);
615 unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw* dest, const LodePNG_InfoRaw* source); /*return value is error code (0 means no error)*/
618 LodePNG_convert
619 Converts raw buffer from one color type to another color type, based on
620 LodePNG_InfoColor structs to describe the input and output color type.
621 See the reference manual at the end of this header file to see which color conversions are supported.
622 return value = LodePNG error code (0 if all went ok, an error if the conversion isn't supported)
623 The out buffer must have size (w * h * bpp + 7) / 8, where bpp is the bits per pixel
624 of the output color type (LodePNG_InfoColor_getBpp)
626 unsigned LodePNG_convert(unsigned char* out, const unsigned char* in, LodePNG_InfoColor* infoOut, LodePNG_InfoColor* infoIn, unsigned w, unsigned h);
628 #ifdef LODEPNG_COMPILE_DECODER
630 /* ////////////////////////////////////////////////////////////////////////// */
631 /* LodePNG Decoder */
632 /* ////////////////////////////////////////////////////////////////////////// */
635 Settings for the decoder. This contains settings for the PNG and the Zlib
636 decoder, but not the Info settings from the Info structs.
638 typedef struct LodePNG_DecodeSettings
640 LodeZlib_DecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/
642 unsigned ignoreCrc; /*ignore CRC checksums*/
643 unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/
645 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
646 unsigned readTextChunks; /*if false but rememberUnknownChunks is true, they're stored in the unknown chunks*/
647 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
649 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
650 unsigned rememberUnknownChunks; /*store all bytes from unknown chunks in the InfoPng (off by default, useful for a png editor)*/
651 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
652 } LodePNG_DecodeSettings;
654 void LodePNG_DecodeSettings_init(LodePNG_DecodeSettings* settings);
657 The LodePNG_Decoder struct has most input and output parameters the decoder uses,
658 such as the settings, the info of the PNG and the raw data, and the error. Only
659 the pixel buffer is not contained in this struct.
661 typedef struct LodePNG_Decoder
663 LodePNG_DecodeSettings settings; /*the decoding settings*/
664 LodePNG_InfoRaw infoRaw; /*specifies the format in which you would like to get the raw pixel buffer*/
665 LodePNG_InfoPng infoPng; /*info of the PNG image obtained after decoding*/
666 unsigned error;
667 } LodePNG_Decoder;
669 /*init, cleanup and copy functions to use with this struct*/
670 void LodePNG_Decoder_init(LodePNG_Decoder* decoder);
671 void LodePNG_Decoder_cleanup(LodePNG_Decoder* decoder);
672 void LodePNG_Decoder_copy(LodePNG_Decoder* dest, const LodePNG_Decoder* source);
675 LodePNG_Decoder_decode
676 Decode based on a LodePNG_Decoder.
677 This function allocates the out buffer and stores the size in *outsize. This buffer
678 needs to be freed after usage.
679 Other information about the PNG file, such as the size, colorType and extra chunks
680 are stored in the infoPng field of the LodePNG_Decoder.
682 void LodePNG_Decoder_decode(LodePNG_Decoder* decoder, unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize);
685 LodePNG_Decoder_inspect
686 Read the PNG header, but not the actual data. This returns only the information
687 that is in the header chunk of the PNG, such as width, height and color type. The
688 information is placed in the infoPng field of the LodePNG_Decoder.
690 void LodePNG_Decoder_inspect(LodePNG_Decoder* decoder, const unsigned char* in, size_t insize); /*read the png header*/
692 #endif /*LODEPNG_COMPILE_DECODER*/
694 #ifdef LODEPNG_COMPILE_ENCODER
696 /* ////////////////////////////////////////////////////////////////////////// */
697 /* LodePNG Encoder */
698 /* ////////////////////////////////////////////////////////////////////////// */
701 LodePNG_EncodeSettings
702 Extra settings used by the encoder.
704 typedef struct LodePNG_EncodeSettings
706 LodeZlib_CompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/
708 unsigned autoLeaveOutAlphaChannel; /*automatically use color type without alpha instead of given one, if given image is opaque*/
709 unsigned force_palette; /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette). If colortype is 3, PLTE is _always_ created.*/
710 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
711 unsigned add_id; /*add LodePNG version as text chunk*/
712 unsigned text_compression; /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/
713 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
714 } LodePNG_EncodeSettings;
716 void LodePNG_EncodeSettings_init(LodePNG_EncodeSettings* settings);
719 LodePNG_Encoder
720 This struct has most input and output parameters the encoder uses,
721 such as the settings, the info of the PNG and the raw data, and the error. Only
722 the pixel buffer is not contained in this struct.
724 typedef struct LodePNG_Encoder
726 LodePNG_EncodeSettings settings; /*compression settings of the encoder*/
727 LodePNG_InfoPng infoPng; /*the info specified by the user is not changed by the encoder. The encoder will try to generate a PNG close to the given info.*/
728 LodePNG_InfoRaw infoRaw; /*put the properties of the input raw image in here*/
729 unsigned error; /*error value filled in if error happened, or 0 if all went ok*/
730 } LodePNG_Encoder;
732 /*init, cleanup and copy functions to use with this struct*/
733 void LodePNG_Encoder_init(LodePNG_Encoder* encoder);
734 void LodePNG_Encoder_cleanup(LodePNG_Encoder* encoder);
735 void LodePNG_Encoder_copy(LodePNG_Encoder* dest, const LodePNG_Encoder* source);
737 /*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/
738 void LodePNG_Encoder_encode(LodePNG_Encoder* encoder, unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h);
740 #endif /*LODEPNG_COMPILE_ENCODER*/
742 /* ////////////////////////////////////////////////////////////////////////// */
743 /* Chunk Traversing Utilities */
744 /* ////////////////////////////////////////////////////////////////////////// */
747 LodePNG_chunk functions:
748 These functions need as input a large enough amount of allocated memory.
749 These functions can be used on raw PNG data, but they are exposed in the API
750 because they are needed if you want to traverse the unknown chunks stored
751 in the LodePNG_UnknownChunks struct, or add new ones to it.
754 unsigned LodePNG_chunk_length(const unsigned char* chunk); /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/
756 void LodePNG_chunk_type(char type[5], const unsigned char* chunk); /*puts the 4-byte type in null terminated string*/
757 unsigned char LodePNG_chunk_type_equals(const unsigned char* chunk, const char* type); /*check if the type is the given type*/
760 These functions get properties of PNG chunks gotten from capitalization of chunk
761 type name, as defined by the PNG standard.
763 unsigned char LodePNG_chunk_critical(const unsigned char* chunk); /*0: ancillary chunk, 1: it's one of the critical chunk types*/
764 unsigned char LodePNG_chunk_private(const unsigned char* chunk); /*0: public, 1: private*/
765 unsigned char LodePNG_chunk_safetocopy(const unsigned char* chunk); /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy*/
767 unsigned char* LodePNG_chunk_data(unsigned char* chunk); /*get pointer to the data of the chunk*/
768 const unsigned char* LodePNG_chunk_data_const(const unsigned char* chunk); /*get pointer to the data of the chunk*/
770 unsigned LodePNG_chunk_check_crc(const unsigned char* chunk); /*returns 0 if the crc is correct, 1 if it's incorrect*/
771 void LodePNG_chunk_generate_crc(unsigned char* chunk); /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/
773 /*iterate to next chunks.*/
774 unsigned char* LodePNG_chunk_next(unsigned char* chunk);
775 const unsigned char* LodePNG_chunk_next_const(const unsigned char* chunk);
778 LodePNG_append_chunk
779 Appends chunk to the data in out. The given chunk should already have its chunk header.
780 The out variable and outlength are updated to reflect the new reallocated buffer.
781 Returns error code (0 if it went ok)
783 unsigned LodePNG_append_chunk(unsigned char** out, size_t* outlength, const unsigned char* chunk);
786 LodePNG_create_chunk
787 Appends new chunk to out. The chunk to append is given by giving its length, type
788 and data separately. The type is a 4-letter string.
789 The out variable and outlength are updated to reflect the new reallocated buffer.
790 Returne error code (0 if it went ok)
792 unsigned LodePNG_create_chunk(unsigned char** out, size_t* outlength, unsigned length, const char* type, const unsigned char* data);
794 #endif /*LODEPNG_COMPILE_PNG*/
796 #ifdef LODEPNG_COMPILE_ZLIB
797 /* ////////////////////////////////////////////////////////////////////////// */
798 /* Zlib encoder and decoder */
799 /* ////////////////////////////////////////////////////////////////////////// */
802 This is "LodeZlib". A C++ wrapper is available further on.
804 LodeZlib can be used to zlib compress and decompress a buffer. It cannot be
805 used to create gzip files however, and it only supports the part of zlib
806 that is required for PNG, it does not support dictionaries.
809 #ifdef LODEPNG_COMPILE_DECODER
810 /*This function reallocates the out buffer and appends the data.
811 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid buffer and *outsize its size in bytes.*/
812 unsigned LodeZlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DecompressSettings* settings);
813 #endif /*LODEPNG_COMPILE_DECODER*/
815 #ifdef LODEPNG_COMPILE_ENCODER
816 /*This function reallocates the out buffer and appends the data.
817 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid buffer and *outsize its size in bytes.*/
818 unsigned LodeZlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_CompressSettings* settings);
819 #endif /*LODEPNG_COMPILE_ENCODER*/
820 #endif /*LODEPNG_COMPILE_ZLIB*/
822 #ifdef LODEPNG_COMPILE_DISK
824 LodePNG_loadFile
825 Load a file from disk into buffer. The function allocates the out buffer, and
826 after usage you are responsible for freeing it.
827 out: output parameter, contains pointer to loaded buffer.
828 outsize: output parameter, size of the allocated out buffer
829 filename: the path to the file to load
830 return value: error code (0 means ok)
832 unsigned LodePNG_loadFile(unsigned char** out, size_t* outsize, const char* filename);
835 LodePNG_saveFile
836 Save a file from buffer to disk. Warning, this function overwrites the file without warning!
837 buffer: the buffer to write
838 buffersize: size of the buffer to write
839 filename: the path to the file to save to
840 return value: error code (0 means ok)
842 unsigned LodePNG_saveFile(const unsigned char* buffer, size_t buffersize, const char* filename);
843 #endif /*LODEPNG_COMPILE_DISK*/
845 #ifdef __cplusplus
847 /* ////////////////////////////////////////////////////////////////////////// */
848 /* LodePNG C++ wrapper */
849 /* ////////////////////////////////////////////////////////////////////////// */
851 //The LodePNG C++ wrapper uses classes with handy constructors and destructors
852 //instead of manual init and cleanup functions, and uses std::vectors instead of
853 //manually allocated memory buffers.
855 #ifdef LODEPNG_COMPILE_ZLIB
856 //The C++ wrapper for LodeZlib
857 namespace LodeZlib
859 #ifdef LODEPNG_COMPILE_DECODER
861 //Zlib-decompress an unsigned char buffer
862 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, const LodeZlib_DecompressSettings& settings = LodeZlib_defaultDecompressSettings);
864 //Zlib-decompress an std::vector
865 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, const LodeZlib_DecompressSettings& settings = LodeZlib_defaultDecompressSettings);
867 #endif //LODEPNG_COMPILE_DECODER
868 #ifdef LODEPNG_COMPILE_ENCODER
870 //Zlib-compress an unsigned char buffer
871 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, const LodeZlib_CompressSettings& settings = LodeZlib_defaultCompressSettings);
873 //Zlib-compress an std::vector
874 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, const LodeZlib_CompressSettings& settings = LodeZlib_defaultCompressSettings);
876 #endif //LODEPNG_COMPILE_ENCODER
877 } //namespace LodeZlib
878 #endif //LODEPNG_COMPILE_ZLIB
880 #ifdef LODEPNG_COMPILE_PNG
881 namespace LodePNG
884 #ifdef LODEPNG_COMPILE_DECODER
886 LodePNG::Decoder
887 Class to decode a PNG image. Before decoding, settings can be set and
888 after decoding, extra information about the PNG can be retrieved.
889 Extends from the C-struct LodePNG_Decoder to add constructors and destructors
890 to initialize/cleanup it automatically. Beware, no virtual destructor is used.
892 class Decoder : public LodePNG_Decoder
894 public:
896 Decoder();
897 ~Decoder();
898 void operator=(const LodePNG_Decoder& other);
900 //decode PNG buffer to raw out buffer. Width and height can be retrieved with getWidth() and getHeight() and error should be checked with hasError() and getError()
901 void decode(std::vector<unsigned char>& out, const unsigned char* in, size_t insize);
903 //decode PNG buffer to raw out buffer. Width and height can be retrieved with getWidth() and getHeight() and error should be checked with hasError() and getError()
904 void decode(std::vector<unsigned char>& out, const std::vector<unsigned char>& in);
906 //inspect functions: get only the info from the PNG header. The info can then be retrieved with the functions of this class.
907 void inspect(const unsigned char* in, size_t insize);
909 //inspect functions: get only the info from the PNG header. The info can then be retrieved with the functions of this class.
910 void inspect(const std::vector<unsigned char>& in);
912 //error checking after decoding
913 bool hasError() const;
914 unsigned getError() const;
916 //convenient access to some InfoPng parameters after decoding
917 unsigned getWidth() const; //width of image in pixels
918 unsigned getHeight() const; //height of image in pixels
919 unsigned getBpp(); //bits per pixel
920 unsigned getChannels(); //amount of channels
921 unsigned isGreyscaleType(); //is it a greyscale type? (colorType 0 or 4)
922 unsigned isAlphaType(); //has it an alpha channel? (colorType 2 or 6)
924 //getters and setters for the decoding settings
925 const LodePNG_DecodeSettings& getSettings() const;
926 LodePNG_DecodeSettings& getSettings();
927 void setSettings(const LodePNG_DecodeSettings& info);
929 //getters and setters for the PNG image info, after decoding this describes information of the PNG image
930 const LodePNG_InfoPng& getInfoPng() const;
931 LodePNG_InfoPng& getInfoPng();
932 void setInfoPng(const LodePNG_InfoPng& info);
933 void swapInfoPng(LodePNG_InfoPng& info); //faster than copying with setInfoPng
935 //getters and setters for the raw image info, this determines in what format you get the pixel buffer from the decoder
936 const LodePNG_InfoRaw& getInfoRaw() const;
937 LodePNG_InfoRaw& getInfoRaw();
938 void setInfoRaw(const LodePNG_InfoRaw& info);
941 #endif //LODEPNG_COMPILE_DECODER
943 #ifdef LODEPNG_COMPILE_ENCODER
945 LodePNG::Encoder
946 Class to encode a PNG image. Before encoding, settings can be set.
947 Extends from the C-struct LodePNG_Enoder to add constructors and destructors
948 to initialize/cleanup it automatically. Beware, no virtual destructor is used.
950 class Encoder : public LodePNG_Encoder
952 public:
954 Encoder();
955 ~Encoder();
956 void operator=(const LodePNG_Encoder& other);
958 //encoding image to PNG buffer
959 void encode(std::vector<unsigned char>& out, const unsigned char* image, unsigned w, unsigned h);
961 //encoding image to PNG buffer
962 void encode(std::vector<unsigned char>& out, const std::vector<unsigned char>& image, unsigned w, unsigned h);
964 //error checking after decoding
965 bool hasError() const;
966 unsigned getError() const;
968 //convenient direct access to some parameters of the InfoPng
969 void clearPalette();
970 void addPalette(unsigned char r, unsigned char g, unsigned char b, unsigned char a); //add 1 color to the palette
971 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
972 void clearText();
973 void addText(const std::string& key, const std::string& str); //push back both texts at once
974 void clearIText();
975 void addIText(const std::string& key, const std::string& langtag, const std::string& transkey, const std::string& str);
976 #endif //LODEPNG_COMPILE_ANCILLARY_CHUNKS
978 //getters and setters for the encoding settings
979 const LodePNG_EncodeSettings& getSettings() const;
980 LodePNG_EncodeSettings& getSettings();
981 void setSettings(const LodePNG_EncodeSettings& info);
983 //getters and setters for the PNG image info, this describes what color type and other settings the resulting PNG should have
984 const LodePNG_InfoPng& getInfoPng() const;
985 LodePNG_InfoPng& getInfoPng();
986 void setInfoPng(const LodePNG_InfoPng& info);
987 void swapInfoPng(LodePNG_InfoPng& info); //faster than copying with setInfoPng
989 //getters and setters for the raw image info, this describes how the encoder should interpret the input pixel buffer
990 const LodePNG_InfoRaw& getInfoRaw() const;
991 LodePNG_InfoRaw& getInfoRaw();
992 void setInfoRaw(const LodePNG_InfoRaw& info);
995 #endif //LODEPNG_COMPILE_ENCODER
997 #ifdef LODEPNG_COMPILE_DISK
999 loadFile
1000 Load a file from disk into an std::vector. If the vector is empty, then either
1001 the file doesn't exist or is an empty file.
1003 void loadFile(std::vector<unsigned char>& buffer, const std::string& filename);
1006 saveFile
1007 Save the binary data in an std::vector to a file on disk. The file is overwritten
1008 without warning.
1010 void saveFile(const std::vector<unsigned char>& buffer, const std::string& filename);
1011 #endif //LODEPNG_COMPILE_DISK
1013 } //namespace LodePNG
1015 #endif //LODEPNG_COMPILE_PNG
1017 #endif /*end of __cplusplus wrapper*/
1020 TODO:
1021 [ ] test if there are no memory leaks or security exploits - done a lot but needs to be checked often
1022 [ ] LZ77 encoder more like the one described in zlib - to make sure it's patentfree
1023 [ ] converting color to 16-bit per channel types
1024 [ ] read all public PNG chunk types (but never let the color profile and gamma ones ever touch RGB values, that is very annoying for textures as well as images in a browser)
1025 [ ] make sure encoder generates no chunks with size > (2^31)-1
1026 [ ] partial decoding (stream processing)
1027 [ ] let the "isFullyOpaque" function check color keys and transparent palettes too
1028 [ ] better name for the variables "codes", "codesD", "codelengthcodes", "clcl" and "lldl"
1029 [ ] check compatibility with vareous compilers - done but needs to be redone for every newer version
1030 [ ] don't stop decoding on errors like 69, 57, 58 (make warnings that the decoder stores in the error at the very end? and make some errors just let it stop with this one chunk but still do the next ones)
1031 [ ] make option to choose if the raw image with non multiple of 8 bits per scanline should have padding bits or not, if people like storing raw images that way
1034 #endif
1037 LodePNG Documentation
1038 ---------------------
1040 This documentations contains background information and examples. For the function
1041 and class documentation, see the comments in the declarations above.
1043 0. table of contents
1044 --------------------
1046 1. about
1047 1.1. supported features
1048 1.2. features not supported
1049 2. C and C++ version
1050 3. security
1051 4. decoding
1052 5. encoding
1053 6. color conversions
1054 6.1. PNG color types
1055 6.2. Default Behaviour of LodePNG
1056 6.3. Color Conversions
1057 6.4. More Notes
1058 7. error values
1059 8. chunks and PNG editing
1060 9. compiler support
1061 10. examples
1062 10.1. decoder C++ example
1063 10.2. encoder C++ example
1064 10.3. decoder C example
1065 11. changes
1066 12. contact information
1069 1. about
1070 --------
1072 PNG is a file format to store raster images losslessly with good compression,
1073 supporting different color types. It can be implemented in a patent-free way.
1075 LodePNG is a PNG codec according to the Portable Network Graphics (PNG)
1076 Specification (Second Edition) - W3C Recommendation 10 November 2003.
1078 The specifications used are:
1080 *) Portable Network Graphics (PNG) Specification (Second Edition):
1081 http://www.w3.org/TR/2003/REC-PNG-20031110
1082 *) RFC 1950 ZLIB Compressed Data Format version 3.3:
1083 http://www.gzip.org/zlib/rfc-zlib.html
1084 *) RFC 1951 DEFLATE Compressed Data Format Specification ver 1.3:
1085 http://www.gzip.org/zlib/rfc-deflate.html
1087 The most recent version of LodePNG can currently be found at
1088 http://members.gamedev.net/lode/projects/LodePNG/
1090 LodePNG works both in C (ISO C90) and C++, with a C++ wrapper that adds
1091 extra functionality.
1093 LodePNG exists out of two files:
1094 -lodepng.h: the header file for both C and C++
1095 -lodepng.c(pp): give it the name lodepng.c or lodepng.cpp (or .cc) depending on your usage
1097 If you want to start using LodePNG right away without reading this doc, get the
1098 files lodepng_examples.c or lodepng_examples.cpp to see how to use it in code,
1099 or check the (smaller) examples in chapter 13 here.
1101 LodePNG is simple but only supports the basic requirements. To achieve
1102 simplicity, the following design choices were made: There are no dependencies
1103 on any external library. To decode PNGs, there's a Decoder struct or class that
1104 can convert any PNG file data into an RGBA image buffer with a single function
1105 call. To encode PNGs, there's an Encoder struct or class that can convert image
1106 data into PNG file data with a single function call. To read and write files,
1107 there are simple functions to convert the files to/from buffers in memory.
1109 This all makes LodePNG suitable for loading textures in games, demoscene
1110 productions, saving a screenshot, images in programs that require them for simple
1111 usage, ... It's less suitable for full fledged image editors, loading PNGs
1112 over network (it requires all the image data to be available before decoding can
1113 begin), life-critical systems, ...
1114 LodePNG has a standards conformant decoder and encoder, and supports the ability
1115 to make a somewhat conformant editor.
1117 1.1. supported features
1118 -----------------------
1120 The following features are supported by the decoder:
1122 *) decoding of PNGs with any color type, bit depth and interlace mode, to a 24- or 32-bit color raw image, or the same color type as the PNG
1123 *) encoding of PNGs, from any raw image to 24- or 32-bit color, or the same color type as the raw image
1124 *) Adam7 interlace and deinterlace for any color type
1125 *) loading the image from harddisk or decoding it from a buffer from other sources than harddisk
1126 *) support for alpha channels, including RGBA color model, translucent palettes and color keying
1127 *) zlib decompression (inflate)
1128 *) zlib compression (deflate)
1129 *) CRC32 and ADLER32 checksums
1130 *) handling of unknown chunks, allowing making a PNG editor that stores custom and unknown chunks.
1131 *) the following chunks are supported (generated/interpreted) by both encoder and decoder:
1132 IHDR: header information
1133 PLTE: color palette
1134 IDAT: pixel data
1135 IEND: the final chunk
1136 tRNS: transparency for palettized images
1137 tEXt: textual information
1138 zTXt: compressed textual information
1139 iTXt: international textual information
1140 bKGD: suggested background color
1141 pHYs: physical dimensions
1142 tIME: modification time
1144 1.2. features not supported
1145 ---------------------------
1147 The following features are _not_ supported:
1149 *) some features needed to make a conformant PNG-Editor might be still missing.
1150 *) partial loading/stream processing. All data must be available and is processed in one call.
1151 *) The following public chunks are not supported but treated as unknown chunks by LodePNG
1152 cHRM, gAMA, iCCP, sRGB, sBIT, hIST, sPLT
1155 2. C and C++ version
1156 --------------------
1158 The C version uses buffers allocated with alloc that you need to free()
1159 yourself. On top of that, you need to use init and cleanup functions for each
1160 struct whenever using a struct from the C version to avoid exploits and memory leaks.
1162 The C++ version has constructors and destructors that take care of these things,
1163 and uses std::vectors in the interface for storing data.
1165 Both the C and the C++ version are contained in this file! The C++ code depends on
1166 the C code, the C code works on its own.
1168 These files work without modification for both C and C++ compilers because all the
1169 additional C++ code is in "#ifdef __cplusplus" blocks that make C-compilers ignore
1170 it, and the C code is made to compile both with strict ISO C90 and C++.
1172 To use the C++ version, you need to rename the source file to lodepng.cpp (instead
1173 of lodepng.c), and compile it with a C++ compiler.
1175 To use the C version, you need to rename the source file to lodepng.c (instead
1176 of lodepng.cpp), and compile it with a C compiler.
1179 3. Security
1180 -----------
1182 As with most software, even if carefully designed, it's always possible that
1183 LodePNG may contain possible exploits.
1185 If you discover a possible exploit, please let me know, and it will be fixed.
1187 When using LodePNG, care has to be taken with the C version of LodePNG, as well as the C-style
1188 structs when working with C++. The following conventions are used for all C-style structs:
1190 -if a struct has a corresponding init function, always call the init function when making a new one, to avoid exploits
1191 -if a struct has a corresponding cleanup function, call it before the struct disappears to avoid memory leaks
1192 -if a struct has a corresponding copy function, use the copy function instead of "=". The destination must also be inited already!
1194 4. Decoding
1195 -----------
1197 Decoding converts a PNG compressed image to a raw pixel buffer.
1199 Most documentation on using the decoder is at its declarations in the header
1200 above. For C, simple decoding can be done with functions such as LodePNG_decode32,
1201 and more advanced decoding can be done with the struct LodePNG_Decoder and its
1202 functions. For C++, simple decoding can be done with the LodePNG::decode functions
1203 and advanced decoding with the LodePNG::Decoder class.
1205 The Decoder contains 3 components:
1206 *) LodePNG_InfoPng: it stores information about the PNG (the input) in an LodePNG_InfoPng struct, don't modify this one yourself
1207 *) Settings: you can specify a few other settings for the decoder to use
1208 *) LodePNG_InfoRaw: here you can say what type of raw image (the output) you want to get
1210 Some of the parameters described below may be inside the sub-struct "LodePNG_InfoColor color".
1211 In the C and C++ version, when using Info structs outside of the decoder or encoder, you need to use their
1212 init and cleanup functions, but normally you use the ones in the decoder that are already handled
1213 in the init and cleanup functions of the decoder itself.
1215 =LodePNG_InfoPng=
1217 This contains information such as the original color type of the PNG image, text
1218 comments, suggested background color, etc... More details about the LodePNG_InfoPng struct
1219 are at its declaration documentation.
1221 Because the dimensions of the image are important, there are shortcuts to get them in the
1222 C++ version: use decoder.getWidth() and decoder.getHeight().
1223 In the C version, use decoder.infoPng.width and decoder.infoPng.height.
1225 =LodePNG_InfoRaw=
1227 In the LodePNG_InfoRaw struct of the Decoder, you can specify which color type you want
1228 the resulting raw image to be. If this is different from the colorType of the
1229 PNG, then the decoder will automatically convert the result to your LodePNG_InfoRaw
1230 settings. Not all combinations of color conversions are supported though, see
1231 a different section for information about the color modes and supported conversions.
1233 Palette of LodePNG_InfoRaw isn't used by the Decoder, when converting from palette color
1234 to palette color, the values of the pixels are left untouched so that the colors
1235 will change if the palette is different. Color key of LodePNG_InfoRaw is not used by the
1236 Decoder. If setting color_convert is false then LodePNG_InfoRaw is completely ignored,
1237 but it will be modified to match the color type of the PNG so will be overwritten.
1239 By default, 32-bit color is used for the result.
1241 =Settings=
1243 The Settings can be used to ignore the errors created by invalid CRC and Adler32
1244 chunks, and to disable the decoding of tEXt chunks.
1246 There's also a setting color_convert, true by default. If false, no conversion
1247 is done, the resulting data will be as it was in the PNG (after decompression)
1248 and you'll have to puzzle the colors of the pixels together yourself using the
1249 color type information in the LodePNG_InfoPng.
1252 5. Encoding
1253 -----------
1255 Encoding converts a raw pixel buffer to a PNG compressed image.
1257 Most documentation on using the encoder is at its declarations in the header
1258 above. For C, simple encoding can be done with functions such as LodePNG_encode32,
1259 and more advanced decoding can be done with the struct LodePNG_Encoder and its
1260 functions. For C++, simple encoding can be done with the LodePNG::encode functions
1261 and advanced decoding with the LodePNG::Encoder class.
1263 Like the decoder, the encoder can also give errors. However it gives less errors
1264 since the encoder input is trusted, the decoder input (a PNG image that could
1265 be forged by anyone) is not trusted.
1267 Like the Decoder, the Encoder has 3 components:
1268 *) LodePNG_InfoRaw: here you say what color type of the raw image (the input) has
1269 *) Settings: you can specify a few settings for the encoder to use
1270 *) LodePNG_InfoPng: the same LodePNG_InfoPng struct as created by the Decoder. For the encoder,
1271 with this you specify how you want the PNG (the output) to be.
1273 Some of the parameters described below may be inside the sub-struct "LodePNG_InfoColor color".
1274 In the C and C++ version, when using Info structs outside of the decoder or encoder, you need to use their
1275 init and cleanup functions, but normally you use the ones in the encoder that are already handled
1276 in the init and cleanup functions of the decoder itself.
1278 =LodePNG_InfoPng=
1280 The Decoder class stores information about the PNG image in an LodePNG_InfoPng object. With
1281 the Encoder you can do the opposite: you give it an LodePNG_InfoPng object, and it'll try
1282 to match the LodePNG_InfoPng you give as close as possible in the PNG it encodes. For
1283 example in the LodePNG_InfoPng you can specify the color type you want to use, possible
1284 tEXt chunks you want the PNG to contain, etc... For an explanation of all the
1285 values in LodePNG_InfoPng see a further section. Not all PNG color types are supported
1286 by the Encoder.
1288 The encoder will not always exactly match the LodePNG_InfoPng struct you give,
1289 it tries as close as possible. Some things are ignored by the encoder. The width
1290 and height of LodePNG_InfoPng are ignored as well, because instead the width and
1291 height of the raw image you give in the input are used. In fact the encoder
1292 currently uses only the following settings from it:
1293 -colorType and bitDepth: the ones it supports
1294 -text chunks, that you can add to the LodePNG_InfoPng with "addText"
1295 -the color key, if applicable for the given color type
1296 -the palette, if you encode to a PNG with colorType 3
1297 -the background color: it'll add a bKGD chunk to the PNG if one is given
1298 -the interlaceMethod: None (0) or Adam7 (1)
1300 When encoding to a PNG with colorType 3, the encoder will generate a PLTE chunk.
1301 If the palette contains any colors for which the alpha channel is not 255 (so
1302 there are translucent colors in the palette), it'll add a tRNS chunk.
1304 =LodePNG_InfoRaw=
1306 You specify the color type of the raw image that you give to the input here,
1307 including a possible transparent color key and palette you happen to be using in
1308 your raw image data.
1310 By default, 32-bit color is assumed, meaning your input has to be in RGBA
1311 format with 4 bytes (unsigned chars) per pixel.
1313 =Settings=
1315 The following settings are supported (some are in sub-structs):
1316 *) autoLeaveOutAlphaChannel: when this option is enabled, when you specify a PNG
1317 color type with alpha channel (not to be confused with the color type of the raw
1318 image you specify!!), but the encoder detects that all pixels of the given image
1319 are opaque, then it'll automatically use the corresponding type without alpha
1320 channel, resulting in a smaller PNG image.
1321 *) btype: the block type for LZ77. 0 = uncompressed, 1 = fixed huffman tree, 2 = dynamic huffman tree (best compression)
1322 *) useLZ77: whether or not to use LZ77 for compressed block types
1323 *) windowSize: the window size used by the LZ77 encoder (1 - 32768)
1324 *) force_palette: if colorType is 2 or 6, you can make the encoder write a PLTE
1325 chunk if force_palette is true. This can used as suggested palette to convert
1326 to by viewers that don't support more than 256 colors (if those still exist)
1327 *) add_id: add text chunk "Encoder: LodePNG <version>" to the image.
1328 *) text_compression: default 0. If 1, it'll store texts as zTXt instead of tEXt chunks.
1329 zTXt chunks use zlib compression on the text. This gives a smaller result on
1330 large texts but a larger result on small texts (such as a single program name).
1331 It's all tEXt or all zTXt though, there's no separate setting per text yet.
1334 6. color conversions
1335 --------------------
1337 In LodePNG, the color mode (bits, channels and palette) used in the PNG image,
1338 and the color mode used in the raw data, are separate and independently
1339 configurable. Therefore, LodePNG needs to do conversions from one color mode to
1340 another. Not all possible conversions are supported (e.g. converting to a color
1341 model with palette isn't supported). This section will explain which conversions
1342 are supported and how to configure this. This explains for example when LodePNG
1343 uses the settings in LodePNG_InfoPng, LodePNG_InfoRaw and Settings.
1345 6.1. PNG color types
1346 --------------------
1348 A PNG image can have many color types, ranging from 1-bit color to 64-bit color,
1349 as well as palettized color modes. After the zlib decompression and unfiltering
1350 in the PNG image is done, the raw pixel data will have that color type and thus
1351 a certain amount of bits per pixel. If you want the output raw image after
1352 decoding to have another color type, a conversion is done by LodePNG.
1354 The PNG specification mentions the following color types:
1356 0: greyscale, bit depths 1, 2, 4, 8, 16
1357 2: RGB, bit depths 8 and 16
1358 3: palette, bit depths 1, 2, 4 and 8
1359 4: greyscale with alpha, bit depths 8 and 16
1360 6: RGBA, bit depths 8 and 16
1362 Bit depth is the amount of bits per color channel.
1364 6.2. Default Behaviour of LodePNG
1365 ---------------------------------
1367 By default, the Decoder will convert the data from the PNG to 32-bit RGBA color,
1368 no matter what color type the PNG has, so that the result can be used directly
1369 as a texture in OpenGL etc... without worries about what color type the original
1370 image has.
1372 The Encoder assumes by default that the raw input you give it is a 32-bit RGBA
1373 buffer and will store the PNG as either 32 bit or 24 bit depending on whether
1374 or not any translucent pixels were detected in it.
1376 To get the default behaviour, don't change the values of LodePNG_InfoRaw and LodePNG_InfoPng of
1377 the encoder, and don't change the values of LodePNG_InfoRaw of the decoder.
1379 6.3. Color Conversions
1380 ----------------------
1382 As explained in the sections about the Encoder and Decoder, you can specify
1383 color types and bit depths in LodePNG_InfoPng and LodePNG_InfoRaw, to change the default behaviour
1384 explained above. (for the Decoder you can only specify the LodePNG_InfoRaw, because the
1385 LodePNG_InfoPng contains what the PNG file has).
1387 To avoid some confusion:
1388 -the Decoder converts from PNG to raw image
1389 -the Encoder converts from raw image to PNG
1390 -the color type and bit depth in LodePNG_InfoRaw, are those of the raw image
1391 -the color type and bit depth in LodePNG_InfoPng, are those of the PNG
1392 -if the color type of the LodePNG_InfoRaw and PNG image aren't the same, a conversion
1393 between the color types is done if the color types are supported. If it is not
1394 supported, an error is returned.
1396 Supported color conversions:
1397 -It's possible to load PNGs from any colortype and to save PNGs of any colorType.
1398 -Both encoder and decoder use the same converter. So both encoder and decoder
1399 suport the same color types at the input and the output. So the decoder supports
1400 any type of PNG image and can convert it to certain types of raw image, while the
1401 encoder supports any type of raw data but only certain color types for the output PNG.
1402 -The converter can convert from _any_ input color type, to 24-bit RGB or 32-bit RGBA
1403 -The converter can convert from greyscale input color type, to 8-bit greyscale or greyscale with alpha
1404 -If both color types are the same, conversion from anything to anything is possible
1405 -Color types that are invalid according to the PNG specification are not allowed
1406 -When converting from a type with alpha channel to one without, the alpha channel information is discarded
1407 -When converting from a type without alpha channel to one with, the result will be opaque except pixels that have the same color as the color key of the input if one was given
1408 -When converting from 16-bit bitDepth to 8-bit bitDepth, the 16-bit precision information is lost, only the most significant byte is kept
1409 -Converting from color to greyscale or to palette is not supported on purpose: there are multiple possible algorithms to do this color reduction, LodePNG does not want to pick one and leaves this choice to the user instead, because it's beyond the scope of PNG encoding.
1410 -Converting from/to a palette type, only keeps the indices, it ignores the colors defined in the palette
1412 No conversion needed...:
1413 -If the color type of the PNG image and raw image are the same, then no
1414 conversion is done, and all color types are supported.
1415 -In the encoder, you can make it save a PNG with any color by giving the
1416 LodePNG_InfoRaw and LodePNG_InfoPng the same color type.
1417 -In the decoder, you can make it store the pixel data in the same color type
1418 as the PNG has, by setting the color_convert setting to false. Settings in
1419 infoRaw are then ignored.
1421 The function LodePNG_convert does this, which is available in the interface but
1422 normally isn't needed since the encoder and decoder already call it.
1424 6.4. More Notes
1425 ---------------
1427 In the PNG file format, if a less than 8-bit per pixel color type is used and the scanlines
1428 have a bit amount that isn't a multiple of 8, then padding bits are used so that each
1429 scanline starts at a fresh byte.
1430 However: The raw input image you give to the encoder, and the raw output image you get from the decoder
1431 will NOT have these padding bits in that case, e.g. in the case of a 1-bit image with a width
1432 of 7 pixels, the first pixel of the second scanline will the the 8th bit of the first byte,
1433 not the first bit of a new byte.
1435 7. error values
1436 ---------------
1438 All functions in LodePNG that return an error code, return 0 if everything went
1439 OK, or one of the code defined by LodePNG if there was an error.
1441 The meaning of the LodePNG error values can be retrieved with the function
1442 LodePNG_error_text: given the numerical error code, it returns a description
1443 of the error in English as a string.
1445 Check the implementation of LodePNG_error_text to see the meaning of each error code.
1448 8. chunks and PNG editing
1449 -------------------------
1451 If you want to add extra chunks to a PNG you encode, or use LodePNG for a PNG
1452 editor that should follow the rules about handling of unknown chunks, or if you
1453 program is able to read other types of chunks than the ones handled by LodePNG,
1454 then that's possible with the chunk functions of LodePNG.
1456 A PNG chunk has the following layout:
1458 4 bytes length
1459 4 bytes type name
1460 length bytes data
1461 4 bytes CRC
1464 8.1. iterating through chunks
1465 -----------------------------
1467 If you have a buffer containing the PNG image data, then the first chunk (the
1468 IHDR chunk) starts at byte number 8 of that buffer. The first 8 bytes are the
1469 signature of the PNG and are not part of a chunk. But if you start at byte 8
1470 then you have a chunk, and can check the following things of it.
1472 NOTE: none of these functions check for memory buffer boundaries. To avoid
1473 exploits, always make sure the buffer contains all the data of the chunks.
1474 When using LodePNG_chunk_next, make sure the returned value is within the
1475 allocated memory.
1477 unsigned LodePNG_chunk_length(const unsigned char* chunk):
1479 Get the length of the chunk's data. The total chunk length is this length + 12.
1481 void LodePNG_chunk_type(char type[5], const unsigned char* chunk):
1482 unsigned char LodePNG_chunk_type_equals(const unsigned char* chunk, const char* type):
1484 Get the type of the chunk or compare if it's a certain type
1486 unsigned char LodePNG_chunk_critical(const unsigned char* chunk):
1487 unsigned char LodePNG_chunk_private(const unsigned char* chunk):
1488 unsigned char LodePNG_chunk_safetocopy(const unsigned char* chunk):
1490 Check if the chunk is critical in the PNG standard (only IHDR, PLTE, IDAT and IEND are).
1491 Check if the chunk is private (public chunks are part of the standard, private ones not).
1492 Check if the chunk is safe to copy. If it's not, then, when modifying data in a critical
1493 chunk, unsafe to copy chunks of the old image may NOT be saved in the new one if your
1494 program doesn't handle that type of unknown chunk.
1496 unsigned char* LodePNG_chunk_data(unsigned char* chunk):
1497 const unsigned char* LodePNG_chunk_data_const(const unsigned char* chunk):
1499 Get a pointer to the start of the data of the chunk.
1501 unsigned LodePNG_chunk_check_crc(const unsigned char* chunk):
1502 void LodePNG_chunk_generate_crc(unsigned char* chunk):
1504 Check if the crc is correct or generate a correct one.
1506 unsigned char* LodePNG_chunk_next(unsigned char* chunk):
1507 const unsigned char* LodePNG_chunk_next_const(const unsigned char* chunk):
1509 Iterate to the next chunk. This works if you have a buffer with consecutive chunks. Note that these
1510 functions do no boundary checking of the allocated data whatsoever, so make sure there is enough
1511 data available in the buffer to be able to go to the next chunk.
1513 unsigned LodePNG_append_chunk(unsigned char** out, size_t* outlength, const unsigned char* chunk):
1514 unsigned LodePNG_create_chunk(unsigned char** out, size_t* outlength, unsigned length, const char* type, const unsigned char* data):
1516 These functions are used to create new chunks that are appended to the data in *out that has
1517 length *outlength. The append function appends an existing chunk to the new data. The create
1518 function creates a new chunk with the given parameters and appends it. Type is the 4-letter
1519 name of the chunk.
1522 8.2. chunks in infoPng
1523 ----------------------
1525 The LodePNG_InfoPng struct contains a struct LodePNG_UnknownChunks in it. This
1526 struct has 3 buffers (each with size) to contain 3 types of unknown chunks:
1527 the ones that come before the PLTE chunk, the ones that come between the PLTE
1528 and the IDAT chunks, and the ones that come after the IDAT chunks.
1529 It's necessary to make the distionction between these 3 cases because the PNG
1530 standard forces to keep the ordering of unknown chunks compared to the critical
1531 chunks, but does not force any other ordering rules.
1533 infoPng.unknown_chunks.data[0] is the chunks before PLTE
1534 infoPng.unknown_chunks.data[1] is the chunks after PLTE, before IDAT
1535 infoPng.unknown_chunks.data[2] is the chunks after IDAT
1537 The chunks in these 3 buffers can be iterated through and read by using the same
1538 way described in the previous subchapter.
1540 When using the decoder to decode a PNG, you can make it store all unknown chunks
1541 if you set the option settings.rememberUnknownChunks to 1. By default, this option
1542 is off and is 0.
1544 The encoder will always encode unknown chunks that are stored in the infoPng. If
1545 you need it to add a particular chunk that isn't known by LodePNG, you can use
1546 LodePNG_append_chunk or LodePNG_create_chunk to the chunk data in
1547 infoPng.unknown_chunks.data[x].
1549 Chunks that are known by LodePNG should not be added in that way. E.g. to make
1550 LodePNG add a bKGD chunk, set background_defined to true and add the correct
1551 parameters there and LodePNG will generate the chunk.
1554 9. compiler support
1555 -------------------
1557 No libraries other than the current standard C library are needed to compile
1558 LodePNG. For the C++ version, only the standard C++ library is needed on top.
1559 Add the files lodepng.c(pp) and lodepng.h to your project, include
1560 lodepng.h where needed, and your program can read/write PNG files.
1562 Use optimization! For both the encoder and decoder, compiling with the best
1563 optimizations makes a large difference.
1565 Make sure that LodePNG is compiled with the same compiler of the same version
1566 and with the same settings as the rest of the program, or the interfaces with
1567 std::vectors and std::strings in C++ can be incompatible resulting in bad things.
1569 CHAR_BITS must be 8 or higher, because LodePNG uses unsigned chars for octets.
1571 *) gcc and g++
1573 LodePNG is developed in gcc so this compiler is natively supported. It gives no
1574 warnings with compiler options "-Wall -Wextra -pedantic -ansi", with gcc and g++
1575 version 4.5.1 on Linux.
1577 *) Mingw and Bloodshed DevC++
1579 The Mingw compiler (a port of gcc) used by Bloodshed DevC++ for Windows is fully
1580 supported by LodePNG.
1582 *) Visual Studio 2005 and Visual C++ 2005 Express Edition
1584 Versions 20070604 up to 20080107 have been tested on VS2005 and work. Visual
1585 studio may give warnings about 'fopen' being deprecated. A multiplatform library
1586 can't support the proposed Visual Studio alternative however.
1588 If you're using LodePNG in VS2005 and don't want to see the deprecated warnings,
1589 put this on top of lodepng.h before the inclusions: #define _CRT_SECURE_NO_DEPRECATE
1591 *) Visual Studio 6.0
1593 The C++ version of LodePNG was not supported by Visual Studio 6.0 because Visual
1594 Studio 6.0 doesn't follow the C++ standard and implements it incorrectly.
1595 The current C version of LodePNG has not been tested in VS6 but may work now.
1597 *) Comeau C/C++
1599 Vesion 20070107 compiles without problems on the Comeau C/C++ Online Test Drive
1600 at http://www.comeaucomputing.com/tryitout in both C90 and C++ mode.
1602 *) Compilers on Macintosh
1604 LodePNG has been reported to work both with the gcc and LLVM for Macintosh, both
1605 for C and C++.
1607 *) Other Compilers
1609 If you encounter problems on other compilers, I'm happy to help out make LodePNG
1610 support the compiler if it supports the ISO C90 and C++ standard well enough and
1611 the required modification doesn't require using non standard or less good C/C++
1612 code or headers.
1615 10. examples
1616 ------------
1618 This decoder and encoder example show the most basic usage of LodePNG (using the
1619 classes, not the simple functions, which would be trivial)
1621 More complex examples can be found in:
1622 -lodepng_examples.c: 9 different examples in C, such as showing the image with SDL, ...
1623 -lodepng_examples.cpp: the same examples in C++ using the C++ wrapper of LodePNG
1625 These files can be found on the LodePNG website or searched for on the internet.
1627 10.1. decoder C++ example
1628 -------------------------
1630 ////////////////////////////////////////////////////////////////////////////////
1631 #include "lodepng.h"
1632 #include <iostream>
1634 int main(int argc, char *argv[])
1636 const char* filename = argc > 1 ? argv[1] : "test.png";
1638 //load and decode
1639 std::vector<unsigned char> buffer, image; //buffer will contain the PNG file, image will contain the raw pixels
1640 LodePNG::loadFile(buffer, filename); //load the image file with given filename
1641 LodePNG::Decoder decoder;
1642 decoder.decode(image, buffer.size() ? &buffer[0] : 0, (unsigned)buffer.size()); //decode the png
1644 //if there's an error, display it
1645 if(decoder.hasError()) std::cout << "error " << decoder.getError() << ": " << LodePNG_error_text(decoder.getError()) << std::endl;
1647 int width = decoder.getWidth(); //get the width in pixels
1648 int height = decoder.getHeight(); //get the height in pixels
1649 //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
1652 //alternative version using the "simple" function
1653 int main(int argc, char *argv[])
1655 const char* filename = argc > 1 ? argv[1] : "test.png";
1657 //load and decode
1658 std::vector<unsigned char> image;
1659 unsigned width, height;
1660 unsigned error = LodePNG::decode(image, width, height, filename);
1662 //if there's an error, display it
1663 if(error != 0) std::cout << "error " << error << ": " << LodePNG_error_text(error) << std::endl;
1665 //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
1667 ////////////////////////////////////////////////////////////////////////////////
1670 10.2. encoder C++ example
1671 -------------------------
1673 ////////////////////////////////////////////////////////////////////////////////
1674 #include "lodepng.h"
1675 #include <iostream>
1677 //saves image to filename given as argument. Warning, this overwrites the file without warning!
1678 int main(int argc, char *argv[])
1680 //check if user gave a filename
1681 if(argc <= 1)
1683 std::cout << "please provide a filename to save to\n";
1684 return 0;
1687 //generate some image
1688 std::vector<unsigned char> image;
1689 image.resize(512 * 512 * 4);
1690 for(unsigned y = 0; y < 512; y++)
1691 for(unsigned x = 0; x < 512; x++)
1693 image[4 * 512 * y + 4 * x + 0] = 255 * !(x & y);
1694 image[4 * 512 * y + 4 * x + 1] = x ^ y;
1695 image[4 * 512 * y + 4 * x + 2] = x | y;
1696 image[4 * 512 * y + 4 * x + 3] = 255;
1699 //encode and save, using the Encoder class
1700 std::vector<unsigned char> buffer;
1701 LodePNG::Encoder encoder;
1702 encoder.encode(buffer, image, 512, 512);
1703 LodePNG::saveFile(buffer, argv[1]);
1705 //the same as the 4 lines of code above, but in 1 call without the class:
1706 //LodePNG::encode(argv[1], image, 512, 512);
1708 ////////////////////////////////////////////////////////////////////////////////
1711 10.3. Decoder C example
1712 -----------------------
1714 This example loads the PNG from a file into a pixel buffer in 1 function call
1716 #include "lodepng.h"
1718 int main(int argc, char *argv[])
1720 unsigned error;
1721 unsigned char* image;
1722 size_t width, height;
1724 if(argc <= 1) return 0;
1726 error = LodePNG_decode32_file(&image, &width, &height, filename);
1728 if(error != 0) printf("error %u: %s\n", error, LodePNG_error_text(error));
1730 //use image here
1732 free(image);
1736 11. changes
1737 -----------
1739 The version number of LodePNG is the date of the change given in the format
1740 yyyymmdd.
1742 Some changes aren't backwards compatible. Those are indicated with a (!)
1743 symbol.
1745 *) 21 feb 2011: fixed compiling for C90. Fixed compiling with sections disabled.
1746 *) 11 dec 2010: encoding is made faster, based on suggestion by Peter Eastman
1747 to optimize long sequences of zeros.
1748 *) 13 nov 2010: added LodePNG_InfoColor_hasPaletteAlpha and
1749 LodePNG_InfoColor_canHaveAlpha functions for convenience.
1750 *) 7 nov 2010: added LodePNG_error_text function to get error code description.
1751 *) 30 okt 2010: made decoding slightly faster
1752 *) 26 okt 2010: (!) changed some C function and struct names (more consistent).
1753 Reorganized the documentation and the declaration order in the header.
1754 *) 08 aug 2010: only changed some comments and external samples.
1755 *) 05 jul 2010: fixed bug thanks to warnings in the new gcc version.
1756 *) 14 mar 2010: fixed bug where too much memory was allocated for char buffers.
1757 *) 02 sep 2008: fixed bug where it could create empty tree that linux apps could
1758 read by ignoring the problem but windows apps couldn't.
1759 *) 06 jun 2008: added more error checks for out of memory cases.
1760 *) 26 apr 2008: added a few more checks here and there to ensure more safety.
1761 *) 06 mar 2008: crash with encoding of strings fixed
1762 *) 02 feb 2008: support for international text chunks added (iTXt)
1763 *) 23 jan 2008: small cleanups, and #defines to divide code in sections
1764 *) 20 jan 2008: support for unknown chunks allowing using LodePNG for an editor.
1765 *) 18 jan 2008: support for tIME and pHYs chunks added to encoder and decoder.
1766 *) 17 jan 2008: ability to encode and decode compressed zTXt chunks added
1767 Also vareous fixes, such as in the deflate and the padding bits code.
1768 *) 13 jan 2008: Added ability to encode Adam7-interlaced images. Improved
1769 filtering code of encoder.
1770 *) 07 jan 2008: (!) changed LodePNG to use ISO C90 instead of C++. A
1771 C++ wrapper around this provides an interface almost identical to before.
1772 Having LodePNG be pure ISO C90 makes it more portable. The C and C++ code
1773 are together in these files but it works both for C and C++ compilers.
1774 *) 29 dec 2007: (!) changed most integer types to unsigned int + other tweaks
1775 *) 30 aug 2007: bug fixed which makes this Borland C++ compatible
1776 *) 09 aug 2007: some VS2005 warnings removed again
1777 *) 21 jul 2007: deflate code placed in new namespace separate from zlib code
1778 *) 08 jun 2007: fixed bug with 2- and 4-bit color, and small interlaced images
1779 *) 04 jun 2007: improved support for Visual Studio 2005: crash with accessing
1780 invalid std::vector element [0] fixed, and level 3 and 4 warnings removed
1781 *) 02 jun 2007: made the encoder add a tag with version by default
1782 *) 27 may 2007: zlib and png code separated (but still in the same file),
1783 simple encoder/decoder functions added for more simple usage cases
1784 *) 19 may 2007: minor fixes, some code cleaning, new error added (error 69),
1785 moved some examples from here to lodepng_examples.cpp
1786 *) 12 may 2007: palette decoding bug fixed
1787 *) 24 apr 2007: changed the license from BSD to the zlib license
1788 *) 11 mar 2007: very simple addition: ability to encode bKGD chunks.
1789 *) 04 mar 2007: (!) tEXt chunk related fixes, and support for encoding
1790 palettized PNG images. Plus little interface change with palette and texts.
1791 *) 03 mar 2007: Made it encode dynamic Huffman shorter with repeat codes.
1792 Fixed a bug where the end code of a block had length 0 in the Huffman tree.
1793 *) 26 feb 2007: Huffman compression with dynamic trees (BTYPE 2) now implemented
1794 and supported by the encoder, resulting in smaller PNGs at the output.
1795 *) 27 jan 2007: Made the Adler-32 test faster so that a timewaste is gone.
1796 *) 24 jan 2007: gave encoder an error interface. Added color conversion from any
1797 greyscale type to 8-bit greyscale with or without alpha.
1798 *) 21 jan 2007: (!) Totally changed the interface. It allows more color types
1799 to convert to and is more uniform. See the manual for how it works now.
1800 *) 07 jan 2007: Some cleanup & fixes, and a few changes over the last days:
1801 encode/decode custom tEXt chunks, separate classes for zlib & deflate, and
1802 at last made the decoder give errors for incorrect Adler32 or Crc.
1803 *) 01 jan 2007: Fixed bug with encoding PNGs with less than 8 bits per channel.
1804 *) 29 dec 2006: Added support for encoding images without alpha channel, and
1805 cleaned out code as well as making certain parts faster.
1806 *) 28 dec 2006: Added "Settings" to the encoder.
1807 *) 26 dec 2006: The encoder now does LZ77 encoding and produces much smaller files now.
1808 Removed some code duplication in the decoder. Fixed little bug in an example.
1809 *) 09 dec 2006: (!) Placed output parameters of public functions as first parameter.
1810 Fixed a bug of the decoder with 16-bit per color.
1811 *) 15 okt 2006: Changed documentation structure
1812 *) 09 okt 2006: Encoder class added. It encodes a valid PNG image from the
1813 given image buffer, however for now it's not compressed.
1814 *) 08 sep 2006: (!) Changed to interface with a Decoder class
1815 *) 30 jul 2006: (!) LodePNG_InfoPng , width and height are now retrieved in different
1816 way. Renamed decodePNG to decodePNGGeneric.
1817 *) 29 jul 2006: (!) Changed the interface: image info is now returned as a
1818 struct of type LodePNG::LodePNG_Info, instead of a vector, which was a bit clumsy.
1819 *) 28 jul 2006: Cleaned the code and added new error checks.
1820 Corrected terminology "deflate" into "inflate".
1821 *) 23 jun 2006: Added SDL example in the documentation in the header, this
1822 example allows easy debugging by displaying the PNG and its transparency.
1823 *) 22 jun 2006: (!) Changed way to obtain error value. Added
1824 loadFile function for convenience. Made decodePNG32 faster.
1825 *) 21 jun 2006: (!) Changed type of info vector to unsigned.
1826 Changed position of palette in info vector. Fixed an important bug that
1827 happened on PNGs with an uncompressed block.
1828 *) 16 jun 2006: Internally changed unsigned into unsigned where
1829 needed, and performed some optimizations.
1830 *) 07 jun 2006: (!) Renamed functions to decodePNG and placed them
1831 in LodePNG namespace. Changed the order of the parameters. Rewrote the
1832 documentation in the header. Renamed files to lodepng.cpp and lodepng.h
1833 *) 22 apr 2006: Optimized and improved some code
1834 *) 07 sep 2005: (!) Changed to std::vector interface
1835 *) 12 aug 2005: Initial release
1838 12. contact information
1839 -----------------------
1841 Feel free to contact me with suggestions, problems, comments, ... concerning
1842 LodePNG. If you encounter a PNG image that doesn't work properly with this
1843 decoder, feel free to send it and I'll use it to find and fix the problem.
1845 My email address is (puzzle the account and domain together with an @ symbol):
1846 Domain: gmail dot com.
1847 Account: lode dot vandevenne.
1850 Copyright (c) 2005-2011 Lode Vandevenne