39 #define LODEPNG_COMPILE_ZLIB 40 #define LODEPNG_COMPILE_PNG 42 #define LODEPNG_COMPILE_ENCODER 43 #define LODEPNG_COMPILE_DISK 51 #ifdef LODEPNG_COMPILE_DECODER 52 typedef struct LodeZlib_DecompressSettings
54 unsigned ignoreAdler32;
55 } LodeZlib_DecompressSettings;
57 extern const LodeZlib_DecompressSettings LodeZlib_defaultDecompressSettings;
58 void LodeZlib_DecompressSettings_init(LodeZlib_DecompressSettings* settings);
61 #ifdef LODEPNG_COMPILE_ENCODER 74 #ifdef LODEPNG_COMPILE_ZLIB 79 #ifdef LODEPNG_COMPILE_DECODER 82 unsigned LodeZlib_decompress(
unsigned char** out,
size_t* outsize,
const unsigned char* in,
size_t insize,
const LodeZlib_DecompressSettings* settings);
85 #ifdef LODEPNG_COMPILE_ENCODER 92 #ifdef LODEPNG_COMPILE_PNG 121 unsigned LodePNG_append_chunk(
unsigned char** out,
size_t* outlength,
const unsigned char* chunk);
155 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 156 typedef struct LodePNG_Time
162 unsigned char minute;
166 typedef struct LodePNG_Text
173 void LodePNG_Text_init(LodePNG_Text* text);
174 void LodePNG_Text_cleanup(LodePNG_Text* text);
175 unsigned LodePNG_Text_copy(LodePNG_Text*
dest,
const LodePNG_Text* source);
178 void LodePNG_Text_clear(LodePNG_Text* text);
179 unsigned LodePNG_Text_add(LodePNG_Text* text,
const char*
key,
const char*
str);
182 typedef struct LodePNG_IText
191 void LodePNG_IText_init(LodePNG_IText* text);
192 void LodePNG_IText_cleanup(LodePNG_IText* text);
193 unsigned LodePNG_IText_copy(LodePNG_IText*
dest,
const LodePNG_IText* source);
196 void LodePNG_IText_clear(LodePNG_IText* text);
197 unsigned LodePNG_IText_add(LodePNG_IText* text,
const char*
key,
const char* langtag,
const char* transkey,
const char*
str);
200 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS 201 typedef struct LodePNG_UnknownChunks
207 unsigned char*
data[3];
210 } LodePNG_UnknownChunks;
212 void LodePNG_UnknownChunks_init(LodePNG_UnknownChunks* chunks);
213 void LodePNG_UnknownChunks_cleanup(LodePNG_UnknownChunks* chunks);
214 unsigned LodePNG_UnknownChunks_copy(LodePNG_UnknownChunks*
dest,
const LodePNG_UnknownChunks* src);
227 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 230 unsigned background_defined;
231 unsigned background_r;
232 unsigned background_g;
233 unsigned background_b;
242 unsigned char time_defined;
246 unsigned phys_defined;
249 unsigned char phys_unit;
253 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS 255 LodePNG_UnknownChunks unknown_chunks;
279 #ifdef LODEPNG_COMPILE_DECODER 281 typedef struct LodePNG_DecodeSettings
283 LodeZlib_DecompressSettings zlibsettings;
286 unsigned color_convert;
288 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 289 unsigned readTextChunks;
292 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS 293 unsigned rememberUnknownChunks;
295 } LodePNG_DecodeSettings;
297 void LodePNG_DecodeSettings_init(LodePNG_DecodeSettings* settings);
299 typedef struct LodePNG_Decoder
301 LodePNG_DecodeSettings settings;
307 void LodePNG_Decoder_init(LodePNG_Decoder*
decoder);
308 void LodePNG_Decoder_cleanup(LodePNG_Decoder*
decoder);
309 void LodePNG_Decoder_copy(LodePNG_Decoder*
dest,
const LodePNG_Decoder* source);
313 void LodePNG_decode(LodePNG_Decoder*
decoder,
unsigned char** out,
size_t* outsize,
const unsigned char* in,
size_t insize);
314 unsigned LodePNG_decode32(
unsigned char** out,
unsigned*
w,
unsigned*
h,
const unsigned char* in,
size_t insize);
315 #ifdef LODEPNG_COMPILE_DISK 316 unsigned LodePNG_decode32f(
unsigned char** out,
unsigned*
w,
unsigned*
h,
const char*
filename);
318 void LodePNG_inspect(LodePNG_Decoder*
decoder,
const unsigned char* in,
size_t size);
322 #ifdef LODEPNG_COMPILE_ENCODER 330 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 332 unsigned text_compression;
352 unsigned LodePNG_encode32(
unsigned char** out,
size_t* outsize,
const unsigned char* image,
unsigned w,
unsigned h);
353 #ifdef LODEPNG_COMPILE_DISK 359 #ifdef LODEPNG_COMPILE_DISK 1361 #include "lodepng.h"
1364 int main(int argc, char *argv[])
1366 const char* filename = argc > 1 ? argv[1] : "test.png";
1369 std::vector<unsigned char> buffer, image;
1370 LodePNG::loadFile(buffer, filename); //load the image file with given filename
1371 LodePNG::Decoder decoder;
1372 decoder.decode(image, buffer.size() ? &buffer[0] : 0, (unsigned)buffer.size()); //decode the png
1374 //if there's an error, display it
1375 if(decoder.hasError()) std::cout << "error: " << decoder.getError() << std::endl;
1377 //the pixels are now in the vector "image", use it as texture, draw it, ...
1380 //alternative version using the "simple" function
1381 int main(int argc, char *argv[])
1383 const char* filename = argc > 1 ? argv[1] : "test.png";
1386 std::vector<unsigned char> image;
1388 unsigned error = LodePNG::decode(image, w, h, filename);
1390 //if there's an error, display it
1391 if(error != 0) std::cout << "error: " << error << std::endl;
1393 //the pixels are now in the vector "image", use it as texture, draw it, ...
1398 13.2 encoder C++ example
1399 ------------------------
1402 #include "lodepng.h"
1405 int main(int argc, char *argv[])
1407 //check if user gave a filename
1410 std::cout << "please provide a filename to save to\n";
1414 //generate some image
1415 std::vector<unsigned char> image;
1416 image.resize(512 * 512 * 4);
1417 for(unsigned y = 0; y < 512; y++)
1418 for(unsigned x = 0; x < 512; x++)
1420 image[4 * 512 * y + 4 * x + 0] = 255 * !(x & y);
1421 image[4 * 512 * y + 4 * x + 1] = x ^ y;
1422 image[4 * 512 * y + 4 * x + 2] = x | y;
1423 image[4 * 512 * y + 4 * x + 3] = 255;
1427 std::vector<unsigned char> buffer;
1428 LodePNG::Encoder encoder;
1429 encoder.encode(buffer, image, 512, 512);
1430 LodePNG::saveFile(buffer, argv[1]);
1432 //the same as the 4 lines of code above, but in 1 call:
1433 //LodePNG::encode(argv[1], image, 512, 512);
1438 13.3 Decoder C example
1439 ----------------------
1441 This example loads the PNG in 1 function call
1443 #include "lodepng.h"
1445 int main(int argc, char *argv[])
1448 unsigned char* image;
1451 if(argc <= 1) return 0;
1453 error = LodePNG_decode3(&image, &w, &h, filename);
1462 Also available in the interface is LodeZlib. Both C and C++ versions of these
1463 functions are available. The interface is similar to that of the "simple" PNG
1464 encoding and decoding functions.
1466 LodeZlib can be used to zlib compress and decompress a buffer. It cannot be
1467 used to create gzip files however. Also, it only supports the part of zlib
1468 that is required for PNG, it does not support compression and decompression
1475 The version number of LodePNG is the date of the change given in the format
1478 Some changes aren't backwards compatible. Those are indicated with a (!)
1481 *) 02 sep 2008: fixed bug where it could create empty tree that linux apps could
1482 read by ignoring the problem but windows apps couldn't.
1483 *) 06 jun 2008: added more error checks for out of memory cases.
1484 *) 26 apr 2008: added a few more checks here and there to ensure more safety.
1485 *) 06 mar 2008: crash with encoding of strings fixed
1486 *) 02 feb 2008: support for international text chunks added (iTXt)
1487 *) 23 jan 2008: small cleanups, and #defines to divide code in sections
1488 *) 20 jan 2008: support for unknown chunks allowing using LodePNG for an editor.
1489 *) 18 jan 2008: support for tIME and pHYs chunks added to encoder and decoder.
1490 *) 17 jan 2008: ability to encode and decode compressed zTXt chunks added
1491 Also vareous fixes, such as in the deflate and the padding bits code.
1492 *) 13 jan 2008: Added ability to encode Adam7-interlaced images. Improved
1493 filtering code of encoder.
1494 *) 07 jan 2008: (!) changed LodePNG to use ISO C90 instead of C++. A
1495 C++ wrapper around this provides an interface almost identical to before.
1496 Having LodePNG be pure ISO C90 makes it more portable. The C and C++ code
1497 are together in these files but it works both for C and C++ compilers.
1498 *) 29 dec 2007: (!) changed most integer types to unsigned int + other tweaks
1499 *) 30 aug 2007: bug fixed which makes this Borland C++ compatible
1500 *) 09 aug 2007: some VS2005 warnings removed again
1501 *) 21 jul 2007: deflate code placed in new namespace separate from zlib code
1502 *) 08 jun 2007: fixed bug with 2- and 4-bit color, and small interlaced images
1503 *) 04 jun 2007: improved support for Visual Studio 2005: crash with accessing
1504 invalid std::vector element [0] fixed, and level 3 and 4 warnings removed
1505 *) 02 jun 2007: made the encoder add a tag with version by default
1506 *) 27 may 2007: zlib and png code separated (but still in the same file),
1507 simple encoder/decoder functions added for more simple usage cases
1508 *) 19 may 2007: minor fixes, some code cleaning, new error added (error 69),
1509 moved some examples from here to lodepng_examples.cpp
1510 *) 12 may 2007: palette decoding bug fixed
1511 *) 24 apr 2007: changed the license from BSD to the zlib license
1512 *) 11 mar 2007: very simple addition: ability to encode bKGD chunks.
1513 *) 04 mar 2007: (!) tEXt chunk related fixes, and support for encoding
1514 palettized PNG images. Plus little interface change with palette and texts.
1515 *) 03 mar 2007: Made it encode dynamic Huffman shorter with repeat codes.
1516 Fixed a bug where the end code of a block had length 0 in the Huffman tree.
1517 *) 26 feb 2007: Huffman compression with dynamic trees (BTYPE 2) now implemented
1518 and supported by the encoder, resulting in smaller PNGs at the output.
1519 *) 27 jan 2007: Made the Adler-32 test faster so that a timewaste is gone.
1520 *) 24 jan 2007: gave encoder an error interface. Added color conversion from any
1521 greyscale type to 8-bit greyscale with or without alpha.
1522 *) 21 jan 2007: (!) Totally changed the interface. It allows more color types
1523 to convert to and is more uniform. See the manual for how it works now.
1524 *) 07 jan 2007: Some cleanup & fixes, and a few changes over the last days:
1525 encode/decode custom tEXt chunks, separate classes for zlib & deflate, and
1526 at last made the decoder give errors for incorrect Adler32 or Crc.
1527 *) 01 jan 2007: Fixed bug with encoding PNGs with less than 8 bits per channel.
1528 *) 29 dec 2006: Added support for encoding images without alpha channel, and
1529 cleaned out code as well as making certain parts faster.
1530 *) 28 dec 2006: Added "Settings" to the encoder.
1531 *) 26 dec 2006: The encoder now does LZ77 encoding and produces much smaller files now.
1532 Removed some code duplication in the decoder. Fixed little bug in an example.
1533 *) 09 dec 2006: (!) Placed output parameters of public functions as first parameter.
1534 Fixed a bug of the decoder with 16-bit per color.
1535 *) 15 okt 2006: Changed documentation structure
1536 *) 09 okt 2006: Encoder class added. It encodes a valid PNG image from the
1537 given image buffer, however for now it's not compressed.
1538 *) 08 sep 2006: (!) Changed to interface with a Decoder class
1539 *) 30 jul 2006: (!) LodePNG_InfoPng , width and height are now retrieved in different
1540 way. Renamed decodePNG to decodePNGGeneric.
1541 *) 29 jul 2006: (!) Changed the interface: image info is now returned as a
1542 struct of type LodePNG::LodePNG_Info, instead of a vector, which was a bit clumsy.
1543 *) 28 jul 2006: Cleaned the code and added new error checks.
1544 Corrected terminology "deflate" into "inflate".
1545 *) 23 jun 2006: Added SDL example in the documentation in the header, this
1546 example allows easy debugging by displaying the PNG and its transparency.
1547 *) 22 jun 2006: (!) Changed way to obtain error value. Added
1548 loadFile function for convenience. Made decodePNG32 faster.
1549 *) 21 jun 2006: (!) Changed type of info vector to unsigned.
1550 Changed position of palette in info vector. Fixed an important bug that
1551 happened on PNGs with an uncompressed block.
1552 *) 16 jun 2006: Internally changed unsigned into unsigned where
1553 needed, and performed some optimizations.
1554 *) 07 jun 2006: (!) Renamed functions to decodePNG and placed them
1555 in LodePNG namespace. Changed the order of the parameters. Rewrote the
1556 documentation in the header. Renamed files to lodepng.cpp and lodepng.h
1557 *) 22 apr 2006: Optimized and improved some code
1558 *) 07 sep 2005: (!) Changed to std::vector interface
1559 *) 12 aug 2005: Initial release
1562 16. contact information
1563 -----------------------
1565 Feel free to contact me with suggestions, problems, comments, ... concerning
1566 LodePNG. If you encounter a PNG image that doesn't work properly with this
1567 decoder, feel free to send it and I'll use it to find and fix the problem.
1569 My email address is (puzzle the account and domain together with an @ symbol):
1570 Domain: gmail dot com.
1571 Account: lode dot vandevenne.
1574 Copyright (c) 2005-2008 Lode Vandevenne
void LodePNG_chunk_generate_crc(unsigned char *chunk)
const unsigned char * LodePNG_chunk_data_const(const unsigned char *chunk)
unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor *info)
unsigned LodePNG_chunk_length(const unsigned char *chunk)
void LodePNG_chunk_type(char type[5], const unsigned char *chunk)
struct LodePNG_InfoPng LodePNG_InfoPng
static constexpr double g
unsigned LodePNG_encode32(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
const LodeZlib_DeflateSettings LodeZlib_defaultDeflateSettings
unsigned char LodePNG_chunk_private(const unsigned char *chunk)
unsigned compressionMethod
unsigned LodePNG_chunk_check_crc(const unsigned char *chunk)
void LodeZlib_DeflateSettings_init(LodeZlib_DeflateSettings *settings)
unsigned char LodePNG_chunk_safetocopy(const unsigned char *chunk)
void LodePNG_Encoder_cleanup(LodePNG_Encoder *encoder)
unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor *info)
unsigned LodeZlib_compress(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodeZlib_DeflateSettings *settings)
unsigned LodePNG_encode32f(const char *filename, const unsigned char *image, unsigned w, unsigned h)
unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor *dest, const LodePNG_InfoColor *source)
void LodePNG_EncodeSettings_init(LodePNG_EncodeSettings *settings)
LodePNG_EncodeSettings settings
unsigned LodePNG_loadFile(unsigned char **out, size_t *outsize, const char *filename)
unsigned char * LodePNG_chunk_data(unsigned char *chunk)
void LodePNG_InfoPng_cleanup(LodePNG_InfoPng *info)
unsigned char * LodePNG_chunk_next(unsigned char *chunk)
unsigned char LodePNG_chunk_critical(const unsigned char *chunk)
void LodePNG_InfoRaw_init(LodePNG_InfoRaw *info)
unsigned LodePNG_InfoColor_addPalette(LodePNG_InfoColor *info, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void LodePNG_InfoPng_init(LodePNG_InfoPng *info)
void LodePNG_Encoder_copy(LodePNG_Encoder *dest, const LodePNG_Encoder *source)
unsigned LodePNG_InfoColor_getChannels(const LodePNG_InfoColor *info)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng *dest, const LodePNG_InfoPng *source)
static QFile::DecoderFn decoder
unsigned LodePNG_convert(unsigned char *out, const unsigned char *in, LodePNG_InfoColor *infoOut, LodePNG_InfoColor *infoIn, unsigned w, unsigned h)
LodeZlib_DeflateSettings zlibsettings
unsigned LodePNG_create_chunk(unsigned char **out, size_t *outlength, unsigned length, const char *type, const unsigned char *data)
void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor *info)
unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor *info)
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor *info)
const unsigned char * LodePNG_chunk_next_const(const unsigned char *chunk)
struct LodePNG_EncodeSettings LodePNG_EncodeSettings
unsigned LodePNG_append_chunk(unsigned char **out, size_t *outlength, const unsigned char *chunk)
struct LodeZlib_DeflateSettings LodeZlib_DeflateSettings
void LodePNG_encode(LodePNG_Encoder *encoder, unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
unsigned LodePNG_saveFile(const unsigned char *buffer, size_t buffersize, const char *filename)
unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw *dest, const LodePNG_InfoRaw *source)
void LodePNG_Encoder_init(LodePNG_Encoder *encoder)
second_as<> second
Type of time stored in seconds, in double precision.
unsigned char LodePNG_chunk_type_equals(const unsigned char *chunk, const char *type)
struct LodePNG_Encoder LodePNG_Encoder
struct LodePNG_InfoColor LodePNG_InfoColor
static QFile::EncoderFn encoder
unsigned autoLeaveOutAlphaChannel
void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw *info)
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
struct LodePNG_InfoRaw LodePNG_InfoRaw