EmbeddedArtists AB
/
app_lcdboard_demo_cube
Example for the LPC4088 QSB Base Board
Embed:
(wiki syntax)
Show/hide line numbers
lodepng.h
00001 /* 00002 LodePNG version 20131222 00003 00004 Copyright (c) 2005-2013 Lode Vandevenne 00005 00006 This software is provided 'as-is', without any express or implied 00007 warranty. In no event will the authors be held liable for any damages 00008 arising from the use of this software. 00009 00010 Permission is granted to anyone to use this software for any purpose, 00011 including commercial applications, and to alter it and redistribute it 00012 freely, subject to the following restrictions: 00013 00014 1. The origin of this software must not be misrepresented; you must not 00015 claim that you wrote the original software. If you use this software 00016 in a product, an acknowledgment in the product documentation would be 00017 appreciated but is not required. 00018 00019 2. Altered source versions must be plainly marked as such, and must not be 00020 misrepresented as being the original software. 00021 00022 3. This notice may not be removed or altered from any source 00023 distribution. 00024 */ 00025 00026 #ifndef LODEPNG_H 00027 #define LODEPNG_H 00028 00029 #include <string.h> /*for size_t*/ 00030 00031 #ifdef __cplusplus 00032 #include <vector> 00033 #include <string> 00034 #endif /*__cplusplus*/ 00035 00036 00037 // Settings modified by Embedded Artists 00038 #define LODEPNG_NO_COMPILE_ENCODER 00039 #define LODEPNG_NO_COMPILE_DISK 00040 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS 00041 #define LODEPNG_NO_COMPILE_ERROR_TEXT 00042 #define LODEPNG_NO_COMPILE_CPP 00043 00044 00045 /* 00046 The following #defines are used to create code sections. They can be disabled 00047 to disable code sections, which can give faster compile time and smaller binary. 00048 The "NO_COMPILE" defines are designed to be used to pass as defines to the 00049 compiler command to disable them without modifying this header, e.g. 00050 -DLODEPNG_NO_COMPILE_ZLIB for gcc. 00051 */ 00052 /*deflate & zlib. If disabled, you must specify alternative zlib functions in 00053 the custom_zlib field of the compress and decompress settings*/ 00054 #ifndef LODEPNG_NO_COMPILE_ZLIB 00055 #define LODEPNG_COMPILE_ZLIB 00056 #endif 00057 /*png encoder and png decoder*/ 00058 #ifndef LODEPNG_NO_COMPILE_PNG 00059 #define LODEPNG_COMPILE_PNG 00060 #endif 00061 /*deflate&zlib decoder and png decoder*/ 00062 #ifndef LODEPNG_NO_COMPILE_DECODER 00063 #define LODEPNG_COMPILE_DECODER 00064 #endif 00065 /*deflate&zlib encoder and png encoder*/ 00066 #ifndef LODEPNG_NO_COMPILE_ENCODER 00067 #define LODEPNG_COMPILE_ENCODER 00068 #endif 00069 /*the optional built in harddisk file loading and saving functions*/ 00070 #ifndef LODEPNG_NO_COMPILE_DISK 00071 #define LODEPNG_COMPILE_DISK 00072 #endif 00073 /*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/ 00074 #ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS 00075 #define LODEPNG_COMPILE_ANCILLARY_CHUNKS 00076 #endif 00077 /*ability to convert error numerical codes to English text string*/ 00078 #ifndef LODEPNG_NO_COMPILE_ERROR_TEXT 00079 #define LODEPNG_COMPILE_ERROR_TEXT 00080 #endif 00081 /*Compile the default allocators (C's free, malloc and realloc). If you disable this, 00082 you can define the functions lodepng_free, lodepng_malloc and lodepng_realloc in your 00083 source files with custom allocators.*/ 00084 #ifndef LODEPNG_NO_COMPILE_ALLOCATORS 00085 #define LODEPNG_COMPILE_ALLOCATORS 00086 #endif 00087 /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ 00088 #ifdef __cplusplus 00089 #ifndef LODEPNG_NO_COMPILE_CPP 00090 #define LODEPNG_COMPILE_CPP 00091 #endif 00092 #endif 00093 00094 #ifdef LODEPNG_COMPILE_PNG 00095 /*The PNG color types (also used for raw).*/ 00096 typedef enum LodePNGColorType 00097 { 00098 LCT_GREY = 0, /*greyscale: 1,2,4,8,16 bit*/ 00099 LCT_RGB = 2, /*RGB: 8,16 bit*/ 00100 LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/ 00101 LCT_GREY_ALPHA = 4, /*greyscale with alpha: 8,16 bit*/ 00102 LCT_RGBA = 6 /*RGB with alpha: 8,16 bit*/ 00103 } LodePNGColorType; 00104 00105 #ifdef LODEPNG_COMPILE_DECODER 00106 /* 00107 Converts PNG data in memory to raw pixel data. 00108 out: Output parameter. Pointer to buffer that will contain the raw pixel data. 00109 After decoding, its size is w * h * (bytes per pixel) bytes larger than 00110 initially. Bytes per pixel depends on colortype and bitdepth. 00111 Must be freed after usage with free(*out). 00112 Note: for 16-bit per channel colors, uses big endian format like PNG does. 00113 w: Output parameter. Pointer to width of pixel data. 00114 h: Output parameter. Pointer to height of pixel data. 00115 in: Memory buffer with the PNG file. 00116 insize: size of the in buffer. 00117 colortype: the desired color type for the raw output image. See explanation on PNG color types. 00118 bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types. 00119 Return value: LodePNG error code (0 means no error). 00120 */ 00121 unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, 00122 const unsigned char* in, size_t insize, 00123 LodePNGColorType colortype, unsigned bitdepth); 00124 00125 /*Same as lodepng_decode_memory, but always decodes to 32-bit RGBA raw image*/ 00126 unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, 00127 const unsigned char* in, size_t insize); 00128 00129 /*Same as lodepng_decode_memory, but always decodes to 24-bit RGB raw image*/ 00130 unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, 00131 const unsigned char* in, size_t insize); 00132 00133 #ifdef LODEPNG_COMPILE_DISK 00134 /* 00135 Load PNG from disk, from file with given name. 00136 Same as the other decode functions, but instead takes a filename as input. 00137 */ 00138 unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, 00139 const char* filename, 00140 LodePNGColorType colortype, unsigned bitdepth); 00141 00142 /*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image.*/ 00143 unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, 00144 const char* filename); 00145 00146 /*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image.*/ 00147 unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, 00148 const char* filename); 00149 #endif /*LODEPNG_COMPILE_DISK*/ 00150 #endif /*LODEPNG_COMPILE_DECODER*/ 00151 00152 00153 #ifdef LODEPNG_COMPILE_ENCODER 00154 /* 00155 Converts raw pixel data into a PNG image in memory. The colortype and bitdepth 00156 of the output PNG image cannot be chosen, they are automatically determined 00157 by the colortype, bitdepth and content of the input pixel data. 00158 Note: for 16-bit per channel colors, needs big endian format like PNG does. 00159 out: Output parameter. Pointer to buffer that will contain the PNG image data. 00160 Must be freed after usage with free(*out). 00161 outsize: Output parameter. Pointer to the size in bytes of the out buffer. 00162 image: The raw pixel data to encode. The size of this buffer should be 00163 w * h * (bytes per pixel), bytes per pixel depends on colortype and bitdepth. 00164 w: width of the raw pixel data in pixels. 00165 h: height of the raw pixel data in pixels. 00166 colortype: the color type of the raw input image. See explanation on PNG color types. 00167 bitdepth: the bit depth of the raw input image. See explanation on PNG color types. 00168 Return value: LodePNG error code (0 means no error). 00169 */ 00170 unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, 00171 const unsigned char* image, unsigned w, unsigned h, 00172 LodePNGColorType colortype, unsigned bitdepth); 00173 00174 /*Same as lodepng_encode_memory, but always encodes from 32-bit RGBA raw image.*/ 00175 unsigned lodepng_encode32(unsigned char** out, size_t* outsize, 00176 const unsigned char* image, unsigned w, unsigned h); 00177 00178 /*Same as lodepng_encode_memory, but always encodes from 24-bit RGB raw image.*/ 00179 unsigned lodepng_encode24(unsigned char** out, size_t* outsize, 00180 const unsigned char* image, unsigned w, unsigned h); 00181 00182 #ifdef LODEPNG_COMPILE_DISK 00183 /* 00184 Converts raw pixel data into a PNG file on disk. 00185 Same as the other encode functions, but instead takes a filename as output. 00186 NOTE: This overwrites existing files without warning! 00187 */ 00188 unsigned lodepng_encode_file(const char* filename, 00189 const unsigned char* image, unsigned w, unsigned h, 00190 LodePNGColorType colortype, unsigned bitdepth); 00191 00192 /*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/ 00193 unsigned lodepng_encode32_file(const char* filename, 00194 const unsigned char* image, unsigned w, unsigned h); 00195 00196 /*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image.*/ 00197 unsigned lodepng_encode24_file(const char* filename, 00198 const unsigned char* image, unsigned w, unsigned h); 00199 #endif /*LODEPNG_COMPILE_DISK*/ 00200 #endif /*LODEPNG_COMPILE_ENCODER*/ 00201 00202 00203 #ifdef LODEPNG_COMPILE_CPP 00204 namespace lodepng 00205 { 00206 #ifdef LODEPNG_COMPILE_DECODER 00207 /*Same as lodepng_decode_memory, but decodes to an std::vector.*/ 00208 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 00209 const unsigned char* in, size_t insize, 00210 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00211 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 00212 const std::vector<unsigned char>& in, 00213 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00214 #ifdef LODEPNG_COMPILE_DISK 00215 /* 00216 Converts PNG file from disk to raw pixel data in memory. 00217 Same as the other decode functions, but instead takes a filename as input. 00218 */ 00219 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 00220 const std::string& filename, 00221 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00222 #endif //LODEPNG_COMPILE_DISK 00223 #endif //LODEPNG_COMPILE_DECODER 00224 00225 #ifdef LODEPNG_COMPILE_ENCODER 00226 /*Same as lodepng_encode_memory, but encodes to an std::vector.*/ 00227 unsigned encode(std::vector<unsigned char>& out, 00228 const unsigned char* in, unsigned w, unsigned h, 00229 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00230 unsigned encode(std::vector<unsigned char>& out, 00231 const std::vector<unsigned char>& in, unsigned w, unsigned h, 00232 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00233 #ifdef LODEPNG_COMPILE_DISK 00234 /* 00235 Converts 32-bit RGBA raw pixel data into a PNG file on disk. 00236 Same as the other encode functions, but instead takes a filename as output. 00237 NOTE: This overwrites existing files without warning! 00238 */ 00239 unsigned encode(const std::string& filename, 00240 const unsigned char* in, unsigned w, unsigned h, 00241 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00242 unsigned encode(const std::string& filename, 00243 const std::vector<unsigned char>& in, unsigned w, unsigned h, 00244 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); 00245 #endif //LODEPNG_COMPILE_DISK 00246 #endif //LODEPNG_COMPILE_ENCODER 00247 } //namespace lodepng 00248 #endif /*LODEPNG_COMPILE_CPP*/ 00249 #endif /*LODEPNG_COMPILE_PNG*/ 00250 00251 #ifdef LODEPNG_COMPILE_ERROR_TEXT 00252 /*Returns an English description of the numerical error code.*/ 00253 const char* lodepng_error_text(unsigned code); 00254 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/ 00255 00256 #ifdef LODEPNG_COMPILE_DECODER 00257 /*Settings for zlib decompression*/ 00258 typedef struct LodePNGDecompressSettings LodePNGDecompressSettings; 00259 struct LodePNGDecompressSettings 00260 { 00261 unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/ 00262 00263 /*use custom zlib decoder instead of built in one (default: null)*/ 00264 unsigned (*custom_zlib)(unsigned char**, size_t*, 00265 const unsigned char*, size_t, 00266 const LodePNGDecompressSettings*); 00267 /*use custom deflate decoder instead of built in one (default: null) 00268 if custom_zlib is used, custom_deflate is ignored since only the built in 00269 zlib function will call custom_deflate*/ 00270 unsigned (*custom_inflate)(unsigned char**, size_t*, 00271 const unsigned char*, size_t, 00272 const LodePNGDecompressSettings*); 00273 00274 const void* custom_context; /*optional custom settings for custom functions*/ 00275 }; 00276 00277 extern const LodePNGDecompressSettings lodepng_default_decompress_settings; 00278 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings); 00279 #endif /*LODEPNG_COMPILE_DECODER*/ 00280 00281 #ifdef LODEPNG_COMPILE_ENCODER 00282 /* 00283 Settings for zlib compression. Tweaking these settings tweaks the balance 00284 between speed and compression ratio. 00285 */ 00286 typedef struct LodePNGCompressSettings LodePNGCompressSettings; 00287 struct LodePNGCompressSettings /*deflate = compress*/ 00288 { 00289 /*LZ77 related settings*/ 00290 unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/ 00291 unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/ 00292 unsigned windowsize; /*must be a power of two <= 32768. higher compresses more but is slower. Typical value: 2048.*/ 00293 unsigned minmatch; /*mininum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ 00294 unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/ 00295 unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/ 00296 00297 /*use custom zlib encoder instead of built in one (default: null)*/ 00298 unsigned (*custom_zlib)(unsigned char**, size_t*, 00299 const unsigned char*, size_t, 00300 const LodePNGCompressSettings*); 00301 /*use custom deflate encoder instead of built in one (default: null) 00302 if custom_zlib is used, custom_deflate is ignored since only the built in 00303 zlib function will call custom_deflate*/ 00304 unsigned (*custom_deflate)(unsigned char**, size_t*, 00305 const unsigned char*, size_t, 00306 const LodePNGCompressSettings*); 00307 00308 const void* custom_context; /*optional custom settings for custom functions*/ 00309 }; 00310 00311 extern const LodePNGCompressSettings lodepng_default_compress_settings; 00312 void lodepng_compress_settings_init(LodePNGCompressSettings* settings); 00313 #endif /*LODEPNG_COMPILE_ENCODER*/ 00314 00315 #ifdef LODEPNG_COMPILE_PNG 00316 /* 00317 Color mode of an image. Contains all information required to decode the pixel 00318 bits to RGBA colors. This information is the same as used in the PNG file 00319 format, and is used both for PNG and raw image data in LodePNG. 00320 */ 00321 typedef struct LodePNGColorMode 00322 { 00323 /*header (IHDR)*/ 00324 LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/ 00325 unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/ 00326 00327 /* 00328 palette (PLTE and tRNS) 00329 00330 Dynamically allocated with the colors of the palette, including alpha. 00331 When encoding a PNG, to store your colors in the palette of the LodePNGColorMode, first use 00332 lodepng_palette_clear, then for each color use lodepng_palette_add. 00333 If you encode an image without alpha with palette, don't forget to put value 255 in each A byte of the palette. 00334 00335 When decoding, by default you can ignore this palette, since LodePNG already 00336 fills the palette colors in the pixels of the raw RGBA output. 00337 00338 The palette is only supported for color type 3. 00339 */ 00340 unsigned char* palette; /*palette in RGBARGBA... order. When allocated, must be either 0, or have size 1024*/ 00341 size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/ 00342 00343 /* 00344 transparent color key (tRNS) 00345 00346 This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit. 00347 For greyscale PNGs, r, g and b will all 3 be set to the same. 00348 00349 When decoding, by default you can ignore this information, since LodePNG sets 00350 pixels with this key to transparent already in the raw RGBA output. 00351 00352 The color key is only supported for color types 0 and 2. 00353 */ 00354 unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/ 00355 unsigned key_r; /*red/greyscale component of color key*/ 00356 unsigned key_g; /*green component of color key*/ 00357 unsigned key_b; /*blue component of color key*/ 00358 } LodePNGColorMode; 00359 00360 /*init, cleanup and copy functions to use with this struct*/ 00361 void lodepng_color_mode_init(LodePNGColorMode* info); 00362 void lodepng_color_mode_cleanup(LodePNGColorMode* info); 00363 /*return value is error code (0 means no error)*/ 00364 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source); 00365 00366 void lodepng_palette_clear(LodePNGColorMode* info); 00367 /*add 1 color to the palette*/ 00368 unsigned lodepng_palette_add(LodePNGColorMode* info, 00369 unsigned char r, unsigned char g, unsigned char b, unsigned char a); 00370 00371 /*get the total amount of bits per pixel, based on colortype and bitdepth in the struct*/ 00372 unsigned lodepng_get_bpp(const LodePNGColorMode* info); 00373 /*get the amount of color channels used, based on colortype in the struct. 00374 If a palette is used, it counts as 1 channel.*/ 00375 unsigned lodepng_get_channels(const LodePNGColorMode* info); 00376 /*is it a greyscale type? (only colortype 0 or 4)*/ 00377 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info); 00378 /*has it got an alpha channel? (only colortype 2 or 6)*/ 00379 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info); 00380 /*has it got a palette? (only colortype 3)*/ 00381 unsigned lodepng_is_palette_type(const LodePNGColorMode* info); 00382 /*only returns true if there is a palette and there is a value in the palette with alpha < 255. 00383 Loops through the palette to check this.*/ 00384 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info); 00385 /* 00386 Check if the given color info indicates the possibility of having non-opaque pixels in the PNG image. 00387 Returns true if the image can have translucent or invisible pixels (it still be opaque if it doesn't use such pixels). 00388 Returns false if the image can only have opaque pixels. 00389 In detail, it returns true only if it's a color type with alpha, or has a palette with non-opaque values, 00390 or if "key_defined" is true. 00391 */ 00392 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info); 00393 /*Returns the byte size of a raw image buffer with given width, height and color mode*/ 00394 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color); 00395 00396 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 00397 /*The information of a Time chunk in PNG.*/ 00398 typedef struct LodePNGTime 00399 { 00400 unsigned year; /*2 bytes used (0-65535)*/ 00401 unsigned month; /*1-12*/ 00402 unsigned day; /*1-31*/ 00403 unsigned hour; /*0-23*/ 00404 unsigned minute; /*0-59*/ 00405 unsigned second; /*0-60 (to allow for leap seconds)*/ 00406 } LodePNGTime; 00407 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 00408 00409 /*Information about the PNG image, except pixels, width and height.*/ 00410 typedef struct LodePNGInfo 00411 { 00412 /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/ 00413 unsigned compression_method;/*compression method of the original file. Always 0.*/ 00414 unsigned filter_method; /*filter method of the original file*/ 00415 unsigned interlace_method; /*interlace method of the original file*/ 00416 LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/ 00417 00418 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 00419 /* 00420 suggested background color chunk (bKGD) 00421 This color uses the same color mode as the PNG (except alpha channel), which can be 1-bit to 16-bit. 00422 00423 For greyscale PNGs, r, g and b will all 3 be set to the same. When encoding 00424 the encoder writes the red one. For palette PNGs: When decoding, the RGB value 00425 will be stored, not a palette index. But when encoding, specify the index of 00426 the palette in background_r, the other two are then ignored. 00427 00428 The decoder does not use this background color to edit the color of pixels. 00429 */ 00430 unsigned background_defined; /*is a suggested background color given?*/ 00431 unsigned background_r; /*red component of suggested background color*/ 00432 unsigned background_g; /*green component of suggested background color*/ 00433 unsigned background_b; /*blue component of suggested background color*/ 00434 00435 /* 00436 non-international text chunks (tEXt and zTXt) 00437 00438 The char** arrays each contain num strings. The actual messages are in 00439 text_strings, while text_keys are keywords that give a short description what 00440 the actual text represents, e.g. Title, Author, Description, or anything else. 00441 00442 A keyword is minimum 1 character and maximum 79 characters long. It's 00443 discouraged to use a single line length longer than 79 characters for texts. 00444 00445 Don't allocate these text buffers yourself. Use the init/cleanup functions 00446 correctly and use lodepng_add_text and lodepng_clear_text. 00447 */ 00448 size_t text_num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/ 00449 char** text_keys; /*the keyword of a text chunk (e.g. "Comment")*/ 00450 char** text_strings; /*the actual text*/ 00451 00452 /* 00453 international text chunks (iTXt) 00454 Similar to the non-international text chunks, but with additional strings 00455 "langtags" and "transkeys". 00456 */ 00457 size_t itext_num; /*the amount of international texts in this PNG*/ 00458 char** itext_keys; /*the English keyword of the text chunk (e.g. "Comment")*/ 00459 char** itext_langtags; /*language tag for this text's language, ISO/IEC 646 string, e.g. ISO 639 language tag*/ 00460 char** itext_transkeys; /*keyword translated to the international language - UTF-8 string*/ 00461 char** itext_strings; /*the actual international text - UTF-8 string*/ 00462 00463 /*time chunk (tIME)*/ 00464 unsigned time_defined; /*set to 1 to make the encoder generate a tIME chunk*/ 00465 LodePNGTime time; 00466 00467 /*phys chunk (pHYs)*/ 00468 unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/ 00469 unsigned phys_x; /*pixels per unit in x direction*/ 00470 unsigned phys_y; /*pixels per unit in y direction*/ 00471 unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ 00472 00473 /* 00474 unknown chunks 00475 There are 3 buffers, one for each position in the PNG where unknown chunks can appear 00476 each buffer contains all unknown chunks for that position consecutively 00477 The 3 buffers are the unknown chunks between certain critical chunks: 00478 0: IHDR-PLTE, 1: PLTE-IDAT, 2: IDAT-IEND 00479 Do not allocate or traverse this data yourself. Use the chunk traversing functions declared 00480 later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct. 00481 */ 00482 unsigned char* unknown_chunks_data[3]; 00483 size_t unknown_chunks_size[3]; /*size in bytes of the unknown chunks, given for protection*/ 00484 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 00485 } LodePNGInfo; 00486 00487 /*init, cleanup and copy functions to use with this struct*/ 00488 void lodepng_info_init(LodePNGInfo* info); 00489 void lodepng_info_cleanup(LodePNGInfo* info); 00490 /*return value is error code (0 means no error)*/ 00491 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source); 00492 00493 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 00494 void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ 00495 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str); /*push back both texts at once*/ 00496 00497 void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/ 00498 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, 00499 const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/ 00500 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 00501 00502 /* 00503 Converts raw buffer from one color type to another color type, based on 00504 LodePNGColorMode structs to describe the input and output color type. 00505 See the reference manual at the end of this header file to see which color conversions are supported. 00506 return value = LodePNG error code (0 if all went ok, an error if the conversion isn't supported) 00507 The out buffer must have size (w * h * bpp + 7) / 8, where bpp is the bits per pixel 00508 of the output color type (lodepng_get_bpp) 00509 The fix_png value works as described in struct LodePNGDecoderSettings. 00510 Note: for 16-bit per channel colors, uses big endian format like PNG does. 00511 */ 00512 unsigned lodepng_convert(unsigned char* out, const unsigned char* in, 00513 LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, 00514 unsigned w, unsigned h, unsigned fix_png); 00515 00516 #ifdef LODEPNG_COMPILE_DECODER 00517 /* 00518 Settings for the decoder. This contains settings for the PNG and the Zlib 00519 decoder, but not the Info settings from the Info structs. 00520 */ 00521 typedef struct LodePNGDecoderSettings 00522 { 00523 LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/ 00524 00525 unsigned ignore_crc; /*ignore CRC checksums*/ 00526 /* 00527 The fix_png setting, if 1, makes the decoder tolerant towards some PNG images 00528 that do not correctly follow the PNG specification. This only supports errors 00529 that are fixable, were found in images that are actually used on the web, and 00530 are silently tolerated by other decoders as well. Currently only one such fix 00531 is implemented: if a palette index is out of bounds given the palette size, 00532 interpret it as opaque black. 00533 By default this value is 0, which makes it stop with an error on such images. 00534 */ 00535 unsigned fix_png; 00536 unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ 00537 00538 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 00539 unsigned read_text_chunks; /*if false but remember_unknown_chunks is true, they're stored in the unknown chunks*/ 00540 /*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/ 00541 unsigned remember_unknown_chunks; 00542 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 00543 } LodePNGDecoderSettings; 00544 00545 void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings); 00546 #endif /*LODEPNG_COMPILE_DECODER*/ 00547 00548 #ifdef LODEPNG_COMPILE_ENCODER 00549 /*automatically use color type with less bits per pixel if losslessly possible. Default: AUTO*/ 00550 typedef enum LodePNGFilterStrategy 00551 { 00552 /*every filter at zero*/ 00553 LFS_ZERO, 00554 /*Use filter that gives minumum sum, as described in the official PNG filter heuristic.*/ 00555 LFS_MINSUM, 00556 /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending 00557 on the image, this is better or worse than minsum.*/ 00558 LFS_ENTROPY, 00559 /* 00560 Brute-force-search PNG filters by compressing each filter for each scanline. 00561 Experimental, very slow, and only rarely gives better compression than MINSUM. 00562 */ 00563 LFS_BRUTE_FORCE, 00564 /*use predefined_filters buffer: you specify the filter type for each scanline*/ 00565 LFS_PREDEFINED 00566 } LodePNGFilterStrategy; 00567 00568 /*automatically use color type with less bits per pixel if losslessly possible. Default: LAC_AUTO*/ 00569 typedef enum LodePNGAutoConvert 00570 { 00571 LAC_NO, /*use color type user requested*/ 00572 LAC_ALPHA, /*use color type user requested, but if only opaque pixels and RGBA or grey+alpha, use RGB or grey*/ 00573 LAC_AUTO, /*use PNG color type that can losslessly represent the uncompressed image the smallest possible*/ 00574 /* 00575 like AUTO, but do not choose 1, 2 or 4 bit per pixel types. 00576 sometimes a PNG image compresses worse if less than 8 bits per pixels. 00577 */ 00578 LAC_AUTO_NO_NIBBLES, 00579 /* 00580 like AUTO, but never choose palette color type. For small images, encoding 00581 the palette may take more bytes than what is gained. Note that AUTO also 00582 already prevents encoding the palette for extremely small images, but that may 00583 not be sufficient because due to the compression it cannot predict when to 00584 switch. 00585 */ 00586 LAC_AUTO_NO_PALETTE, 00587 LAC_AUTO_NO_NIBBLES_NO_PALETTE 00588 } LodePNGAutoConvert; 00589 00590 00591 /* 00592 Automatically chooses color type that gives smallest amount of bits in the 00593 output image, e.g. grey if there are only greyscale pixels, palette if there 00594 are less than 256 colors, ... 00595 The auto_convert parameter allows limiting it to not use palette, ... 00596 */ 00597 unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, 00598 const unsigned char* image, unsigned w, unsigned h, 00599 const LodePNGColorMode* mode_in, 00600 LodePNGAutoConvert auto_convert); 00601 00602 /*Settings for the encoder.*/ 00603 typedef struct LodePNGEncoderSettings 00604 { 00605 LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/ 00606 00607 LodePNGAutoConvert auto_convert; /*how to automatically choose output PNG color type, if at all*/ 00608 00609 /*If true, follows the official PNG heuristic: if the PNG uses a palette or lower than 00610 8 bit depth, set all filters to zero. Otherwise use the filter_strategy. Note that to 00611 completely follow the official PNG heuristic, filter_palette_zero must be true and 00612 filter_strategy must be LFS_MINSUM*/ 00613 unsigned filter_palette_zero; 00614 /*Which filter strategy to use when not using zeroes due to filter_palette_zero. 00615 Set filter_palette_zero to 0 to ensure always using your chosen strategy. Default: LFS_MINSUM*/ 00616 LodePNGFilterStrategy filter_strategy; 00617 /*used if filter_strategy is LFS_PREDEFINED. In that case, this must point to a buffer with 00618 the same length as the amount of scanlines in the image, and each value must <= 5. You 00619 have to cleanup this buffer, LodePNG will never free it. Don't forget that filter_palette_zero 00620 must be set to 0 to ensure this is also used on palette or low bitdepth images.*/ 00621 const unsigned char* predefined_filters; 00622 00623 /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette). 00624 If colortype is 3, PLTE is _always_ created.*/ 00625 unsigned force_palette; 00626 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 00627 /*add LodePNG identifier and version as a text chunk, for debugging*/ 00628 unsigned add_id; 00629 /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/ 00630 unsigned text_compression; 00631 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 00632 } LodePNGEncoderSettings; 00633 00634 void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings); 00635 #endif /*LODEPNG_COMPILE_ENCODER*/ 00636 00637 00638 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) 00639 /*The settings, state and information for extended encoding and decoding.*/ 00640 typedef struct LodePNGState 00641 { 00642 #ifdef LODEPNG_COMPILE_DECODER 00643 LodePNGDecoderSettings decoder; /*the decoding settings*/ 00644 #endif /*LODEPNG_COMPILE_DECODER*/ 00645 #ifdef LODEPNG_COMPILE_ENCODER 00646 LodePNGEncoderSettings encoder; /*the encoding settings*/ 00647 #endif /*LODEPNG_COMPILE_ENCODER*/ 00648 LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/ 00649 LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/ 00650 unsigned error; 00651 #ifdef LODEPNG_COMPILE_CPP 00652 //For the lodepng::State subclass. 00653 virtual ~LodePNGState(){} 00654 #endif 00655 } LodePNGState; 00656 00657 /*init, cleanup and copy functions to use with this struct*/ 00658 void lodepng_state_init(LodePNGState* state); 00659 void lodepng_state_cleanup(LodePNGState* state); 00660 void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source); 00661 #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */ 00662 00663 #ifdef LODEPNG_COMPILE_DECODER 00664 /* 00665 Same as lodepng_decode_memory, but uses a LodePNGState to allow custom settings and 00666 getting much more information about the PNG image and color mode. 00667 */ 00668 unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, 00669 LodePNGState* state, 00670 const unsigned char* in, size_t insize); 00671 00672 /* 00673 Read the PNG header, but not the actual data. This returns only the information 00674 that is in the header chunk of the PNG, such as width, height and color type. The 00675 information is placed in the info_png field of the LodePNGState. 00676 */ 00677 unsigned lodepng_inspect(unsigned* w, unsigned* h, 00678 LodePNGState* state, 00679 const unsigned char* in, size_t insize); 00680 #endif /*LODEPNG_COMPILE_DECODER*/ 00681 00682 00683 #ifdef LODEPNG_COMPILE_ENCODER 00684 /*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/ 00685 unsigned lodepng_encode(unsigned char** out, size_t* outsize, 00686 const unsigned char* image, unsigned w, unsigned h, 00687 LodePNGState* state); 00688 #endif /*LODEPNG_COMPILE_ENCODER*/ 00689 00690 /* 00691 The lodepng_chunk functions are normally not needed, except to traverse the 00692 unknown chunks stored in the LodePNGInfo struct, or add new ones to it. 00693 It also allows traversing the chunks of an encoded PNG file yourself. 00694 00695 PNG standard chunk naming conventions: 00696 First byte: uppercase = critical, lowercase = ancillary 00697 Second byte: uppercase = public, lowercase = private 00698 Third byte: must be uppercase 00699 Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy 00700 */ 00701 00702 /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/ 00703 unsigned lodepng_chunk_length(const unsigned char* chunk); 00704 00705 /*puts the 4-byte type in null terminated string*/ 00706 void lodepng_chunk_type(char type[5], const unsigned char* chunk); 00707 00708 /*check if the type is the given type*/ 00709 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type); 00710 00711 /*0: it's one of the critical chunk types, 1: it's an ancillary chunk (see PNG standard)*/ 00712 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk); 00713 00714 /*0: public, 1: private (see PNG standard)*/ 00715 unsigned char lodepng_chunk_private(const unsigned char* chunk); 00716 00717 /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy (see PNG standard)*/ 00718 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk); 00719 00720 /*get pointer to the data of the chunk, where the input points to the header of the chunk*/ 00721 unsigned char* lodepng_chunk_data(unsigned char* chunk); 00722 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk); 00723 00724 /*returns 0 if the crc is correct, 1 if it's incorrect (0 for OK as usual!)*/ 00725 unsigned lodepng_chunk_check_crc(const unsigned char* chunk); 00726 00727 /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/ 00728 void lodepng_chunk_generate_crc(unsigned char* chunk); 00729 00730 /*iterate to next chunks. don't use on IEND chunk, as there is no next chunk then*/ 00731 unsigned char* lodepng_chunk_next(unsigned char* chunk); 00732 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk); 00733 00734 /* 00735 Appends chunk to the data in out. The given chunk should already have its chunk header. 00736 The out variable and outlength are updated to reflect the new reallocated buffer. 00737 Returns error code (0 if it went ok) 00738 */ 00739 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk); 00740 00741 /* 00742 Appends new chunk to out. The chunk to append is given by giving its length, type 00743 and data separately. The type is a 4-letter string. 00744 The out variable and outlength are updated to reflect the new reallocated buffer. 00745 Returne error code (0 if it went ok) 00746 */ 00747 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, 00748 const char* type, const unsigned char* data); 00749 00750 00751 /*Calculate CRC32 of buffer*/ 00752 unsigned lodepng_crc32(const unsigned char* buf, size_t len); 00753 #endif /*LODEPNG_COMPILE_PNG*/ 00754 00755 00756 #ifdef LODEPNG_COMPILE_ZLIB 00757 /* 00758 This zlib part can be used independently to zlib compress and decompress a 00759 buffer. It cannot be used to create gzip files however, and it only supports the 00760 part of zlib that is required for PNG, it does not support dictionaries. 00761 */ 00762 00763 #ifdef LODEPNG_COMPILE_DECODER 00764 /*Inflate a buffer. Inflate is the decompression step of deflate. Out buffer must be freed after use.*/ 00765 unsigned lodepng_inflate(unsigned char** out, size_t* outsize, 00766 const unsigned char* in, size_t insize, 00767 const LodePNGDecompressSettings* settings); 00768 00769 /* 00770 Decompresses Zlib data. Reallocates the out buffer and appends the data. The 00771 data must be according to the zlib specification. 00772 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid 00773 buffer and *outsize its size in bytes. out must be freed by user after usage. 00774 */ 00775 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, 00776 const unsigned char* in, size_t insize, 00777 const LodePNGDecompressSettings* settings); 00778 #endif /*LODEPNG_COMPILE_DECODER*/ 00779 00780 #ifdef LODEPNG_COMPILE_ENCODER 00781 /* 00782 Compresses data with Zlib. Reallocates the out buffer and appends the data. 00783 Zlib adds a small header and trailer around the deflate data. 00784 The data is output in the format of the zlib specification. 00785 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid 00786 buffer and *outsize its size in bytes. out must be freed by user after usage. 00787 */ 00788 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, 00789 const unsigned char* in, size_t insize, 00790 const LodePNGCompressSettings* settings); 00791 00792 /* 00793 Find length-limited Huffman code for given frequencies. This function is in the 00794 public interface only for tests, it's used internally by lodepng_deflate. 00795 */ 00796 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, 00797 size_t numcodes, unsigned maxbitlen); 00798 00799 /*Compress a buffer with deflate. See RFC 1951. Out buffer must be freed after use.*/ 00800 unsigned lodepng_deflate(unsigned char** out, size_t* outsize, 00801 const unsigned char* in, size_t insize, 00802 const LodePNGCompressSettings* settings); 00803 00804 #endif /*LODEPNG_COMPILE_ENCODER*/ 00805 #endif /*LODEPNG_COMPILE_ZLIB*/ 00806 00807 #ifdef LODEPNG_COMPILE_DISK 00808 /* 00809 Load a file from disk into buffer. The function allocates the out buffer, and 00810 after usage you should free it. 00811 out: output parameter, contains pointer to loaded buffer. 00812 outsize: output parameter, size of the allocated out buffer 00813 filename: the path to the file to load 00814 return value: error code (0 means ok) 00815 */ 00816 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename); 00817 00818 /* 00819 Save a file from buffer to disk. Warning, if it exists, this function overwrites 00820 the file without warning! 00821 buffer: the buffer to write 00822 buffersize: size of the buffer to write 00823 filename: the path to the file to save to 00824 return value: error code (0 means ok) 00825 */ 00826 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename); 00827 #endif /*LODEPNG_COMPILE_DISK*/ 00828 00829 #ifdef LODEPNG_COMPILE_CPP 00830 //The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. 00831 namespace lodepng 00832 { 00833 #ifdef LODEPNG_COMPILE_PNG 00834 class State : public LodePNGState 00835 { 00836 public: 00837 State(); 00838 State(const State& other); 00839 virtual ~State(); 00840 State& operator=(const State& other); 00841 }; 00842 00843 #ifdef LODEPNG_COMPILE_DECODER 00844 //Same as other lodepng::decode, but using a State for more settings and information. 00845 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 00846 State& state, 00847 const unsigned char* in, size_t insize); 00848 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 00849 State& state, 00850 const std::vector<unsigned char>& in); 00851 #endif /*LODEPNG_COMPILE_DECODER*/ 00852 00853 #ifdef LODEPNG_COMPILE_ENCODER 00854 //Same as other lodepng::encode, but using a State for more settings and information. 00855 unsigned encode(std::vector<unsigned char>& out, 00856 const unsigned char* in, unsigned w, unsigned h, 00857 State& state); 00858 unsigned encode(std::vector<unsigned char>& out, 00859 const std::vector<unsigned char>& in, unsigned w, unsigned h, 00860 State& state); 00861 #endif /*LODEPNG_COMPILE_ENCODER*/ 00862 00863 #ifdef LODEPNG_COMPILE_DISK 00864 /* 00865 Load a file from disk into an std::vector. If the vector is empty, then either 00866 the file doesn't exist or is an empty file. 00867 */ 00868 void load_file(std::vector<unsigned char>& buffer, const std::string& filename); 00869 00870 /* 00871 Save the binary data in an std::vector to a file on disk. The file is overwritten 00872 without warning. 00873 */ 00874 void save_file(const std::vector<unsigned char>& buffer, const std::string& filename); 00875 #endif //LODEPNG_COMPILE_DISK 00876 #endif //LODEPNG_COMPILE_PNG 00877 00878 #ifdef LODEPNG_COMPILE_ZLIB 00879 #ifdef LODEPNG_COMPILE_DECODER 00880 //Zlib-decompress an unsigned char buffer 00881 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, 00882 const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); 00883 00884 //Zlib-decompress an std::vector 00885 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, 00886 const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); 00887 #endif //LODEPNG_COMPILE_DECODER 00888 00889 #ifdef LODEPNG_COMPILE_ENCODER 00890 //Zlib-compress an unsigned char buffer 00891 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, 00892 const LodePNGCompressSettings& settings = lodepng_default_compress_settings); 00893 00894 //Zlib-compress an std::vector 00895 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, 00896 const LodePNGCompressSettings& settings = lodepng_default_compress_settings); 00897 #endif //LODEPNG_COMPILE_ENCODER 00898 #endif //LODEPNG_COMPILE_ZLIB 00899 } //namespace lodepng 00900 #endif /*LODEPNG_COMPILE_CPP*/ 00901 00902 /* 00903 TODO: 00904 [.] test if there are no memory leaks or security exploits - done a lot but needs to be checked often 00905 [.] check compatibility with vareous compilers - done but needs to be redone for every newer version 00906 [X] converting color to 16-bit per channel types 00907 [ ] read all public PNG chunk types (but never let the color profile and gamma ones touch RGB values) 00908 [ ] make sure encoder generates no chunks with size > (2^31)-1 00909 [ ] partial decoding (stream processing) 00910 [X] let the "isFullyOpaque" function check color keys and transparent palettes too 00911 [X] better name for the variables "codes", "codesD", "codelengthcodes", "clcl" and "lldl" 00912 [ ] don't stop decoding on errors like 69, 57, 58 (make warnings) 00913 [ ] make option to choose if the raw image with non multiple of 8 bits per scanline should have padding bits or not 00914 [ ] let the C++ wrapper catch exceptions coming from the standard library and return LodePNG error codes 00915 */ 00916 00917 #endif /*LODEPNG_H inclusion guard*/ 00918 00919 /* 00920 LodePNG Documentation 00921 --------------------- 00922 00923 0. table of contents 00924 -------------------- 00925 00926 1. about 00927 1.1. supported features 00928 1.2. features not supported 00929 2. C and C++ version 00930 3. security 00931 4. decoding 00932 5. encoding 00933 6. color conversions 00934 6.1. PNG color types 00935 6.2. color conversions 00936 6.3. padding bits 00937 6.4. A note about 16-bits per channel and endianness 00938 7. error values 00939 8. chunks and PNG editing 00940 9. compiler support 00941 10. examples 00942 10.1. decoder C++ example 00943 10.2. decoder C example 00944 11. changes 00945 12. contact information 00946 00947 00948 1. about 00949 -------- 00950 00951 PNG is a file format to store raster images losslessly with good compression, 00952 supporting different color types and alpha channel. 00953 00954 LodePNG is a PNG codec according to the Portable Network Graphics (PNG) 00955 Specification (Second Edition) - W3C Recommendation 10 November 2003. 00956 00957 The specifications used are: 00958 00959 *) Portable Network Graphics (PNG) Specification (Second Edition): 00960 http://www.w3.org/TR/2003/REC-PNG-20031110 00961 *) RFC 1950 ZLIB Compressed Data Format version 3.3: 00962 http://www.gzip.org/zlib/rfc-zlib.html 00963 *) RFC 1951 DEFLATE Compressed Data Format Specification ver 1.3: 00964 http://www.gzip.org/zlib/rfc-deflate.html 00965 00966 The most recent version of LodePNG can currently be found at 00967 http://lodev.org/lodepng/ 00968 00969 LodePNG works both in C (ISO C90) and C++, with a C++ wrapper that adds 00970 extra functionality. 00971 00972 LodePNG exists out of two files: 00973 -lodepng.h: the header file for both C and C++ 00974 -lodepng.c(pp): give it the name lodepng.c or lodepng.cpp (or .cc) depending on your usage 00975 00976 If you want to start using LodePNG right away without reading this doc, get the 00977 examples from the LodePNG website to see how to use it in code, or check the 00978 smaller examples in chapter 13 here. 00979 00980 LodePNG is simple but only supports the basic requirements. To achieve 00981 simplicity, the following design choices were made: There are no dependencies 00982 on any external library. There are functions to decode and encode a PNG with 00983 a single function call, and extended versions of these functions taking a 00984 LodePNGState struct allowing to specify or get more information. By default 00985 the colors of the raw image are always RGB or RGBA, no matter what color type 00986 the PNG file uses. To read and write files, there are simple functions to 00987 convert the files to/from buffers in memory. 00988 00989 This all makes LodePNG suitable for loading textures in games, demos and small 00990 programs, ... It's less suitable for full fledged image editors, loading PNGs 00991 over network (it requires all the image data to be available before decoding can 00992 begin), life-critical systems, ... 00993 00994 1.1. supported features 00995 ----------------------- 00996 00997 The following features are supported by the decoder: 00998 00999 *) decoding of PNGs with any color type, bit depth and interlace mode, to a 24- or 32-bit color raw image, 01000 or the same color type as the PNG 01001 *) encoding of PNGs, from any raw image to 24- or 32-bit color, or the same color type as the raw image 01002 *) Adam7 interlace and deinterlace for any color type 01003 *) loading the image from harddisk or decoding it from a buffer from other sources than harddisk 01004 *) support for alpha channels, including RGBA color model, translucent palettes and color keying 01005 *) zlib decompression (inflate) 01006 *) zlib compression (deflate) 01007 *) CRC32 and ADLER32 checksums 01008 *) handling of unknown chunks, allowing making a PNG editor that stores custom and unknown chunks. 01009 *) the following chunks are supported (generated/interpreted) by both encoder and decoder: 01010 IHDR: header information 01011 PLTE: color palette 01012 IDAT: pixel data 01013 IEND: the final chunk 01014 tRNS: transparency for palettized images 01015 tEXt: textual information 01016 zTXt: compressed textual information 01017 iTXt: international textual information 01018 bKGD: suggested background color 01019 pHYs: physical dimensions 01020 tIME: modification time 01021 01022 1.2. features not supported 01023 --------------------------- 01024 01025 The following features are _not_ supported: 01026 01027 *) some features needed to make a conformant PNG-Editor might be still missing. 01028 *) partial loading/stream processing. All data must be available and is processed in one call. 01029 *) The following public chunks are not supported but treated as unknown chunks by LodePNG 01030 cHRM, gAMA, iCCP, sRGB, sBIT, hIST, sPLT 01031 Some of these are not supported on purpose: LodePNG wants to provide the RGB values 01032 stored in the pixels, not values modified by system dependent gamma or color models. 01033 01034 01035 2. C and C++ version 01036 -------------------- 01037 01038 The C version uses buffers allocated with alloc that you need to free() 01039 yourself. You need to use init and cleanup functions for each struct whenever 01040 using a struct from the C version to avoid exploits and memory leaks. 01041 01042 The C++ version has extra functions with std::vectors in the interface and the 01043 lodepng::State class which is a LodePNGState with constructor and destructor. 01044 01045 These files work without modification for both C and C++ compilers because all 01046 the additional C++ code is in "#ifdef __cplusplus" blocks that make C-compilers 01047 ignore it, and the C code is made to compile both with strict ISO C90 and C++. 01048 01049 To use the C++ version, you need to rename the source file to lodepng.cpp 01050 (instead of lodepng.c), and compile it with a C++ compiler. 01051 01052 To use the C version, you need to rename the source file to lodepng.c (instead 01053 of lodepng.cpp), and compile it with a C compiler. 01054 01055 01056 3. Security 01057 ----------- 01058 01059 Even if carefully designed, it's always possible that LodePNG contains possible 01060 exploits. If you discover one, please let me know, and it will be fixed. 01061 01062 When using LodePNG, care has to be taken with the C version of LodePNG, as well 01063 as the C-style structs when working with C++. The following conventions are used 01064 for all C-style structs: 01065 01066 -if a struct has a corresponding init function, always call the init function when making a new one 01067 -if a struct has a corresponding cleanup function, call it before the struct disappears to avoid memory leaks 01068 -if a struct has a corresponding copy function, use the copy function instead of "=". 01069 The destination must also be inited already. 01070 01071 01072 4. Decoding 01073 ----------- 01074 01075 Decoding converts a PNG compressed image to a raw pixel buffer. 01076 01077 Most documentation on using the decoder is at its declarations in the header 01078 above. For C, simple decoding can be done with functions such as 01079 lodepng_decode32, and more advanced decoding can be done with the struct 01080 LodePNGState and lodepng_decode. For C++, all decoding can be done with the 01081 various lodepng::decode functions, and lodepng::State can be used for advanced 01082 features. 01083 01084 When using the LodePNGState, it uses the following fields for decoding: 01085 *) LodePNGInfo info_png: it stores extra information about the PNG (the input) in here 01086 *) LodePNGColorMode info_raw: here you can say what color mode of the raw image (the output) you want to get 01087 *) LodePNGDecoderSettings decoder: you can specify a few extra settings for the decoder to use 01088 01089 LodePNGInfo info_png 01090 -------------------- 01091 01092 After decoding, this contains extra information of the PNG image, except the actual 01093 pixels, width and height because these are already gotten directly from the decoder 01094 functions. 01095 01096 It contains for example the original color type of the PNG image, text comments, 01097 suggested background color, etc... More details about the LodePNGInfo struct are 01098 at its declaration documentation. 01099 01100 LodePNGColorMode info_raw 01101 ------------------------- 01102 01103 When decoding, here you can specify which color type you want 01104 the resulting raw image to be. If this is different from the colortype of the 01105 PNG, then the decoder will automatically convert the result. This conversion 01106 always works, except if you want it to convert a color PNG to greyscale or to 01107 a palette with missing colors. 01108 01109 By default, 32-bit color is used for the result. 01110 01111 LodePNGDecoderSettings decoder 01112 ------------------------------ 01113 01114 The settings can be used to ignore the errors created by invalid CRC and Adler32 01115 chunks, and to disable the decoding of tEXt chunks. 01116 01117 There's also a setting color_convert, true by default. If false, no conversion 01118 is done, the resulting data will be as it was in the PNG (after decompression) 01119 and you'll have to puzzle the colors of the pixels together yourself using the 01120 color type information in the LodePNGInfo. 01121 01122 01123 5. Encoding 01124 ----------- 01125 01126 Encoding converts a raw pixel buffer to a PNG compressed image. 01127 01128 Most documentation on using the encoder is at its declarations in the header 01129 above. For C, simple encoding can be done with functions such as 01130 lodepng_encode32, and more advanced decoding can be done with the struct 01131 LodePNGState and lodepng_encode. For C++, all encoding can be done with the 01132 various lodepng::encode functions, and lodepng::State can be used for advanced 01133 features. 01134 01135 Like the decoder, the encoder can also give errors. However it gives less errors 01136 since the encoder input is trusted, the decoder input (a PNG image that could 01137 be forged by anyone) is not trusted. 01138 01139 When using the LodePNGState, it uses the following fields for encoding: 01140 *) LodePNGInfo info_png: here you specify how you want the PNG (the output) to be. 01141 *) LodePNGColorMode info_raw: here you say what color type of the raw image (the input) has 01142 *) LodePNGEncoderSettings encoder: you can specify a few settings for the encoder to use 01143 01144 LodePNGInfo info_png 01145 -------------------- 01146 01147 When encoding, you use this the opposite way as when decoding: for encoding, 01148 you fill in the values you want the PNG to have before encoding. By default it's 01149 not needed to specify a color type for the PNG since it's automatically chosen, 01150 but it's possible to choose it yourself given the right settings. 01151 01152 The encoder will not always exactly match the LodePNGInfo struct you give, 01153 it tries as close as possible. Some things are ignored by the encoder. The 01154 encoder uses, for example, the following settings from it when applicable: 01155 colortype and bitdepth, text chunks, time chunk, the color key, the palette, the 01156 background color, the interlace method, unknown chunks, ... 01157 01158 When encoding to a PNG with colortype 3, the encoder will generate a PLTE chunk. 01159 If the palette contains any colors for which the alpha channel is not 255 (so 01160 there are translucent colors in the palette), it'll add a tRNS chunk. 01161 01162 LodePNGColorMode info_raw 01163 ------------------------- 01164 01165 You specify the color type of the raw image that you give to the input here, 01166 including a possible transparent color key and palette you happen to be using in 01167 your raw image data. 01168 01169 By default, 32-bit color is assumed, meaning your input has to be in RGBA 01170 format with 4 bytes (unsigned chars) per pixel. 01171 01172 LodePNGEncoderSettings encoder 01173 ------------------------------ 01174 01175 The following settings are supported (some are in sub-structs): 01176 *) auto_convert: when this option is enabled, the encoder will 01177 automatically choose the smallest possible color mode (including color key) that 01178 can encode the colors of all pixels without information loss. 01179 *) btype: the block type for LZ77. 0 = uncompressed, 1 = fixed huffman tree, 01180 2 = dynamic huffman tree (best compression). Should be 2 for proper 01181 compression. 01182 *) use_lz77: whether or not to use LZ77 for compressed block types. Should be 01183 true for proper compression. 01184 *) windowsize: the window size used by the LZ77 encoder (1 - 32768). Has value 01185 2048 by default, but can be set to 32768 for better, but slow, compression. 01186 *) force_palette: if colortype is 2 or 6, you can make the encoder write a PLTE 01187 chunk if force_palette is true. This can used as suggested palette to convert 01188 to by viewers that don't support more than 256 colors (if those still exist) 01189 *) add_id: add text chunk "Encoder: LodePNG <version>" to the image. 01190 *) text_compression: default 1. If 1, it'll store texts as zTXt instead of tEXt chunks. 01191 zTXt chunks use zlib compression on the text. This gives a smaller result on 01192 large texts but a larger result on small texts (such as a single program name). 01193 It's all tEXt or all zTXt though, there's no separate setting per text yet. 01194 01195 01196 6. color conversions 01197 -------------------- 01198 01199 An important thing to note about LodePNG, is that the color type of the PNG, and 01200 the color type of the raw image, are completely independent. By default, when 01201 you decode a PNG, you get the result as a raw image in the color type you want, 01202 no matter whether the PNG was encoded with a palette, greyscale or RGBA color. 01203 And if you encode an image, by default LodePNG will automatically choose the PNG 01204 color type that gives good compression based on the values of colors and amount 01205 of colors in the image. It can be configured to let you control it instead as 01206 well, though. 01207 01208 To be able to do this, LodePNG does conversions from one color mode to another. 01209 It can convert from almost any color type to any other color type, except the 01210 following conversions: RGB to greyscale is not supported, and converting to a 01211 palette when the palette doesn't have a required color is not supported. This is 01212 not supported on purpose: this is information loss which requires a color 01213 reduction algorithm that is beyong the scope of a PNG encoder (yes, RGB to grey 01214 is easy, but there are multiple ways if you want to give some channels more 01215 weight). 01216 01217 By default, when decoding, you get the raw image in 32-bit RGBA or 24-bit RGB 01218 color, no matter what color type the PNG has. And by default when encoding, 01219 LodePNG automatically picks the best color model for the output PNG, and expects 01220 the input image to be 32-bit RGBA or 24-bit RGB. So, unless you want to control 01221 the color format of the images yourself, you can skip this chapter. 01222 01223 6.1. PNG color types 01224 -------------------- 01225 01226 A PNG image can have many color types, ranging from 1-bit color to 64-bit color, 01227 as well as palettized color modes. After the zlib decompression and unfiltering 01228 in the PNG image is done, the raw pixel data will have that color type and thus 01229 a certain amount of bits per pixel. If you want the output raw image after 01230 decoding to have another color type, a conversion is done by LodePNG. 01231 01232 The PNG specification gives the following color types: 01233 01234 0: greyscale, bit depths 1, 2, 4, 8, 16 01235 2: RGB, bit depths 8 and 16 01236 3: palette, bit depths 1, 2, 4 and 8 01237 4: greyscale with alpha, bit depths 8 and 16 01238 6: RGBA, bit depths 8 and 16 01239 01240 Bit depth is the amount of bits per pixel per color channel. So the total amount 01241 of bits per pixel is: amount of channels * bitdepth. 01242 01243 6.2. color conversions 01244 ---------------------- 01245 01246 As explained in the sections about the encoder and decoder, you can specify 01247 color types and bit depths in info_png and info_raw to change the default 01248 behaviour. 01249 01250 If, when decoding, you want the raw image to be something else than the default, 01251 you need to set the color type and bit depth you want in the LodePNGColorMode, 01252 or the parameters of the simple function of LodePNG you're using. 01253 01254 If, when encoding, you use another color type than the default in the input 01255 image, you need to specify its color type and bit depth in the LodePNGColorMode 01256 of the raw image, or use the parameters of the simplefunction of LodePNG you're 01257 using. 01258 01259 If, when encoding, you don't want LodePNG to choose the output PNG color type 01260 but control it yourself, you need to set auto_convert in the encoder settings 01261 to LAC_NONE, and specify the color type you want in the LodePNGInfo of the 01262 encoder. 01263 01264 If you do any of the above, LodePNG may need to do a color conversion, which 01265 follows the rules below, and may sometimes not be allowed. 01266 01267 To avoid some confusion: 01268 -the decoder converts from PNG to raw image 01269 -the encoder converts from raw image to PNG 01270 -the colortype and bitdepth in LodePNGColorMode info_raw, are those of the raw image 01271 -the colortype and bitdepth in the color field of LodePNGInfo info_png, are those of the PNG 01272 -when encoding, the color type in LodePNGInfo is ignored if auto_convert 01273 is enabled, it is automatically generated instead 01274 -when decoding, the color type in LodePNGInfo is set by the decoder to that of the original 01275 PNG image, but it can be ignored since the raw image has the color type you requested instead 01276 -if the color type of the LodePNGColorMode and PNG image aren't the same, a conversion 01277 between the color types is done if the color types are supported. If it is not 01278 supported, an error is returned. If the types are the same, no conversion is done. 01279 -even though some conversions aren't supported, LodePNG supports loading PNGs from any 01280 colortype and saving PNGs to any colortype, sometimes it just requires preparing 01281 the raw image correctly before encoding. 01282 -both encoder and decoder use the same color converter. 01283 01284 Non supported color conversions: 01285 -color to greyscale: no error is thrown, but the result will look ugly because 01286 only the red channel is taken 01287 -anything, to palette when that palette does not have that color in it: in this 01288 case an error is thrown 01289 01290 Supported color conversions: 01291 -anything to 8-bit RGB, 8-bit RGBA, 16-bit RGB, 16-bit RGBA 01292 -any grey or grey+alpha, to grey or grey+alpha 01293 -anything to a palette, as long as the palette has the requested colors in it 01294 -removing alpha channel 01295 -higher to smaller bitdepth, and vice versa 01296 01297 If you want no color conversion to be done: 01298 -In the encoder, you can make it save a PNG with any color type by giving the 01299 raw color mode and LodePNGInfo the same color mode, and setting auto_convert to 01300 LAC_NO. 01301 -In the decoder, you can make it store the pixel data in the same color type 01302 as the PNG has, by setting the color_convert setting to false. Settings in 01303 info_raw are then ignored. 01304 01305 The function lodepng_convert does the color conversion. It is available in the 01306 interface but normally isn't needed since the encoder and decoder already call 01307 it. 01308 01309 6.3. padding bits 01310 ----------------- 01311 01312 In the PNG file format, if a less than 8-bit per pixel color type is used and the scanlines 01313 have a bit amount that isn't a multiple of 8, then padding bits are used so that each 01314 scanline starts at a fresh byte. But that is NOT true for the LodePNG raw input and output. 01315 The raw input image you give to the encoder, and the raw output image you get from the decoder 01316 will NOT have these padding bits, e.g. in the case of a 1-bit image with a width 01317 of 7 pixels, the first pixel of the second scanline will the the 8th bit of the first byte, 01318 not the first bit of a new byte. 01319 01320 6.4. A note about 16-bits per channel and endianness 01321 ---------------------------------------------------- 01322 01323 LodePNG uses unsigned char arrays for 16-bit per channel colors too, just like 01324 for any other color format. The 16-bit values are stored in big endian (most 01325 significant byte first) in these arrays. This is the opposite order of the 01326 little endian used by x86 CPU's. 01327 01328 LodePNG always uses big endian because the PNG file format does so internally. 01329 Conversions to other formats than PNG uses internally are not supported by 01330 LodePNG on purpose, there are myriads of formats, including endianness of 16-bit 01331 colors, the order in which you store R, G, B and A, and so on. Supporting and 01332 converting to/from all that is outside the scope of LodePNG. 01333 01334 This may mean that, depending on your use case, you may want to convert the big 01335 endian output of LodePNG to little endian with a for loop. This is certainly not 01336 always needed, many applications and libraries support big endian 16-bit colors 01337 anyway, but it means you cannot simply cast the unsigned char* buffer to an 01338 unsigned short* buffer on x86 CPUs. 01339 01340 01341 7. error values 01342 --------------- 01343 01344 All functions in LodePNG that return an error code, return 0 if everything went 01345 OK, or a non-zero code if there was an error. 01346 01347 The meaning of the LodePNG error values can be retrieved with the function 01348 lodepng_error_text: given the numerical error code, it returns a description 01349 of the error in English as a string. 01350 01351 Check the implementation of lodepng_error_text to see the meaning of each code. 01352 01353 01354 8. chunks and PNG editing 01355 ------------------------- 01356 01357 If you want to add extra chunks to a PNG you encode, or use LodePNG for a PNG 01358 editor that should follow the rules about handling of unknown chunks, or if your 01359 program is able to read other types of chunks than the ones handled by LodePNG, 01360 then that's possible with the chunk functions of LodePNG. 01361 01362 A PNG chunk has the following layout: 01363 01364 4 bytes length 01365 4 bytes type name 01366 length bytes data 01367 4 bytes CRC 01368 01369 8.1. iterating through chunks 01370 ----------------------------- 01371 01372 If you have a buffer containing the PNG image data, then the first chunk (the 01373 IHDR chunk) starts at byte number 8 of that buffer. The first 8 bytes are the 01374 signature of the PNG and are not part of a chunk. But if you start at byte 8 01375 then you have a chunk, and can check the following things of it. 01376 01377 NOTE: none of these functions check for memory buffer boundaries. To avoid 01378 exploits, always make sure the buffer contains all the data of the chunks. 01379 When using lodepng_chunk_next, make sure the returned value is within the 01380 allocated memory. 01381 01382 unsigned lodepng_chunk_length(const unsigned char* chunk): 01383 01384 Get the length of the chunk's data. The total chunk length is this length + 12. 01385 01386 void lodepng_chunk_type(char type[5], const unsigned char* chunk): 01387 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type): 01388 01389 Get the type of the chunk or compare if it's a certain type 01390 01391 unsigned char lodepng_chunk_critical(const unsigned char* chunk): 01392 unsigned char lodepng_chunk_private(const unsigned char* chunk): 01393 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk): 01394 01395 Check if the chunk is critical in the PNG standard (only IHDR, PLTE, IDAT and IEND are). 01396 Check if the chunk is private (public chunks are part of the standard, private ones not). 01397 Check if the chunk is safe to copy. If it's not, then, when modifying data in a critical 01398 chunk, unsafe to copy chunks of the old image may NOT be saved in the new one if your 01399 program doesn't handle that type of unknown chunk. 01400 01401 unsigned char* lodepng_chunk_data(unsigned char* chunk): 01402 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk): 01403 01404 Get a pointer to the start of the data of the chunk. 01405 01406 unsigned lodepng_chunk_check_crc(const unsigned char* chunk): 01407 void lodepng_chunk_generate_crc(unsigned char* chunk): 01408 01409 Check if the crc is correct or generate a correct one. 01410 01411 unsigned char* lodepng_chunk_next(unsigned char* chunk): 01412 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk): 01413 01414 Iterate to the next chunk. This works if you have a buffer with consecutive chunks. Note that these 01415 functions do no boundary checking of the allocated data whatsoever, so make sure there is enough 01416 data available in the buffer to be able to go to the next chunk. 01417 01418 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk): 01419 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, 01420 const char* type, const unsigned char* data): 01421 01422 These functions are used to create new chunks that are appended to the data in *out that has 01423 length *outlength. The append function appends an existing chunk to the new data. The create 01424 function creates a new chunk with the given parameters and appends it. Type is the 4-letter 01425 name of the chunk. 01426 01427 8.2. chunks in info_png 01428 ----------------------- 01429 01430 The LodePNGInfo struct contains fields with the unknown chunk in it. It has 3 01431 buffers (each with size) to contain 3 types of unknown chunks: 01432 the ones that come before the PLTE chunk, the ones that come between the PLTE 01433 and the IDAT chunks, and the ones that come after the IDAT chunks. 01434 It's necessary to make the distionction between these 3 cases because the PNG 01435 standard forces to keep the ordering of unknown chunks compared to the critical 01436 chunks, but does not force any other ordering rules. 01437 01438 info_png.unknown_chunks_data[0] is the chunks before PLTE 01439 info_png.unknown_chunks_data[1] is the chunks after PLTE, before IDAT 01440 info_png.unknown_chunks_data[2] is the chunks after IDAT 01441 01442 The chunks in these 3 buffers can be iterated through and read by using the same 01443 way described in the previous subchapter. 01444 01445 When using the decoder to decode a PNG, you can make it store all unknown chunks 01446 if you set the option settings.remember_unknown_chunks to 1. By default, this 01447 option is off (0). 01448 01449 The encoder will always encode unknown chunks that are stored in the info_png. 01450 If you need it to add a particular chunk that isn't known by LodePNG, you can 01451 use lodepng_chunk_append or lodepng_chunk_create to the chunk data in 01452 info_png.unknown_chunks_data[x]. 01453 01454 Chunks that are known by LodePNG should not be added in that way. E.g. to make 01455 LodePNG add a bKGD chunk, set background_defined to true and add the correct 01456 parameters there instead. 01457 01458 01459 9. compiler support 01460 ------------------- 01461 01462 No libraries other than the current standard C library are needed to compile 01463 LodePNG. For the C++ version, only the standard C++ library is needed on top. 01464 Add the files lodepng.c(pp) and lodepng.h to your project, include 01465 lodepng.h where needed, and your program can read/write PNG files. 01466 01467 If performance is important, use optimization when compiling! For both the 01468 encoder and decoder, this makes a large difference. 01469 01470 Make sure that LodePNG is compiled with the same compiler of the same version 01471 and with the same settings as the rest of the program, or the interfaces with 01472 std::vectors and std::strings in C++ can be incompatible. 01473 01474 CHAR_BITS must be 8 or higher, because LodePNG uses unsigned chars for octets. 01475 01476 *) gcc and g++ 01477 01478 LodePNG is developed in gcc so this compiler is natively supported. It gives no 01479 warnings with compiler options "-Wall -Wextra -pedantic -ansi", with gcc and g++ 01480 version 4.7.1 on Linux, 32-bit and 64-bit. 01481 01482 *) Mingw 01483 01484 The Mingw compiler (a port of gcc) for Windows is fully supported by LodePNG. 01485 01486 *) Visual Studio 2005 and up, Visual C++ Express Edition 2005 and up 01487 01488 Visual Studio may give warnings about 'fopen' being deprecated. A multiplatform library 01489 can't support the proposed Visual Studio alternative however, so LodePNG keeps using 01490 fopen. If you don't want to see the deprecated warnings, put this on top of lodepng.h 01491 before the inclusions: 01492 #define _CRT_SECURE_NO_DEPRECATE 01493 01494 Other than the above warnings, LodePNG should be warning-free with warning 01495 level 3 (W3). Warning level 4 (W4) will give warnings about integer conversions. 01496 I'm not planning to resolve these warnings. To get rid of them, let Visual 01497 Studio use warning level W3 for lodepng.cpp only: right click lodepng.cpp, 01498 Properties, C/C++, General, Warning Level: Level 3 (/W3). 01499 01500 Visual Studio may want "stdafx.h" files to be included in each source file and 01501 give an error "unexpected end of file while looking for precompiled header". 01502 That is not standard C++ and will not be added to the stock LodePNG. You can 01503 disable it for lodepng.cpp only by right clicking it, Properties, C/C++, 01504 Precompiled Headers, and set it to Not Using Precompiled Headers there. 01505 01506 *) Visual Studio 6.0 01507 01508 LodePNG support for Visual Studio 6.0 is not guaranteed because VS6 doesn't 01509 follow the C++ standard correctly. 01510 01511 *) Comeau C/C++ 01512 01513 Vesion 20070107 compiles without problems on the Comeau C/C++ Online Test Drive 01514 at http://www.comeaucomputing.com/tryitout in both C90 and C++ mode. 01515 01516 *) Compilers on Macintosh 01517 01518 LodePNG has been reported to work both with the gcc and LLVM for Macintosh, both 01519 for C and C++. 01520 01521 *) Other Compilers 01522 01523 If you encounter problems on other compilers, feel free to let me know and I may 01524 try to fix it if the compiler is modern standards complient. 01525 01526 01527 10. examples 01528 ------------ 01529 01530 This decoder example shows the most basic usage of LodePNG. More complex 01531 examples can be found on the LodePNG website. 01532 01533 10.1. decoder C++ example 01534 ------------------------- 01535 01536 #include "lodepng.h" 01537 #include <iostream> 01538 01539 int main(int argc, char *argv[]) 01540 { 01541 const char* filename = argc > 1 ? argv[1] : "test.png"; 01542 01543 //load and decode 01544 std::vector<unsigned char> image; 01545 unsigned width, height; 01546 unsigned error = lodepng::decode(image, width, height, filename); 01547 01548 //if there's an error, display it 01549 if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; 01550 01551 //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ... 01552 } 01553 01554 10.2. decoder C example 01555 ----------------------- 01556 01557 #include "lodepng.h" 01558 01559 int main(int argc, char *argv[]) 01560 { 01561 unsigned error; 01562 unsigned char* image; 01563 size_t width, height; 01564 const char* filename = argc > 1 ? argv[1] : "test.png"; 01565 01566 error = lodepng_decode32_file(&image, &width, &height, filename); 01567 01568 if(error) printf("decoder error %u: %s\n", error, lodepng_error_text(error)); 01569 01570 / * use image here * / 01571 01572 free(image); 01573 return 0; 01574 } 01575 01576 01577 11. changes 01578 ----------- 01579 01580 The version number of LodePNG is the date of the change given in the format 01581 yyyymmdd. 01582 01583 Some changes aren't backwards compatible. Those are indicated with a (!) 01584 symbol. 01585 01586 *) 22 dec 2013: Power of two windowsize required for optimization. 01587 *) 15 apr 2013: Fixed bug with LAC_ALPHA and color key. 01588 *) 25 mar 2013: Added an optional feature to ignore some PNG errors (fix_png). 01589 *) 11 mar 2013 (!): Bugfix with custom free. Changed from "my" to "lodepng_" 01590 prefix for the custom allocators and made it possible with a new #define to 01591 use custom ones in your project without needing to change lodepng's code. 01592 *) 28 jan 2013: Bugfix with color key. 01593 *) 27 okt 2012: Tweaks in text chunk keyword length error handling. 01594 *) 8 okt 2012 (!): Added new filter strategy (entropy) and new auto color mode. 01595 (no palette). Better deflate tree encoding. New compression tweak settings. 01596 Faster color conversions while decoding. Some internal cleanups. 01597 *) 23 sep 2012: Reduced warnings in Visual Studio a little bit. 01598 *) 1 sep 2012 (!): Removed #define's for giving custom (de)compression functions 01599 and made it work with function pointers instead. 01600 *) 23 jun 2012: Added more filter strategies. Made it easier to use custom alloc 01601 and free functions and toggle #defines from compiler flags. Small fixes. 01602 *) 6 may 2012 (!): Made plugging in custom zlib/deflate functions more flexible. 01603 *) 22 apr 2012 (!): Made interface more consistent, renaming a lot. Removed 01604 redundant C++ codec classes. Reduced amount of structs. Everything changed, 01605 but it is cleaner now imho and functionality remains the same. Also fixed 01606 several bugs and shrinked the implementation code. Made new samples. 01607 *) 6 nov 2011 (!): By default, the encoder now automatically chooses the best 01608 PNG color model and bit depth, based on the amount and type of colors of the 01609 raw image. For this, autoLeaveOutAlphaChannel replaced by auto_choose_color. 01610 *) 9 okt 2011: simpler hash chain implementation for the encoder. 01611 *) 8 sep 2011: lz77 encoder lazy matching instead of greedy matching. 01612 *) 23 aug 2011: tweaked the zlib compression parameters after benchmarking. 01613 A bug with the PNG filtertype heuristic was fixed, so that it chooses much 01614 better ones (it's quite significant). A setting to do an experimental, slow, 01615 brute force search for PNG filter types is added. 01616 *) 17 aug 2011 (!): changed some C zlib related function names. 01617 *) 16 aug 2011: made the code less wide (max 120 characters per line). 01618 *) 17 apr 2011: code cleanup. Bugfixes. Convert low to 16-bit per sample colors. 01619 *) 21 feb 2011: fixed compiling for C90. Fixed compiling with sections disabled. 01620 *) 11 dec 2010: encoding is made faster, based on suggestion by Peter Eastman 01621 to optimize long sequences of zeros. 01622 *) 13 nov 2010: added LodePNG_InfoColor_hasPaletteAlpha and 01623 LodePNG_InfoColor_canHaveAlpha functions for convenience. 01624 *) 7 nov 2010: added LodePNG_error_text function to get error code description. 01625 *) 30 okt 2010: made decoding slightly faster 01626 *) 26 okt 2010: (!) changed some C function and struct names (more consistent). 01627 Reorganized the documentation and the declaration order in the header. 01628 *) 08 aug 2010: only changed some comments and external samples. 01629 *) 05 jul 2010: fixed bug thanks to warnings in the new gcc version. 01630 *) 14 mar 2010: fixed bug where too much memory was allocated for char buffers. 01631 *) 02 sep 2008: fixed bug where it could create empty tree that linux apps could 01632 read by ignoring the problem but windows apps couldn't. 01633 *) 06 jun 2008: added more error checks for out of memory cases. 01634 *) 26 apr 2008: added a few more checks here and there to ensure more safety. 01635 *) 06 mar 2008: crash with encoding of strings fixed 01636 *) 02 feb 2008: support for international text chunks added (iTXt) 01637 *) 23 jan 2008: small cleanups, and #defines to divide code in sections 01638 *) 20 jan 2008: support for unknown chunks allowing using LodePNG for an editor. 01639 *) 18 jan 2008: support for tIME and pHYs chunks added to encoder and decoder. 01640 *) 17 jan 2008: ability to encode and decode compressed zTXt chunks added 01641 Also vareous fixes, such as in the deflate and the padding bits code. 01642 *) 13 jan 2008: Added ability to encode Adam7-interlaced images. Improved 01643 filtering code of encoder. 01644 *) 07 jan 2008: (!) changed LodePNG to use ISO C90 instead of C++. A 01645 C++ wrapper around this provides an interface almost identical to before. 01646 Having LodePNG be pure ISO C90 makes it more portable. The C and C++ code 01647 are together in these files but it works both for C and C++ compilers. 01648 *) 29 dec 2007: (!) changed most integer types to unsigned int + other tweaks 01649 *) 30 aug 2007: bug fixed which makes this Borland C++ compatible 01650 *) 09 aug 2007: some VS2005 warnings removed again 01651 *) 21 jul 2007: deflate code placed in new namespace separate from zlib code 01652 *) 08 jun 2007: fixed bug with 2- and 4-bit color, and small interlaced images 01653 *) 04 jun 2007: improved support for Visual Studio 2005: crash with accessing 01654 invalid std::vector element [0] fixed, and level 3 and 4 warnings removed 01655 *) 02 jun 2007: made the encoder add a tag with version by default 01656 *) 27 may 2007: zlib and png code separated (but still in the same file), 01657 simple encoder/decoder functions added for more simple usage cases 01658 *) 19 may 2007: minor fixes, some code cleaning, new error added (error 69), 01659 moved some examples from here to lodepng_examples.cpp 01660 *) 12 may 2007: palette decoding bug fixed 01661 *) 24 apr 2007: changed the license from BSD to the zlib license 01662 *) 11 mar 2007: very simple addition: ability to encode bKGD chunks. 01663 *) 04 mar 2007: (!) tEXt chunk related fixes, and support for encoding 01664 palettized PNG images. Plus little interface change with palette and texts. 01665 *) 03 mar 2007: Made it encode dynamic Huffman shorter with repeat codes. 01666 Fixed a bug where the end code of a block had length 0 in the Huffman tree. 01667 *) 26 feb 2007: Huffman compression with dynamic trees (BTYPE 2) now implemented 01668 and supported by the encoder, resulting in smaller PNGs at the output. 01669 *) 27 jan 2007: Made the Adler-32 test faster so that a timewaste is gone. 01670 *) 24 jan 2007: gave encoder an error interface. Added color conversion from any 01671 greyscale type to 8-bit greyscale with or without alpha. 01672 *) 21 jan 2007: (!) Totally changed the interface. It allows more color types 01673 to convert to and is more uniform. See the manual for how it works now. 01674 *) 07 jan 2007: Some cleanup & fixes, and a few changes over the last days: 01675 encode/decode custom tEXt chunks, separate classes for zlib & deflate, and 01676 at last made the decoder give errors for incorrect Adler32 or Crc. 01677 *) 01 jan 2007: Fixed bug with encoding PNGs with less than 8 bits per channel. 01678 *) 29 dec 2006: Added support for encoding images without alpha channel, and 01679 cleaned out code as well as making certain parts faster. 01680 *) 28 dec 2006: Added "Settings" to the encoder. 01681 *) 26 dec 2006: The encoder now does LZ77 encoding and produces much smaller files now. 01682 Removed some code duplication in the decoder. Fixed little bug in an example. 01683 *) 09 dec 2006: (!) Placed output parameters of public functions as first parameter. 01684 Fixed a bug of the decoder with 16-bit per color. 01685 *) 15 okt 2006: Changed documentation structure 01686 *) 09 okt 2006: Encoder class added. It encodes a valid PNG image from the 01687 given image buffer, however for now it's not compressed. 01688 *) 08 sep 2006: (!) Changed to interface with a Decoder class 01689 *) 30 jul 2006: (!) LodePNG_InfoPng , width and height are now retrieved in different 01690 way. Renamed decodePNG to decodePNGGeneric. 01691 *) 29 jul 2006: (!) Changed the interface: image info is now returned as a 01692 struct of type LodePNG::LodePNG_Info, instead of a vector, which was a bit clumsy. 01693 *) 28 jul 2006: Cleaned the code and added new error checks. 01694 Corrected terminology "deflate" into "inflate". 01695 *) 23 jun 2006: Added SDL example in the documentation in the header, this 01696 example allows easy debugging by displaying the PNG and its transparency. 01697 *) 22 jun 2006: (!) Changed way to obtain error value. Added 01698 loadFile function for convenience. Made decodePNG32 faster. 01699 *) 21 jun 2006: (!) Changed type of info vector to unsigned. 01700 Changed position of palette in info vector. Fixed an important bug that 01701 happened on PNGs with an uncompressed block. 01702 *) 16 jun 2006: Internally changed unsigned into unsigned where 01703 needed, and performed some optimizations. 01704 *) 07 jun 2006: (!) Renamed functions to decodePNG and placed them 01705 in LodePNG namespace. Changed the order of the parameters. Rewrote the 01706 documentation in the header. Renamed files to lodepng.cpp and lodepng.h 01707 *) 22 apr 2006: Optimized and improved some code 01708 *) 07 sep 2005: (!) Changed to std::vector interface 01709 *) 12 aug 2005: Initial release (C++, decoder only) 01710 01711 01712 12. contact information 01713 ----------------------- 01714 01715 Feel free to contact me with suggestions, problems, comments, ... concerning 01716 LodePNG. If you encounter a PNG image that doesn't work properly with this 01717 decoder, feel free to send it and I'll use it to find and fix the problem. 01718 01719 My email address is (puzzle the account and domain together with an @ symbol): 01720 Domain: gmail dot com. 01721 Account: lode dot vandevenne. 01722 01723 01724 Copyright (c) 2005-2013 Lode Vandevenne 01725 */
Generated on Fri Jul 15 2022 14:51:32 by 1.7.2