lodepng.cc (220283B)
1 /* 2 LodePNG version 20161127 3 4 Copyright (c) 2005-2016 Lode Vandevenne 5 6 This software is provided 'as-is', without any express or implied 7 warranty. In no event will the authors be held liable for any damages 8 arising from the use of this software. 9 10 Permission is granted to anyone to use this software for any purpose, 11 including commercial applications, and to alter it and redistribute it 12 freely, subject to the following restrictions: 13 14 1. The origin of this software must not be misrepresented; you must not 15 claim that you wrote the original software. If you use this software 16 in a product, an acknowledgment in the product documentation would be 17 appreciated but is not required. 18 19 2. Altered source versions must be plainly marked as such, and must not be 20 misrepresented as being the original software. 21 22 3. This notice may not be removed or altered from any source 23 distribution. 24 */ 25 26 /* 27 The manual and changelog are in the header file "lodepng.h" 28 Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for C. 29 */ 30 31 #include "lodepng.h" 32 33 #include <limits.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 37 #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/ 38 #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/ 39 #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/ 40 #endif /*_MSC_VER */ 41 42 const char* LODEPNG_VERSION_STRING = "20161127"; 43 44 /* 45 This source file is built up in the following large parts. The code sections 46 with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way. 47 -Tools for C and common code for PNG and Zlib 48 -C Code for Zlib (huffman, deflate, ...) 49 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...) 50 -The C++ wrapper around all of the above 51 */ 52 53 /*The malloc, realloc and free functions defined here with "lodepng_" in front 54 of the name, so that you can easily change them to others related to your 55 platform if needed. Everything else in the code calls these. Pass 56 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out 57 #define LODEPNG_COMPILE_ALLOCATORS in the header, to disable the ones here and 58 define them in your own project's source files without needing to change 59 lodepng source code. Don't forget to remove "static" if you copypaste them 60 from here.*/ 61 62 #ifdef LODEPNG_COMPILE_ALLOCATORS 63 static void* lodepng_malloc(size_t size) 64 { 65 return malloc(size); 66 } 67 68 static void* lodepng_realloc(void* ptr, size_t new_size) 69 { 70 return realloc(ptr, new_size); 71 } 72 73 static void lodepng_free(void* ptr) 74 { 75 free(ptr); 76 } 77 #else /*LODEPNG_COMPILE_ALLOCATORS*/ 78 void* lodepng_malloc(size_t size); 79 void* lodepng_realloc(void* ptr, size_t new_size); 80 void lodepng_free(void* ptr); 81 #endif /*LODEPNG_COMPILE_ALLOCATORS*/ 82 83 /* ////////////////////////////////////////////////////////////////////////// */ 84 /* ////////////////////////////////////////////////////////////////////////// */ 85 /* // Tools for C, and common code for PNG and Zlib. // */ 86 /* ////////////////////////////////////////////////////////////////////////// */ 87 /* ////////////////////////////////////////////////////////////////////////// */ 88 89 /* 90 Often in case of an error a value is assigned to a variable and then it breaks 91 out of a loop (to go to the cleanup phase of a function). This macro does that. 92 It makes the error handling code shorter and more readable. 93 94 Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83); 95 */ 96 #define CERROR_BREAK(errorvar, code)\ 97 {\ 98 errorvar = code;\ 99 break;\ 100 } 101 102 /*version of CERROR_BREAK that assumes the common case where the error variable is named "error"*/ 103 #define ERROR_BREAK(code) CERROR_BREAK(error, code) 104 105 /*Set error var to the error code, and return it.*/ 106 #define CERROR_RETURN_ERROR(errorvar, code)\ 107 {\ 108 errorvar = code;\ 109 return code;\ 110 } 111 112 /*Try the code, if it returns error, also return the error.*/ 113 #define CERROR_TRY_RETURN(call)\ 114 {\ 115 unsigned error = call;\ 116 if(error) return error;\ 117 } 118 119 /*Set error var to the error code, and return from the void function.*/ 120 #define CERROR_RETURN(errorvar, code)\ 121 {\ 122 errorvar = code;\ 123 return;\ 124 } 125 126 /* 127 About uivector, ucvector and string: 128 -All of them wrap dynamic arrays or text strings in a similar way. 129 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version. 130 -The string tools are made to avoid problems with compilers that declare things like strncat as deprecated. 131 -They're not used in the interface, only internally in this file as static functions. 132 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor. 133 */ 134 135 #ifdef LODEPNG_COMPILE_ZLIB 136 /*dynamic vector of unsigned ints*/ 137 typedef struct uivector 138 { 139 unsigned* data; 140 size_t size; /*size in number of unsigned longs*/ 141 size_t allocsize; /*allocated size in bytes*/ 142 } uivector; 143 144 static void uivector_cleanup(void* p) 145 { 146 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0; 147 lodepng_free(((uivector*)p)->data); 148 ((uivector*)p)->data = NULL; 149 } 150 151 /*returns 1 if success, 0 if failure ==> nothing done*/ 152 static unsigned uivector_reserve(uivector* p, size_t allocsize) 153 { 154 if(allocsize > p->allocsize) 155 { 156 size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2); 157 void* data = lodepng_realloc(p->data, newsize); 158 if(data) 159 { 160 p->allocsize = newsize; 161 p->data = (unsigned*)data; 162 } 163 else return 0; /*error: not enough memory*/ 164 } 165 return 1; 166 } 167 168 /*returns 1 if success, 0 if failure ==> nothing done*/ 169 static unsigned uivector_resize(uivector* p, size_t size) 170 { 171 if(!uivector_reserve(p, size * sizeof(unsigned))) return 0; 172 p->size = size; 173 return 1; /*success*/ 174 } 175 176 /*resize and give all new elements the value*/ 177 static unsigned uivector_resizev(uivector* p, size_t size, unsigned value) 178 { 179 size_t oldsize = p->size, i; 180 if(!uivector_resize(p, size)) return 0; 181 for(i = oldsize; i < size; ++i) p->data[i] = value; 182 return 1; 183 } 184 185 static void uivector_init(uivector* p) 186 { 187 p->data = NULL; 188 p->size = p->allocsize = 0; 189 } 190 191 #ifdef LODEPNG_COMPILE_ENCODER 192 /*returns 1 if success, 0 if failure ==> nothing done*/ 193 static unsigned uivector_push_back(uivector* p, unsigned c) 194 { 195 if(!uivector_resize(p, p->size + 1)) return 0; 196 p->data[p->size - 1] = c; 197 return 1; 198 } 199 #endif /*LODEPNG_COMPILE_ENCODER*/ 200 #endif /*LODEPNG_COMPILE_ZLIB*/ 201 202 /* /////////////////////////////////////////////////////////////////////////// */ 203 204 /*dynamic vector of unsigned chars*/ 205 typedef struct ucvector 206 { 207 unsigned char* data; 208 size_t size; /*used size*/ 209 size_t allocsize; /*allocated size*/ 210 } ucvector; 211 212 /*returns 1 if success, 0 if failure ==> nothing done*/ 213 static unsigned ucvector_reserve(ucvector* p, size_t allocsize) 214 { 215 if(allocsize > p->allocsize) 216 { 217 size_t newsize = (allocsize > p->allocsize * 2) ? allocsize : (allocsize * 3 / 2); 218 void* data = lodepng_realloc(p->data, newsize); 219 if(data) 220 { 221 p->allocsize = newsize; 222 p->data = (unsigned char*)data; 223 } 224 else return 0; /*error: not enough memory*/ 225 } 226 return 1; 227 } 228 229 /*returns 1 if success, 0 if failure ==> nothing done*/ 230 static unsigned ucvector_resize(ucvector* p, size_t size) 231 { 232 if(!ucvector_reserve(p, size * sizeof(unsigned char))) return 0; 233 p->size = size; 234 return 1; /*success*/ 235 } 236 237 #ifdef LODEPNG_COMPILE_PNG 238 239 static void ucvector_cleanup(void* p) 240 { 241 ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0; 242 lodepng_free(((ucvector*)p)->data); 243 ((ucvector*)p)->data = NULL; 244 } 245 246 static void ucvector_init(ucvector* p) 247 { 248 p->data = NULL; 249 p->size = p->allocsize = 0; 250 } 251 #endif /*LODEPNG_COMPILE_PNG*/ 252 253 #ifdef LODEPNG_COMPILE_ZLIB 254 /*you can both convert from vector to buffer&size and vica versa. If you use 255 init_buffer to take over a buffer and size, it is not needed to use cleanup*/ 256 static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size) 257 { 258 p->data = buffer; 259 p->allocsize = p->size = size; 260 } 261 #endif /*LODEPNG_COMPILE_ZLIB*/ 262 263 #if (defined(LODEPNG_COMPILE_PNG) && defined(LODEPNG_COMPILE_ANCILLARY_CHUNKS)) || defined(LODEPNG_COMPILE_ENCODER) 264 /*returns 1 if success, 0 if failure ==> nothing done*/ 265 static unsigned ucvector_push_back(ucvector* p, unsigned char c) 266 { 267 if(!ucvector_resize(p, p->size + 1)) return 0; 268 p->data[p->size - 1] = c; 269 return 1; 270 } 271 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ 272 273 274 /* ////////////////////////////////////////////////////////////////////////// */ 275 276 #ifdef LODEPNG_COMPILE_PNG 277 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 278 /*returns 1 if success, 0 if failure ==> nothing done*/ 279 static unsigned string_resize(char** out, size_t size) 280 { 281 char* data = (char*)lodepng_realloc(*out, size + 1); 282 if(data) 283 { 284 data[size] = 0; /*null termination char*/ 285 *out = data; 286 } 287 return data != 0; 288 } 289 290 /*init a {char*, size_t} pair for use as string*/ 291 static void string_init(char** out) 292 { 293 *out = NULL; 294 string_resize(out, 0); 295 } 296 297 /*free the above pair again*/ 298 static void string_cleanup(char** out) 299 { 300 lodepng_free(*out); 301 *out = NULL; 302 } 303 304 static void string_set(char** out, const char* in) 305 { 306 size_t insize = strlen(in), i; 307 if(string_resize(out, insize)) 308 { 309 for(i = 0; i != insize; ++i) 310 { 311 (*out)[i] = in[i]; 312 } 313 } 314 } 315 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 316 #endif /*LODEPNG_COMPILE_PNG*/ 317 318 /* ////////////////////////////////////////////////////////////////////////// */ 319 320 unsigned lodepng_read32bitInt(const unsigned char* buffer) 321 { 322 return (unsigned)((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]); 323 } 324 325 #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER) 326 /*buffer must have at least 4 allocated bytes available*/ 327 static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) 328 { 329 buffer[0] = (unsigned char)((value >> 24) & 0xff); 330 buffer[1] = (unsigned char)((value >> 16) & 0xff); 331 buffer[2] = (unsigned char)((value >> 8) & 0xff); 332 buffer[3] = (unsigned char)((value ) & 0xff); 333 } 334 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ 335 336 #ifdef LODEPNG_COMPILE_ENCODER 337 static void lodepng_add32bitInt(ucvector* buffer, unsigned value) 338 { 339 ucvector_resize(buffer, buffer->size + 4); /*todo: give error if resize failed*/ 340 lodepng_set32bitInt(&buffer->data[buffer->size - 4], value); 341 } 342 #endif /*LODEPNG_COMPILE_ENCODER*/ 343 344 /* ////////////////////////////////////////////////////////////////////////// */ 345 /* / File IO / */ 346 /* ////////////////////////////////////////////////////////////////////////// */ 347 348 #ifdef LODEPNG_COMPILE_DISK 349 350 /* returns negative value on error. This should be pure C compatible, so no fstat. */ 351 static long lodepng_filesize(const char* filename) 352 { 353 FILE* file; 354 long size; 355 file = fopen(filename, "rb"); 356 if(!file) return -1; 357 358 if(fseek(file, 0, SEEK_END) != 0) 359 { 360 fclose(file); 361 return -1; 362 } 363 364 size = ftell(file); 365 /* It may give LONG_MAX as directory size, this is invalid for us. */ 366 if(size == LONG_MAX) size = -1; 367 368 fclose(file); 369 return size; 370 } 371 372 /* load file into buffer that already has the correct allocated size. Returns error code.*/ 373 static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) 374 { 375 FILE* file; 376 size_t readsize; 377 file = fopen(filename, "rb"); 378 if(!file) return 78; 379 380 readsize = fread(out, 1, size, file); 381 fclose(file); 382 383 if (readsize != size) return 78; 384 return 0; 385 } 386 387 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) 388 { 389 long size = lodepng_filesize(filename); 390 if (size < 0) return 78; 391 *outsize = (size_t)size; 392 393 *out = (unsigned char*)lodepng_malloc((size_t)size); 394 if(!(*out) && size > 0) return 83; /*the above malloc failed*/ 395 396 return lodepng_buffer_file(*out, (size_t)size, filename); 397 } 398 399 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ 400 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) 401 { 402 FILE* file; 403 file = fopen(filename, "wb" ); 404 if(!file) return 79; 405 fwrite((char*)buffer , 1 , buffersize, file); 406 fclose(file); 407 return 0; 408 } 409 410 #endif /*LODEPNG_COMPILE_DISK*/ 411 412 /* ////////////////////////////////////////////////////////////////////////// */ 413 /* ////////////////////////////////////////////////////////////////////////// */ 414 /* // End of common code and tools. Begin of Zlib related code. // */ 415 /* ////////////////////////////////////////////////////////////////////////// */ 416 /* ////////////////////////////////////////////////////////////////////////// */ 417 418 #ifdef LODEPNG_COMPILE_ZLIB 419 #ifdef LODEPNG_COMPILE_ENCODER 420 /*TODO: this ignores potential out of memory errors*/ 421 #define addBitToStream(/*size_t**/ bitpointer, /*ucvector**/ bitstream, /*unsigned char*/ bit)\ 422 {\ 423 /*add a new byte at the end*/\ 424 if(((*bitpointer) & 7) == 0) ucvector_push_back(bitstream, (unsigned char)0);\ 425 /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/\ 426 (bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7));\ 427 ++(*bitpointer);\ 428 } 429 430 static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) 431 { 432 size_t i; 433 for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1)); 434 } 435 436 static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) 437 { 438 size_t i; 439 for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1)); 440 } 441 #endif /*LODEPNG_COMPILE_ENCODER*/ 442 443 #ifdef LODEPNG_COMPILE_DECODER 444 445 #define READBIT(bitpointer, bitstream) ((bitstream[bitpointer >> 3] >> (bitpointer & 0x7)) & (unsigned char)1) 446 447 static unsigned char readBitFromStream(size_t* bitpointer, const unsigned char* bitstream) 448 { 449 unsigned char result = (unsigned char)(READBIT(*bitpointer, bitstream)); 450 ++(*bitpointer); 451 return result; 452 } 453 454 static unsigned readBitsFromStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) 455 { 456 unsigned result = 0, i; 457 for(i = 0; i != nbits; ++i) 458 { 459 result += ((unsigned)READBIT(*bitpointer, bitstream)) << i; 460 ++(*bitpointer); 461 } 462 return result; 463 } 464 #endif /*LODEPNG_COMPILE_DECODER*/ 465 466 /* ////////////////////////////////////////////////////////////////////////// */ 467 /* / Deflate - Huffman / */ 468 /* ////////////////////////////////////////////////////////////////////////// */ 469 470 #define FIRST_LENGTH_CODE_INDEX 257 471 #define LAST_LENGTH_CODE_INDEX 285 472 /*256 literals, the end code, some length codes, and 2 unused codes*/ 473 #define NUM_DEFLATE_CODE_SYMBOLS 288 474 /*the distance codes have their own symbols, 30 used, 2 unused*/ 475 #define NUM_DISTANCE_SYMBOLS 32 476 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/ 477 #define NUM_CODE_LENGTH_CODES 19 478 479 /*the base lengths represented by codes 257-285*/ 480 static const unsigned LENGTHBASE[29] 481 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 482 67, 83, 99, 115, 131, 163, 195, 227, 258}; 483 484 /*the extra bits used by codes 257-285 (added to base length)*/ 485 static const unsigned LENGTHEXTRA[29] 486 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 487 4, 4, 4, 4, 5, 5, 5, 5, 0}; 488 489 /*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/ 490 static const unsigned DISTANCEBASE[30] 491 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 492 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; 493 494 /*the extra bits of backwards distances (added to base)*/ 495 static const unsigned DISTANCEEXTRA[30] 496 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 497 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; 498 499 /*the order in which "code length alphabet code lengths" are stored, out of this 500 the huffman tree of the dynamic huffman tree lengths is generated*/ 501 static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES] 502 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 503 504 /* ////////////////////////////////////////////////////////////////////////// */ 505 506 /* 507 Huffman tree struct, containing multiple representations of the tree 508 */ 509 typedef struct HuffmanTree 510 { 511 unsigned* tree2d; 512 unsigned* tree1d; 513 unsigned* lengths; /*the lengths of the codes of the 1d-tree*/ 514 unsigned maxbitlen; /*maximum number of bits a single code can get*/ 515 unsigned numcodes; /*number of symbols in the alphabet = number of codes*/ 516 } HuffmanTree; 517 518 /*function used for debug purposes to draw the tree in ascii art with C++*/ 519 /* 520 static void HuffmanTree_draw(HuffmanTree* tree) 521 { 522 std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl; 523 for(size_t i = 0; i != tree->tree1d.size; ++i) 524 { 525 if(tree->lengths.data[i]) 526 std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl; 527 } 528 std::cout << std::endl; 529 }*/ 530 531 static void HuffmanTree_init(HuffmanTree* tree) 532 { 533 tree->tree2d = 0; 534 tree->tree1d = 0; 535 tree->lengths = 0; 536 } 537 538 static void HuffmanTree_cleanup(HuffmanTree* tree) 539 { 540 lodepng_free(tree->tree2d); 541 lodepng_free(tree->tree1d); 542 lodepng_free(tree->lengths); 543 } 544 545 /*the tree representation used by the decoder. return value is error*/ 546 static unsigned HuffmanTree_make2DTree(HuffmanTree* tree) 547 { 548 unsigned nodefilled = 0; /*up to which node it is filled*/ 549 unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/ 550 unsigned n, i; 551 552 tree->tree2d = (unsigned*)lodepng_malloc(tree->numcodes * 2 * sizeof(unsigned)); 553 if(!tree->tree2d) return 83; /*alloc fail*/ 554 555 /* 556 convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means 557 uninited, a value >= numcodes is an address to another bit, a value < numcodes 558 is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as 559 many columns as codes - 1. 560 A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. 561 Here, the internal nodes are stored (what their 0 and 1 option point to). 562 There is only memory for such good tree currently, if there are more nodes 563 (due to too long length codes), error 55 will happen 564 */ 565 for(n = 0; n < tree->numcodes * 2; ++n) 566 { 567 tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/ 568 } 569 570 for(n = 0; n < tree->numcodes; ++n) /*the codes*/ 571 { 572 for(i = 0; i != tree->lengths[n]; ++i) /*the bits for this code*/ 573 { 574 unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1); 575 /*oversubscribed, see comment in lodepng_error_text*/ 576 if(treepos > 2147483647 || treepos + 2 > tree->numcodes) return 55; 577 if(tree->tree2d[2 * treepos + bit] == 32767) /*not yet filled in*/ 578 { 579 if(i + 1 == tree->lengths[n]) /*last bit*/ 580 { 581 tree->tree2d[2 * treepos + bit] = n; /*put the current code in it*/ 582 treepos = 0; 583 } 584 else 585 { 586 /*put address of the next step in here, first that address has to be found of course 587 (it's just nodefilled + 1)...*/ 588 ++nodefilled; 589 /*addresses encoded with numcodes added to it*/ 590 tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes; 591 treepos = nodefilled; 592 } 593 } 594 else treepos = tree->tree2d[2 * treepos + bit] - tree->numcodes; 595 } 596 } 597 598 for(n = 0; n < tree->numcodes * 2; ++n) 599 { 600 if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/ 601 } 602 603 return 0; 604 } 605 606 /* 607 Second step for the ...makeFromLengths and ...makeFromFrequencies functions. 608 numcodes, lengths and maxbitlen must already be filled in correctly. return 609 value is error. 610 */ 611 static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) 612 { 613 uivector blcount; 614 uivector nextcode; 615 unsigned error = 0; 616 unsigned bits, n; 617 618 uivector_init(&blcount); 619 uivector_init(&nextcode); 620 621 tree->tree1d = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned)); 622 if(!tree->tree1d) error = 83; /*alloc fail*/ 623 624 if(!uivector_resizev(&blcount, tree->maxbitlen + 1, 0) 625 || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0)) 626 error = 83; /*alloc fail*/ 627 628 if(!error) 629 { 630 /*step 1: count number of instances of each code length*/ 631 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]]; 632 /*step 2: generate the nextcode values*/ 633 for(bits = 1; bits <= tree->maxbitlen; ++bits) 634 { 635 nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1; 636 } 637 /*step 3: generate all the codes*/ 638 for(n = 0; n != tree->numcodes; ++n) 639 { 640 if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++; 641 } 642 } 643 644 uivector_cleanup(&blcount); 645 uivector_cleanup(&nextcode); 646 647 if(!error) return HuffmanTree_make2DTree(tree); 648 else return error; 649 } 650 651 /* 652 given the code lengths (as stored in the PNG file), generate the tree as defined 653 by Deflate. maxbitlen is the maximum bits that a code in the tree can have. 654 return value is error. 655 */ 656 static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, 657 size_t numcodes, unsigned maxbitlen) 658 { 659 unsigned i; 660 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned)); 661 if(!tree->lengths) return 83; /*alloc fail*/ 662 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i]; 663 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ 664 tree->maxbitlen = maxbitlen; 665 return HuffmanTree_makeFromLengths2(tree); 666 } 667 668 #ifdef LODEPNG_COMPILE_ENCODER 669 670 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding", 671 Jyrki Katajainen, Alistair Moffat, Andrew Turpin, 1995.*/ 672 673 /*chain node for boundary package merge*/ 674 typedef struct BPMNode 675 { 676 int weight; /*the sum of all weights in this chain*/ 677 unsigned index; /*index of this leaf node (called "count" in the paper)*/ 678 struct BPMNode* tail; /*the next nodes in this chain (null if last)*/ 679 int in_use; 680 } BPMNode; 681 682 /*lists of chains*/ 683 typedef struct BPMLists 684 { 685 /*memory pool*/ 686 unsigned memsize; 687 BPMNode* memory; 688 unsigned numfree; 689 unsigned nextfree; 690 BPMNode** freelist; 691 /*two heads of lookahead chains per list*/ 692 unsigned listsize; 693 BPMNode** chains0; 694 BPMNode** chains1; 695 } BPMLists; 696 697 /*creates a new chain node with the given parameters, from the memory in the lists */ 698 static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail) 699 { 700 unsigned i; 701 BPMNode* result; 702 703 /*memory full, so garbage collect*/ 704 if(lists->nextfree >= lists->numfree) 705 { 706 /*mark only those that are in use*/ 707 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0; 708 for(i = 0; i != lists->listsize; ++i) 709 { 710 BPMNode* node; 711 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1; 712 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1; 713 } 714 /*collect those that are free*/ 715 lists->numfree = 0; 716 for(i = 0; i != lists->memsize; ++i) 717 { 718 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i]; 719 } 720 lists->nextfree = 0; 721 } 722 723 result = lists->freelist[lists->nextfree++]; 724 result->weight = weight; 725 result->index = index; 726 result->tail = tail; 727 return result; 728 } 729 730 /*sort the leaves with stable mergesort*/ 731 static void bpmnode_sort(BPMNode* leaves, size_t num) 732 { 733 BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num); 734 size_t width, counter = 0; 735 for(width = 1; width < num; width *= 2) 736 { 737 BPMNode* a = (counter & 1) ? mem : leaves; 738 BPMNode* b = (counter & 1) ? leaves : mem; 739 size_t p; 740 for(p = 0; p < num; p += 2 * width) 741 { 742 size_t q = (p + width > num) ? num : (p + width); 743 size_t r = (p + 2 * width > num) ? num : (p + 2 * width); 744 size_t i = p, j = q, k; 745 for(k = p; k < r; k++) 746 { 747 if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++]; 748 else b[k] = a[j++]; 749 } 750 } 751 counter++; 752 } 753 if(counter & 1) memcpy(leaves, mem, sizeof(*leaves) * num); 754 lodepng_free(mem); 755 } 756 757 /*Boundary Package Merge step, numpresent is the amount of leaves, and c is the current chain.*/ 758 static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num) 759 { 760 unsigned lastindex = lists->chains1[c]->index; 761 762 if(c == 0) 763 { 764 if(lastindex >= numpresent) return; 765 lists->chains0[c] = lists->chains1[c]; 766 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0); 767 } 768 else 769 { 770 /*sum of the weights of the head nodes of the previous lookahead chains.*/ 771 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight; 772 lists->chains0[c] = lists->chains1[c]; 773 if(lastindex < numpresent && sum > leaves[lastindex].weight) 774 { 775 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[c]->tail); 776 return; 777 } 778 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]); 779 /*in the end we are only interested in the chain of the last list, so no 780 need to recurse if we're at the last one (this gives measurable speedup)*/ 781 if(num + 1 < (int)(2 * numpresent - 2)) 782 { 783 boundaryPM(lists, leaves, numpresent, c - 1, num); 784 boundaryPM(lists, leaves, numpresent, c - 1, num); 785 } 786 } 787 } 788 789 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, 790 size_t numcodes, unsigned maxbitlen) 791 { 792 unsigned error = 0; 793 unsigned i; 794 size_t numpresent = 0; /*number of symbols with non-zero frequency*/ 795 BPMNode* leaves; /*the symbols, only those with > 0 frequency*/ 796 797 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/ 798 if((1u << maxbitlen) < numcodes) return 80; /*error: represent all symbols*/ 799 800 leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves)); 801 if(!leaves) return 83; /*alloc fail*/ 802 803 for(i = 0; i != numcodes; ++i) 804 { 805 if(frequencies[i] > 0) 806 { 807 leaves[numpresent].weight = (int)frequencies[i]; 808 leaves[numpresent].index = i; 809 ++numpresent; 810 } 811 } 812 813 for(i = 0; i != numcodes; ++i) lengths[i] = 0; 814 815 /*ensure at least two present symbols. There should be at least one symbol 816 according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To 817 make these work as well ensure there are at least two symbols. The 818 Package-Merge code below also doesn't work correctly if there's only one 819 symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/ 820 if(numpresent == 0) 821 { 822 lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/ 823 } 824 else if(numpresent == 1) 825 { 826 lengths[leaves[0].index] = 1; 827 lengths[leaves[0].index == 0 ? 1 : 0] = 1; 828 } 829 else 830 { 831 BPMLists lists; 832 BPMNode* node; 833 834 bpmnode_sort(leaves, numpresent); 835 836 lists.listsize = maxbitlen; 837 lists.memsize = 2 * maxbitlen * (maxbitlen + 1); 838 lists.nextfree = 0; 839 lists.numfree = lists.memsize; 840 lists.memory = (BPMNode*)lodepng_malloc(lists.memsize * sizeof(*lists.memory)); 841 lists.freelist = (BPMNode**)lodepng_malloc(lists.memsize * sizeof(BPMNode*)); 842 lists.chains0 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*)); 843 lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*)); 844 if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/ 845 846 if(!error) 847 { 848 for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i]; 849 850 bpmnode_create(&lists, leaves[0].weight, 1, 0); 851 bpmnode_create(&lists, leaves[1].weight, 2, 0); 852 853 for(i = 0; i != lists.listsize; ++i) 854 { 855 lists.chains0[i] = &lists.memory[0]; 856 lists.chains1[i] = &lists.memory[1]; 857 } 858 859 /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/ 860 for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i); 861 862 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) 863 { 864 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index]; 865 } 866 } 867 868 lodepng_free(lists.memory); 869 lodepng_free(lists.freelist); 870 lodepng_free(lists.chains0); 871 lodepng_free(lists.chains1); 872 } 873 874 lodepng_free(leaves); 875 return error; 876 } 877 878 /*Create the Huffman tree given the symbol frequencies*/ 879 static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies, 880 size_t mincodes, size_t numcodes, unsigned maxbitlen) 881 { 882 unsigned error = 0; 883 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/ 884 tree->maxbitlen = maxbitlen; 885 tree->numcodes = (unsigned)numcodes; /*number of symbols*/ 886 tree->lengths = (unsigned*)lodepng_realloc(tree->lengths, numcodes * sizeof(unsigned)); 887 if(!tree->lengths) return 83; /*alloc fail*/ 888 /*initialize all lengths to 0*/ 889 memset(tree->lengths, 0, numcodes * sizeof(unsigned)); 890 891 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen); 892 if(!error) error = HuffmanTree_makeFromLengths2(tree); 893 return error; 894 } 895 896 static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index) 897 { 898 return tree->tree1d[index]; 899 } 900 901 static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index) 902 { 903 return tree->lengths[index]; 904 } 905 #endif /*LODEPNG_COMPILE_ENCODER*/ 906 907 /*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/ 908 static unsigned generateFixedLitLenTree(HuffmanTree* tree) 909 { 910 unsigned i, error = 0; 911 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); 912 if(!bitlen) return 83; /*alloc fail*/ 913 914 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/ 915 for(i = 0; i <= 143; ++i) bitlen[i] = 8; 916 for(i = 144; i <= 255; ++i) bitlen[i] = 9; 917 for(i = 256; i <= 279; ++i) bitlen[i] = 7; 918 for(i = 280; i <= 287; ++i) bitlen[i] = 8; 919 920 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15); 921 922 lodepng_free(bitlen); 923 return error; 924 } 925 926 /*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/ 927 static unsigned generateFixedDistanceTree(HuffmanTree* tree) 928 { 929 unsigned i, error = 0; 930 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); 931 if(!bitlen) return 83; /*alloc fail*/ 932 933 /*there are 32 distance codes, but 30-31 are unused*/ 934 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5; 935 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15); 936 937 lodepng_free(bitlen); 938 return error; 939 } 940 941 #ifdef LODEPNG_COMPILE_DECODER 942 943 /* 944 returns the code, or (unsigned)(-1) if error happened 945 inbitlength is the length of the complete buffer, in bits (so its byte length times 8) 946 */ 947 static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp, 948 const HuffmanTree* codetree, size_t inbitlength) 949 { 950 unsigned treepos = 0, ct; 951 for(;;) 952 { 953 if(*bp >= inbitlength) return (unsigned)(-1); /*error: end of input memory reached without endcode*/ 954 /* 955 decode the symbol from the tree. The "readBitFromStream" code is inlined in 956 the expression below because this is the biggest bottleneck while decoding 957 */ 958 ct = codetree->tree2d[(treepos << 1) + READBIT(*bp, in)]; 959 ++(*bp); 960 if(ct < codetree->numcodes) return ct; /*the symbol is decoded, return it*/ 961 else treepos = ct - codetree->numcodes; /*symbol not yet decoded, instead move tree position*/ 962 963 if(treepos >= codetree->numcodes) return (unsigned)(-1); /*error: it appeared outside the codetree*/ 964 } 965 } 966 #endif /*LODEPNG_COMPILE_DECODER*/ 967 968 #ifdef LODEPNG_COMPILE_DECODER 969 970 /* ////////////////////////////////////////////////////////////////////////// */ 971 /* / Inflator (Decompressor) / */ 972 /* ////////////////////////////////////////////////////////////////////////// */ 973 974 /*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/ 975 static void getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) 976 { 977 /*TODO: check for out of memory errors*/ 978 generateFixedLitLenTree(tree_ll); 979 generateFixedDistanceTree(tree_d); 980 } 981 982 /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/ 983 static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, 984 const unsigned char* in, size_t* bp, size_t inlength) 985 { 986 /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/ 987 unsigned error = 0; 988 unsigned n, HLIT, HDIST, HCLEN, i; 989 size_t inbitlength = inlength * 8; 990 991 /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/ 992 unsigned* bitlen_ll = 0; /*lit,len code lengths*/ 993 unsigned* bitlen_d = 0; /*dist code lengths*/ 994 /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/ 995 unsigned* bitlen_cl = 0; 996 HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/ 997 998 if((*bp) + 14 > (inlength << 3)) return 49; /*error: the bit pointer is or will go past the memory*/ 999 1000 /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/ 1001 HLIT = readBitsFromStream(bp, in, 5) + 257; 1002 /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/ 1003 HDIST = readBitsFromStream(bp, in, 5) + 1; 1004 /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ 1005 HCLEN = readBitsFromStream(bp, in, 4) + 4; 1006 1007 if((*bp) + HCLEN * 3 > (inlength << 3)) return 50; /*error: the bit pointer is or will go past the memory*/ 1008 1009 HuffmanTree_init(&tree_cl); 1010 1011 while(!error) 1012 { 1013 /*read the code length codes out of 3 * (amount of code length codes) bits*/ 1014 1015 bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); 1016 if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/); 1017 1018 for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i) 1019 { 1020 if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3); 1021 else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/ 1022 } 1023 1024 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7); 1025 if(error) break; 1026 1027 /*now we can use this tree to read the lengths for the tree that this function will return*/ 1028 bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); 1029 bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); 1030 if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); 1031 for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0; 1032 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0; 1033 1034 /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/ 1035 i = 0; 1036 while(i < HLIT + HDIST) 1037 { 1038 unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength); 1039 if(code <= 15) /*a length code*/ 1040 { 1041 if(i < HLIT) bitlen_ll[i] = code; 1042 else bitlen_d[i - HLIT] = code; 1043 ++i; 1044 } 1045 else if(code == 16) /*repeat previous*/ 1046 { 1047 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/ 1048 unsigned value; /*set value to the previous code*/ 1049 1050 if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/ 1051 1052 if((*bp + 2) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ 1053 replength += readBitsFromStream(bp, in, 2); 1054 1055 if(i < HLIT + 1) value = bitlen_ll[i - 1]; 1056 else value = bitlen_d[i - HLIT - 1]; 1057 /*repeat this value in the next lengths*/ 1058 for(n = 0; n < replength; ++n) 1059 { 1060 if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/ 1061 if(i < HLIT) bitlen_ll[i] = value; 1062 else bitlen_d[i - HLIT] = value; 1063 ++i; 1064 } 1065 } 1066 else if(code == 17) /*repeat "0" 3-10 times*/ 1067 { 1068 unsigned replength = 3; /*read in the bits that indicate repeat length*/ 1069 if((*bp + 3) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ 1070 replength += readBitsFromStream(bp, in, 3); 1071 1072 /*repeat this value in the next lengths*/ 1073 for(n = 0; n < replength; ++n) 1074 { 1075 if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/ 1076 1077 if(i < HLIT) bitlen_ll[i] = 0; 1078 else bitlen_d[i - HLIT] = 0; 1079 ++i; 1080 } 1081 } 1082 else if(code == 18) /*repeat "0" 11-138 times*/ 1083 { 1084 unsigned replength = 11; /*read in the bits that indicate repeat length*/ 1085 if((*bp + 7) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ 1086 replength += readBitsFromStream(bp, in, 7); 1087 1088 /*repeat this value in the next lengths*/ 1089 for(n = 0; n < replength; ++n) 1090 { 1091 if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/ 1092 1093 if(i < HLIT) bitlen_ll[i] = 0; 1094 else bitlen_d[i - HLIT] = 0; 1095 ++i; 1096 } 1097 } 1098 else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ 1099 { 1100 if(code == (unsigned)(-1)) 1101 { 1102 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol 1103 (10=no endcode, 11=wrong jump outside of tree)*/ 1104 error = (*bp) > inbitlength ? 10 : 11; 1105 } 1106 else error = 16; /*unexisting code, this can never happen*/ 1107 break; 1108 } 1109 } 1110 if(error) break; 1111 1112 if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/ 1113 1114 /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/ 1115 error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15); 1116 if(error) break; 1117 error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15); 1118 1119 break; /*end of error-while*/ 1120 } 1121 1122 lodepng_free(bitlen_cl); 1123 lodepng_free(bitlen_ll); 1124 lodepng_free(bitlen_d); 1125 HuffmanTree_cleanup(&tree_cl); 1126 1127 return error; 1128 } 1129 1130 /*inflate a block with dynamic of fixed Huffman tree*/ 1131 static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size_t* bp, 1132 size_t* pos, size_t inlength, unsigned btype) 1133 { 1134 unsigned error = 0; 1135 HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/ 1136 HuffmanTree tree_d; /*the huffman tree for distance codes*/ 1137 size_t inbitlength = inlength * 8; 1138 1139 HuffmanTree_init(&tree_ll); 1140 HuffmanTree_init(&tree_d); 1141 1142 if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d); 1143 else if(btype == 2) error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength); 1144 1145 while(!error) /*decode all symbols until end reached, breaks at end code*/ 1146 { 1147 /*code_ll is literal, length or end code*/ 1148 unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength); 1149 if(code_ll <= 255) /*literal symbol*/ 1150 { 1151 /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/ 1152 if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/); 1153 out->data[*pos] = (unsigned char)code_ll; 1154 ++(*pos); 1155 } 1156 else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ 1157 { 1158 unsigned code_d, distance; 1159 unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/ 1160 size_t start, forward, backward, length; 1161 1162 /*part 1: get length base*/ 1163 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX]; 1164 1165 /*part 2: get extra bits and add the value of that to length*/ 1166 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX]; 1167 if((*bp + numextrabits_l) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ 1168 length += readBitsFromStream(bp, in, numextrabits_l); 1169 1170 /*part 3: get distance code*/ 1171 code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength); 1172 if(code_d > 29) 1173 { 1174 if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ 1175 { 1176 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol 1177 (10=no endcode, 11=wrong jump outside of tree)*/ 1178 error = (*bp) > inlength * 8 ? 10 : 11; 1179 } 1180 else error = 18; /*error: invalid distance code (30-31 are never used)*/ 1181 break; 1182 } 1183 distance = DISTANCEBASE[code_d]; 1184 1185 /*part 4: get extra bits from distance*/ 1186 numextrabits_d = DISTANCEEXTRA[code_d]; 1187 if((*bp + numextrabits_d) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ 1188 distance += readBitsFromStream(bp, in, numextrabits_d); 1189 1190 /*part 5: fill in all the out[n] values based on the length and dist*/ 1191 start = (*pos); 1192 if(distance > start) ERROR_BREAK(52); /*too long backward distance*/ 1193 backward = start - distance; 1194 1195 if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/); 1196 if (distance < length) { 1197 for(forward = 0; forward < length; ++forward) 1198 { 1199 out->data[(*pos)++] = out->data[backward++]; 1200 } 1201 } else { 1202 memcpy(out->data + *pos, out->data + backward, length); 1203 *pos += length; 1204 } 1205 } 1206 else if(code_ll == 256) 1207 { 1208 break; /*end code, break the loop*/ 1209 } 1210 else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ 1211 { 1212 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol 1213 (10=no endcode, 11=wrong jump outside of tree)*/ 1214 error = ((*bp) > inlength * 8) ? 10 : 11; 1215 break; 1216 } 1217 } 1218 1219 HuffmanTree_cleanup(&tree_ll); 1220 HuffmanTree_cleanup(&tree_d); 1221 1222 return error; 1223 } 1224 1225 static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, size_t* bp, size_t* pos, size_t inlength) 1226 { 1227 size_t p; 1228 unsigned LEN, NLEN, n, error = 0; 1229 1230 /*go to first boundary of byte*/ 1231 while(((*bp) & 0x7) != 0) ++(*bp); 1232 p = (*bp) / 8; /*byte position*/ 1233 1234 /*read LEN (2 bytes) and NLEN (2 bytes)*/ 1235 if(p + 4 >= inlength) return 52; /*error, bit pointer will jump past memory*/ 1236 LEN = in[p] + 256u * in[p + 1]; p += 2; 1237 NLEN = in[p] + 256u * in[p + 1]; p += 2; 1238 1239 /*check if 16-bit NLEN is really the one's complement of LEN*/ 1240 if(LEN + NLEN != 65535) return 21; /*error: NLEN is not one's complement of LEN*/ 1241 1242 if(!ucvector_resize(out, (*pos) + LEN)) return 83; /*alloc fail*/ 1243 1244 /*read the literal data: LEN bytes are now stored in the out buffer*/ 1245 if(p + LEN > inlength) return 23; /*error: reading outside of in buffer*/ 1246 for(n = 0; n < LEN; ++n) out->data[(*pos)++] = in[p++]; 1247 1248 (*bp) = p * 8; 1249 1250 return error; 1251 } 1252 1253 static unsigned lodepng_inflatev(ucvector* out, 1254 const unsigned char* in, size_t insize, 1255 const LodePNGDecompressSettings* settings) 1256 { 1257 /*bit pointer in the "in" data, current byte is bp >> 3, current bit is bp & 0x7 (from lsb to msb of the byte)*/ 1258 size_t bp = 0; 1259 unsigned BFINAL = 0; 1260 size_t pos = 0; /*byte position in the out buffer*/ 1261 unsigned error = 0; 1262 1263 (void)settings; 1264 1265 while(!BFINAL) 1266 { 1267 unsigned BTYPE; 1268 if(bp + 2 >= insize * 8) return 52; /*error, bit pointer will jump past memory*/ 1269 BFINAL = readBitFromStream(&bp, in); 1270 BTYPE = 1u * readBitFromStream(&bp, in); 1271 BTYPE += 2u * readBitFromStream(&bp, in); 1272 1273 if(BTYPE == 3) return 20; /*error: invalid BTYPE*/ 1274 else if(BTYPE == 0) error = inflateNoCompression(out, in, &bp, &pos, insize); /*no compression*/ 1275 else error = inflateHuffmanBlock(out, in, &bp, &pos, insize, BTYPE); /*compression, BTYPE 01 or 10*/ 1276 1277 if(error) return error; 1278 } 1279 1280 return error; 1281 } 1282 1283 unsigned lodepng_inflate(unsigned char** out, size_t* outsize, 1284 const unsigned char* in, size_t insize, 1285 const LodePNGDecompressSettings* settings) 1286 { 1287 unsigned error; 1288 ucvector v; 1289 ucvector_init_buffer(&v, *out, *outsize); 1290 error = lodepng_inflatev(&v, in, insize, settings); 1291 *out = v.data; 1292 *outsize = v.size; 1293 return error; 1294 } 1295 1296 static unsigned inflate(unsigned char** out, size_t* outsize, 1297 const unsigned char* in, size_t insize, 1298 const LodePNGDecompressSettings* settings) 1299 { 1300 if(settings->custom_inflate) 1301 { 1302 return settings->custom_inflate(out, outsize, in, insize, settings); 1303 } 1304 else 1305 { 1306 return lodepng_inflate(out, outsize, in, insize, settings); 1307 } 1308 } 1309 1310 #endif /*LODEPNG_COMPILE_DECODER*/ 1311 1312 #ifdef LODEPNG_COMPILE_ENCODER 1313 1314 /* ////////////////////////////////////////////////////////////////////////// */ 1315 /* / Deflator (Compressor) / */ 1316 /* ////////////////////////////////////////////////////////////////////////// */ 1317 1318 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258; 1319 1320 /*bitlen is the size in bits of the code*/ 1321 static void addHuffmanSymbol(size_t* bp, ucvector* compressed, unsigned code, unsigned bitlen) 1322 { 1323 addBitsToStreamReversed(bp, compressed, code, bitlen); 1324 } 1325 1326 /*search the index in the array, that has the largest value smaller than or equal to the given value, 1327 given array must be sorted (if no value is smaller, it returns the size of the given array)*/ 1328 static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) 1329 { 1330 /*binary search (only small gain over linear). TODO: use CPU log2 instruction for getting symbols instead*/ 1331 size_t left = 1; 1332 size_t right = array_size - 1; 1333 1334 while(left <= right) { 1335 size_t mid = (left + right) >> 1; 1336 if (array[mid] >= value) right = mid - 1; 1337 else left = mid + 1; 1338 } 1339 if(left >= array_size || array[left] > value) left--; 1340 return left; 1341 } 1342 1343 static void addLengthDistance(uivector* values, size_t length, size_t distance) 1344 { 1345 /*values in encoded vector are those used by deflate: 1346 0-255: literal bytes 1347 256: end 1348 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits) 1349 286-287: invalid*/ 1350 1351 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length); 1352 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]); 1353 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance); 1354 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]); 1355 1356 uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX); 1357 uivector_push_back(values, extra_length); 1358 uivector_push_back(values, dist_code); 1359 uivector_push_back(values, extra_distance); 1360 } 1361 1362 /*3 bytes of data get encoded into two bytes. The hash cannot use more than 3 1363 bytes as input because 3 is the minimum match length for deflate*/ 1364 static const unsigned HASH_NUM_VALUES = 65536; 1365 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as initializer*/ 1366 1367 typedef struct Hash 1368 { 1369 int* head; /*hash value to head circular pos - can be outdated if went around window*/ 1370 /*circular pos to prev circular pos*/ 1371 unsigned short* chain; 1372 int* val; /*circular pos to hash value*/ 1373 1374 /*TODO: do this not only for zeros but for any repeated byte. However for PNG 1375 it's always going to be the zeros that dominate, so not important for PNG*/ 1376 int* headz; /*similar to head, but for chainz*/ 1377 unsigned short* chainz; /*those with same amount of zeros*/ 1378 unsigned short* zeros; /*length of zeros streak, used as a second hash chain*/ 1379 } Hash; 1380 1381 static unsigned hash_init(Hash* hash, unsigned windowsize) 1382 { 1383 unsigned i; 1384 hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES); 1385 hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize); 1386 hash->chain = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); 1387 1388 hash->zeros = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); 1389 hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1)); 1390 hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize); 1391 1392 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) 1393 { 1394 return 83; /*alloc fail*/ 1395 } 1396 1397 /*initialize hash table*/ 1398 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1; 1399 for(i = 0; i != windowsize; ++i) hash->val[i] = -1; 1400 for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized*/ 1401 1402 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1; 1403 for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialized*/ 1404 1405 return 0; 1406 } 1407 1408 static void hash_cleanup(Hash* hash) 1409 { 1410 lodepng_free(hash->head); 1411 lodepng_free(hash->val); 1412 lodepng_free(hash->chain); 1413 1414 lodepng_free(hash->zeros); 1415 lodepng_free(hash->headz); 1416 lodepng_free(hash->chainz); 1417 } 1418 1419 1420 1421 static unsigned getHash(const unsigned char* data, size_t size, size_t pos) 1422 { 1423 unsigned result = 0; 1424 if(pos + 2 < size) 1425 { 1426 /*A simple shift and xor hash is used. Since the data of PNGs is dominated 1427 by zeroes due to the filters, a better hash does not have a significant 1428 effect on speed in traversing the chain, and causes more time spend on 1429 calculating the hash.*/ 1430 result ^= (unsigned)(data[pos + 0] << 0u); 1431 result ^= (unsigned)(data[pos + 1] << 4u); 1432 result ^= (unsigned)(data[pos + 2] << 8u); 1433 } else { 1434 size_t amount, i; 1435 if(pos >= size) return 0; 1436 amount = size - pos; 1437 for(i = 0; i != amount; ++i) result ^= (unsigned)(data[pos + i] << (i * 8u)); 1438 } 1439 return result & HASH_BIT_MASK; 1440 } 1441 1442 static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) 1443 { 1444 const unsigned char* start = data + pos; 1445 const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH; 1446 if(end > data + size) end = data + size; 1447 data = start; 1448 while(data != end && *data == 0) ++data; 1449 /*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/ 1450 return (unsigned)(data - start); 1451 } 1452 1453 /*wpos = pos & (windowsize - 1)*/ 1454 static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros) 1455 { 1456 hash->val[wpos] = (int)hashval; 1457 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval]; 1458 hash->head[hashval] = wpos; 1459 1460 hash->zeros[wpos] = numzeros; 1461 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros]; 1462 hash->headz[numzeros] = wpos; 1463 } 1464 1465 /* 1466 LZ77-encode the data. Return value is error code. The input are raw bytes, the output 1467 is in the form of unsigned integers with codes representing for example literal bytes, or 1468 length/distance pairs. 1469 It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a 1470 sliding window (of windowsize) is used, and all past bytes in that window can be used as 1471 the "dictionary". A brute force search through all possible distances would be slow, and 1472 this hash technique is one out of several ways to speed this up. 1473 */ 1474 static unsigned encodeLZ77(uivector* out, Hash* hash, 1475 const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize, 1476 unsigned minmatch, unsigned nicematch, unsigned lazymatching) 1477 { 1478 size_t pos; 1479 unsigned i, error = 0; 1480 /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/ 1481 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8; 1482 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64; 1483 1484 unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/ 1485 unsigned numzeros = 0; 1486 1487 unsigned offset; /*the offset represents the distance in LZ77 terminology*/ 1488 unsigned length; 1489 unsigned lazy = 0; 1490 unsigned lazylength = 0, lazyoffset = 0; 1491 unsigned hashval; 1492 unsigned current_offset, current_length; 1493 unsigned prev_offset; 1494 const unsigned char *lastptr, *foreptr, *backptr; 1495 unsigned hashpos; 1496 1497 if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/ 1498 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/ 1499 1500 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH; 1501 1502 for(pos = inpos; pos < insize; ++pos) 1503 { 1504 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/ 1505 unsigned chainlength = 0; 1506 1507 hashval = getHash(in, insize, pos); 1508 1509 if(usezeros && hashval == 0) 1510 { 1511 if(numzeros == 0) numzeros = countZeros(in, insize, pos); 1512 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; 1513 } 1514 else 1515 { 1516 numzeros = 0; 1517 } 1518 1519 updateHashChain(hash, wpos, hashval, numzeros); 1520 1521 /*the length and offset found for the current position*/ 1522 length = 0; 1523 offset = 0; 1524 1525 hashpos = hash->chain[wpos]; 1526 1527 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH]; 1528 1529 /*search for the longest string*/ 1530 prev_offset = 0; 1531 for(;;) 1532 { 1533 if(chainlength++ >= maxchainlength) break; 1534 current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize; 1535 1536 if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/ 1537 prev_offset = current_offset; 1538 if(current_offset > 0) 1539 { 1540 /*test the next characters*/ 1541 foreptr = &in[pos]; 1542 backptr = &in[pos - current_offset]; 1543 1544 /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/ 1545 if(numzeros >= 3) 1546 { 1547 unsigned skip = hash->zeros[hashpos]; 1548 if(skip > numzeros) skip = numzeros; 1549 backptr += skip; 1550 foreptr += skip; 1551 } 1552 1553 while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ 1554 { 1555 ++backptr; 1556 ++foreptr; 1557 } 1558 current_length = (unsigned)(foreptr - &in[pos]); 1559 1560 if(current_length > length) 1561 { 1562 length = current_length; /*the longest length*/ 1563 offset = current_offset; /*the offset that is related to this longest length*/ 1564 /*jump out once a length of max length is found (speed gain). This also jumps 1565 out if length is MAX_SUPPORTED_DEFLATE_LENGTH*/ 1566 if(current_length >= nicematch) break; 1567 } 1568 } 1569 1570 if(hashpos == hash->chain[hashpos]) break; 1571 1572 if(numzeros >= 3 && length > numzeros) 1573 { 1574 hashpos = hash->chainz[hashpos]; 1575 if(hash->zeros[hashpos] != numzeros) break; 1576 } 1577 else 1578 { 1579 hashpos = hash->chain[hashpos]; 1580 /*outdated hash value, happens if particular value was not encountered in whole last window*/ 1581 if(hash->val[hashpos] != (int)hashval) break; 1582 } 1583 } 1584 1585 if(lazymatching) 1586 { 1587 if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) 1588 { 1589 lazy = 1; 1590 lazylength = length; 1591 lazyoffset = offset; 1592 continue; /*try the next byte*/ 1593 } 1594 if(lazy) 1595 { 1596 lazy = 0; 1597 if(pos == 0) ERROR_BREAK(81); 1598 if(length > lazylength + 1) 1599 { 1600 /*push the previous character as literal*/ 1601 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/); 1602 } 1603 else 1604 { 1605 length = lazylength; 1606 offset = lazyoffset; 1607 hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/ 1608 hash->headz[numzeros] = -1; /*idem*/ 1609 --pos; 1610 } 1611 } 1612 } 1613 if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/); 1614 1615 /*encode it as length/distance pair or literal value*/ 1616 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ 1617 { 1618 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); 1619 } 1620 else if(length < minmatch || (length == 3 && offset > 4096)) 1621 { 1622 /*compensate for the fact that longer offsets have more extra bits, a 1623 length of only 3 may be not worth it then*/ 1624 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); 1625 } 1626 else 1627 { 1628 addLengthDistance(out, length, offset); 1629 for(i = 1; i < length; ++i) 1630 { 1631 ++pos; 1632 wpos = pos & (windowsize - 1); 1633 hashval = getHash(in, insize, pos); 1634 if(usezeros && hashval == 0) 1635 { 1636 if(numzeros == 0) numzeros = countZeros(in, insize, pos); 1637 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros; 1638 } 1639 else 1640 { 1641 numzeros = 0; 1642 } 1643 updateHashChain(hash, wpos, hashval, numzeros); 1644 } 1645 } 1646 } /*end of the loop through each character of input*/ 1647 1648 return error; 1649 } 1650 1651 /* /////////////////////////////////////////////////////////////////////////// */ 1652 1653 static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) 1654 { 1655 /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte, 1656 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/ 1657 1658 size_t i, j, numdeflateblocks = (datasize + 65534) / 65535; 1659 unsigned datapos = 0; 1660 for(i = 0; i != numdeflateblocks; ++i) 1661 { 1662 unsigned BFINAL, BTYPE, LEN, NLEN; 1663 unsigned char firstbyte; 1664 1665 BFINAL = (i == numdeflateblocks - 1); 1666 BTYPE = 0; 1667 1668 firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1)); 1669 ucvector_push_back(out, firstbyte); 1670 1671 LEN = 65535; 1672 if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos; 1673 NLEN = 65535 - LEN; 1674 1675 ucvector_push_back(out, (unsigned char)(LEN & 255)); 1676 ucvector_push_back(out, (unsigned char)(LEN >> 8)); 1677 ucvector_push_back(out, (unsigned char)(NLEN & 255)); 1678 ucvector_push_back(out, (unsigned char)(NLEN >> 8)); 1679 1680 /*Decompressed data*/ 1681 for(j = 0; j < 65535 && datapos < datasize; ++j) 1682 { 1683 ucvector_push_back(out, data[datapos++]); 1684 } 1685 } 1686 1687 return 0; 1688 } 1689 1690 /* 1691 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman trees. 1692 tree_ll: the tree for lit and len codes. 1693 tree_d: the tree for distance codes. 1694 */ 1695 static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encoded, 1696 const HuffmanTree* tree_ll, const HuffmanTree* tree_d) 1697 { 1698 size_t i = 0; 1699 for(i = 0; i != lz77_encoded->size; ++i) 1700 { 1701 unsigned val = lz77_encoded->data[i]; 1702 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val)); 1703 if(val > 256) /*for a length code, 3 more things have to be added*/ 1704 { 1705 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX; 1706 unsigned n_length_extra_bits = LENGTHEXTRA[length_index]; 1707 unsigned length_extra_bits = lz77_encoded->data[++i]; 1708 1709 unsigned distance_code = lz77_encoded->data[++i]; 1710 1711 unsigned distance_index = distance_code; 1712 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index]; 1713 unsigned distance_extra_bits = lz77_encoded->data[++i]; 1714 1715 addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits); 1716 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code), 1717 HuffmanTree_getLength(tree_d, distance_code)); 1718 addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits); 1719 } 1720 } 1721 } 1722 1723 /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/ 1724 static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, 1725 const unsigned char* data, size_t datapos, size_t dataend, 1726 const LodePNGCompressSettings* settings, unsigned final) 1727 { 1728 unsigned error = 0; 1729 1730 /* 1731 A block is compressed as follows: The PNG data is lz77 encoded, resulting in 1732 literal bytes and length/distance pairs. This is then huffman compressed with 1733 two huffman trees. One huffman tree is used for the lit and len values ("ll"), 1734 another huffman tree is used for the dist values ("d"). These two trees are 1735 stored using their code lengths, and to compress even more these code lengths 1736 are also run-length encoded and huffman compressed. This gives a huffman tree 1737 of code lengths "cl". The code lenghts used to describe this third tree are 1738 the code length code lengths ("clcl"). 1739 */ 1740 1741 /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/ 1742 uivector lz77_encoded; 1743 HuffmanTree tree_ll; /*tree for lit,len values*/ 1744 HuffmanTree tree_d; /*tree for distance codes*/ 1745 HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/ 1746 uivector frequencies_ll; /*frequency of lit,len codes*/ 1747 uivector frequencies_d; /*frequency of dist codes*/ 1748 uivector frequencies_cl; /*frequency of code length codes*/ 1749 uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/ 1750 uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/ 1751 /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl 1752 (these are written as is in the file, it would be crazy to compress these using yet another huffman 1753 tree that needs to be represented by yet another set of code lengths)*/ 1754 uivector bitlen_cl; 1755 size_t datasize = dataend - datapos; 1756 1757 /* 1758 Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies: 1759 bitlen_lld is to tree_cl what data is to tree_ll and tree_d. 1760 bitlen_lld_e is to bitlen_lld what lz77_encoded is to data. 1761 bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded. 1762 */ 1763 1764 unsigned BFINAL = final; 1765 size_t numcodes_ll, numcodes_d, i; 1766 unsigned HLIT, HDIST, HCLEN; 1767 1768 uivector_init(&lz77_encoded); 1769 HuffmanTree_init(&tree_ll); 1770 HuffmanTree_init(&tree_d); 1771 HuffmanTree_init(&tree_cl); 1772 uivector_init(&frequencies_ll); 1773 uivector_init(&frequencies_d); 1774 uivector_init(&frequencies_cl); 1775 uivector_init(&bitlen_lld); 1776 uivector_init(&bitlen_lld_e); 1777 uivector_init(&bitlen_cl); 1778 1779 /*This while loop never loops due to a break at the end, it is here to 1780 allow breaking out of it to the cleanup phase on error conditions.*/ 1781 while(!error) 1782 { 1783 if(settings->use_lz77) 1784 { 1785 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, 1786 settings->minmatch, settings->nicematch, settings->lazymatching); 1787 if(error) break; 1788 } 1789 else 1790 { 1791 if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/); 1792 for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/ 1793 } 1794 1795 if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/); 1796 if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/); 1797 1798 /*Count the frequencies of lit, len and dist codes*/ 1799 for(i = 0; i != lz77_encoded.size; ++i) 1800 { 1801 unsigned symbol = lz77_encoded.data[i]; 1802 ++frequencies_ll.data[symbol]; 1803 if(symbol > 256) 1804 { 1805 unsigned dist = lz77_encoded.data[i + 2]; 1806 ++frequencies_d.data[dist]; 1807 i += 3; 1808 } 1809 } 1810 frequencies_ll.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/ 1811 1812 /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/ 1813 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.data, 257, frequencies_ll.size, 15); 1814 if(error) break; 1815 /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/ 1816 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.data, 2, frequencies_d.size, 15); 1817 if(error) break; 1818 1819 numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286; 1820 numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30; 1821 /*store the code lengths of both generated trees in bitlen_lld*/ 1822 for(i = 0; i != numcodes_ll; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i)); 1823 for(i = 0; i != numcodes_d; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i)); 1824 1825 /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times), 1826 17 (3-10 zeroes), 18 (11-138 zeroes)*/ 1827 for(i = 0; i != (unsigned)bitlen_lld.size; ++i) 1828 { 1829 unsigned j = 0; /*amount of repititions*/ 1830 while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) ++j; 1831 1832 if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/ 1833 { 1834 ++j; /*include the first zero*/ 1835 if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ 1836 { 1837 uivector_push_back(&bitlen_lld_e, 17); 1838 uivector_push_back(&bitlen_lld_e, j - 3); 1839 } 1840 else /*repeat code 18 supports max 138 zeroes*/ 1841 { 1842 if(j > 138) j = 138; 1843 uivector_push_back(&bitlen_lld_e, 18); 1844 uivector_push_back(&bitlen_lld_e, j - 11); 1845 } 1846 i += (j - 1); 1847 } 1848 else if(j >= 3) /*repeat code for value other than zero*/ 1849 { 1850 size_t k; 1851 unsigned num = j / 6, rest = j % 6; 1852 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); 1853 for(k = 0; k < num; ++k) 1854 { 1855 uivector_push_back(&bitlen_lld_e, 16); 1856 uivector_push_back(&bitlen_lld_e, 6 - 3); 1857 } 1858 if(rest >= 3) 1859 { 1860 uivector_push_back(&bitlen_lld_e, 16); 1861 uivector_push_back(&bitlen_lld_e, rest - 3); 1862 } 1863 else j -= rest; 1864 i += j; 1865 } 1866 else /*too short to benefit from repeat code*/ 1867 { 1868 uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); 1869 } 1870 } 1871 1872 /*generate tree_cl, the huffmantree of huffmantrees*/ 1873 1874 if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/); 1875 for(i = 0; i != bitlen_lld_e.size; ++i) 1876 { 1877 ++frequencies_cl.data[bitlen_lld_e.data[i]]; 1878 /*after a repeat code come the bits that specify the number of repetitions, 1879 those don't need to be in the frequencies_cl calculation*/ 1880 if(bitlen_lld_e.data[i] >= 16) ++i; 1881 } 1882 1883 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data, 1884 frequencies_cl.size, frequencies_cl.size, 7); 1885 if(error) break; 1886 1887 if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/); 1888 for(i = 0; i != tree_cl.numcodes; ++i) 1889 { 1890 /*lenghts of code length tree is in the order as specified by deflate*/ 1891 bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]); 1892 } 1893 while(bitlen_cl.data[bitlen_cl.size - 1] == 0 && bitlen_cl.size > 4) 1894 { 1895 /*remove zeros at the end, but minimum size must be 4*/ 1896 if(!uivector_resize(&bitlen_cl, bitlen_cl.size - 1)) ERROR_BREAK(83 /*alloc fail*/); 1897 } 1898 if(error) break; 1899 1900 /* 1901 Write everything into the output 1902 1903 After the BFINAL and BTYPE, the dynamic block consists out of the following: 1904 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN 1905 - (HCLEN+4)*3 bits code lengths of code length alphabet 1906 - HLIT + 257 code lenghts of lit/length alphabet (encoded using the code length 1907 alphabet, + possible repetition codes 16, 17, 18) 1908 - HDIST + 1 code lengths of distance alphabet (encoded using the code length 1909 alphabet, + possible repetition codes 16, 17, 18) 1910 - compressed data 1911 - 256 (end code) 1912 */ 1913 1914 /*Write block type*/ 1915 addBitToStream(bp, out, BFINAL); 1916 addBitToStream(bp, out, 0); /*first bit of BTYPE "dynamic"*/ 1917 addBitToStream(bp, out, 1); /*second bit of BTYPE "dynamic"*/ 1918 1919 /*write the HLIT, HDIST and HCLEN values*/ 1920 HLIT = (unsigned)(numcodes_ll - 257); 1921 HDIST = (unsigned)(numcodes_d - 1); 1922 HCLEN = (unsigned)bitlen_cl.size - 4; 1923 /*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/ 1924 while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) --HCLEN; 1925 addBitsToStream(bp, out, HLIT, 5); 1926 addBitsToStream(bp, out, HDIST, 5); 1927 addBitsToStream(bp, out, HCLEN, 4); 1928 1929 /*write the code lenghts of the code length alphabet*/ 1930 for(i = 0; i != HCLEN + 4; ++i) addBitsToStream(bp, out, bitlen_cl.data[i], 3); 1931 1932 /*write the lenghts of the lit/len AND the dist alphabet*/ 1933 for(i = 0; i != bitlen_lld_e.size; ++i) 1934 { 1935 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]), 1936 HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i])); 1937 /*extra bits of repeat codes*/ 1938 if(bitlen_lld_e.data[i] == 16) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 2); 1939 else if(bitlen_lld_e.data[i] == 17) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 3); 1940 else if(bitlen_lld_e.data[i] == 18) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 7); 1941 } 1942 1943 /*write the compressed data symbols*/ 1944 writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); 1945 /*error: the length of the end code 256 must be larger than 0*/ 1946 if(HuffmanTree_getLength(&tree_ll, 256) == 0) ERROR_BREAK(64); 1947 1948 /*write the end code*/ 1949 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); 1950 1951 break; /*end of error-while*/ 1952 } 1953 1954 /*cleanup*/ 1955 uivector_cleanup(&lz77_encoded); 1956 HuffmanTree_cleanup(&tree_ll); 1957 HuffmanTree_cleanup(&tree_d); 1958 HuffmanTree_cleanup(&tree_cl); 1959 uivector_cleanup(&frequencies_ll); 1960 uivector_cleanup(&frequencies_d); 1961 uivector_cleanup(&frequencies_cl); 1962 uivector_cleanup(&bitlen_lld_e); 1963 uivector_cleanup(&bitlen_lld); 1964 uivector_cleanup(&bitlen_cl); 1965 1966 return error; 1967 } 1968 1969 static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash, 1970 const unsigned char* data, 1971 size_t datapos, size_t dataend, 1972 const LodePNGCompressSettings* settings, unsigned final) 1973 { 1974 HuffmanTree tree_ll; /*tree for literal values and length codes*/ 1975 HuffmanTree tree_d; /*tree for distance codes*/ 1976 1977 unsigned BFINAL = final; 1978 unsigned error = 0; 1979 size_t i; 1980 1981 HuffmanTree_init(&tree_ll); 1982 HuffmanTree_init(&tree_d); 1983 1984 generateFixedLitLenTree(&tree_ll); 1985 generateFixedDistanceTree(&tree_d); 1986 1987 addBitToStream(bp, out, BFINAL); 1988 addBitToStream(bp, out, 1); /*first bit of BTYPE*/ 1989 addBitToStream(bp, out, 0); /*second bit of BTYPE*/ 1990 1991 if(settings->use_lz77) /*LZ77 encoded*/ 1992 { 1993 uivector lz77_encoded; 1994 uivector_init(&lz77_encoded); 1995 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, 1996 settings->minmatch, settings->nicematch, settings->lazymatching); 1997 if(!error) writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); 1998 uivector_cleanup(&lz77_encoded); 1999 } 2000 else /*no LZ77, but still will be Huffman compressed*/ 2001 { 2002 for(i = datapos; i < dataend; ++i) 2003 { 2004 addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i])); 2005 } 2006 } 2007 /*add END code*/ 2008 if(!error) addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); 2009 2010 /*cleanup*/ 2011 HuffmanTree_cleanup(&tree_ll); 2012 HuffmanTree_cleanup(&tree_d); 2013 2014 return error; 2015 } 2016 2017 static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize, 2018 const LodePNGCompressSettings* settings) 2019 { 2020 unsigned error = 0; 2021 size_t i, blocksize, numdeflateblocks; 2022 size_t bp = 0; /*the bit pointer*/ 2023 Hash hash; 2024 2025 if(settings->btype > 2) return 61; 2026 else if(settings->btype == 0) return deflateNoCompression(out, in, insize); 2027 else if(settings->btype == 1) blocksize = insize; 2028 else /*if(settings->btype == 2)*/ 2029 { 2030 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/ 2031 blocksize = insize / 8 + 8; 2032 if(blocksize < 65536) blocksize = 65536; 2033 if(blocksize > 262144) blocksize = 262144; 2034 } 2035 2036 numdeflateblocks = (insize + blocksize - 1) / blocksize; 2037 if(numdeflateblocks == 0) numdeflateblocks = 1; 2038 2039 error = hash_init(&hash, settings->windowsize); 2040 if(error) return error; 2041 2042 for(i = 0; i != numdeflateblocks && !error; ++i) 2043 { 2044 unsigned final = (i == numdeflateblocks - 1); 2045 size_t start = i * blocksize; 2046 size_t end = start + blocksize; 2047 if(end > insize) end = insize; 2048 2049 if(settings->btype == 1) error = deflateFixed(out, &bp, &hash, in, start, end, settings, final); 2050 else if(settings->btype == 2) error = deflateDynamic(out, &bp, &hash, in, start, end, settings, final); 2051 } 2052 2053 hash_cleanup(&hash); 2054 2055 return error; 2056 } 2057 2058 unsigned lodepng_deflate(unsigned char** out, size_t* outsize, 2059 const unsigned char* in, size_t insize, 2060 const LodePNGCompressSettings* settings) 2061 { 2062 unsigned error; 2063 ucvector v; 2064 ucvector_init_buffer(&v, *out, *outsize); 2065 error = lodepng_deflatev(&v, in, insize, settings); 2066 *out = v.data; 2067 *outsize = v.size; 2068 return error; 2069 } 2070 2071 static unsigned deflate(unsigned char** out, size_t* outsize, 2072 const unsigned char* in, size_t insize, 2073 const LodePNGCompressSettings* settings) 2074 { 2075 if(settings->custom_deflate) 2076 { 2077 return settings->custom_deflate(out, outsize, in, insize, settings); 2078 } 2079 else 2080 { 2081 return lodepng_deflate(out, outsize, in, insize, settings); 2082 } 2083 } 2084 2085 #endif /*LODEPNG_COMPILE_DECODER*/ 2086 2087 /* ////////////////////////////////////////////////////////////////////////// */ 2088 /* / Adler32 */ 2089 /* ////////////////////////////////////////////////////////////////////////// */ 2090 2091 static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) 2092 { 2093 unsigned s1 = adler & 0xffff; 2094 unsigned s2 = (adler >> 16) & 0xffff; 2095 2096 while(len > 0) 2097 { 2098 /*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/ 2099 unsigned amount = len > 5550 ? 5550 : len; 2100 len -= amount; 2101 while(amount > 0) 2102 { 2103 s1 += (*data++); 2104 s2 += s1; 2105 --amount; 2106 } 2107 s1 %= 65521; 2108 s2 %= 65521; 2109 } 2110 2111 return (s2 << 16) | s1; 2112 } 2113 2114 /*Return the adler32 of the bytes data[0..len-1]*/ 2115 static unsigned adler32(const unsigned char* data, unsigned len) 2116 { 2117 return update_adler32(1L, data, len); 2118 } 2119 2120 /* ////////////////////////////////////////////////////////////////////////// */ 2121 /* / Zlib / */ 2122 /* ////////////////////////////////////////////////////////////////////////// */ 2123 2124 #ifdef LODEPNG_COMPILE_DECODER 2125 2126 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, 2127 size_t insize, const LodePNGDecompressSettings* settings) 2128 { 2129 unsigned error = 0; 2130 unsigned CM, CINFO, FDICT; 2131 2132 if(insize < 2) return 53; /*error, size of zlib data too small*/ 2133 /*read information from zlib header*/ 2134 if((in[0] * 256 + in[1]) % 31 != 0) 2135 { 2136 /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/ 2137 return 24; 2138 } 2139 2140 CM = in[0] & 15; 2141 CINFO = (in[0] >> 4) & 15; 2142 /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/ 2143 FDICT = (in[1] >> 5) & 1; 2144 /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/ 2145 2146 if(CM != 8 || CINFO > 7) 2147 { 2148 /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/ 2149 return 25; 2150 } 2151 if(FDICT != 0) 2152 { 2153 /*error: the specification of PNG says about the zlib stream: 2154 "The additional flags shall not specify a preset dictionary."*/ 2155 return 26; 2156 } 2157 2158 error = inflate(out, outsize, in + 2, insize - 2, settings); 2159 if(error) return error; 2160 2161 if(!settings->ignore_adler32) 2162 { 2163 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]); 2164 unsigned checksum = adler32(*out, (unsigned)(*outsize)); 2165 if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/ 2166 } 2167 2168 return 0; /*no error*/ 2169 } 2170 2171 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, 2172 size_t insize, const LodePNGDecompressSettings* settings) 2173 { 2174 if(settings->custom_zlib) 2175 { 2176 return settings->custom_zlib(out, outsize, in, insize, settings); 2177 } 2178 else 2179 { 2180 return lodepng_zlib_decompress(out, outsize, in, insize, settings); 2181 } 2182 } 2183 2184 #endif /*LODEPNG_COMPILE_DECODER*/ 2185 2186 #ifdef LODEPNG_COMPILE_ENCODER 2187 2188 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, 2189 size_t insize, const LodePNGCompressSettings* settings) 2190 { 2191 /*initially, *out must be NULL and outsize 0, if you just give some random *out 2192 that's pointing to a non allocated buffer, this'll crash*/ 2193 ucvector outv; 2194 size_t i; 2195 unsigned error; 2196 unsigned char* deflatedata = 0; 2197 size_t deflatesize = 0; 2198 2199 /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/ 2200 unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/ 2201 unsigned FLEVEL = 0; 2202 unsigned FDICT = 0; 2203 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64; 2204 unsigned FCHECK = 31 - CMFFLG % 31; 2205 CMFFLG += FCHECK; 2206 2207 /*ucvector-controlled version of the output buffer, for dynamic array*/ 2208 ucvector_init_buffer(&outv, *out, *outsize); 2209 2210 ucvector_push_back(&outv, (unsigned char)(CMFFLG >> 8)); 2211 ucvector_push_back(&outv, (unsigned char)(CMFFLG & 255)); 2212 2213 error = deflate(&deflatedata, &deflatesize, in, insize, settings); 2214 2215 if(!error) 2216 { 2217 unsigned ADLER32 = adler32(in, (unsigned)insize); 2218 for(i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]); 2219 lodepng_free(deflatedata); 2220 lodepng_add32bitInt(&outv, ADLER32); 2221 } 2222 2223 *out = outv.data; 2224 *outsize = outv.size; 2225 2226 return error; 2227 } 2228 2229 /* compress using the default or custom zlib function */ 2230 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, 2231 size_t insize, const LodePNGCompressSettings* settings) 2232 { 2233 if(settings->custom_zlib) 2234 { 2235 return settings->custom_zlib(out, outsize, in, insize, settings); 2236 } 2237 else 2238 { 2239 return lodepng_zlib_compress(out, outsize, in, insize, settings); 2240 } 2241 } 2242 2243 #endif /*LODEPNG_COMPILE_ENCODER*/ 2244 2245 #else /*no LODEPNG_COMPILE_ZLIB*/ 2246 2247 #ifdef LODEPNG_COMPILE_DECODER 2248 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, 2249 size_t insize, const LodePNGDecompressSettings* settings) 2250 { 2251 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ 2252 return settings->custom_zlib(out, outsize, in, insize, settings); 2253 } 2254 #endif /*LODEPNG_COMPILE_DECODER*/ 2255 #ifdef LODEPNG_COMPILE_ENCODER 2256 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, 2257 size_t insize, const LodePNGCompressSettings* settings) 2258 { 2259 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */ 2260 return settings->custom_zlib(out, outsize, in, insize, settings); 2261 } 2262 #endif /*LODEPNG_COMPILE_ENCODER*/ 2263 2264 #endif /*LODEPNG_COMPILE_ZLIB*/ 2265 2266 /* ////////////////////////////////////////////////////////////////////////// */ 2267 2268 #ifdef LODEPNG_COMPILE_ENCODER 2269 2270 /*this is a good tradeoff between speed and compression ratio*/ 2271 #define DEFAULT_WINDOWSIZE 2048 2272 2273 void lodepng_compress_settings_init(LodePNGCompressSettings* settings) 2274 { 2275 /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/ 2276 settings->btype = 2; 2277 settings->use_lz77 = 1; 2278 settings->windowsize = DEFAULT_WINDOWSIZE; 2279 settings->minmatch = 3; 2280 settings->nicematch = 128; 2281 settings->lazymatching = 1; 2282 2283 settings->custom_zlib = 0; 2284 settings->custom_deflate = 0; 2285 settings->custom_context = 0; 2286 } 2287 2288 const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT_WINDOWSIZE, 3, 128, 1, 0, 0, 0}; 2289 2290 2291 #endif /*LODEPNG_COMPILE_ENCODER*/ 2292 2293 #ifdef LODEPNG_COMPILE_DECODER 2294 2295 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) 2296 { 2297 settings->ignore_adler32 = 0; 2298 2299 settings->custom_zlib = 0; 2300 settings->custom_inflate = 0; 2301 settings->custom_context = 0; 2302 } 2303 2304 const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0}; 2305 2306 #endif /*LODEPNG_COMPILE_DECODER*/ 2307 2308 /* ////////////////////////////////////////////////////////////////////////// */ 2309 /* ////////////////////////////////////////////////////////////////////////// */ 2310 /* // End of Zlib related code. Begin of PNG related code. // */ 2311 /* ////////////////////////////////////////////////////////////////////////// */ 2312 /* ////////////////////////////////////////////////////////////////////////// */ 2313 2314 #ifdef LODEPNG_COMPILE_PNG 2315 2316 /* ////////////////////////////////////////////////////////////////////////// */ 2317 /* / CRC32 / */ 2318 /* ////////////////////////////////////////////////////////////////////////// */ 2319 2320 2321 #ifndef LODEPNG_NO_COMPILE_CRC 2322 /* CRC polynomial: 0xedb88320 */ 2323 static unsigned lodepng_crc32_table[256] = { 2324 0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u, 2325 249268274u, 2044508324u, 3772115230u, 2547177864u, 162941995u, 2125561021u, 3887607047u, 2428444049u, 2326 498536548u, 1789927666u, 4089016648u, 2227061214u, 450548861u, 1843258603u, 4107580753u, 2211677639u, 2327 325883990u, 1684777152u, 4251122042u, 2321926636u, 335633487u, 1661365465u, 4195302755u, 2366115317u, 2328 997073096u, 1281953886u, 3579855332u, 2724688242u, 1006888145u, 1258607687u, 3524101629u, 2768942443u, 2329 901097722u, 1119000684u, 3686517206u, 2898065728u, 853044451u, 1172266101u, 3705015759u, 2882616665u, 2330 651767980u, 1373503546u, 3369554304u, 3218104598u, 565507253u, 1454621731u, 3485111705u, 3099436303u, 2331 671266974u, 1594198024u, 3322730930u, 2970347812u, 795835527u, 1483230225u, 3244367275u, 3060149565u, 2332 1994146192u, 31158534u, 2563907772u, 4023717930u, 1907459465u, 112637215u, 2680153253u, 3904427059u, 2333 2013776290u, 251722036u, 2517215374u, 3775830040u, 2137656763u, 141376813u, 2439277719u, 3865271297u, 2334 1802195444u, 476864866u, 2238001368u, 4066508878u, 1812370925u, 453092731u, 2181625025u, 4111451223u, 2335 1706088902u, 314042704u, 2344532202u, 4240017532u, 1658658271u, 366619977u, 2362670323u, 4224994405u, 2336 1303535960u, 984961486u, 2747007092u, 3569037538u, 1256170817u, 1037604311u, 2765210733u, 3554079995u, 2337 1131014506u, 879679996u, 2909243462u, 3663771856u, 1141124467u, 855842277u, 2852801631u, 3708648649u, 2338 1342533948u, 654459306u, 3188396048u, 3373015174u, 1466479909u, 544179635u, 3110523913u, 3462522015u, 2339 1591671054u, 702138776u, 2966460450u, 3352799412u, 1504918807u, 783551873u, 3082640443u, 3233442989u, 2340 3988292384u, 2596254646u, 62317068u, 1957810842u, 3939845945u, 2647816111u, 81470997u, 1943803523u, 2341 3814918930u, 2489596804u, 225274430u, 2053790376u, 3826175755u, 2466906013u, 167816743u, 2097651377u, 2342 4027552580u, 2265490386u, 503444072u, 1762050814u, 4150417245u, 2154129355u, 426522225u, 1852507879u, 2343 4275313526u, 2312317920u, 282753626u, 1742555852u, 4189708143u, 2394877945u, 397917763u, 1622183637u, 2344 3604390888u, 2714866558u, 953729732u, 1340076626u, 3518719985u, 2797360999u, 1068828381u, 1219638859u, 2345 3624741850u, 2936675148u, 906185462u, 1090812512u, 3747672003u, 2825379669u, 829329135u, 1181335161u, 2346 3412177804u, 3160834842u, 628085408u, 1382605366u, 3423369109u, 3138078467u, 570562233u, 1426400815u, 2347 3317316542u, 2998733608u, 733239954u, 1555261956u, 3268935591u, 3050360625u, 752459403u, 1541320221u, 2348 2607071920u, 3965973030u, 1969922972u, 40735498u, 2617837225u, 3943577151u, 1913087877u, 83908371u, 2349 2512341634u, 3803740692u, 2075208622u, 213261112u, 2463272603u, 3855990285u, 2094854071u, 198958881u, 2350 2262029012u, 4057260610u, 1759359992u, 534414190u, 2176718541u, 4139329115u, 1873836001u, 414664567u, 2351 2282248934u, 4279200368u, 1711684554u, 285281116u, 2405801727u, 4167216745u, 1634467795u, 376229701u, 2352 2685067896u, 3608007406u, 1308918612u, 956543938u, 2808555105u, 3495958263u, 1231636301u, 1047427035u, 2353 2932959818u, 3654703836u, 1088359270u, 936918000u, 2847714899u, 3736837829u, 1202900863u, 817233897u, 2354 3183342108u, 3401237130u, 1404277552u, 615818150u, 3134207493u, 3453421203u, 1423857449u, 601450431u, 2355 3009837614u, 3294710456u, 1567103746u, 711928724u, 3020668471u, 3272380065u, 1510334235u, 755167117u 2356 }; 2357 2358 /*Return the CRC of the bytes buf[0..len-1].*/ 2359 unsigned lodepng_crc32(const unsigned char* data, size_t length) 2360 { 2361 unsigned r = 0xffffffffu; 2362 size_t i; 2363 for(i = 0; i < length; ++i) 2364 { 2365 r = lodepng_crc32_table[(r ^ data[i]) & 0xff] ^ (r >> 8); 2366 } 2367 return r ^ 0xffffffffu; 2368 } 2369 #else /* !LODEPNG_NO_COMPILE_CRC */ 2370 unsigned lodepng_crc32(const unsigned char* data, size_t length); 2371 #endif /* !LODEPNG_NO_COMPILE_CRC */ 2372 2373 /* ////////////////////////////////////////////////////////////////////////// */ 2374 /* / Reading and writing single bits and bytes from/to stream for LodePNG / */ 2375 /* ////////////////////////////////////////////////////////////////////////// */ 2376 2377 static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) 2378 { 2379 unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1); 2380 ++(*bitpointer); 2381 return result; 2382 } 2383 2384 static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) 2385 { 2386 unsigned result = 0; 2387 size_t i; 2388 for(i = 0 ; i < nbits; ++i) 2389 { 2390 result <<= 1; 2391 result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream); 2392 } 2393 return result; 2394 } 2395 2396 #ifdef LODEPNG_COMPILE_DECODER 2397 static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) 2398 { 2399 /*the current bit in bitstream must be 0 for this to work*/ 2400 if(bit) 2401 { 2402 /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/ 2403 bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); 2404 } 2405 ++(*bitpointer); 2406 } 2407 #endif /*LODEPNG_COMPILE_DECODER*/ 2408 2409 static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) 2410 { 2411 /*the current bit in bitstream may be 0 or 1 for this to work*/ 2412 if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7)))); 2413 else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7))); 2414 ++(*bitpointer); 2415 } 2416 2417 /* ////////////////////////////////////////////////////////////////////////// */ 2418 /* / PNG chunks / */ 2419 /* ////////////////////////////////////////////////////////////////////////// */ 2420 2421 unsigned lodepng_chunk_length(const unsigned char* chunk) 2422 { 2423 return lodepng_read32bitInt(&chunk[0]); 2424 } 2425 2426 void lodepng_chunk_type(char type[5], const unsigned char* chunk) 2427 { 2428 unsigned i; 2429 for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i]; 2430 type[4] = 0; /*null termination char*/ 2431 } 2432 2433 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) 2434 { 2435 if(strlen(type) != 4) return 0; 2436 return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]); 2437 } 2438 2439 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) 2440 { 2441 return((chunk[4] & 32) != 0); 2442 } 2443 2444 unsigned char lodepng_chunk_private(const unsigned char* chunk) 2445 { 2446 return((chunk[6] & 32) != 0); 2447 } 2448 2449 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) 2450 { 2451 return((chunk[7] & 32) != 0); 2452 } 2453 2454 unsigned char* lodepng_chunk_data(unsigned char* chunk) 2455 { 2456 return &chunk[8]; 2457 } 2458 2459 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) 2460 { 2461 return &chunk[8]; 2462 } 2463 2464 unsigned lodepng_chunk_check_crc(const unsigned char* chunk) 2465 { 2466 unsigned length = lodepng_chunk_length(chunk); 2467 unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]); 2468 /*the CRC is taken of the data and the 4 chunk type letters, not the length*/ 2469 unsigned checksum = lodepng_crc32(&chunk[4], length + 4); 2470 if(CRC != checksum) return 1; 2471 else return 0; 2472 } 2473 2474 void lodepng_chunk_generate_crc(unsigned char* chunk) 2475 { 2476 unsigned length = lodepng_chunk_length(chunk); 2477 unsigned CRC = lodepng_crc32(&chunk[4], length + 4); 2478 lodepng_set32bitInt(chunk + 8 + length, CRC); 2479 } 2480 2481 unsigned char* lodepng_chunk_next(unsigned char* chunk) 2482 { 2483 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; 2484 return &chunk[total_chunk_length]; 2485 } 2486 2487 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk) 2488 { 2489 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; 2490 return &chunk[total_chunk_length]; 2491 } 2492 2493 unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk) 2494 { 2495 unsigned i; 2496 unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; 2497 unsigned char *chunk_start, *new_buffer; 2498 size_t new_length = (*outlength) + total_chunk_length; 2499 if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/ 2500 2501 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length); 2502 if(!new_buffer) return 83; /*alloc fail*/ 2503 (*out) = new_buffer; 2504 (*outlength) = new_length; 2505 chunk_start = &(*out)[new_length - total_chunk_length]; 2506 2507 for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i]; 2508 2509 return 0; 2510 } 2511 2512 unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, 2513 const char* type, const unsigned char* data) 2514 { 2515 unsigned i; 2516 unsigned char *chunk, *new_buffer; 2517 size_t new_length = (*outlength) + length + 12; 2518 if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/ 2519 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length); 2520 if(!new_buffer) return 83; /*alloc fail*/ 2521 (*out) = new_buffer; 2522 (*outlength) = new_length; 2523 chunk = &(*out)[(*outlength) - length - 12]; 2524 2525 /*1: length*/ 2526 lodepng_set32bitInt(chunk, (unsigned)length); 2527 2528 /*2: chunk name (4 letters)*/ 2529 chunk[4] = (unsigned char)type[0]; 2530 chunk[5] = (unsigned char)type[1]; 2531 chunk[6] = (unsigned char)type[2]; 2532 chunk[7] = (unsigned char)type[3]; 2533 2534 /*3: the data*/ 2535 for(i = 0; i != length; ++i) chunk[8 + i] = data[i]; 2536 2537 /*4: CRC (of the chunkname characters and the data)*/ 2538 lodepng_chunk_generate_crc(chunk); 2539 2540 return 0; 2541 } 2542 2543 /* ////////////////////////////////////////////////////////////////////////// */ 2544 /* / Color types and such / */ 2545 /* ////////////////////////////////////////////////////////////////////////// */ 2546 2547 /*return type is a LodePNG error code*/ 2548 static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) /*bd = bitdepth*/ 2549 { 2550 switch(colortype) 2551 { 2552 case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/ 2553 case 2: if(!( bd == 8 || bd == 16)) return 37; break; /*RGB*/ 2554 case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/ 2555 case 4: if(!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/ 2556 case 6: if(!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/ 2557 default: return 31; 2558 } 2559 return 0; /*allowed color type / bits combination*/ 2560 } 2561 2562 static unsigned getNumColorChannels(LodePNGColorType colortype) 2563 { 2564 switch(colortype) 2565 { 2566 case 0: return 1; /*grey*/ 2567 case 2: return 3; /*RGB*/ 2568 case 3: return 1; /*palette*/ 2569 case 4: return 2; /*grey + alpha*/ 2570 case 6: return 4; /*RGBA*/ 2571 } 2572 return 0; /*unexisting color type*/ 2573 } 2574 2575 static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) 2576 { 2577 /*bits per pixel is amount of channels * bits per channel*/ 2578 return getNumColorChannels(colortype) * bitdepth; 2579 } 2580 2581 /* ////////////////////////////////////////////////////////////////////////// */ 2582 2583 void lodepng_color_mode_init(LodePNGColorMode* info) 2584 { 2585 info->key_defined = 0; 2586 info->key_r = info->key_g = info->key_b = 0; 2587 info->colortype = LCT_RGBA; 2588 info->bitdepth = 8; 2589 info->palette = 0; 2590 info->palettesize = 0; 2591 } 2592 2593 void lodepng_color_mode_cleanup(LodePNGColorMode* info) 2594 { 2595 lodepng_palette_clear(info); 2596 } 2597 2598 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) 2599 { 2600 size_t i; 2601 lodepng_color_mode_cleanup(dest); 2602 *dest = *source; 2603 if(source->palette) 2604 { 2605 dest->palette = (unsigned char*)lodepng_malloc(1024); 2606 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/ 2607 for(i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i]; 2608 } 2609 return 0; 2610 } 2611 2612 static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) 2613 { 2614 size_t i; 2615 if(a->colortype != b->colortype) return 0; 2616 if(a->bitdepth != b->bitdepth) return 0; 2617 if(a->key_defined != b->key_defined) return 0; 2618 if(a->key_defined) 2619 { 2620 if(a->key_r != b->key_r) return 0; 2621 if(a->key_g != b->key_g) return 0; 2622 if(a->key_b != b->key_b) return 0; 2623 } 2624 /*if one of the palette sizes is 0, then we consider it to be the same as the 2625 other: it means that e.g. the palette was not given by the user and should be 2626 considered the same as the palette inside the PNG.*/ 2627 if(1/*a->palettesize != 0 && b->palettesize != 0*/) { 2628 if(a->palettesize != b->palettesize) return 0; 2629 for(i = 0; i != a->palettesize * 4; ++i) 2630 { 2631 if(a->palette[i] != b->palette[i]) return 0; 2632 } 2633 } 2634 return 1; 2635 } 2636 2637 void lodepng_palette_clear(LodePNGColorMode* info) 2638 { 2639 if(info->palette) lodepng_free(info->palette); 2640 info->palette = 0; 2641 info->palettesize = 0; 2642 } 2643 2644 unsigned lodepng_palette_add(LodePNGColorMode* info, 2645 unsigned char r, unsigned char g, unsigned char b, unsigned char a) 2646 { 2647 unsigned char* data; 2648 /*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with 2649 the max of 256 colors, it'll have the exact alloc size*/ 2650 if(!info->palette) /*allocate palette if empty*/ 2651 { 2652 /*room for 256 colors with 4 bytes each*/ 2653 data = (unsigned char*)lodepng_realloc(info->palette, 1024); 2654 if(!data) return 83; /*alloc fail*/ 2655 else info->palette = data; 2656 } 2657 info->palette[4 * info->palettesize + 0] = r; 2658 info->palette[4 * info->palettesize + 1] = g; 2659 info->palette[4 * info->palettesize + 2] = b; 2660 info->palette[4 * info->palettesize + 3] = a; 2661 ++info->palettesize; 2662 return 0; 2663 } 2664 2665 unsigned lodepng_get_bpp(const LodePNGColorMode* info) 2666 { 2667 /*calculate bits per pixel out of colortype and bitdepth*/ 2668 return lodepng_get_bpp_lct(info->colortype, info->bitdepth); 2669 } 2670 2671 unsigned lodepng_get_channels(const LodePNGColorMode* info) 2672 { 2673 return getNumColorChannels(info->colortype); 2674 } 2675 2676 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) 2677 { 2678 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA; 2679 } 2680 2681 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) 2682 { 2683 return (info->colortype & 4) != 0; /*4 or 6*/ 2684 } 2685 2686 unsigned lodepng_is_palette_type(const LodePNGColorMode* info) 2687 { 2688 return info->colortype == LCT_PALETTE; 2689 } 2690 2691 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) 2692 { 2693 size_t i; 2694 for(i = 0; i != info->palettesize; ++i) 2695 { 2696 if(info->palette[i * 4 + 3] < 255) return 1; 2697 } 2698 return 0; 2699 } 2700 2701 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) 2702 { 2703 return info->key_defined 2704 || lodepng_is_alpha_type(info) 2705 || lodepng_has_palette_alpha(info); 2706 } 2707 2708 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) 2709 { 2710 /*will not overflow for any color type if roughly w * h < 268435455*/ 2711 size_t bpp = lodepng_get_bpp(color); 2712 size_t n = w * h; 2713 return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; 2714 } 2715 2716 size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) 2717 { 2718 /*will not overflow for any color type if roughly w * h < 268435455*/ 2719 size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth); 2720 size_t n = w * h; 2721 return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; 2722 } 2723 2724 2725 #ifdef LODEPNG_COMPILE_PNG 2726 #ifdef LODEPNG_COMPILE_DECODER 2727 /*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer*/ 2728 static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, const LodePNGColorMode* color) 2729 { 2730 /*will not overflow for any color type if roughly w * h < 268435455*/ 2731 size_t bpp = lodepng_get_bpp(color); 2732 size_t line = ((w / 8) * bpp) + ((w & 7) * bpp + 7) / 8; 2733 return h * line; 2734 } 2735 #endif /*LODEPNG_COMPILE_DECODER*/ 2736 #endif /*LODEPNG_COMPILE_PNG*/ 2737 2738 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 2739 2740 static void LodePNGUnknownChunks_init(LodePNGInfo* info) 2741 { 2742 unsigned i; 2743 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0; 2744 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0; 2745 } 2746 2747 static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) 2748 { 2749 unsigned i; 2750 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]); 2751 } 2752 2753 static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) 2754 { 2755 unsigned i; 2756 2757 LodePNGUnknownChunks_cleanup(dest); 2758 2759 for(i = 0; i != 3; ++i) 2760 { 2761 size_t j; 2762 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i]; 2763 dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]); 2764 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/ 2765 for(j = 0; j < src->unknown_chunks_size[i]; ++j) 2766 { 2767 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j]; 2768 } 2769 } 2770 2771 return 0; 2772 } 2773 2774 /******************************************************************************/ 2775 2776 static void LodePNGText_init(LodePNGInfo* info) 2777 { 2778 info->text_num = 0; 2779 info->text_keys = NULL; 2780 info->text_strings = NULL; 2781 } 2782 2783 static void LodePNGText_cleanup(LodePNGInfo* info) 2784 { 2785 size_t i; 2786 for(i = 0; i != info->text_num; ++i) 2787 { 2788 string_cleanup(&info->text_keys[i]); 2789 string_cleanup(&info->text_strings[i]); 2790 } 2791 lodepng_free(info->text_keys); 2792 lodepng_free(info->text_strings); 2793 } 2794 2795 static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) 2796 { 2797 size_t i = 0; 2798 dest->text_keys = 0; 2799 dest->text_strings = 0; 2800 dest->text_num = 0; 2801 for(i = 0; i != source->text_num; ++i) 2802 { 2803 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i])); 2804 } 2805 return 0; 2806 } 2807 2808 void lodepng_clear_text(LodePNGInfo* info) 2809 { 2810 LodePNGText_cleanup(info); 2811 } 2812 2813 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) 2814 { 2815 char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1))); 2816 char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num + 1))); 2817 if(!new_keys || !new_strings) 2818 { 2819 lodepng_free(new_keys); 2820 lodepng_free(new_strings); 2821 return 83; /*alloc fail*/ 2822 } 2823 2824 ++info->text_num; 2825 info->text_keys = new_keys; 2826 info->text_strings = new_strings; 2827 2828 string_init(&info->text_keys[info->text_num - 1]); 2829 string_set(&info->text_keys[info->text_num - 1], key); 2830 2831 string_init(&info->text_strings[info->text_num - 1]); 2832 string_set(&info->text_strings[info->text_num - 1], str); 2833 2834 return 0; 2835 } 2836 2837 /******************************************************************************/ 2838 2839 static void LodePNGIText_init(LodePNGInfo* info) 2840 { 2841 info->itext_num = 0; 2842 info->itext_keys = NULL; 2843 info->itext_langtags = NULL; 2844 info->itext_transkeys = NULL; 2845 info->itext_strings = NULL; 2846 } 2847 2848 static void LodePNGIText_cleanup(LodePNGInfo* info) 2849 { 2850 size_t i; 2851 for(i = 0; i != info->itext_num; ++i) 2852 { 2853 string_cleanup(&info->itext_keys[i]); 2854 string_cleanup(&info->itext_langtags[i]); 2855 string_cleanup(&info->itext_transkeys[i]); 2856 string_cleanup(&info->itext_strings[i]); 2857 } 2858 lodepng_free(info->itext_keys); 2859 lodepng_free(info->itext_langtags); 2860 lodepng_free(info->itext_transkeys); 2861 lodepng_free(info->itext_strings); 2862 } 2863 2864 static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) 2865 { 2866 size_t i = 0; 2867 dest->itext_keys = 0; 2868 dest->itext_langtags = 0; 2869 dest->itext_transkeys = 0; 2870 dest->itext_strings = 0; 2871 dest->itext_num = 0; 2872 for(i = 0; i != source->itext_num; ++i) 2873 { 2874 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i], 2875 source->itext_transkeys[i], source->itext_strings[i])); 2876 } 2877 return 0; 2878 } 2879 2880 void lodepng_clear_itext(LodePNGInfo* info) 2881 { 2882 LodePNGIText_cleanup(info); 2883 } 2884 2885 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, 2886 const char* transkey, const char* str) 2887 { 2888 char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1))); 2889 char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1))); 2890 char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1))); 2891 char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1))); 2892 if(!new_keys || !new_langtags || !new_transkeys || !new_strings) 2893 { 2894 lodepng_free(new_keys); 2895 lodepng_free(new_langtags); 2896 lodepng_free(new_transkeys); 2897 lodepng_free(new_strings); 2898 return 83; /*alloc fail*/ 2899 } 2900 2901 ++info->itext_num; 2902 info->itext_keys = new_keys; 2903 info->itext_langtags = new_langtags; 2904 info->itext_transkeys = new_transkeys; 2905 info->itext_strings = new_strings; 2906 2907 string_init(&info->itext_keys[info->itext_num - 1]); 2908 string_set(&info->itext_keys[info->itext_num - 1], key); 2909 2910 string_init(&info->itext_langtags[info->itext_num - 1]); 2911 string_set(&info->itext_langtags[info->itext_num - 1], langtag); 2912 2913 string_init(&info->itext_transkeys[info->itext_num - 1]); 2914 string_set(&info->itext_transkeys[info->itext_num - 1], transkey); 2915 2916 string_init(&info->itext_strings[info->itext_num - 1]); 2917 string_set(&info->itext_strings[info->itext_num - 1], str); 2918 2919 return 0; 2920 } 2921 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 2922 2923 void lodepng_info_init(LodePNGInfo* info) 2924 { 2925 lodepng_color_mode_init(&info->color); 2926 info->interlace_method = 0; 2927 info->compression_method = 0; 2928 info->filter_method = 0; 2929 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 2930 info->background_defined = 0; 2931 info->background_r = info->background_g = info->background_b = 0; 2932 2933 LodePNGText_init(info); 2934 LodePNGIText_init(info); 2935 2936 info->time_defined = 0; 2937 info->phys_defined = 0; 2938 2939 LodePNGUnknownChunks_init(info); 2940 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 2941 } 2942 2943 void lodepng_info_cleanup(LodePNGInfo* info) 2944 { 2945 lodepng_color_mode_cleanup(&info->color); 2946 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 2947 LodePNGText_cleanup(info); 2948 LodePNGIText_cleanup(info); 2949 2950 LodePNGUnknownChunks_cleanup(info); 2951 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 2952 } 2953 2954 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) 2955 { 2956 lodepng_info_cleanup(dest); 2957 *dest = *source; 2958 lodepng_color_mode_init(&dest->color); 2959 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color)); 2960 2961 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 2962 CERROR_TRY_RETURN(LodePNGText_copy(dest, source)); 2963 CERROR_TRY_RETURN(LodePNGIText_copy(dest, source)); 2964 2965 LodePNGUnknownChunks_init(dest); 2966 CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source)); 2967 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 2968 return 0; 2969 } 2970 2971 void lodepng_info_swap(LodePNGInfo* a, LodePNGInfo* b) 2972 { 2973 LodePNGInfo temp = *a; 2974 *a = *b; 2975 *b = temp; 2976 } 2977 2978 /* ////////////////////////////////////////////////////////////////////////// */ 2979 2980 /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/ 2981 static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) 2982 { 2983 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/ 2984 /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/ 2985 unsigned p = index & m; 2986 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/ 2987 in = in << (bits * (m - p)); 2988 if(p == 0) out[index * bits / 8] = in; 2989 else out[index * bits / 8] |= in; 2990 } 2991 2992 typedef struct ColorTree ColorTree; 2993 2994 /* 2995 One node of a color tree 2996 This is the data structure used to count the number of unique colors and to get a palette 2997 index for a color. It's like an octree, but because the alpha channel is used too, each 2998 node has 16 instead of 8 children. 2999 */ 3000 struct ColorTree 3001 { 3002 ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/ 3003 int index; /*the payload. Only has a meaningful value if this is in the last level*/ 3004 }; 3005 3006 static void color_tree_init(ColorTree* tree) 3007 { 3008 int i; 3009 for(i = 0; i != 16; ++i) tree->children[i] = 0; 3010 tree->index = -1; 3011 } 3012 3013 static void color_tree_cleanup(ColorTree* tree) 3014 { 3015 int i; 3016 for(i = 0; i != 16; ++i) 3017 { 3018 if(tree->children[i]) 3019 { 3020 color_tree_cleanup(tree->children[i]); 3021 lodepng_free(tree->children[i]); 3022 } 3023 } 3024 } 3025 3026 /*returns -1 if color not present, its index otherwise*/ 3027 static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) 3028 { 3029 int bit = 0; 3030 for(bit = 0; bit < 8; ++bit) 3031 { 3032 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); 3033 if(!tree->children[i]) return -1; 3034 else tree = tree->children[i]; 3035 } 3036 return tree ? tree->index : -1; 3037 } 3038 3039 #ifdef LODEPNG_COMPILE_ENCODER 3040 static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) 3041 { 3042 return color_tree_get(tree, r, g, b, a) >= 0; 3043 } 3044 #endif /*LODEPNG_COMPILE_ENCODER*/ 3045 3046 /*color is not allowed to already exist. 3047 Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")*/ 3048 static void color_tree_add(ColorTree* tree, 3049 unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index) 3050 { 3051 int bit; 3052 for(bit = 0; bit < 8; ++bit) 3053 { 3054 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); 3055 if(!tree->children[i]) 3056 { 3057 tree->children[i] = (ColorTree*)lodepng_malloc(sizeof(ColorTree)); 3058 color_tree_init(tree->children[i]); 3059 } 3060 tree = tree->children[i]; 3061 } 3062 tree->index = (int)index; 3063 } 3064 3065 /*put a pixel, given its RGBA color, into image of any color type*/ 3066 static unsigned rgba8ToPixel(unsigned char* out, size_t i, 3067 const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, 3068 unsigned char r, unsigned char g, unsigned char b, unsigned char a) 3069 { 3070 if(mode->colortype == LCT_GREY) 3071 { 3072 unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; 3073 if(mode->bitdepth == 8) out[i] = grey; 3074 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey; 3075 else 3076 { 3077 /*take the most significant bits of grey*/ 3078 grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1); 3079 addColorBits(out, i, mode->bitdepth, grey); 3080 } 3081 } 3082 else if(mode->colortype == LCT_RGB) 3083 { 3084 if(mode->bitdepth == 8) 3085 { 3086 out[i * 3 + 0] = r; 3087 out[i * 3 + 1] = g; 3088 out[i * 3 + 2] = b; 3089 } 3090 else 3091 { 3092 out[i * 6 + 0] = out[i * 6 + 1] = r; 3093 out[i * 6 + 2] = out[i * 6 + 3] = g; 3094 out[i * 6 + 4] = out[i * 6 + 5] = b; 3095 } 3096 } 3097 else if(mode->colortype == LCT_PALETTE) 3098 { 3099 int index = color_tree_get(tree, r, g, b, a); 3100 if(index < 0) return 82; /*color not in palette*/ 3101 if(mode->bitdepth == 8) out[i] = index; 3102 else addColorBits(out, i, mode->bitdepth, (unsigned)index); 3103 } 3104 else if(mode->colortype == LCT_GREY_ALPHA) 3105 { 3106 unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; 3107 if(mode->bitdepth == 8) 3108 { 3109 out[i * 2 + 0] = grey; 3110 out[i * 2 + 1] = a; 3111 } 3112 else if(mode->bitdepth == 16) 3113 { 3114 out[i * 4 + 0] = out[i * 4 + 1] = grey; 3115 out[i * 4 + 2] = out[i * 4 + 3] = a; 3116 } 3117 } 3118 else if(mode->colortype == LCT_RGBA) 3119 { 3120 if(mode->bitdepth == 8) 3121 { 3122 out[i * 4 + 0] = r; 3123 out[i * 4 + 1] = g; 3124 out[i * 4 + 2] = b; 3125 out[i * 4 + 3] = a; 3126 } 3127 else 3128 { 3129 out[i * 8 + 0] = out[i * 8 + 1] = r; 3130 out[i * 8 + 2] = out[i * 8 + 3] = g; 3131 out[i * 8 + 4] = out[i * 8 + 5] = b; 3132 out[i * 8 + 6] = out[i * 8 + 7] = a; 3133 } 3134 } 3135 3136 return 0; /*no error*/ 3137 } 3138 3139 /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/ 3140 static void rgba16ToPixel(unsigned char* out, size_t i, 3141 const LodePNGColorMode* mode, 3142 unsigned short r, unsigned short g, unsigned short b, unsigned short a) 3143 { 3144 if(mode->colortype == LCT_GREY) 3145 { 3146 unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; 3147 out[i * 2 + 0] = (grey >> 8) & 255; 3148 out[i * 2 + 1] = grey & 255; 3149 } 3150 else if(mode->colortype == LCT_RGB) 3151 { 3152 out[i * 6 + 0] = (r >> 8) & 255; 3153 out[i * 6 + 1] = r & 255; 3154 out[i * 6 + 2] = (g >> 8) & 255; 3155 out[i * 6 + 3] = g & 255; 3156 out[i * 6 + 4] = (b >> 8) & 255; 3157 out[i * 6 + 5] = b & 255; 3158 } 3159 else if(mode->colortype == LCT_GREY_ALPHA) 3160 { 3161 unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; 3162 out[i * 4 + 0] = (grey >> 8) & 255; 3163 out[i * 4 + 1] = grey & 255; 3164 out[i * 4 + 2] = (a >> 8) & 255; 3165 out[i * 4 + 3] = a & 255; 3166 } 3167 else if(mode->colortype == LCT_RGBA) 3168 { 3169 out[i * 8 + 0] = (r >> 8) & 255; 3170 out[i * 8 + 1] = r & 255; 3171 out[i * 8 + 2] = (g >> 8) & 255; 3172 out[i * 8 + 3] = g & 255; 3173 out[i * 8 + 4] = (b >> 8) & 255; 3174 out[i * 8 + 5] = b & 255; 3175 out[i * 8 + 6] = (a >> 8) & 255; 3176 out[i * 8 + 7] = a & 255; 3177 } 3178 } 3179 3180 /*Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/ 3181 static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, 3182 unsigned char* b, unsigned char* a, 3183 const unsigned char* in, size_t i, 3184 const LodePNGColorMode* mode) 3185 { 3186 if(mode->colortype == LCT_GREY) 3187 { 3188 if(mode->bitdepth == 8) 3189 { 3190 *r = *g = *b = in[i]; 3191 if(mode->key_defined && *r == mode->key_r) *a = 0; 3192 else *a = 255; 3193 } 3194 else if(mode->bitdepth == 16) 3195 { 3196 *r = *g = *b = in[i * 2 + 0]; 3197 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; 3198 else *a = 255; 3199 } 3200 else 3201 { 3202 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ 3203 size_t j = i * mode->bitdepth; 3204 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); 3205 *r = *g = *b = (value * 255) / highest; 3206 if(mode->key_defined && value == mode->key_r) *a = 0; 3207 else *a = 255; 3208 } 3209 } 3210 else if(mode->colortype == LCT_RGB) 3211 { 3212 if(mode->bitdepth == 8) 3213 { 3214 *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2]; 3215 if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0; 3216 else *a = 255; 3217 } 3218 else 3219 { 3220 *r = in[i * 6 + 0]; 3221 *g = in[i * 6 + 2]; 3222 *b = in[i * 6 + 4]; 3223 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r 3224 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g 3225 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; 3226 else *a = 255; 3227 } 3228 } 3229 else if(mode->colortype == LCT_PALETTE) 3230 { 3231 unsigned index; 3232 if(mode->bitdepth == 8) index = in[i]; 3233 else 3234 { 3235 size_t j = i * mode->bitdepth; 3236 index = readBitsFromReversedStream(&j, in, mode->bitdepth); 3237 } 3238 3239 if(index >= mode->palettesize) 3240 { 3241 /*This is an error according to the PNG spec, but common PNG decoders make it black instead. 3242 Done here too, slightly faster due to no error handling needed.*/ 3243 *r = *g = *b = 0; 3244 *a = 255; 3245 } 3246 else 3247 { 3248 *r = mode->palette[index * 4 + 0]; 3249 *g = mode->palette[index * 4 + 1]; 3250 *b = mode->palette[index * 4 + 2]; 3251 *a = mode->palette[index * 4 + 3]; 3252 } 3253 } 3254 else if(mode->colortype == LCT_GREY_ALPHA) 3255 { 3256 if(mode->bitdepth == 8) 3257 { 3258 *r = *g = *b = in[i * 2 + 0]; 3259 *a = in[i * 2 + 1]; 3260 } 3261 else 3262 { 3263 *r = *g = *b = in[i * 4 + 0]; 3264 *a = in[i * 4 + 2]; 3265 } 3266 } 3267 else if(mode->colortype == LCT_RGBA) 3268 { 3269 if(mode->bitdepth == 8) 3270 { 3271 *r = in[i * 4 + 0]; 3272 *g = in[i * 4 + 1]; 3273 *b = in[i * 4 + 2]; 3274 *a = in[i * 4 + 3]; 3275 } 3276 else 3277 { 3278 *r = in[i * 8 + 0]; 3279 *g = in[i * 8 + 2]; 3280 *b = in[i * 8 + 4]; 3281 *a = in[i * 8 + 6]; 3282 } 3283 } 3284 } 3285 3286 /*Similar to getPixelColorRGBA8, but with all the for loops inside of the color 3287 mode test cases, optimized to convert the colors much faster, when converting 3288 to RGBA or RGB with 8 bit per cannel. buffer must be RGBA or RGB output with 3289 enough memory, if has_alpha is true the output is RGBA. mode has the color mode 3290 of the input buffer.*/ 3291 static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels, 3292 unsigned has_alpha, const unsigned char* in, 3293 const LodePNGColorMode* mode) 3294 { 3295 unsigned num_channels = has_alpha ? 4 : 3; 3296 size_t i; 3297 if(mode->colortype == LCT_GREY) 3298 { 3299 if(mode->bitdepth == 8) 3300 { 3301 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3302 { 3303 buffer[0] = buffer[1] = buffer[2] = in[i]; 3304 if(has_alpha) buffer[3] = mode->key_defined && in[i] == mode->key_r ? 0 : 255; 3305 } 3306 } 3307 else if(mode->bitdepth == 16) 3308 { 3309 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3310 { 3311 buffer[0] = buffer[1] = buffer[2] = in[i * 2]; 3312 if(has_alpha) buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; 3313 } 3314 } 3315 else 3316 { 3317 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ 3318 size_t j = 0; 3319 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3320 { 3321 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); 3322 buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest; 3323 if(has_alpha) buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; 3324 } 3325 } 3326 } 3327 else if(mode->colortype == LCT_RGB) 3328 { 3329 if(mode->bitdepth == 8) 3330 { 3331 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3332 { 3333 buffer[0] = in[i * 3 + 0]; 3334 buffer[1] = in[i * 3 + 1]; 3335 buffer[2] = in[i * 3 + 2]; 3336 if(has_alpha) buffer[3] = mode->key_defined && buffer[0] == mode->key_r 3337 && buffer[1]== mode->key_g && buffer[2] == mode->key_b ? 0 : 255; 3338 } 3339 } 3340 else 3341 { 3342 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3343 { 3344 buffer[0] = in[i * 6 + 0]; 3345 buffer[1] = in[i * 6 + 2]; 3346 buffer[2] = in[i * 6 + 4]; 3347 if(has_alpha) buffer[3] = mode->key_defined 3348 && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r 3349 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g 3350 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255; 3351 } 3352 } 3353 } 3354 else if(mode->colortype == LCT_PALETTE) 3355 { 3356 unsigned index; 3357 size_t j = 0; 3358 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3359 { 3360 if(mode->bitdepth == 8) index = in[i]; 3361 else index = readBitsFromReversedStream(&j, in, mode->bitdepth); 3362 3363 if(index >= mode->palettesize) 3364 { 3365 /*This is an error according to the PNG spec, but most PNG decoders make it black instead. 3366 Done here too, slightly faster due to no error handling needed.*/ 3367 buffer[0] = buffer[1] = buffer[2] = 0; 3368 if(has_alpha) buffer[3] = 255; 3369 } 3370 else 3371 { 3372 buffer[0] = mode->palette[index * 4 + 0]; 3373 buffer[1] = mode->palette[index * 4 + 1]; 3374 buffer[2] = mode->palette[index * 4 + 2]; 3375 if(has_alpha) buffer[3] = mode->palette[index * 4 + 3]; 3376 } 3377 } 3378 } 3379 else if(mode->colortype == LCT_GREY_ALPHA) 3380 { 3381 if(mode->bitdepth == 8) 3382 { 3383 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3384 { 3385 buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0]; 3386 if(has_alpha) buffer[3] = in[i * 2 + 1]; 3387 } 3388 } 3389 else 3390 { 3391 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3392 { 3393 buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0]; 3394 if(has_alpha) buffer[3] = in[i * 4 + 2]; 3395 } 3396 } 3397 } 3398 else if(mode->colortype == LCT_RGBA) 3399 { 3400 if(mode->bitdepth == 8) 3401 { 3402 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3403 { 3404 buffer[0] = in[i * 4 + 0]; 3405 buffer[1] = in[i * 4 + 1]; 3406 buffer[2] = in[i * 4 + 2]; 3407 if(has_alpha) buffer[3] = in[i * 4 + 3]; 3408 } 3409 } 3410 else 3411 { 3412 for(i = 0; i != numpixels; ++i, buffer += num_channels) 3413 { 3414 buffer[0] = in[i * 8 + 0]; 3415 buffer[1] = in[i * 8 + 2]; 3416 buffer[2] = in[i * 8 + 4]; 3417 if(has_alpha) buffer[3] = in[i * 8 + 6]; 3418 } 3419 } 3420 } 3421 } 3422 3423 /*Get RGBA16 color of pixel with index i (y * width + x) from the raw image with 3424 given color type, but the given color type must be 16-bit itself.*/ 3425 static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a, 3426 const unsigned char* in, size_t i, const LodePNGColorMode* mode) 3427 { 3428 if(mode->colortype == LCT_GREY) 3429 { 3430 *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1]; 3431 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; 3432 else *a = 65535; 3433 } 3434 else if(mode->colortype == LCT_RGB) 3435 { 3436 *r = 256u * in[i * 6 + 0] + in[i * 6 + 1]; 3437 *g = 256u * in[i * 6 + 2] + in[i * 6 + 3]; 3438 *b = 256u * in[i * 6 + 4] + in[i * 6 + 5]; 3439 if(mode->key_defined 3440 && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r 3441 && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g 3442 && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; 3443 else *a = 65535; 3444 } 3445 else if(mode->colortype == LCT_GREY_ALPHA) 3446 { 3447 *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1]; 3448 *a = 256u * in[i * 4 + 2] + in[i * 4 + 3]; 3449 } 3450 else if(mode->colortype == LCT_RGBA) 3451 { 3452 *r = 256u * in[i * 8 + 0] + in[i * 8 + 1]; 3453 *g = 256u * in[i * 8 + 2] + in[i * 8 + 3]; 3454 *b = 256u * in[i * 8 + 4] + in[i * 8 + 5]; 3455 *a = 256u * in[i * 8 + 6] + in[i * 8 + 7]; 3456 } 3457 } 3458 3459 unsigned lodepng_convert(unsigned char* out, const unsigned char* in, 3460 const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, 3461 unsigned w, unsigned h) 3462 { 3463 size_t i; 3464 ColorTree tree; 3465 size_t numpixels = w * h; 3466 3467 if(lodepng_color_mode_equal(mode_out, mode_in)) 3468 { 3469 size_t numbytes = lodepng_get_raw_size(w, h, mode_in); 3470 for(i = 0; i != numbytes; ++i) out[i] = in[i]; 3471 return 0; 3472 } 3473 3474 if(mode_out->colortype == LCT_PALETTE) 3475 { 3476 size_t palettesize = mode_out->palettesize; 3477 const unsigned char* palette = mode_out->palette; 3478 size_t palsize = 1u << mode_out->bitdepth; 3479 /*if the user specified output palette but did not give the values, assume 3480 they want the values of the input color type (assuming that one is palette). 3481 Note that we never create a new palette ourselves.*/ 3482 if(palettesize == 0) 3483 { 3484 palettesize = mode_in->palettesize; 3485 palette = mode_in->palette; 3486 } 3487 if(palettesize < palsize) palsize = palettesize; 3488 color_tree_init(&tree); 3489 for(i = 0; i != palsize; ++i) 3490 { 3491 const unsigned char* p = &palette[i * 4]; 3492 color_tree_add(&tree, p[0], p[1], p[2], p[3], i); 3493 } 3494 } 3495 3496 if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) 3497 { 3498 for(i = 0; i != numpixels; ++i) 3499 { 3500 unsigned short r = 0, g = 0, b = 0, a = 0; 3501 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); 3502 rgba16ToPixel(out, i, mode_out, r, g, b, a); 3503 } 3504 } 3505 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) 3506 { 3507 getPixelColorsRGBA8(out, numpixels, 1, in, mode_in); 3508 } 3509 else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) 3510 { 3511 getPixelColorsRGBA8(out, numpixels, 0, in, mode_in); 3512 } 3513 else 3514 { 3515 unsigned char r = 0, g = 0, b = 0, a = 0; 3516 for(i = 0; i != numpixels; ++i) 3517 { 3518 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); 3519 CERROR_TRY_RETURN(rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a)); 3520 } 3521 } 3522 3523 if(mode_out->colortype == LCT_PALETTE) 3524 { 3525 color_tree_cleanup(&tree); 3526 } 3527 3528 return 0; /*no error*/ 3529 } 3530 3531 #ifdef LODEPNG_COMPILE_ENCODER 3532 3533 void lodepng_color_profile_init(LodePNGColorProfile* profile) 3534 { 3535 profile->colored = 0; 3536 profile->key = 0; 3537 profile->key_r = profile->key_g = profile->key_b = 0; 3538 profile->alpha = 0; 3539 profile->numcolors = 0; 3540 profile->bits = 1; 3541 } 3542 3543 /*function used for debug purposes with C++*/ 3544 /*void printColorProfile(LodePNGColorProfile* p) 3545 { 3546 std::cout << "colored: " << (int)p->colored << ", "; 3547 std::cout << "key: " << (int)p->key << ", "; 3548 std::cout << "key_r: " << (int)p->key_r << ", "; 3549 std::cout << "key_g: " << (int)p->key_g << ", "; 3550 std::cout << "key_b: " << (int)p->key_b << ", "; 3551 std::cout << "alpha: " << (int)p->alpha << ", "; 3552 std::cout << "numcolors: " << (int)p->numcolors << ", "; 3553 std::cout << "bits: " << (int)p->bits << std::endl; 3554 }*/ 3555 3556 /*Returns how many bits needed to represent given value (max 8 bit)*/ 3557 static unsigned getValueRequiredBits(unsigned char value) 3558 { 3559 if(value == 0 || value == 255) return 1; 3560 /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/ 3561 if(value % 17 == 0) return value % 85 == 0 ? 2 : 4; 3562 return 8; 3563 } 3564 3565 /*profile must already have been inited with mode. 3566 It's ok to set some parameters of profile to done already.*/ 3567 unsigned lodepng_get_color_profile(LodePNGColorProfile* profile, 3568 const unsigned char* in, unsigned w, unsigned h, 3569 const LodePNGColorMode* mode) 3570 { 3571 unsigned error = 0; 3572 size_t i; 3573 ColorTree tree; 3574 size_t numpixels = w * h; 3575 3576 unsigned colored_done = lodepng_is_greyscale_type(mode) ? 1 : 0; 3577 unsigned alpha_done = lodepng_can_have_alpha(mode) ? 0 : 1; 3578 unsigned numcolors_done = 0; 3579 unsigned bpp = lodepng_get_bpp(mode); 3580 unsigned bits_done = bpp == 1 ? 1 : 0; 3581 unsigned maxnumcolors = 257; 3582 unsigned sixteen = 0; 3583 if(bpp <= 8) maxnumcolors = bpp == 1 ? 2 : (bpp == 2 ? 4 : (bpp == 4 ? 16 : 256)); 3584 3585 color_tree_init(&tree); 3586 3587 /*Check if the 16-bit input is truly 16-bit*/ 3588 if(mode->bitdepth == 16) 3589 { 3590 unsigned short r, g, b, a; 3591 for(i = 0; i != numpixels; ++i) 3592 { 3593 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); 3594 if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) || 3595 (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/ 3596 { 3597 sixteen = 1; 3598 break; 3599 } 3600 } 3601 } 3602 3603 if(sixteen) 3604 { 3605 unsigned short r = 0, g = 0, b = 0, a = 0; 3606 profile->bits = 16; 3607 bits_done = numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ 3608 3609 for(i = 0; i != numpixels; ++i) 3610 { 3611 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); 3612 3613 if(!colored_done && (r != g || r != b)) 3614 { 3615 profile->colored = 1; 3616 colored_done = 1; 3617 } 3618 3619 if(!alpha_done) 3620 { 3621 unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b); 3622 if(a != 65535 && (a != 0 || (profile->key && !matchkey))) 3623 { 3624 profile->alpha = 1; 3625 profile->key = 0; 3626 alpha_done = 1; 3627 } 3628 else if(a == 0 && !profile->alpha && !profile->key) 3629 { 3630 profile->key = 1; 3631 profile->key_r = r; 3632 profile->key_g = g; 3633 profile->key_b = b; 3634 } 3635 else if(a == 65535 && profile->key && matchkey) 3636 { 3637 /* Color key cannot be used if an opaque pixel also has that RGB color. */ 3638 profile->alpha = 1; 3639 profile->key = 0; 3640 alpha_done = 1; 3641 } 3642 } 3643 if(alpha_done && numcolors_done && colored_done && bits_done) break; 3644 } 3645 3646 if(profile->key && !profile->alpha) 3647 { 3648 for(i = 0; i != numpixels; ++i) 3649 { 3650 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); 3651 if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b) 3652 { 3653 /* Color key cannot be used if an opaque pixel also has that RGB color. */ 3654 profile->alpha = 1; 3655 profile->key = 0; 3656 alpha_done = 1; 3657 } 3658 } 3659 } 3660 } 3661 else /* < 16-bit */ 3662 { 3663 unsigned char r = 0, g = 0, b = 0, a = 0; 3664 for(i = 0; i != numpixels; ++i) 3665 { 3666 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode); 3667 3668 if(!bits_done && profile->bits < 8) 3669 { 3670 /*only r is checked, < 8 bits is only relevant for greyscale*/ 3671 unsigned bits = getValueRequiredBits(r); 3672 if(bits > profile->bits) profile->bits = bits; 3673 } 3674 bits_done = (profile->bits >= bpp); 3675 3676 if(!colored_done && (r != g || r != b)) 3677 { 3678 profile->colored = 1; 3679 colored_done = 1; 3680 if(profile->bits < 8) profile->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/ 3681 } 3682 3683 if(!alpha_done) 3684 { 3685 unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b); 3686 if(a != 255 && (a != 0 || (profile->key && !matchkey))) 3687 { 3688 profile->alpha = 1; 3689 profile->key = 0; 3690 alpha_done = 1; 3691 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ 3692 } 3693 else if(a == 0 && !profile->alpha && !profile->key) 3694 { 3695 profile->key = 1; 3696 profile->key_r = r; 3697 profile->key_g = g; 3698 profile->key_b = b; 3699 } 3700 else if(a == 255 && profile->key && matchkey) 3701 { 3702 /* Color key cannot be used if an opaque pixel also has that RGB color. */ 3703 profile->alpha = 1; 3704 profile->key = 0; 3705 alpha_done = 1; 3706 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ 3707 } 3708 } 3709 3710 if(!numcolors_done) 3711 { 3712 if(!color_tree_has(&tree, r, g, b, a)) 3713 { 3714 color_tree_add(&tree, r, g, b, a, profile->numcolors); 3715 if(profile->numcolors < 256) 3716 { 3717 unsigned char* p = profile->palette; 3718 unsigned n = profile->numcolors; 3719 p[n * 4 + 0] = r; 3720 p[n * 4 + 1] = g; 3721 p[n * 4 + 2] = b; 3722 p[n * 4 + 3] = a; 3723 } 3724 ++profile->numcolors; 3725 numcolors_done = profile->numcolors >= maxnumcolors; 3726 } 3727 } 3728 3729 if(alpha_done && numcolors_done && colored_done && bits_done) break; 3730 } 3731 3732 if(profile->key && !profile->alpha) 3733 { 3734 for(i = 0; i != numpixels; ++i) 3735 { 3736 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode); 3737 if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b) 3738 { 3739 /* Color key cannot be used if an opaque pixel also has that RGB color. */ 3740 profile->alpha = 1; 3741 profile->key = 0; 3742 alpha_done = 1; 3743 if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ 3744 } 3745 } 3746 } 3747 3748 /*make the profile's key always 16-bit for consistency - repeat each byte twice*/ 3749 profile->key_r += (profile->key_r << 8); 3750 profile->key_g += (profile->key_g << 8); 3751 profile->key_b += (profile->key_b << 8); 3752 } 3753 3754 color_tree_cleanup(&tree); 3755 return error; 3756 } 3757 3758 /*Automatically chooses color type that gives smallest amount of bits in the 3759 output image, e.g. grey if there are only greyscale pixels, palette if there 3760 are less than 256 colors, ... 3761 Updates values of mode with a potentially smaller color model. mode_out should 3762 contain the user chosen color model, but will be overwritten with the new chosen one.*/ 3763 unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out, 3764 const unsigned char* image, unsigned w, unsigned h, 3765 const LodePNGColorMode* mode_in) 3766 { 3767 LodePNGColorProfile prof; 3768 unsigned error = 0; 3769 unsigned i, n, palettebits, palette_ok; 3770 3771 lodepng_color_profile_init(&prof); 3772 error = lodepng_get_color_profile(&prof, image, w, h, mode_in); 3773 if(error) return error; 3774 mode_out->key_defined = 0; 3775 3776 if(prof.key && w * h <= 16) 3777 { 3778 prof.alpha = 1; /*too few pixels to justify tRNS chunk overhead*/ 3779 prof.key = 0; 3780 if(prof.bits < 8) prof.bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/ 3781 } 3782 n = prof.numcolors; 3783 palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8)); 3784 palette_ok = n <= 256 && prof.bits <= 8; 3785 if(w * h < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/ 3786 if(!prof.colored && prof.bits <= palettebits) palette_ok = 0; /*grey is less overhead*/ 3787 3788 if(palette_ok) 3789 { 3790 unsigned char* p = prof.palette; 3791 lodepng_palette_clear(mode_out); /*remove potential earlier palette*/ 3792 for(i = 0; i != prof.numcolors; ++i) 3793 { 3794 error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]); 3795 if(error) break; 3796 } 3797 3798 mode_out->colortype = LCT_PALETTE; 3799 mode_out->bitdepth = palettebits; 3800 3801 if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize 3802 && mode_in->bitdepth == mode_out->bitdepth) 3803 { 3804 /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/ 3805 lodepng_color_mode_cleanup(mode_out); 3806 lodepng_color_mode_copy(mode_out, mode_in); 3807 } 3808 } 3809 else /*8-bit or 16-bit per channel*/ 3810 { 3811 mode_out->bitdepth = prof.bits; 3812 mode_out->colortype = prof.alpha ? (prof.colored ? LCT_RGBA : LCT_GREY_ALPHA) 3813 : (prof.colored ? LCT_RGB : LCT_GREY); 3814 3815 if(prof.key) 3816 { 3817 unsigned mask = (1u << mode_out->bitdepth) - 1u; /*profile always uses 16-bit, mask converts it*/ 3818 mode_out->key_r = prof.key_r & mask; 3819 mode_out->key_g = prof.key_g & mask; 3820 mode_out->key_b = prof.key_b & mask; 3821 mode_out->key_defined = 1; 3822 } 3823 } 3824 3825 return error; 3826 } 3827 3828 #endif /* #ifdef LODEPNG_COMPILE_ENCODER */ 3829 3830 /* 3831 Paeth predicter, used by PNG filter type 4 3832 The parameters are of type short, but should come from unsigned chars, the shorts 3833 are only needed to make the paeth calculation correct. 3834 */ 3835 static unsigned char paethPredictor(short a, short b, short c) 3836 { 3837 short pa = abs(b - c); 3838 short pb = abs(a - c); 3839 short pc = abs(a + b - c - c); 3840 3841 if(pc < pa && pc < pb) return (unsigned char)c; 3842 else if(pb < pa) return (unsigned char)b; 3843 else return (unsigned char)a; 3844 } 3845 3846 /*shared values used by multiple Adam7 related functions*/ 3847 3848 static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/ 3849 static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/ 3850 static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/ 3851 static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/ 3852 3853 /* 3854 Outputs various dimensions and positions in the image related to the Adam7 reduced images. 3855 passw: output containing the width of the 7 passes 3856 passh: output containing the height of the 7 passes 3857 filter_passstart: output containing the index of the start and end of each 3858 reduced image with filter bytes 3859 padded_passstart output containing the index of the start and end of each 3860 reduced image when without filter bytes but with padded scanlines 3861 passstart: output containing the index of the start and end of each reduced 3862 image without padding between scanlines, but still padding between the images 3863 w, h: width and height of non-interlaced image 3864 bpp: bits per pixel 3865 "padded" is only relevant if bpp is less than 8 and a scanline or image does not 3866 end at a full byte 3867 */ 3868 static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], 3869 size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) 3870 { 3871 /*the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass*/ 3872 unsigned i; 3873 3874 /*calculate width and height in pixels of each pass*/ 3875 for(i = 0; i != 7; ++i) 3876 { 3877 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; 3878 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; 3879 if(passw[i] == 0) passh[i] = 0; 3880 if(passh[i] == 0) passw[i] = 0; 3881 } 3882 3883 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0; 3884 for(i = 0; i != 7; ++i) 3885 { 3886 /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ 3887 filter_passstart[i + 1] = filter_passstart[i] 3888 + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); 3889 /*bits padded if needed to fill full byte at end of each scanline*/ 3890 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); 3891 /*only padded at end of reduced image*/ 3892 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; 3893 } 3894 } 3895 3896 #ifdef LODEPNG_COMPILE_DECODER 3897 3898 /* ////////////////////////////////////////////////////////////////////////// */ 3899 /* / PNG Decoder / */ 3900 /* ////////////////////////////////////////////////////////////////////////// */ 3901 3902 /*read the information from the header and store it in the LodePNGInfo. return value is error*/ 3903 unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, 3904 const unsigned char* in, size_t insize) 3905 { 3906 LodePNGInfo* info = &state->info_png; 3907 if(insize == 0 || in == 0) 3908 { 3909 CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ 3910 } 3911 if(insize < 33) 3912 { 3913 CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/ 3914 } 3915 3916 /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/ 3917 lodepng_info_cleanup(info); 3918 lodepng_info_init(info); 3919 3920 if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 3921 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) 3922 { 3923 CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/ 3924 } 3925 if(lodepng_chunk_length(in + 8) != 13) 3926 { 3927 CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/ 3928 } 3929 if(!lodepng_chunk_type_equals(in + 8, "IHDR")) 3930 { 3931 CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ 3932 } 3933 3934 /*read the values given in the header*/ 3935 *w = lodepng_read32bitInt(&in[16]); 3936 *h = lodepng_read32bitInt(&in[20]); 3937 info->color.bitdepth = in[24]; 3938 info->color.colortype = (LodePNGColorType)in[25]; 3939 info->compression_method = in[26]; 3940 info->filter_method = in[27]; 3941 info->interlace_method = in[28]; 3942 3943 if(*w == 0 || *h == 0) 3944 { 3945 CERROR_RETURN_ERROR(state->error, 93); 3946 } 3947 3948 if(!state->decoder.ignore_crc) 3949 { 3950 unsigned CRC = lodepng_read32bitInt(&in[29]); 3951 unsigned checksum = lodepng_crc32(&in[12], 17); 3952 if(CRC != checksum) 3953 { 3954 CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ 3955 } 3956 } 3957 3958 /*error: only compression method 0 is allowed in the specification*/ 3959 if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); 3960 /*error: only filter method 0 is allowed in the specification*/ 3961 if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33); 3962 /*error: only interlace methods 0 and 1 exist in the specification*/ 3963 if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); 3964 3965 state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); 3966 return state->error; 3967 } 3968 3969 static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, 3970 size_t bytewidth, unsigned char filterType, size_t length) 3971 { 3972 /* 3973 For PNG filter method 0 3974 unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte, 3975 the filter works byte per byte (bytewidth = 1) 3976 precon is the previous unfiltered scanline, recon the result, scanline the current one 3977 the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead 3978 recon and scanline MAY be the same memory address! precon must be disjoint. 3979 */ 3980 3981 size_t i; 3982 switch(filterType) 3983 { 3984 case 0: 3985 for(i = 0; i != length; ++i) recon[i] = scanline[i]; 3986 break; 3987 case 1: 3988 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i]; 3989 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth]; 3990 break; 3991 case 2: 3992 if(precon) 3993 { 3994 for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i]; 3995 } 3996 else 3997 { 3998 for(i = 0; i != length; ++i) recon[i] = scanline[i]; 3999 } 4000 break; 4001 case 3: 4002 if(precon) 4003 { 4004 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1); 4005 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >> 1); 4006 } 4007 else 4008 { 4009 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i]; 4010 for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1); 4011 } 4012 break; 4013 case 4: 4014 if(precon) 4015 { 4016 for(i = 0; i != bytewidth; ++i) 4017 { 4018 recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/ 4019 } 4020 for(i = bytewidth; i < length; ++i) 4021 { 4022 recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); 4023 } 4024 } 4025 else 4026 { 4027 for(i = 0; i != bytewidth; ++i) 4028 { 4029 recon[i] = scanline[i]; 4030 } 4031 for(i = bytewidth; i < length; ++i) 4032 { 4033 /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/ 4034 recon[i] = (scanline[i] + recon[i - bytewidth]); 4035 } 4036 } 4037 break; 4038 default: return 36; /*error: unexisting filter type given*/ 4039 } 4040 return 0; 4041 } 4042 4043 static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) 4044 { 4045 /* 4046 For PNG filter method 0 4047 this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times) 4048 out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline 4049 w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel 4050 in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes) 4051 */ 4052 4053 unsigned y; 4054 unsigned char* prevline = 0; 4055 4056 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ 4057 size_t bytewidth = (bpp + 7) / 8; 4058 size_t linebytes = (w * bpp + 7) / 8; 4059 4060 for(y = 0; y < h; ++y) 4061 { 4062 size_t outindex = linebytes * y; 4063 size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ 4064 unsigned char filterType = in[inindex]; 4065 4066 CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes)); 4067 4068 prevline = &out[outindex]; 4069 } 4070 4071 return 0; 4072 } 4073 4074 /* 4075 in: Adam7 interlaced image, with no padding bits between scanlines, but between 4076 reduced images so that each reduced image starts at a byte. 4077 out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h 4078 bpp: bits per pixel 4079 out has the following size in bits: w * h * bpp. 4080 in is possibly bigger due to padding bits between reduced images. 4081 out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation 4082 (because that's likely a little bit faster) 4083 NOTE: comments about padding bits are only relevant if bpp < 8 4084 */ 4085 static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) 4086 { 4087 unsigned passw[7], passh[7]; 4088 size_t filter_passstart[8], padded_passstart[8], passstart[8]; 4089 unsigned i; 4090 4091 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); 4092 4093 if(bpp >= 8) 4094 { 4095 for(i = 0; i != 7; ++i) 4096 { 4097 unsigned x, y, b; 4098 size_t bytewidth = bpp / 8; 4099 for(y = 0; y < passh[i]; ++y) 4100 for(x = 0; x < passw[i]; ++x) 4101 { 4102 size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth; 4103 size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; 4104 for(b = 0; b < bytewidth; ++b) 4105 { 4106 out[pixeloutstart + b] = in[pixelinstart + b]; 4107 } 4108 } 4109 } 4110 } 4111 else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ 4112 { 4113 for(i = 0; i != 7; ++i) 4114 { 4115 unsigned x, y, b; 4116 unsigned ilinebits = bpp * passw[i]; 4117 unsigned olinebits = bpp * w; 4118 size_t obp, ibp; /*bit pointers (for out and in buffer)*/ 4119 for(y = 0; y < passh[i]; ++y) 4120 for(x = 0; x < passw[i]; ++x) 4121 { 4122 ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp); 4123 obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; 4124 for(b = 0; b < bpp; ++b) 4125 { 4126 unsigned char bit = readBitFromReversedStream(&ibp, in); 4127 /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/ 4128 setBitOfReversedStream0(&obp, out, bit); 4129 } 4130 } 4131 } 4132 } 4133 } 4134 4135 static void removePaddingBits(unsigned char* out, const unsigned char* in, 4136 size_t olinebits, size_t ilinebits, unsigned h) 4137 { 4138 /* 4139 After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need 4140 to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers 4141 for the Adam7 code, the color convert code and the output to the user. 4142 in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must 4143 have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits 4144 also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7 4145 only useful if (ilinebits - olinebits) is a value in the range 1..7 4146 */ 4147 unsigned y; 4148 size_t diff = ilinebits - olinebits; 4149 size_t ibp = 0, obp = 0; /*input and output bit pointers*/ 4150 for(y = 0; y < h; ++y) 4151 { 4152 size_t x; 4153 for(x = 0; x < olinebits; ++x) 4154 { 4155 unsigned char bit = readBitFromReversedStream(&ibp, in); 4156 setBitOfReversedStream(&obp, out, bit); 4157 } 4158 ibp += diff; 4159 } 4160 } 4161 4162 /*out must be buffer big enough to contain full image, and in must contain the full decompressed data from 4163 the IDAT chunks (with filter index bytes and possible padding bits) 4164 return value is error*/ 4165 static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, 4166 unsigned w, unsigned h, const LodePNGInfo* info_png) 4167 { 4168 /* 4169 This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. 4170 Steps: 4171 *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8) 4172 *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace 4173 NOTE: the in buffer will be overwritten with intermediate data! 4174 */ 4175 unsigned bpp = lodepng_get_bpp(&info_png->color); 4176 if(bpp == 0) return 31; /*error: invalid colortype*/ 4177 4178 if(info_png->interlace_method == 0) 4179 { 4180 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) 4181 { 4182 CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp)); 4183 removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h); 4184 } 4185 /*we can immediately filter into the out buffer, no other steps needed*/ 4186 else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp)); 4187 } 4188 else /*interlace_method is 1 (Adam7)*/ 4189 { 4190 unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; 4191 unsigned i; 4192 4193 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); 4194 4195 for(i = 0; i != 7; ++i) 4196 { 4197 CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp)); 4198 /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, 4199 move bytes instead of bits or move not at all*/ 4200 if(bpp < 8) 4201 { 4202 /*remove padding bits in scanlines; after this there still may be padding 4203 bits between the different reduced images: each reduced image still starts nicely at a byte*/ 4204 removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, 4205 ((passw[i] * bpp + 7) / 8) * 8, passh[i]); 4206 } 4207 } 4208 4209 Adam7_deinterlace(out, in, w, h, bpp); 4210 } 4211 4212 return 0; 4213 } 4214 4215 static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) 4216 { 4217 unsigned pos = 0, i; 4218 if(color->palette) lodepng_free(color->palette); 4219 color->palettesize = chunkLength / 3; 4220 color->palette = (unsigned char*)lodepng_malloc(4 * color->palettesize); 4221 if(!color->palette && color->palettesize) 4222 { 4223 color->palettesize = 0; 4224 return 83; /*alloc fail*/ 4225 } 4226 if(color->palettesize > 256) return 38; /*error: palette too big*/ 4227 4228 for(i = 0; i != color->palettesize; ++i) 4229 { 4230 color->palette[4 * i + 0] = data[pos++]; /*R*/ 4231 color->palette[4 * i + 1] = data[pos++]; /*G*/ 4232 color->palette[4 * i + 2] = data[pos++]; /*B*/ 4233 color->palette[4 * i + 3] = 255; /*alpha*/ 4234 } 4235 4236 return 0; /* OK */ 4237 } 4238 4239 static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) 4240 { 4241 unsigned i; 4242 if(color->colortype == LCT_PALETTE) 4243 { 4244 /*error: more alpha values given than there are palette entries*/ 4245 if(chunkLength > color->palettesize) return 38; 4246 4247 for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i]; 4248 } 4249 else if(color->colortype == LCT_GREY) 4250 { 4251 /*error: this chunk must be 2 bytes for greyscale image*/ 4252 if(chunkLength != 2) return 30; 4253 4254 color->key_defined = 1; 4255 color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1]; 4256 } 4257 else if(color->colortype == LCT_RGB) 4258 { 4259 /*error: this chunk must be 6 bytes for RGB image*/ 4260 if(chunkLength != 6) return 41; 4261 4262 color->key_defined = 1; 4263 color->key_r = 256u * data[0] + data[1]; 4264 color->key_g = 256u * data[2] + data[3]; 4265 color->key_b = 256u * data[4] + data[5]; 4266 } 4267 else return 42; /*error: tRNS chunk not allowed for other color models*/ 4268 4269 return 0; /* OK */ 4270 } 4271 4272 4273 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4274 /*background color chunk (bKGD)*/ 4275 static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) 4276 { 4277 if(info->color.colortype == LCT_PALETTE) 4278 { 4279 /*error: this chunk must be 1 byte for indexed color image*/ 4280 if(chunkLength != 1) return 43; 4281 4282 info->background_defined = 1; 4283 info->background_r = info->background_g = info->background_b = data[0]; 4284 } 4285 else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) 4286 { 4287 /*error: this chunk must be 2 bytes for greyscale image*/ 4288 if(chunkLength != 2) return 44; 4289 4290 info->background_defined = 1; 4291 info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1]; 4292 } 4293 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) 4294 { 4295 /*error: this chunk must be 6 bytes for greyscale image*/ 4296 if(chunkLength != 6) return 45; 4297 4298 info->background_defined = 1; 4299 info->background_r = 256u * data[0] + data[1]; 4300 info->background_g = 256u * data[2] + data[3]; 4301 info->background_b = 256u * data[4] + data[5]; 4302 } 4303 4304 return 0; /* OK */ 4305 } 4306 4307 /*text chunk (tEXt)*/ 4308 static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) 4309 { 4310 unsigned error = 0; 4311 char *key = 0, *str = 0; 4312 unsigned i; 4313 4314 while(!error) /*not really a while loop, only used to break on error*/ 4315 { 4316 unsigned length, string2_begin; 4317 4318 length = 0; 4319 while(length < chunkLength && data[length] != 0) ++length; 4320 /*even though it's not allowed by the standard, no error is thrown if 4321 there's no null termination char, if the text is empty*/ 4322 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/ 4323 4324 key = (char*)lodepng_malloc(length + 1); 4325 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ 4326 4327 key[length] = 0; 4328 for(i = 0; i != length; ++i) key[i] = (char)data[i]; 4329 4330 string2_begin = length + 1; /*skip keyword null terminator*/ 4331 4332 length = chunkLength < string2_begin ? 0 : chunkLength - string2_begin; 4333 str = (char*)lodepng_malloc(length + 1); 4334 if(!str) CERROR_BREAK(error, 83); /*alloc fail*/ 4335 4336 str[length] = 0; 4337 for(i = 0; i != length; ++i) str[i] = (char)data[string2_begin + i]; 4338 4339 error = lodepng_add_text(info, key, str); 4340 4341 break; 4342 } 4343 4344 lodepng_free(key); 4345 lodepng_free(str); 4346 4347 return error; 4348 } 4349 4350 /*compressed text chunk (zTXt)*/ 4351 static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, 4352 const unsigned char* data, size_t chunkLength) 4353 { 4354 unsigned error = 0; 4355 unsigned i; 4356 4357 unsigned length, string2_begin; 4358 char *key = 0; 4359 ucvector decoded; 4360 4361 ucvector_init(&decoded); 4362 4363 while(!error) /*not really a while loop, only used to break on error*/ 4364 { 4365 for(length = 0; length < chunkLength && data[length] != 0; ++length) ; 4366 if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ 4367 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/ 4368 4369 key = (char*)lodepng_malloc(length + 1); 4370 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ 4371 4372 key[length] = 0; 4373 for(i = 0; i != length; ++i) key[i] = (char)data[i]; 4374 4375 if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ 4376 4377 string2_begin = length + 2; 4378 if(string2_begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ 4379 4380 length = chunkLength - string2_begin; 4381 /*will fail if zlib error, e.g. if length is too small*/ 4382 error = zlib_decompress(&decoded.data, &decoded.size, 4383 (unsigned char*)(&data[string2_begin]), 4384 length, zlibsettings); 4385 if(error) break; 4386 ucvector_push_back(&decoded, 0); 4387 4388 error = lodepng_add_text(info, key, (char*)decoded.data); 4389 4390 break; 4391 } 4392 4393 lodepng_free(key); 4394 ucvector_cleanup(&decoded); 4395 4396 return error; 4397 } 4398 4399 /*international text chunk (iTXt)*/ 4400 static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, 4401 const unsigned char* data, size_t chunkLength) 4402 { 4403 unsigned error = 0; 4404 unsigned i; 4405 4406 unsigned length, begin, compressed; 4407 char *key = 0, *langtag = 0, *transkey = 0; 4408 ucvector decoded; 4409 ucvector_init(&decoded); 4410 4411 while(!error) /*not really a while loop, only used to break on error*/ 4412 { 4413 /*Quick check if the chunk length isn't too small. Even without check 4414 it'd still fail with other error checks below if it's too short. This just gives a different error code.*/ 4415 if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/ 4416 4417 /*read the key*/ 4418 for(length = 0; length < chunkLength && data[length] != 0; ++length) ; 4419 if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/ 4420 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/ 4421 4422 key = (char*)lodepng_malloc(length + 1); 4423 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ 4424 4425 key[length] = 0; 4426 for(i = 0; i != length; ++i) key[i] = (char)data[i]; 4427 4428 /*read the compression method*/ 4429 compressed = data[length + 1]; 4430 if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ 4431 4432 /*even though it's not allowed by the standard, no error is thrown if 4433 there's no null termination char, if the text is empty for the next 3 texts*/ 4434 4435 /*read the langtag*/ 4436 begin = length + 3; 4437 length = 0; 4438 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length; 4439 4440 langtag = (char*)lodepng_malloc(length + 1); 4441 if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/ 4442 4443 langtag[length] = 0; 4444 for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i]; 4445 4446 /*read the transkey*/ 4447 begin += length + 1; 4448 length = 0; 4449 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length; 4450 4451 transkey = (char*)lodepng_malloc(length + 1); 4452 if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/ 4453 4454 transkey[length] = 0; 4455 for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i]; 4456 4457 /*read the actual text*/ 4458 begin += length + 1; 4459 4460 length = chunkLength < begin ? 0 : chunkLength - begin; 4461 4462 if(compressed) 4463 { 4464 /*will fail if zlib error, e.g. if length is too small*/ 4465 error = zlib_decompress(&decoded.data, &decoded.size, 4466 (unsigned char*)(&data[begin]), 4467 length, zlibsettings); 4468 if(error) break; 4469 if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size; 4470 ucvector_push_back(&decoded, 0); 4471 } 4472 else 4473 { 4474 if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/); 4475 4476 decoded.data[length] = 0; 4477 for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i]; 4478 } 4479 4480 error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data); 4481 4482 break; 4483 } 4484 4485 lodepng_free(key); 4486 lodepng_free(langtag); 4487 lodepng_free(transkey); 4488 ucvector_cleanup(&decoded); 4489 4490 return error; 4491 } 4492 4493 static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) 4494 { 4495 if(chunkLength != 7) return 73; /*invalid tIME chunk size*/ 4496 4497 info->time_defined = 1; 4498 info->time.year = 256u * data[0] + data[1]; 4499 info->time.month = data[2]; 4500 info->time.day = data[3]; 4501 info->time.hour = data[4]; 4502 info->time.minute = data[5]; 4503 info->time.second = data[6]; 4504 4505 return 0; /* OK */ 4506 } 4507 4508 static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) 4509 { 4510 if(chunkLength != 9) return 74; /*invalid pHYs chunk size*/ 4511 4512 info->phys_defined = 1; 4513 info->phys_x = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3]; 4514 info->phys_y = 16777216u * data[4] + 65536u * data[5] + 256u * data[6] + data[7]; 4515 info->phys_unit = data[8]; 4516 4517 return 0; /* OK */ 4518 } 4519 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4520 4521 /*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/ 4522 static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, 4523 LodePNGState* state, 4524 const unsigned char* in, size_t insize) 4525 { 4526 unsigned char IEND = 0; 4527 const unsigned char* chunk; 4528 size_t i; 4529 ucvector idat; /*the data from idat chunks*/ 4530 ucvector scanlines; 4531 size_t predict; 4532 size_t numpixels; 4533 size_t outsize = 0; 4534 4535 /*for unknown chunk order*/ 4536 unsigned unknown = 0; 4537 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4538 unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/ 4539 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4540 4541 /*provide some proper output values if error will happen*/ 4542 *out = 0; 4543 4544 state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/ 4545 if(state->error) return; 4546 4547 numpixels = *w * *h; 4548 4549 /*multiplication overflow*/ 4550 if(*h != 0 && numpixels / *h != *w) CERROR_RETURN(state->error, 92); 4551 /*multiplication overflow possible further below. Allows up to 2^31-1 pixel 4552 bytes with 16-bit RGBA, the rest is room for filter bytes.*/ 4553 if(numpixels > 268435455) CERROR_RETURN(state->error, 92); 4554 4555 ucvector_init(&idat); 4556 chunk = &in[33]; /*first byte of the first chunk after the header*/ 4557 4558 /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. 4559 IDAT data is put at the start of the in buffer*/ 4560 while(!IEND && !state->error) 4561 { 4562 unsigned chunkLength; 4563 const unsigned char* data; /*the data in the chunk*/ 4564 4565 /*error: size of the in buffer too small to contain next chunk*/ 4566 if((size_t)((chunk - in) + 12) > insize || chunk < in) CERROR_BREAK(state->error, 30); 4567 4568 /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/ 4569 chunkLength = lodepng_chunk_length(chunk); 4570 /*error: chunk length larger than the max PNG chunk size*/ 4571 if(chunkLength > 2147483647) CERROR_BREAK(state->error, 63); 4572 4573 if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) 4574 { 4575 CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/ 4576 } 4577 4578 data = lodepng_chunk_data_const(chunk); 4579 4580 /*IDAT chunk, containing compressed image data*/ 4581 if(lodepng_chunk_type_equals(chunk, "IDAT")) 4582 { 4583 size_t oldsize = idat.size; 4584 if(!ucvector_resize(&idat, oldsize + chunkLength)) CERROR_BREAK(state->error, 83 /*alloc fail*/); 4585 for(i = 0; i != chunkLength; ++i) idat.data[oldsize + i] = data[i]; 4586 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4587 critical_pos = 3; 4588 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4589 } 4590 /*IEND chunk*/ 4591 else if(lodepng_chunk_type_equals(chunk, "IEND")) 4592 { 4593 IEND = 1; 4594 } 4595 /*palette chunk (PLTE)*/ 4596 else if(lodepng_chunk_type_equals(chunk, "PLTE")) 4597 { 4598 state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength); 4599 if(state->error) break; 4600 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4601 critical_pos = 2; 4602 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4603 } 4604 /*palette transparency chunk (tRNS)*/ 4605 else if(lodepng_chunk_type_equals(chunk, "tRNS")) 4606 { 4607 state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength); 4608 if(state->error) break; 4609 } 4610 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4611 /*background color chunk (bKGD)*/ 4612 else if(lodepng_chunk_type_equals(chunk, "bKGD")) 4613 { 4614 state->error = readChunk_bKGD(&state->info_png, data, chunkLength); 4615 if(state->error) break; 4616 } 4617 /*text chunk (tEXt)*/ 4618 else if(lodepng_chunk_type_equals(chunk, "tEXt")) 4619 { 4620 if(state->decoder.read_text_chunks) 4621 { 4622 state->error = readChunk_tEXt(&state->info_png, data, chunkLength); 4623 if(state->error) break; 4624 } 4625 } 4626 /*compressed text chunk (zTXt)*/ 4627 else if(lodepng_chunk_type_equals(chunk, "zTXt")) 4628 { 4629 if(state->decoder.read_text_chunks) 4630 { 4631 state->error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); 4632 if(state->error) break; 4633 } 4634 } 4635 /*international text chunk (iTXt)*/ 4636 else if(lodepng_chunk_type_equals(chunk, "iTXt")) 4637 { 4638 if(state->decoder.read_text_chunks) 4639 { 4640 state->error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); 4641 if(state->error) break; 4642 } 4643 } 4644 else if(lodepng_chunk_type_equals(chunk, "tIME")) 4645 { 4646 state->error = readChunk_tIME(&state->info_png, data, chunkLength); 4647 if(state->error) break; 4648 } 4649 else if(lodepng_chunk_type_equals(chunk, "pHYs")) 4650 { 4651 state->error = readChunk_pHYs(&state->info_png, data, chunkLength); 4652 if(state->error) break; 4653 } 4654 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4655 else /*it's not an implemented chunk type, so ignore it: skip over the data*/ 4656 { 4657 /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/ 4658 if(!lodepng_chunk_ancillary(chunk)) CERROR_BREAK(state->error, 69); 4659 4660 unknown = 1; 4661 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4662 if(state->decoder.remember_unknown_chunks) 4663 { 4664 state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1], 4665 &state->info_png.unknown_chunks_size[critical_pos - 1], chunk); 4666 if(state->error) break; 4667 } 4668 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4669 } 4670 4671 if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ 4672 { 4673 if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/ 4674 } 4675 4676 if(!IEND) chunk = lodepng_chunk_next_const(chunk); 4677 } 4678 4679 ucvector_init(&scanlines); 4680 /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation. 4681 If the decompressed size does not match the prediction, the image must be corrupt.*/ 4682 if(state->info_png.interlace_method == 0) 4683 { 4684 /*The extra *h is added because this are the filter bytes every scanline starts with*/ 4685 predict = lodepng_get_raw_size_idat(*w, *h, &state->info_png.color) + *h; 4686 } 4687 else 4688 { 4689 /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/ 4690 const LodePNGColorMode* color = &state->info_png.color; 4691 predict = 0; 4692 predict += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3); 4693 if(*w > 4) predict += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, color) + ((*h + 7) >> 3); 4694 predict += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, color) + ((*h + 3) >> 3); 4695 if(*w > 2) predict += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, color) + ((*h + 3) >> 2); 4696 predict += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, color) + ((*h + 1) >> 2); 4697 if(*w > 1) predict += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, color) + ((*h + 1) >> 1); 4698 predict += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, color) + ((*h + 0) >> 1); 4699 } 4700 if(!state->error && !ucvector_reserve(&scanlines, predict)) state->error = 83; /*alloc fail*/ 4701 if(!state->error) 4702 { 4703 state->error = zlib_decompress(&scanlines.data, &scanlines.size, idat.data, 4704 idat.size, &state->decoder.zlibsettings); 4705 if(!state->error && scanlines.size != predict) state->error = 91; /*decompressed size doesn't match prediction*/ 4706 } 4707 ucvector_cleanup(&idat); 4708 4709 if(!state->error) 4710 { 4711 outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color); 4712 *out = (unsigned char*)lodepng_malloc(outsize); 4713 if(!*out) state->error = 83; /*alloc fail*/ 4714 } 4715 if(!state->error) 4716 { 4717 for(i = 0; i < outsize; i++) (*out)[i] = 0; 4718 state->error = postProcessScanlines(*out, scanlines.data, *w, *h, &state->info_png); 4719 } 4720 ucvector_cleanup(&scanlines); 4721 } 4722 4723 unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, 4724 LodePNGState* state, 4725 const unsigned char* in, size_t insize) 4726 { 4727 *out = 0; 4728 decodeGeneric(out, w, h, state, in, insize); 4729 if(state->error) return state->error; 4730 if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) 4731 { 4732 /*same color type, no copying or converting of data needed*/ 4733 /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype 4734 the raw image has to the end user*/ 4735 if(!state->decoder.color_convert) 4736 { 4737 state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color); 4738 if(state->error) return state->error; 4739 } 4740 } 4741 else 4742 { 4743 /*color conversion needed; sort of copy of the data*/ 4744 unsigned char* data = *out; 4745 size_t outsize; 4746 4747 /*TODO: check if this works according to the statement in the documentation: "The converter can convert 4748 from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/ 4749 if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) 4750 && !(state->info_raw.bitdepth == 8)) 4751 { 4752 return 56; /*unsupported color mode conversion*/ 4753 } 4754 4755 outsize = lodepng_get_raw_size(*w, *h, &state->info_raw); 4756 *out = (unsigned char*)lodepng_malloc(outsize); 4757 if(!(*out)) 4758 { 4759 state->error = 83; /*alloc fail*/ 4760 } 4761 else state->error = lodepng_convert(*out, data, &state->info_raw, 4762 &state->info_png.color, *w, *h); 4763 lodepng_free(data); 4764 } 4765 return state->error; 4766 } 4767 4768 unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, 4769 size_t insize, LodePNGColorType colortype, unsigned bitdepth) 4770 { 4771 unsigned error; 4772 LodePNGState state; 4773 lodepng_state_init(&state); 4774 state.info_raw.colortype = colortype; 4775 state.info_raw.bitdepth = bitdepth; 4776 error = lodepng_decode(out, w, h, &state, in, insize); 4777 lodepng_state_cleanup(&state); 4778 return error; 4779 } 4780 4781 unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) 4782 { 4783 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8); 4784 } 4785 4786 unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) 4787 { 4788 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8); 4789 } 4790 4791 #ifdef LODEPNG_COMPILE_DISK 4792 unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename, 4793 LodePNGColorType colortype, unsigned bitdepth) 4794 { 4795 unsigned char* buffer = 0; 4796 size_t buffersize; 4797 unsigned error; 4798 error = lodepng_load_file(&buffer, &buffersize, filename); 4799 if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth); 4800 lodepng_free(buffer); 4801 return error; 4802 } 4803 4804 unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) 4805 { 4806 return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8); 4807 } 4808 4809 unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) 4810 { 4811 return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8); 4812 } 4813 #endif /*LODEPNG_COMPILE_DISK*/ 4814 4815 void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) 4816 { 4817 settings->color_convert = 1; 4818 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4819 settings->read_text_chunks = 1; 4820 settings->remember_unknown_chunks = 0; 4821 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 4822 settings->ignore_crc = 0; 4823 lodepng_decompress_settings_init(&settings->zlibsettings); 4824 } 4825 4826 #endif /*LODEPNG_COMPILE_DECODER*/ 4827 4828 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) 4829 4830 void lodepng_state_init(LodePNGState* state) 4831 { 4832 #ifdef LODEPNG_COMPILE_DECODER 4833 lodepng_decoder_settings_init(&state->decoder); 4834 #endif /*LODEPNG_COMPILE_DECODER*/ 4835 #ifdef LODEPNG_COMPILE_ENCODER 4836 lodepng_encoder_settings_init(&state->encoder); 4837 #endif /*LODEPNG_COMPILE_ENCODER*/ 4838 lodepng_color_mode_init(&state->info_raw); 4839 lodepng_info_init(&state->info_png); 4840 state->error = 1; 4841 } 4842 4843 void lodepng_state_cleanup(LodePNGState* state) 4844 { 4845 lodepng_color_mode_cleanup(&state->info_raw); 4846 lodepng_info_cleanup(&state->info_png); 4847 } 4848 4849 void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) 4850 { 4851 lodepng_state_cleanup(dest); 4852 *dest = *source; 4853 lodepng_color_mode_init(&dest->info_raw); 4854 lodepng_info_init(&dest->info_png); 4855 dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); if(dest->error) return; 4856 dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); if(dest->error) return; 4857 } 4858 4859 #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */ 4860 4861 #ifdef LODEPNG_COMPILE_ENCODER 4862 4863 /* ////////////////////////////////////////////////////////////////////////// */ 4864 /* / PNG Encoder / */ 4865 /* ////////////////////////////////////////////////////////////////////////// */ 4866 4867 /*chunkName must be string of 4 characters*/ 4868 static unsigned addChunk(ucvector* out, const char* chunkName, const unsigned char* data, size_t length) 4869 { 4870 CERROR_TRY_RETURN(lodepng_chunk_create(&out->data, &out->size, (unsigned)length, chunkName, data)); 4871 out->allocsize = out->size; /*fix the allocsize again*/ 4872 return 0; 4873 } 4874 4875 static void writeSignature(ucvector* out) 4876 { 4877 /*8 bytes PNG signature, aka the magic bytes*/ 4878 ucvector_push_back(out, 137); 4879 ucvector_push_back(out, 80); 4880 ucvector_push_back(out, 78); 4881 ucvector_push_back(out, 71); 4882 ucvector_push_back(out, 13); 4883 ucvector_push_back(out, 10); 4884 ucvector_push_back(out, 26); 4885 ucvector_push_back(out, 10); 4886 } 4887 4888 static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h, 4889 LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method) 4890 { 4891 unsigned error = 0; 4892 ucvector header; 4893 ucvector_init(&header); 4894 4895 lodepng_add32bitInt(&header, w); /*width*/ 4896 lodepng_add32bitInt(&header, h); /*height*/ 4897 ucvector_push_back(&header, (unsigned char)bitdepth); /*bit depth*/ 4898 ucvector_push_back(&header, (unsigned char)colortype); /*color type*/ 4899 ucvector_push_back(&header, 0); /*compression method*/ 4900 ucvector_push_back(&header, 0); /*filter method*/ 4901 ucvector_push_back(&header, interlace_method); /*interlace method*/ 4902 4903 error = addChunk(out, "IHDR", header.data, header.size); 4904 ucvector_cleanup(&header); 4905 4906 return error; 4907 } 4908 4909 static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info) 4910 { 4911 unsigned error = 0; 4912 size_t i; 4913 ucvector PLTE; 4914 ucvector_init(&PLTE); 4915 for(i = 0; i != info->palettesize * 4; ++i) 4916 { 4917 /*add all channels except alpha channel*/ 4918 if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]); 4919 } 4920 error = addChunk(out, "PLTE", PLTE.data, PLTE.size); 4921 ucvector_cleanup(&PLTE); 4922 4923 return error; 4924 } 4925 4926 static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info) 4927 { 4928 unsigned error = 0; 4929 size_t i; 4930 ucvector tRNS; 4931 ucvector_init(&tRNS); 4932 if(info->colortype == LCT_PALETTE) 4933 { 4934 size_t amount = info->palettesize; 4935 /*the tail of palette values that all have 255 as alpha, does not have to be encoded*/ 4936 for(i = info->palettesize; i != 0; --i) 4937 { 4938 if(info->palette[4 * (i - 1) + 3] == 255) --amount; 4939 else break; 4940 } 4941 /*add only alpha channel*/ 4942 for(i = 0; i != amount; ++i) ucvector_push_back(&tRNS, info->palette[4 * i + 3]); 4943 } 4944 else if(info->colortype == LCT_GREY) 4945 { 4946 if(info->key_defined) 4947 { 4948 ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8)); 4949 ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255)); 4950 } 4951 } 4952 else if(info->colortype == LCT_RGB) 4953 { 4954 if(info->key_defined) 4955 { 4956 ucvector_push_back(&tRNS, (unsigned char)(info->key_r >> 8)); 4957 ucvector_push_back(&tRNS, (unsigned char)(info->key_r & 255)); 4958 ucvector_push_back(&tRNS, (unsigned char)(info->key_g >> 8)); 4959 ucvector_push_back(&tRNS, (unsigned char)(info->key_g & 255)); 4960 ucvector_push_back(&tRNS, (unsigned char)(info->key_b >> 8)); 4961 ucvector_push_back(&tRNS, (unsigned char)(info->key_b & 255)); 4962 } 4963 } 4964 4965 error = addChunk(out, "tRNS", tRNS.data, tRNS.size); 4966 ucvector_cleanup(&tRNS); 4967 4968 return error; 4969 } 4970 4971 static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize, 4972 LodePNGCompressSettings* zlibsettings) 4973 { 4974 ucvector zlibdata; 4975 unsigned error = 0; 4976 4977 /*compress with the Zlib compressor*/ 4978 ucvector_init(&zlibdata); 4979 error = zlib_compress(&zlibdata.data, &zlibdata.size, data, datasize, zlibsettings); 4980 if(!error) error = addChunk(out, "IDAT", zlibdata.data, zlibdata.size); 4981 ucvector_cleanup(&zlibdata); 4982 4983 return error; 4984 } 4985 4986 static unsigned addChunk_IEND(ucvector* out) 4987 { 4988 unsigned error = 0; 4989 error = addChunk(out, "IEND", 0, 0); 4990 return error; 4991 } 4992 4993 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 4994 4995 static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring) 4996 { 4997 unsigned error = 0; 4998 size_t i; 4999 ucvector text; 5000 ucvector_init(&text); 5001 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)keyword[i]); 5002 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ 5003 ucvector_push_back(&text, 0); /*0 termination char*/ 5004 for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)textstring[i]); 5005 error = addChunk(out, "tEXt", text.data, text.size); 5006 ucvector_cleanup(&text); 5007 5008 return error; 5009 } 5010 5011 static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* textstring, 5012 LodePNGCompressSettings* zlibsettings) 5013 { 5014 unsigned error = 0; 5015 ucvector data, compressed; 5016 size_t i, textsize = strlen(textstring); 5017 5018 ucvector_init(&data); 5019 ucvector_init(&compressed); 5020 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]); 5021 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ 5022 ucvector_push_back(&data, 0); /*0 termination char*/ 5023 ucvector_push_back(&data, 0); /*compression method: 0*/ 5024 5025 error = zlib_compress(&compressed.data, &compressed.size, 5026 (unsigned char*)textstring, textsize, zlibsettings); 5027 if(!error) 5028 { 5029 for(i = 0; i != compressed.size; ++i) ucvector_push_back(&data, compressed.data[i]); 5030 error = addChunk(out, "zTXt", data.data, data.size); 5031 } 5032 5033 ucvector_cleanup(&compressed); 5034 ucvector_cleanup(&data); 5035 return error; 5036 } 5037 5038 static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* keyword, const char* langtag, 5039 const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) 5040 { 5041 unsigned error = 0; 5042 ucvector data; 5043 size_t i, textsize = strlen(textstring); 5044 5045 ucvector_init(&data); 5046 5047 for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]); 5048 if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/ 5049 ucvector_push_back(&data, 0); /*null termination char*/ 5050 ucvector_push_back(&data, compressed ? 1 : 0); /*compression flag*/ 5051 ucvector_push_back(&data, 0); /*compression method*/ 5052 for(i = 0; langtag[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)langtag[i]); 5053 ucvector_push_back(&data, 0); /*null termination char*/ 5054 for(i = 0; transkey[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)transkey[i]); 5055 ucvector_push_back(&data, 0); /*null termination char*/ 5056 5057 if(compressed) 5058 { 5059 ucvector compressed_data; 5060 ucvector_init(&compressed_data); 5061 error = zlib_compress(&compressed_data.data, &compressed_data.size, 5062 (unsigned char*)textstring, textsize, zlibsettings); 5063 if(!error) 5064 { 5065 for(i = 0; i != compressed_data.size; ++i) ucvector_push_back(&data, compressed_data.data[i]); 5066 } 5067 ucvector_cleanup(&compressed_data); 5068 } 5069 else /*not compressed*/ 5070 { 5071 for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)textstring[i]); 5072 } 5073 5074 if(!error) error = addChunk(out, "iTXt", data.data, data.size); 5075 ucvector_cleanup(&data); 5076 return error; 5077 } 5078 5079 static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info) 5080 { 5081 unsigned error = 0; 5082 ucvector bKGD; 5083 ucvector_init(&bKGD); 5084 if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) 5085 { 5086 ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8)); 5087 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); 5088 } 5089 else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) 5090 { 5091 ucvector_push_back(&bKGD, (unsigned char)(info->background_r >> 8)); 5092 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); 5093 ucvector_push_back(&bKGD, (unsigned char)(info->background_g >> 8)); 5094 ucvector_push_back(&bKGD, (unsigned char)(info->background_g & 255)); 5095 ucvector_push_back(&bKGD, (unsigned char)(info->background_b >> 8)); 5096 ucvector_push_back(&bKGD, (unsigned char)(info->background_b & 255)); 5097 } 5098 else if(info->color.colortype == LCT_PALETTE) 5099 { 5100 ucvector_push_back(&bKGD, (unsigned char)(info->background_r & 255)); /*palette index*/ 5101 } 5102 5103 error = addChunk(out, "bKGD", bKGD.data, bKGD.size); 5104 ucvector_cleanup(&bKGD); 5105 5106 return error; 5107 } 5108 5109 static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time) 5110 { 5111 unsigned error = 0; 5112 unsigned char* data = (unsigned char*)lodepng_malloc(7); 5113 if(!data) return 83; /*alloc fail*/ 5114 data[0] = (unsigned char)(time->year >> 8); 5115 data[1] = (unsigned char)(time->year & 255); 5116 data[2] = (unsigned char)time->month; 5117 data[3] = (unsigned char)time->day; 5118 data[4] = (unsigned char)time->hour; 5119 data[5] = (unsigned char)time->minute; 5120 data[6] = (unsigned char)time->second; 5121 error = addChunk(out, "tIME", data, 7); 5122 lodepng_free(data); 5123 return error; 5124 } 5125 5126 static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info) 5127 { 5128 unsigned error = 0; 5129 ucvector data; 5130 ucvector_init(&data); 5131 5132 lodepng_add32bitInt(&data, info->phys_x); 5133 lodepng_add32bitInt(&data, info->phys_y); 5134 ucvector_push_back(&data, info->phys_unit); 5135 5136 error = addChunk(out, "pHYs", data.data, data.size); 5137 ucvector_cleanup(&data); 5138 5139 return error; 5140 } 5141 5142 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5143 5144 static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline, 5145 size_t length, size_t bytewidth, unsigned char filterType) 5146 { 5147 size_t i; 5148 switch(filterType) 5149 { 5150 case 0: /*None*/ 5151 for(i = 0; i != length; ++i) out[i] = scanline[i]; 5152 break; 5153 case 1: /*Sub*/ 5154 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i]; 5155 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth]; 5156 break; 5157 case 2: /*Up*/ 5158 if(prevline) 5159 { 5160 for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i]; 5161 } 5162 else 5163 { 5164 for(i = 0; i != length; ++i) out[i] = scanline[i]; 5165 } 5166 break; 5167 case 3: /*Average*/ 5168 if(prevline) 5169 { 5170 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1); 5171 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) >> 1); 5172 } 5173 else 5174 { 5175 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i]; 5176 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1); 5177 } 5178 break; 5179 case 4: /*Paeth*/ 5180 if(prevline) 5181 { 5182 /*paethPredictor(0, prevline[i], 0) is always prevline[i]*/ 5183 for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]); 5184 for(i = bytewidth; i < length; ++i) 5185 { 5186 out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth])); 5187 } 5188 } 5189 else 5190 { 5191 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i]; 5192 /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/ 5193 for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]); 5194 } 5195 break; 5196 default: return; /*unexisting filter type given*/ 5197 } 5198 } 5199 5200 /* log2 approximation. A slight bit faster than std::log. */ 5201 static float flog2(float f) 5202 { 5203 float result = 0; 5204 while(f > 32) { result += 4; f /= 16; } 5205 while(f > 2) { ++result; f /= 2; } 5206 return result + 1.442695f * (f * f * f / 3 - 3 * f * f / 2 + 3 * f - 1.83333f); 5207 } 5208 5209 static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, 5210 const LodePNGColorMode* info, const LodePNGEncoderSettings* settings) 5211 { 5212 /* 5213 For PNG filter method 0 5214 out must be a buffer with as size: h + (w * h * bpp + 7) / 8, because there are 5215 the scanlines with 1 extra byte per scanline 5216 */ 5217 5218 unsigned bpp = lodepng_get_bpp(info); 5219 /*the width of a scanline in bytes, not including the filter type*/ 5220 size_t linebytes = (w * bpp + 7) / 8; 5221 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ 5222 size_t bytewidth = (bpp + 7) / 8; 5223 const unsigned char* prevline = 0; 5224 unsigned x, y; 5225 unsigned error = 0; 5226 LodePNGFilterStrategy strategy = settings->filter_strategy; 5227 5228 /* 5229 There is a heuristic called the minimum sum of absolute differences heuristic, suggested by the PNG standard: 5230 * If the image type is Palette, or the bit depth is smaller than 8, then do not filter the image (i.e. 5231 use fixed filtering, with the filter None). 5232 * (The other case) If the image type is Grayscale or RGB (with or without Alpha), and the bit depth is 5233 not smaller than 8, then use adaptive filtering heuristic as follows: independently for each row, apply 5234 all five filters and select the filter that produces the smallest sum of absolute values per row. 5235 This heuristic is used if filter strategy is LFS_MINSUM and filter_palette_zero is true. 5236 5237 If filter_palette_zero is true and filter_strategy is not LFS_MINSUM, the above heuristic is followed, 5238 but for "the other case", whatever strategy filter_strategy is set to instead of the minimum sum 5239 heuristic is used. 5240 */ 5241 if(settings->filter_palette_zero && 5242 (info->colortype == LCT_PALETTE || info->bitdepth < 8)) strategy = LFS_ZERO; 5243 5244 if(bpp == 0) return 31; /*error: invalid color type*/ 5245 5246 if(strategy == LFS_ZERO) 5247 { 5248 for(y = 0; y != h; ++y) 5249 { 5250 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ 5251 size_t inindex = linebytes * y; 5252 out[outindex] = 0; /*filter type byte*/ 5253 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, 0); 5254 prevline = &in[inindex]; 5255 } 5256 } 5257 else if(strategy == LFS_MINSUM) 5258 { 5259 /*adaptive filtering*/ 5260 size_t sum[5]; 5261 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/ 5262 size_t smallest = 0; 5263 unsigned char type, bestType = 0; 5264 5265 for(type = 0; type != 5; ++type) 5266 { 5267 attempt[type] = (unsigned char*)lodepng_malloc(linebytes); 5268 if(!attempt[type]) return 83; /*alloc fail*/ 5269 } 5270 5271 if(!error) 5272 { 5273 for(y = 0; y != h; ++y) 5274 { 5275 /*try the 5 filter types*/ 5276 for(type = 0; type != 5; ++type) 5277 { 5278 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); 5279 5280 /*calculate the sum of the result*/ 5281 sum[type] = 0; 5282 if(type == 0) 5283 { 5284 for(x = 0; x != linebytes; ++x) sum[type] += (unsigned char)(attempt[type][x]); 5285 } 5286 else 5287 { 5288 for(x = 0; x != linebytes; ++x) 5289 { 5290 /*For differences, each byte should be treated as signed, values above 127 are negative 5291 (converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there. 5292 This means filtertype 0 is almost never chosen, but that is justified.*/ 5293 unsigned char s = attempt[type][x]; 5294 sum[type] += s < 128 ? s : (255U - s); 5295 } 5296 } 5297 5298 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ 5299 if(type == 0 || sum[type] < smallest) 5300 { 5301 bestType = type; 5302 smallest = sum[type]; 5303 } 5304 } 5305 5306 prevline = &in[y * linebytes]; 5307 5308 /*now fill the out values*/ 5309 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ 5310 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; 5311 } 5312 } 5313 5314 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); 5315 } 5316 else if(strategy == LFS_ENTROPY) 5317 { 5318 float sum[5]; 5319 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/ 5320 float smallest = 0; 5321 unsigned type, bestType = 0; 5322 unsigned count[256]; 5323 5324 for(type = 0; type != 5; ++type) 5325 { 5326 attempt[type] = (unsigned char*)lodepng_malloc(linebytes); 5327 if(!attempt[type]) return 83; /*alloc fail*/ 5328 } 5329 5330 for(y = 0; y != h; ++y) 5331 { 5332 /*try the 5 filter types*/ 5333 for(type = 0; type != 5; ++type) 5334 { 5335 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); 5336 for(x = 0; x != 256; ++x) count[x] = 0; 5337 for(x = 0; x != linebytes; ++x) ++count[attempt[type][x]]; 5338 ++count[type]; /*the filter type itself is part of the scanline*/ 5339 sum[type] = 0; 5340 for(x = 0; x != 256; ++x) 5341 { 5342 float p = count[x] / (float)(linebytes + 1); 5343 sum[type] += count[x] == 0 ? 0 : flog2(1 / p) * p; 5344 } 5345 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ 5346 if(type == 0 || sum[type] < smallest) 5347 { 5348 bestType = type; 5349 smallest = sum[type]; 5350 } 5351 } 5352 5353 prevline = &in[y * linebytes]; 5354 5355 /*now fill the out values*/ 5356 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ 5357 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; 5358 } 5359 5360 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); 5361 } 5362 else if(strategy == LFS_PREDEFINED) 5363 { 5364 for(y = 0; y != h; ++y) 5365 { 5366 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ 5367 size_t inindex = linebytes * y; 5368 unsigned char type = settings->predefined_filters[y]; 5369 out[outindex] = type; /*filter type byte*/ 5370 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type); 5371 prevline = &in[inindex]; 5372 } 5373 } 5374 else if(strategy == LFS_BRUTE_FORCE) 5375 { 5376 /*brute force filter chooser. 5377 deflate the scanline after every filter attempt to see which one deflates best. 5378 This is very slow and gives only slightly smaller, sometimes even larger, result*/ 5379 size_t size[5]; 5380 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/ 5381 size_t smallest = 0; 5382 unsigned type = 0, bestType = 0; 5383 unsigned char* dummy; 5384 LodePNGCompressSettings zlibsettings = settings->zlibsettings; 5385 /*use fixed tree on the attempts so that the tree is not adapted to the filtertype on purpose, 5386 to simulate the true case where the tree is the same for the whole image. Sometimes it gives 5387 better result with dynamic tree anyway. Using the fixed tree sometimes gives worse, but in rare 5388 cases better compression. It does make this a bit less slow, so it's worth doing this.*/ 5389 zlibsettings.btype = 1; 5390 /*a custom encoder likely doesn't read the btype setting and is optimized for complete PNG 5391 images only, so disable it*/ 5392 zlibsettings.custom_zlib = 0; 5393 zlibsettings.custom_deflate = 0; 5394 for(type = 0; type != 5; ++type) 5395 { 5396 attempt[type] = (unsigned char*)lodepng_malloc(linebytes); 5397 if(!attempt[type]) return 83; /*alloc fail*/ 5398 } 5399 for(y = 0; y != h; ++y) /*try the 5 filter types*/ 5400 { 5401 for(type = 0; type != 5; ++type) 5402 { 5403 unsigned testsize = linebytes; 5404 /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/ 5405 5406 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type); 5407 size[type] = 0; 5408 dummy = 0; 5409 zlib_compress(&dummy, &size[type], attempt[type], testsize, &zlibsettings); 5410 lodepng_free(dummy); 5411 /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/ 5412 if(type == 0 || size[type] < smallest) 5413 { 5414 bestType = type; 5415 smallest = size[type]; 5416 } 5417 } 5418 prevline = &in[y * linebytes]; 5419 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ 5420 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x]; 5421 } 5422 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]); 5423 } 5424 else return 88; /* unknown filter strategy */ 5425 5426 return error; 5427 } 5428 5429 static void addPaddingBits(unsigned char* out, const unsigned char* in, 5430 size_t olinebits, size_t ilinebits, unsigned h) 5431 { 5432 /*The opposite of the removePaddingBits function 5433 olinebits must be >= ilinebits*/ 5434 unsigned y; 5435 size_t diff = olinebits - ilinebits; 5436 size_t obp = 0, ibp = 0; /*bit pointers*/ 5437 for(y = 0; y != h; ++y) 5438 { 5439 size_t x; 5440 for(x = 0; x < ilinebits; ++x) 5441 { 5442 unsigned char bit = readBitFromReversedStream(&ibp, in); 5443 setBitOfReversedStream(&obp, out, bit); 5444 } 5445 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid 5446 "Use of uninitialised value of size ###" warning from valgrind*/ 5447 for(x = 0; x != diff; ++x) setBitOfReversedStream(&obp, out, 0); 5448 } 5449 } 5450 5451 /* 5452 in: non-interlaced image with size w*h 5453 out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with 5454 no padding bits between scanlines, but between reduced images so that each 5455 reduced image starts at a byte. 5456 bpp: bits per pixel 5457 there are no padding bits, not between scanlines, not between reduced images 5458 in has the following size in bits: w * h * bpp. 5459 out is possibly bigger due to padding bits between reduced images 5460 NOTE: comments about padding bits are only relevant if bpp < 8 5461 */ 5462 static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) 5463 { 5464 unsigned passw[7], passh[7]; 5465 size_t filter_passstart[8], padded_passstart[8], passstart[8]; 5466 unsigned i; 5467 5468 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); 5469 5470 if(bpp >= 8) 5471 { 5472 for(i = 0; i != 7; ++i) 5473 { 5474 unsigned x, y, b; 5475 size_t bytewidth = bpp / 8; 5476 for(y = 0; y < passh[i]; ++y) 5477 for(x = 0; x < passw[i]; ++x) 5478 { 5479 size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; 5480 size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth; 5481 for(b = 0; b < bytewidth; ++b) 5482 { 5483 out[pixeloutstart + b] = in[pixelinstart + b]; 5484 } 5485 } 5486 } 5487 } 5488 else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ 5489 { 5490 for(i = 0; i != 7; ++i) 5491 { 5492 unsigned x, y, b; 5493 unsigned ilinebits = bpp * passw[i]; 5494 unsigned olinebits = bpp * w; 5495 size_t obp, ibp; /*bit pointers (for out and in buffer)*/ 5496 for(y = 0; y < passh[i]; ++y) 5497 for(x = 0; x < passw[i]; ++x) 5498 { 5499 ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; 5500 obp = (8 * passstart[i]) + (y * ilinebits + x * bpp); 5501 for(b = 0; b < bpp; ++b) 5502 { 5503 unsigned char bit = readBitFromReversedStream(&ibp, in); 5504 setBitOfReversedStream(&obp, out, bit); 5505 } 5506 } 5507 } 5508 } 5509 } 5510 5511 /*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image. 5512 return value is error**/ 5513 static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, 5514 unsigned w, unsigned h, 5515 const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) 5516 { 5517 /* 5518 This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps: 5519 *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter 5520 *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter 5521 */ 5522 unsigned bpp = lodepng_get_bpp(&info_png->color); 5523 unsigned error = 0; 5524 5525 if(info_png->interlace_method == 0) 5526 { 5527 *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/ 5528 *out = (unsigned char*)lodepng_malloc(*outsize); 5529 if(!(*out) && (*outsize)) error = 83; /*alloc fail*/ 5530 5531 if(!error) 5532 { 5533 /*non multiple of 8 bits per scanline, padding bits needed per scanline*/ 5534 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) 5535 { 5536 unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7) / 8)); 5537 if(!padded) error = 83; /*alloc fail*/ 5538 if(!error) 5539 { 5540 addPaddingBits(padded, in, ((w * bpp + 7) / 8) * 8, w * bpp, h); 5541 error = filter(*out, padded, w, h, &info_png->color, settings); 5542 } 5543 lodepng_free(padded); 5544 } 5545 else 5546 { 5547 /*we can immediately filter into the out buffer, no other steps needed*/ 5548 error = filter(*out, in, w, h, &info_png->color, settings); 5549 } 5550 } 5551 } 5552 else /*interlace_method is 1 (Adam7)*/ 5553 { 5554 unsigned passw[7], passh[7]; 5555 size_t filter_passstart[8], padded_passstart[8], passstart[8]; 5556 unsigned char* adam7; 5557 5558 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); 5559 5560 *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/ 5561 *out = (unsigned char*)lodepng_malloc(*outsize); 5562 if(!(*out)) error = 83; /*alloc fail*/ 5563 5564 adam7 = (unsigned char*)lodepng_malloc(passstart[7]); 5565 if(!adam7 && passstart[7]) error = 83; /*alloc fail*/ 5566 5567 if(!error) 5568 { 5569 unsigned i; 5570 5571 Adam7_interlace(adam7, in, w, h, bpp); 5572 for(i = 0; i != 7; ++i) 5573 { 5574 if(bpp < 8) 5575 { 5576 unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]); 5577 if(!padded) ERROR_BREAK(83); /*alloc fail*/ 5578 addPaddingBits(padded, &adam7[passstart[i]], 5579 ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]); 5580 error = filter(&(*out)[filter_passstart[i]], padded, 5581 passw[i], passh[i], &info_png->color, settings); 5582 lodepng_free(padded); 5583 } 5584 else 5585 { 5586 error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], 5587 passw[i], passh[i], &info_png->color, settings); 5588 } 5589 5590 if(error) break; 5591 } 5592 } 5593 5594 lodepng_free(adam7); 5595 } 5596 5597 return error; 5598 } 5599 5600 /* 5601 palette must have 4 * palettesize bytes allocated, and given in format RGBARGBARGBARGBA... 5602 returns 0 if the palette is opaque, 5603 returns 1 if the palette has a single color with alpha 0 ==> color key 5604 returns 2 if the palette is semi-translucent. 5605 */ 5606 static unsigned getPaletteTranslucency(const unsigned char* palette, size_t palettesize) 5607 { 5608 size_t i; 5609 unsigned key = 0; 5610 unsigned r = 0, g = 0, b = 0; /*the value of the color with alpha 0, so long as color keying is possible*/ 5611 for(i = 0; i != palettesize; ++i) 5612 { 5613 if(!key && palette[4 * i + 3] == 0) 5614 { 5615 r = palette[4 * i + 0]; g = palette[4 * i + 1]; b = palette[4 * i + 2]; 5616 key = 1; 5617 i = (size_t)(-1); /*restart from beginning, to detect earlier opaque colors with key's value*/ 5618 } 5619 else if(palette[4 * i + 3] != 255) return 2; 5620 /*when key, no opaque RGB may have key's RGB*/ 5621 else if(key && r == palette[i * 4 + 0] && g == palette[i * 4 + 1] && b == palette[i * 4 + 2]) return 2; 5622 } 5623 return key; 5624 } 5625 5626 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5627 static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) 5628 { 5629 unsigned char* inchunk = data; 5630 while((size_t)(inchunk - data) < datasize) 5631 { 5632 CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk)); 5633 out->allocsize = out->size; /*fix the allocsize again*/ 5634 inchunk = lodepng_chunk_next(inchunk); 5635 } 5636 return 0; 5637 } 5638 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5639 5640 unsigned lodepng_encode(unsigned char** out, size_t* outsize, 5641 const unsigned char* image, unsigned w, unsigned h, 5642 LodePNGState* state) 5643 { 5644 LodePNGInfo info; 5645 ucvector outv; 5646 unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ 5647 size_t datasize = 0; 5648 5649 /*provide some proper output values if error will happen*/ 5650 *out = 0; 5651 *outsize = 0; 5652 state->error = 0; 5653 5654 lodepng_info_init(&info); 5655 lodepng_info_copy(&info, &state->info_png); 5656 5657 if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette) 5658 && (info.color.palettesize == 0 || info.color.palettesize > 256)) 5659 { 5660 state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ 5661 return state->error; 5662 } 5663 5664 if(state->encoder.auto_convert) 5665 { 5666 state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw); 5667 } 5668 if(state->error) return state->error; 5669 5670 if(state->encoder.zlibsettings.btype > 2) 5671 { 5672 CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ 5673 } 5674 if(state->info_png.interlace_method > 1) 5675 { 5676 CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ 5677 } 5678 5679 state->error = checkColorValidity(info.color.colortype, info.color.bitdepth); 5680 if(state->error) return state->error; /*error: unexisting color type given*/ 5681 state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); 5682 if(state->error) return state->error; /*error: unexisting color type given*/ 5683 5684 if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) 5685 { 5686 unsigned char* converted; 5687 size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8; 5688 5689 converted = (unsigned char*)lodepng_malloc(size); 5690 if(!converted && size) state->error = 83; /*alloc fail*/ 5691 if(!state->error) 5692 { 5693 state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); 5694 } 5695 if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); 5696 lodepng_free(converted); 5697 } 5698 else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); 5699 5700 ucvector_init(&outv); 5701 while(!state->error) /*while only executed once, to break on error*/ 5702 { 5703 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5704 size_t i; 5705 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5706 /*write signature and chunks*/ 5707 writeSignature(&outv); 5708 /*IHDR*/ 5709 addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method); 5710 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5711 /*unknown chunks between IHDR and PLTE*/ 5712 if(info.unknown_chunks_data[0]) 5713 { 5714 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]); 5715 if(state->error) break; 5716 } 5717 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5718 /*PLTE*/ 5719 if(info.color.colortype == LCT_PALETTE) 5720 { 5721 addChunk_PLTE(&outv, &info.color); 5722 } 5723 if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA)) 5724 { 5725 addChunk_PLTE(&outv, &info.color); 5726 } 5727 /*tRNS*/ 5728 if(info.color.colortype == LCT_PALETTE && getPaletteTranslucency(info.color.palette, info.color.palettesize) != 0) 5729 { 5730 addChunk_tRNS(&outv, &info.color); 5731 } 5732 if((info.color.colortype == LCT_GREY || info.color.colortype == LCT_RGB) && info.color.key_defined) 5733 { 5734 addChunk_tRNS(&outv, &info.color); 5735 } 5736 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5737 /*bKGD (must come between PLTE and the IDAt chunks*/ 5738 if(info.background_defined) addChunk_bKGD(&outv, &info); 5739 /*pHYs (must come before the IDAT chunks)*/ 5740 if(info.phys_defined) addChunk_pHYs(&outv, &info); 5741 5742 /*unknown chunks between PLTE and IDAT*/ 5743 if(info.unknown_chunks_data[1]) 5744 { 5745 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]); 5746 if(state->error) break; 5747 } 5748 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5749 /*IDAT (multiple IDAT chunks must be consecutive)*/ 5750 state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings); 5751 if(state->error) break; 5752 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5753 /*tIME*/ 5754 if(info.time_defined) addChunk_tIME(&outv, &info.time); 5755 /*tEXt and/or zTXt*/ 5756 for(i = 0; i != info.text_num; ++i) 5757 { 5758 if(strlen(info.text_keys[i]) > 79) 5759 { 5760 state->error = 66; /*text chunk too large*/ 5761 break; 5762 } 5763 if(strlen(info.text_keys[i]) < 1) 5764 { 5765 state->error = 67; /*text chunk too small*/ 5766 break; 5767 } 5768 if(state->encoder.text_compression) 5769 { 5770 addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings); 5771 } 5772 else 5773 { 5774 addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); 5775 } 5776 } 5777 /*LodePNG version id in text chunk*/ 5778 if(state->encoder.add_id) 5779 { 5780 unsigned alread_added_id_text = 0; 5781 for(i = 0; i != info.text_num; ++i) 5782 { 5783 if(!strcmp(info.text_keys[i], "LodePNG")) 5784 { 5785 alread_added_id_text = 1; 5786 break; 5787 } 5788 } 5789 if(alread_added_id_text == 0) 5790 { 5791 addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/ 5792 } 5793 } 5794 /*iTXt*/ 5795 for(i = 0; i != info.itext_num; ++i) 5796 { 5797 if(strlen(info.itext_keys[i]) > 79) 5798 { 5799 state->error = 66; /*text chunk too large*/ 5800 break; 5801 } 5802 if(strlen(info.itext_keys[i]) < 1) 5803 { 5804 state->error = 67; /*text chunk too small*/ 5805 break; 5806 } 5807 addChunk_iTXt(&outv, state->encoder.text_compression, 5808 info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i], 5809 &state->encoder.zlibsettings); 5810 } 5811 5812 /*unknown chunks between IDAT and IEND*/ 5813 if(info.unknown_chunks_data[2]) 5814 { 5815 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]); 5816 if(state->error) break; 5817 } 5818 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5819 addChunk_IEND(&outv); 5820 5821 break; /*this isn't really a while loop; no error happened so break out now!*/ 5822 } 5823 5824 lodepng_info_cleanup(&info); 5825 lodepng_free(data); 5826 /*instead of cleaning the vector up, give it to the output*/ 5827 *out = outv.data; 5828 *outsize = outv.size; 5829 5830 return state->error; 5831 } 5832 5833 unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsigned char* image, 5834 unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) 5835 { 5836 unsigned error; 5837 LodePNGState state; 5838 lodepng_state_init(&state); 5839 state.info_raw.colortype = colortype; 5840 state.info_raw.bitdepth = bitdepth; 5841 state.info_png.color.colortype = colortype; 5842 state.info_png.color.bitdepth = bitdepth; 5843 lodepng_encode(out, outsize, image, w, h, &state); 5844 error = state.error; 5845 lodepng_state_cleanup(&state); 5846 return error; 5847 } 5848 5849 unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) 5850 { 5851 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGBA, 8); 5852 } 5853 5854 unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) 5855 { 5856 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGB, 8); 5857 } 5858 5859 #ifdef LODEPNG_COMPILE_DISK 5860 unsigned lodepng_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h, 5861 LodePNGColorType colortype, unsigned bitdepth) 5862 { 5863 unsigned char* buffer; 5864 size_t buffersize; 5865 unsigned error = lodepng_encode_memory(&buffer, &buffersize, image, w, h, colortype, bitdepth); 5866 if(!error) error = lodepng_save_file(buffer, buffersize, filename); 5867 lodepng_free(buffer); 5868 return error; 5869 } 5870 5871 unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) 5872 { 5873 return lodepng_encode_file(filename, image, w, h, LCT_RGBA, 8); 5874 } 5875 5876 unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) 5877 { 5878 return lodepng_encode_file(filename, image, w, h, LCT_RGB, 8); 5879 } 5880 #endif /*LODEPNG_COMPILE_DISK*/ 5881 5882 void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) 5883 { 5884 lodepng_compress_settings_init(&settings->zlibsettings); 5885 settings->filter_palette_zero = 1; 5886 settings->filter_strategy = LFS_MINSUM; 5887 settings->auto_convert = 1; 5888 settings->force_palette = 0; 5889 settings->predefined_filters = 0; 5890 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS 5891 settings->add_id = 0; 5892 settings->text_compression = 1; 5893 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ 5894 } 5895 5896 #endif /*LODEPNG_COMPILE_ENCODER*/ 5897 #endif /*LODEPNG_COMPILE_PNG*/ 5898 5899 #ifdef LODEPNG_COMPILE_ERROR_TEXT 5900 /* 5901 This returns the description of a numerical error code in English. This is also 5902 the documentation of all the error codes. 5903 */ 5904 const char* lodepng_error_text(unsigned code) 5905 { 5906 switch(code) 5907 { 5908 case 0: return "no error, everything went ok"; 5909 case 1: return "nothing done yet"; /*the Encoder/Decoder has done nothing yet, error checking makes no sense yet*/ 5910 case 10: return "end of input memory reached without huffman end code"; /*while huffman decoding*/ 5911 case 11: return "error in code tree made it jump outside of huffman tree"; /*while huffman decoding*/ 5912 case 13: return "problem while processing dynamic deflate block"; 5913 case 14: return "problem while processing dynamic deflate block"; 5914 case 15: return "problem while processing dynamic deflate block"; 5915 case 16: return "unexisting code while processing dynamic deflate block"; 5916 case 17: return "end of out buffer memory reached while inflating"; 5917 case 18: return "invalid distance code while inflating"; 5918 case 19: return "end of out buffer memory reached while inflating"; 5919 case 20: return "invalid deflate block BTYPE encountered while decoding"; 5920 case 21: return "NLEN is not ones complement of LEN in a deflate block"; 5921 /*end of out buffer memory reached while inflating: 5922 This can happen if the inflated deflate data is longer than the amount of bytes required to fill up 5923 all the pixels of the image, given the color depth and image dimensions. Something that doesn't 5924 happen in a normal, well encoded, PNG image.*/ 5925 case 22: return "end of out buffer memory reached while inflating"; 5926 case 23: return "end of in buffer memory reached while inflating"; 5927 case 24: return "invalid FCHECK in zlib header"; 5928 case 25: return "invalid compression method in zlib header"; 5929 case 26: return "FDICT encountered in zlib header while it's not used for PNG"; 5930 case 27: return "PNG file is smaller than a PNG header"; 5931 /*Checks the magic file header, the first 8 bytes of the PNG file*/ 5932 case 28: return "incorrect PNG signature, it's no PNG or corrupted"; 5933 case 29: return "first chunk is not the header chunk"; 5934 case 30: return "chunk length too large, chunk broken off at end of file"; 5935 case 31: return "illegal PNG color type or bpp"; 5936 case 32: return "illegal PNG compression method"; 5937 case 33: return "illegal PNG filter method"; 5938 case 34: return "illegal PNG interlace method"; 5939 case 35: return "chunk length of a chunk is too large or the chunk too small"; 5940 case 36: return "illegal PNG filter type encountered"; 5941 case 37: return "illegal bit depth for this color type given"; 5942 case 38: return "the palette is too big"; /*more than 256 colors*/ 5943 case 39: return "more palette alpha values given in tRNS chunk than there are colors in the palette"; 5944 case 40: return "tRNS chunk has wrong size for greyscale image"; 5945 case 41: return "tRNS chunk has wrong size for RGB image"; 5946 case 42: return "tRNS chunk appeared while it was not allowed for this color type"; 5947 case 43: return "bKGD chunk has wrong size for palette image"; 5948 case 44: return "bKGD chunk has wrong size for greyscale image"; 5949 case 45: return "bKGD chunk has wrong size for RGB image"; 5950 case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?"; 5951 case 49: return "jumped past memory while generating dynamic huffman tree"; 5952 case 50: return "jumped past memory while generating dynamic huffman tree"; 5953 case 51: return "jumped past memory while inflating huffman block"; 5954 case 52: return "jumped past memory while inflating"; 5955 case 53: return "size of zlib data too small"; 5956 case 54: return "repeat symbol in tree while there was no value symbol yet"; 5957 /*jumped past tree while generating huffman tree, this could be when the 5958 tree will have more leaves than symbols after generating it out of the 5959 given lenghts. They call this an oversubscribed dynamic bit lengths tree in zlib.*/ 5960 case 55: return "jumped past tree while generating huffman tree"; 5961 case 56: return "given output image colortype or bitdepth not supported for color conversion"; 5962 case 57: return "invalid CRC encountered (checking CRC can be disabled)"; 5963 case 58: return "invalid ADLER32 encountered (checking ADLER32 can be disabled)"; 5964 case 59: return "requested color conversion not supported"; 5965 case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)"; 5966 case 61: return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)"; 5967 /*LodePNG leaves the choice of RGB to greyscale conversion formula to the user.*/ 5968 case 62: return "conversion from color to greyscale not supported"; 5969 case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk"; /*(2^31-1)*/ 5970 /*this would result in the inability of a deflated block to ever contain an end code. It must be at least 1.*/ 5971 case 64: return "the length of the END symbol 256 in the Huffman tree is 0"; 5972 case 66: return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes"; 5973 case 67: return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte"; 5974 case 68: return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors"; 5975 case 69: return "unknown chunk type with 'critical' flag encountered by the decoder"; 5976 case 71: return "unexisting interlace mode given to encoder (must be 0 or 1)"; 5977 case 72: return "while decoding, unexisting compression method encountering in zTXt or iTXt chunk (it must be 0)"; 5978 case 73: return "invalid tIME chunk size"; 5979 case 74: return "invalid pHYs chunk size"; 5980 /*length could be wrong, or data chopped off*/ 5981 case 75: return "no null termination char found while decoding text chunk"; 5982 case 76: return "iTXt chunk too short to contain required bytes"; 5983 case 77: return "integer overflow in buffer size"; 5984 case 78: return "failed to open file for reading"; /*file doesn't exist or couldn't be opened for reading*/ 5985 case 79: return "failed to open file for writing"; 5986 case 80: return "tried creating a tree of 0 symbols"; 5987 case 81: return "lazy matching at pos 0 is impossible"; 5988 case 82: return "color conversion to palette requested while a color isn't in palette"; 5989 case 83: return "memory allocation failed"; 5990 case 84: return "given image too small to contain all pixels to be encoded"; 5991 case 86: return "impossible offset in lz77 encoding (internal bug)"; 5992 case 87: return "must provide custom zlib function pointer if LODEPNG_COMPILE_ZLIB is not defined"; 5993 case 88: return "invalid filter strategy given for LodePNGEncoderSettings.filter_strategy"; 5994 case 89: return "text chunk keyword too short or long: must have size 1-79"; 5995 /*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/ 5996 case 90: return "windowsize must be a power of two"; 5997 case 91: return "invalid decompressed idat size"; 5998 case 92: return "too many pixels, not supported"; 5999 case 93: return "zero width or height is invalid"; 6000 case 94: return "header chunk must have a size of 13 bytes"; 6001 } 6002 return "unknown error code"; 6003 } 6004 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/ 6005 6006 /* ////////////////////////////////////////////////////////////////////////// */ 6007 /* ////////////////////////////////////////////////////////////////////////// */ 6008 /* // C++ Wrapper // */ 6009 /* ////////////////////////////////////////////////////////////////////////// */ 6010 /* ////////////////////////////////////////////////////////////////////////// */ 6011 6012 #ifdef LODEPNG_COMPILE_CPP 6013 namespace lodepng 6014 { 6015 6016 #ifdef LODEPNG_COMPILE_DISK 6017 unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename) 6018 { 6019 long size = lodepng_filesize(filename.c_str()); 6020 if(size < 0) return 78; 6021 buffer.resize((size_t)size); 6022 return size == 0 ? 0 : lodepng_buffer_file(&buffer[0], (size_t)size, filename.c_str()); 6023 } 6024 6025 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ 6026 unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename) 6027 { 6028 return lodepng_save_file(buffer.empty() ? 0 : &buffer[0], buffer.size(), filename.c_str()); 6029 } 6030 #endif /* LODEPNG_COMPILE_DISK */ 6031 6032 #ifdef LODEPNG_COMPILE_ZLIB 6033 #ifdef LODEPNG_COMPILE_DECODER 6034 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, 6035 const LodePNGDecompressSettings& settings) 6036 { 6037 unsigned char* buffer = 0; 6038 size_t buffersize = 0; 6039 unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings); 6040 if(buffer) 6041 { 6042 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6043 lodepng_free(buffer); 6044 } 6045 return error; 6046 } 6047 6048 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, 6049 const LodePNGDecompressSettings& settings) 6050 { 6051 return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings); 6052 } 6053 #endif /* LODEPNG_COMPILE_DECODER */ 6054 6055 #ifdef LODEPNG_COMPILE_ENCODER 6056 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize, 6057 const LodePNGCompressSettings& settings) 6058 { 6059 unsigned char* buffer = 0; 6060 size_t buffersize = 0; 6061 unsigned error = zlib_compress(&buffer, &buffersize, in, insize, &settings); 6062 if(buffer) 6063 { 6064 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6065 lodepng_free(buffer); 6066 } 6067 return error; 6068 } 6069 6070 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in, 6071 const LodePNGCompressSettings& settings) 6072 { 6073 return compress(out, in.empty() ? 0 : &in[0], in.size(), settings); 6074 } 6075 #endif /* LODEPNG_COMPILE_ENCODER */ 6076 #endif /* LODEPNG_COMPILE_ZLIB */ 6077 6078 6079 #ifdef LODEPNG_COMPILE_PNG 6080 6081 State::State() 6082 { 6083 lodepng_state_init(this); 6084 } 6085 6086 State::State(const State& other) 6087 { 6088 lodepng_state_init(this); 6089 lodepng_state_copy(this, &other); 6090 } 6091 6092 State::~State() 6093 { 6094 lodepng_state_cleanup(this); 6095 } 6096 6097 State& State::operator=(const State& other) 6098 { 6099 lodepng_state_copy(this, &other); 6100 return *this; 6101 } 6102 6103 #ifdef LODEPNG_COMPILE_DECODER 6104 6105 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const unsigned char* in, 6106 size_t insize, LodePNGColorType colortype, unsigned bitdepth) 6107 { 6108 unsigned char* buffer; 6109 unsigned error = lodepng_decode_memory(&buffer, &w, &h, in, insize, colortype, bitdepth); 6110 if(buffer && !error) 6111 { 6112 State state; 6113 state.info_raw.colortype = colortype; 6114 state.info_raw.bitdepth = bitdepth; 6115 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); 6116 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6117 lodepng_free(buffer); 6118 } 6119 return error; 6120 } 6121 6122 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 6123 const std::vector<unsigned char>& in, LodePNGColorType colortype, unsigned bitdepth) 6124 { 6125 return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colortype, bitdepth); 6126 } 6127 6128 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 6129 State& state, 6130 const unsigned char* in, size_t insize) 6131 { 6132 unsigned char* buffer = NULL; 6133 unsigned error = lodepng_decode(&buffer, &w, &h, &state, in, insize); 6134 if(buffer && !error) 6135 { 6136 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); 6137 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6138 } 6139 lodepng_free(buffer); 6140 return error; 6141 } 6142 6143 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, 6144 State& state, 6145 const std::vector<unsigned char>& in) 6146 { 6147 return decode(out, w, h, state, in.empty() ? 0 : &in[0], in.size()); 6148 } 6149 6150 #ifdef LODEPNG_COMPILE_DISK 6151 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename, 6152 LodePNGColorType colortype, unsigned bitdepth) 6153 { 6154 std::vector<unsigned char> buffer; 6155 unsigned error = load_file(buffer, filename); 6156 if(error) return error; 6157 return decode(out, w, h, buffer, colortype, bitdepth); 6158 } 6159 #endif /* LODEPNG_COMPILE_DECODER */ 6160 #endif /* LODEPNG_COMPILE_DISK */ 6161 6162 #ifdef LODEPNG_COMPILE_ENCODER 6163 unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h, 6164 LodePNGColorType colortype, unsigned bitdepth) 6165 { 6166 unsigned char* buffer; 6167 size_t buffersize; 6168 unsigned error = lodepng_encode_memory(&buffer, &buffersize, in, w, h, colortype, bitdepth); 6169 if(buffer) 6170 { 6171 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6172 lodepng_free(buffer); 6173 } 6174 return error; 6175 } 6176 6177 unsigned encode(std::vector<unsigned char>& out, 6178 const std::vector<unsigned char>& in, unsigned w, unsigned h, 6179 LodePNGColorType colortype, unsigned bitdepth) 6180 { 6181 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; 6182 return encode(out, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); 6183 } 6184 6185 unsigned encode(std::vector<unsigned char>& out, 6186 const unsigned char* in, unsigned w, unsigned h, 6187 State& state) 6188 { 6189 unsigned char* buffer; 6190 size_t buffersize; 6191 unsigned error = lodepng_encode(&buffer, &buffersize, in, w, h, &state); 6192 if(buffer) 6193 { 6194 out.insert(out.end(), &buffer[0], &buffer[buffersize]); 6195 lodepng_free(buffer); 6196 } 6197 return error; 6198 } 6199 6200 unsigned encode(std::vector<unsigned char>& out, 6201 const std::vector<unsigned char>& in, unsigned w, unsigned h, 6202 State& state) 6203 { 6204 if(lodepng_get_raw_size(w, h, &state.info_raw) > in.size()) return 84; 6205 return encode(out, in.empty() ? 0 : &in[0], w, h, state); 6206 } 6207 6208 #ifdef LODEPNG_COMPILE_DISK 6209 unsigned encode(const std::string& filename, 6210 const unsigned char* in, unsigned w, unsigned h, 6211 LodePNGColorType colortype, unsigned bitdepth) 6212 { 6213 std::vector<unsigned char> buffer; 6214 unsigned error = encode(buffer, in, w, h, colortype, bitdepth); 6215 if(!error) error = save_file(buffer, filename); 6216 return error; 6217 } 6218 6219 unsigned encode(const std::string& filename, 6220 const std::vector<unsigned char>& in, unsigned w, unsigned h, 6221 LodePNGColorType colortype, unsigned bitdepth) 6222 { 6223 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; 6224 return encode(filename, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); 6225 } 6226 #endif /* LODEPNG_COMPILE_DISK */ 6227 #endif /* LODEPNG_COMPILE_ENCODER */ 6228 #endif /* LODEPNG_COMPILE_PNG */ 6229 } /* namespace lodepng */ 6230 #endif /*LODEPNG_COMPILE_CPP*/