Example for the LPC4088 QSB Base Board

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Wed Apr 09 10:26:13 2014 +0000
Revision:
3:33a36d79ab7d
Parent:
0:83b8ee8e8d4b
Updated to latest version of EALib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:83b8ee8e8d4b 1 /*
embeddedartists 0:83b8ee8e8d4b 2 LodePNG version 20131222
embeddedartists 0:83b8ee8e8d4b 3
embeddedartists 0:83b8ee8e8d4b 4 Copyright (c) 2005-2013 Lode Vandevenne
embeddedartists 0:83b8ee8e8d4b 5
embeddedartists 0:83b8ee8e8d4b 6 This software is provided 'as-is', without any express or implied
embeddedartists 0:83b8ee8e8d4b 7 warranty. In no event will the authors be held liable for any damages
embeddedartists 0:83b8ee8e8d4b 8 arising from the use of this software.
embeddedartists 0:83b8ee8e8d4b 9
embeddedartists 0:83b8ee8e8d4b 10 Permission is granted to anyone to use this software for any purpose,
embeddedartists 0:83b8ee8e8d4b 11 including commercial applications, and to alter it and redistribute it
embeddedartists 0:83b8ee8e8d4b 12 freely, subject to the following restrictions:
embeddedartists 0:83b8ee8e8d4b 13
embeddedartists 0:83b8ee8e8d4b 14 1. The origin of this software must not be misrepresented; you must not
embeddedartists 0:83b8ee8e8d4b 15 claim that you wrote the original software. If you use this software
embeddedartists 0:83b8ee8e8d4b 16 in a product, an acknowledgment in the product documentation would be
embeddedartists 0:83b8ee8e8d4b 17 appreciated but is not required.
embeddedartists 0:83b8ee8e8d4b 18
embeddedartists 0:83b8ee8e8d4b 19 2. Altered source versions must be plainly marked as such, and must not be
embeddedartists 0:83b8ee8e8d4b 20 misrepresented as being the original software.
embeddedartists 0:83b8ee8e8d4b 21
embeddedartists 0:83b8ee8e8d4b 22 3. This notice may not be removed or altered from any source
embeddedartists 0:83b8ee8e8d4b 23 distribution.
embeddedartists 0:83b8ee8e8d4b 24 */
embeddedartists 0:83b8ee8e8d4b 25
embeddedartists 0:83b8ee8e8d4b 26 #ifndef LODEPNG_H
embeddedartists 0:83b8ee8e8d4b 27 #define LODEPNG_H
embeddedartists 0:83b8ee8e8d4b 28
embeddedartists 0:83b8ee8e8d4b 29 #include <string.h> /*for size_t*/
embeddedartists 0:83b8ee8e8d4b 30
embeddedartists 0:83b8ee8e8d4b 31 #ifdef __cplusplus
embeddedartists 0:83b8ee8e8d4b 32 #include <vector>
embeddedartists 0:83b8ee8e8d4b 33 #include <string>
embeddedartists 0:83b8ee8e8d4b 34 #endif /*__cplusplus*/
embeddedartists 0:83b8ee8e8d4b 35
embeddedartists 0:83b8ee8e8d4b 36
embeddedartists 0:83b8ee8e8d4b 37 // Settings modified by Embedded Artists
embeddedartists 0:83b8ee8e8d4b 38 #define LODEPNG_NO_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 39 #define LODEPNG_NO_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 40 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 41 #define LODEPNG_NO_COMPILE_ERROR_TEXT
embeddedartists 0:83b8ee8e8d4b 42 #define LODEPNG_NO_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 43
embeddedartists 0:83b8ee8e8d4b 44
embeddedartists 0:83b8ee8e8d4b 45 /*
embeddedartists 0:83b8ee8e8d4b 46 The following #defines are used to create code sections. They can be disabled
embeddedartists 0:83b8ee8e8d4b 47 to disable code sections, which can give faster compile time and smaller binary.
embeddedartists 0:83b8ee8e8d4b 48 The "NO_COMPILE" defines are designed to be used to pass as defines to the
embeddedartists 0:83b8ee8e8d4b 49 compiler command to disable them without modifying this header, e.g.
embeddedartists 0:83b8ee8e8d4b 50 -DLODEPNG_NO_COMPILE_ZLIB for gcc.
embeddedartists 0:83b8ee8e8d4b 51 */
embeddedartists 0:83b8ee8e8d4b 52 /*deflate & zlib. If disabled, you must specify alternative zlib functions in
embeddedartists 0:83b8ee8e8d4b 53 the custom_zlib field of the compress and decompress settings*/
embeddedartists 0:83b8ee8e8d4b 54 #ifndef LODEPNG_NO_COMPILE_ZLIB
embeddedartists 0:83b8ee8e8d4b 55 #define LODEPNG_COMPILE_ZLIB
embeddedartists 0:83b8ee8e8d4b 56 #endif
embeddedartists 0:83b8ee8e8d4b 57 /*png encoder and png decoder*/
embeddedartists 0:83b8ee8e8d4b 58 #ifndef LODEPNG_NO_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 59 #define LODEPNG_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 60 #endif
embeddedartists 0:83b8ee8e8d4b 61 /*deflate&zlib decoder and png decoder*/
embeddedartists 0:83b8ee8e8d4b 62 #ifndef LODEPNG_NO_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 63 #define LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 64 #endif
embeddedartists 0:83b8ee8e8d4b 65 /*deflate&zlib encoder and png encoder*/
embeddedartists 0:83b8ee8e8d4b 66 #ifndef LODEPNG_NO_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 67 #define LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 68 #endif
embeddedartists 0:83b8ee8e8d4b 69 /*the optional built in harddisk file loading and saving functions*/
embeddedartists 0:83b8ee8e8d4b 70 #ifndef LODEPNG_NO_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 71 #define LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 72 #endif
embeddedartists 0:83b8ee8e8d4b 73 /*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/
embeddedartists 0:83b8ee8e8d4b 74 #ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 75 #define LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 76 #endif
embeddedartists 0:83b8ee8e8d4b 77 /*ability to convert error numerical codes to English text string*/
embeddedartists 0:83b8ee8e8d4b 78 #ifndef LODEPNG_NO_COMPILE_ERROR_TEXT
embeddedartists 0:83b8ee8e8d4b 79 #define LODEPNG_COMPILE_ERROR_TEXT
embeddedartists 0:83b8ee8e8d4b 80 #endif
embeddedartists 0:83b8ee8e8d4b 81 /*Compile the default allocators (C's free, malloc and realloc). If you disable this,
embeddedartists 0:83b8ee8e8d4b 82 you can define the functions lodepng_free, lodepng_malloc and lodepng_realloc in your
embeddedartists 0:83b8ee8e8d4b 83 source files with custom allocators.*/
embeddedartists 0:83b8ee8e8d4b 84 #ifndef LODEPNG_NO_COMPILE_ALLOCATORS
embeddedartists 0:83b8ee8e8d4b 85 #define LODEPNG_COMPILE_ALLOCATORS
embeddedartists 0:83b8ee8e8d4b 86 #endif
embeddedartists 0:83b8ee8e8d4b 87 /*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/
embeddedartists 0:83b8ee8e8d4b 88 #ifdef __cplusplus
embeddedartists 0:83b8ee8e8d4b 89 #ifndef LODEPNG_NO_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 90 #define LODEPNG_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 91 #endif
embeddedartists 0:83b8ee8e8d4b 92 #endif
embeddedartists 0:83b8ee8e8d4b 93
embeddedartists 0:83b8ee8e8d4b 94 #ifdef LODEPNG_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 95 /*The PNG color types (also used for raw).*/
embeddedartists 0:83b8ee8e8d4b 96 typedef enum LodePNGColorType
embeddedartists 0:83b8ee8e8d4b 97 {
embeddedartists 0:83b8ee8e8d4b 98 LCT_GREY = 0, /*greyscale: 1,2,4,8,16 bit*/
embeddedartists 0:83b8ee8e8d4b 99 LCT_RGB = 2, /*RGB: 8,16 bit*/
embeddedartists 0:83b8ee8e8d4b 100 LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/
embeddedartists 0:83b8ee8e8d4b 101 LCT_GREY_ALPHA = 4, /*greyscale with alpha: 8,16 bit*/
embeddedartists 0:83b8ee8e8d4b 102 LCT_RGBA = 6 /*RGB with alpha: 8,16 bit*/
embeddedartists 0:83b8ee8e8d4b 103 } LodePNGColorType;
embeddedartists 0:83b8ee8e8d4b 104
embeddedartists 0:83b8ee8e8d4b 105 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 106 /*
embeddedartists 0:83b8ee8e8d4b 107 Converts PNG data in memory to raw pixel data.
embeddedartists 0:83b8ee8e8d4b 108 out: Output parameter. Pointer to buffer that will contain the raw pixel data.
embeddedartists 0:83b8ee8e8d4b 109 After decoding, its size is w * h * (bytes per pixel) bytes larger than
embeddedartists 0:83b8ee8e8d4b 110 initially. Bytes per pixel depends on colortype and bitdepth.
embeddedartists 0:83b8ee8e8d4b 111 Must be freed after usage with free(*out).
embeddedartists 0:83b8ee8e8d4b 112 Note: for 16-bit per channel colors, uses big endian format like PNG does.
embeddedartists 0:83b8ee8e8d4b 113 w: Output parameter. Pointer to width of pixel data.
embeddedartists 0:83b8ee8e8d4b 114 h: Output parameter. Pointer to height of pixel data.
embeddedartists 0:83b8ee8e8d4b 115 in: Memory buffer with the PNG file.
embeddedartists 0:83b8ee8e8d4b 116 insize: size of the in buffer.
embeddedartists 0:83b8ee8e8d4b 117 colortype: the desired color type for the raw output image. See explanation on PNG color types.
embeddedartists 0:83b8ee8e8d4b 118 bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types.
embeddedartists 0:83b8ee8e8d4b 119 Return value: LodePNG error code (0 means no error).
embeddedartists 0:83b8ee8e8d4b 120 */
embeddedartists 0:83b8ee8e8d4b 121 unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 122 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 123 LodePNGColorType colortype, unsigned bitdepth);
embeddedartists 0:83b8ee8e8d4b 124
embeddedartists 0:83b8ee8e8d4b 125 /*Same as lodepng_decode_memory, but always decodes to 32-bit RGBA raw image*/
embeddedartists 0:83b8ee8e8d4b 126 unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 127 const unsigned char* in, size_t insize);
embeddedartists 0:83b8ee8e8d4b 128
embeddedartists 0:83b8ee8e8d4b 129 /*Same as lodepng_decode_memory, but always decodes to 24-bit RGB raw image*/
embeddedartists 0:83b8ee8e8d4b 130 unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 131 const unsigned char* in, size_t insize);
embeddedartists 0:83b8ee8e8d4b 132
embeddedartists 0:83b8ee8e8d4b 133 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 134 /*
embeddedartists 0:83b8ee8e8d4b 135 Load PNG from disk, from file with given name.
embeddedartists 0:83b8ee8e8d4b 136 Same as the other decode functions, but instead takes a filename as input.
embeddedartists 0:83b8ee8e8d4b 137 */
embeddedartists 0:83b8ee8e8d4b 138 unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 139 const char* filename,
embeddedartists 0:83b8ee8e8d4b 140 LodePNGColorType colortype, unsigned bitdepth);
embeddedartists 0:83b8ee8e8d4b 141
embeddedartists 0:83b8ee8e8d4b 142 /*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image.*/
embeddedartists 0:83b8ee8e8d4b 143 unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 144 const char* filename);
embeddedartists 0:83b8ee8e8d4b 145
embeddedartists 0:83b8ee8e8d4b 146 /*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image.*/
embeddedartists 0:83b8ee8e8d4b 147 unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 148 const char* filename);
embeddedartists 0:83b8ee8e8d4b 149 #endif /*LODEPNG_COMPILE_DISK*/
embeddedartists 0:83b8ee8e8d4b 150 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 151
embeddedartists 0:83b8ee8e8d4b 152
embeddedartists 0:83b8ee8e8d4b 153 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 154 /*
embeddedartists 0:83b8ee8e8d4b 155 Converts raw pixel data into a PNG image in memory. The colortype and bitdepth
embeddedartists 0:83b8ee8e8d4b 156 of the output PNG image cannot be chosen, they are automatically determined
embeddedartists 0:83b8ee8e8d4b 157 by the colortype, bitdepth and content of the input pixel data.
embeddedartists 0:83b8ee8e8d4b 158 Note: for 16-bit per channel colors, needs big endian format like PNG does.
embeddedartists 0:83b8ee8e8d4b 159 out: Output parameter. Pointer to buffer that will contain the PNG image data.
embeddedartists 0:83b8ee8e8d4b 160 Must be freed after usage with free(*out).
embeddedartists 0:83b8ee8e8d4b 161 outsize: Output parameter. Pointer to the size in bytes of the out buffer.
embeddedartists 0:83b8ee8e8d4b 162 image: The raw pixel data to encode. The size of this buffer should be
embeddedartists 0:83b8ee8e8d4b 163 w * h * (bytes per pixel), bytes per pixel depends on colortype and bitdepth.
embeddedartists 0:83b8ee8e8d4b 164 w: width of the raw pixel data in pixels.
embeddedartists 0:83b8ee8e8d4b 165 h: height of the raw pixel data in pixels.
embeddedartists 0:83b8ee8e8d4b 166 colortype: the color type of the raw input image. See explanation on PNG color types.
embeddedartists 0:83b8ee8e8d4b 167 bitdepth: the bit depth of the raw input image. See explanation on PNG color types.
embeddedartists 0:83b8ee8e8d4b 168 Return value: LodePNG error code (0 means no error).
embeddedartists 0:83b8ee8e8d4b 169 */
embeddedartists 0:83b8ee8e8d4b 170 unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 171 const unsigned char* image, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 172 LodePNGColorType colortype, unsigned bitdepth);
embeddedartists 0:83b8ee8e8d4b 173
embeddedartists 0:83b8ee8e8d4b 174 /*Same as lodepng_encode_memory, but always encodes from 32-bit RGBA raw image.*/
embeddedartists 0:83b8ee8e8d4b 175 unsigned lodepng_encode32(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 176 const unsigned char* image, unsigned w, unsigned h);
embeddedartists 0:83b8ee8e8d4b 177
embeddedartists 0:83b8ee8e8d4b 178 /*Same as lodepng_encode_memory, but always encodes from 24-bit RGB raw image.*/
embeddedartists 0:83b8ee8e8d4b 179 unsigned lodepng_encode24(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 180 const unsigned char* image, unsigned w, unsigned h);
embeddedartists 0:83b8ee8e8d4b 181
embeddedartists 0:83b8ee8e8d4b 182 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 183 /*
embeddedartists 0:83b8ee8e8d4b 184 Converts raw pixel data into a PNG file on disk.
embeddedartists 0:83b8ee8e8d4b 185 Same as the other encode functions, but instead takes a filename as output.
embeddedartists 0:83b8ee8e8d4b 186 NOTE: This overwrites existing files without warning!
embeddedartists 0:83b8ee8e8d4b 187 */
embeddedartists 0:83b8ee8e8d4b 188 unsigned lodepng_encode_file(const char* filename,
embeddedartists 0:83b8ee8e8d4b 189 const unsigned char* image, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 190 LodePNGColorType colortype, unsigned bitdepth);
embeddedartists 0:83b8ee8e8d4b 191
embeddedartists 0:83b8ee8e8d4b 192 /*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/
embeddedartists 0:83b8ee8e8d4b 193 unsigned lodepng_encode32_file(const char* filename,
embeddedartists 0:83b8ee8e8d4b 194 const unsigned char* image, unsigned w, unsigned h);
embeddedartists 0:83b8ee8e8d4b 195
embeddedartists 0:83b8ee8e8d4b 196 /*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image.*/
embeddedartists 0:83b8ee8e8d4b 197 unsigned lodepng_encode24_file(const char* filename,
embeddedartists 0:83b8ee8e8d4b 198 const unsigned char* image, unsigned w, unsigned h);
embeddedartists 0:83b8ee8e8d4b 199 #endif /*LODEPNG_COMPILE_DISK*/
embeddedartists 0:83b8ee8e8d4b 200 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 201
embeddedartists 0:83b8ee8e8d4b 202
embeddedartists 0:83b8ee8e8d4b 203 #ifdef LODEPNG_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 204 namespace lodepng
embeddedartists 0:83b8ee8e8d4b 205 {
embeddedartists 0:83b8ee8e8d4b 206 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 207 /*Same as lodepng_decode_memory, but decodes to an std::vector.*/
embeddedartists 0:83b8ee8e8d4b 208 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
embeddedartists 0:83b8ee8e8d4b 209 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 210 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 211 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
embeddedartists 0:83b8ee8e8d4b 212 const std::vector<unsigned char>& in,
embeddedartists 0:83b8ee8e8d4b 213 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 214 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 215 /*
embeddedartists 0:83b8ee8e8d4b 216 Converts PNG file from disk to raw pixel data in memory.
embeddedartists 0:83b8ee8e8d4b 217 Same as the other decode functions, but instead takes a filename as input.
embeddedartists 0:83b8ee8e8d4b 218 */
embeddedartists 0:83b8ee8e8d4b 219 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
embeddedartists 0:83b8ee8e8d4b 220 const std::string& filename,
embeddedartists 0:83b8ee8e8d4b 221 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 222 #endif //LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 223 #endif //LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 224
embeddedartists 0:83b8ee8e8d4b 225 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 226 /*Same as lodepng_encode_memory, but encodes to an std::vector.*/
embeddedartists 0:83b8ee8e8d4b 227 unsigned encode(std::vector<unsigned char>& out,
embeddedartists 0:83b8ee8e8d4b 228 const unsigned char* in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 229 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 230 unsigned encode(std::vector<unsigned char>& out,
embeddedartists 0:83b8ee8e8d4b 231 const std::vector<unsigned char>& in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 232 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 233 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 234 /*
embeddedartists 0:83b8ee8e8d4b 235 Converts 32-bit RGBA raw pixel data into a PNG file on disk.
embeddedartists 0:83b8ee8e8d4b 236 Same as the other encode functions, but instead takes a filename as output.
embeddedartists 0:83b8ee8e8d4b 237 NOTE: This overwrites existing files without warning!
embeddedartists 0:83b8ee8e8d4b 238 */
embeddedartists 0:83b8ee8e8d4b 239 unsigned encode(const std::string& filename,
embeddedartists 0:83b8ee8e8d4b 240 const unsigned char* in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 241 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 242 unsigned encode(const std::string& filename,
embeddedartists 0:83b8ee8e8d4b 243 const std::vector<unsigned char>& in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 244 LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8);
embeddedartists 0:83b8ee8e8d4b 245 #endif //LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 246 #endif //LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 247 } //namespace lodepng
embeddedartists 0:83b8ee8e8d4b 248 #endif /*LODEPNG_COMPILE_CPP*/
embeddedartists 0:83b8ee8e8d4b 249 #endif /*LODEPNG_COMPILE_PNG*/
embeddedartists 0:83b8ee8e8d4b 250
embeddedartists 0:83b8ee8e8d4b 251 #ifdef LODEPNG_COMPILE_ERROR_TEXT
embeddedartists 0:83b8ee8e8d4b 252 /*Returns an English description of the numerical error code.*/
embeddedartists 0:83b8ee8e8d4b 253 const char* lodepng_error_text(unsigned code);
embeddedartists 0:83b8ee8e8d4b 254 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/
embeddedartists 0:83b8ee8e8d4b 255
embeddedartists 0:83b8ee8e8d4b 256 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 257 /*Settings for zlib decompression*/
embeddedartists 0:83b8ee8e8d4b 258 typedef struct LodePNGDecompressSettings LodePNGDecompressSettings;
embeddedartists 0:83b8ee8e8d4b 259 struct LodePNGDecompressSettings
embeddedartists 0:83b8ee8e8d4b 260 {
embeddedartists 0:83b8ee8e8d4b 261 unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/
embeddedartists 0:83b8ee8e8d4b 262
embeddedartists 0:83b8ee8e8d4b 263 /*use custom zlib decoder instead of built in one (default: null)*/
embeddedartists 0:83b8ee8e8d4b 264 unsigned (*custom_zlib)(unsigned char**, size_t*,
embeddedartists 0:83b8ee8e8d4b 265 const unsigned char*, size_t,
embeddedartists 0:83b8ee8e8d4b 266 const LodePNGDecompressSettings*);
embeddedartists 0:83b8ee8e8d4b 267 /*use custom deflate decoder instead of built in one (default: null)
embeddedartists 0:83b8ee8e8d4b 268 if custom_zlib is used, custom_deflate is ignored since only the built in
embeddedartists 0:83b8ee8e8d4b 269 zlib function will call custom_deflate*/
embeddedartists 0:83b8ee8e8d4b 270 unsigned (*custom_inflate)(unsigned char**, size_t*,
embeddedartists 0:83b8ee8e8d4b 271 const unsigned char*, size_t,
embeddedartists 0:83b8ee8e8d4b 272 const LodePNGDecompressSettings*);
embeddedartists 0:83b8ee8e8d4b 273
embeddedartists 0:83b8ee8e8d4b 274 const void* custom_context; /*optional custom settings for custom functions*/
embeddedartists 0:83b8ee8e8d4b 275 };
embeddedartists 0:83b8ee8e8d4b 276
embeddedartists 0:83b8ee8e8d4b 277 extern const LodePNGDecompressSettings lodepng_default_decompress_settings;
embeddedartists 0:83b8ee8e8d4b 278 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 279 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 280
embeddedartists 0:83b8ee8e8d4b 281 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 282 /*
embeddedartists 0:83b8ee8e8d4b 283 Settings for zlib compression. Tweaking these settings tweaks the balance
embeddedartists 0:83b8ee8e8d4b 284 between speed and compression ratio.
embeddedartists 0:83b8ee8e8d4b 285 */
embeddedartists 0:83b8ee8e8d4b 286 typedef struct LodePNGCompressSettings LodePNGCompressSettings;
embeddedartists 0:83b8ee8e8d4b 287 struct LodePNGCompressSettings /*deflate = compress*/
embeddedartists 0:83b8ee8e8d4b 288 {
embeddedartists 0:83b8ee8e8d4b 289 /*LZ77 related settings*/
embeddedartists 0:83b8ee8e8d4b 290 unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/
embeddedartists 0:83b8ee8e8d4b 291 unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/
embeddedartists 0:83b8ee8e8d4b 292 unsigned windowsize; /*must be a power of two <= 32768. higher compresses more but is slower. Typical value: 2048.*/
embeddedartists 0:83b8ee8e8d4b 293 unsigned minmatch; /*mininum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/
embeddedartists 0:83b8ee8e8d4b 294 unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/
embeddedartists 0:83b8ee8e8d4b 295 unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/
embeddedartists 0:83b8ee8e8d4b 296
embeddedartists 0:83b8ee8e8d4b 297 /*use custom zlib encoder instead of built in one (default: null)*/
embeddedartists 0:83b8ee8e8d4b 298 unsigned (*custom_zlib)(unsigned char**, size_t*,
embeddedartists 0:83b8ee8e8d4b 299 const unsigned char*, size_t,
embeddedartists 0:83b8ee8e8d4b 300 const LodePNGCompressSettings*);
embeddedartists 0:83b8ee8e8d4b 301 /*use custom deflate encoder instead of built in one (default: null)
embeddedartists 0:83b8ee8e8d4b 302 if custom_zlib is used, custom_deflate is ignored since only the built in
embeddedartists 0:83b8ee8e8d4b 303 zlib function will call custom_deflate*/
embeddedartists 0:83b8ee8e8d4b 304 unsigned (*custom_deflate)(unsigned char**, size_t*,
embeddedartists 0:83b8ee8e8d4b 305 const unsigned char*, size_t,
embeddedartists 0:83b8ee8e8d4b 306 const LodePNGCompressSettings*);
embeddedartists 0:83b8ee8e8d4b 307
embeddedartists 0:83b8ee8e8d4b 308 const void* custom_context; /*optional custom settings for custom functions*/
embeddedartists 0:83b8ee8e8d4b 309 };
embeddedartists 0:83b8ee8e8d4b 310
embeddedartists 0:83b8ee8e8d4b 311 extern const LodePNGCompressSettings lodepng_default_compress_settings;
embeddedartists 0:83b8ee8e8d4b 312 void lodepng_compress_settings_init(LodePNGCompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 313 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 314
embeddedartists 0:83b8ee8e8d4b 315 #ifdef LODEPNG_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 316 /*
embeddedartists 0:83b8ee8e8d4b 317 Color mode of an image. Contains all information required to decode the pixel
embeddedartists 0:83b8ee8e8d4b 318 bits to RGBA colors. This information is the same as used in the PNG file
embeddedartists 0:83b8ee8e8d4b 319 format, and is used both for PNG and raw image data in LodePNG.
embeddedartists 0:83b8ee8e8d4b 320 */
embeddedartists 0:83b8ee8e8d4b 321 typedef struct LodePNGColorMode
embeddedartists 0:83b8ee8e8d4b 322 {
embeddedartists 0:83b8ee8e8d4b 323 /*header (IHDR)*/
embeddedartists 0:83b8ee8e8d4b 324 LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/
embeddedartists 0:83b8ee8e8d4b 325 unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/
embeddedartists 0:83b8ee8e8d4b 326
embeddedartists 0:83b8ee8e8d4b 327 /*
embeddedartists 0:83b8ee8e8d4b 328 palette (PLTE and tRNS)
embeddedartists 0:83b8ee8e8d4b 329
embeddedartists 0:83b8ee8e8d4b 330 Dynamically allocated with the colors of the palette, including alpha.
embeddedartists 0:83b8ee8e8d4b 331 When encoding a PNG, to store your colors in the palette of the LodePNGColorMode, first use
embeddedartists 0:83b8ee8e8d4b 332 lodepng_palette_clear, then for each color use lodepng_palette_add.
embeddedartists 0:83b8ee8e8d4b 333 If you encode an image without alpha with palette, don't forget to put value 255 in each A byte of the palette.
embeddedartists 0:83b8ee8e8d4b 334
embeddedartists 0:83b8ee8e8d4b 335 When decoding, by default you can ignore this palette, since LodePNG already
embeddedartists 0:83b8ee8e8d4b 336 fills the palette colors in the pixels of the raw RGBA output.
embeddedartists 0:83b8ee8e8d4b 337
embeddedartists 0:83b8ee8e8d4b 338 The palette is only supported for color type 3.
embeddedartists 0:83b8ee8e8d4b 339 */
embeddedartists 0:83b8ee8e8d4b 340 unsigned char* palette; /*palette in RGBARGBA... order. When allocated, must be either 0, or have size 1024*/
embeddedartists 0:83b8ee8e8d4b 341 size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/
embeddedartists 0:83b8ee8e8d4b 342
embeddedartists 0:83b8ee8e8d4b 343 /*
embeddedartists 0:83b8ee8e8d4b 344 transparent color key (tRNS)
embeddedartists 0:83b8ee8e8d4b 345
embeddedartists 0:83b8ee8e8d4b 346 This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit.
embeddedartists 0:83b8ee8e8d4b 347 For greyscale PNGs, r, g and b will all 3 be set to the same.
embeddedartists 0:83b8ee8e8d4b 348
embeddedartists 0:83b8ee8e8d4b 349 When decoding, by default you can ignore this information, since LodePNG sets
embeddedartists 0:83b8ee8e8d4b 350 pixels with this key to transparent already in the raw RGBA output.
embeddedartists 0:83b8ee8e8d4b 351
embeddedartists 0:83b8ee8e8d4b 352 The color key is only supported for color types 0 and 2.
embeddedartists 0:83b8ee8e8d4b 353 */
embeddedartists 0:83b8ee8e8d4b 354 unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/
embeddedartists 0:83b8ee8e8d4b 355 unsigned key_r; /*red/greyscale component of color key*/
embeddedartists 0:83b8ee8e8d4b 356 unsigned key_g; /*green component of color key*/
embeddedartists 0:83b8ee8e8d4b 357 unsigned key_b; /*blue component of color key*/
embeddedartists 0:83b8ee8e8d4b 358 } LodePNGColorMode;
embeddedartists 0:83b8ee8e8d4b 359
embeddedartists 0:83b8ee8e8d4b 360 /*init, cleanup and copy functions to use with this struct*/
embeddedartists 0:83b8ee8e8d4b 361 void lodepng_color_mode_init(LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 362 void lodepng_color_mode_cleanup(LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 363 /*return value is error code (0 means no error)*/
embeddedartists 0:83b8ee8e8d4b 364 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source);
embeddedartists 0:83b8ee8e8d4b 365
embeddedartists 0:83b8ee8e8d4b 366 void lodepng_palette_clear(LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 367 /*add 1 color to the palette*/
embeddedartists 0:83b8ee8e8d4b 368 unsigned lodepng_palette_add(LodePNGColorMode* info,
embeddedartists 0:83b8ee8e8d4b 369 unsigned char r, unsigned char g, unsigned char b, unsigned char a);
embeddedartists 0:83b8ee8e8d4b 370
embeddedartists 0:83b8ee8e8d4b 371 /*get the total amount of bits per pixel, based on colortype and bitdepth in the struct*/
embeddedartists 0:83b8ee8e8d4b 372 unsigned lodepng_get_bpp(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 373 /*get the amount of color channels used, based on colortype in the struct.
embeddedartists 0:83b8ee8e8d4b 374 If a palette is used, it counts as 1 channel.*/
embeddedartists 0:83b8ee8e8d4b 375 unsigned lodepng_get_channels(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 376 /*is it a greyscale type? (only colortype 0 or 4)*/
embeddedartists 0:83b8ee8e8d4b 377 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 378 /*has it got an alpha channel? (only colortype 2 or 6)*/
embeddedartists 0:83b8ee8e8d4b 379 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 380 /*has it got a palette? (only colortype 3)*/
embeddedartists 0:83b8ee8e8d4b 381 unsigned lodepng_is_palette_type(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 382 /*only returns true if there is a palette and there is a value in the palette with alpha < 255.
embeddedartists 0:83b8ee8e8d4b 383 Loops through the palette to check this.*/
embeddedartists 0:83b8ee8e8d4b 384 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 385 /*
embeddedartists 0:83b8ee8e8d4b 386 Check if the given color info indicates the possibility of having non-opaque pixels in the PNG image.
embeddedartists 0:83b8ee8e8d4b 387 Returns true if the image can have translucent or invisible pixels (it still be opaque if it doesn't use such pixels).
embeddedartists 0:83b8ee8e8d4b 388 Returns false if the image can only have opaque pixels.
embeddedartists 0:83b8ee8e8d4b 389 In detail, it returns true only if it's a color type with alpha, or has a palette with non-opaque values,
embeddedartists 0:83b8ee8e8d4b 390 or if "key_defined" is true.
embeddedartists 0:83b8ee8e8d4b 391 */
embeddedartists 0:83b8ee8e8d4b 392 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info);
embeddedartists 0:83b8ee8e8d4b 393 /*Returns the byte size of a raw image buffer with given width, height and color mode*/
embeddedartists 0:83b8ee8e8d4b 394 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color);
embeddedartists 0:83b8ee8e8d4b 395
embeddedartists 0:83b8ee8e8d4b 396 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 397 /*The information of a Time chunk in PNG.*/
embeddedartists 0:83b8ee8e8d4b 398 typedef struct LodePNGTime
embeddedartists 0:83b8ee8e8d4b 399 {
embeddedartists 0:83b8ee8e8d4b 400 unsigned year; /*2 bytes used (0-65535)*/
embeddedartists 0:83b8ee8e8d4b 401 unsigned month; /*1-12*/
embeddedartists 0:83b8ee8e8d4b 402 unsigned day; /*1-31*/
embeddedartists 0:83b8ee8e8d4b 403 unsigned hour; /*0-23*/
embeddedartists 0:83b8ee8e8d4b 404 unsigned minute; /*0-59*/
embeddedartists 0:83b8ee8e8d4b 405 unsigned second; /*0-60 (to allow for leap seconds)*/
embeddedartists 0:83b8ee8e8d4b 406 } LodePNGTime;
embeddedartists 0:83b8ee8e8d4b 407 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
embeddedartists 0:83b8ee8e8d4b 408
embeddedartists 0:83b8ee8e8d4b 409 /*Information about the PNG image, except pixels, width and height.*/
embeddedartists 0:83b8ee8e8d4b 410 typedef struct LodePNGInfo
embeddedartists 0:83b8ee8e8d4b 411 {
embeddedartists 0:83b8ee8e8d4b 412 /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/
embeddedartists 0:83b8ee8e8d4b 413 unsigned compression_method;/*compression method of the original file. Always 0.*/
embeddedartists 0:83b8ee8e8d4b 414 unsigned filter_method; /*filter method of the original file*/
embeddedartists 0:83b8ee8e8d4b 415 unsigned interlace_method; /*interlace method of the original file*/
embeddedartists 0:83b8ee8e8d4b 416 LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/
embeddedartists 0:83b8ee8e8d4b 417
embeddedartists 0:83b8ee8e8d4b 418 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 419 /*
embeddedartists 0:83b8ee8e8d4b 420 suggested background color chunk (bKGD)
embeddedartists 0:83b8ee8e8d4b 421 This color uses the same color mode as the PNG (except alpha channel), which can be 1-bit to 16-bit.
embeddedartists 0:83b8ee8e8d4b 422
embeddedartists 0:83b8ee8e8d4b 423 For greyscale PNGs, r, g and b will all 3 be set to the same. When encoding
embeddedartists 0:83b8ee8e8d4b 424 the encoder writes the red one. For palette PNGs: When decoding, the RGB value
embeddedartists 0:83b8ee8e8d4b 425 will be stored, not a palette index. But when encoding, specify the index of
embeddedartists 0:83b8ee8e8d4b 426 the palette in background_r, the other two are then ignored.
embeddedartists 0:83b8ee8e8d4b 427
embeddedartists 0:83b8ee8e8d4b 428 The decoder does not use this background color to edit the color of pixels.
embeddedartists 0:83b8ee8e8d4b 429 */
embeddedartists 0:83b8ee8e8d4b 430 unsigned background_defined; /*is a suggested background color given?*/
embeddedartists 0:83b8ee8e8d4b 431 unsigned background_r; /*red component of suggested background color*/
embeddedartists 0:83b8ee8e8d4b 432 unsigned background_g; /*green component of suggested background color*/
embeddedartists 0:83b8ee8e8d4b 433 unsigned background_b; /*blue component of suggested background color*/
embeddedartists 0:83b8ee8e8d4b 434
embeddedartists 0:83b8ee8e8d4b 435 /*
embeddedartists 0:83b8ee8e8d4b 436 non-international text chunks (tEXt and zTXt)
embeddedartists 0:83b8ee8e8d4b 437
embeddedartists 0:83b8ee8e8d4b 438 The char** arrays each contain num strings. The actual messages are in
embeddedartists 0:83b8ee8e8d4b 439 text_strings, while text_keys are keywords that give a short description what
embeddedartists 0:83b8ee8e8d4b 440 the actual text represents, e.g. Title, Author, Description, or anything else.
embeddedartists 0:83b8ee8e8d4b 441
embeddedartists 0:83b8ee8e8d4b 442 A keyword is minimum 1 character and maximum 79 characters long. It's
embeddedartists 0:83b8ee8e8d4b 443 discouraged to use a single line length longer than 79 characters for texts.
embeddedartists 0:83b8ee8e8d4b 444
embeddedartists 0:83b8ee8e8d4b 445 Don't allocate these text buffers yourself. Use the init/cleanup functions
embeddedartists 0:83b8ee8e8d4b 446 correctly and use lodepng_add_text and lodepng_clear_text.
embeddedartists 0:83b8ee8e8d4b 447 */
embeddedartists 0:83b8ee8e8d4b 448 size_t text_num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/
embeddedartists 0:83b8ee8e8d4b 449 char** text_keys; /*the keyword of a text chunk (e.g. "Comment")*/
embeddedartists 0:83b8ee8e8d4b 450 char** text_strings; /*the actual text*/
embeddedartists 0:83b8ee8e8d4b 451
embeddedartists 0:83b8ee8e8d4b 452 /*
embeddedartists 0:83b8ee8e8d4b 453 international text chunks (iTXt)
embeddedartists 0:83b8ee8e8d4b 454 Similar to the non-international text chunks, but with additional strings
embeddedartists 0:83b8ee8e8d4b 455 "langtags" and "transkeys".
embeddedartists 0:83b8ee8e8d4b 456 */
embeddedartists 0:83b8ee8e8d4b 457 size_t itext_num; /*the amount of international texts in this PNG*/
embeddedartists 0:83b8ee8e8d4b 458 char** itext_keys; /*the English keyword of the text chunk (e.g. "Comment")*/
embeddedartists 0:83b8ee8e8d4b 459 char** itext_langtags; /*language tag for this text's language, ISO/IEC 646 string, e.g. ISO 639 language tag*/
embeddedartists 0:83b8ee8e8d4b 460 char** itext_transkeys; /*keyword translated to the international language - UTF-8 string*/
embeddedartists 0:83b8ee8e8d4b 461 char** itext_strings; /*the actual international text - UTF-8 string*/
embeddedartists 0:83b8ee8e8d4b 462
embeddedartists 0:83b8ee8e8d4b 463 /*time chunk (tIME)*/
embeddedartists 0:83b8ee8e8d4b 464 unsigned time_defined; /*set to 1 to make the encoder generate a tIME chunk*/
embeddedartists 0:83b8ee8e8d4b 465 LodePNGTime time;
embeddedartists 0:83b8ee8e8d4b 466
embeddedartists 0:83b8ee8e8d4b 467 /*phys chunk (pHYs)*/
embeddedartists 0:83b8ee8e8d4b 468 unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/
embeddedartists 0:83b8ee8e8d4b 469 unsigned phys_x; /*pixels per unit in x direction*/
embeddedartists 0:83b8ee8e8d4b 470 unsigned phys_y; /*pixels per unit in y direction*/
embeddedartists 0:83b8ee8e8d4b 471 unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/
embeddedartists 0:83b8ee8e8d4b 472
embeddedartists 0:83b8ee8e8d4b 473 /*
embeddedartists 0:83b8ee8e8d4b 474 unknown chunks
embeddedartists 0:83b8ee8e8d4b 475 There are 3 buffers, one for each position in the PNG where unknown chunks can appear
embeddedartists 0:83b8ee8e8d4b 476 each buffer contains all unknown chunks for that position consecutively
embeddedartists 0:83b8ee8e8d4b 477 The 3 buffers are the unknown chunks between certain critical chunks:
embeddedartists 0:83b8ee8e8d4b 478 0: IHDR-PLTE, 1: PLTE-IDAT, 2: IDAT-IEND
embeddedartists 0:83b8ee8e8d4b 479 Do not allocate or traverse this data yourself. Use the chunk traversing functions declared
embeddedartists 0:83b8ee8e8d4b 480 later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct.
embeddedartists 0:83b8ee8e8d4b 481 */
embeddedartists 0:83b8ee8e8d4b 482 unsigned char* unknown_chunks_data[3];
embeddedartists 0:83b8ee8e8d4b 483 size_t unknown_chunks_size[3]; /*size in bytes of the unknown chunks, given for protection*/
embeddedartists 0:83b8ee8e8d4b 484 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
embeddedartists 0:83b8ee8e8d4b 485 } LodePNGInfo;
embeddedartists 0:83b8ee8e8d4b 486
embeddedartists 0:83b8ee8e8d4b 487 /*init, cleanup and copy functions to use with this struct*/
embeddedartists 0:83b8ee8e8d4b 488 void lodepng_info_init(LodePNGInfo* info);
embeddedartists 0:83b8ee8e8d4b 489 void lodepng_info_cleanup(LodePNGInfo* info);
embeddedartists 0:83b8ee8e8d4b 490 /*return value is error code (0 means no error)*/
embeddedartists 0:83b8ee8e8d4b 491 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source);
embeddedartists 0:83b8ee8e8d4b 492
embeddedartists 0:83b8ee8e8d4b 493 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 494 void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/
embeddedartists 0:83b8ee8e8d4b 495 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str); /*push back both texts at once*/
embeddedartists 0:83b8ee8e8d4b 496
embeddedartists 0:83b8ee8e8d4b 497 void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/
embeddedartists 0:83b8ee8e8d4b 498 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag,
embeddedartists 0:83b8ee8e8d4b 499 const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/
embeddedartists 0:83b8ee8e8d4b 500 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
embeddedartists 0:83b8ee8e8d4b 501
embeddedartists 0:83b8ee8e8d4b 502 /*
embeddedartists 0:83b8ee8e8d4b 503 Converts raw buffer from one color type to another color type, based on
embeddedartists 0:83b8ee8e8d4b 504 LodePNGColorMode structs to describe the input and output color type.
embeddedartists 0:83b8ee8e8d4b 505 See the reference manual at the end of this header file to see which color conversions are supported.
embeddedartists 0:83b8ee8e8d4b 506 return value = LodePNG error code (0 if all went ok, an error if the conversion isn't supported)
embeddedartists 0:83b8ee8e8d4b 507 The out buffer must have size (w * h * bpp + 7) / 8, where bpp is the bits per pixel
embeddedartists 0:83b8ee8e8d4b 508 of the output color type (lodepng_get_bpp)
embeddedartists 0:83b8ee8e8d4b 509 The fix_png value works as described in struct LodePNGDecoderSettings.
embeddedartists 0:83b8ee8e8d4b 510 Note: for 16-bit per channel colors, uses big endian format like PNG does.
embeddedartists 0:83b8ee8e8d4b 511 */
embeddedartists 0:83b8ee8e8d4b 512 unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
embeddedartists 0:83b8ee8e8d4b 513 LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in,
embeddedartists 0:83b8ee8e8d4b 514 unsigned w, unsigned h, unsigned fix_png);
embeddedartists 0:83b8ee8e8d4b 515
embeddedartists 0:83b8ee8e8d4b 516 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 517 /*
embeddedartists 0:83b8ee8e8d4b 518 Settings for the decoder. This contains settings for the PNG and the Zlib
embeddedartists 0:83b8ee8e8d4b 519 decoder, but not the Info settings from the Info structs.
embeddedartists 0:83b8ee8e8d4b 520 */
embeddedartists 0:83b8ee8e8d4b 521 typedef struct LodePNGDecoderSettings
embeddedartists 0:83b8ee8e8d4b 522 {
embeddedartists 0:83b8ee8e8d4b 523 LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/
embeddedartists 0:83b8ee8e8d4b 524
embeddedartists 0:83b8ee8e8d4b 525 unsigned ignore_crc; /*ignore CRC checksums*/
embeddedartists 0:83b8ee8e8d4b 526 /*
embeddedartists 0:83b8ee8e8d4b 527 The fix_png setting, if 1, makes the decoder tolerant towards some PNG images
embeddedartists 0:83b8ee8e8d4b 528 that do not correctly follow the PNG specification. This only supports errors
embeddedartists 0:83b8ee8e8d4b 529 that are fixable, were found in images that are actually used on the web, and
embeddedartists 0:83b8ee8e8d4b 530 are silently tolerated by other decoders as well. Currently only one such fix
embeddedartists 0:83b8ee8e8d4b 531 is implemented: if a palette index is out of bounds given the palette size,
embeddedartists 0:83b8ee8e8d4b 532 interpret it as opaque black.
embeddedartists 0:83b8ee8e8d4b 533 By default this value is 0, which makes it stop with an error on such images.
embeddedartists 0:83b8ee8e8d4b 534 */
embeddedartists 0:83b8ee8e8d4b 535 unsigned fix_png;
embeddedartists 0:83b8ee8e8d4b 536 unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/
embeddedartists 0:83b8ee8e8d4b 537
embeddedartists 0:83b8ee8e8d4b 538 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 539 unsigned read_text_chunks; /*if false but remember_unknown_chunks is true, they're stored in the unknown chunks*/
embeddedartists 0:83b8ee8e8d4b 540 /*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/
embeddedartists 0:83b8ee8e8d4b 541 unsigned remember_unknown_chunks;
embeddedartists 0:83b8ee8e8d4b 542 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
embeddedartists 0:83b8ee8e8d4b 543 } LodePNGDecoderSettings;
embeddedartists 0:83b8ee8e8d4b 544
embeddedartists 0:83b8ee8e8d4b 545 void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings);
embeddedartists 0:83b8ee8e8d4b 546 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 547
embeddedartists 0:83b8ee8e8d4b 548 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 549 /*automatically use color type with less bits per pixel if losslessly possible. Default: AUTO*/
embeddedartists 0:83b8ee8e8d4b 550 typedef enum LodePNGFilterStrategy
embeddedartists 0:83b8ee8e8d4b 551 {
embeddedartists 0:83b8ee8e8d4b 552 /*every filter at zero*/
embeddedartists 0:83b8ee8e8d4b 553 LFS_ZERO,
embeddedartists 0:83b8ee8e8d4b 554 /*Use filter that gives minumum sum, as described in the official PNG filter heuristic.*/
embeddedartists 0:83b8ee8e8d4b 555 LFS_MINSUM,
embeddedartists 0:83b8ee8e8d4b 556 /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending
embeddedartists 0:83b8ee8e8d4b 557 on the image, this is better or worse than minsum.*/
embeddedartists 0:83b8ee8e8d4b 558 LFS_ENTROPY,
embeddedartists 0:83b8ee8e8d4b 559 /*
embeddedartists 0:83b8ee8e8d4b 560 Brute-force-search PNG filters by compressing each filter for each scanline.
embeddedartists 0:83b8ee8e8d4b 561 Experimental, very slow, and only rarely gives better compression than MINSUM.
embeddedartists 0:83b8ee8e8d4b 562 */
embeddedartists 0:83b8ee8e8d4b 563 LFS_BRUTE_FORCE,
embeddedartists 0:83b8ee8e8d4b 564 /*use predefined_filters buffer: you specify the filter type for each scanline*/
embeddedartists 0:83b8ee8e8d4b 565 LFS_PREDEFINED
embeddedartists 0:83b8ee8e8d4b 566 } LodePNGFilterStrategy;
embeddedartists 0:83b8ee8e8d4b 567
embeddedartists 0:83b8ee8e8d4b 568 /*automatically use color type with less bits per pixel if losslessly possible. Default: LAC_AUTO*/
embeddedartists 0:83b8ee8e8d4b 569 typedef enum LodePNGAutoConvert
embeddedartists 0:83b8ee8e8d4b 570 {
embeddedartists 0:83b8ee8e8d4b 571 LAC_NO, /*use color type user requested*/
embeddedartists 0:83b8ee8e8d4b 572 LAC_ALPHA, /*use color type user requested, but if only opaque pixels and RGBA or grey+alpha, use RGB or grey*/
embeddedartists 0:83b8ee8e8d4b 573 LAC_AUTO, /*use PNG color type that can losslessly represent the uncompressed image the smallest possible*/
embeddedartists 0:83b8ee8e8d4b 574 /*
embeddedartists 0:83b8ee8e8d4b 575 like AUTO, but do not choose 1, 2 or 4 bit per pixel types.
embeddedartists 0:83b8ee8e8d4b 576 sometimes a PNG image compresses worse if less than 8 bits per pixels.
embeddedartists 0:83b8ee8e8d4b 577 */
embeddedartists 0:83b8ee8e8d4b 578 LAC_AUTO_NO_NIBBLES,
embeddedartists 0:83b8ee8e8d4b 579 /*
embeddedartists 0:83b8ee8e8d4b 580 like AUTO, but never choose palette color type. For small images, encoding
embeddedartists 0:83b8ee8e8d4b 581 the palette may take more bytes than what is gained. Note that AUTO also
embeddedartists 0:83b8ee8e8d4b 582 already prevents encoding the palette for extremely small images, but that may
embeddedartists 0:83b8ee8e8d4b 583 not be sufficient because due to the compression it cannot predict when to
embeddedartists 0:83b8ee8e8d4b 584 switch.
embeddedartists 0:83b8ee8e8d4b 585 */
embeddedartists 0:83b8ee8e8d4b 586 LAC_AUTO_NO_PALETTE,
embeddedartists 0:83b8ee8e8d4b 587 LAC_AUTO_NO_NIBBLES_NO_PALETTE
embeddedartists 0:83b8ee8e8d4b 588 } LodePNGAutoConvert;
embeddedartists 0:83b8ee8e8d4b 589
embeddedartists 0:83b8ee8e8d4b 590
embeddedartists 0:83b8ee8e8d4b 591 /*
embeddedartists 0:83b8ee8e8d4b 592 Automatically chooses color type that gives smallest amount of bits in the
embeddedartists 0:83b8ee8e8d4b 593 output image, e.g. grey if there are only greyscale pixels, palette if there
embeddedartists 0:83b8ee8e8d4b 594 are less than 256 colors, ...
embeddedartists 0:83b8ee8e8d4b 595 The auto_convert parameter allows limiting it to not use palette, ...
embeddedartists 0:83b8ee8e8d4b 596 */
embeddedartists 0:83b8ee8e8d4b 597 unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out,
embeddedartists 0:83b8ee8e8d4b 598 const unsigned char* image, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 599 const LodePNGColorMode* mode_in,
embeddedartists 0:83b8ee8e8d4b 600 LodePNGAutoConvert auto_convert);
embeddedartists 0:83b8ee8e8d4b 601
embeddedartists 0:83b8ee8e8d4b 602 /*Settings for the encoder.*/
embeddedartists 0:83b8ee8e8d4b 603 typedef struct LodePNGEncoderSettings
embeddedartists 0:83b8ee8e8d4b 604 {
embeddedartists 0:83b8ee8e8d4b 605 LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/
embeddedartists 0:83b8ee8e8d4b 606
embeddedartists 0:83b8ee8e8d4b 607 LodePNGAutoConvert auto_convert; /*how to automatically choose output PNG color type, if at all*/
embeddedartists 0:83b8ee8e8d4b 608
embeddedartists 0:83b8ee8e8d4b 609 /*If true, follows the official PNG heuristic: if the PNG uses a palette or lower than
embeddedartists 0:83b8ee8e8d4b 610 8 bit depth, set all filters to zero. Otherwise use the filter_strategy. Note that to
embeddedartists 0:83b8ee8e8d4b 611 completely follow the official PNG heuristic, filter_palette_zero must be true and
embeddedartists 0:83b8ee8e8d4b 612 filter_strategy must be LFS_MINSUM*/
embeddedartists 0:83b8ee8e8d4b 613 unsigned filter_palette_zero;
embeddedartists 0:83b8ee8e8d4b 614 /*Which filter strategy to use when not using zeroes due to filter_palette_zero.
embeddedartists 0:83b8ee8e8d4b 615 Set filter_palette_zero to 0 to ensure always using your chosen strategy. Default: LFS_MINSUM*/
embeddedartists 0:83b8ee8e8d4b 616 LodePNGFilterStrategy filter_strategy;
embeddedartists 0:83b8ee8e8d4b 617 /*used if filter_strategy is LFS_PREDEFINED. In that case, this must point to a buffer with
embeddedartists 0:83b8ee8e8d4b 618 the same length as the amount of scanlines in the image, and each value must <= 5. You
embeddedartists 0:83b8ee8e8d4b 619 have to cleanup this buffer, LodePNG will never free it. Don't forget that filter_palette_zero
embeddedartists 0:83b8ee8e8d4b 620 must be set to 0 to ensure this is also used on palette or low bitdepth images.*/
embeddedartists 0:83b8ee8e8d4b 621 const unsigned char* predefined_filters;
embeddedartists 0:83b8ee8e8d4b 622
embeddedartists 0:83b8ee8e8d4b 623 /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette).
embeddedartists 0:83b8ee8e8d4b 624 If colortype is 3, PLTE is _always_ created.*/
embeddedartists 0:83b8ee8e8d4b 625 unsigned force_palette;
embeddedartists 0:83b8ee8e8d4b 626 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
embeddedartists 0:83b8ee8e8d4b 627 /*add LodePNG identifier and version as a text chunk, for debugging*/
embeddedartists 0:83b8ee8e8d4b 628 unsigned add_id;
embeddedartists 0:83b8ee8e8d4b 629 /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/
embeddedartists 0:83b8ee8e8d4b 630 unsigned text_compression;
embeddedartists 0:83b8ee8e8d4b 631 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
embeddedartists 0:83b8ee8e8d4b 632 } LodePNGEncoderSettings;
embeddedartists 0:83b8ee8e8d4b 633
embeddedartists 0:83b8ee8e8d4b 634 void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings);
embeddedartists 0:83b8ee8e8d4b 635 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 636
embeddedartists 0:83b8ee8e8d4b 637
embeddedartists 0:83b8ee8e8d4b 638 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER)
embeddedartists 0:83b8ee8e8d4b 639 /*The settings, state and information for extended encoding and decoding.*/
embeddedartists 0:83b8ee8e8d4b 640 typedef struct LodePNGState
embeddedartists 0:83b8ee8e8d4b 641 {
embeddedartists 0:83b8ee8e8d4b 642 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 643 LodePNGDecoderSettings decoder; /*the decoding settings*/
embeddedartists 0:83b8ee8e8d4b 644 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 645 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 646 LodePNGEncoderSettings encoder; /*the encoding settings*/
embeddedartists 0:83b8ee8e8d4b 647 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 648 LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/
embeddedartists 0:83b8ee8e8d4b 649 LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/
embeddedartists 0:83b8ee8e8d4b 650 unsigned error;
embeddedartists 0:83b8ee8e8d4b 651 #ifdef LODEPNG_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 652 //For the lodepng::State subclass.
embeddedartists 0:83b8ee8e8d4b 653 virtual ~LodePNGState(){}
embeddedartists 0:83b8ee8e8d4b 654 #endif
embeddedartists 0:83b8ee8e8d4b 655 } LodePNGState;
embeddedartists 0:83b8ee8e8d4b 656
embeddedartists 0:83b8ee8e8d4b 657 /*init, cleanup and copy functions to use with this struct*/
embeddedartists 0:83b8ee8e8d4b 658 void lodepng_state_init(LodePNGState* state);
embeddedartists 0:83b8ee8e8d4b 659 void lodepng_state_cleanup(LodePNGState* state);
embeddedartists 0:83b8ee8e8d4b 660 void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source);
embeddedartists 0:83b8ee8e8d4b 661 #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */
embeddedartists 0:83b8ee8e8d4b 662
embeddedartists 0:83b8ee8e8d4b 663 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 664 /*
embeddedartists 0:83b8ee8e8d4b 665 Same as lodepng_decode_memory, but uses a LodePNGState to allow custom settings and
embeddedartists 0:83b8ee8e8d4b 666 getting much more information about the PNG image and color mode.
embeddedartists 0:83b8ee8e8d4b 667 */
embeddedartists 0:83b8ee8e8d4b 668 unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 669 LodePNGState* state,
embeddedartists 0:83b8ee8e8d4b 670 const unsigned char* in, size_t insize);
embeddedartists 0:83b8ee8e8d4b 671
embeddedartists 0:83b8ee8e8d4b 672 /*
embeddedartists 0:83b8ee8e8d4b 673 Read the PNG header, but not the actual data. This returns only the information
embeddedartists 0:83b8ee8e8d4b 674 that is in the header chunk of the PNG, such as width, height and color type. The
embeddedartists 0:83b8ee8e8d4b 675 information is placed in the info_png field of the LodePNGState.
embeddedartists 0:83b8ee8e8d4b 676 */
embeddedartists 0:83b8ee8e8d4b 677 unsigned lodepng_inspect(unsigned* w, unsigned* h,
embeddedartists 0:83b8ee8e8d4b 678 LodePNGState* state,
embeddedartists 0:83b8ee8e8d4b 679 const unsigned char* in, size_t insize);
embeddedartists 0:83b8ee8e8d4b 680 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 681
embeddedartists 0:83b8ee8e8d4b 682
embeddedartists 0:83b8ee8e8d4b 683 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 684 /*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/
embeddedartists 0:83b8ee8e8d4b 685 unsigned lodepng_encode(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 686 const unsigned char* image, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 687 LodePNGState* state);
embeddedartists 0:83b8ee8e8d4b 688 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 689
embeddedartists 0:83b8ee8e8d4b 690 /*
embeddedartists 0:83b8ee8e8d4b 691 The lodepng_chunk functions are normally not needed, except to traverse the
embeddedartists 0:83b8ee8e8d4b 692 unknown chunks stored in the LodePNGInfo struct, or add new ones to it.
embeddedartists 0:83b8ee8e8d4b 693 It also allows traversing the chunks of an encoded PNG file yourself.
embeddedartists 0:83b8ee8e8d4b 694
embeddedartists 0:83b8ee8e8d4b 695 PNG standard chunk naming conventions:
embeddedartists 0:83b8ee8e8d4b 696 First byte: uppercase = critical, lowercase = ancillary
embeddedartists 0:83b8ee8e8d4b 697 Second byte: uppercase = public, lowercase = private
embeddedartists 0:83b8ee8e8d4b 698 Third byte: must be uppercase
embeddedartists 0:83b8ee8e8d4b 699 Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy
embeddedartists 0:83b8ee8e8d4b 700 */
embeddedartists 0:83b8ee8e8d4b 701
embeddedartists 0:83b8ee8e8d4b 702 /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/
embeddedartists 0:83b8ee8e8d4b 703 unsigned lodepng_chunk_length(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 704
embeddedartists 0:83b8ee8e8d4b 705 /*puts the 4-byte type in null terminated string*/
embeddedartists 0:83b8ee8e8d4b 706 void lodepng_chunk_type(char type[5], const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 707
embeddedartists 0:83b8ee8e8d4b 708 /*check if the type is the given type*/
embeddedartists 0:83b8ee8e8d4b 709 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type);
embeddedartists 0:83b8ee8e8d4b 710
embeddedartists 0:83b8ee8e8d4b 711 /*0: it's one of the critical chunk types, 1: it's an ancillary chunk (see PNG standard)*/
embeddedartists 0:83b8ee8e8d4b 712 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 713
embeddedartists 0:83b8ee8e8d4b 714 /*0: public, 1: private (see PNG standard)*/
embeddedartists 0:83b8ee8e8d4b 715 unsigned char lodepng_chunk_private(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 716
embeddedartists 0:83b8ee8e8d4b 717 /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy (see PNG standard)*/
embeddedartists 0:83b8ee8e8d4b 718 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 719
embeddedartists 0:83b8ee8e8d4b 720 /*get pointer to the data of the chunk, where the input points to the header of the chunk*/
embeddedartists 0:83b8ee8e8d4b 721 unsigned char* lodepng_chunk_data(unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 722 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 723
embeddedartists 0:83b8ee8e8d4b 724 /*returns 0 if the crc is correct, 1 if it's incorrect (0 for OK as usual!)*/
embeddedartists 0:83b8ee8e8d4b 725 unsigned lodepng_chunk_check_crc(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 726
embeddedartists 0:83b8ee8e8d4b 727 /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/
embeddedartists 0:83b8ee8e8d4b 728 void lodepng_chunk_generate_crc(unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 729
embeddedartists 0:83b8ee8e8d4b 730 /*iterate to next chunks. don't use on IEND chunk, as there is no next chunk then*/
embeddedartists 0:83b8ee8e8d4b 731 unsigned char* lodepng_chunk_next(unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 732 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 733
embeddedartists 0:83b8ee8e8d4b 734 /*
embeddedartists 0:83b8ee8e8d4b 735 Appends chunk to the data in out. The given chunk should already have its chunk header.
embeddedartists 0:83b8ee8e8d4b 736 The out variable and outlength are updated to reflect the new reallocated buffer.
embeddedartists 0:83b8ee8e8d4b 737 Returns error code (0 if it went ok)
embeddedartists 0:83b8ee8e8d4b 738 */
embeddedartists 0:83b8ee8e8d4b 739 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk);
embeddedartists 0:83b8ee8e8d4b 740
embeddedartists 0:83b8ee8e8d4b 741 /*
embeddedartists 0:83b8ee8e8d4b 742 Appends new chunk to out. The chunk to append is given by giving its length, type
embeddedartists 0:83b8ee8e8d4b 743 and data separately. The type is a 4-letter string.
embeddedartists 0:83b8ee8e8d4b 744 The out variable and outlength are updated to reflect the new reallocated buffer.
embeddedartists 0:83b8ee8e8d4b 745 Returne error code (0 if it went ok)
embeddedartists 0:83b8ee8e8d4b 746 */
embeddedartists 0:83b8ee8e8d4b 747 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length,
embeddedartists 0:83b8ee8e8d4b 748 const char* type, const unsigned char* data);
embeddedartists 0:83b8ee8e8d4b 749
embeddedartists 0:83b8ee8e8d4b 750
embeddedartists 0:83b8ee8e8d4b 751 /*Calculate CRC32 of buffer*/
embeddedartists 0:83b8ee8e8d4b 752 unsigned lodepng_crc32(const unsigned char* buf, size_t len);
embeddedartists 0:83b8ee8e8d4b 753 #endif /*LODEPNG_COMPILE_PNG*/
embeddedartists 0:83b8ee8e8d4b 754
embeddedartists 0:83b8ee8e8d4b 755
embeddedartists 0:83b8ee8e8d4b 756 #ifdef LODEPNG_COMPILE_ZLIB
embeddedartists 0:83b8ee8e8d4b 757 /*
embeddedartists 0:83b8ee8e8d4b 758 This zlib part can be used independently to zlib compress and decompress a
embeddedartists 0:83b8ee8e8d4b 759 buffer. It cannot be used to create gzip files however, and it only supports the
embeddedartists 0:83b8ee8e8d4b 760 part of zlib that is required for PNG, it does not support dictionaries.
embeddedartists 0:83b8ee8e8d4b 761 */
embeddedartists 0:83b8ee8e8d4b 762
embeddedartists 0:83b8ee8e8d4b 763 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 764 /*Inflate a buffer. Inflate is the decompression step of deflate. Out buffer must be freed after use.*/
embeddedartists 0:83b8ee8e8d4b 765 unsigned lodepng_inflate(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 766 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 767 const LodePNGDecompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 768
embeddedartists 0:83b8ee8e8d4b 769 /*
embeddedartists 0:83b8ee8e8d4b 770 Decompresses Zlib data. Reallocates the out buffer and appends the data. The
embeddedartists 0:83b8ee8e8d4b 771 data must be according to the zlib specification.
embeddedartists 0:83b8ee8e8d4b 772 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid
embeddedartists 0:83b8ee8e8d4b 773 buffer and *outsize its size in bytes. out must be freed by user after usage.
embeddedartists 0:83b8ee8e8d4b 774 */
embeddedartists 0:83b8ee8e8d4b 775 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 776 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 777 const LodePNGDecompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 778 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 779
embeddedartists 0:83b8ee8e8d4b 780 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 781 /*
embeddedartists 0:83b8ee8e8d4b 782 Compresses data with Zlib. Reallocates the out buffer and appends the data.
embeddedartists 0:83b8ee8e8d4b 783 Zlib adds a small header and trailer around the deflate data.
embeddedartists 0:83b8ee8e8d4b 784 The data is output in the format of the zlib specification.
embeddedartists 0:83b8ee8e8d4b 785 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid
embeddedartists 0:83b8ee8e8d4b 786 buffer and *outsize its size in bytes. out must be freed by user after usage.
embeddedartists 0:83b8ee8e8d4b 787 */
embeddedartists 0:83b8ee8e8d4b 788 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 789 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 790 const LodePNGCompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 791
embeddedartists 0:83b8ee8e8d4b 792 /*
embeddedartists 0:83b8ee8e8d4b 793 Find length-limited Huffman code for given frequencies. This function is in the
embeddedartists 0:83b8ee8e8d4b 794 public interface only for tests, it's used internally by lodepng_deflate.
embeddedartists 0:83b8ee8e8d4b 795 */
embeddedartists 0:83b8ee8e8d4b 796 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
embeddedartists 0:83b8ee8e8d4b 797 size_t numcodes, unsigned maxbitlen);
embeddedartists 0:83b8ee8e8d4b 798
embeddedartists 0:83b8ee8e8d4b 799 /*Compress a buffer with deflate. See RFC 1951. Out buffer must be freed after use.*/
embeddedartists 0:83b8ee8e8d4b 800 unsigned lodepng_deflate(unsigned char** out, size_t* outsize,
embeddedartists 0:83b8ee8e8d4b 801 const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 802 const LodePNGCompressSettings* settings);
embeddedartists 0:83b8ee8e8d4b 803
embeddedartists 0:83b8ee8e8d4b 804 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 805 #endif /*LODEPNG_COMPILE_ZLIB*/
embeddedartists 0:83b8ee8e8d4b 806
embeddedartists 0:83b8ee8e8d4b 807 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 808 /*
embeddedartists 0:83b8ee8e8d4b 809 Load a file from disk into buffer. The function allocates the out buffer, and
embeddedartists 0:83b8ee8e8d4b 810 after usage you should free it.
embeddedartists 0:83b8ee8e8d4b 811 out: output parameter, contains pointer to loaded buffer.
embeddedartists 0:83b8ee8e8d4b 812 outsize: output parameter, size of the allocated out buffer
embeddedartists 0:83b8ee8e8d4b 813 filename: the path to the file to load
embeddedartists 0:83b8ee8e8d4b 814 return value: error code (0 means ok)
embeddedartists 0:83b8ee8e8d4b 815 */
embeddedartists 0:83b8ee8e8d4b 816 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename);
embeddedartists 0:83b8ee8e8d4b 817
embeddedartists 0:83b8ee8e8d4b 818 /*
embeddedartists 0:83b8ee8e8d4b 819 Save a file from buffer to disk. Warning, if it exists, this function overwrites
embeddedartists 0:83b8ee8e8d4b 820 the file without warning!
embeddedartists 0:83b8ee8e8d4b 821 buffer: the buffer to write
embeddedartists 0:83b8ee8e8d4b 822 buffersize: size of the buffer to write
embeddedartists 0:83b8ee8e8d4b 823 filename: the path to the file to save to
embeddedartists 0:83b8ee8e8d4b 824 return value: error code (0 means ok)
embeddedartists 0:83b8ee8e8d4b 825 */
embeddedartists 0:83b8ee8e8d4b 826 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename);
embeddedartists 0:83b8ee8e8d4b 827 #endif /*LODEPNG_COMPILE_DISK*/
embeddedartists 0:83b8ee8e8d4b 828
embeddedartists 0:83b8ee8e8d4b 829 #ifdef LODEPNG_COMPILE_CPP
embeddedartists 0:83b8ee8e8d4b 830 //The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers.
embeddedartists 0:83b8ee8e8d4b 831 namespace lodepng
embeddedartists 0:83b8ee8e8d4b 832 {
embeddedartists 0:83b8ee8e8d4b 833 #ifdef LODEPNG_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 834 class State : public LodePNGState
embeddedartists 0:83b8ee8e8d4b 835 {
embeddedartists 0:83b8ee8e8d4b 836 public:
embeddedartists 0:83b8ee8e8d4b 837 State();
embeddedartists 0:83b8ee8e8d4b 838 State(const State& other);
embeddedartists 0:83b8ee8e8d4b 839 virtual ~State();
embeddedartists 0:83b8ee8e8d4b 840 State& operator=(const State& other);
embeddedartists 0:83b8ee8e8d4b 841 };
embeddedartists 0:83b8ee8e8d4b 842
embeddedartists 0:83b8ee8e8d4b 843 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 844 //Same as other lodepng::decode, but using a State for more settings and information.
embeddedartists 0:83b8ee8e8d4b 845 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
embeddedartists 0:83b8ee8e8d4b 846 State& state,
embeddedartists 0:83b8ee8e8d4b 847 const unsigned char* in, size_t insize);
embeddedartists 0:83b8ee8e8d4b 848 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
embeddedartists 0:83b8ee8e8d4b 849 State& state,
embeddedartists 0:83b8ee8e8d4b 850 const std::vector<unsigned char>& in);
embeddedartists 0:83b8ee8e8d4b 851 #endif /*LODEPNG_COMPILE_DECODER*/
embeddedartists 0:83b8ee8e8d4b 852
embeddedartists 0:83b8ee8e8d4b 853 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 854 //Same as other lodepng::encode, but using a State for more settings and information.
embeddedartists 0:83b8ee8e8d4b 855 unsigned encode(std::vector<unsigned char>& out,
embeddedartists 0:83b8ee8e8d4b 856 const unsigned char* in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 857 State& state);
embeddedartists 0:83b8ee8e8d4b 858 unsigned encode(std::vector<unsigned char>& out,
embeddedartists 0:83b8ee8e8d4b 859 const std::vector<unsigned char>& in, unsigned w, unsigned h,
embeddedartists 0:83b8ee8e8d4b 860 State& state);
embeddedartists 0:83b8ee8e8d4b 861 #endif /*LODEPNG_COMPILE_ENCODER*/
embeddedartists 0:83b8ee8e8d4b 862
embeddedartists 0:83b8ee8e8d4b 863 #ifdef LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 864 /*
embeddedartists 0:83b8ee8e8d4b 865 Load a file from disk into an std::vector. If the vector is empty, then either
embeddedartists 0:83b8ee8e8d4b 866 the file doesn't exist or is an empty file.
embeddedartists 0:83b8ee8e8d4b 867 */
embeddedartists 0:83b8ee8e8d4b 868 void load_file(std::vector<unsigned char>& buffer, const std::string& filename);
embeddedartists 0:83b8ee8e8d4b 869
embeddedartists 0:83b8ee8e8d4b 870 /*
embeddedartists 0:83b8ee8e8d4b 871 Save the binary data in an std::vector to a file on disk. The file is overwritten
embeddedartists 0:83b8ee8e8d4b 872 without warning.
embeddedartists 0:83b8ee8e8d4b 873 */
embeddedartists 0:83b8ee8e8d4b 874 void save_file(const std::vector<unsigned char>& buffer, const std::string& filename);
embeddedartists 0:83b8ee8e8d4b 875 #endif //LODEPNG_COMPILE_DISK
embeddedartists 0:83b8ee8e8d4b 876 #endif //LODEPNG_COMPILE_PNG
embeddedartists 0:83b8ee8e8d4b 877
embeddedartists 0:83b8ee8e8d4b 878 #ifdef LODEPNG_COMPILE_ZLIB
embeddedartists 0:83b8ee8e8d4b 879 #ifdef LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 880 //Zlib-decompress an unsigned char buffer
embeddedartists 0:83b8ee8e8d4b 881 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 882 const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings);
embeddedartists 0:83b8ee8e8d4b 883
embeddedartists 0:83b8ee8e8d4b 884 //Zlib-decompress an std::vector
embeddedartists 0:83b8ee8e8d4b 885 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
embeddedartists 0:83b8ee8e8d4b 886 const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings);
embeddedartists 0:83b8ee8e8d4b 887 #endif //LODEPNG_COMPILE_DECODER
embeddedartists 0:83b8ee8e8d4b 888
embeddedartists 0:83b8ee8e8d4b 889 #ifdef LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 890 //Zlib-compress an unsigned char buffer
embeddedartists 0:83b8ee8e8d4b 891 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
embeddedartists 0:83b8ee8e8d4b 892 const LodePNGCompressSettings& settings = lodepng_default_compress_settings);
embeddedartists 0:83b8ee8e8d4b 893
embeddedartists 0:83b8ee8e8d4b 894 //Zlib-compress an std::vector
embeddedartists 0:83b8ee8e8d4b 895 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
embeddedartists 0:83b8ee8e8d4b 896 const LodePNGCompressSettings& settings = lodepng_default_compress_settings);
embeddedartists 0:83b8ee8e8d4b 897 #endif //LODEPNG_COMPILE_ENCODER
embeddedartists 0:83b8ee8e8d4b 898 #endif //LODEPNG_COMPILE_ZLIB
embeddedartists 0:83b8ee8e8d4b 899 } //namespace lodepng
embeddedartists 0:83b8ee8e8d4b 900 #endif /*LODEPNG_COMPILE_CPP*/
embeddedartists 0:83b8ee8e8d4b 901
embeddedartists 0:83b8ee8e8d4b 902 /*
embeddedartists 0:83b8ee8e8d4b 903 TODO:
embeddedartists 0:83b8ee8e8d4b 904 [.] test if there are no memory leaks or security exploits - done a lot but needs to be checked often
embeddedartists 0:83b8ee8e8d4b 905 [.] check compatibility with vareous compilers - done but needs to be redone for every newer version
embeddedartists 0:83b8ee8e8d4b 906 [X] converting color to 16-bit per channel types
embeddedartists 0:83b8ee8e8d4b 907 [ ] read all public PNG chunk types (but never let the color profile and gamma ones touch RGB values)
embeddedartists 0:83b8ee8e8d4b 908 [ ] make sure encoder generates no chunks with size > (2^31)-1
embeddedartists 0:83b8ee8e8d4b 909 [ ] partial decoding (stream processing)
embeddedartists 0:83b8ee8e8d4b 910 [X] let the "isFullyOpaque" function check color keys and transparent palettes too
embeddedartists 0:83b8ee8e8d4b 911 [X] better name for the variables "codes", "codesD", "codelengthcodes", "clcl" and "lldl"
embeddedartists 0:83b8ee8e8d4b 912 [ ] don't stop decoding on errors like 69, 57, 58 (make warnings)
embeddedartists 0:83b8ee8e8d4b 913 [ ] make option to choose if the raw image with non multiple of 8 bits per scanline should have padding bits or not
embeddedartists 0:83b8ee8e8d4b 914 [ ] let the C++ wrapper catch exceptions coming from the standard library and return LodePNG error codes
embeddedartists 0:83b8ee8e8d4b 915 */
embeddedartists 0:83b8ee8e8d4b 916
embeddedartists 0:83b8ee8e8d4b 917 #endif /*LODEPNG_H inclusion guard*/
embeddedartists 0:83b8ee8e8d4b 918
embeddedartists 0:83b8ee8e8d4b 919 /*
embeddedartists 0:83b8ee8e8d4b 920 LodePNG Documentation
embeddedartists 0:83b8ee8e8d4b 921 ---------------------
embeddedartists 0:83b8ee8e8d4b 922
embeddedartists 0:83b8ee8e8d4b 923 0. table of contents
embeddedartists 0:83b8ee8e8d4b 924 --------------------
embeddedartists 0:83b8ee8e8d4b 925
embeddedartists 0:83b8ee8e8d4b 926 1. about
embeddedartists 0:83b8ee8e8d4b 927 1.1. supported features
embeddedartists 0:83b8ee8e8d4b 928 1.2. features not supported
embeddedartists 0:83b8ee8e8d4b 929 2. C and C++ version
embeddedartists 0:83b8ee8e8d4b 930 3. security
embeddedartists 0:83b8ee8e8d4b 931 4. decoding
embeddedartists 0:83b8ee8e8d4b 932 5. encoding
embeddedartists 0:83b8ee8e8d4b 933 6. color conversions
embeddedartists 0:83b8ee8e8d4b 934 6.1. PNG color types
embeddedartists 0:83b8ee8e8d4b 935 6.2. color conversions
embeddedartists 0:83b8ee8e8d4b 936 6.3. padding bits
embeddedartists 0:83b8ee8e8d4b 937 6.4. A note about 16-bits per channel and endianness
embeddedartists 0:83b8ee8e8d4b 938 7. error values
embeddedartists 0:83b8ee8e8d4b 939 8. chunks and PNG editing
embeddedartists 0:83b8ee8e8d4b 940 9. compiler support
embeddedartists 0:83b8ee8e8d4b 941 10. examples
embeddedartists 0:83b8ee8e8d4b 942 10.1. decoder C++ example
embeddedartists 0:83b8ee8e8d4b 943 10.2. decoder C example
embeddedartists 0:83b8ee8e8d4b 944 11. changes
embeddedartists 0:83b8ee8e8d4b 945 12. contact information
embeddedartists 0:83b8ee8e8d4b 946
embeddedartists 0:83b8ee8e8d4b 947
embeddedartists 0:83b8ee8e8d4b 948 1. about
embeddedartists 0:83b8ee8e8d4b 949 --------
embeddedartists 0:83b8ee8e8d4b 950
embeddedartists 0:83b8ee8e8d4b 951 PNG is a file format to store raster images losslessly with good compression,
embeddedartists 0:83b8ee8e8d4b 952 supporting different color types and alpha channel.
embeddedartists 0:83b8ee8e8d4b 953
embeddedartists 0:83b8ee8e8d4b 954 LodePNG is a PNG codec according to the Portable Network Graphics (PNG)
embeddedartists 0:83b8ee8e8d4b 955 Specification (Second Edition) - W3C Recommendation 10 November 2003.
embeddedartists 0:83b8ee8e8d4b 956
embeddedartists 0:83b8ee8e8d4b 957 The specifications used are:
embeddedartists 0:83b8ee8e8d4b 958
embeddedartists 0:83b8ee8e8d4b 959 *) Portable Network Graphics (PNG) Specification (Second Edition):
embeddedartists 0:83b8ee8e8d4b 960 http://www.w3.org/TR/2003/REC-PNG-20031110
embeddedartists 0:83b8ee8e8d4b 961 *) RFC 1950 ZLIB Compressed Data Format version 3.3:
embeddedartists 0:83b8ee8e8d4b 962 http://www.gzip.org/zlib/rfc-zlib.html
embeddedartists 0:83b8ee8e8d4b 963 *) RFC 1951 DEFLATE Compressed Data Format Specification ver 1.3:
embeddedartists 0:83b8ee8e8d4b 964 http://www.gzip.org/zlib/rfc-deflate.html
embeddedartists 0:83b8ee8e8d4b 965
embeddedartists 0:83b8ee8e8d4b 966 The most recent version of LodePNG can currently be found at
embeddedartists 0:83b8ee8e8d4b 967 http://lodev.org/lodepng/
embeddedartists 0:83b8ee8e8d4b 968
embeddedartists 0:83b8ee8e8d4b 969 LodePNG works both in C (ISO C90) and C++, with a C++ wrapper that adds
embeddedartists 0:83b8ee8e8d4b 970 extra functionality.
embeddedartists 0:83b8ee8e8d4b 971
embeddedartists 0:83b8ee8e8d4b 972 LodePNG exists out of two files:
embeddedartists 0:83b8ee8e8d4b 973 -lodepng.h: the header file for both C and C++
embeddedartists 0:83b8ee8e8d4b 974 -lodepng.c(pp): give it the name lodepng.c or lodepng.cpp (or .cc) depending on your usage
embeddedartists 0:83b8ee8e8d4b 975
embeddedartists 0:83b8ee8e8d4b 976 If you want to start using LodePNG right away without reading this doc, get the
embeddedartists 0:83b8ee8e8d4b 977 examples from the LodePNG website to see how to use it in code, or check the
embeddedartists 0:83b8ee8e8d4b 978 smaller examples in chapter 13 here.
embeddedartists 0:83b8ee8e8d4b 979
embeddedartists 0:83b8ee8e8d4b 980 LodePNG is simple but only supports the basic requirements. To achieve
embeddedartists 0:83b8ee8e8d4b 981 simplicity, the following design choices were made: There are no dependencies
embeddedartists 0:83b8ee8e8d4b 982 on any external library. There are functions to decode and encode a PNG with
embeddedartists 0:83b8ee8e8d4b 983 a single function call, and extended versions of these functions taking a
embeddedartists 0:83b8ee8e8d4b 984 LodePNGState struct allowing to specify or get more information. By default
embeddedartists 0:83b8ee8e8d4b 985 the colors of the raw image are always RGB or RGBA, no matter what color type
embeddedartists 0:83b8ee8e8d4b 986 the PNG file uses. To read and write files, there are simple functions to
embeddedartists 0:83b8ee8e8d4b 987 convert the files to/from buffers in memory.
embeddedartists 0:83b8ee8e8d4b 988
embeddedartists 0:83b8ee8e8d4b 989 This all makes LodePNG suitable for loading textures in games, demos and small
embeddedartists 0:83b8ee8e8d4b 990 programs, ... It's less suitable for full fledged image editors, loading PNGs
embeddedartists 0:83b8ee8e8d4b 991 over network (it requires all the image data to be available before decoding can
embeddedartists 0:83b8ee8e8d4b 992 begin), life-critical systems, ...
embeddedartists 0:83b8ee8e8d4b 993
embeddedartists 0:83b8ee8e8d4b 994 1.1. supported features
embeddedartists 0:83b8ee8e8d4b 995 -----------------------
embeddedartists 0:83b8ee8e8d4b 996
embeddedartists 0:83b8ee8e8d4b 997 The following features are supported by the decoder:
embeddedartists 0:83b8ee8e8d4b 998
embeddedartists 0:83b8ee8e8d4b 999 *) decoding of PNGs with any color type, bit depth and interlace mode, to a 24- or 32-bit color raw image,
embeddedartists 0:83b8ee8e8d4b 1000 or the same color type as the PNG
embeddedartists 0:83b8ee8e8d4b 1001 *) encoding of PNGs, from any raw image to 24- or 32-bit color, or the same color type as the raw image
embeddedartists 0:83b8ee8e8d4b 1002 *) Adam7 interlace and deinterlace for any color type
embeddedartists 0:83b8ee8e8d4b 1003 *) loading the image from harddisk or decoding it from a buffer from other sources than harddisk
embeddedartists 0:83b8ee8e8d4b 1004 *) support for alpha channels, including RGBA color model, translucent palettes and color keying
embeddedartists 0:83b8ee8e8d4b 1005 *) zlib decompression (inflate)
embeddedartists 0:83b8ee8e8d4b 1006 *) zlib compression (deflate)
embeddedartists 0:83b8ee8e8d4b 1007 *) CRC32 and ADLER32 checksums
embeddedartists 0:83b8ee8e8d4b 1008 *) handling of unknown chunks, allowing making a PNG editor that stores custom and unknown chunks.
embeddedartists 0:83b8ee8e8d4b 1009 *) the following chunks are supported (generated/interpreted) by both encoder and decoder:
embeddedartists 0:83b8ee8e8d4b 1010 IHDR: header information
embeddedartists 0:83b8ee8e8d4b 1011 PLTE: color palette
embeddedartists 0:83b8ee8e8d4b 1012 IDAT: pixel data
embeddedartists 0:83b8ee8e8d4b 1013 IEND: the final chunk
embeddedartists 0:83b8ee8e8d4b 1014 tRNS: transparency for palettized images
embeddedartists 0:83b8ee8e8d4b 1015 tEXt: textual information
embeddedartists 0:83b8ee8e8d4b 1016 zTXt: compressed textual information
embeddedartists 0:83b8ee8e8d4b 1017 iTXt: international textual information
embeddedartists 0:83b8ee8e8d4b 1018 bKGD: suggested background color
embeddedartists 0:83b8ee8e8d4b 1019 pHYs: physical dimensions
embeddedartists 0:83b8ee8e8d4b 1020 tIME: modification time
embeddedartists 0:83b8ee8e8d4b 1021
embeddedartists 0:83b8ee8e8d4b 1022 1.2. features not supported
embeddedartists 0:83b8ee8e8d4b 1023 ---------------------------
embeddedartists 0:83b8ee8e8d4b 1024
embeddedartists 0:83b8ee8e8d4b 1025 The following features are _not_ supported:
embeddedartists 0:83b8ee8e8d4b 1026
embeddedartists 0:83b8ee8e8d4b 1027 *) some features needed to make a conformant PNG-Editor might be still missing.
embeddedartists 0:83b8ee8e8d4b 1028 *) partial loading/stream processing. All data must be available and is processed in one call.
embeddedartists 0:83b8ee8e8d4b 1029 *) The following public chunks are not supported but treated as unknown chunks by LodePNG
embeddedartists 0:83b8ee8e8d4b 1030 cHRM, gAMA, iCCP, sRGB, sBIT, hIST, sPLT
embeddedartists 0:83b8ee8e8d4b 1031 Some of these are not supported on purpose: LodePNG wants to provide the RGB values
embeddedartists 0:83b8ee8e8d4b 1032 stored in the pixels, not values modified by system dependent gamma or color models.
embeddedartists 0:83b8ee8e8d4b 1033
embeddedartists 0:83b8ee8e8d4b 1034
embeddedartists 0:83b8ee8e8d4b 1035 2. C and C++ version
embeddedartists 0:83b8ee8e8d4b 1036 --------------------
embeddedartists 0:83b8ee8e8d4b 1037
embeddedartists 0:83b8ee8e8d4b 1038 The C version uses buffers allocated with alloc that you need to free()
embeddedartists 0:83b8ee8e8d4b 1039 yourself. You need to use init and cleanup functions for each struct whenever
embeddedartists 0:83b8ee8e8d4b 1040 using a struct from the C version to avoid exploits and memory leaks.
embeddedartists 0:83b8ee8e8d4b 1041
embeddedartists 0:83b8ee8e8d4b 1042 The C++ version has extra functions with std::vectors in the interface and the
embeddedartists 0:83b8ee8e8d4b 1043 lodepng::State class which is a LodePNGState with constructor and destructor.
embeddedartists 0:83b8ee8e8d4b 1044
embeddedartists 0:83b8ee8e8d4b 1045 These files work without modification for both C and C++ compilers because all
embeddedartists 0:83b8ee8e8d4b 1046 the additional C++ code is in "#ifdef __cplusplus" blocks that make C-compilers
embeddedartists 0:83b8ee8e8d4b 1047 ignore it, and the C code is made to compile both with strict ISO C90 and C++.
embeddedartists 0:83b8ee8e8d4b 1048
embeddedartists 0:83b8ee8e8d4b 1049 To use the C++ version, you need to rename the source file to lodepng.cpp
embeddedartists 0:83b8ee8e8d4b 1050 (instead of lodepng.c), and compile it with a C++ compiler.
embeddedartists 0:83b8ee8e8d4b 1051
embeddedartists 0:83b8ee8e8d4b 1052 To use the C version, you need to rename the source file to lodepng.c (instead
embeddedartists 0:83b8ee8e8d4b 1053 of lodepng.cpp), and compile it with a C compiler.
embeddedartists 0:83b8ee8e8d4b 1054
embeddedartists 0:83b8ee8e8d4b 1055
embeddedartists 0:83b8ee8e8d4b 1056 3. Security
embeddedartists 0:83b8ee8e8d4b 1057 -----------
embeddedartists 0:83b8ee8e8d4b 1058
embeddedartists 0:83b8ee8e8d4b 1059 Even if carefully designed, it's always possible that LodePNG contains possible
embeddedartists 0:83b8ee8e8d4b 1060 exploits. If you discover one, please let me know, and it will be fixed.
embeddedartists 0:83b8ee8e8d4b 1061
embeddedartists 0:83b8ee8e8d4b 1062 When using LodePNG, care has to be taken with the C version of LodePNG, as well
embeddedartists 0:83b8ee8e8d4b 1063 as the C-style structs when working with C++. The following conventions are used
embeddedartists 0:83b8ee8e8d4b 1064 for all C-style structs:
embeddedartists 0:83b8ee8e8d4b 1065
embeddedartists 0:83b8ee8e8d4b 1066 -if a struct has a corresponding init function, always call the init function when making a new one
embeddedartists 0:83b8ee8e8d4b 1067 -if a struct has a corresponding cleanup function, call it before the struct disappears to avoid memory leaks
embeddedartists 0:83b8ee8e8d4b 1068 -if a struct has a corresponding copy function, use the copy function instead of "=".
embeddedartists 0:83b8ee8e8d4b 1069 The destination must also be inited already.
embeddedartists 0:83b8ee8e8d4b 1070
embeddedartists 0:83b8ee8e8d4b 1071
embeddedartists 0:83b8ee8e8d4b 1072 4. Decoding
embeddedartists 0:83b8ee8e8d4b 1073 -----------
embeddedartists 0:83b8ee8e8d4b 1074
embeddedartists 0:83b8ee8e8d4b 1075 Decoding converts a PNG compressed image to a raw pixel buffer.
embeddedartists 0:83b8ee8e8d4b 1076
embeddedartists 0:83b8ee8e8d4b 1077 Most documentation on using the decoder is at its declarations in the header
embeddedartists 0:83b8ee8e8d4b 1078 above. For C, simple decoding can be done with functions such as
embeddedartists 0:83b8ee8e8d4b 1079 lodepng_decode32, and more advanced decoding can be done with the struct
embeddedartists 0:83b8ee8e8d4b 1080 LodePNGState and lodepng_decode. For C++, all decoding can be done with the
embeddedartists 0:83b8ee8e8d4b 1081 various lodepng::decode functions, and lodepng::State can be used for advanced
embeddedartists 0:83b8ee8e8d4b 1082 features.
embeddedartists 0:83b8ee8e8d4b 1083
embeddedartists 0:83b8ee8e8d4b 1084 When using the LodePNGState, it uses the following fields for decoding:
embeddedartists 0:83b8ee8e8d4b 1085 *) LodePNGInfo info_png: it stores extra information about the PNG (the input) in here
embeddedartists 0:83b8ee8e8d4b 1086 *) LodePNGColorMode info_raw: here you can say what color mode of the raw image (the output) you want to get
embeddedartists 0:83b8ee8e8d4b 1087 *) LodePNGDecoderSettings decoder: you can specify a few extra settings for the decoder to use
embeddedartists 0:83b8ee8e8d4b 1088
embeddedartists 0:83b8ee8e8d4b 1089 LodePNGInfo info_png
embeddedartists 0:83b8ee8e8d4b 1090 --------------------
embeddedartists 0:83b8ee8e8d4b 1091
embeddedartists 0:83b8ee8e8d4b 1092 After decoding, this contains extra information of the PNG image, except the actual
embeddedartists 0:83b8ee8e8d4b 1093 pixels, width and height because these are already gotten directly from the decoder
embeddedartists 0:83b8ee8e8d4b 1094 functions.
embeddedartists 0:83b8ee8e8d4b 1095
embeddedartists 0:83b8ee8e8d4b 1096 It contains for example the original color type of the PNG image, text comments,
embeddedartists 0:83b8ee8e8d4b 1097 suggested background color, etc... More details about the LodePNGInfo struct are
embeddedartists 0:83b8ee8e8d4b 1098 at its declaration documentation.
embeddedartists 0:83b8ee8e8d4b 1099
embeddedartists 0:83b8ee8e8d4b 1100 LodePNGColorMode info_raw
embeddedartists 0:83b8ee8e8d4b 1101 -------------------------
embeddedartists 0:83b8ee8e8d4b 1102
embeddedartists 0:83b8ee8e8d4b 1103 When decoding, here you can specify which color type you want
embeddedartists 0:83b8ee8e8d4b 1104 the resulting raw image to be. If this is different from the colortype of the
embeddedartists 0:83b8ee8e8d4b 1105 PNG, then the decoder will automatically convert the result. This conversion
embeddedartists 0:83b8ee8e8d4b 1106 always works, except if you want it to convert a color PNG to greyscale or to
embeddedartists 0:83b8ee8e8d4b 1107 a palette with missing colors.
embeddedartists 0:83b8ee8e8d4b 1108
embeddedartists 0:83b8ee8e8d4b 1109 By default, 32-bit color is used for the result.
embeddedartists 0:83b8ee8e8d4b 1110
embeddedartists 0:83b8ee8e8d4b 1111 LodePNGDecoderSettings decoder
embeddedartists 0:83b8ee8e8d4b 1112 ------------------------------
embeddedartists 0:83b8ee8e8d4b 1113
embeddedartists 0:83b8ee8e8d4b 1114 The settings can be used to ignore the errors created by invalid CRC and Adler32
embeddedartists 0:83b8ee8e8d4b 1115 chunks, and to disable the decoding of tEXt chunks.
embeddedartists 0:83b8ee8e8d4b 1116
embeddedartists 0:83b8ee8e8d4b 1117 There's also a setting color_convert, true by default. If false, no conversion
embeddedartists 0:83b8ee8e8d4b 1118 is done, the resulting data will be as it was in the PNG (after decompression)
embeddedartists 0:83b8ee8e8d4b 1119 and you'll have to puzzle the colors of the pixels together yourself using the
embeddedartists 0:83b8ee8e8d4b 1120 color type information in the LodePNGInfo.
embeddedartists 0:83b8ee8e8d4b 1121
embeddedartists 0:83b8ee8e8d4b 1122
embeddedartists 0:83b8ee8e8d4b 1123 5. Encoding
embeddedartists 0:83b8ee8e8d4b 1124 -----------
embeddedartists 0:83b8ee8e8d4b 1125
embeddedartists 0:83b8ee8e8d4b 1126 Encoding converts a raw pixel buffer to a PNG compressed image.
embeddedartists 0:83b8ee8e8d4b 1127
embeddedartists 0:83b8ee8e8d4b 1128 Most documentation on using the encoder is at its declarations in the header
embeddedartists 0:83b8ee8e8d4b 1129 above. For C, simple encoding can be done with functions such as
embeddedartists 0:83b8ee8e8d4b 1130 lodepng_encode32, and more advanced decoding can be done with the struct
embeddedartists 0:83b8ee8e8d4b 1131 LodePNGState and lodepng_encode. For C++, all encoding can be done with the
embeddedartists 0:83b8ee8e8d4b 1132 various lodepng::encode functions, and lodepng::State can be used for advanced
embeddedartists 0:83b8ee8e8d4b 1133 features.
embeddedartists 0:83b8ee8e8d4b 1134
embeddedartists 0:83b8ee8e8d4b 1135 Like the decoder, the encoder can also give errors. However it gives less errors
embeddedartists 0:83b8ee8e8d4b 1136 since the encoder input is trusted, the decoder input (a PNG image that could
embeddedartists 0:83b8ee8e8d4b 1137 be forged by anyone) is not trusted.
embeddedartists 0:83b8ee8e8d4b 1138
embeddedartists 0:83b8ee8e8d4b 1139 When using the LodePNGState, it uses the following fields for encoding:
embeddedartists 0:83b8ee8e8d4b 1140 *) LodePNGInfo info_png: here you specify how you want the PNG (the output) to be.
embeddedartists 0:83b8ee8e8d4b 1141 *) LodePNGColorMode info_raw: here you say what color type of the raw image (the input) has
embeddedartists 0:83b8ee8e8d4b 1142 *) LodePNGEncoderSettings encoder: you can specify a few settings for the encoder to use
embeddedartists 0:83b8ee8e8d4b 1143
embeddedartists 0:83b8ee8e8d4b 1144 LodePNGInfo info_png
embeddedartists 0:83b8ee8e8d4b 1145 --------------------
embeddedartists 0:83b8ee8e8d4b 1146
embeddedartists 0:83b8ee8e8d4b 1147 When encoding, you use this the opposite way as when decoding: for encoding,
embeddedartists 0:83b8ee8e8d4b 1148 you fill in the values you want the PNG to have before encoding. By default it's
embeddedartists 0:83b8ee8e8d4b 1149 not needed to specify a color type for the PNG since it's automatically chosen,
embeddedartists 0:83b8ee8e8d4b 1150 but it's possible to choose it yourself given the right settings.
embeddedartists 0:83b8ee8e8d4b 1151
embeddedartists 0:83b8ee8e8d4b 1152 The encoder will not always exactly match the LodePNGInfo struct you give,
embeddedartists 0:83b8ee8e8d4b 1153 it tries as close as possible. Some things are ignored by the encoder. The
embeddedartists 0:83b8ee8e8d4b 1154 encoder uses, for example, the following settings from it when applicable:
embeddedartists 0:83b8ee8e8d4b 1155 colortype and bitdepth, text chunks, time chunk, the color key, the palette, the
embeddedartists 0:83b8ee8e8d4b 1156 background color, the interlace method, unknown chunks, ...
embeddedartists 0:83b8ee8e8d4b 1157
embeddedartists 0:83b8ee8e8d4b 1158 When encoding to a PNG with colortype 3, the encoder will generate a PLTE chunk.
embeddedartists 0:83b8ee8e8d4b 1159 If the palette contains any colors for which the alpha channel is not 255 (so
embeddedartists 0:83b8ee8e8d4b 1160 there are translucent colors in the palette), it'll add a tRNS chunk.
embeddedartists 0:83b8ee8e8d4b 1161
embeddedartists 0:83b8ee8e8d4b 1162 LodePNGColorMode info_raw
embeddedartists 0:83b8ee8e8d4b 1163 -------------------------
embeddedartists 0:83b8ee8e8d4b 1164
embeddedartists 0:83b8ee8e8d4b 1165 You specify the color type of the raw image that you give to the input here,
embeddedartists 0:83b8ee8e8d4b 1166 including a possible transparent color key and palette you happen to be using in
embeddedartists 0:83b8ee8e8d4b 1167 your raw image data.
embeddedartists 0:83b8ee8e8d4b 1168
embeddedartists 0:83b8ee8e8d4b 1169 By default, 32-bit color is assumed, meaning your input has to be in RGBA
embeddedartists 0:83b8ee8e8d4b 1170 format with 4 bytes (unsigned chars) per pixel.
embeddedartists 0:83b8ee8e8d4b 1171
embeddedartists 0:83b8ee8e8d4b 1172 LodePNGEncoderSettings encoder
embeddedartists 0:83b8ee8e8d4b 1173 ------------------------------
embeddedartists 0:83b8ee8e8d4b 1174
embeddedartists 0:83b8ee8e8d4b 1175 The following settings are supported (some are in sub-structs):
embeddedartists 0:83b8ee8e8d4b 1176 *) auto_convert: when this option is enabled, the encoder will
embeddedartists 0:83b8ee8e8d4b 1177 automatically choose the smallest possible color mode (including color key) that
embeddedartists 0:83b8ee8e8d4b 1178 can encode the colors of all pixels without information loss.
embeddedartists 0:83b8ee8e8d4b 1179 *) btype: the block type for LZ77. 0 = uncompressed, 1 = fixed huffman tree,
embeddedartists 0:83b8ee8e8d4b 1180 2 = dynamic huffman tree (best compression). Should be 2 for proper
embeddedartists 0:83b8ee8e8d4b 1181 compression.
embeddedartists 0:83b8ee8e8d4b 1182 *) use_lz77: whether or not to use LZ77 for compressed block types. Should be
embeddedartists 0:83b8ee8e8d4b 1183 true for proper compression.
embeddedartists 0:83b8ee8e8d4b 1184 *) windowsize: the window size used by the LZ77 encoder (1 - 32768). Has value
embeddedartists 0:83b8ee8e8d4b 1185 2048 by default, but can be set to 32768 for better, but slow, compression.
embeddedartists 0:83b8ee8e8d4b 1186 *) force_palette: if colortype is 2 or 6, you can make the encoder write a PLTE
embeddedartists 0:83b8ee8e8d4b 1187 chunk if force_palette is true. This can used as suggested palette to convert
embeddedartists 0:83b8ee8e8d4b 1188 to by viewers that don't support more than 256 colors (if those still exist)
embeddedartists 0:83b8ee8e8d4b 1189 *) add_id: add text chunk "Encoder: LodePNG <version>" to the image.
embeddedartists 0:83b8ee8e8d4b 1190 *) text_compression: default 1. If 1, it'll store texts as zTXt instead of tEXt chunks.
embeddedartists 0:83b8ee8e8d4b 1191 zTXt chunks use zlib compression on the text. This gives a smaller result on
embeddedartists 0:83b8ee8e8d4b 1192 large texts but a larger result on small texts (such as a single program name).
embeddedartists 0:83b8ee8e8d4b 1193 It's all tEXt or all zTXt though, there's no separate setting per text yet.
embeddedartists 0:83b8ee8e8d4b 1194
embeddedartists 0:83b8ee8e8d4b 1195
embeddedartists 0:83b8ee8e8d4b 1196 6. color conversions
embeddedartists 0:83b8ee8e8d4b 1197 --------------------
embeddedartists 0:83b8ee8e8d4b 1198
embeddedartists 0:83b8ee8e8d4b 1199 An important thing to note about LodePNG, is that the color type of the PNG, and
embeddedartists 0:83b8ee8e8d4b 1200 the color type of the raw image, are completely independent. By default, when
embeddedartists 0:83b8ee8e8d4b 1201 you decode a PNG, you get the result as a raw image in the color type you want,
embeddedartists 0:83b8ee8e8d4b 1202 no matter whether the PNG was encoded with a palette, greyscale or RGBA color.
embeddedartists 0:83b8ee8e8d4b 1203 And if you encode an image, by default LodePNG will automatically choose the PNG
embeddedartists 0:83b8ee8e8d4b 1204 color type that gives good compression based on the values of colors and amount
embeddedartists 0:83b8ee8e8d4b 1205 of colors in the image. It can be configured to let you control it instead as
embeddedartists 0:83b8ee8e8d4b 1206 well, though.
embeddedartists 0:83b8ee8e8d4b 1207
embeddedartists 0:83b8ee8e8d4b 1208 To be able to do this, LodePNG does conversions from one color mode to another.
embeddedartists 0:83b8ee8e8d4b 1209 It can convert from almost any color type to any other color type, except the
embeddedartists 0:83b8ee8e8d4b 1210 following conversions: RGB to greyscale is not supported, and converting to a
embeddedartists 0:83b8ee8e8d4b 1211 palette when the palette doesn't have a required color is not supported. This is
embeddedartists 0:83b8ee8e8d4b 1212 not supported on purpose: this is information loss which requires a color
embeddedartists 0:83b8ee8e8d4b 1213 reduction algorithm that is beyong the scope of a PNG encoder (yes, RGB to grey
embeddedartists 0:83b8ee8e8d4b 1214 is easy, but there are multiple ways if you want to give some channels more
embeddedartists 0:83b8ee8e8d4b 1215 weight).
embeddedartists 0:83b8ee8e8d4b 1216
embeddedartists 0:83b8ee8e8d4b 1217 By default, when decoding, you get the raw image in 32-bit RGBA or 24-bit RGB
embeddedartists 0:83b8ee8e8d4b 1218 color, no matter what color type the PNG has. And by default when encoding,
embeddedartists 0:83b8ee8e8d4b 1219 LodePNG automatically picks the best color model for the output PNG, and expects
embeddedartists 0:83b8ee8e8d4b 1220 the input image to be 32-bit RGBA or 24-bit RGB. So, unless you want to control
embeddedartists 0:83b8ee8e8d4b 1221 the color format of the images yourself, you can skip this chapter.
embeddedartists 0:83b8ee8e8d4b 1222
embeddedartists 0:83b8ee8e8d4b 1223 6.1. PNG color types
embeddedartists 0:83b8ee8e8d4b 1224 --------------------
embeddedartists 0:83b8ee8e8d4b 1225
embeddedartists 0:83b8ee8e8d4b 1226 A PNG image can have many color types, ranging from 1-bit color to 64-bit color,
embeddedartists 0:83b8ee8e8d4b 1227 as well as palettized color modes. After the zlib decompression and unfiltering
embeddedartists 0:83b8ee8e8d4b 1228 in the PNG image is done, the raw pixel data will have that color type and thus
embeddedartists 0:83b8ee8e8d4b 1229 a certain amount of bits per pixel. If you want the output raw image after
embeddedartists 0:83b8ee8e8d4b 1230 decoding to have another color type, a conversion is done by LodePNG.
embeddedartists 0:83b8ee8e8d4b 1231
embeddedartists 0:83b8ee8e8d4b 1232 The PNG specification gives the following color types:
embeddedartists 0:83b8ee8e8d4b 1233
embeddedartists 0:83b8ee8e8d4b 1234 0: greyscale, bit depths 1, 2, 4, 8, 16
embeddedartists 0:83b8ee8e8d4b 1235 2: RGB, bit depths 8 and 16
embeddedartists 0:83b8ee8e8d4b 1236 3: palette, bit depths 1, 2, 4 and 8
embeddedartists 0:83b8ee8e8d4b 1237 4: greyscale with alpha, bit depths 8 and 16
embeddedartists 0:83b8ee8e8d4b 1238 6: RGBA, bit depths 8 and 16
embeddedartists 0:83b8ee8e8d4b 1239
embeddedartists 0:83b8ee8e8d4b 1240 Bit depth is the amount of bits per pixel per color channel. So the total amount
embeddedartists 0:83b8ee8e8d4b 1241 of bits per pixel is: amount of channels * bitdepth.
embeddedartists 0:83b8ee8e8d4b 1242
embeddedartists 0:83b8ee8e8d4b 1243 6.2. color conversions
embeddedartists 0:83b8ee8e8d4b 1244 ----------------------
embeddedartists 0:83b8ee8e8d4b 1245
embeddedartists 0:83b8ee8e8d4b 1246 As explained in the sections about the encoder and decoder, you can specify
embeddedartists 0:83b8ee8e8d4b 1247 color types and bit depths in info_png and info_raw to change the default
embeddedartists 0:83b8ee8e8d4b 1248 behaviour.
embeddedartists 0:83b8ee8e8d4b 1249
embeddedartists 0:83b8ee8e8d4b 1250 If, when decoding, you want the raw image to be something else than the default,
embeddedartists 0:83b8ee8e8d4b 1251 you need to set the color type and bit depth you want in the LodePNGColorMode,
embeddedartists 0:83b8ee8e8d4b 1252 or the parameters of the simple function of LodePNG you're using.
embeddedartists 0:83b8ee8e8d4b 1253
embeddedartists 0:83b8ee8e8d4b 1254 If, when encoding, you use another color type than the default in the input
embeddedartists 0:83b8ee8e8d4b 1255 image, you need to specify its color type and bit depth in the LodePNGColorMode
embeddedartists 0:83b8ee8e8d4b 1256 of the raw image, or use the parameters of the simplefunction of LodePNG you're
embeddedartists 0:83b8ee8e8d4b 1257 using.
embeddedartists 0:83b8ee8e8d4b 1258
embeddedartists 0:83b8ee8e8d4b 1259 If, when encoding, you don't want LodePNG to choose the output PNG color type
embeddedartists 0:83b8ee8e8d4b 1260 but control it yourself, you need to set auto_convert in the encoder settings
embeddedartists 0:83b8ee8e8d4b 1261 to LAC_NONE, and specify the color type you want in the LodePNGInfo of the
embeddedartists 0:83b8ee8e8d4b 1262 encoder.
embeddedartists 0:83b8ee8e8d4b 1263
embeddedartists 0:83b8ee8e8d4b 1264 If you do any of the above, LodePNG may need to do a color conversion, which
embeddedartists 0:83b8ee8e8d4b 1265 follows the rules below, and may sometimes not be allowed.
embeddedartists 0:83b8ee8e8d4b 1266
embeddedartists 0:83b8ee8e8d4b 1267 To avoid some confusion:
embeddedartists 0:83b8ee8e8d4b 1268 -the decoder converts from PNG to raw image
embeddedartists 0:83b8ee8e8d4b 1269 -the encoder converts from raw image to PNG
embeddedartists 0:83b8ee8e8d4b 1270 -the colortype and bitdepth in LodePNGColorMode info_raw, are those of the raw image
embeddedartists 0:83b8ee8e8d4b 1271 -the colortype and bitdepth in the color field of LodePNGInfo info_png, are those of the PNG
embeddedartists 0:83b8ee8e8d4b 1272 -when encoding, the color type in LodePNGInfo is ignored if auto_convert
embeddedartists 0:83b8ee8e8d4b 1273 is enabled, it is automatically generated instead
embeddedartists 0:83b8ee8e8d4b 1274 -when decoding, the color type in LodePNGInfo is set by the decoder to that of the original
embeddedartists 0:83b8ee8e8d4b 1275 PNG image, but it can be ignored since the raw image has the color type you requested instead
embeddedartists 0:83b8ee8e8d4b 1276 -if the color type of the LodePNGColorMode and PNG image aren't the same, a conversion
embeddedartists 0:83b8ee8e8d4b 1277 between the color types is done if the color types are supported. If it is not
embeddedartists 0:83b8ee8e8d4b 1278 supported, an error is returned. If the types are the same, no conversion is done.
embeddedartists 0:83b8ee8e8d4b 1279 -even though some conversions aren't supported, LodePNG supports loading PNGs from any
embeddedartists 0:83b8ee8e8d4b 1280 colortype and saving PNGs to any colortype, sometimes it just requires preparing
embeddedartists 0:83b8ee8e8d4b 1281 the raw image correctly before encoding.
embeddedartists 0:83b8ee8e8d4b 1282 -both encoder and decoder use the same color converter.
embeddedartists 0:83b8ee8e8d4b 1283
embeddedartists 0:83b8ee8e8d4b 1284 Non supported color conversions:
embeddedartists 0:83b8ee8e8d4b 1285 -color to greyscale: no error is thrown, but the result will look ugly because
embeddedartists 0:83b8ee8e8d4b 1286 only the red channel is taken
embeddedartists 0:83b8ee8e8d4b 1287 -anything, to palette when that palette does not have that color in it: in this
embeddedartists 0:83b8ee8e8d4b 1288 case an error is thrown
embeddedartists 0:83b8ee8e8d4b 1289
embeddedartists 0:83b8ee8e8d4b 1290 Supported color conversions:
embeddedartists 0:83b8ee8e8d4b 1291 -anything to 8-bit RGB, 8-bit RGBA, 16-bit RGB, 16-bit RGBA
embeddedartists 0:83b8ee8e8d4b 1292 -any grey or grey+alpha, to grey or grey+alpha
embeddedartists 0:83b8ee8e8d4b 1293 -anything to a palette, as long as the palette has the requested colors in it
embeddedartists 0:83b8ee8e8d4b 1294 -removing alpha channel
embeddedartists 0:83b8ee8e8d4b 1295 -higher to smaller bitdepth, and vice versa
embeddedartists 0:83b8ee8e8d4b 1296
embeddedartists 0:83b8ee8e8d4b 1297 If you want no color conversion to be done:
embeddedartists 0:83b8ee8e8d4b 1298 -In the encoder, you can make it save a PNG with any color type by giving the
embeddedartists 0:83b8ee8e8d4b 1299 raw color mode and LodePNGInfo the same color mode, and setting auto_convert to
embeddedartists 0:83b8ee8e8d4b 1300 LAC_NO.
embeddedartists 0:83b8ee8e8d4b 1301 -In the decoder, you can make it store the pixel data in the same color type
embeddedartists 0:83b8ee8e8d4b 1302 as the PNG has, by setting the color_convert setting to false. Settings in
embeddedartists 0:83b8ee8e8d4b 1303 info_raw are then ignored.
embeddedartists 0:83b8ee8e8d4b 1304
embeddedartists 0:83b8ee8e8d4b 1305 The function lodepng_convert does the color conversion. It is available in the
embeddedartists 0:83b8ee8e8d4b 1306 interface but normally isn't needed since the encoder and decoder already call
embeddedartists 0:83b8ee8e8d4b 1307 it.
embeddedartists 0:83b8ee8e8d4b 1308
embeddedartists 0:83b8ee8e8d4b 1309 6.3. padding bits
embeddedartists 0:83b8ee8e8d4b 1310 -----------------
embeddedartists 0:83b8ee8e8d4b 1311
embeddedartists 0:83b8ee8e8d4b 1312 In the PNG file format, if a less than 8-bit per pixel color type is used and the scanlines
embeddedartists 0:83b8ee8e8d4b 1313 have a bit amount that isn't a multiple of 8, then padding bits are used so that each
embeddedartists 0:83b8ee8e8d4b 1314 scanline starts at a fresh byte. But that is NOT true for the LodePNG raw input and output.
embeddedartists 0:83b8ee8e8d4b 1315 The raw input image you give to the encoder, and the raw output image you get from the decoder
embeddedartists 0:83b8ee8e8d4b 1316 will NOT have these padding bits, e.g. in the case of a 1-bit image with a width
embeddedartists 0:83b8ee8e8d4b 1317 of 7 pixels, the first pixel of the second scanline will the the 8th bit of the first byte,
embeddedartists 0:83b8ee8e8d4b 1318 not the first bit of a new byte.
embeddedartists 0:83b8ee8e8d4b 1319
embeddedartists 0:83b8ee8e8d4b 1320 6.4. A note about 16-bits per channel and endianness
embeddedartists 0:83b8ee8e8d4b 1321 ----------------------------------------------------
embeddedartists 0:83b8ee8e8d4b 1322
embeddedartists 0:83b8ee8e8d4b 1323 LodePNG uses unsigned char arrays for 16-bit per channel colors too, just like
embeddedartists 0:83b8ee8e8d4b 1324 for any other color format. The 16-bit values are stored in big endian (most
embeddedartists 0:83b8ee8e8d4b 1325 significant byte first) in these arrays. This is the opposite order of the
embeddedartists 0:83b8ee8e8d4b 1326 little endian used by x86 CPU's.
embeddedartists 0:83b8ee8e8d4b 1327
embeddedartists 0:83b8ee8e8d4b 1328 LodePNG always uses big endian because the PNG file format does so internally.
embeddedartists 0:83b8ee8e8d4b 1329 Conversions to other formats than PNG uses internally are not supported by
embeddedartists 0:83b8ee8e8d4b 1330 LodePNG on purpose, there are myriads of formats, including endianness of 16-bit
embeddedartists 0:83b8ee8e8d4b 1331 colors, the order in which you store R, G, B and A, and so on. Supporting and
embeddedartists 0:83b8ee8e8d4b 1332 converting to/from all that is outside the scope of LodePNG.
embeddedartists 0:83b8ee8e8d4b 1333
embeddedartists 0:83b8ee8e8d4b 1334 This may mean that, depending on your use case, you may want to convert the big
embeddedartists 0:83b8ee8e8d4b 1335 endian output of LodePNG to little endian with a for loop. This is certainly not
embeddedartists 0:83b8ee8e8d4b 1336 always needed, many applications and libraries support big endian 16-bit colors
embeddedartists 0:83b8ee8e8d4b 1337 anyway, but it means you cannot simply cast the unsigned char* buffer to an
embeddedartists 0:83b8ee8e8d4b 1338 unsigned short* buffer on x86 CPUs.
embeddedartists 0:83b8ee8e8d4b 1339
embeddedartists 0:83b8ee8e8d4b 1340
embeddedartists 0:83b8ee8e8d4b 1341 7. error values
embeddedartists 0:83b8ee8e8d4b 1342 ---------------
embeddedartists 0:83b8ee8e8d4b 1343
embeddedartists 0:83b8ee8e8d4b 1344 All functions in LodePNG that return an error code, return 0 if everything went
embeddedartists 0:83b8ee8e8d4b 1345 OK, or a non-zero code if there was an error.
embeddedartists 0:83b8ee8e8d4b 1346
embeddedartists 0:83b8ee8e8d4b 1347 The meaning of the LodePNG error values can be retrieved with the function
embeddedartists 0:83b8ee8e8d4b 1348 lodepng_error_text: given the numerical error code, it returns a description
embeddedartists 0:83b8ee8e8d4b 1349 of the error in English as a string.
embeddedartists 0:83b8ee8e8d4b 1350
embeddedartists 0:83b8ee8e8d4b 1351 Check the implementation of lodepng_error_text to see the meaning of each code.
embeddedartists 0:83b8ee8e8d4b 1352
embeddedartists 0:83b8ee8e8d4b 1353
embeddedartists 0:83b8ee8e8d4b 1354 8. chunks and PNG editing
embeddedartists 0:83b8ee8e8d4b 1355 -------------------------
embeddedartists 0:83b8ee8e8d4b 1356
embeddedartists 0:83b8ee8e8d4b 1357 If you want to add extra chunks to a PNG you encode, or use LodePNG for a PNG
embeddedartists 0:83b8ee8e8d4b 1358 editor that should follow the rules about handling of unknown chunks, or if your
embeddedartists 0:83b8ee8e8d4b 1359 program is able to read other types of chunks than the ones handled by LodePNG,
embeddedartists 0:83b8ee8e8d4b 1360 then that's possible with the chunk functions of LodePNG.
embeddedartists 0:83b8ee8e8d4b 1361
embeddedartists 0:83b8ee8e8d4b 1362 A PNG chunk has the following layout:
embeddedartists 0:83b8ee8e8d4b 1363
embeddedartists 0:83b8ee8e8d4b 1364 4 bytes length
embeddedartists 0:83b8ee8e8d4b 1365 4 bytes type name
embeddedartists 0:83b8ee8e8d4b 1366 length bytes data
embeddedartists 0:83b8ee8e8d4b 1367 4 bytes CRC
embeddedartists 0:83b8ee8e8d4b 1368
embeddedartists 0:83b8ee8e8d4b 1369 8.1. iterating through chunks
embeddedartists 0:83b8ee8e8d4b 1370 -----------------------------
embeddedartists 0:83b8ee8e8d4b 1371
embeddedartists 0:83b8ee8e8d4b 1372 If you have a buffer containing the PNG image data, then the first chunk (the
embeddedartists 0:83b8ee8e8d4b 1373 IHDR chunk) starts at byte number 8 of that buffer. The first 8 bytes are the
embeddedartists 0:83b8ee8e8d4b 1374 signature of the PNG and are not part of a chunk. But if you start at byte 8
embeddedartists 0:83b8ee8e8d4b 1375 then you have a chunk, and can check the following things of it.
embeddedartists 0:83b8ee8e8d4b 1376
embeddedartists 0:83b8ee8e8d4b 1377 NOTE: none of these functions check for memory buffer boundaries. To avoid
embeddedartists 0:83b8ee8e8d4b 1378 exploits, always make sure the buffer contains all the data of the chunks.
embeddedartists 0:83b8ee8e8d4b 1379 When using lodepng_chunk_next, make sure the returned value is within the
embeddedartists 0:83b8ee8e8d4b 1380 allocated memory.
embeddedartists 0:83b8ee8e8d4b 1381
embeddedartists 0:83b8ee8e8d4b 1382 unsigned lodepng_chunk_length(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1383
embeddedartists 0:83b8ee8e8d4b 1384 Get the length of the chunk's data. The total chunk length is this length + 12.
embeddedartists 0:83b8ee8e8d4b 1385
embeddedartists 0:83b8ee8e8d4b 1386 void lodepng_chunk_type(char type[5], const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1387 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type):
embeddedartists 0:83b8ee8e8d4b 1388
embeddedartists 0:83b8ee8e8d4b 1389 Get the type of the chunk or compare if it's a certain type
embeddedartists 0:83b8ee8e8d4b 1390
embeddedartists 0:83b8ee8e8d4b 1391 unsigned char lodepng_chunk_critical(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1392 unsigned char lodepng_chunk_private(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1393 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1394
embeddedartists 0:83b8ee8e8d4b 1395 Check if the chunk is critical in the PNG standard (only IHDR, PLTE, IDAT and IEND are).
embeddedartists 0:83b8ee8e8d4b 1396 Check if the chunk is private (public chunks are part of the standard, private ones not).
embeddedartists 0:83b8ee8e8d4b 1397 Check if the chunk is safe to copy. If it's not, then, when modifying data in a critical
embeddedartists 0:83b8ee8e8d4b 1398 chunk, unsafe to copy chunks of the old image may NOT be saved in the new one if your
embeddedartists 0:83b8ee8e8d4b 1399 program doesn't handle that type of unknown chunk.
embeddedartists 0:83b8ee8e8d4b 1400
embeddedartists 0:83b8ee8e8d4b 1401 unsigned char* lodepng_chunk_data(unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1402 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1403
embeddedartists 0:83b8ee8e8d4b 1404 Get a pointer to the start of the data of the chunk.
embeddedartists 0:83b8ee8e8d4b 1405
embeddedartists 0:83b8ee8e8d4b 1406 unsigned lodepng_chunk_check_crc(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1407 void lodepng_chunk_generate_crc(unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1408
embeddedartists 0:83b8ee8e8d4b 1409 Check if the crc is correct or generate a correct one.
embeddedartists 0:83b8ee8e8d4b 1410
embeddedartists 0:83b8ee8e8d4b 1411 unsigned char* lodepng_chunk_next(unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1412 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1413
embeddedartists 0:83b8ee8e8d4b 1414 Iterate to the next chunk. This works if you have a buffer with consecutive chunks. Note that these
embeddedartists 0:83b8ee8e8d4b 1415 functions do no boundary checking of the allocated data whatsoever, so make sure there is enough
embeddedartists 0:83b8ee8e8d4b 1416 data available in the buffer to be able to go to the next chunk.
embeddedartists 0:83b8ee8e8d4b 1417
embeddedartists 0:83b8ee8e8d4b 1418 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk):
embeddedartists 0:83b8ee8e8d4b 1419 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length,
embeddedartists 0:83b8ee8e8d4b 1420 const char* type, const unsigned char* data):
embeddedartists 0:83b8ee8e8d4b 1421
embeddedartists 0:83b8ee8e8d4b 1422 These functions are used to create new chunks that are appended to the data in *out that has
embeddedartists 0:83b8ee8e8d4b 1423 length *outlength. The append function appends an existing chunk to the new data. The create
embeddedartists 0:83b8ee8e8d4b 1424 function creates a new chunk with the given parameters and appends it. Type is the 4-letter
embeddedartists 0:83b8ee8e8d4b 1425 name of the chunk.
embeddedartists 0:83b8ee8e8d4b 1426
embeddedartists 0:83b8ee8e8d4b 1427 8.2. chunks in info_png
embeddedartists 0:83b8ee8e8d4b 1428 -----------------------
embeddedartists 0:83b8ee8e8d4b 1429
embeddedartists 0:83b8ee8e8d4b 1430 The LodePNGInfo struct contains fields with the unknown chunk in it. It has 3
embeddedartists 0:83b8ee8e8d4b 1431 buffers (each with size) to contain 3 types of unknown chunks:
embeddedartists 0:83b8ee8e8d4b 1432 the ones that come before the PLTE chunk, the ones that come between the PLTE
embeddedartists 0:83b8ee8e8d4b 1433 and the IDAT chunks, and the ones that come after the IDAT chunks.
embeddedartists 0:83b8ee8e8d4b 1434 It's necessary to make the distionction between these 3 cases because the PNG
embeddedartists 0:83b8ee8e8d4b 1435 standard forces to keep the ordering of unknown chunks compared to the critical
embeddedartists 0:83b8ee8e8d4b 1436 chunks, but does not force any other ordering rules.
embeddedartists 0:83b8ee8e8d4b 1437
embeddedartists 0:83b8ee8e8d4b 1438 info_png.unknown_chunks_data[0] is the chunks before PLTE
embeddedartists 0:83b8ee8e8d4b 1439 info_png.unknown_chunks_data[1] is the chunks after PLTE, before IDAT
embeddedartists 0:83b8ee8e8d4b 1440 info_png.unknown_chunks_data[2] is the chunks after IDAT
embeddedartists 0:83b8ee8e8d4b 1441
embeddedartists 0:83b8ee8e8d4b 1442 The chunks in these 3 buffers can be iterated through and read by using the same
embeddedartists 0:83b8ee8e8d4b 1443 way described in the previous subchapter.
embeddedartists 0:83b8ee8e8d4b 1444
embeddedartists 0:83b8ee8e8d4b 1445 When using the decoder to decode a PNG, you can make it store all unknown chunks
embeddedartists 0:83b8ee8e8d4b 1446 if you set the option settings.remember_unknown_chunks to 1. By default, this
embeddedartists 0:83b8ee8e8d4b 1447 option is off (0).
embeddedartists 0:83b8ee8e8d4b 1448
embeddedartists 0:83b8ee8e8d4b 1449 The encoder will always encode unknown chunks that are stored in the info_png.
embeddedartists 0:83b8ee8e8d4b 1450 If you need it to add a particular chunk that isn't known by LodePNG, you can
embeddedartists 0:83b8ee8e8d4b 1451 use lodepng_chunk_append or lodepng_chunk_create to the chunk data in
embeddedartists 0:83b8ee8e8d4b 1452 info_png.unknown_chunks_data[x].
embeddedartists 0:83b8ee8e8d4b 1453
embeddedartists 0:83b8ee8e8d4b 1454 Chunks that are known by LodePNG should not be added in that way. E.g. to make
embeddedartists 0:83b8ee8e8d4b 1455 LodePNG add a bKGD chunk, set background_defined to true and add the correct
embeddedartists 0:83b8ee8e8d4b 1456 parameters there instead.
embeddedartists 0:83b8ee8e8d4b 1457
embeddedartists 0:83b8ee8e8d4b 1458
embeddedartists 0:83b8ee8e8d4b 1459 9. compiler support
embeddedartists 0:83b8ee8e8d4b 1460 -------------------
embeddedartists 0:83b8ee8e8d4b 1461
embeddedartists 0:83b8ee8e8d4b 1462 No libraries other than the current standard C library are needed to compile
embeddedartists 0:83b8ee8e8d4b 1463 LodePNG. For the C++ version, only the standard C++ library is needed on top.
embeddedartists 0:83b8ee8e8d4b 1464 Add the files lodepng.c(pp) and lodepng.h to your project, include
embeddedartists 0:83b8ee8e8d4b 1465 lodepng.h where needed, and your program can read/write PNG files.
embeddedartists 0:83b8ee8e8d4b 1466
embeddedartists 0:83b8ee8e8d4b 1467 If performance is important, use optimization when compiling! For both the
embeddedartists 0:83b8ee8e8d4b 1468 encoder and decoder, this makes a large difference.
embeddedartists 0:83b8ee8e8d4b 1469
embeddedartists 0:83b8ee8e8d4b 1470 Make sure that LodePNG is compiled with the same compiler of the same version
embeddedartists 0:83b8ee8e8d4b 1471 and with the same settings as the rest of the program, or the interfaces with
embeddedartists 0:83b8ee8e8d4b 1472 std::vectors and std::strings in C++ can be incompatible.
embeddedartists 0:83b8ee8e8d4b 1473
embeddedartists 0:83b8ee8e8d4b 1474 CHAR_BITS must be 8 or higher, because LodePNG uses unsigned chars for octets.
embeddedartists 0:83b8ee8e8d4b 1475
embeddedartists 0:83b8ee8e8d4b 1476 *) gcc and g++
embeddedartists 0:83b8ee8e8d4b 1477
embeddedartists 0:83b8ee8e8d4b 1478 LodePNG is developed in gcc so this compiler is natively supported. It gives no
embeddedartists 0:83b8ee8e8d4b 1479 warnings with compiler options "-Wall -Wextra -pedantic -ansi", with gcc and g++
embeddedartists 0:83b8ee8e8d4b 1480 version 4.7.1 on Linux, 32-bit and 64-bit.
embeddedartists 0:83b8ee8e8d4b 1481
embeddedartists 0:83b8ee8e8d4b 1482 *) Mingw
embeddedartists 0:83b8ee8e8d4b 1483
embeddedartists 0:83b8ee8e8d4b 1484 The Mingw compiler (a port of gcc) for Windows is fully supported by LodePNG.
embeddedartists 0:83b8ee8e8d4b 1485
embeddedartists 0:83b8ee8e8d4b 1486 *) Visual Studio 2005 and up, Visual C++ Express Edition 2005 and up
embeddedartists 0:83b8ee8e8d4b 1487
embeddedartists 0:83b8ee8e8d4b 1488 Visual Studio may give warnings about 'fopen' being deprecated. A multiplatform library
embeddedartists 0:83b8ee8e8d4b 1489 can't support the proposed Visual Studio alternative however, so LodePNG keeps using
embeddedartists 0:83b8ee8e8d4b 1490 fopen. If you don't want to see the deprecated warnings, put this on top of lodepng.h
embeddedartists 0:83b8ee8e8d4b 1491 before the inclusions:
embeddedartists 0:83b8ee8e8d4b 1492 #define _CRT_SECURE_NO_DEPRECATE
embeddedartists 0:83b8ee8e8d4b 1493
embeddedartists 0:83b8ee8e8d4b 1494 Other than the above warnings, LodePNG should be warning-free with warning
embeddedartists 0:83b8ee8e8d4b 1495 level 3 (W3). Warning level 4 (W4) will give warnings about integer conversions.
embeddedartists 0:83b8ee8e8d4b 1496 I'm not planning to resolve these warnings. To get rid of them, let Visual
embeddedartists 0:83b8ee8e8d4b 1497 Studio use warning level W3 for lodepng.cpp only: right click lodepng.cpp,
embeddedartists 0:83b8ee8e8d4b 1498 Properties, C/C++, General, Warning Level: Level 3 (/W3).
embeddedartists 0:83b8ee8e8d4b 1499
embeddedartists 0:83b8ee8e8d4b 1500 Visual Studio may want "stdafx.h" files to be included in each source file and
embeddedartists 0:83b8ee8e8d4b 1501 give an error "unexpected end of file while looking for precompiled header".
embeddedartists 0:83b8ee8e8d4b 1502 That is not standard C++ and will not be added to the stock LodePNG. You can
embeddedartists 0:83b8ee8e8d4b 1503 disable it for lodepng.cpp only by right clicking it, Properties, C/C++,
embeddedartists 0:83b8ee8e8d4b 1504 Precompiled Headers, and set it to Not Using Precompiled Headers there.
embeddedartists 0:83b8ee8e8d4b 1505
embeddedartists 0:83b8ee8e8d4b 1506 *) Visual Studio 6.0
embeddedartists 0:83b8ee8e8d4b 1507
embeddedartists 0:83b8ee8e8d4b 1508 LodePNG support for Visual Studio 6.0 is not guaranteed because VS6 doesn't
embeddedartists 0:83b8ee8e8d4b 1509 follow the C++ standard correctly.
embeddedartists 0:83b8ee8e8d4b 1510
embeddedartists 0:83b8ee8e8d4b 1511 *) Comeau C/C++
embeddedartists 0:83b8ee8e8d4b 1512
embeddedartists 0:83b8ee8e8d4b 1513 Vesion 20070107 compiles without problems on the Comeau C/C++ Online Test Drive
embeddedartists 0:83b8ee8e8d4b 1514 at http://www.comeaucomputing.com/tryitout in both C90 and C++ mode.
embeddedartists 0:83b8ee8e8d4b 1515
embeddedartists 0:83b8ee8e8d4b 1516 *) Compilers on Macintosh
embeddedartists 0:83b8ee8e8d4b 1517
embeddedartists 0:83b8ee8e8d4b 1518 LodePNG has been reported to work both with the gcc and LLVM for Macintosh, both
embeddedartists 0:83b8ee8e8d4b 1519 for C and C++.
embeddedartists 0:83b8ee8e8d4b 1520
embeddedartists 0:83b8ee8e8d4b 1521 *) Other Compilers
embeddedartists 0:83b8ee8e8d4b 1522
embeddedartists 0:83b8ee8e8d4b 1523 If you encounter problems on other compilers, feel free to let me know and I may
embeddedartists 0:83b8ee8e8d4b 1524 try to fix it if the compiler is modern standards complient.
embeddedartists 0:83b8ee8e8d4b 1525
embeddedartists 0:83b8ee8e8d4b 1526
embeddedartists 0:83b8ee8e8d4b 1527 10. examples
embeddedartists 0:83b8ee8e8d4b 1528 ------------
embeddedartists 0:83b8ee8e8d4b 1529
embeddedartists 0:83b8ee8e8d4b 1530 This decoder example shows the most basic usage of LodePNG. More complex
embeddedartists 0:83b8ee8e8d4b 1531 examples can be found on the LodePNG website.
embeddedartists 0:83b8ee8e8d4b 1532
embeddedartists 0:83b8ee8e8d4b 1533 10.1. decoder C++ example
embeddedartists 0:83b8ee8e8d4b 1534 -------------------------
embeddedartists 0:83b8ee8e8d4b 1535
embeddedartists 0:83b8ee8e8d4b 1536 #include "lodepng.h"
embeddedartists 0:83b8ee8e8d4b 1537 #include <iostream>
embeddedartists 0:83b8ee8e8d4b 1538
embeddedartists 0:83b8ee8e8d4b 1539 int main(int argc, char *argv[])
embeddedartists 0:83b8ee8e8d4b 1540 {
embeddedartists 0:83b8ee8e8d4b 1541 const char* filename = argc > 1 ? argv[1] : "test.png";
embeddedartists 0:83b8ee8e8d4b 1542
embeddedartists 0:83b8ee8e8d4b 1543 //load and decode
embeddedartists 0:83b8ee8e8d4b 1544 std::vector<unsigned char> image;
embeddedartists 0:83b8ee8e8d4b 1545 unsigned width, height;
embeddedartists 0:83b8ee8e8d4b 1546 unsigned error = lodepng::decode(image, width, height, filename);
embeddedartists 0:83b8ee8e8d4b 1547
embeddedartists 0:83b8ee8e8d4b 1548 //if there's an error, display it
embeddedartists 0:83b8ee8e8d4b 1549 if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
embeddedartists 0:83b8ee8e8d4b 1550
embeddedartists 0:83b8ee8e8d4b 1551 //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
embeddedartists 0:83b8ee8e8d4b 1552 }
embeddedartists 0:83b8ee8e8d4b 1553
embeddedartists 0:83b8ee8e8d4b 1554 10.2. decoder C example
embeddedartists 0:83b8ee8e8d4b 1555 -----------------------
embeddedartists 0:83b8ee8e8d4b 1556
embeddedartists 0:83b8ee8e8d4b 1557 #include "lodepng.h"
embeddedartists 0:83b8ee8e8d4b 1558
embeddedartists 0:83b8ee8e8d4b 1559 int main(int argc, char *argv[])
embeddedartists 0:83b8ee8e8d4b 1560 {
embeddedartists 0:83b8ee8e8d4b 1561 unsigned error;
embeddedartists 0:83b8ee8e8d4b 1562 unsigned char* image;
embeddedartists 0:83b8ee8e8d4b 1563 size_t width, height;
embeddedartists 0:83b8ee8e8d4b 1564 const char* filename = argc > 1 ? argv[1] : "test.png";
embeddedartists 0:83b8ee8e8d4b 1565
embeddedartists 0:83b8ee8e8d4b 1566 error = lodepng_decode32_file(&image, &width, &height, filename);
embeddedartists 0:83b8ee8e8d4b 1567
embeddedartists 0:83b8ee8e8d4b 1568 if(error) printf("decoder error %u: %s\n", error, lodepng_error_text(error));
embeddedartists 0:83b8ee8e8d4b 1569
embeddedartists 0:83b8ee8e8d4b 1570 / * use image here * /
embeddedartists 0:83b8ee8e8d4b 1571
embeddedartists 0:83b8ee8e8d4b 1572 free(image);
embeddedartists 0:83b8ee8e8d4b 1573 return 0;
embeddedartists 0:83b8ee8e8d4b 1574 }
embeddedartists 0:83b8ee8e8d4b 1575
embeddedartists 0:83b8ee8e8d4b 1576
embeddedartists 0:83b8ee8e8d4b 1577 11. changes
embeddedartists 0:83b8ee8e8d4b 1578 -----------
embeddedartists 0:83b8ee8e8d4b 1579
embeddedartists 0:83b8ee8e8d4b 1580 The version number of LodePNG is the date of the change given in the format
embeddedartists 0:83b8ee8e8d4b 1581 yyyymmdd.
embeddedartists 0:83b8ee8e8d4b 1582
embeddedartists 0:83b8ee8e8d4b 1583 Some changes aren't backwards compatible. Those are indicated with a (!)
embeddedartists 0:83b8ee8e8d4b 1584 symbol.
embeddedartists 0:83b8ee8e8d4b 1585
embeddedartists 0:83b8ee8e8d4b 1586 *) 22 dec 2013: Power of two windowsize required for optimization.
embeddedartists 0:83b8ee8e8d4b 1587 *) 15 apr 2013: Fixed bug with LAC_ALPHA and color key.
embeddedartists 0:83b8ee8e8d4b 1588 *) 25 mar 2013: Added an optional feature to ignore some PNG errors (fix_png).
embeddedartists 0:83b8ee8e8d4b 1589 *) 11 mar 2013 (!): Bugfix with custom free. Changed from "my" to "lodepng_"
embeddedartists 0:83b8ee8e8d4b 1590 prefix for the custom allocators and made it possible with a new #define to
embeddedartists 0:83b8ee8e8d4b 1591 use custom ones in your project without needing to change lodepng's code.
embeddedartists 0:83b8ee8e8d4b 1592 *) 28 jan 2013: Bugfix with color key.
embeddedartists 0:83b8ee8e8d4b 1593 *) 27 okt 2012: Tweaks in text chunk keyword length error handling.
embeddedartists 0:83b8ee8e8d4b 1594 *) 8 okt 2012 (!): Added new filter strategy (entropy) and new auto color mode.
embeddedartists 0:83b8ee8e8d4b 1595 (no palette). Better deflate tree encoding. New compression tweak settings.
embeddedartists 0:83b8ee8e8d4b 1596 Faster color conversions while decoding. Some internal cleanups.
embeddedartists 0:83b8ee8e8d4b 1597 *) 23 sep 2012: Reduced warnings in Visual Studio a little bit.
embeddedartists 0:83b8ee8e8d4b 1598 *) 1 sep 2012 (!): Removed #define's for giving custom (de)compression functions
embeddedartists 0:83b8ee8e8d4b 1599 and made it work with function pointers instead.
embeddedartists 0:83b8ee8e8d4b 1600 *) 23 jun 2012: Added more filter strategies. Made it easier to use custom alloc
embeddedartists 0:83b8ee8e8d4b 1601 and free functions and toggle #defines from compiler flags. Small fixes.
embeddedartists 0:83b8ee8e8d4b 1602 *) 6 may 2012 (!): Made plugging in custom zlib/deflate functions more flexible.
embeddedartists 0:83b8ee8e8d4b 1603 *) 22 apr 2012 (!): Made interface more consistent, renaming a lot. Removed
embeddedartists 0:83b8ee8e8d4b 1604 redundant C++ codec classes. Reduced amount of structs. Everything changed,
embeddedartists 0:83b8ee8e8d4b 1605 but it is cleaner now imho and functionality remains the same. Also fixed
embeddedartists 0:83b8ee8e8d4b 1606 several bugs and shrinked the implementation code. Made new samples.
embeddedartists 0:83b8ee8e8d4b 1607 *) 6 nov 2011 (!): By default, the encoder now automatically chooses the best
embeddedartists 0:83b8ee8e8d4b 1608 PNG color model and bit depth, based on the amount and type of colors of the
embeddedartists 0:83b8ee8e8d4b 1609 raw image. For this, autoLeaveOutAlphaChannel replaced by auto_choose_color.
embeddedartists 0:83b8ee8e8d4b 1610 *) 9 okt 2011: simpler hash chain implementation for the encoder.
embeddedartists 0:83b8ee8e8d4b 1611 *) 8 sep 2011: lz77 encoder lazy matching instead of greedy matching.
embeddedartists 0:83b8ee8e8d4b 1612 *) 23 aug 2011: tweaked the zlib compression parameters after benchmarking.
embeddedartists 0:83b8ee8e8d4b 1613 A bug with the PNG filtertype heuristic was fixed, so that it chooses much
embeddedartists 0:83b8ee8e8d4b 1614 better ones (it's quite significant). A setting to do an experimental, slow,
embeddedartists 0:83b8ee8e8d4b 1615 brute force search for PNG filter types is added.
embeddedartists 0:83b8ee8e8d4b 1616 *) 17 aug 2011 (!): changed some C zlib related function names.
embeddedartists 0:83b8ee8e8d4b 1617 *) 16 aug 2011: made the code less wide (max 120 characters per line).
embeddedartists 0:83b8ee8e8d4b 1618 *) 17 apr 2011: code cleanup. Bugfixes. Convert low to 16-bit per sample colors.
embeddedartists 0:83b8ee8e8d4b 1619 *) 21 feb 2011: fixed compiling for C90. Fixed compiling with sections disabled.
embeddedartists 0:83b8ee8e8d4b 1620 *) 11 dec 2010: encoding is made faster, based on suggestion by Peter Eastman
embeddedartists 0:83b8ee8e8d4b 1621 to optimize long sequences of zeros.
embeddedartists 0:83b8ee8e8d4b 1622 *) 13 nov 2010: added LodePNG_InfoColor_hasPaletteAlpha and
embeddedartists 0:83b8ee8e8d4b 1623 LodePNG_InfoColor_canHaveAlpha functions for convenience.
embeddedartists 0:83b8ee8e8d4b 1624 *) 7 nov 2010: added LodePNG_error_text function to get error code description.
embeddedartists 0:83b8ee8e8d4b 1625 *) 30 okt 2010: made decoding slightly faster
embeddedartists 0:83b8ee8e8d4b 1626 *) 26 okt 2010: (!) changed some C function and struct names (more consistent).
embeddedartists 0:83b8ee8e8d4b 1627 Reorganized the documentation and the declaration order in the header.
embeddedartists 0:83b8ee8e8d4b 1628 *) 08 aug 2010: only changed some comments and external samples.
embeddedartists 0:83b8ee8e8d4b 1629 *) 05 jul 2010: fixed bug thanks to warnings in the new gcc version.
embeddedartists 0:83b8ee8e8d4b 1630 *) 14 mar 2010: fixed bug where too much memory was allocated for char buffers.
embeddedartists 0:83b8ee8e8d4b 1631 *) 02 sep 2008: fixed bug where it could create empty tree that linux apps could
embeddedartists 0:83b8ee8e8d4b 1632 read by ignoring the problem but windows apps couldn't.
embeddedartists 0:83b8ee8e8d4b 1633 *) 06 jun 2008: added more error checks for out of memory cases.
embeddedartists 0:83b8ee8e8d4b 1634 *) 26 apr 2008: added a few more checks here and there to ensure more safety.
embeddedartists 0:83b8ee8e8d4b 1635 *) 06 mar 2008: crash with encoding of strings fixed
embeddedartists 0:83b8ee8e8d4b 1636 *) 02 feb 2008: support for international text chunks added (iTXt)
embeddedartists 0:83b8ee8e8d4b 1637 *) 23 jan 2008: small cleanups, and #defines to divide code in sections
embeddedartists 0:83b8ee8e8d4b 1638 *) 20 jan 2008: support for unknown chunks allowing using LodePNG for an editor.
embeddedartists 0:83b8ee8e8d4b 1639 *) 18 jan 2008: support for tIME and pHYs chunks added to encoder and decoder.
embeddedartists 0:83b8ee8e8d4b 1640 *) 17 jan 2008: ability to encode and decode compressed zTXt chunks added
embeddedartists 0:83b8ee8e8d4b 1641 Also vareous fixes, such as in the deflate and the padding bits code.
embeddedartists 0:83b8ee8e8d4b 1642 *) 13 jan 2008: Added ability to encode Adam7-interlaced images. Improved
embeddedartists 0:83b8ee8e8d4b 1643 filtering code of encoder.
embeddedartists 0:83b8ee8e8d4b 1644 *) 07 jan 2008: (!) changed LodePNG to use ISO C90 instead of C++. A
embeddedartists 0:83b8ee8e8d4b 1645 C++ wrapper around this provides an interface almost identical to before.
embeddedartists 0:83b8ee8e8d4b 1646 Having LodePNG be pure ISO C90 makes it more portable. The C and C++ code
embeddedartists 0:83b8ee8e8d4b 1647 are together in these files but it works both for C and C++ compilers.
embeddedartists 0:83b8ee8e8d4b 1648 *) 29 dec 2007: (!) changed most integer types to unsigned int + other tweaks
embeddedartists 0:83b8ee8e8d4b 1649 *) 30 aug 2007: bug fixed which makes this Borland C++ compatible
embeddedartists 0:83b8ee8e8d4b 1650 *) 09 aug 2007: some VS2005 warnings removed again
embeddedartists 0:83b8ee8e8d4b 1651 *) 21 jul 2007: deflate code placed in new namespace separate from zlib code
embeddedartists 0:83b8ee8e8d4b 1652 *) 08 jun 2007: fixed bug with 2- and 4-bit color, and small interlaced images
embeddedartists 0:83b8ee8e8d4b 1653 *) 04 jun 2007: improved support for Visual Studio 2005: crash with accessing
embeddedartists 0:83b8ee8e8d4b 1654 invalid std::vector element [0] fixed, and level 3 and 4 warnings removed
embeddedartists 0:83b8ee8e8d4b 1655 *) 02 jun 2007: made the encoder add a tag with version by default
embeddedartists 0:83b8ee8e8d4b 1656 *) 27 may 2007: zlib and png code separated (but still in the same file),
embeddedartists 0:83b8ee8e8d4b 1657 simple encoder/decoder functions added for more simple usage cases
embeddedartists 0:83b8ee8e8d4b 1658 *) 19 may 2007: minor fixes, some code cleaning, new error added (error 69),
embeddedartists 0:83b8ee8e8d4b 1659 moved some examples from here to lodepng_examples.cpp
embeddedartists 0:83b8ee8e8d4b 1660 *) 12 may 2007: palette decoding bug fixed
embeddedartists 0:83b8ee8e8d4b 1661 *) 24 apr 2007: changed the license from BSD to the zlib license
embeddedartists 0:83b8ee8e8d4b 1662 *) 11 mar 2007: very simple addition: ability to encode bKGD chunks.
embeddedartists 0:83b8ee8e8d4b 1663 *) 04 mar 2007: (!) tEXt chunk related fixes, and support for encoding
embeddedartists 0:83b8ee8e8d4b 1664 palettized PNG images. Plus little interface change with palette and texts.
embeddedartists 0:83b8ee8e8d4b 1665 *) 03 mar 2007: Made it encode dynamic Huffman shorter with repeat codes.
embeddedartists 0:83b8ee8e8d4b 1666 Fixed a bug where the end code of a block had length 0 in the Huffman tree.
embeddedartists 0:83b8ee8e8d4b 1667 *) 26 feb 2007: Huffman compression with dynamic trees (BTYPE 2) now implemented
embeddedartists 0:83b8ee8e8d4b 1668 and supported by the encoder, resulting in smaller PNGs at the output.
embeddedartists 0:83b8ee8e8d4b 1669 *) 27 jan 2007: Made the Adler-32 test faster so that a timewaste is gone.
embeddedartists 0:83b8ee8e8d4b 1670 *) 24 jan 2007: gave encoder an error interface. Added color conversion from any
embeddedartists 0:83b8ee8e8d4b 1671 greyscale type to 8-bit greyscale with or without alpha.
embeddedartists 0:83b8ee8e8d4b 1672 *) 21 jan 2007: (!) Totally changed the interface. It allows more color types
embeddedartists 0:83b8ee8e8d4b 1673 to convert to and is more uniform. See the manual for how it works now.
embeddedartists 0:83b8ee8e8d4b 1674 *) 07 jan 2007: Some cleanup & fixes, and a few changes over the last days:
embeddedartists 0:83b8ee8e8d4b 1675 encode/decode custom tEXt chunks, separate classes for zlib & deflate, and
embeddedartists 0:83b8ee8e8d4b 1676 at last made the decoder give errors for incorrect Adler32 or Crc.
embeddedartists 0:83b8ee8e8d4b 1677 *) 01 jan 2007: Fixed bug with encoding PNGs with less than 8 bits per channel.
embeddedartists 0:83b8ee8e8d4b 1678 *) 29 dec 2006: Added support for encoding images without alpha channel, and
embeddedartists 0:83b8ee8e8d4b 1679 cleaned out code as well as making certain parts faster.
embeddedartists 0:83b8ee8e8d4b 1680 *) 28 dec 2006: Added "Settings" to the encoder.
embeddedartists 0:83b8ee8e8d4b 1681 *) 26 dec 2006: The encoder now does LZ77 encoding and produces much smaller files now.
embeddedartists 0:83b8ee8e8d4b 1682 Removed some code duplication in the decoder. Fixed little bug in an example.
embeddedartists 0:83b8ee8e8d4b 1683 *) 09 dec 2006: (!) Placed output parameters of public functions as first parameter.
embeddedartists 0:83b8ee8e8d4b 1684 Fixed a bug of the decoder with 16-bit per color.
embeddedartists 0:83b8ee8e8d4b 1685 *) 15 okt 2006: Changed documentation structure
embeddedartists 0:83b8ee8e8d4b 1686 *) 09 okt 2006: Encoder class added. It encodes a valid PNG image from the
embeddedartists 0:83b8ee8e8d4b 1687 given image buffer, however for now it's not compressed.
embeddedartists 0:83b8ee8e8d4b 1688 *) 08 sep 2006: (!) Changed to interface with a Decoder class
embeddedartists 0:83b8ee8e8d4b 1689 *) 30 jul 2006: (!) LodePNG_InfoPng , width and height are now retrieved in different
embeddedartists 0:83b8ee8e8d4b 1690 way. Renamed decodePNG to decodePNGGeneric.
embeddedartists 0:83b8ee8e8d4b 1691 *) 29 jul 2006: (!) Changed the interface: image info is now returned as a
embeddedartists 0:83b8ee8e8d4b 1692 struct of type LodePNG::LodePNG_Info, instead of a vector, which was a bit clumsy.
embeddedartists 0:83b8ee8e8d4b 1693 *) 28 jul 2006: Cleaned the code and added new error checks.
embeddedartists 0:83b8ee8e8d4b 1694 Corrected terminology "deflate" into "inflate".
embeddedartists 0:83b8ee8e8d4b 1695 *) 23 jun 2006: Added SDL example in the documentation in the header, this
embeddedartists 0:83b8ee8e8d4b 1696 example allows easy debugging by displaying the PNG and its transparency.
embeddedartists 0:83b8ee8e8d4b 1697 *) 22 jun 2006: (!) Changed way to obtain error value. Added
embeddedartists 0:83b8ee8e8d4b 1698 loadFile function for convenience. Made decodePNG32 faster.
embeddedartists 0:83b8ee8e8d4b 1699 *) 21 jun 2006: (!) Changed type of info vector to unsigned.
embeddedartists 0:83b8ee8e8d4b 1700 Changed position of palette in info vector. Fixed an important bug that
embeddedartists 0:83b8ee8e8d4b 1701 happened on PNGs with an uncompressed block.
embeddedartists 0:83b8ee8e8d4b 1702 *) 16 jun 2006: Internally changed unsigned into unsigned where
embeddedartists 0:83b8ee8e8d4b 1703 needed, and performed some optimizations.
embeddedartists 0:83b8ee8e8d4b 1704 *) 07 jun 2006: (!) Renamed functions to decodePNG and placed them
embeddedartists 0:83b8ee8e8d4b 1705 in LodePNG namespace. Changed the order of the parameters. Rewrote the
embeddedartists 0:83b8ee8e8d4b 1706 documentation in the header. Renamed files to lodepng.cpp and lodepng.h
embeddedartists 0:83b8ee8e8d4b 1707 *) 22 apr 2006: Optimized and improved some code
embeddedartists 0:83b8ee8e8d4b 1708 *) 07 sep 2005: (!) Changed to std::vector interface
embeddedartists 0:83b8ee8e8d4b 1709 *) 12 aug 2005: Initial release (C++, decoder only)
embeddedartists 0:83b8ee8e8d4b 1710
embeddedartists 0:83b8ee8e8d4b 1711
embeddedartists 0:83b8ee8e8d4b 1712 12. contact information
embeddedartists 0:83b8ee8e8d4b 1713 -----------------------
embeddedartists 0:83b8ee8e8d4b 1714
embeddedartists 0:83b8ee8e8d4b 1715 Feel free to contact me with suggestions, problems, comments, ... concerning
embeddedartists 0:83b8ee8e8d4b 1716 LodePNG. If you encounter a PNG image that doesn't work properly with this
embeddedartists 0:83b8ee8e8d4b 1717 decoder, feel free to send it and I'll use it to find and fix the problem.
embeddedartists 0:83b8ee8e8d4b 1718
embeddedartists 0:83b8ee8e8d4b 1719 My email address is (puzzle the account and domain together with an @ symbol):
embeddedartists 0:83b8ee8e8d4b 1720 Domain: gmail dot com.
embeddedartists 0:83b8ee8e8d4b 1721 Account: lode dot vandevenne.
embeddedartists 0:83b8ee8e8d4b 1722
embeddedartists 0:83b8ee8e8d4b 1723
embeddedartists 0:83b8ee8e8d4b 1724 Copyright (c) 2005-2013 Lode Vandevenne
embeddedartists 0:83b8ee8e8d4b 1725 */