annotate thirdparty/LodePNG/lodepng.c @ 695:d2e8b5edea7e

add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
author Sam <sam@basx.dev>
date Sat, 20 May 2023 16:35:23 +0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
695
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2 LodePNG version 20230410
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4 Copyright (c) 2005-2023 Lode Vandevenne
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6 This software is provided 'as-is', without any express or implied
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
7 warranty. In no event will the authors be held liable for any damages
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
8 arising from the use of this software.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
9
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
10 Permission is granted to anyone to use this software for any purpose,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
11 including commercial applications, and to alter it and redistribute it
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
12 freely, subject to the following restrictions:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
13
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
14 1. The origin of this software must not be misrepresented; you must not
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
15 claim that you wrote the original software. If you use this software
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
16 in a product, an acknowledgment in the product documentation would be
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
17 appreciated but is not required.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
18
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
19 2. Altered source versions must be plainly marked as such, and must not be
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
20 misrepresented as being the original software.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
21
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
22 3. This notice may not be removed or altered from any source
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
23 distribution.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
24 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
25
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
26 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
27 The manual and changelog are in the header file "lodepng.h"
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
28 Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for C.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
29 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
30
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
31 #include "lodepng.h"
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
32
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
33 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
34 #include <limits.h> /* LONG_MAX */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
35 #include <stdio.h> /* file handling */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
36 #endif /* LODEPNG_COMPILE_DISK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
37
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
38 #ifdef LODEPNG_COMPILE_ALLOCATORS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
39 #include <stdlib.h> /* allocations */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
40 #endif /* LODEPNG_COMPILE_ALLOCATORS */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
41
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
42 #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
43 #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
44 #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
45 #endif /*_MSC_VER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
46
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
47 const char* LODEPNG_VERSION_STRING = "20230410";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
48
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
49 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
50 This source file is divided into the following large parts. The code sections
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
51 with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
52 -Tools for C and common code for PNG and Zlib
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
53 -C Code for Zlib (huffman, deflate, ...)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
54 -C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
55 -The C++ wrapper around all of the above
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
56 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
57
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
58 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
59 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
60 /* // Tools for C, and common code for PNG and Zlib. // */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
61 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
62 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
63
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
64 /*The malloc, realloc and free functions defined here with "lodepng_" in front
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
65 of the name, so that you can easily change them to others related to your
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
66 platform if needed. Everything else in the code calls these. Pass
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
67 -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler, or comment out
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
68 #define LODEPNG_COMPILE_ALLOCATORS in the header, to disable the ones here and
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
69 define them in your own project's source files without needing to change
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
70 lodepng source code. Don't forget to remove "static" if you copypaste them
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
71 from here.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
72
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
73 #ifdef LODEPNG_COMPILE_ALLOCATORS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
74 static void* lodepng_malloc(size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
75 #ifdef LODEPNG_MAX_ALLOC
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
76 if(size > LODEPNG_MAX_ALLOC) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
77 #endif
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
78 return malloc(size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
79 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
80
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
81 /* NOTE: when realloc returns NULL, it leaves the original memory untouched */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
82 static void* lodepng_realloc(void* ptr, size_t new_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
83 #ifdef LODEPNG_MAX_ALLOC
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
84 if(new_size > LODEPNG_MAX_ALLOC) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
85 #endif
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
86 return realloc(ptr, new_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
87 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
88
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
89 static void lodepng_free(void* ptr) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
90 free(ptr);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
91 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
92 #else /*LODEPNG_COMPILE_ALLOCATORS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
93 /* TODO: support giving additional void* payload to the custom allocators */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
94 void* lodepng_malloc(size_t size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
95 void* lodepng_realloc(void* ptr, size_t new_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
96 void lodepng_free(void* ptr);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
97 #endif /*LODEPNG_COMPILE_ALLOCATORS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
98
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
99 /* convince the compiler to inline a function, for use when this measurably improves performance */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
100 /* inline is not available in C90, but use it when supported by the compiler */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
101 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined(__cplusplus) && (__cplusplus >= 199711L))
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
102 #define LODEPNG_INLINE inline
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
103 #else
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
104 #define LODEPNG_INLINE /* not available */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
105 #endif
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
106
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
107 /* restrict is not available in C90, but use it when supported by the compiler */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
108 #if (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) ||\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
109 (defined(_MSC_VER) && (_MSC_VER >= 1400)) || \
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
110 (defined(__WATCOMC__) && (__WATCOMC__ >= 1250) && !defined(__cplusplus))
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
111 #define LODEPNG_RESTRICT __restrict
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
112 #else
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
113 #define LODEPNG_RESTRICT /* not available */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
114 #endif
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
115
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
116 /* Replacements for C library functions such as memcpy and strlen, to support platforms
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
117 where a full C library is not available. The compiler can recognize them and compile
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
118 to something as fast. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
119
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
120 static void lodepng_memcpy(void* LODEPNG_RESTRICT dst,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
121 const void* LODEPNG_RESTRICT src, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
122 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
123 for(i = 0; i < size; i++) ((char*)dst)[i] = ((const char*)src)[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
124 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
125
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
126 static void lodepng_memset(void* LODEPNG_RESTRICT dst,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
127 int value, size_t num) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
128 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
129 for(i = 0; i < num; i++) ((char*)dst)[i] = (char)value;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
130 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
131
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
132 /* does not check memory out of bounds, do not use on untrusted data */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
133 static size_t lodepng_strlen(const char* a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
134 const char* orig = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
135 /* avoid warning about unused function in case of disabled COMPILE... macros */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
136 (void)(&lodepng_strlen);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
137 while(*a) a++;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
138 return (size_t)(a - orig);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
139 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
140
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
141 #define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b))
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
142 #define LODEPNG_MIN(a, b) (((a) < (b)) ? (a) : (b))
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
143
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
144 #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
145 /* Safely check if adding two integers will overflow (no undefined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
146 behavior, compiler removing the code, etc...) and output result. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
147 static int lodepng_addofl(size_t a, size_t b, size_t* result) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
148 *result = a + b; /* Unsigned addition is well defined and safe in C90 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
149 return *result < a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
150 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
151 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
152
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
153 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
154 /* Safely check if multiplying two integers will overflow (no undefined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
155 behavior, compiler removing the code, etc...) and output result. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
156 static int lodepng_mulofl(size_t a, size_t b, size_t* result) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
157 *result = a * b; /* Unsigned multiplication is well defined and safe in C90 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
158 return (a != 0 && *result / a != b);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
159 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
160
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
161 #ifdef LODEPNG_COMPILE_ZLIB
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
162 /* Safely check if a + b > c, even if overflow could happen. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
163 static int lodepng_gtofl(size_t a, size_t b, size_t c) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
164 size_t d;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
165 if(lodepng_addofl(a, b, &d)) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
166 return d > c;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
167 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
168 #endif /*LODEPNG_COMPILE_ZLIB*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
169 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
170
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
171
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
172 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
173 Often in case of an error a value is assigned to a variable and then it breaks
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
174 out of a loop (to go to the cleanup phase of a function). This macro does that.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
175 It makes the error handling code shorter and more readable.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
176
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
177 Example: if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
178 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
179 #define CERROR_BREAK(errorvar, code){\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
180 errorvar = code;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
181 break;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
182 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
183
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
184 /*version of CERROR_BREAK that assumes the common case where the error variable is named "error"*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
185 #define ERROR_BREAK(code) CERROR_BREAK(error, code)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
186
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
187 /*Set error var to the error code, and return it.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
188 #define CERROR_RETURN_ERROR(errorvar, code){\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
189 errorvar = code;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
190 return code;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
191 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
192
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
193 /*Try the code, if it returns error, also return the error.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
194 #define CERROR_TRY_RETURN(call){\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
195 unsigned error = call;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
196 if(error) return error;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
197 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
198
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
199 /*Set error var to the error code, and return from the void function.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
200 #define CERROR_RETURN(errorvar, code){\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
201 errorvar = code;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
202 return;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
203 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
204
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
205 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
206 About uivector, ucvector and string:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
207 -All of them wrap dynamic arrays or text strings in a similar way.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
208 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
209 -The string tools are made to avoid problems with compilers that declare things like strncat as deprecated.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
210 -They're not used in the interface, only internally in this file as static functions.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
211 -As with many other structs in this file, the init and cleanup functions serve as ctor and dtor.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
212 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
213
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
214 #ifdef LODEPNG_COMPILE_ZLIB
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
215 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
216 /*dynamic vector of unsigned ints*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
217 typedef struct uivector {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
218 unsigned* data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
219 size_t size; /*size in number of unsigned longs*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
220 size_t allocsize; /*allocated size in bytes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
221 } uivector;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
222
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
223 static void uivector_cleanup(void* p) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
224 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
225 lodepng_free(((uivector*)p)->data);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
226 ((uivector*)p)->data = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
227 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
228
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
229 /*returns 1 if success, 0 if failure ==> nothing done*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
230 static unsigned uivector_resize(uivector* p, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
231 size_t allocsize = size * sizeof(unsigned);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
232 if(allocsize > p->allocsize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
233 size_t newsize = allocsize + (p->allocsize >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
234 void* data = lodepng_realloc(p->data, newsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
235 if(data) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
236 p->allocsize = newsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
237 p->data = (unsigned*)data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
238 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
239 else return 0; /*error: not enough memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
240 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
241 p->size = size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
242 return 1; /*success*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
243 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
244
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
245 static void uivector_init(uivector* p) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
246 p->data = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
247 p->size = p->allocsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
248 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
249
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
250 /*returns 1 if success, 0 if failure ==> nothing done*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
251 static unsigned uivector_push_back(uivector* p, unsigned c) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
252 if(!uivector_resize(p, p->size + 1)) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
253 p->data[p->size - 1] = c;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
254 return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
255 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
256 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
257 #endif /*LODEPNG_COMPILE_ZLIB*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
258
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
259 /* /////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
260
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
261 /*dynamic vector of unsigned chars*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
262 typedef struct ucvector {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
263 unsigned char* data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
264 size_t size; /*used size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
265 size_t allocsize; /*allocated size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
266 } ucvector;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
267
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
268 /*returns 1 if success, 0 if failure ==> nothing done*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
269 static unsigned ucvector_reserve(ucvector* p, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
270 if(size > p->allocsize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
271 size_t newsize = size + (p->allocsize >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
272 void* data = lodepng_realloc(p->data, newsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
273 if(data) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
274 p->allocsize = newsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
275 p->data = (unsigned char*)data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
276 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
277 else return 0; /*error: not enough memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
278 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
279 return 1; /*success*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
280 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
281
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
282 /*returns 1 if success, 0 if failure ==> nothing done*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
283 static unsigned ucvector_resize(ucvector* p, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
284 p->size = size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
285 return ucvector_reserve(p, size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
286 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
287
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
288 static ucvector ucvector_init(unsigned char* buffer, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
289 ucvector v;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
290 v.data = buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
291 v.allocsize = v.size = size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
292 return v;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
293 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
294
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
295 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
296
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
297 #ifdef LODEPNG_COMPILE_PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
298 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
299
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
300 /*free string pointer and set it to NULL*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
301 static void string_cleanup(char** out) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
302 lodepng_free(*out);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
303 *out = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
304 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
305
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
306 /*also appends null termination character*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
307 static char* alloc_string_sized(const char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
308 char* out = (char*)lodepng_malloc(insize + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
309 if(out) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
310 lodepng_memcpy(out, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
311 out[insize] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
312 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
313 return out;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
314 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
315
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
316 /* dynamically allocates a new string with a copy of the null terminated input text */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
317 static char* alloc_string(const char* in) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
318 return alloc_string_sized(in, lodepng_strlen(in));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
319 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
320 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
321 #endif /*LODEPNG_COMPILE_PNG*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
322
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
323 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
324
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
325 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
326 static unsigned lodepng_read32bitInt(const unsigned char* buffer) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
327 return (((unsigned)buffer[0] << 24u) | ((unsigned)buffer[1] << 16u) |
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
328 ((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
329 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
330 #endif /*defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
331
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
332 #if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
333 /*buffer must have at least 4 allocated bytes available*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
334 static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
335 buffer[0] = (unsigned char)((value >> 24) & 0xff);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
336 buffer[1] = (unsigned char)((value >> 16) & 0xff);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
337 buffer[2] = (unsigned char)((value >> 8) & 0xff);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
338 buffer[3] = (unsigned char)((value ) & 0xff);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
339 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
340 #endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
341
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
342 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
343 /* / File IO / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
344 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
345
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
346 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
347
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
348 /* returns negative value on error. This should be pure C compatible, so no fstat. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
349 static long lodepng_filesize(const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
350 FILE* file;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
351 long size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
352 file = fopen(filename, "rb");
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
353 if(!file) return -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
354
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
355 if(fseek(file, 0, SEEK_END) != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
356 fclose(file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
357 return -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
358 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
359
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
360 size = ftell(file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
361 /* It may give LONG_MAX as directory size, this is invalid for us. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
362 if(size == LONG_MAX) size = -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
363
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
364 fclose(file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
365 return size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
366 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
367
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
368 /* load file into buffer that already has the correct allocated size. Returns error code.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
369 static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
370 FILE* file;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
371 size_t readsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
372 file = fopen(filename, "rb");
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
373 if(!file) return 78;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
374
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
375 readsize = fread(out, 1, size, file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
376 fclose(file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
377
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
378 if(readsize != size) return 78;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
379 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
380 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
381
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
382 unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
383 long size = lodepng_filesize(filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
384 if(size < 0) return 78;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
385 *outsize = (size_t)size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
386
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
387 *out = (unsigned char*)lodepng_malloc((size_t)size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
388 if(!(*out) && size > 0) return 83; /*the above malloc failed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
389
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
390 return lodepng_buffer_file(*out, (size_t)size, filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
391 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
392
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
393 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
394 unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
395 FILE* file;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
396 file = fopen(filename, "wb" );
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
397 if(!file) return 79;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
398 fwrite(buffer, 1, buffersize, file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
399 fclose(file);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
400 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
401 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
402
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
403 #endif /*LODEPNG_COMPILE_DISK*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
404
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
405 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
406 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
407 /* // End of common code and tools. Begin of Zlib related code. // */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
408 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
409 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
410
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
411 #ifdef LODEPNG_COMPILE_ZLIB
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
412 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
413
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
414 typedef struct {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
415 ucvector* data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
416 unsigned char bp; /*ok to overflow, indicates bit pos inside byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
417 } LodePNGBitWriter;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
418
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
419 static void LodePNGBitWriter_init(LodePNGBitWriter* writer, ucvector* data) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
420 writer->data = data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
421 writer->bp = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
422 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
423
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
424 /*TODO: this ignores potential out of memory errors*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
425 #define WRITEBIT(writer, bit){\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
426 /* append new byte */\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
427 if(((writer->bp) & 7u) == 0) {\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
428 if(!ucvector_resize(writer->data, writer->data->size + 1)) return;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
429 writer->data->data[writer->data->size - 1] = 0;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
430 }\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
431 (writer->data->data[writer->data->size - 1]) |= (bit << ((writer->bp) & 7u));\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
432 ++writer->bp;\
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
433 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
434
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
435 /* LSB of value is written first, and LSB of bytes is used first */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
436 static void writeBits(LodePNGBitWriter* writer, unsigned value, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
437 if(nbits == 1) { /* compiler should statically compile this case if nbits == 1 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
438 WRITEBIT(writer, value);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
439 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
440 /* TODO: increase output size only once here rather than in each WRITEBIT */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
441 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
442 for(i = 0; i != nbits; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
443 WRITEBIT(writer, (unsigned char)((value >> i) & 1));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
444 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
445 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
446 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
447
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
448 /* This one is to use for adding huffman symbol, the value bits are written MSB first */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
449 static void writeBitsReversed(LodePNGBitWriter* writer, unsigned value, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
450 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
451 for(i = 0; i != nbits; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
452 /* TODO: increase output size only once here rather than in each WRITEBIT */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
453 WRITEBIT(writer, (unsigned char)((value >> (nbits - 1u - i)) & 1u));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
454 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
455 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
456 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
457
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
458 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
459
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
460 typedef struct {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
461 const unsigned char* data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
462 size_t size; /*size of data in bytes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
463 size_t bitsize; /*size of data in bits, end of valid bp values, should be 8*size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
464 size_t bp;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
465 unsigned buffer; /*buffer for reading bits. NOTE: 'unsigned' must support at least 32 bits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
466 } LodePNGBitReader;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
467
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
468 /* data size argument is in bytes. Returns error if size too large causing overflow */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
469 static unsigned LodePNGBitReader_init(LodePNGBitReader* reader, const unsigned char* data, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
470 size_t temp;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
471 reader->data = data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
472 reader->size = size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
473 /* size in bits, return error if overflow (if size_t is 32 bit this supports up to 500MB) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
474 if(lodepng_mulofl(size, 8u, &reader->bitsize)) return 105;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
475 /*ensure incremented bp can be compared to bitsize without overflow even when it would be incremented 32 too much and
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
476 trying to ensure 32 more bits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
477 if(lodepng_addofl(reader->bitsize, 64u, &temp)) return 105;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
478 reader->bp = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
479 reader->buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
480 return 0; /*ok*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
481 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
482
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
483 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
484 ensureBits functions:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
485 Ensures the reader can at least read nbits bits in one or more readBits calls,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
486 safely even if not enough bits are available.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
487 The nbits parameter is unused but is given for documentation purposes, error
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
488 checking for amount of bits must be done beforehand.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
489 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
490
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
491 /*See ensureBits documentation above. This one ensures up to 9 bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
492 static LODEPNG_INLINE void ensureBits9(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
493 size_t start = reader->bp >> 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
494 size_t size = reader->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
495 if(start + 1u < size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
496 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
497 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
498 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
499 reader->buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
500 if(start + 0u < size) reader->buffer = reader->data[start + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
501 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
502 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
503 (void)nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
504 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
505
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
506 /*See ensureBits documentation above. This one ensures up to 17 bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
507 static LODEPNG_INLINE void ensureBits17(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
508 size_t start = reader->bp >> 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
509 size_t size = reader->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
510 if(start + 2u < size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
511 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) |
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
512 ((unsigned)reader->data[start + 2] << 16u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
513 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
514 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
515 reader->buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
516 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
517 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
518 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
519 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
520 (void)nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
521 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
522
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
523 /*See ensureBits documentation above. This one ensures up to 25 bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
524 static LODEPNG_INLINE void ensureBits25(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
525 size_t start = reader->bp >> 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
526 size_t size = reader->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
527 if(start + 3u < size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
528 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) |
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
529 ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
530 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
531 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
532 reader->buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
533 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
534 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
535 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
536 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
537 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
538 (void)nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
539 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
540
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
541 /*See ensureBits documentation above. This one ensures up to 32 bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
542 static LODEPNG_INLINE void ensureBits32(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
543 size_t start = reader->bp >> 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
544 size_t size = reader->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
545 if(start + 4u < size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
546 reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) |
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
547 ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
548 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
549 reader->buffer |= (((unsigned)reader->data[start + 4] << 24u) << (8u - (reader->bp & 7u)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
550 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
551 reader->buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
552 if(start + 0u < size) reader->buffer |= reader->data[start + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
553 if(start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
554 if(start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
555 if(start + 3u < size) reader->buffer |= ((unsigned)reader->data[start + 3] << 24u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
556 reader->buffer >>= (reader->bp & 7u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
557 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
558 (void)nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
559 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
560
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
561 /* Get bits without advancing the bit pointer. Must have enough bits available with ensureBits. Max nbits is 31. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
562 static LODEPNG_INLINE unsigned peekBits(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
563 /* The shift allows nbits to be only up to 31. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
564 return reader->buffer & ((1u << nbits) - 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
565 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
566
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
567 /* Must have enough bits available with ensureBits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
568 static LODEPNG_INLINE void advanceBits(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
569 reader->buffer >>= nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
570 reader->bp += nbits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
571 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
572
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
573 /* Must have enough bits available with ensureBits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
574 static LODEPNG_INLINE unsigned readBits(LodePNGBitReader* reader, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
575 unsigned result = peekBits(reader, nbits);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
576 advanceBits(reader, nbits);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
577 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
578 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
579 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
580
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
581 static unsigned reverseBits(unsigned bits, unsigned num) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
582 /*TODO: implement faster lookup table based version when needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
583 unsigned i, result = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
584 for(i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
585 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
586 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
587
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
588 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
589 /* / Deflate - Huffman / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
590 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
591
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
592 #define FIRST_LENGTH_CODE_INDEX 257
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
593 #define LAST_LENGTH_CODE_INDEX 285
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
594 /*256 literals, the end code, some length codes, and 2 unused codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
595 #define NUM_DEFLATE_CODE_SYMBOLS 288
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
596 /*the distance codes have their own symbols, 30 used, 2 unused*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
597 #define NUM_DISTANCE_SYMBOLS 32
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
598 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
599 #define NUM_CODE_LENGTH_CODES 19
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
600
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
601 /*the base lengths represented by codes 257-285*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
602 static const unsigned LENGTHBASE[29]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
603 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
604 67, 83, 99, 115, 131, 163, 195, 227, 258};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
605
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
606 /*the extra bits used by codes 257-285 (added to base length)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
607 static const unsigned LENGTHEXTRA[29]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
608 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
609 4, 4, 4, 4, 5, 5, 5, 5, 0};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
610
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
611 /*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
612 static const unsigned DISTANCEBASE[30]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
613 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
614 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
615
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
616 /*the extra bits of backwards distances (added to base)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
617 static const unsigned DISTANCEEXTRA[30]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
618 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
619 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
620
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
621 /*the order in which "code length alphabet code lengths" are stored as specified by deflate, out of this the huffman
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
622 tree of the dynamic huffman tree lengths is generated*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
623 static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
624 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
625
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
626 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
627
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
628 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
629 Huffman tree struct, containing multiple representations of the tree
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
630 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
631 typedef struct HuffmanTree {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
632 unsigned* codes; /*the huffman codes (bit patterns representing the symbols)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
633 unsigned* lengths; /*the lengths of the huffman codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
634 unsigned maxbitlen; /*maximum number of bits a single code can get*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
635 unsigned numcodes; /*number of symbols in the alphabet = number of codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
636 /* for reading only */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
637 unsigned char* table_len; /*length of symbol from lookup table, or max length if secondary lookup needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
638 unsigned short* table_value; /*value of symbol from lookup table, or pointer to secondary table if needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
639 } HuffmanTree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
640
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
641 static void HuffmanTree_init(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
642 tree->codes = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
643 tree->lengths = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
644 tree->table_len = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
645 tree->table_value = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
646 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
647
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
648 static void HuffmanTree_cleanup(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
649 lodepng_free(tree->codes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
650 lodepng_free(tree->lengths);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
651 lodepng_free(tree->table_len);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
652 lodepng_free(tree->table_value);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
653 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
654
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
655 /* amount of bits for first huffman table lookup (aka root bits), see HuffmanTree_makeTable and huffmanDecodeSymbol.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
656 /* values 8u and 9u work the fastest */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
657 #define FIRSTBITS 9u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
658
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
659 /* a symbol value too big to represent any valid symbol, to indicate reading disallowed huffman bits combination,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
660 which is possible in case of only 0 or 1 present symbols. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
661 #define INVALIDSYMBOL 65535u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
662
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
663 /* make table for huffman decoding */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
664 static unsigned HuffmanTree_makeTable(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
665 static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
666 static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
667 size_t i, numpresent, pointer, size; /*total table size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
668 unsigned* maxlens = (unsigned*)lodepng_malloc(headsize * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
669 if(!maxlens) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
670
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
671 /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
672 lodepng_memset(maxlens, 0, headsize * sizeof(*maxlens));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
673 for(i = 0; i < tree->numcodes; i++) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
674 unsigned symbol = tree->codes[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
675 unsigned l = tree->lengths[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
676 unsigned index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
677 if(l <= FIRSTBITS) continue; /*symbols that fit in first table don't increase secondary table size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
678 /*get the FIRSTBITS MSBs, the MSBs of the symbol are encoded first. See later comment about the reversing*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
679 index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
680 maxlens[index] = LODEPNG_MAX(maxlens[index], l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
681 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
682 /* compute total table size: size of first table plus all secondary tables for symbols longer than FIRSTBITS */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
683 size = headsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
684 for(i = 0; i < headsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
685 unsigned l = maxlens[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
686 if(l > FIRSTBITS) size += (1u << (l - FIRSTBITS));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
687 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
688 tree->table_len = (unsigned char*)lodepng_malloc(size * sizeof(*tree->table_len));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
689 tree->table_value = (unsigned short*)lodepng_malloc(size * sizeof(*tree->table_value));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
690 if(!tree->table_len || !tree->table_value) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
691 lodepng_free(maxlens);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
692 /* freeing tree->table values is done at a higher scope */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
693 return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
694 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
695 /*initialize with an invalid length to indicate unused entries*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
696 for(i = 0; i < size; ++i) tree->table_len[i] = 16;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
697
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
698 /*fill in the first table for long symbols: max prefix size and pointer to secondary tables*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
699 pointer = headsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
700 for(i = 0; i < headsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
701 unsigned l = maxlens[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
702 if(l <= FIRSTBITS) continue;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
703 tree->table_len[i] = l;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
704 tree->table_value[i] = pointer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
705 pointer += (1u << (l - FIRSTBITS));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
706 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
707 lodepng_free(maxlens);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
708
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
709 /*fill in the first table for short symbols, or secondary table for long symbols*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
710 numpresent = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
711 for(i = 0; i < tree->numcodes; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
712 unsigned l = tree->lengths[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
713 unsigned symbol, reverse;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
714 if(l == 0) continue;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
715 symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
716 /*reverse bits, because the huffman bits are given in MSB first order but the bit reader reads LSB first*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
717 reverse = reverseBits(symbol, l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
718 numpresent++;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
719
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
720 if(l <= FIRSTBITS) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
721 /*short symbol, fully in first table, replicated num times if l < FIRSTBITS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
722 unsigned num = 1u << (FIRSTBITS - l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
723 unsigned j;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
724 for(j = 0; j < num; ++j) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
725 /*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB's*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
726 unsigned index = reverse | (j << l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
727 if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
728 tree->table_len[index] = l;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
729 tree->table_value[index] = i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
730 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
731 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
732 /*long symbol, shares prefix with other long symbols in first lookup table, needs second lookup*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
733 /*the FIRSTBITS MSBs of the symbol are the first table index*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
734 unsigned index = reverse & mask;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
735 unsigned maxlen = tree->table_len[index];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
736 /*log2 of secondary table length, should be >= l - FIRSTBITS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
737 unsigned tablelen = maxlen - FIRSTBITS;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
738 unsigned start = tree->table_value[index]; /*starting index in secondary table*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
739 unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary table*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
740 unsigned j;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
741 if(maxlen < l) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
742 for(j = 0; j < num; ++j) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
743 unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
744 unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
745 tree->table_len[index2] = l;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
746 tree->table_value[index2] = i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
747 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
748 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
749 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
750
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
751 if(numpresent < 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
752 /* In case of exactly 1 symbol, in theory the huffman symbol needs 0 bits,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
753 but deflate uses 1 bit instead. In case of 0 symbols, no symbols can
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
754 appear at all, but such huffman tree could still exist (e.g. if distance
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
755 codes are never used). In both cases, not all symbols of the table will be
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
756 filled in. Fill them in with an invalid symbol value so returning them from
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
757 huffmanDecodeSymbol will cause error. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
758 for(i = 0; i < size; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
759 if(tree->table_len[i] == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
760 /* As length, use a value smaller than FIRSTBITS for the head table,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
761 and a value larger than FIRSTBITS for the secondary table, to ensure
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
762 valid behavior for advanceBits when reading this symbol. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
763 tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
764 tree->table_value[i] = INVALIDSYMBOL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
765 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
766 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
767 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
768 /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
769 If that is not the case (due to too long length codes), the table will not
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
770 have been fully used, and this is an error (not all bit combinations can be
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
771 decoded): an oversubscribed huffman tree, indicated by error 55. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
772 for(i = 0; i < size; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
773 if(tree->table_len[i] == 16) return 55;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
774 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
775 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
776
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
777 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
778 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
779
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
780 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
781 Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
782 numcodes, lengths and maxbitlen must already be filled in correctly. return
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
783 value is error.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
784 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
785 static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
786 unsigned* blcount;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
787 unsigned* nextcode;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
788 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
789 unsigned bits, n;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
790
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
791 tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
792 blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
793 nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
794 if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
795
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
796 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
797 for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
798 /*step 1: count number of instances of each code length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
799 for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
800 /*step 2: generate the nextcode values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
801 for(bits = 1; bits <= tree->maxbitlen; ++bits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
802 nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
803 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
804 /*step 3: generate all the codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
805 for(n = 0; n != tree->numcodes; ++n) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
806 if(tree->lengths[n] != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
807 tree->codes[n] = nextcode[tree->lengths[n]]++;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
808 /*remove superfluous bits from the code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
809 tree->codes[n] &= ((1u << tree->lengths[n]) - 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
810 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
811 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
812 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
813
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
814 lodepng_free(blcount);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
815 lodepng_free(nextcode);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
816
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
817 if(!error) error = HuffmanTree_makeTable(tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
818 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
819 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
820
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
821 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
822 given the code lengths (as stored in the PNG file), generate the tree as defined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
823 by Deflate. maxbitlen is the maximum bits that a code in the tree can have.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
824 return value is error.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
825 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
826 static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
827 size_t numcodes, unsigned maxbitlen) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
828 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
829 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
830 if(!tree->lengths) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
831 for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
832 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
833 tree->maxbitlen = maxbitlen;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
834 return HuffmanTree_makeFromLengths2(tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
835 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
836
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
837 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
838
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
839 /*BPM: Boundary Package Merge, see "A Fast and Space-Economical Algorithm for Length-Limited Coding",
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
840 Jyrki Katajainen, Alistair Moffat, Andrew Turpin, 1995.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
841
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
842 /*chain node for boundary package merge*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
843 typedef struct BPMNode {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
844 int weight; /*the sum of all weights in this chain*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
845 unsigned index; /*index of this leaf node (called "count" in the paper)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
846 struct BPMNode* tail; /*the next nodes in this chain (null if last)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
847 int in_use;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
848 } BPMNode;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
849
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
850 /*lists of chains*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
851 typedef struct BPMLists {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
852 /*memory pool*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
853 unsigned memsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
854 BPMNode* memory;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
855 unsigned numfree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
856 unsigned nextfree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
857 BPMNode** freelist;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
858 /*two heads of lookahead chains per list*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
859 unsigned listsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
860 BPMNode** chains0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
861 BPMNode** chains1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
862 } BPMLists;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
863
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
864 /*creates a new chain node with the given parameters, from the memory in the lists */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
865 static BPMNode* bpmnode_create(BPMLists* lists, int weight, unsigned index, BPMNode* tail) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
866 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
867 BPMNode* result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
868
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
869 /*memory full, so garbage collect*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
870 if(lists->nextfree >= lists->numfree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
871 /*mark only those that are in use*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
872 for(i = 0; i != lists->memsize; ++i) lists->memory[i].in_use = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
873 for(i = 0; i != lists->listsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
874 BPMNode* node;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
875 for(node = lists->chains0[i]; node != 0; node = node->tail) node->in_use = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
876 for(node = lists->chains1[i]; node != 0; node = node->tail) node->in_use = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
877 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
878 /*collect those that are free*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
879 lists->numfree = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
880 for(i = 0; i != lists->memsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
881 if(!lists->memory[i].in_use) lists->freelist[lists->numfree++] = &lists->memory[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
882 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
883 lists->nextfree = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
884 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
885
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
886 result = lists->freelist[lists->nextfree++];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
887 result->weight = weight;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
888 result->index = index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
889 result->tail = tail;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
890 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
891 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
892
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
893 /*sort the leaves with stable mergesort*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
894 static void bpmnode_sort(BPMNode* leaves, size_t num) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
895 BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
896 size_t width, counter = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
897 for(width = 1; width < num; width *= 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
898 BPMNode* a = (counter & 1) ? mem : leaves;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
899 BPMNode* b = (counter & 1) ? leaves : mem;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
900 size_t p;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
901 for(p = 0; p < num; p += 2 * width) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
902 size_t q = (p + width > num) ? num : (p + width);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
903 size_t r = (p + 2 * width > num) ? num : (p + 2 * width);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
904 size_t i = p, j = q, k;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
905 for(k = p; k < r; k++) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
906 if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
907 else b[k] = a[j++];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
908 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
909 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
910 counter++;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
911 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
912 if(counter & 1) lodepng_memcpy(leaves, mem, sizeof(*leaves) * num);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
913 lodepng_free(mem);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
914 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
915
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
916 /*Boundary Package Merge step, numpresent is the amount of leaves, and c is the current chain.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
917 static void boundaryPM(BPMLists* lists, BPMNode* leaves, size_t numpresent, int c, int num) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
918 unsigned lastindex = lists->chains1[c]->index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
919
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
920 if(c == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
921 if(lastindex >= numpresent) return;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
922 lists->chains0[c] = lists->chains1[c];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
923 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
924 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
925 /*sum of the weights of the head nodes of the previous lookahead chains.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
926 int sum = lists->chains0[c - 1]->weight + lists->chains1[c - 1]->weight;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
927 lists->chains0[c] = lists->chains1[c];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
928 if(lastindex < numpresent && sum > leaves[lastindex].weight) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
929 lists->chains1[c] = bpmnode_create(lists, leaves[lastindex].weight, lastindex + 1, lists->chains1[c]->tail);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
930 return;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
931 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
932 lists->chains1[c] = bpmnode_create(lists, sum, lastindex, lists->chains1[c - 1]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
933 /*in the end we are only interested in the chain of the last list, so no
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
934 need to recurse if we're at the last one (this gives measurable speedup)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
935 if(num + 1 < (int)(2 * numpresent - 2)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
936 boundaryPM(lists, leaves, numpresent, c - 1, num);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
937 boundaryPM(lists, leaves, numpresent, c - 1, num);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
938 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
939 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
940 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
941
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
942 unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
943 size_t numcodes, unsigned maxbitlen) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
944 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
945 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
946 size_t numpresent = 0; /*number of symbols with non-zero frequency*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
947 BPMNode* leaves; /*the symbols, only those with > 0 frequency*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
948
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
949 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
950 if((1u << maxbitlen) < (unsigned)numcodes) return 80; /*error: represent all symbols*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
951
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
952 leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
953 if(!leaves) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
954
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
955 for(i = 0; i != numcodes; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
956 if(frequencies[i] > 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
957 leaves[numpresent].weight = (int)frequencies[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
958 leaves[numpresent].index = i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
959 ++numpresent;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
960 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
961 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
962
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
963 lodepng_memset(lengths, 0, numcodes * sizeof(*lengths));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
964
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
965 /*ensure at least two present symbols. There should be at least one symbol
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
966 according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
967 make these work as well ensure there are at least two symbols. The
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
968 Package-Merge code below also doesn't work correctly if there's only one
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
969 symbol, it'd give it the theoretical 0 bits but in practice zlib wants 1 bit*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
970 if(numpresent == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
971 lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
972 } else if(numpresent == 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
973 lengths[leaves[0].index] = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
974 lengths[leaves[0].index == 0 ? 1 : 0] = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
975 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
976 BPMLists lists;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
977 BPMNode* node;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
978
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
979 bpmnode_sort(leaves, numpresent);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
980
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
981 lists.listsize = maxbitlen;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
982 lists.memsize = 2 * maxbitlen * (maxbitlen + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
983 lists.nextfree = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
984 lists.numfree = lists.memsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
985 lists.memory = (BPMNode*)lodepng_malloc(lists.memsize * sizeof(*lists.memory));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
986 lists.freelist = (BPMNode**)lodepng_malloc(lists.memsize * sizeof(BPMNode*));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
987 lists.chains0 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
988 lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
989 if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
990
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
991 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
992 for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
993
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
994 bpmnode_create(&lists, leaves[0].weight, 1, 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
995 bpmnode_create(&lists, leaves[1].weight, 2, 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
996
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
997 for(i = 0; i != lists.listsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
998 lists.chains0[i] = &lists.memory[0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
999 lists.chains1[i] = &lists.memory[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1000 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1001
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1002 /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1003 for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1004
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1005 for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1006 for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1007 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1008 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1009
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1010 lodepng_free(lists.memory);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1011 lodepng_free(lists.freelist);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1012 lodepng_free(lists.chains0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1013 lodepng_free(lists.chains1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1014 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1015
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1016 lodepng_free(leaves);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1017 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1018 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1019
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1020 /*Create the Huffman tree given the symbol frequencies*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1021 static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1022 size_t mincodes, size_t numcodes, unsigned maxbitlen) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1023 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1024 while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1025 tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1026 if(!tree->lengths) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1027 tree->maxbitlen = maxbitlen;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1028 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1029
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1030 error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1031 if(!error) error = HuffmanTree_makeFromLengths2(tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1032 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1033 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1034 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1035
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1036 /*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1037 static unsigned generateFixedLitLenTree(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1038 unsigned i, error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1039 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1040 if(!bitlen) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1041
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1042 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1043 for(i = 0; i <= 143; ++i) bitlen[i] = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1044 for(i = 144; i <= 255; ++i) bitlen[i] = 9;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1045 for(i = 256; i <= 279; ++i) bitlen[i] = 7;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1046 for(i = 280; i <= 287; ++i) bitlen[i] = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1047
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1048 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1049
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1050 lodepng_free(bitlen);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1051 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1052 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1053
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1054 /*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1055 static unsigned generateFixedDistanceTree(HuffmanTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1056 unsigned i, error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1057 unsigned* bitlen = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1058 if(!bitlen) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1059
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1060 /*there are 32 distance codes, but 30-31 are unused*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1061 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1062 error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1063
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1064 lodepng_free(bitlen);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1065 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1066 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1067
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1068 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1069
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1070 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1071 returns the code. The bit reader must already have been ensured at least 15 bits
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1072 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1073 static unsigned huffmanDecodeSymbol(LodePNGBitReader* reader, const HuffmanTree* codetree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1074 unsigned short code = peekBits(reader, FIRSTBITS);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1075 unsigned short l = codetree->table_len[code];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1076 unsigned short value = codetree->table_value[code];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1077 if(l <= FIRSTBITS) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1078 advanceBits(reader, l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1079 return value;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1080 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1081 advanceBits(reader, FIRSTBITS);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1082 value += peekBits(reader, l - FIRSTBITS);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1083 advanceBits(reader, codetree->table_len[value] - FIRSTBITS);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1084 return codetree->table_value[value];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1085 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1086 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1087 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1088
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1089 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1090
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1091 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1092 /* / Inflator (Decompressor) / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1093 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1094
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1095 /*get the tree of a deflated block with fixed tree, as specified in the deflate specification
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1096 Returns error code.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1097 static unsigned getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1098 unsigned error = generateFixedLitLenTree(tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1099 if(error) return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1100 return generateFixedDistanceTree(tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1101 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1102
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1103 /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1104 static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1105 LodePNGBitReader* reader) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1106 /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1107 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1108 unsigned n, HLIT, HDIST, HCLEN, i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1109
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1110 /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1111 unsigned* bitlen_ll = 0; /*lit,len code lengths*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1112 unsigned* bitlen_d = 0; /*dist code lengths*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1113 /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1114 unsigned* bitlen_cl = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1115 HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1116
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1117 if(reader->bitsize - reader->bp < 14) return 49; /*error: the bit pointer is or will go past the memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1118 ensureBits17(reader, 14);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1119
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1120 /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1121 HLIT = readBits(reader, 5) + 257;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1122 /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1123 HDIST = readBits(reader, 5) + 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1124 /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1125 HCLEN = readBits(reader, 4) + 4;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1126
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1127 bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1128 if(!bitlen_cl) return 83 /*alloc fail*/;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1129
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1130 HuffmanTree_init(&tree_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1131
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1132 while(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1133 /*read the code length codes out of 3 * (amount of code length codes) bits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1134 if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1135 ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1136 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1137 for(i = 0; i != HCLEN; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1138 ensureBits9(reader, 3); /*out of bounds already checked above */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1139 bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1140 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1141 for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1142 bitlen_cl[CLCL_ORDER[i]] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1143 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1144
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1145 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1146 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1147
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1148 /*now we can use this tree to read the lengths for the tree that this function will return*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1149 bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1150 bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1151 if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1152 lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1153 lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1154
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1155 /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1156 i = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1157 while(i < HLIT + HDIST) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1158 unsigned code;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1159 ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1160 code = huffmanDecodeSymbol(reader, &tree_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1161 if(code <= 15) /*a length code*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1162 if(i < HLIT) bitlen_ll[i] = code;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1163 else bitlen_d[i - HLIT] = code;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1164 ++i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1165 } else if(code == 16) /*repeat previous*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1166 unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1167 unsigned value; /*set value to the previous code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1168
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1169 if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1170
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1171 replength += readBits(reader, 2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1172
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1173 if(i < HLIT + 1) value = bitlen_ll[i - 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1174 else value = bitlen_d[i - HLIT - 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1175 /*repeat this value in the next lengths*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1176 for(n = 0; n < replength; ++n) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1177 if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1178 if(i < HLIT) bitlen_ll[i] = value;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1179 else bitlen_d[i - HLIT] = value;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1180 ++i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1181 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1182 } else if(code == 17) /*repeat "0" 3-10 times*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1183 unsigned replength = 3; /*read in the bits that indicate repeat length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1184 replength += readBits(reader, 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1185
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1186 /*repeat this value in the next lengths*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1187 for(n = 0; n < replength; ++n) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1188 if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1189
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1190 if(i < HLIT) bitlen_ll[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1191 else bitlen_d[i - HLIT] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1192 ++i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1193 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1194 } else if(code == 18) /*repeat "0" 11-138 times*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1195 unsigned replength = 11; /*read in the bits that indicate repeat length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1196 replength += readBits(reader, 7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1197
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1198 /*repeat this value in the next lengths*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1199 for(n = 0; n < replength; ++n) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1200 if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1201
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1202 if(i < HLIT) bitlen_ll[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1203 else bitlen_d[i - HLIT] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1204 ++i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1205 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1206 } else /*if(code == INVALIDSYMBOL)*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1207 ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1208 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1209 /*check if any of the ensureBits above went out of bounds*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1210 if(reader->bp > reader->bitsize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1211 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1212 (10=no endcode, 11=wrong jump outside of tree)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1213 /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1214 ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1215 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1216 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1217 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1218
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1219 if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1220
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1221 /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1222 error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1223 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1224 error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1225
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1226 break; /*end of error-while*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1227 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1228
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1229 lodepng_free(bitlen_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1230 lodepng_free(bitlen_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1231 lodepng_free(bitlen_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1232 HuffmanTree_cleanup(&tree_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1233
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1234 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1235 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1236
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1237 /*inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1238 static unsigned inflateHuffmanBlock(ucvector* out, LodePNGBitReader* reader,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1239 unsigned btype, size_t max_output_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1240 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1241 HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1242 HuffmanTree tree_d; /*the huffman tree for distance codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1243 const size_t reserved_size = 260; /* must be at least 258 for max length, and a few extra for adding a few extra literals */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1244 int done = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1245
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1246 if(!ucvector_reserve(out, out->size + reserved_size)) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1247
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1248 HuffmanTree_init(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1249 HuffmanTree_init(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1250
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1251 if(btype == 1) error = getTreeInflateFixed(&tree_ll, &tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1252 else /*if(btype == 2)*/ error = getTreeInflateDynamic(&tree_ll, &tree_d, reader);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1253
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1254
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1255 while(!error && !done) /*decode all symbols until end reached, breaks at end code*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1256 /*code_ll is literal, length or end code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1257 unsigned code_ll;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1258 /* ensure enough bits for 2 huffman code reads (15 bits each): if the first is a literal, a second literal is read at once. This
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1259 appears to be slightly faster, than ensuring 20 bits here for 1 huffman symbol and the potential 5 extra bits for the length symbol.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1260 ensureBits32(reader, 30);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1261 code_ll = huffmanDecodeSymbol(reader, &tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1262 if(code_ll <= 255) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1263 /*slightly faster code path if multiple literals in a row*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1264 out->data[out->size++] = (unsigned char)code_ll;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1265 code_ll = huffmanDecodeSymbol(reader, &tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1266 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1267 if(code_ll <= 255) /*literal symbol*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1268 out->data[out->size++] = (unsigned char)code_ll;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1269 } else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1270 unsigned code_d, distance;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1271 unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1272 size_t start, backward, length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1273
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1274 /*part 1: get length base*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1275 length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1276
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1277 /*part 2: get extra bits and add the value of that to length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1278 numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1279 if(numextrabits_l != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1280 /* bits already ensured above */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1281 ensureBits25(reader, 5);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1282 length += readBits(reader, numextrabits_l);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1283 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1284
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1285 /*part 3: get distance code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1286 ensureBits32(reader, 28); /* up to 15 for the huffman symbol, up to 13 for the extra bits */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1287 code_d = huffmanDecodeSymbol(reader, &tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1288 if(code_d > 29) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1289 if(code_d <= 31) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1290 ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1291 } else /* if(code_d == INVALIDSYMBOL) */{
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1292 ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1293 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1294 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1295 distance = DISTANCEBASE[code_d];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1296
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1297 /*part 4: get extra bits from distance*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1298 numextrabits_d = DISTANCEEXTRA[code_d];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1299 if(numextrabits_d != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1300 /* bits already ensured above */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1301 distance += readBits(reader, numextrabits_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1302 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1303
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1304 /*part 5: fill in all the out[n] values based on the length and dist*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1305 start = out->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1306 if(distance > start) ERROR_BREAK(52); /*too long backward distance*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1307 backward = start - distance;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1308
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1309 out->size += length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1310 if(distance < length) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1311 size_t forward;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1312 lodepng_memcpy(out->data + start, out->data + backward, distance);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1313 start += distance;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1314 for(forward = distance; forward < length; ++forward) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1315 out->data[start++] = out->data[backward++];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1316 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1317 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1318 lodepng_memcpy(out->data + start, out->data + backward, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1319 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1320 } else if(code_ll == 256) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1321 done = 1; /*end code, finish the loop*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1322 } else /*if(code_ll == INVALIDSYMBOL)*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1323 ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1324 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1325 if(out->allocsize - out->size < reserved_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1326 if(!ucvector_reserve(out, out->size + reserved_size)) ERROR_BREAK(83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1327 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1328 /*check if any of the ensureBits above went out of bounds*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1329 if(reader->bp > reader->bitsize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1330 /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1331 (10=no endcode, 11=wrong jump outside of tree)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1332 /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1333 ERROR_BREAK(51); /*error, bit pointer jumps past memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1334 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1335 if(max_output_size && out->size > max_output_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1336 ERROR_BREAK(109); /*error, larger than max size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1337 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1338 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1339
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1340 HuffmanTree_cleanup(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1341 HuffmanTree_cleanup(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1342
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1343 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1344 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1345
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1346 static unsigned inflateNoCompression(ucvector* out, LodePNGBitReader* reader,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1347 const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1348 size_t bytepos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1349 size_t size = reader->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1350 unsigned LEN, NLEN, error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1351
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1352 /*go to first boundary of byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1353 bytepos = (reader->bp + 7u) >> 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1354
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1355 /*read LEN (2 bytes) and NLEN (2 bytes)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1356 if(bytepos + 4 >= size) return 52; /*error, bit pointer will jump past memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1357 LEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1358 NLEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1359
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1360 /*check if 16-bit NLEN is really the one's complement of LEN*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1361 if(!settings->ignore_nlen && LEN + NLEN != 65535) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1362 return 21; /*error: NLEN is not one's complement of LEN*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1363 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1364
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1365 if(!ucvector_resize(out, out->size + LEN)) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1366
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1367 /*read the literal data: LEN bytes are now stored in the out buffer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1368 if(bytepos + LEN > size) return 23; /*error: reading outside of in buffer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1369
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1370 /*out->data can be NULL (when LEN is zero), and arithmetics on NULL ptr is undefined*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1371 if (LEN) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1372 lodepng_memcpy(out->data + out->size - LEN, reader->data + bytepos, LEN);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1373 bytepos += LEN;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1374 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1375
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1376 reader->bp = bytepos << 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1377
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1378 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1379 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1380
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1381 static unsigned lodepng_inflatev(ucvector* out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1382 const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1383 const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1384 unsigned BFINAL = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1385 LodePNGBitReader reader;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1386 unsigned error = LodePNGBitReader_init(&reader, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1387
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1388 if(error) return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1389
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1390 while(!BFINAL) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1391 unsigned BTYPE;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1392 if(reader.bitsize - reader.bp < 3) return 52; /*error, bit pointer will jump past memory*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1393 ensureBits9(&reader, 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1394 BFINAL = readBits(&reader, 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1395 BTYPE = readBits(&reader, 2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1396
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1397 if(BTYPE == 3) return 20; /*error: invalid BTYPE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1398 else if(BTYPE == 0) error = inflateNoCompression(out, &reader, settings); /*no compression*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1399 else error = inflateHuffmanBlock(out, &reader, BTYPE, settings->max_output_size); /*compression, BTYPE 01 or 10*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1400 if(!error && settings->max_output_size && out->size > settings->max_output_size) error = 109;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1401 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1402 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1403
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1404 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1405 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1406
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1407 unsigned lodepng_inflate(unsigned char** out, size_t* outsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1408 const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1409 const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1410 ucvector v = ucvector_init(*out, *outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1411 unsigned error = lodepng_inflatev(&v, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1412 *out = v.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1413 *outsize = v.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1414 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1415 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1416
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1417 static unsigned inflatev(ucvector* out, const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1418 const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1419 if(settings->custom_inflate) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1420 unsigned error = settings->custom_inflate(&out->data, &out->size, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1421 out->allocsize = out->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1422 if(error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1423 /*the custom inflate is allowed to have its own error codes, however, we translate it to code 110*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1424 error = 110;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1425 /*if there's a max output size, and the custom zlib returned error, then indicate that error instead*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1426 if(settings->max_output_size && out->size > settings->max_output_size) error = 109;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1427 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1428 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1429 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1430 return lodepng_inflatev(out, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1431 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1432 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1433
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1434 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1435
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1436 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1437
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1438 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1439 /* / Deflator (Compressor) / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1440 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1441
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1442 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1443
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1444 /*search the index in the array, that has the largest value smaller than or equal to the given value,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1445 given array must be sorted (if no value is smaller, it returns the size of the given array)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1446 static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1447 /*binary search (only small gain over linear). TODO: use CPU log2 instruction for getting symbols instead*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1448 size_t left = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1449 size_t right = array_size - 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1450
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1451 while(left <= right) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1452 size_t mid = (left + right) >> 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1453 if(array[mid] >= value) right = mid - 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1454 else left = mid + 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1455 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1456 if(left >= array_size || array[left] > value) left--;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1457 return left;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1458 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1459
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1460 static void addLengthDistance(uivector* values, size_t length, size_t distance) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1461 /*values in encoded vector are those used by deflate:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1462 0-255: literal bytes
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1463 256: end
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1464 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1465 286-287: invalid*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1466
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1467 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1468 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1469 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1470 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1471
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1472 size_t pos = values->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1473 /*TODO: return error when this fails (out of memory)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1474 unsigned ok = uivector_resize(values, values->size + 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1475 if(ok) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1476 values->data[pos + 0] = length_code + FIRST_LENGTH_CODE_INDEX;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1477 values->data[pos + 1] = extra_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1478 values->data[pos + 2] = dist_code;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1479 values->data[pos + 3] = extra_distance;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1480 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1481 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1482
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1483 /*3 bytes of data get encoded into two bytes. The hash cannot use more than 3
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1484 bytes as input because 3 is the minimum match length for deflate*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1485 static const unsigned HASH_NUM_VALUES = 65536;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1486 static const unsigned HASH_BIT_MASK = 65535; /*HASH_NUM_VALUES - 1, but C90 does not like that as initializer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1487
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1488 typedef struct Hash {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1489 int* head; /*hash value to head circular pos - can be outdated if went around window*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1490 /*circular pos to prev circular pos*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1491 unsigned short* chain;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1492 int* val; /*circular pos to hash value*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1493
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1494 /*TODO: do this not only for zeros but for any repeated byte. However for PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1495 it's always going to be the zeros that dominate, so not important for PNG*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1496 int* headz; /*similar to head, but for chainz*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1497 unsigned short* chainz; /*those with same amount of zeros*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1498 unsigned short* zeros; /*length of zeros streak, used as a second hash chain*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1499 } Hash;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1500
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1501 static unsigned hash_init(Hash* hash, unsigned windowsize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1502 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1503 hash->head = (int*)lodepng_malloc(sizeof(int) * HASH_NUM_VALUES);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1504 hash->val = (int*)lodepng_malloc(sizeof(int) * windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1505 hash->chain = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1506
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1507 hash->zeros = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1508 hash->headz = (int*)lodepng_malloc(sizeof(int) * (MAX_SUPPORTED_DEFLATE_LENGTH + 1));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1509 hash->chainz = (unsigned short*)lodepng_malloc(sizeof(unsigned short) * windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1510
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1511 if(!hash->head || !hash->chain || !hash->val || !hash->headz|| !hash->chainz || !hash->zeros) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1512 return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1513 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1514
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1515 /*initialize hash table*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1516 for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1517 for(i = 0; i != windowsize; ++i) hash->val[i] = -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1518 for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1519
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1520 for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1521 for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialized*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1522
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1523 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1524 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1525
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1526 static void hash_cleanup(Hash* hash) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1527 lodepng_free(hash->head);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1528 lodepng_free(hash->val);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1529 lodepng_free(hash->chain);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1530
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1531 lodepng_free(hash->zeros);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1532 lodepng_free(hash->headz);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1533 lodepng_free(hash->chainz);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1534 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1535
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1536
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1537
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1538 static unsigned getHash(const unsigned char* data, size_t size, size_t pos) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1539 unsigned result = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1540 if(pos + 2 < size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1541 /*A simple shift and xor hash is used. Since the data of PNGs is dominated
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1542 by zeroes due to the filters, a better hash does not have a significant
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1543 effect on speed in traversing the chain, and causes more time spend on
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1544 calculating the hash.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1545 result ^= ((unsigned)data[pos + 0] << 0u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1546 result ^= ((unsigned)data[pos + 1] << 4u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1547 result ^= ((unsigned)data[pos + 2] << 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1548 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1549 size_t amount, i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1550 if(pos >= size) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1551 amount = size - pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1552 for(i = 0; i != amount; ++i) result ^= ((unsigned)data[pos + i] << (i * 8u));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1553 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1554 return result & HASH_BIT_MASK;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1555 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1556
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1557 static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1558 const unsigned char* start = data + pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1559 const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1560 if(end > data + size) end = data + size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1561 data = start;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1562 while(data != end && *data == 0) ++data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1563 /*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1564 return (unsigned)(data - start);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1565 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1566
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1567 /*wpos = pos & (windowsize - 1)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1568 static void updateHashChain(Hash* hash, size_t wpos, unsigned hashval, unsigned short numzeros) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1569 hash->val[wpos] = (int)hashval;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1570 if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1571 hash->head[hashval] = (int)wpos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1572
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1573 hash->zeros[wpos] = numzeros;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1574 if(hash->headz[numzeros] != -1) hash->chainz[wpos] = hash->headz[numzeros];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1575 hash->headz[numzeros] = (int)wpos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1576 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1577
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1578 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1579 LZ77-encode the data. Return value is error code. The input are raw bytes, the output
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1580 is in the form of unsigned integers with codes representing for example literal bytes, or
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1581 length/distance pairs.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1582 It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1583 sliding window (of windowsize) is used, and all past bytes in that window can be used as
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1584 the "dictionary". A brute force search through all possible distances would be slow, and
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1585 this hash technique is one out of several ways to speed this up.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1586 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1587 static unsigned encodeLZ77(uivector* out, Hash* hash,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1588 const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1589 unsigned minmatch, unsigned nicematch, unsigned lazymatching) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1590 size_t pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1591 unsigned i, error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1592 /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1593 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1594 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1595
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1596 unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1597 unsigned numzeros = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1598
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1599 unsigned offset; /*the offset represents the distance in LZ77 terminology*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1600 unsigned length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1601 unsigned lazy = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1602 unsigned lazylength = 0, lazyoffset = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1603 unsigned hashval;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1604 unsigned current_offset, current_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1605 unsigned prev_offset;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1606 const unsigned char *lastptr, *foreptr, *backptr;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1607 unsigned hashpos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1608
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1609 if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1610 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1611
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1612 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1613
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1614 for(pos = inpos; pos < insize; ++pos) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1615 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1616 unsigned chainlength = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1617
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1618 hashval = getHash(in, insize, pos);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1619
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1620 if(usezeros && hashval == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1621 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1622 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1623 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1624 numzeros = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1625 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1626
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1627 updateHashChain(hash, wpos, hashval, numzeros);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1628
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1629 /*the length and offset found for the current position*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1630 length = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1631 offset = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1632
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1633 hashpos = hash->chain[wpos];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1634
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1635 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1636
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1637 /*search for the longest string*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1638 prev_offset = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1639 for(;;) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1640 if(chainlength++ >= maxchainlength) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1641 current_offset = (unsigned)(hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1642
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1643 if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1644 prev_offset = current_offset;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1645 if(current_offset > 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1646 /*test the next characters*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1647 foreptr = &in[pos];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1648 backptr = &in[pos - current_offset];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1649
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1650 /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1651 if(numzeros >= 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1652 unsigned skip = hash->zeros[hashpos];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1653 if(skip > numzeros) skip = numzeros;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1654 backptr += skip;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1655 foreptr += skip;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1656 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1657
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1658 while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1659 ++backptr;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1660 ++foreptr;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1661 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1662 current_length = (unsigned)(foreptr - &in[pos]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1663
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1664 if(current_length > length) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1665 length = current_length; /*the longest length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1666 offset = current_offset; /*the offset that is related to this longest length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1667 /*jump out once a length of max length is found (speed gain). This also jumps
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1668 out if length is MAX_SUPPORTED_DEFLATE_LENGTH*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1669 if(current_length >= nicematch) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1670 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1671 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1672
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1673 if(hashpos == hash->chain[hashpos]) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1674
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1675 if(numzeros >= 3 && length > numzeros) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1676 hashpos = hash->chainz[hashpos];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1677 if(hash->zeros[hashpos] != numzeros) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1678 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1679 hashpos = hash->chain[hashpos];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1680 /*outdated hash value, happens if particular value was not encountered in whole last window*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1681 if(hash->val[hashpos] != (int)hashval) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1682 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1683 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1684
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1685 if(lazymatching) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1686 if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1687 lazy = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1688 lazylength = length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1689 lazyoffset = offset;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1690 continue; /*try the next byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1691 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1692 if(lazy) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1693 lazy = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1694 if(pos == 0) ERROR_BREAK(81);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1695 if(length > lazylength + 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1696 /*push the previous character as literal*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1697 if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1698 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1699 length = lazylength;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1700 offset = lazyoffset;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1701 hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1702 hash->headz[numzeros] = -1; /*idem*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1703 --pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1704 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1705 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1706 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1707 if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1708
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1709 /*encode it as length/distance pair or literal value*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1710 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1711 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1712 } else if(length < minmatch || (length == 3 && offset > 4096)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1713 /*compensate for the fact that longer offsets have more extra bits, a
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1714 length of only 3 may be not worth it then*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1715 if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1716 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1717 addLengthDistance(out, length, offset);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1718 for(i = 1; i < length; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1719 ++pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1720 wpos = pos & (windowsize - 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1721 hashval = getHash(in, insize, pos);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1722 if(usezeros && hashval == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1723 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1724 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1725 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1726 numzeros = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1727 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1728 updateHashChain(hash, wpos, hashval, numzeros);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1729 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1730 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1731 } /*end of the loop through each character of input*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1732
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1733 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1734 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1735
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1736 /* /////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1737
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1738 static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1739 /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1740 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1741
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1742 size_t i, numdeflateblocks = (datasize + 65534u) / 65535u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1743 unsigned datapos = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1744 for(i = 0; i != numdeflateblocks; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1745 unsigned BFINAL, BTYPE, LEN, NLEN;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1746 unsigned char firstbyte;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1747 size_t pos = out->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1748
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1749 BFINAL = (i == numdeflateblocks - 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1750 BTYPE = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1751
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1752 LEN = 65535;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1753 if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1754 NLEN = 65535 - LEN;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1755
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1756 if(!ucvector_resize(out, out->size + LEN + 5)) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1757
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1758 firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1u) << 1u) + ((BTYPE & 2u) << 1u));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1759 out->data[pos + 0] = firstbyte;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1760 out->data[pos + 1] = (unsigned char)(LEN & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1761 out->data[pos + 2] = (unsigned char)(LEN >> 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1762 out->data[pos + 3] = (unsigned char)(NLEN & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1763 out->data[pos + 4] = (unsigned char)(NLEN >> 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1764 lodepng_memcpy(out->data + pos + 5, data + datapos, LEN);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1765 datapos += LEN;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1766 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1767
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1768 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1769 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1770
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1771 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1772 write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman trees.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1773 tree_ll: the tree for lit and len codes.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1774 tree_d: the tree for distance codes.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1775 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1776 static void writeLZ77data(LodePNGBitWriter* writer, const uivector* lz77_encoded,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1777 const HuffmanTree* tree_ll, const HuffmanTree* tree_d) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1778 size_t i = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1779 for(i = 0; i != lz77_encoded->size; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1780 unsigned val = lz77_encoded->data[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1781 writeBitsReversed(writer, tree_ll->codes[val], tree_ll->lengths[val]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1782 if(val > 256) /*for a length code, 3 more things have to be added*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1783 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1784 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1785 unsigned length_extra_bits = lz77_encoded->data[++i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1786
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1787 unsigned distance_code = lz77_encoded->data[++i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1788
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1789 unsigned distance_index = distance_code;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1790 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1791 unsigned distance_extra_bits = lz77_encoded->data[++i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1792
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1793 writeBits(writer, length_extra_bits, n_length_extra_bits);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1794 writeBitsReversed(writer, tree_d->codes[distance_code], tree_d->lengths[distance_code]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1795 writeBits(writer, distance_extra_bits, n_distance_extra_bits);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1796 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1797 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1798 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1799
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1800 /*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1801 static unsigned deflateDynamic(LodePNGBitWriter* writer, Hash* hash,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1802 const unsigned char* data, size_t datapos, size_t dataend,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1803 const LodePNGCompressSettings* settings, unsigned final) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1804 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1805
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1806 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1807 A block is compressed as follows: The PNG data is lz77 encoded, resulting in
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1808 literal bytes and length/distance pairs. This is then huffman compressed with
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1809 two huffman trees. One huffman tree is used for the lit and len values ("ll"),
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1810 another huffman tree is used for the dist values ("d"). These two trees are
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1811 stored using their code lengths, and to compress even more these code lengths
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1812 are also run-length encoded and huffman compressed. This gives a huffman tree
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1813 of code lengths "cl". The code lengths used to describe this third tree are
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1814 the code length code lengths ("clcl").
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1815 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1816
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1817 /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1818 uivector lz77_encoded;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1819 HuffmanTree tree_ll; /*tree for lit,len values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1820 HuffmanTree tree_d; /*tree for distance codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1821 HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1822 unsigned* frequencies_ll = 0; /*frequency of lit,len codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1823 unsigned* frequencies_d = 0; /*frequency of dist codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1824 unsigned* frequencies_cl = 0; /*frequency of code length codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1825 unsigned* bitlen_lld = 0; /*lit,len,dist code lengths (int bits), literally (without repeat codes).*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1826 unsigned* bitlen_lld_e = 0; /*bitlen_lld encoded with repeat codes (this is a rudimentary run length compression)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1827 size_t datasize = dataend - datapos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1828
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1829 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1830 If we could call "bitlen_cl" the the code length code lengths ("clcl"), that is the bit lengths of codes to represent
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1831 tree_cl in CLCL_ORDER, then due to the huffman compression of huffman tree representations ("two levels"), there are
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1832 some analogies:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1833 bitlen_lld is to tree_cl what data is to tree_ll and tree_d.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1834 bitlen_lld_e is to bitlen_lld what lz77_encoded is to data.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1835 bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1836 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1837
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1838 unsigned BFINAL = final;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1839 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1840 size_t numcodes_ll, numcodes_d, numcodes_lld, numcodes_lld_e, numcodes_cl;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1841 unsigned HLIT, HDIST, HCLEN;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1842
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1843 uivector_init(&lz77_encoded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1844 HuffmanTree_init(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1845 HuffmanTree_init(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1846 HuffmanTree_init(&tree_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1847 /* could fit on stack, but >1KB is on the larger side so allocate instead */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1848 frequencies_ll = (unsigned*)lodepng_malloc(286 * sizeof(*frequencies_ll));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1849 frequencies_d = (unsigned*)lodepng_malloc(30 * sizeof(*frequencies_d));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1850 frequencies_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(*frequencies_cl));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1851
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1852 if(!frequencies_ll || !frequencies_d || !frequencies_cl) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1853
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1854 /*This while loop never loops due to a break at the end, it is here to
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1855 allow breaking out of it to the cleanup phase on error conditions.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1856 while(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1857 lodepng_memset(frequencies_ll, 0, 286 * sizeof(*frequencies_ll));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1858 lodepng_memset(frequencies_d, 0, 30 * sizeof(*frequencies_d));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1859 lodepng_memset(frequencies_cl, 0, NUM_CODE_LENGTH_CODES * sizeof(*frequencies_cl));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1860
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1861 if(settings->use_lz77) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1862 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1863 settings->minmatch, settings->nicematch, settings->lazymatching);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1864 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1865 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1866 if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1867 for(i = datapos; i < dataend; ++i) lz77_encoded.data[i - datapos] = data[i]; /*no LZ77, but still will be Huffman compressed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1868 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1869
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1870 /*Count the frequencies of lit, len and dist codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1871 for(i = 0; i != lz77_encoded.size; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1872 unsigned symbol = lz77_encoded.data[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1873 ++frequencies_ll[symbol];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1874 if(symbol > 256) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1875 unsigned dist = lz77_encoded.data[i + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1876 ++frequencies_d[dist];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1877 i += 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1878 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1879 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1880 frequencies_ll[256] = 1; /*there will be exactly 1 end code, at the end of the block*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1881
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1882 /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1883 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll, 257, 286, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1884 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1885 /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1886 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d, 2, 30, 15);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1887 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1888
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1889 numcodes_ll = LODEPNG_MIN(tree_ll.numcodes, 286);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1890 numcodes_d = LODEPNG_MIN(tree_d.numcodes, 30);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1891 /*store the code lengths of both generated trees in bitlen_lld*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1892 numcodes_lld = numcodes_ll + numcodes_d;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1893 bitlen_lld = (unsigned*)lodepng_malloc(numcodes_lld * sizeof(*bitlen_lld));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1894 /*numcodes_lld_e never needs more size than bitlen_lld*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1895 bitlen_lld_e = (unsigned*)lodepng_malloc(numcodes_lld * sizeof(*bitlen_lld_e));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1896 if(!bitlen_lld || !bitlen_lld_e) ERROR_BREAK(83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1897 numcodes_lld_e = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1898
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1899 for(i = 0; i != numcodes_ll; ++i) bitlen_lld[i] = tree_ll.lengths[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1900 for(i = 0; i != numcodes_d; ++i) bitlen_lld[numcodes_ll + i] = tree_d.lengths[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1901
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1902 /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times),
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1903 17 (3-10 zeroes), 18 (11-138 zeroes)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1904 for(i = 0; i != numcodes_lld; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1905 unsigned j = 0; /*amount of repetitions*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1906 while(i + j + 1 < numcodes_lld && bitlen_lld[i + j + 1] == bitlen_lld[i]) ++j;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1907
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1908 if(bitlen_lld[i] == 0 && j >= 2) /*repeat code for zeroes*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1909 ++j; /*include the first zero*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1910 if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1911 bitlen_lld_e[numcodes_lld_e++] = 17;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1912 bitlen_lld_e[numcodes_lld_e++] = j - 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1913 } else /*repeat code 18 supports max 138 zeroes*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1914 if(j > 138) j = 138;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1915 bitlen_lld_e[numcodes_lld_e++] = 18;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1916 bitlen_lld_e[numcodes_lld_e++] = j - 11;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1917 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1918 i += (j - 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1919 } else if(j >= 3) /*repeat code for value other than zero*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1920 size_t k;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1921 unsigned num = j / 6u, rest = j % 6u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1922 bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1923 for(k = 0; k < num; ++k) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1924 bitlen_lld_e[numcodes_lld_e++] = 16;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1925 bitlen_lld_e[numcodes_lld_e++] = 6 - 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1926 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1927 if(rest >= 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1928 bitlen_lld_e[numcodes_lld_e++] = 16;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1929 bitlen_lld_e[numcodes_lld_e++] = rest - 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1930 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1931 else j -= rest;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1932 i += j;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1933 } else /*too short to benefit from repeat code*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1934 bitlen_lld_e[numcodes_lld_e++] = bitlen_lld[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1935 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1936 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1937
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1938 /*generate tree_cl, the huffmantree of huffmantrees*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1939 for(i = 0; i != numcodes_lld_e; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1940 ++frequencies_cl[bitlen_lld_e[i]];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1941 /*after a repeat code come the bits that specify the number of repetitions,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1942 those don't need to be in the frequencies_cl calculation*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1943 if(bitlen_lld_e[i] >= 16) ++i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1944 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1945
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1946 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1947 NUM_CODE_LENGTH_CODES, NUM_CODE_LENGTH_CODES, 7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1948 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1949
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1950 /*compute amount of code-length-code-lengths to output*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1951 numcodes_cl = NUM_CODE_LENGTH_CODES;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1952 /*trim zeros at the end (using CLCL_ORDER), but minimum size must be 4 (see HCLEN below)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1953 while(numcodes_cl > 4u && tree_cl.lengths[CLCL_ORDER[numcodes_cl - 1u]] == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1954 numcodes_cl--;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1955 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1956
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1957 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1958 Write everything into the output
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1959
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1960 After the BFINAL and BTYPE, the dynamic block consists out of the following:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1961 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1962 - (HCLEN+4)*3 bits code lengths of code length alphabet
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1963 - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1964 alphabet, + possible repetition codes 16, 17, 18)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1965 - HDIST + 1 code lengths of distance alphabet (encoded using the code length
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1966 alphabet, + possible repetition codes 16, 17, 18)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1967 - compressed data
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1968 - 256 (end code)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1969 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1970
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1971 /*Write block type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1972 writeBits(writer, BFINAL, 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1973 writeBits(writer, 0, 1); /*first bit of BTYPE "dynamic"*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1974 writeBits(writer, 1, 1); /*second bit of BTYPE "dynamic"*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1975
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1976 /*write the HLIT, HDIST and HCLEN values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1977 /*all three sizes take trimmed ending zeroes into account, done either by HuffmanTree_makeFromFrequencies
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1978 or in the loop for numcodes_cl above, which saves space. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1979 HLIT = (unsigned)(numcodes_ll - 257);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1980 HDIST = (unsigned)(numcodes_d - 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1981 HCLEN = (unsigned)(numcodes_cl - 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1982 writeBits(writer, HLIT, 5);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1983 writeBits(writer, HDIST, 5);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1984 writeBits(writer, HCLEN, 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1985
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1986 /*write the code lengths of the code length alphabet ("bitlen_cl")*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1987 for(i = 0; i != numcodes_cl; ++i) writeBits(writer, tree_cl.lengths[CLCL_ORDER[i]], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1988
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1989 /*write the lengths of the lit/len AND the dist alphabet*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1990 for(i = 0; i != numcodes_lld_e; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1991 writeBitsReversed(writer, tree_cl.codes[bitlen_lld_e[i]], tree_cl.lengths[bitlen_lld_e[i]]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1992 /*extra bits of repeat codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1993 if(bitlen_lld_e[i] == 16) writeBits(writer, bitlen_lld_e[++i], 2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1994 else if(bitlen_lld_e[i] == 17) writeBits(writer, bitlen_lld_e[++i], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1995 else if(bitlen_lld_e[i] == 18) writeBits(writer, bitlen_lld_e[++i], 7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1996 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1997
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1998 /*write the compressed data symbols*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
1999 writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2000 /*error: the length of the end code 256 must be larger than 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2001 if(tree_ll.lengths[256] == 0) ERROR_BREAK(64);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2002
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2003 /*write the end code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2004 writeBitsReversed(writer, tree_ll.codes[256], tree_ll.lengths[256]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2005
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2006 break; /*end of error-while*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2007 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2008
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2009 /*cleanup*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2010 uivector_cleanup(&lz77_encoded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2011 HuffmanTree_cleanup(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2012 HuffmanTree_cleanup(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2013 HuffmanTree_cleanup(&tree_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2014 lodepng_free(frequencies_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2015 lodepng_free(frequencies_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2016 lodepng_free(frequencies_cl);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2017 lodepng_free(bitlen_lld);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2018 lodepng_free(bitlen_lld_e);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2019
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2020 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2021 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2022
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2023 static unsigned deflateFixed(LodePNGBitWriter* writer, Hash* hash,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2024 const unsigned char* data,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2025 size_t datapos, size_t dataend,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2026 const LodePNGCompressSettings* settings, unsigned final) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2027 HuffmanTree tree_ll; /*tree for literal values and length codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2028 HuffmanTree tree_d; /*tree for distance codes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2029
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2030 unsigned BFINAL = final;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2031 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2032 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2033
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2034 HuffmanTree_init(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2035 HuffmanTree_init(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2036
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2037 error = generateFixedLitLenTree(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2038 if(!error) error = generateFixedDistanceTree(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2039
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2040 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2041 writeBits(writer, BFINAL, 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2042 writeBits(writer, 1, 1); /*first bit of BTYPE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2043 writeBits(writer, 0, 1); /*second bit of BTYPE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2044
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2045 if(settings->use_lz77) /*LZ77 encoded*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2046 uivector lz77_encoded;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2047 uivector_init(&lz77_encoded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2048 error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2049 settings->minmatch, settings->nicematch, settings->lazymatching);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2050 if(!error) writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2051 uivector_cleanup(&lz77_encoded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2052 } else /*no LZ77, but still will be Huffman compressed*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2053 for(i = datapos; i < dataend; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2054 writeBitsReversed(writer, tree_ll.codes[data[i]], tree_ll.lengths[data[i]]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2055 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2056 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2057 /*add END code*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2058 if(!error) writeBitsReversed(writer,tree_ll.codes[256], tree_ll.lengths[256]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2059 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2060
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2061 /*cleanup*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2062 HuffmanTree_cleanup(&tree_ll);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2063 HuffmanTree_cleanup(&tree_d);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2064
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2065 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2066 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2067
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2068 static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2069 const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2070 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2071 size_t i, blocksize, numdeflateblocks;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2072 Hash hash;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2073 LodePNGBitWriter writer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2074
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2075 LodePNGBitWriter_init(&writer, out);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2076
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2077 if(settings->btype > 2) return 61;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2078 else if(settings->btype == 0) return deflateNoCompression(out, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2079 else if(settings->btype == 1) blocksize = insize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2080 else /*if(settings->btype == 2)*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2081 /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2082 blocksize = insize / 8u + 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2083 if(blocksize < 65536) blocksize = 65536;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2084 if(blocksize > 262144) blocksize = 262144;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2085 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2086
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2087 numdeflateblocks = (insize + blocksize - 1) / blocksize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2088 if(numdeflateblocks == 0) numdeflateblocks = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2089
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2090 error = hash_init(&hash, settings->windowsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2091
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2092 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2093 for(i = 0; i != numdeflateblocks && !error; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2094 unsigned final = (i == numdeflateblocks - 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2095 size_t start = i * blocksize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2096 size_t end = start + blocksize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2097 if(end > insize) end = insize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2098
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2099 if(settings->btype == 1) error = deflateFixed(&writer, &hash, in, start, end, settings, final);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2100 else if(settings->btype == 2) error = deflateDynamic(&writer, &hash, in, start, end, settings, final);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2101 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2102 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2103
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2104 hash_cleanup(&hash);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2105
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2106 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2107 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2108
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2109 unsigned lodepng_deflate(unsigned char** out, size_t* outsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2110 const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2111 const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2112 ucvector v = ucvector_init(*out, *outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2113 unsigned error = lodepng_deflatev(&v, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2114 *out = v.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2115 *outsize = v.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2116 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2117 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2118
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2119 static unsigned deflate(unsigned char** out, size_t* outsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2120 const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2121 const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2122 if(settings->custom_deflate) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2123 unsigned error = settings->custom_deflate(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2124 /*the custom deflate is allowed to have its own error codes, however, we translate it to code 111*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2125 return error ? 111 : 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2126 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2127 return lodepng_deflate(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2128 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2129 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2130
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2131 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2132
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2133 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2134 /* / Adler32 / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2135 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2136
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2137 static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2138 unsigned s1 = adler & 0xffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2139 unsigned s2 = (adler >> 16u) & 0xffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2140
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2141 while(len != 0u) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2142 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2143 /*at least 5552 sums can be done before the sums overflow, saving a lot of module divisions*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2144 unsigned amount = len > 5552u ? 5552u : len;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2145 len -= amount;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2146 for(i = 0; i != amount; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2147 s1 += (*data++);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2148 s2 += s1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2149 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2150 s1 %= 65521u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2151 s2 %= 65521u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2152 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2153
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2154 return (s2 << 16u) | s1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2155 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2156
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2157 /*Return the adler32 of the bytes data[0..len-1]*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2158 static unsigned adler32(const unsigned char* data, unsigned len) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2159 return update_adler32(1u, data, len);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2160 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2161
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2162 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2163 /* / Zlib / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2164 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2165
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2166 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2167
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2168 static unsigned lodepng_zlib_decompressv(ucvector* out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2169 const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2170 const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2171 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2172 unsigned CM, CINFO, FDICT;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2173
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2174 if(insize < 2) return 53; /*error, size of zlib data too small*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2175 /*read information from zlib header*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2176 if((in[0] * 256 + in[1]) % 31 != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2177 /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2178 return 24;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2179 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2180
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2181 CM = in[0] & 15;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2182 CINFO = (in[0] >> 4) & 15;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2183 /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2184 FDICT = (in[1] >> 5) & 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2185 /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2186
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2187 if(CM != 8 || CINFO > 7) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2188 /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2189 return 25;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2190 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2191 if(FDICT != 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2192 /*error: the specification of PNG says about the zlib stream:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2193 "The additional flags shall not specify a preset dictionary."*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2194 return 26;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2195 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2196
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2197 error = inflatev(out, in + 2, insize - 2, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2198 if(error) return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2199
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2200 if(!settings->ignore_adler32) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2201 unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2202 unsigned checksum = adler32(out->data, (unsigned)(out->size));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2203 if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2204 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2205
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2206 return 0; /*no error*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2207 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2208
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2209
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2210 unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2211 size_t insize, const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2212 ucvector v = ucvector_init(*out, *outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2213 unsigned error = lodepng_zlib_decompressv(&v, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2214 *out = v.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2215 *outsize = v.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2216 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2217 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2218
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2219 /*expected_size is expected output size, to avoid intermediate allocations. Set to 0 if not known. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2220 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2221 const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2222 unsigned error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2223 if(settings->custom_zlib) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2224 error = settings->custom_zlib(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2225 if(error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2226 /*the custom zlib is allowed to have its own error codes, however, we translate it to code 110*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2227 error = 110;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2228 /*if there's a max output size, and the custom zlib returned error, then indicate that error instead*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2229 if(settings->max_output_size && *outsize > settings->max_output_size) error = 109;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2230 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2231 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2232 ucvector v = ucvector_init(*out, *outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2233 if(expected_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2234 /*reserve the memory to avoid intermediate reallocations*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2235 ucvector_resize(&v, *outsize + expected_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2236 v.size = *outsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2237 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2238 error = lodepng_zlib_decompressv(&v, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2239 *out = v.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2240 *outsize = v.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2241 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2242 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2243 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2244
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2245 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2246
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2247 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2248
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2249 unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2250 size_t insize, const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2251 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2252 unsigned error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2253 unsigned char* deflatedata = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2254 size_t deflatesize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2255
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2256 error = deflate(&deflatedata, &deflatesize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2257
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2258 *out = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2259 *outsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2260 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2261 *outsize = deflatesize + 6;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2262 *out = (unsigned char*)lodepng_malloc(*outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2263 if(!*out) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2264 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2265
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2266 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2267 unsigned ADLER32 = adler32(in, (unsigned)insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2268 /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2269 unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2270 unsigned FLEVEL = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2271 unsigned FDICT = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2272 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2273 unsigned FCHECK = 31 - CMFFLG % 31;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2274 CMFFLG += FCHECK;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2275
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2276 (*out)[0] = (unsigned char)(CMFFLG >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2277 (*out)[1] = (unsigned char)(CMFFLG & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2278 for(i = 0; i != deflatesize; ++i) (*out)[i + 2] = deflatedata[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2279 lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2280 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2281
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2282 lodepng_free(deflatedata);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2283 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2284 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2285
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2286 /* compress using the default or custom zlib function */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2287 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2288 size_t insize, const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2289 if(settings->custom_zlib) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2290 unsigned error = settings->custom_zlib(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2291 /*the custom zlib is allowed to have its own error codes, however, we translate it to code 111*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2292 return error ? 111 : 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2293 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2294 return lodepng_zlib_compress(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2295 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2296 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2297
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2298 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2299
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2300 #else /*no LODEPNG_COMPILE_ZLIB*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2301
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2302 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2303 static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2304 const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2305 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2306 (void)expected_size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2307 return settings->custom_zlib(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2308 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2309 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2310 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2311 static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2312 size_t insize, const LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2313 if(!settings->custom_zlib) return 87; /*no custom zlib function provided */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2314 return settings->custom_zlib(out, outsize, in, insize, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2315 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2316 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2317
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2318 #endif /*LODEPNG_COMPILE_ZLIB*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2319
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2320 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2321
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2322 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2323
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2324 /*this is a good tradeoff between speed and compression ratio*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2325 #define DEFAULT_WINDOWSIZE 2048
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2326
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2327 void lodepng_compress_settings_init(LodePNGCompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2328 /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2329 settings->btype = 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2330 settings->use_lz77 = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2331 settings->windowsize = DEFAULT_WINDOWSIZE;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2332 settings->minmatch = 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2333 settings->nicematch = 128;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2334 settings->lazymatching = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2335
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2336 settings->custom_zlib = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2337 settings->custom_deflate = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2338 settings->custom_context = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2339 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2340
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2341 const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT_WINDOWSIZE, 3, 128, 1, 0, 0, 0};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2342
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2343
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2344 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2345
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2346 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2347
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2348 void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2349 settings->ignore_adler32 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2350 settings->ignore_nlen = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2351 settings->max_output_size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2352
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2353 settings->custom_zlib = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2354 settings->custom_inflate = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2355 settings->custom_context = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2356 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2357
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2358 const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0, 0, 0};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2359
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2360 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2361
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2362 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2363 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2364 /* // End of Zlib related code. Begin of PNG related code. // */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2365 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2366 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2367
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2368 #ifdef LODEPNG_COMPILE_PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2369
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2370 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2371 /* / CRC32 / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2372 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2373
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2374
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2375 #ifdef LODEPNG_COMPILE_CRC
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2376
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2377 static const unsigned lodepng_crc32_table0[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2378 0x00000000u, 0x77073096u, 0xee0e612cu, 0x990951bau, 0x076dc419u, 0x706af48fu, 0xe963a535u, 0x9e6495a3u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2379 0x0edb8832u, 0x79dcb8a4u, 0xe0d5e91eu, 0x97d2d988u, 0x09b64c2bu, 0x7eb17cbdu, 0xe7b82d07u, 0x90bf1d91u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2380 0x1db71064u, 0x6ab020f2u, 0xf3b97148u, 0x84be41deu, 0x1adad47du, 0x6ddde4ebu, 0xf4d4b551u, 0x83d385c7u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2381 0x136c9856u, 0x646ba8c0u, 0xfd62f97au, 0x8a65c9ecu, 0x14015c4fu, 0x63066cd9u, 0xfa0f3d63u, 0x8d080df5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2382 0x3b6e20c8u, 0x4c69105eu, 0xd56041e4u, 0xa2677172u, 0x3c03e4d1u, 0x4b04d447u, 0xd20d85fdu, 0xa50ab56bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2383 0x35b5a8fau, 0x42b2986cu, 0xdbbbc9d6u, 0xacbcf940u, 0x32d86ce3u, 0x45df5c75u, 0xdcd60dcfu, 0xabd13d59u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2384 0x26d930acu, 0x51de003au, 0xc8d75180u, 0xbfd06116u, 0x21b4f4b5u, 0x56b3c423u, 0xcfba9599u, 0xb8bda50fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2385 0x2802b89eu, 0x5f058808u, 0xc60cd9b2u, 0xb10be924u, 0x2f6f7c87u, 0x58684c11u, 0xc1611dabu, 0xb6662d3du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2386 0x76dc4190u, 0x01db7106u, 0x98d220bcu, 0xefd5102au, 0x71b18589u, 0x06b6b51fu, 0x9fbfe4a5u, 0xe8b8d433u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2387 0x7807c9a2u, 0x0f00f934u, 0x9609a88eu, 0xe10e9818u, 0x7f6a0dbbu, 0x086d3d2du, 0x91646c97u, 0xe6635c01u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2388 0x6b6b51f4u, 0x1c6c6162u, 0x856530d8u, 0xf262004eu, 0x6c0695edu, 0x1b01a57bu, 0x8208f4c1u, 0xf50fc457u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2389 0x65b0d9c6u, 0x12b7e950u, 0x8bbeb8eau, 0xfcb9887cu, 0x62dd1ddfu, 0x15da2d49u, 0x8cd37cf3u, 0xfbd44c65u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2390 0x4db26158u, 0x3ab551ceu, 0xa3bc0074u, 0xd4bb30e2u, 0x4adfa541u, 0x3dd895d7u, 0xa4d1c46du, 0xd3d6f4fbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2391 0x4369e96au, 0x346ed9fcu, 0xad678846u, 0xda60b8d0u, 0x44042d73u, 0x33031de5u, 0xaa0a4c5fu, 0xdd0d7cc9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2392 0x5005713cu, 0x270241aau, 0xbe0b1010u, 0xc90c2086u, 0x5768b525u, 0x206f85b3u, 0xb966d409u, 0xce61e49fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2393 0x5edef90eu, 0x29d9c998u, 0xb0d09822u, 0xc7d7a8b4u, 0x59b33d17u, 0x2eb40d81u, 0xb7bd5c3bu, 0xc0ba6cadu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2394 0xedb88320u, 0x9abfb3b6u, 0x03b6e20cu, 0x74b1d29au, 0xead54739u, 0x9dd277afu, 0x04db2615u, 0x73dc1683u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2395 0xe3630b12u, 0x94643b84u, 0x0d6d6a3eu, 0x7a6a5aa8u, 0xe40ecf0bu, 0x9309ff9du, 0x0a00ae27u, 0x7d079eb1u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2396 0xf00f9344u, 0x8708a3d2u, 0x1e01f268u, 0x6906c2feu, 0xf762575du, 0x806567cbu, 0x196c3671u, 0x6e6b06e7u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2397 0xfed41b76u, 0x89d32be0u, 0x10da7a5au, 0x67dd4accu, 0xf9b9df6fu, 0x8ebeeff9u, 0x17b7be43u, 0x60b08ed5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2398 0xd6d6a3e8u, 0xa1d1937eu, 0x38d8c2c4u, 0x4fdff252u, 0xd1bb67f1u, 0xa6bc5767u, 0x3fb506ddu, 0x48b2364bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2399 0xd80d2bdau, 0xaf0a1b4cu, 0x36034af6u, 0x41047a60u, 0xdf60efc3u, 0xa867df55u, 0x316e8eefu, 0x4669be79u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2400 0xcb61b38cu, 0xbc66831au, 0x256fd2a0u, 0x5268e236u, 0xcc0c7795u, 0xbb0b4703u, 0x220216b9u, 0x5505262fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2401 0xc5ba3bbeu, 0xb2bd0b28u, 0x2bb45a92u, 0x5cb36a04u, 0xc2d7ffa7u, 0xb5d0cf31u, 0x2cd99e8bu, 0x5bdeae1du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2402 0x9b64c2b0u, 0xec63f226u, 0x756aa39cu, 0x026d930au, 0x9c0906a9u, 0xeb0e363fu, 0x72076785u, 0x05005713u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2403 0x95bf4a82u, 0xe2b87a14u, 0x7bb12baeu, 0x0cb61b38u, 0x92d28e9bu, 0xe5d5be0du, 0x7cdcefb7u, 0x0bdbdf21u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2404 0x86d3d2d4u, 0xf1d4e242u, 0x68ddb3f8u, 0x1fda836eu, 0x81be16cdu, 0xf6b9265bu, 0x6fb077e1u, 0x18b74777u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2405 0x88085ae6u, 0xff0f6a70u, 0x66063bcau, 0x11010b5cu, 0x8f659effu, 0xf862ae69u, 0x616bffd3u, 0x166ccf45u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2406 0xa00ae278u, 0xd70dd2eeu, 0x4e048354u, 0x3903b3c2u, 0xa7672661u, 0xd06016f7u, 0x4969474du, 0x3e6e77dbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2407 0xaed16a4au, 0xd9d65adcu, 0x40df0b66u, 0x37d83bf0u, 0xa9bcae53u, 0xdebb9ec5u, 0x47b2cf7fu, 0x30b5ffe9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2408 0xbdbdf21cu, 0xcabac28au, 0x53b39330u, 0x24b4a3a6u, 0xbad03605u, 0xcdd70693u, 0x54de5729u, 0x23d967bfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2409 0xb3667a2eu, 0xc4614ab8u, 0x5d681b02u, 0x2a6f2b94u, 0xb40bbe37u, 0xc30c8ea1u, 0x5a05df1bu, 0x2d02ef8du
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2410 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2411
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2412 static const unsigned lodepng_crc32_table1[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2413 0x00000000u, 0x191b3141u, 0x32366282u, 0x2b2d53c3u, 0x646cc504u, 0x7d77f445u, 0x565aa786u, 0x4f4196c7u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2414 0xc8d98a08u, 0xd1c2bb49u, 0xfaefe88au, 0xe3f4d9cbu, 0xacb54f0cu, 0xb5ae7e4du, 0x9e832d8eu, 0x87981ccfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2415 0x4ac21251u, 0x53d92310u, 0x78f470d3u, 0x61ef4192u, 0x2eaed755u, 0x37b5e614u, 0x1c98b5d7u, 0x05838496u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2416 0x821b9859u, 0x9b00a918u, 0xb02dfadbu, 0xa936cb9au, 0xe6775d5du, 0xff6c6c1cu, 0xd4413fdfu, 0xcd5a0e9eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2417 0x958424a2u, 0x8c9f15e3u, 0xa7b24620u, 0xbea97761u, 0xf1e8e1a6u, 0xe8f3d0e7u, 0xc3de8324u, 0xdac5b265u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2418 0x5d5daeaau, 0x44469febu, 0x6f6bcc28u, 0x7670fd69u, 0x39316baeu, 0x202a5aefu, 0x0b07092cu, 0x121c386du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2419 0xdf4636f3u, 0xc65d07b2u, 0xed705471u, 0xf46b6530u, 0xbb2af3f7u, 0xa231c2b6u, 0x891c9175u, 0x9007a034u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2420 0x179fbcfbu, 0x0e848dbau, 0x25a9de79u, 0x3cb2ef38u, 0x73f379ffu, 0x6ae848beu, 0x41c51b7du, 0x58de2a3cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2421 0xf0794f05u, 0xe9627e44u, 0xc24f2d87u, 0xdb541cc6u, 0x94158a01u, 0x8d0ebb40u, 0xa623e883u, 0xbf38d9c2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2422 0x38a0c50du, 0x21bbf44cu, 0x0a96a78fu, 0x138d96ceu, 0x5ccc0009u, 0x45d73148u, 0x6efa628bu, 0x77e153cau,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2423 0xbabb5d54u, 0xa3a06c15u, 0x888d3fd6u, 0x91960e97u, 0xded79850u, 0xc7cca911u, 0xece1fad2u, 0xf5facb93u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2424 0x7262d75cu, 0x6b79e61du, 0x4054b5deu, 0x594f849fu, 0x160e1258u, 0x0f152319u, 0x243870dau, 0x3d23419bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2425 0x65fd6ba7u, 0x7ce65ae6u, 0x57cb0925u, 0x4ed03864u, 0x0191aea3u, 0x188a9fe2u, 0x33a7cc21u, 0x2abcfd60u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2426 0xad24e1afu, 0xb43fd0eeu, 0x9f12832du, 0x8609b26cu, 0xc94824abu, 0xd05315eau, 0xfb7e4629u, 0xe2657768u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2427 0x2f3f79f6u, 0x362448b7u, 0x1d091b74u, 0x04122a35u, 0x4b53bcf2u, 0x52488db3u, 0x7965de70u, 0x607eef31u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2428 0xe7e6f3feu, 0xfefdc2bfu, 0xd5d0917cu, 0xcccba03du, 0x838a36fau, 0x9a9107bbu, 0xb1bc5478u, 0xa8a76539u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2429 0x3b83984bu, 0x2298a90au, 0x09b5fac9u, 0x10aecb88u, 0x5fef5d4fu, 0x46f46c0eu, 0x6dd93fcdu, 0x74c20e8cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2430 0xf35a1243u, 0xea412302u, 0xc16c70c1u, 0xd8774180u, 0x9736d747u, 0x8e2de606u, 0xa500b5c5u, 0xbc1b8484u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2431 0x71418a1au, 0x685abb5bu, 0x4377e898u, 0x5a6cd9d9u, 0x152d4f1eu, 0x0c367e5fu, 0x271b2d9cu, 0x3e001cddu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2432 0xb9980012u, 0xa0833153u, 0x8bae6290u, 0x92b553d1u, 0xddf4c516u, 0xc4eff457u, 0xefc2a794u, 0xf6d996d5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2433 0xae07bce9u, 0xb71c8da8u, 0x9c31de6bu, 0x852aef2au, 0xca6b79edu, 0xd37048acu, 0xf85d1b6fu, 0xe1462a2eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2434 0x66de36e1u, 0x7fc507a0u, 0x54e85463u, 0x4df36522u, 0x02b2f3e5u, 0x1ba9c2a4u, 0x30849167u, 0x299fa026u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2435 0xe4c5aeb8u, 0xfdde9ff9u, 0xd6f3cc3au, 0xcfe8fd7bu, 0x80a96bbcu, 0x99b25afdu, 0xb29f093eu, 0xab84387fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2436 0x2c1c24b0u, 0x350715f1u, 0x1e2a4632u, 0x07317773u, 0x4870e1b4u, 0x516bd0f5u, 0x7a468336u, 0x635db277u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2437 0xcbfad74eu, 0xd2e1e60fu, 0xf9ccb5ccu, 0xe0d7848du, 0xaf96124au, 0xb68d230bu, 0x9da070c8u, 0x84bb4189u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2438 0x03235d46u, 0x1a386c07u, 0x31153fc4u, 0x280e0e85u, 0x674f9842u, 0x7e54a903u, 0x5579fac0u, 0x4c62cb81u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2439 0x8138c51fu, 0x9823f45eu, 0xb30ea79du, 0xaa1596dcu, 0xe554001bu, 0xfc4f315au, 0xd7626299u, 0xce7953d8u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2440 0x49e14f17u, 0x50fa7e56u, 0x7bd72d95u, 0x62cc1cd4u, 0x2d8d8a13u, 0x3496bb52u, 0x1fbbe891u, 0x06a0d9d0u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2441 0x5e7ef3ecu, 0x4765c2adu, 0x6c48916eu, 0x7553a02fu, 0x3a1236e8u, 0x230907a9u, 0x0824546au, 0x113f652bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2442 0x96a779e4u, 0x8fbc48a5u, 0xa4911b66u, 0xbd8a2a27u, 0xf2cbbce0u, 0xebd08da1u, 0xc0fdde62u, 0xd9e6ef23u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2443 0x14bce1bdu, 0x0da7d0fcu, 0x268a833fu, 0x3f91b27eu, 0x70d024b9u, 0x69cb15f8u, 0x42e6463bu, 0x5bfd777au,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2444 0xdc656bb5u, 0xc57e5af4u, 0xee530937u, 0xf7483876u, 0xb809aeb1u, 0xa1129ff0u, 0x8a3fcc33u, 0x9324fd72u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2445 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2446
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2447 static const unsigned lodepng_crc32_table2[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2448 0x00000000u, 0x01c26a37u, 0x0384d46eu, 0x0246be59u, 0x0709a8dcu, 0x06cbc2ebu, 0x048d7cb2u, 0x054f1685u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2449 0x0e1351b8u, 0x0fd13b8fu, 0x0d9785d6u, 0x0c55efe1u, 0x091af964u, 0x08d89353u, 0x0a9e2d0au, 0x0b5c473du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2450 0x1c26a370u, 0x1de4c947u, 0x1fa2771eu, 0x1e601d29u, 0x1b2f0bacu, 0x1aed619bu, 0x18abdfc2u, 0x1969b5f5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2451 0x1235f2c8u, 0x13f798ffu, 0x11b126a6u, 0x10734c91u, 0x153c5a14u, 0x14fe3023u, 0x16b88e7au, 0x177ae44du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2452 0x384d46e0u, 0x398f2cd7u, 0x3bc9928eu, 0x3a0bf8b9u, 0x3f44ee3cu, 0x3e86840bu, 0x3cc03a52u, 0x3d025065u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2453 0x365e1758u, 0x379c7d6fu, 0x35dac336u, 0x3418a901u, 0x3157bf84u, 0x3095d5b3u, 0x32d36beau, 0x331101ddu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2454 0x246be590u, 0x25a98fa7u, 0x27ef31feu, 0x262d5bc9u, 0x23624d4cu, 0x22a0277bu, 0x20e69922u, 0x2124f315u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2455 0x2a78b428u, 0x2bbade1fu, 0x29fc6046u, 0x283e0a71u, 0x2d711cf4u, 0x2cb376c3u, 0x2ef5c89au, 0x2f37a2adu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2456 0x709a8dc0u, 0x7158e7f7u, 0x731e59aeu, 0x72dc3399u, 0x7793251cu, 0x76514f2bu, 0x7417f172u, 0x75d59b45u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2457 0x7e89dc78u, 0x7f4bb64fu, 0x7d0d0816u, 0x7ccf6221u, 0x798074a4u, 0x78421e93u, 0x7a04a0cau, 0x7bc6cafdu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2458 0x6cbc2eb0u, 0x6d7e4487u, 0x6f38fadeu, 0x6efa90e9u, 0x6bb5866cu, 0x6a77ec5bu, 0x68315202u, 0x69f33835u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2459 0x62af7f08u, 0x636d153fu, 0x612bab66u, 0x60e9c151u, 0x65a6d7d4u, 0x6464bde3u, 0x662203bau, 0x67e0698du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2460 0x48d7cb20u, 0x4915a117u, 0x4b531f4eu, 0x4a917579u, 0x4fde63fcu, 0x4e1c09cbu, 0x4c5ab792u, 0x4d98dda5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2461 0x46c49a98u, 0x4706f0afu, 0x45404ef6u, 0x448224c1u, 0x41cd3244u, 0x400f5873u, 0x4249e62au, 0x438b8c1du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2462 0x54f16850u, 0x55330267u, 0x5775bc3eu, 0x56b7d609u, 0x53f8c08cu, 0x523aaabbu, 0x507c14e2u, 0x51be7ed5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2463 0x5ae239e8u, 0x5b2053dfu, 0x5966ed86u, 0x58a487b1u, 0x5deb9134u, 0x5c29fb03u, 0x5e6f455au, 0x5fad2f6du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2464 0xe1351b80u, 0xe0f771b7u, 0xe2b1cfeeu, 0xe373a5d9u, 0xe63cb35cu, 0xe7fed96bu, 0xe5b86732u, 0xe47a0d05u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2465 0xef264a38u, 0xeee4200fu, 0xeca29e56u, 0xed60f461u, 0xe82fe2e4u, 0xe9ed88d3u, 0xebab368au, 0xea695cbdu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2466 0xfd13b8f0u, 0xfcd1d2c7u, 0xfe976c9eu, 0xff5506a9u, 0xfa1a102cu, 0xfbd87a1bu, 0xf99ec442u, 0xf85cae75u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2467 0xf300e948u, 0xf2c2837fu, 0xf0843d26u, 0xf1465711u, 0xf4094194u, 0xf5cb2ba3u, 0xf78d95fau, 0xf64fffcdu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2468 0xd9785d60u, 0xd8ba3757u, 0xdafc890eu, 0xdb3ee339u, 0xde71f5bcu, 0xdfb39f8bu, 0xddf521d2u, 0xdc374be5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2469 0xd76b0cd8u, 0xd6a966efu, 0xd4efd8b6u, 0xd52db281u, 0xd062a404u, 0xd1a0ce33u, 0xd3e6706au, 0xd2241a5du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2470 0xc55efe10u, 0xc49c9427u, 0xc6da2a7eu, 0xc7184049u, 0xc25756ccu, 0xc3953cfbu, 0xc1d382a2u, 0xc011e895u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2471 0xcb4dafa8u, 0xca8fc59fu, 0xc8c97bc6u, 0xc90b11f1u, 0xcc440774u, 0xcd866d43u, 0xcfc0d31au, 0xce02b92du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2472 0x91af9640u, 0x906dfc77u, 0x922b422eu, 0x93e92819u, 0x96a63e9cu, 0x976454abu, 0x9522eaf2u, 0x94e080c5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2473 0x9fbcc7f8u, 0x9e7eadcfu, 0x9c381396u, 0x9dfa79a1u, 0x98b56f24u, 0x99770513u, 0x9b31bb4au, 0x9af3d17du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2474 0x8d893530u, 0x8c4b5f07u, 0x8e0de15eu, 0x8fcf8b69u, 0x8a809decu, 0x8b42f7dbu, 0x89044982u, 0x88c623b5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2475 0x839a6488u, 0x82580ebfu, 0x801eb0e6u, 0x81dcdad1u, 0x8493cc54u, 0x8551a663u, 0x8717183au, 0x86d5720du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2476 0xa9e2d0a0u, 0xa820ba97u, 0xaa6604ceu, 0xaba46ef9u, 0xaeeb787cu, 0xaf29124bu, 0xad6fac12u, 0xacadc625u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2477 0xa7f18118u, 0xa633eb2fu, 0xa4755576u, 0xa5b73f41u, 0xa0f829c4u, 0xa13a43f3u, 0xa37cfdaau, 0xa2be979du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2478 0xb5c473d0u, 0xb40619e7u, 0xb640a7beu, 0xb782cd89u, 0xb2cddb0cu, 0xb30fb13bu, 0xb1490f62u, 0xb08b6555u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2479 0xbbd72268u, 0xba15485fu, 0xb853f606u, 0xb9919c31u, 0xbcde8ab4u, 0xbd1ce083u, 0xbf5a5edau, 0xbe9834edu
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2480 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2481
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2482 static const unsigned lodepng_crc32_table3[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2483 0x00000000u, 0xb8bc6765u, 0xaa09c88bu, 0x12b5afeeu, 0x8f629757u, 0x37def032u, 0x256b5fdcu, 0x9dd738b9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2484 0xc5b428efu, 0x7d084f8au, 0x6fbde064u, 0xd7018701u, 0x4ad6bfb8u, 0xf26ad8ddu, 0xe0df7733u, 0x58631056u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2485 0x5019579fu, 0xe8a530fau, 0xfa109f14u, 0x42acf871u, 0xdf7bc0c8u, 0x67c7a7adu, 0x75720843u, 0xcdce6f26u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2486 0x95ad7f70u, 0x2d111815u, 0x3fa4b7fbu, 0x8718d09eu, 0x1acfe827u, 0xa2738f42u, 0xb0c620acu, 0x087a47c9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2487 0xa032af3eu, 0x188ec85bu, 0x0a3b67b5u, 0xb28700d0u, 0x2f503869u, 0x97ec5f0cu, 0x8559f0e2u, 0x3de59787u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2488 0x658687d1u, 0xdd3ae0b4u, 0xcf8f4f5au, 0x7733283fu, 0xeae41086u, 0x525877e3u, 0x40edd80du, 0xf851bf68u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2489 0xf02bf8a1u, 0x48979fc4u, 0x5a22302au, 0xe29e574fu, 0x7f496ff6u, 0xc7f50893u, 0xd540a77du, 0x6dfcc018u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2490 0x359fd04eu, 0x8d23b72bu, 0x9f9618c5u, 0x272a7fa0u, 0xbafd4719u, 0x0241207cu, 0x10f48f92u, 0xa848e8f7u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2491 0x9b14583du, 0x23a83f58u, 0x311d90b6u, 0x89a1f7d3u, 0x1476cf6au, 0xaccaa80fu, 0xbe7f07e1u, 0x06c36084u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2492 0x5ea070d2u, 0xe61c17b7u, 0xf4a9b859u, 0x4c15df3cu, 0xd1c2e785u, 0x697e80e0u, 0x7bcb2f0eu, 0xc377486bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2493 0xcb0d0fa2u, 0x73b168c7u, 0x6104c729u, 0xd9b8a04cu, 0x446f98f5u, 0xfcd3ff90u, 0xee66507eu, 0x56da371bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2494 0x0eb9274du, 0xb6054028u, 0xa4b0efc6u, 0x1c0c88a3u, 0x81dbb01au, 0x3967d77fu, 0x2bd27891u, 0x936e1ff4u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2495 0x3b26f703u, 0x839a9066u, 0x912f3f88u, 0x299358edu, 0xb4446054u, 0x0cf80731u, 0x1e4da8dfu, 0xa6f1cfbau,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2496 0xfe92dfecu, 0x462eb889u, 0x549b1767u, 0xec277002u, 0x71f048bbu, 0xc94c2fdeu, 0xdbf98030u, 0x6345e755u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2497 0x6b3fa09cu, 0xd383c7f9u, 0xc1366817u, 0x798a0f72u, 0xe45d37cbu, 0x5ce150aeu, 0x4e54ff40u, 0xf6e89825u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2498 0xae8b8873u, 0x1637ef16u, 0x048240f8u, 0xbc3e279du, 0x21e91f24u, 0x99557841u, 0x8be0d7afu, 0x335cb0cau,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2499 0xed59b63bu, 0x55e5d15eu, 0x47507eb0u, 0xffec19d5u, 0x623b216cu, 0xda874609u, 0xc832e9e7u, 0x708e8e82u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2500 0x28ed9ed4u, 0x9051f9b1u, 0x82e4565fu, 0x3a58313au, 0xa78f0983u, 0x1f336ee6u, 0x0d86c108u, 0xb53aa66du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2501 0xbd40e1a4u, 0x05fc86c1u, 0x1749292fu, 0xaff54e4au, 0x322276f3u, 0x8a9e1196u, 0x982bbe78u, 0x2097d91du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2502 0x78f4c94bu, 0xc048ae2eu, 0xd2fd01c0u, 0x6a4166a5u, 0xf7965e1cu, 0x4f2a3979u, 0x5d9f9697u, 0xe523f1f2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2503 0x4d6b1905u, 0xf5d77e60u, 0xe762d18eu, 0x5fdeb6ebu, 0xc2098e52u, 0x7ab5e937u, 0x680046d9u, 0xd0bc21bcu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2504 0x88df31eau, 0x3063568fu, 0x22d6f961u, 0x9a6a9e04u, 0x07bda6bdu, 0xbf01c1d8u, 0xadb46e36u, 0x15080953u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2505 0x1d724e9au, 0xa5ce29ffu, 0xb77b8611u, 0x0fc7e174u, 0x9210d9cdu, 0x2aacbea8u, 0x38191146u, 0x80a57623u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2506 0xd8c66675u, 0x607a0110u, 0x72cfaefeu, 0xca73c99bu, 0x57a4f122u, 0xef189647u, 0xfdad39a9u, 0x45115eccu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2507 0x764dee06u, 0xcef18963u, 0xdc44268du, 0x64f841e8u, 0xf92f7951u, 0x41931e34u, 0x5326b1dau, 0xeb9ad6bfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2508 0xb3f9c6e9u, 0x0b45a18cu, 0x19f00e62u, 0xa14c6907u, 0x3c9b51beu, 0x842736dbu, 0x96929935u, 0x2e2efe50u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2509 0x2654b999u, 0x9ee8defcu, 0x8c5d7112u, 0x34e11677u, 0xa9362eceu, 0x118a49abu, 0x033fe645u, 0xbb838120u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2510 0xe3e09176u, 0x5b5cf613u, 0x49e959fdu, 0xf1553e98u, 0x6c820621u, 0xd43e6144u, 0xc68bceaau, 0x7e37a9cfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2511 0xd67f4138u, 0x6ec3265du, 0x7c7689b3u, 0xc4caeed6u, 0x591dd66fu, 0xe1a1b10au, 0xf3141ee4u, 0x4ba87981u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2512 0x13cb69d7u, 0xab770eb2u, 0xb9c2a15cu, 0x017ec639u, 0x9ca9fe80u, 0x241599e5u, 0x36a0360bu, 0x8e1c516eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2513 0x866616a7u, 0x3eda71c2u, 0x2c6fde2cu, 0x94d3b949u, 0x090481f0u, 0xb1b8e695u, 0xa30d497bu, 0x1bb12e1eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2514 0x43d23e48u, 0xfb6e592du, 0xe9dbf6c3u, 0x516791a6u, 0xccb0a91fu, 0x740cce7au, 0x66b96194u, 0xde0506f1u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2515 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2516
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2517 static const unsigned lodepng_crc32_table4[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2518 0x00000000u, 0x3d6029b0u, 0x7ac05360u, 0x47a07ad0u, 0xf580a6c0u, 0xc8e08f70u, 0x8f40f5a0u, 0xb220dc10u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2519 0x30704bc1u, 0x0d106271u, 0x4ab018a1u, 0x77d03111u, 0xc5f0ed01u, 0xf890c4b1u, 0xbf30be61u, 0x825097d1u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2520 0x60e09782u, 0x5d80be32u, 0x1a20c4e2u, 0x2740ed52u, 0x95603142u, 0xa80018f2u, 0xefa06222u, 0xd2c04b92u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2521 0x5090dc43u, 0x6df0f5f3u, 0x2a508f23u, 0x1730a693u, 0xa5107a83u, 0x98705333u, 0xdfd029e3u, 0xe2b00053u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2522 0xc1c12f04u, 0xfca106b4u, 0xbb017c64u, 0x866155d4u, 0x344189c4u, 0x0921a074u, 0x4e81daa4u, 0x73e1f314u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2523 0xf1b164c5u, 0xccd14d75u, 0x8b7137a5u, 0xb6111e15u, 0x0431c205u, 0x3951ebb5u, 0x7ef19165u, 0x4391b8d5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2524 0xa121b886u, 0x9c419136u, 0xdbe1ebe6u, 0xe681c256u, 0x54a11e46u, 0x69c137f6u, 0x2e614d26u, 0x13016496u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2525 0x9151f347u, 0xac31daf7u, 0xeb91a027u, 0xd6f18997u, 0x64d15587u, 0x59b17c37u, 0x1e1106e7u, 0x23712f57u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2526 0x58f35849u, 0x659371f9u, 0x22330b29u, 0x1f532299u, 0xad73fe89u, 0x9013d739u, 0xd7b3ade9u, 0xead38459u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2527 0x68831388u, 0x55e33a38u, 0x124340e8u, 0x2f236958u, 0x9d03b548u, 0xa0639cf8u, 0xe7c3e628u, 0xdaa3cf98u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2528 0x3813cfcbu, 0x0573e67bu, 0x42d39cabu, 0x7fb3b51bu, 0xcd93690bu, 0xf0f340bbu, 0xb7533a6bu, 0x8a3313dbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2529 0x0863840au, 0x3503adbau, 0x72a3d76au, 0x4fc3fedau, 0xfde322cau, 0xc0830b7au, 0x872371aau, 0xba43581au,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2530 0x9932774du, 0xa4525efdu, 0xe3f2242du, 0xde920d9du, 0x6cb2d18du, 0x51d2f83du, 0x167282edu, 0x2b12ab5du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2531 0xa9423c8cu, 0x9422153cu, 0xd3826fecu, 0xeee2465cu, 0x5cc29a4cu, 0x61a2b3fcu, 0x2602c92cu, 0x1b62e09cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2532 0xf9d2e0cfu, 0xc4b2c97fu, 0x8312b3afu, 0xbe729a1fu, 0x0c52460fu, 0x31326fbfu, 0x7692156fu, 0x4bf23cdfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2533 0xc9a2ab0eu, 0xf4c282beu, 0xb362f86eu, 0x8e02d1deu, 0x3c220dceu, 0x0142247eu, 0x46e25eaeu, 0x7b82771eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2534 0xb1e6b092u, 0x8c869922u, 0xcb26e3f2u, 0xf646ca42u, 0x44661652u, 0x79063fe2u, 0x3ea64532u, 0x03c66c82u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2535 0x8196fb53u, 0xbcf6d2e3u, 0xfb56a833u, 0xc6368183u, 0x74165d93u, 0x49767423u, 0x0ed60ef3u, 0x33b62743u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2536 0xd1062710u, 0xec660ea0u, 0xabc67470u, 0x96a65dc0u, 0x248681d0u, 0x19e6a860u, 0x5e46d2b0u, 0x6326fb00u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2537 0xe1766cd1u, 0xdc164561u, 0x9bb63fb1u, 0xa6d61601u, 0x14f6ca11u, 0x2996e3a1u, 0x6e369971u, 0x5356b0c1u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2538 0x70279f96u, 0x4d47b626u, 0x0ae7ccf6u, 0x3787e546u, 0x85a73956u, 0xb8c710e6u, 0xff676a36u, 0xc2074386u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2539 0x4057d457u, 0x7d37fde7u, 0x3a978737u, 0x07f7ae87u, 0xb5d77297u, 0x88b75b27u, 0xcf1721f7u, 0xf2770847u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2540 0x10c70814u, 0x2da721a4u, 0x6a075b74u, 0x576772c4u, 0xe547aed4u, 0xd8278764u, 0x9f87fdb4u, 0xa2e7d404u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2541 0x20b743d5u, 0x1dd76a65u, 0x5a7710b5u, 0x67173905u, 0xd537e515u, 0xe857cca5u, 0xaff7b675u, 0x92979fc5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2542 0xe915e8dbu, 0xd475c16bu, 0x93d5bbbbu, 0xaeb5920bu, 0x1c954e1bu, 0x21f567abu, 0x66551d7bu, 0x5b3534cbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2543 0xd965a31au, 0xe4058aaau, 0xa3a5f07au, 0x9ec5d9cau, 0x2ce505dau, 0x11852c6au, 0x562556bau, 0x6b457f0au,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2544 0x89f57f59u, 0xb49556e9u, 0xf3352c39u, 0xce550589u, 0x7c75d999u, 0x4115f029u, 0x06b58af9u, 0x3bd5a349u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2545 0xb9853498u, 0x84e51d28u, 0xc34567f8u, 0xfe254e48u, 0x4c059258u, 0x7165bbe8u, 0x36c5c138u, 0x0ba5e888u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2546 0x28d4c7dfu, 0x15b4ee6fu, 0x521494bfu, 0x6f74bd0fu, 0xdd54611fu, 0xe03448afu, 0xa794327fu, 0x9af41bcfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2547 0x18a48c1eu, 0x25c4a5aeu, 0x6264df7eu, 0x5f04f6ceu, 0xed242adeu, 0xd044036eu, 0x97e479beu, 0xaa84500eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2548 0x4834505du, 0x755479edu, 0x32f4033du, 0x0f942a8du, 0xbdb4f69du, 0x80d4df2du, 0xc774a5fdu, 0xfa148c4du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2549 0x78441b9cu, 0x4524322cu, 0x028448fcu, 0x3fe4614cu, 0x8dc4bd5cu, 0xb0a494ecu, 0xf704ee3cu, 0xca64c78cu
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2550 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2551
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2552 static const unsigned lodepng_crc32_table5[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2553 0x00000000u, 0xcb5cd3a5u, 0x4dc8a10bu, 0x869472aeu, 0x9b914216u, 0x50cd91b3u, 0xd659e31du, 0x1d0530b8u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2554 0xec53826du, 0x270f51c8u, 0xa19b2366u, 0x6ac7f0c3u, 0x77c2c07bu, 0xbc9e13deu, 0x3a0a6170u, 0xf156b2d5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2555 0x03d6029bu, 0xc88ad13eu, 0x4e1ea390u, 0x85427035u, 0x9847408du, 0x531b9328u, 0xd58fe186u, 0x1ed33223u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2556 0xef8580f6u, 0x24d95353u, 0xa24d21fdu, 0x6911f258u, 0x7414c2e0u, 0xbf481145u, 0x39dc63ebu, 0xf280b04eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2557 0x07ac0536u, 0xccf0d693u, 0x4a64a43du, 0x81387798u, 0x9c3d4720u, 0x57619485u, 0xd1f5e62bu, 0x1aa9358eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2558 0xebff875bu, 0x20a354feu, 0xa6372650u, 0x6d6bf5f5u, 0x706ec54du, 0xbb3216e8u, 0x3da66446u, 0xf6fab7e3u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2559 0x047a07adu, 0xcf26d408u, 0x49b2a6a6u, 0x82ee7503u, 0x9feb45bbu, 0x54b7961eu, 0xd223e4b0u, 0x197f3715u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2560 0xe82985c0u, 0x23755665u, 0xa5e124cbu, 0x6ebdf76eu, 0x73b8c7d6u, 0xb8e41473u, 0x3e7066ddu, 0xf52cb578u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2561 0x0f580a6cu, 0xc404d9c9u, 0x4290ab67u, 0x89cc78c2u, 0x94c9487au, 0x5f959bdfu, 0xd901e971u, 0x125d3ad4u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2562 0xe30b8801u, 0x28575ba4u, 0xaec3290au, 0x659ffaafu, 0x789aca17u, 0xb3c619b2u, 0x35526b1cu, 0xfe0eb8b9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2563 0x0c8e08f7u, 0xc7d2db52u, 0x4146a9fcu, 0x8a1a7a59u, 0x971f4ae1u, 0x5c439944u, 0xdad7ebeau, 0x118b384fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2564 0xe0dd8a9au, 0x2b81593fu, 0xad152b91u, 0x6649f834u, 0x7b4cc88cu, 0xb0101b29u, 0x36846987u, 0xfdd8ba22u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2565 0x08f40f5au, 0xc3a8dcffu, 0x453cae51u, 0x8e607df4u, 0x93654d4cu, 0x58399ee9u, 0xdeadec47u, 0x15f13fe2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2566 0xe4a78d37u, 0x2ffb5e92u, 0xa96f2c3cu, 0x6233ff99u, 0x7f36cf21u, 0xb46a1c84u, 0x32fe6e2au, 0xf9a2bd8fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2567 0x0b220dc1u, 0xc07ede64u, 0x46eaaccau, 0x8db67f6fu, 0x90b34fd7u, 0x5bef9c72u, 0xdd7beedcu, 0x16273d79u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2568 0xe7718facu, 0x2c2d5c09u, 0xaab92ea7u, 0x61e5fd02u, 0x7ce0cdbau, 0xb7bc1e1fu, 0x31286cb1u, 0xfa74bf14u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2569 0x1eb014d8u, 0xd5ecc77du, 0x5378b5d3u, 0x98246676u, 0x852156ceu, 0x4e7d856bu, 0xc8e9f7c5u, 0x03b52460u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2570 0xf2e396b5u, 0x39bf4510u, 0xbf2b37beu, 0x7477e41bu, 0x6972d4a3u, 0xa22e0706u, 0x24ba75a8u, 0xefe6a60du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2571 0x1d661643u, 0xd63ac5e6u, 0x50aeb748u, 0x9bf264edu, 0x86f75455u, 0x4dab87f0u, 0xcb3ff55eu, 0x006326fbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2572 0xf135942eu, 0x3a69478bu, 0xbcfd3525u, 0x77a1e680u, 0x6aa4d638u, 0xa1f8059du, 0x276c7733u, 0xec30a496u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2573 0x191c11eeu, 0xd240c24bu, 0x54d4b0e5u, 0x9f886340u, 0x828d53f8u, 0x49d1805du, 0xcf45f2f3u, 0x04192156u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2574 0xf54f9383u, 0x3e134026u, 0xb8873288u, 0x73dbe12du, 0x6eded195u, 0xa5820230u, 0x2316709eu, 0xe84aa33bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2575 0x1aca1375u, 0xd196c0d0u, 0x5702b27eu, 0x9c5e61dbu, 0x815b5163u, 0x4a0782c6u, 0xcc93f068u, 0x07cf23cdu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2576 0xf6999118u, 0x3dc542bdu, 0xbb513013u, 0x700de3b6u, 0x6d08d30eu, 0xa65400abu, 0x20c07205u, 0xeb9ca1a0u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2577 0x11e81eb4u, 0xdab4cd11u, 0x5c20bfbfu, 0x977c6c1au, 0x8a795ca2u, 0x41258f07u, 0xc7b1fda9u, 0x0ced2e0cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2578 0xfdbb9cd9u, 0x36e74f7cu, 0xb0733dd2u, 0x7b2fee77u, 0x662adecfu, 0xad760d6au, 0x2be27fc4u, 0xe0beac61u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2579 0x123e1c2fu, 0xd962cf8au, 0x5ff6bd24u, 0x94aa6e81u, 0x89af5e39u, 0x42f38d9cu, 0xc467ff32u, 0x0f3b2c97u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2580 0xfe6d9e42u, 0x35314de7u, 0xb3a53f49u, 0x78f9ececu, 0x65fcdc54u, 0xaea00ff1u, 0x28347d5fu, 0xe368aefau,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2581 0x16441b82u, 0xdd18c827u, 0x5b8cba89u, 0x90d0692cu, 0x8dd55994u, 0x46898a31u, 0xc01df89fu, 0x0b412b3au,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2582 0xfa1799efu, 0x314b4a4au, 0xb7df38e4u, 0x7c83eb41u, 0x6186dbf9u, 0xaada085cu, 0x2c4e7af2u, 0xe712a957u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2583 0x15921919u, 0xdececabcu, 0x585ab812u, 0x93066bb7u, 0x8e035b0fu, 0x455f88aau, 0xc3cbfa04u, 0x089729a1u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2584 0xf9c19b74u, 0x329d48d1u, 0xb4093a7fu, 0x7f55e9dau, 0x6250d962u, 0xa90c0ac7u, 0x2f987869u, 0xe4c4abccu
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2585 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2586
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2587 static const unsigned lodepng_crc32_table6[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2588 0x00000000u, 0xa6770bb4u, 0x979f1129u, 0x31e81a9du, 0xf44f2413u, 0x52382fa7u, 0x63d0353au, 0xc5a73e8eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2589 0x33ef4e67u, 0x959845d3u, 0xa4705f4eu, 0x020754fau, 0xc7a06a74u, 0x61d761c0u, 0x503f7b5du, 0xf64870e9u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2590 0x67de9cceu, 0xc1a9977au, 0xf0418de7u, 0x56368653u, 0x9391b8ddu, 0x35e6b369u, 0x040ea9f4u, 0xa279a240u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2591 0x5431d2a9u, 0xf246d91du, 0xc3aec380u, 0x65d9c834u, 0xa07ef6bau, 0x0609fd0eu, 0x37e1e793u, 0x9196ec27u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2592 0xcfbd399cu, 0x69ca3228u, 0x582228b5u, 0xfe552301u, 0x3bf21d8fu, 0x9d85163bu, 0xac6d0ca6u, 0x0a1a0712u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2593 0xfc5277fbu, 0x5a257c4fu, 0x6bcd66d2u, 0xcdba6d66u, 0x081d53e8u, 0xae6a585cu, 0x9f8242c1u, 0x39f54975u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2594 0xa863a552u, 0x0e14aee6u, 0x3ffcb47bu, 0x998bbfcfu, 0x5c2c8141u, 0xfa5b8af5u, 0xcbb39068u, 0x6dc49bdcu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2595 0x9b8ceb35u, 0x3dfbe081u, 0x0c13fa1cu, 0xaa64f1a8u, 0x6fc3cf26u, 0xc9b4c492u, 0xf85cde0fu, 0x5e2bd5bbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2596 0x440b7579u, 0xe27c7ecdu, 0xd3946450u, 0x75e36fe4u, 0xb044516au, 0x16335adeu, 0x27db4043u, 0x81ac4bf7u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2597 0x77e43b1eu, 0xd19330aau, 0xe07b2a37u, 0x460c2183u, 0x83ab1f0du, 0x25dc14b9u, 0x14340e24u, 0xb2430590u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2598 0x23d5e9b7u, 0x85a2e203u, 0xb44af89eu, 0x123df32au, 0xd79acda4u, 0x71edc610u, 0x4005dc8du, 0xe672d739u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2599 0x103aa7d0u, 0xb64dac64u, 0x87a5b6f9u, 0x21d2bd4du, 0xe47583c3u, 0x42028877u, 0x73ea92eau, 0xd59d995eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2600 0x8bb64ce5u, 0x2dc14751u, 0x1c295dccu, 0xba5e5678u, 0x7ff968f6u, 0xd98e6342u, 0xe86679dfu, 0x4e11726bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2601 0xb8590282u, 0x1e2e0936u, 0x2fc613abu, 0x89b1181fu, 0x4c162691u, 0xea612d25u, 0xdb8937b8u, 0x7dfe3c0cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2602 0xec68d02bu, 0x4a1fdb9fu, 0x7bf7c102u, 0xdd80cab6u, 0x1827f438u, 0xbe50ff8cu, 0x8fb8e511u, 0x29cfeea5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2603 0xdf879e4cu, 0x79f095f8u, 0x48188f65u, 0xee6f84d1u, 0x2bc8ba5fu, 0x8dbfb1ebu, 0xbc57ab76u, 0x1a20a0c2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2604 0x8816eaf2u, 0x2e61e146u, 0x1f89fbdbu, 0xb9fef06fu, 0x7c59cee1u, 0xda2ec555u, 0xebc6dfc8u, 0x4db1d47cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2605 0xbbf9a495u, 0x1d8eaf21u, 0x2c66b5bcu, 0x8a11be08u, 0x4fb68086u, 0xe9c18b32u, 0xd82991afu, 0x7e5e9a1bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2606 0xefc8763cu, 0x49bf7d88u, 0x78576715u, 0xde206ca1u, 0x1b87522fu, 0xbdf0599bu, 0x8c184306u, 0x2a6f48b2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2607 0xdc27385bu, 0x7a5033efu, 0x4bb82972u, 0xedcf22c6u, 0x28681c48u, 0x8e1f17fcu, 0xbff70d61u, 0x198006d5u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2608 0x47abd36eu, 0xe1dcd8dau, 0xd034c247u, 0x7643c9f3u, 0xb3e4f77du, 0x1593fcc9u, 0x247be654u, 0x820cede0u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2609 0x74449d09u, 0xd23396bdu, 0xe3db8c20u, 0x45ac8794u, 0x800bb91au, 0x267cb2aeu, 0x1794a833u, 0xb1e3a387u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2610 0x20754fa0u, 0x86024414u, 0xb7ea5e89u, 0x119d553du, 0xd43a6bb3u, 0x724d6007u, 0x43a57a9au, 0xe5d2712eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2611 0x139a01c7u, 0xb5ed0a73u, 0x840510eeu, 0x22721b5au, 0xe7d525d4u, 0x41a22e60u, 0x704a34fdu, 0xd63d3f49u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2612 0xcc1d9f8bu, 0x6a6a943fu, 0x5b828ea2u, 0xfdf58516u, 0x3852bb98u, 0x9e25b02cu, 0xafcdaab1u, 0x09baa105u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2613 0xfff2d1ecu, 0x5985da58u, 0x686dc0c5u, 0xce1acb71u, 0x0bbdf5ffu, 0xadcafe4bu, 0x9c22e4d6u, 0x3a55ef62u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2614 0xabc30345u, 0x0db408f1u, 0x3c5c126cu, 0x9a2b19d8u, 0x5f8c2756u, 0xf9fb2ce2u, 0xc813367fu, 0x6e643dcbu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2615 0x982c4d22u, 0x3e5b4696u, 0x0fb35c0bu, 0xa9c457bfu, 0x6c636931u, 0xca146285u, 0xfbfc7818u, 0x5d8b73acu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2616 0x03a0a617u, 0xa5d7ada3u, 0x943fb73eu, 0x3248bc8au, 0xf7ef8204u, 0x519889b0u, 0x6070932du, 0xc6079899u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2617 0x304fe870u, 0x9638e3c4u, 0xa7d0f959u, 0x01a7f2edu, 0xc400cc63u, 0x6277c7d7u, 0x539fdd4au, 0xf5e8d6feu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2618 0x647e3ad9u, 0xc209316du, 0xf3e12bf0u, 0x55962044u, 0x90311ecau, 0x3646157eu, 0x07ae0fe3u, 0xa1d90457u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2619 0x579174beu, 0xf1e67f0au, 0xc00e6597u, 0x66796e23u, 0xa3de50adu, 0x05a95b19u, 0x34414184u, 0x92364a30u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2620 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2621
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2622 static const unsigned lodepng_crc32_table7[256] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2623 0x00000000u, 0xccaa009eu, 0x4225077du, 0x8e8f07e3u, 0x844a0efau, 0x48e00e64u, 0xc66f0987u, 0x0ac50919u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2624 0xd3e51bb5u, 0x1f4f1b2bu, 0x91c01cc8u, 0x5d6a1c56u, 0x57af154fu, 0x9b0515d1u, 0x158a1232u, 0xd92012acu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2625 0x7cbb312bu, 0xb01131b5u, 0x3e9e3656u, 0xf23436c8u, 0xf8f13fd1u, 0x345b3f4fu, 0xbad438acu, 0x767e3832u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2626 0xaf5e2a9eu, 0x63f42a00u, 0xed7b2de3u, 0x21d12d7du, 0x2b142464u, 0xe7be24fau, 0x69312319u, 0xa59b2387u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2627 0xf9766256u, 0x35dc62c8u, 0xbb53652bu, 0x77f965b5u, 0x7d3c6cacu, 0xb1966c32u, 0x3f196bd1u, 0xf3b36b4fu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2628 0x2a9379e3u, 0xe639797du, 0x68b67e9eu, 0xa41c7e00u, 0xaed97719u, 0x62737787u, 0xecfc7064u, 0x205670fau,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2629 0x85cd537du, 0x496753e3u, 0xc7e85400u, 0x0b42549eu, 0x01875d87u, 0xcd2d5d19u, 0x43a25afau, 0x8f085a64u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2630 0x562848c8u, 0x9a824856u, 0x140d4fb5u, 0xd8a74f2bu, 0xd2624632u, 0x1ec846acu, 0x9047414fu, 0x5ced41d1u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2631 0x299dc2edu, 0xe537c273u, 0x6bb8c590u, 0xa712c50eu, 0xadd7cc17u, 0x617dcc89u, 0xeff2cb6au, 0x2358cbf4u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2632 0xfa78d958u, 0x36d2d9c6u, 0xb85dde25u, 0x74f7debbu, 0x7e32d7a2u, 0xb298d73cu, 0x3c17d0dfu, 0xf0bdd041u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2633 0x5526f3c6u, 0x998cf358u, 0x1703f4bbu, 0xdba9f425u, 0xd16cfd3cu, 0x1dc6fda2u, 0x9349fa41u, 0x5fe3fadfu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2634 0x86c3e873u, 0x4a69e8edu, 0xc4e6ef0eu, 0x084cef90u, 0x0289e689u, 0xce23e617u, 0x40ace1f4u, 0x8c06e16au,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2635 0xd0eba0bbu, 0x1c41a025u, 0x92cea7c6u, 0x5e64a758u, 0x54a1ae41u, 0x980baedfu, 0x1684a93cu, 0xda2ea9a2u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2636 0x030ebb0eu, 0xcfa4bb90u, 0x412bbc73u, 0x8d81bcedu, 0x8744b5f4u, 0x4beeb56au, 0xc561b289u, 0x09cbb217u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2637 0xac509190u, 0x60fa910eu, 0xee7596edu, 0x22df9673u, 0x281a9f6au, 0xe4b09ff4u, 0x6a3f9817u, 0xa6959889u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2638 0x7fb58a25u, 0xb31f8abbu, 0x3d908d58u, 0xf13a8dc6u, 0xfbff84dfu, 0x37558441u, 0xb9da83a2u, 0x7570833cu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2639 0x533b85dau, 0x9f918544u, 0x111e82a7u, 0xddb48239u, 0xd7718b20u, 0x1bdb8bbeu, 0x95548c5du, 0x59fe8cc3u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2640 0x80de9e6fu, 0x4c749ef1u, 0xc2fb9912u, 0x0e51998cu, 0x04949095u, 0xc83e900bu, 0x46b197e8u, 0x8a1b9776u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2641 0x2f80b4f1u, 0xe32ab46fu, 0x6da5b38cu, 0xa10fb312u, 0xabcaba0bu, 0x6760ba95u, 0xe9efbd76u, 0x2545bde8u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2642 0xfc65af44u, 0x30cfafdau, 0xbe40a839u, 0x72eaa8a7u, 0x782fa1beu, 0xb485a120u, 0x3a0aa6c3u, 0xf6a0a65du,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2643 0xaa4de78cu, 0x66e7e712u, 0xe868e0f1u, 0x24c2e06fu, 0x2e07e976u, 0xe2ade9e8u, 0x6c22ee0bu, 0xa088ee95u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2644 0x79a8fc39u, 0xb502fca7u, 0x3b8dfb44u, 0xf727fbdau, 0xfde2f2c3u, 0x3148f25du, 0xbfc7f5beu, 0x736df520u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2645 0xd6f6d6a7u, 0x1a5cd639u, 0x94d3d1dau, 0x5879d144u, 0x52bcd85du, 0x9e16d8c3u, 0x1099df20u, 0xdc33dfbeu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2646 0x0513cd12u, 0xc9b9cd8cu, 0x4736ca6fu, 0x8b9ccaf1u, 0x8159c3e8u, 0x4df3c376u, 0xc37cc495u, 0x0fd6c40bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2647 0x7aa64737u, 0xb60c47a9u, 0x3883404au, 0xf42940d4u, 0xfeec49cdu, 0x32464953u, 0xbcc94eb0u, 0x70634e2eu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2648 0xa9435c82u, 0x65e95c1cu, 0xeb665bffu, 0x27cc5b61u, 0x2d095278u, 0xe1a352e6u, 0x6f2c5505u, 0xa386559bu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2649 0x061d761cu, 0xcab77682u, 0x44387161u, 0x889271ffu, 0x825778e6u, 0x4efd7878u, 0xc0727f9bu, 0x0cd87f05u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2650 0xd5f86da9u, 0x19526d37u, 0x97dd6ad4u, 0x5b776a4au, 0x51b26353u, 0x9d1863cdu, 0x1397642eu, 0xdf3d64b0u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2651 0x83d02561u, 0x4f7a25ffu, 0xc1f5221cu, 0x0d5f2282u, 0x079a2b9bu, 0xcb302b05u, 0x45bf2ce6u, 0x89152c78u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2652 0x50353ed4u, 0x9c9f3e4au, 0x121039a9u, 0xdeba3937u, 0xd47f302eu, 0x18d530b0u, 0x965a3753u, 0x5af037cdu,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2653 0xff6b144au, 0x33c114d4u, 0xbd4e1337u, 0x71e413a9u, 0x7b211ab0u, 0xb78b1a2eu, 0x39041dcdu, 0xf5ae1d53u,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2654 0x2c8e0fffu, 0xe0240f61u, 0x6eab0882u, 0xa201081cu, 0xa8c40105u, 0x646e019bu, 0xeae10678u, 0x264b06e6u
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2655 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2656
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2657 /* Computes the cyclic redundancy check as used by PNG chunks*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2658 unsigned lodepng_crc32(const unsigned char* data, size_t length) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2659 /*Using the Slicing by Eight algorithm*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2660 unsigned r = 0xffffffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2661 while(length >= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2662 r = lodepng_crc32_table7[(data[0] ^ (r & 0xffu))] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2663 lodepng_crc32_table6[(data[1] ^ ((r >> 8) & 0xffu))] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2664 lodepng_crc32_table5[(data[2] ^ ((r >> 16) & 0xffu))] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2665 lodepng_crc32_table4[(data[3] ^ ((r >> 24) & 0xffu))] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2666 lodepng_crc32_table3[data[4]] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2667 lodepng_crc32_table2[data[5]] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2668 lodepng_crc32_table1[data[6]] ^
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2669 lodepng_crc32_table0[data[7]];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2670 data += 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2671 length -= 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2672 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2673 while(length--) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2674 r = lodepng_crc32_table0[(r ^ *data++) & 0xffu] ^ (r >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2675 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2676 return r ^ 0xffffffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2677 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2678 #else /* LODEPNG_COMPILE_CRC */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2679 /*in this case, the function is only declared here, and must be defined externally
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2680 so that it will be linked in.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2681
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2682 Example implementation that uses a much smaller lookup table for memory constrained cases:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2683
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2684 unsigned lodepng_crc32(const unsigned char* data, size_t length) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2685 unsigned r = 0xffffffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2686 static const unsigned table[16] = {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2687 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2688 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2689 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2690 while(length--) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2691 r = table[(r ^ *data) & 0xf] ^ (r >> 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2692 r = table[(r ^ (*data >> 4)) & 0xf] ^ (r >> 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2693 data++;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2694 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2695 return r ^ 0xffffffffu;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2696 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2697 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2698 unsigned lodepng_crc32(const unsigned char* data, size_t length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2699 #endif /* LODEPNG_COMPILE_CRC */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2700
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2701 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2702 /* / Reading and writing PNG color channel bits / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2703 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2704
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2705 /* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2706 so LodePNGBitWriter and LodePNGBitReader can't be used for those. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2707
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2708 static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2709 unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2710 ++(*bitpointer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2711 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2712 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2713
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2714 /* TODO: make this faster */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2715 static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2716 unsigned result = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2717 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2718 for(i = 0 ; i < nbits; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2719 result <<= 1u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2720 result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2721 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2722 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2723 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2724
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2725 static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2726 /*the current bit in bitstream may be 0 or 1 for this to work*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2727 if(bit == 0) bitstream[(*bitpointer) >> 3u] &= (unsigned char)(~(1u << (7u - ((*bitpointer) & 7u))));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2728 else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2729 ++(*bitpointer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2730 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2731
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2732 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2733 /* / PNG chunks / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2734 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2735
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2736 unsigned lodepng_chunk_length(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2737 return lodepng_read32bitInt(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2738 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2739
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2740 void lodepng_chunk_type(char type[5], const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2741 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2742 for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2743 type[4] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2744 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2745
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2746 unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2747 if(lodepng_strlen(type) != 4) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2748 return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2749 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2750
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2751 unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2752 return((chunk[4] & 32) != 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2753 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2754
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2755 unsigned char lodepng_chunk_private(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2756 return((chunk[6] & 32) != 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2757 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2758
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2759 unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2760 return((chunk[7] & 32) != 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2761 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2762
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2763 unsigned char* lodepng_chunk_data(unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2764 return &chunk[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2765 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2766
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2767 const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2768 return &chunk[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2769 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2770
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2771 unsigned lodepng_chunk_check_crc(const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2772 unsigned length = lodepng_chunk_length(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2773 unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2774 /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2775 unsigned checksum = lodepng_crc32(&chunk[4], length + 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2776 if(CRC != checksum) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2777 else return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2778 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2779
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2780 void lodepng_chunk_generate_crc(unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2781 unsigned length = lodepng_chunk_length(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2782 unsigned CRC = lodepng_crc32(&chunk[4], length + 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2783 lodepng_set32bitInt(chunk + 8 + length, CRC);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2784 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2785
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2786 unsigned char* lodepng_chunk_next(unsigned char* chunk, unsigned char* end) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2787 size_t available_size = (size_t)(end - chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2788 if(chunk >= end || available_size < 12) return end; /*too small to contain a chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2789 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2790 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2791 /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2792 return chunk + 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2793 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2794 size_t total_chunk_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2795 if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return end;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2796 if(total_chunk_length > available_size) return end; /*outside of range*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2797 return chunk + total_chunk_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2798 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2799 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2800
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2801 const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk, const unsigned char* end) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2802 size_t available_size = (size_t)(end - chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2803 if(chunk >= end || available_size < 12) return end; /*too small to contain a chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2804 if(chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2805 && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2806 /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2807 return chunk + 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2808 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2809 size_t total_chunk_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2810 if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return end;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2811 if(total_chunk_length > available_size) return end; /*outside of range*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2812 return chunk + total_chunk_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2813 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2814 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2815
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2816 unsigned char* lodepng_chunk_find(unsigned char* chunk, unsigned char* end, const char type[5]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2817 for(;;) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2818 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2819 if(lodepng_chunk_type_equals(chunk, type)) return chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2820 chunk = lodepng_chunk_next(chunk, end);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2821 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2822 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2823
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2824 const unsigned char* lodepng_chunk_find_const(const unsigned char* chunk, const unsigned char* end, const char type[5]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2825 for(;;) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2826 if(chunk >= end || end - chunk < 12) return 0; /* past file end: chunk + 12 > end */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2827 if(lodepng_chunk_type_equals(chunk, type)) return chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2828 chunk = lodepng_chunk_next_const(chunk, end);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2829 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2830 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2831
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2832 unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsigned char* chunk) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2833 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2834 size_t total_chunk_length, new_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2835 unsigned char *chunk_start, *new_buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2836
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2837 if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return 77;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2838 if(lodepng_addofl(*outsize, total_chunk_length, &new_length)) return 77;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2839
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2840 new_buffer = (unsigned char*)lodepng_realloc(*out, new_length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2841 if(!new_buffer) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2842 (*out) = new_buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2843 (*outsize) = new_length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2844 chunk_start = &(*out)[new_length - total_chunk_length];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2845
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2846 for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2847
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2848 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2849 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2850
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2851 /*Sets length and name and allocates the space for data and crc but does not
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2852 set data or crc yet. Returns the start of the chunk in chunk. The start of
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2853 the data is at chunk + 8. To finalize chunk, add the data, then use
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2854 lodepng_chunk_generate_crc */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2855 static unsigned lodepng_chunk_init(unsigned char** chunk,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2856 ucvector* out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2857 unsigned length, const char* type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2858 size_t new_length = out->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2859 if(lodepng_addofl(new_length, length, &new_length)) return 77;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2860 if(lodepng_addofl(new_length, 12, &new_length)) return 77;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2861 if(!ucvector_resize(out, new_length)) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2862 *chunk = out->data + new_length - length - 12u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2863
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2864 /*1: length*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2865 lodepng_set32bitInt(*chunk, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2866
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2867 /*2: chunk name (4 letters)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2868 lodepng_memcpy(*chunk + 4, type, 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2869
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2870 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2871 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2872
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2873 /* like lodepng_chunk_create but with custom allocsize */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2874 static unsigned lodepng_chunk_createv(ucvector* out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2875 unsigned length, const char* type, const unsigned char* data) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2876 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2877 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, length, type));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2878
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2879 /*3: the data*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2880 lodepng_memcpy(chunk + 8, data, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2881
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2882 /*4: CRC (of the chunkname characters and the data)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2883 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2884
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2885 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2886 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2887
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2888 unsigned lodepng_chunk_create(unsigned char** out, size_t* outsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2889 unsigned length, const char* type, const unsigned char* data) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2890 ucvector v = ucvector_init(*out, *outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2891 unsigned error = lodepng_chunk_createv(&v, length, type, data);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2892 *out = v.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2893 *outsize = v.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2894 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2895 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2896
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2897 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2898 /* / Color types, channels, bits / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2899 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2900
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2901 /*checks if the colortype is valid and the bitdepth bd is allowed for this colortype.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2902 Return value is a LodePNG error code.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2903 static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2904 switch(colortype) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2905 case LCT_GREY: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2906 case LCT_RGB: if(!( bd == 8 || bd == 16)) return 37; break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2907 case LCT_PALETTE: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2908 case LCT_GREY_ALPHA: if(!( bd == 8 || bd == 16)) return 37; break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2909 case LCT_RGBA: if(!( bd == 8 || bd == 16)) return 37; break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2910 case LCT_MAX_OCTET_VALUE: return 31; /* invalid color type */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2911 default: return 31; /* invalid color type */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2912 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2913 return 0; /*allowed color type / bits combination*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2914 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2915
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2916 static unsigned getNumColorChannels(LodePNGColorType colortype) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2917 switch(colortype) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2918 case LCT_GREY: return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2919 case LCT_RGB: return 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2920 case LCT_PALETTE: return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2921 case LCT_GREY_ALPHA: return 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2922 case LCT_RGBA: return 4;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2923 case LCT_MAX_OCTET_VALUE: return 0; /* invalid color type */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2924 default: return 0; /*invalid color type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2925 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2926 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2927
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2928 static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2929 /*bits per pixel is amount of channels * bits per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2930 return getNumColorChannels(colortype) * bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2931 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2932
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2933 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2934
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2935 void lodepng_color_mode_init(LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2936 info->key_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2937 info->key_r = info->key_g = info->key_b = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2938 info->colortype = LCT_RGBA;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2939 info->bitdepth = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2940 info->palette = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2941 info->palettesize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2942 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2943
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2944 /*allocates palette memory if needed, and initializes all colors to black*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2945 static void lodepng_color_mode_alloc_palette(LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2946 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2947 /*if the palette is already allocated, it will have size 1024 so no reallocation needed in that case*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2948 /*the palette must have room for up to 256 colors with 4 bytes each.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2949 if(!info->palette) info->palette = (unsigned char*)lodepng_malloc(1024);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2950 if(!info->palette) return; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2951 for(i = 0; i != 256; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2952 /*Initialize all unused colors with black, the value used for invalid palette indices.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2953 This is an error according to the PNG spec, but common PNG decoders make it black instead.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2954 That makes color conversion slightly faster due to no error handling needed.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2955 info->palette[i * 4 + 0] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2956 info->palette[i * 4 + 1] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2957 info->palette[i * 4 + 2] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2958 info->palette[i * 4 + 3] = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2959 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2960 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2961
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2962 void lodepng_color_mode_cleanup(LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2963 lodepng_palette_clear(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2964 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2965
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2966 unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2967 lodepng_color_mode_cleanup(dest);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2968 lodepng_memcpy(dest, source, sizeof(LodePNGColorMode));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2969 if(source->palette) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2970 dest->palette = (unsigned char*)lodepng_malloc(1024);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2971 if(!dest->palette && source->palettesize) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2972 lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2973 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2974 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2975 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2976
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2977 LodePNGColorMode lodepng_color_mode_make(LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2978 LodePNGColorMode result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2979 lodepng_color_mode_init(&result);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2980 result.colortype = colortype;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2981 result.bitdepth = bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2982 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2983 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2984
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2985 static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2986 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2987 if(a->colortype != b->colortype) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2988 if(a->bitdepth != b->bitdepth) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2989 if(a->key_defined != b->key_defined) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2990 if(a->key_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2991 if(a->key_r != b->key_r) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2992 if(a->key_g != b->key_g) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2993 if(a->key_b != b->key_b) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2994 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2995 if(a->palettesize != b->palettesize) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2996 for(i = 0; i != a->palettesize * 4; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2997 if(a->palette[i] != b->palette[i]) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2998 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
2999 return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3000 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3001
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3002 void lodepng_palette_clear(LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3003 if(info->palette) lodepng_free(info->palette);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3004 info->palette = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3005 info->palettesize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3006 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3007
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3008 unsigned lodepng_palette_add(LodePNGColorMode* info,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3009 unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3010 if(!info->palette) /*allocate palette if empty*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3011 lodepng_color_mode_alloc_palette(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3012 if(!info->palette) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3013 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3014 if(info->palettesize >= 256) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3015 return 108; /*too many palette values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3016 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3017 info->palette[4 * info->palettesize + 0] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3018 info->palette[4 * info->palettesize + 1] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3019 info->palette[4 * info->palettesize + 2] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3020 info->palette[4 * info->palettesize + 3] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3021 ++info->palettesize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3022 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3023 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3024
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3025 /*calculate bits per pixel out of colortype and bitdepth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3026 unsigned lodepng_get_bpp(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3027 return lodepng_get_bpp_lct(info->colortype, info->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3028 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3029
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3030 unsigned lodepng_get_channels(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3031 return getNumColorChannels(info->colortype);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3032 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3033
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3034 unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3035 return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3036 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3037
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3038 unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3039 return (info->colortype & 4) != 0; /*4 or 6*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3040 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3041
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3042 unsigned lodepng_is_palette_type(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3043 return info->colortype == LCT_PALETTE;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3044 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3045
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3046 unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3047 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3048 for(i = 0; i != info->palettesize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3049 if(info->palette[i * 4 + 3] < 255) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3050 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3051 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3052 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3053
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3054 unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3055 return info->key_defined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3056 || lodepng_is_alpha_type(info)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3057 || lodepng_has_palette_alpha(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3058 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3059
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3060 static size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3061 size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3062 size_t n = (size_t)w * (size_t)h;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3063 return ((n / 8u) * bpp) + ((n & 7u) * bpp + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3064 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3065
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3066 size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3067 return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3068 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3069
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3070
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3071 #ifdef LODEPNG_COMPILE_PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3072
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3073 /*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3074 and in addition has one extra byte per line: the filter byte. So this gives a larger
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3075 result than lodepng_get_raw_size. Set h to 1 to get the size of 1 row including filter byte. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3076 static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, unsigned bpp) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3077 /* + 1 for the filter byte, and possibly plus padding bits per line. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3078 /* Ignoring casts, the expression is equal to (w * bpp + 7) / 8 + 1, but avoids overflow of w * bpp */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3079 size_t line = ((size_t)(w / 8u) * bpp) + 1u + ((w & 7u) * bpp + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3080 return (size_t)h * line;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3081 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3082
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3083 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3084 /*Safely checks whether size_t overflow can be caused due to amount of pixels.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3085 This check is overcautious rather than precise. If this check indicates no overflow,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3086 you can safely compute in a size_t (but not an unsigned):
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3087 -(size_t)w * (size_t)h * 8
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3088 -amount of bytes in IDAT (including filter, padding and Adam7 bytes)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3089 -amount of bytes in raw color model
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3090 Returns 1 if overflow possible, 0 if not.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3091 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3092 static int lodepng_pixel_overflow(unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3093 const LodePNGColorMode* pngcolor, const LodePNGColorMode* rawcolor) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3094 size_t bpp = LODEPNG_MAX(lodepng_get_bpp(pngcolor), lodepng_get_bpp(rawcolor));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3095 size_t numpixels, total;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3096 size_t line; /* bytes per line in worst case */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3097
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3098 if(lodepng_mulofl((size_t)w, (size_t)h, &numpixels)) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3099 if(lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per channel color */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3100
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3101 /* Bytes per scanline with the expression "(w / 8u) * bpp) + ((w & 7u) * bpp + 7u) / 8u" */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3102 if(lodepng_mulofl((size_t)(w / 8u), bpp, &line)) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3103 if(lodepng_addofl(line, ((w & 7u) * bpp + 7u) / 8u, &line)) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3104
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3105 if(lodepng_addofl(line, 5, &line)) return 1; /* 5 bytes overhead per line: 1 filterbyte, 4 for Adam7 worst case */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3106 if(lodepng_mulofl(line, h, &total)) return 1; /* Total bytes in worst case */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3107
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3108 return 0; /* no overflow */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3109 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3110 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3111 #endif /*LODEPNG_COMPILE_PNG*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3112
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3113 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3114
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3115 static void LodePNGUnknownChunks_init(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3116 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3117 for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3118 for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3119 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3120
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3121 static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3122 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3123 for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3124 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3125
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3126 static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3127 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3128
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3129 LodePNGUnknownChunks_cleanup(dest);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3130
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3131 for(i = 0; i != 3; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3132 size_t j;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3133 dest->unknown_chunks_size[i] = src->unknown_chunks_size[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3134 dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3135 if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3136 for(j = 0; j < src->unknown_chunks_size[i]; ++j) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3137 dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3138 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3139 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3140
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3141 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3142 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3143
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3144 /******************************************************************************/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3145
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3146 static void LodePNGText_init(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3147 info->text_num = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3148 info->text_keys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3149 info->text_strings = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3150 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3151
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3152 static void LodePNGText_cleanup(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3153 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3154 for(i = 0; i != info->text_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3155 string_cleanup(&info->text_keys[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3156 string_cleanup(&info->text_strings[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3157 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3158 lodepng_free(info->text_keys);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3159 lodepng_free(info->text_strings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3160 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3161
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3162 static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3163 size_t i = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3164 dest->text_keys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3165 dest->text_strings = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3166 dest->text_num = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3167 for(i = 0; i != source->text_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3168 CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i]));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3169 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3170 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3171 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3172
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3173 static unsigned lodepng_add_text_sized(LodePNGInfo* info, const char* key, const char* str, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3174 char** new_keys = (char**)(lodepng_realloc(info->text_keys, sizeof(char*) * (info->text_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3175 char** new_strings = (char**)(lodepng_realloc(info->text_strings, sizeof(char*) * (info->text_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3176
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3177 if(new_keys) info->text_keys = new_keys;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3178 if(new_strings) info->text_strings = new_strings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3179
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3180 if(!new_keys || !new_strings) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3181
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3182 ++info->text_num;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3183 info->text_keys[info->text_num - 1] = alloc_string(key);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3184 info->text_strings[info->text_num - 1] = alloc_string_sized(str, size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3185 if(!info->text_keys[info->text_num - 1] || !info->text_strings[info->text_num - 1]) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3186
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3187 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3188 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3189
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3190 unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3191 return lodepng_add_text_sized(info, key, str, lodepng_strlen(str));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3192 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3193
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3194 void lodepng_clear_text(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3195 LodePNGText_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3196 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3197
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3198 /******************************************************************************/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3199
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3200 static void LodePNGIText_init(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3201 info->itext_num = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3202 info->itext_keys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3203 info->itext_langtags = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3204 info->itext_transkeys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3205 info->itext_strings = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3206 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3207
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3208 static void LodePNGIText_cleanup(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3209 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3210 for(i = 0; i != info->itext_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3211 string_cleanup(&info->itext_keys[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3212 string_cleanup(&info->itext_langtags[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3213 string_cleanup(&info->itext_transkeys[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3214 string_cleanup(&info->itext_strings[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3215 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3216 lodepng_free(info->itext_keys);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3217 lodepng_free(info->itext_langtags);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3218 lodepng_free(info->itext_transkeys);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3219 lodepng_free(info->itext_strings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3220 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3221
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3222 static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3223 size_t i = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3224 dest->itext_keys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3225 dest->itext_langtags = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3226 dest->itext_transkeys = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3227 dest->itext_strings = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3228 dest->itext_num = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3229 for(i = 0; i != source->itext_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3230 CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3231 source->itext_transkeys[i], source->itext_strings[i]));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3232 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3233 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3234 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3235
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3236 void lodepng_clear_itext(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3237 LodePNGIText_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3238 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3239
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3240 static unsigned lodepng_add_itext_sized(LodePNGInfo* info, const char* key, const char* langtag,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3241 const char* transkey, const char* str, size_t size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3242 char** new_keys = (char**)(lodepng_realloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3243 char** new_langtags = (char**)(lodepng_realloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3244 char** new_transkeys = (char**)(lodepng_realloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3245 char** new_strings = (char**)(lodepng_realloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1)));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3246
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3247 if(new_keys) info->itext_keys = new_keys;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3248 if(new_langtags) info->itext_langtags = new_langtags;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3249 if(new_transkeys) info->itext_transkeys = new_transkeys;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3250 if(new_strings) info->itext_strings = new_strings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3251
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3252 if(!new_keys || !new_langtags || !new_transkeys || !new_strings) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3253
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3254 ++info->itext_num;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3255
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3256 info->itext_keys[info->itext_num - 1] = alloc_string(key);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3257 info->itext_langtags[info->itext_num - 1] = alloc_string(langtag);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3258 info->itext_transkeys[info->itext_num - 1] = alloc_string(transkey);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3259 info->itext_strings[info->itext_num - 1] = alloc_string_sized(str, size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3260
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3261 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3262 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3263
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3264 unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3265 const char* transkey, const char* str) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3266 return lodepng_add_itext_sized(info, key, langtag, transkey, str, lodepng_strlen(str));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3267 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3268
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3269 /* same as set but does not delete */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3270 static unsigned lodepng_assign_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3271 if(profile_size == 0) return 100; /*invalid ICC profile size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3272
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3273 info->iccp_name = alloc_string(name);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3274 info->iccp_profile = (unsigned char*)lodepng_malloc(profile_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3275
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3276 if(!info->iccp_name || !info->iccp_profile) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3277
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3278 lodepng_memcpy(info->iccp_profile, profile, profile_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3279 info->iccp_profile_size = profile_size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3280
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3281 return 0; /*ok*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3282 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3283
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3284 unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3285 if(info->iccp_name) lodepng_clear_icc(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3286 info->iccp_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3287
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3288 return lodepng_assign_icc(info, name, profile, profile_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3289 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3290
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3291 void lodepng_clear_icc(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3292 string_cleanup(&info->iccp_name);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3293 lodepng_free(info->iccp_profile);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3294 info->iccp_profile = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3295 info->iccp_profile_size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3296 info->iccp_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3297 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3298 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3299
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3300 void lodepng_info_init(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3301 lodepng_color_mode_init(&info->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3302 info->interlace_method = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3303 info->compression_method = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3304 info->filter_method = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3305 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3306 info->background_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3307 info->background_r = info->background_g = info->background_b = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3308
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3309 LodePNGText_init(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3310 LodePNGIText_init(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3311
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3312 info->time_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3313 info->phys_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3314
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3315 info->gama_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3316 info->chrm_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3317 info->srgb_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3318 info->iccp_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3319 info->iccp_name = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3320 info->iccp_profile = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3321
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3322 info->sbit_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3323 info->sbit_r = info->sbit_g = info->sbit_b = info->sbit_a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3324
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3325 LodePNGUnknownChunks_init(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3326 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3327 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3328
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3329 void lodepng_info_cleanup(LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3330 lodepng_color_mode_cleanup(&info->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3331 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3332 LodePNGText_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3333 LodePNGIText_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3334
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3335 lodepng_clear_icc(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3336
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3337 LodePNGUnknownChunks_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3338 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3339 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3340
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3341 unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3342 lodepng_info_cleanup(dest);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3343 lodepng_memcpy(dest, source, sizeof(LodePNGInfo));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3344 lodepng_color_mode_init(&dest->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3345 CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3346
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3347 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3348 CERROR_TRY_RETURN(LodePNGText_copy(dest, source));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3349 CERROR_TRY_RETURN(LodePNGIText_copy(dest, source));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3350 if(source->iccp_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3351 CERROR_TRY_RETURN(lodepng_assign_icc(dest, source->iccp_name, source->iccp_profile, source->iccp_profile_size));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3352 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3353
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3354 LodePNGUnknownChunks_init(dest);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3355 CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3356 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3357 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3358 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3359
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3360 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3361
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3362 /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3363 static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3364 unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3365 /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3366 unsigned p = index & m;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3367 in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3368 in = in << (bits * (m - p));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3369 if(p == 0) out[index * bits / 8u] = in;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3370 else out[index * bits / 8u] |= in;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3371 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3372
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3373 typedef struct ColorTree ColorTree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3374
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3375 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3376 One node of a color tree
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3377 This is the data structure used to count the number of unique colors and to get a palette
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3378 index for a color. It's like an octree, but because the alpha channel is used too, each
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3379 node has 16 instead of 8 children.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3380 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3381 struct ColorTree {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3382 ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3383 int index; /*the payload. Only has a meaningful value if this is in the last level*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3384 };
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3385
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3386 static void color_tree_init(ColorTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3387 lodepng_memset(tree->children, 0, 16 * sizeof(*tree->children));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3388 tree->index = -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3389 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3390
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3391 static void color_tree_cleanup(ColorTree* tree) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3392 int i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3393 for(i = 0; i != 16; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3394 if(tree->children[i]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3395 color_tree_cleanup(tree->children[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3396 lodepng_free(tree->children[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3397 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3398 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3399 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3400
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3401 /*returns -1 if color not present, its index otherwise*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3402 static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3403 int bit = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3404 for(bit = 0; bit < 8; ++bit) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3405 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3406 if(!tree->children[i]) return -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3407 else tree = tree->children[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3408 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3409 return tree ? tree->index : -1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3410 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3411
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3412 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3413 static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3414 return color_tree_get(tree, r, g, b, a) >= 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3415 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3416 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3417
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3418 /*color is not allowed to already exist.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3419 Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3420 Returns error code, or 0 if ok*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3421 static unsigned color_tree_add(ColorTree* tree,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3422 unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3423 int bit;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3424 for(bit = 0; bit < 8; ++bit) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3425 int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3426 if(!tree->children[i]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3427 tree->children[i] = (ColorTree*)lodepng_malloc(sizeof(ColorTree));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3428 if(!tree->children[i]) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3429 color_tree_init(tree->children[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3430 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3431 tree = tree->children[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3432 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3433 tree->index = (int)index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3434 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3435 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3436
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3437 /*put a pixel, given its RGBA color, into image of any color type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3438 static unsigned rgba8ToPixel(unsigned char* out, size_t i,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3439 const LodePNGColorMode* mode, ColorTree* tree /*for palette*/,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3440 unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3441 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3442 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3443 if(mode->bitdepth == 8) out[i] = gray;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3444 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3445 else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3446 /*take the most significant bits of gray*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3447 gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3448 addColorBits(out, i, mode->bitdepth, gray);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3449 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3450 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3451 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3452 out[i * 3 + 0] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3453 out[i * 3 + 1] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3454 out[i * 3 + 2] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3455 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3456 out[i * 6 + 0] = out[i * 6 + 1] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3457 out[i * 6 + 2] = out[i * 6 + 3] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3458 out[i * 6 + 4] = out[i * 6 + 5] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3459 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3460 } else if(mode->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3461 int index = color_tree_get(tree, r, g, b, a);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3462 if(index < 0) return 82; /*color not in palette*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3463 if(mode->bitdepth == 8) out[i] = index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3464 else addColorBits(out, i, mode->bitdepth, (unsigned)index);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3465 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3466 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3467 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3468 out[i * 2 + 0] = gray;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3469 out[i * 2 + 1] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3470 } else if(mode->bitdepth == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3471 out[i * 4 + 0] = out[i * 4 + 1] = gray;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3472 out[i * 4 + 2] = out[i * 4 + 3] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3473 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3474 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3475 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3476 out[i * 4 + 0] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3477 out[i * 4 + 1] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3478 out[i * 4 + 2] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3479 out[i * 4 + 3] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3480 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3481 out[i * 8 + 0] = out[i * 8 + 1] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3482 out[i * 8 + 2] = out[i * 8 + 3] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3483 out[i * 8 + 4] = out[i * 8 + 5] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3484 out[i * 8 + 6] = out[i * 8 + 7] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3485 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3486 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3487
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3488 return 0; /*no error*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3489 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3490
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3491 /*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3492 static void rgba16ToPixel(unsigned char* out, size_t i,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3493 const LodePNGColorMode* mode,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3494 unsigned short r, unsigned short g, unsigned short b, unsigned short a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3495 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3496 unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3497 out[i * 2 + 0] = (gray >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3498 out[i * 2 + 1] = gray & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3499 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3500 out[i * 6 + 0] = (r >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3501 out[i * 6 + 1] = r & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3502 out[i * 6 + 2] = (g >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3503 out[i * 6 + 3] = g & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3504 out[i * 6 + 4] = (b >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3505 out[i * 6 + 5] = b & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3506 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3507 unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3508 out[i * 4 + 0] = (gray >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3509 out[i * 4 + 1] = gray & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3510 out[i * 4 + 2] = (a >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3511 out[i * 4 + 3] = a & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3512 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3513 out[i * 8 + 0] = (r >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3514 out[i * 8 + 1] = r & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3515 out[i * 8 + 2] = (g >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3516 out[i * 8 + 3] = g & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3517 out[i * 8 + 4] = (b >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3518 out[i * 8 + 5] = b & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3519 out[i * 8 + 6] = (a >> 8) & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3520 out[i * 8 + 7] = a & 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3521 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3522 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3523
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3524 /*Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3525 static void getPixelColorRGBA8(unsigned char* r, unsigned char* g,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3526 unsigned char* b, unsigned char* a,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3527 const unsigned char* in, size_t i,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3528 const LodePNGColorMode* mode) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3529 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3530 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3531 *r = *g = *b = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3532 if(mode->key_defined && *r == mode->key_r) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3533 else *a = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3534 } else if(mode->bitdepth == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3535 *r = *g = *b = in[i * 2 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3536 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3537 else *a = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3538 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3539 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3540 size_t j = i * mode->bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3541 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3542 *r = *g = *b = (value * 255) / highest;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3543 if(mode->key_defined && value == mode->key_r) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3544 else *a = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3545 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3546 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3547 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3548 *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3549 if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3550 else *a = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3551 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3552 *r = in[i * 6 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3553 *g = in[i * 6 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3554 *b = in[i * 6 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3555 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3556 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3557 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3558 else *a = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3559 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3560 } else if(mode->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3561 unsigned index;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3562 if(mode->bitdepth == 8) index = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3563 else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3564 size_t j = i * mode->bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3565 index = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3566 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3567 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3568 *r = mode->palette[index * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3569 *g = mode->palette[index * 4 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3570 *b = mode->palette[index * 4 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3571 *a = mode->palette[index * 4 + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3572 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3573 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3574 *r = *g = *b = in[i * 2 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3575 *a = in[i * 2 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3576 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3577 *r = *g = *b = in[i * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3578 *a = in[i * 4 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3579 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3580 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3581 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3582 *r = in[i * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3583 *g = in[i * 4 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3584 *b = in[i * 4 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3585 *a = in[i * 4 + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3586 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3587 *r = in[i * 8 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3588 *g = in[i * 8 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3589 *b = in[i * 8 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3590 *a = in[i * 8 + 6];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3591 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3592 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3593 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3594
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3595 /*Similar to getPixelColorRGBA8, but with all the for loops inside of the color
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3596 mode test cases, optimized to convert the colors much faster, when converting
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3597 to the common case of RGBA with 8 bit per channel. buffer must be RGBA with
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3598 enough memory.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3599 static void getPixelColorsRGBA8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3600 const unsigned char* LODEPNG_RESTRICT in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3601 const LodePNGColorMode* mode) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3602 unsigned num_channels = 4;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3603 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3604 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3605 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3606 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3607 buffer[0] = buffer[1] = buffer[2] = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3608 buffer[3] = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3609 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3610 if(mode->key_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3611 buffer -= numpixels * num_channels;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3612 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3613 if(buffer[0] == mode->key_r) buffer[3] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3614 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3615 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3616 } else if(mode->bitdepth == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3617 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3618 buffer[0] = buffer[1] = buffer[2] = in[i * 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3619 buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3620 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3621 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3622 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3623 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3624 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3625 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3626 buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3627 buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3628 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3629 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3630 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3631 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3632 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3633 lodepng_memcpy(buffer, &in[i * 3], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3634 buffer[3] = 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3635 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3636 if(mode->key_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3637 buffer -= numpixels * num_channels;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3638 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3639 if(buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3640 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3641 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3642 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3643 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3644 buffer[0] = in[i * 6 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3645 buffer[1] = in[i * 6 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3646 buffer[2] = in[i * 6 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3647 buffer[3] = mode->key_defined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3648 && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3649 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3650 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3651 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3652 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3653 } else if(mode->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3654 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3655 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3656 unsigned index = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3657 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3658 lodepng_memcpy(buffer, &mode->palette[index * 4], 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3659 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3660 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3661 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3662 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3663 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3664 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3665 lodepng_memcpy(buffer, &mode->palette[index * 4], 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3666 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3667 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3668 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3669 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3670 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3671 buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3672 buffer[3] = in[i * 2 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3673 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3674 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3675 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3676 buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3677 buffer[3] = in[i * 4 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3678 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3679 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3680 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3681 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3682 lodepng_memcpy(buffer, in, numpixels * 4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3683 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3684 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3685 buffer[0] = in[i * 8 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3686 buffer[1] = in[i * 8 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3687 buffer[2] = in[i * 8 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3688 buffer[3] = in[i * 8 + 6];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3689 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3690 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3691 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3692 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3693
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3694 /*Similar to getPixelColorsRGBA8, but with 3-channel RGB output.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3695 static void getPixelColorsRGB8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3696 const unsigned char* LODEPNG_RESTRICT in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3697 const LodePNGColorMode* mode) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3698 const unsigned num_channels = 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3699 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3700 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3701 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3702 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3703 buffer[0] = buffer[1] = buffer[2] = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3704 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3705 } else if(mode->bitdepth == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3706 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3707 buffer[0] = buffer[1] = buffer[2] = in[i * 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3708 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3709 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3710 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3711 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3712 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3713 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3714 buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3715 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3716 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3717 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3718 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3719 lodepng_memcpy(buffer, in, numpixels * 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3720 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3721 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3722 buffer[0] = in[i * 6 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3723 buffer[1] = in[i * 6 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3724 buffer[2] = in[i * 6 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3725 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3726 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3727 } else if(mode->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3728 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3729 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3730 unsigned index = in[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3731 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3732 lodepng_memcpy(buffer, &mode->palette[index * 4], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3733 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3734 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3735 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3736 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3737 unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3738 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3739 lodepng_memcpy(buffer, &mode->palette[index * 4], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3740 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3741 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3742 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3743 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3744 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3745 buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3746 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3747 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3748 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3749 buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3750 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3751 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3752 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3753 if(mode->bitdepth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3754 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3755 lodepng_memcpy(buffer, &in[i * 4], 3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3756 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3757 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3758 for(i = 0; i != numpixels; ++i, buffer += num_channels) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3759 buffer[0] = in[i * 8 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3760 buffer[1] = in[i * 8 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3761 buffer[2] = in[i * 8 + 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3762 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3763 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3764 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3765 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3766
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3767 /*Get RGBA16 color of pixel with index i (y * width + x) from the raw image with
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3768 given color type, but the given color type must be 16-bit itself.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3769 static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3770 const unsigned char* in, size_t i, const LodePNGColorMode* mode) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3771 if(mode->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3772 *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3773 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3774 else *a = 65535;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3775 } else if(mode->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3776 *r = 256u * in[i * 6 + 0] + in[i * 6 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3777 *g = 256u * in[i * 6 + 2] + in[i * 6 + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3778 *b = 256u * in[i * 6 + 4] + in[i * 6 + 5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3779 if(mode->key_defined
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3780 && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3781 && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3782 && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3783 else *a = 65535;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3784 } else if(mode->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3785 *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3786 *a = 256u * in[i * 4 + 2] + in[i * 4 + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3787 } else if(mode->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3788 *r = 256u * in[i * 8 + 0] + in[i * 8 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3789 *g = 256u * in[i * 8 + 2] + in[i * 8 + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3790 *b = 256u * in[i * 8 + 4] + in[i * 8 + 5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3791 *a = 256u * in[i * 8 + 6] + in[i * 8 + 7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3792 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3793 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3794
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3795 unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3796 const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3797 unsigned w, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3798 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3799 ColorTree tree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3800 size_t numpixels = (size_t)w * (size_t)h;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3801 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3802
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3803 if(mode_in->colortype == LCT_PALETTE && !mode_in->palette) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3804 return 107; /* error: must provide palette if input mode is palette */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3805 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3806
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3807 if(lodepng_color_mode_equal(mode_out, mode_in)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3808 size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3809 lodepng_memcpy(out, in, numbytes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3810 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3811 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3812
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3813 if(mode_out->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3814 size_t palettesize = mode_out->palettesize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3815 const unsigned char* palette = mode_out->palette;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3816 size_t palsize = (size_t)1u << mode_out->bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3817 /*if the user specified output palette but did not give the values, assume
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3818 they want the values of the input color type (assuming that one is palette).
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3819 Note that we never create a new palette ourselves.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3820 if(palettesize == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3821 palettesize = mode_in->palettesize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3822 palette = mode_in->palette;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3823 /*if the input was also palette with same bitdepth, then the color types are also
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3824 equal, so copy literally. This to preserve the exact indices that were in the PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3825 even in case there are duplicate colors in the palette.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3826 if(mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3827 size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3828 lodepng_memcpy(out, in, numbytes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3829 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3830 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3831 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3832 if(palettesize < palsize) palsize = palettesize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3833 color_tree_init(&tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3834 for(i = 0; i != palsize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3835 const unsigned char* p = &palette[i * 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3836 error = color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned)i);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3837 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3838 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3839 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3840
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3841 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3842 if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3843 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3844 unsigned short r = 0, g = 0, b = 0, a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3845 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3846 rgba16ToPixel(out, i, mode_out, r, g, b, a);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3847 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3848 } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3849 getPixelColorsRGBA8(out, numpixels, in, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3850 } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3851 getPixelColorsRGB8(out, numpixels, in, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3852 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3853 unsigned char r = 0, g = 0, b = 0, a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3854 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3855 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3856 error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3857 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3858 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3859 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3860 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3861
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3862 if(mode_out->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3863 color_tree_cleanup(&tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3864 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3865
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3866 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3867 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3868
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3869
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3870 /* Converts a single rgb color without alpha from one type to another, color bits truncated to
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3871 their bitdepth. In case of single channel (gray or palette), only the r channel is used. Slow
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3872 function, do not use to process all pixels of an image. Alpha channel not supported on purpose:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3873 this is for bKGD, supporting alpha may prevent it from finding a color in the palette, from the
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3874 specification it looks like bKGD should ignore the alpha values of the palette since it can use
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3875 any palette index but doesn't have an alpha channel. Idem with ignoring color key. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3876 unsigned lodepng_convert_rgb(
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3877 unsigned* r_out, unsigned* g_out, unsigned* b_out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3878 unsigned r_in, unsigned g_in, unsigned b_in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3879 const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3880 unsigned r = 0, g = 0, b = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3881 unsigned mul = 65535 / ((1u << mode_in->bitdepth) - 1u); /*65535, 21845, 4369, 257, 1*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3882 unsigned shift = 16 - mode_out->bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3883
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3884 if(mode_in->colortype == LCT_GREY || mode_in->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3885 r = g = b = r_in * mul;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3886 } else if(mode_in->colortype == LCT_RGB || mode_in->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3887 r = r_in * mul;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3888 g = g_in * mul;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3889 b = b_in * mul;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3890 } else if(mode_in->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3891 if(r_in >= mode_in->palettesize) return 82;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3892 r = mode_in->palette[r_in * 4 + 0] * 257u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3893 g = mode_in->palette[r_in * 4 + 1] * 257u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3894 b = mode_in->palette[r_in * 4 + 2] * 257u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3895 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3896 return 31;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3897 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3898
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3899 /* now convert to output format */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3900 if(mode_out->colortype == LCT_GREY || mode_out->colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3901 *r_out = r >> shift ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3902 } else if(mode_out->colortype == LCT_RGB || mode_out->colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3903 *r_out = r >> shift ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3904 *g_out = g >> shift ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3905 *b_out = b >> shift ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3906 } else if(mode_out->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3907 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3908 /* a 16-bit color cannot be in the palette */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3909 if((r >> 8) != (r & 255) || (g >> 8) != (g & 255) || (b >> 8) != (b & 255)) return 82;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3910 for(i = 0; i < mode_out->palettesize; i++) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3911 unsigned j = i * 4;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3912 if((r >> 8) == mode_out->palette[j + 0] && (g >> 8) == mode_out->palette[j + 1] &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3913 (b >> 8) == mode_out->palette[j + 2]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3914 *r_out = i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3915 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3916 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3917 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3918 return 82;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3919 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3920 return 31;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3921 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3922
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3923 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3924 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3925
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3926 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3927
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3928 void lodepng_color_stats_init(LodePNGColorStats* stats) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3929 /*stats*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3930 stats->colored = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3931 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3932 stats->key_r = stats->key_g = stats->key_b = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3933 stats->alpha = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3934 stats->numcolors = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3935 stats->bits = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3936 stats->numpixels = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3937 /*settings*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3938 stats->allow_palette = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3939 stats->allow_greyscale = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3940 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3941
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3942 /*function used for debug purposes with C++*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3943 /*void printColorStats(LodePNGColorStats* p) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3944 std::cout << "colored: " << (int)p->colored << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3945 std::cout << "key: " << (int)p->key << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3946 std::cout << "key_r: " << (int)p->key_r << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3947 std::cout << "key_g: " << (int)p->key_g << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3948 std::cout << "key_b: " << (int)p->key_b << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3949 std::cout << "alpha: " << (int)p->alpha << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3950 std::cout << "numcolors: " << (int)p->numcolors << ", ";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3951 std::cout << "bits: " << (int)p->bits << std::endl;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3952 }*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3953
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3954 /*Returns how many bits needed to represent given value (max 8 bit)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3955 static unsigned getValueRequiredBits(unsigned char value) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3956 if(value == 0 || value == 255) return 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3957 /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3958 if(value % 17 == 0) return value % 85 == 0 ? 2 : 4;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3959 return 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3960 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3961
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3962 /*stats must already have been inited. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3963 unsigned lodepng_compute_color_stats(LodePNGColorStats* stats,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3964 const unsigned char* in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3965 const LodePNGColorMode* mode_in) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3966 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3967 ColorTree tree;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3968 size_t numpixels = (size_t)w * (size_t)h;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3969 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3970
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3971 /* mark things as done already if it would be impossible to have a more expensive case */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3972 unsigned colored_done = lodepng_is_greyscale_type(mode_in) ? 1 : 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3973 unsigned alpha_done = lodepng_can_have_alpha(mode_in) ? 0 : 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3974 unsigned numcolors_done = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3975 unsigned bpp = lodepng_get_bpp(mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3976 unsigned bits_done = (stats->bits == 1 && bpp == 1) ? 1 : 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3977 unsigned sixteen = 0; /* whether the input image is 16 bit */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3978 unsigned maxnumcolors = 257;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3979 if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, stats->numcolors + (1u << bpp));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3980
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3981 stats->numpixels += numpixels;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3982
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3983 /*if palette not allowed, no need to compute numcolors*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3984 if(!stats->allow_palette) numcolors_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3985
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3986 color_tree_init(&tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3987
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3988 /*If the stats was already filled in from previous data, fill its palette in tree
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3989 and mark things as done already if we know they are the most expensive case already*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3990 if(stats->alpha) alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3991 if(stats->colored) colored_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3992 if(stats->bits == 16) numcolors_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3993 if(stats->bits >= bpp) bits_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3994 if(stats->numcolors >= maxnumcolors) numcolors_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3995
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3996 if(!numcolors_done) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3997 for(i = 0; i < stats->numcolors; i++) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3998 const unsigned char* color = &stats->palette[i * 4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
3999 error = color_tree_add(&tree, color[0], color[1], color[2], color[3], i);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4000 if(error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4001 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4002 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4003
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4004 /*Check if the 16-bit input is truly 16-bit*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4005 if(mode_in->bitdepth == 16 && !sixteen) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4006 unsigned short r = 0, g = 0, b = 0, a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4007 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4008 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4009 if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) ||
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4010 (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4011 stats->bits = 16;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4012 sixteen = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4013 bits_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4014 numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4015 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4016 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4017 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4018 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4019
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4020 if(sixteen) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4021 unsigned short r = 0, g = 0, b = 0, a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4022
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4023 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4024 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4025
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4026 if(!colored_done && (r != g || r != b)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4027 stats->colored = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4028 colored_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4029 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4030
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4031 if(!alpha_done) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4032 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4033 if(a != 65535 && (a != 0 || (stats->key && !matchkey))) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4034 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4035 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4036 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4037 } else if(a == 0 && !stats->alpha && !stats->key) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4038 stats->key = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4039 stats->key_r = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4040 stats->key_g = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4041 stats->key_b = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4042 } else if(a == 65535 && stats->key && matchkey) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4043 /* Color key cannot be used if an opaque pixel also has that RGB color. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4044 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4045 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4046 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4047 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4048 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4049 if(alpha_done && numcolors_done && colored_done && bits_done) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4050 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4051
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4052 if(stats->key && !stats->alpha) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4053 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4054 getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4055 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4056 /* Color key cannot be used if an opaque pixel also has that RGB color. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4057 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4058 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4059 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4060 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4061 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4062 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4063 } else /* < 16-bit */ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4064 unsigned char r = 0, g = 0, b = 0, a = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4065 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4066 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4067
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4068 if(!bits_done && stats->bits < 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4069 /*only r is checked, < 8 bits is only relevant for grayscale*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4070 unsigned bits = getValueRequiredBits(r);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4071 if(bits > stats->bits) stats->bits = bits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4072 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4073 bits_done = (stats->bits >= bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4074
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4075 if(!colored_done && (r != g || r != b)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4076 stats->colored = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4077 colored_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4078 if(stats->bits < 8) stats->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4079 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4080
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4081 if(!alpha_done) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4082 unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4083 if(a != 255 && (a != 0 || (stats->key && !matchkey))) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4084 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4085 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4086 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4087 if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4088 } else if(a == 0 && !stats->alpha && !stats->key) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4089 stats->key = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4090 stats->key_r = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4091 stats->key_g = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4092 stats->key_b = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4093 } else if(a == 255 && stats->key && matchkey) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4094 /* Color key cannot be used if an opaque pixel also has that RGB color. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4095 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4096 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4097 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4098 if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4099 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4100 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4101
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4102 if(!numcolors_done) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4103 if(!color_tree_has(&tree, r, g, b, a)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4104 error = color_tree_add(&tree, r, g, b, a, stats->numcolors);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4105 if(error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4106 if(stats->numcolors < 256) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4107 unsigned char* p = stats->palette;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4108 unsigned n = stats->numcolors;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4109 p[n * 4 + 0] = r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4110 p[n * 4 + 1] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4111 p[n * 4 + 2] = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4112 p[n * 4 + 3] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4113 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4114 ++stats->numcolors;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4115 numcolors_done = stats->numcolors >= maxnumcolors;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4116 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4117 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4118
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4119 if(alpha_done && numcolors_done && colored_done && bits_done) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4120 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4121
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4122 if(stats->key && !stats->alpha) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4123 for(i = 0; i != numpixels; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4124 getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4125 if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4126 /* Color key cannot be used if an opaque pixel also has that RGB color. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4127 stats->alpha = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4128 stats->key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4129 alpha_done = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4130 if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4131 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4132 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4133 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4134
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4135 /*make the stats's key always 16-bit for consistency - repeat each byte twice*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4136 stats->key_r += (stats->key_r << 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4137 stats->key_g += (stats->key_g << 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4138 stats->key_b += (stats->key_b << 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4139 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4140
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4141 cleanup:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4142 color_tree_cleanup(&tree);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4143 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4144 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4145
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4146 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4147 /*Adds a single color to the color stats. The stats must already have been inited. The color must be given as 16-bit
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4148 (with 2 bytes repeating for 8-bit and 65535 for opaque alpha channel). This function is expensive, do not call it for
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4149 all pixels of an image but only for a few additional values. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4150 static unsigned lodepng_color_stats_add(LodePNGColorStats* stats,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4151 unsigned r, unsigned g, unsigned b, unsigned a) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4152 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4153 unsigned char image[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4154 LodePNGColorMode mode;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4155 lodepng_color_mode_init(&mode);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4156 image[0] = r >> 8; image[1] = r; image[2] = g >> 8; image[3] = g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4157 image[4] = b >> 8; image[5] = b; image[6] = a >> 8; image[7] = a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4158 mode.bitdepth = 16;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4159 mode.colortype = LCT_RGBA;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4160 error = lodepng_compute_color_stats(stats, image, 1, 1, &mode);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4161 lodepng_color_mode_cleanup(&mode);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4162 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4163 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4164 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4165
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4166 /*Computes a minimal PNG color model that can contain all colors as indicated by the stats.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4167 The stats should be computed with lodepng_compute_color_stats.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4168 mode_in is raw color profile of the image the stats were computed on, to copy palette order from when relevant.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4169 Minimal PNG color model means the color type and bit depth that gives smallest amount of bits in the output image,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4170 e.g. gray if only grayscale pixels, palette if less than 256 colors, color key if only single transparent color, ...
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4171 This is used if auto_convert is enabled (it is by default).
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4172 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4173 static unsigned auto_choose_color(LodePNGColorMode* mode_out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4174 const LodePNGColorMode* mode_in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4175 const LodePNGColorStats* stats) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4176 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4177 unsigned palettebits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4178 size_t i, n;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4179 size_t numpixels = stats->numpixels;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4180 unsigned palette_ok, gray_ok;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4181
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4182 unsigned alpha = stats->alpha;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4183 unsigned key = stats->key;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4184 unsigned bits = stats->bits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4185
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4186 mode_out->key_defined = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4187
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4188 if(key && numpixels <= 16) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4189 alpha = 1; /*too few pixels to justify tRNS chunk overhead*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4190 key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4191 if(bits < 8) bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4192 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4193
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4194 gray_ok = !stats->colored;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4195 if(!stats->allow_greyscale) gray_ok = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4196 if(!gray_ok && bits < 8) bits = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4197
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4198 n = stats->numcolors;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4199 palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4200 palette_ok = n <= 256 && bits <= 8 && n != 0; /*n==0 means likely numcolors wasn't computed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4201 if(numpixels < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4202 if(gray_ok && !alpha && bits <= palettebits) palette_ok = 0; /*gray is less overhead*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4203 if(!stats->allow_palette) palette_ok = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4204
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4205 if(palette_ok) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4206 const unsigned char* p = stats->palette;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4207 lodepng_palette_clear(mode_out); /*remove potential earlier palette*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4208 for(i = 0; i != stats->numcolors; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4209 error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4210 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4211 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4212
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4213 mode_out->colortype = LCT_PALETTE;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4214 mode_out->bitdepth = palettebits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4215
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4216 if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4217 && mode_in->bitdepth == mode_out->bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4218 /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4219 lodepng_color_mode_cleanup(mode_out); /*clears palette, keeps the above set colortype and bitdepth fields as-is*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4220 lodepng_color_mode_copy(mode_out, mode_in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4221 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4222 } else /*8-bit or 16-bit per channel*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4223 mode_out->bitdepth = bits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4224 mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4225 : (gray_ok ? LCT_GREY : LCT_RGB);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4226 if(key) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4227 unsigned mask = (1u << mode_out->bitdepth) - 1u; /*stats always uses 16-bit, mask converts it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4228 mode_out->key_r = stats->key_r & mask;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4229 mode_out->key_g = stats->key_g & mask;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4230 mode_out->key_b = stats->key_b & mask;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4231 mode_out->key_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4232 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4233 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4234
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4235 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4236 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4237
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4238 #endif /* #ifdef LODEPNG_COMPILE_ENCODER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4239
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4240 /*Paeth predictor, used by PNG filter type 4*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4241 static unsigned char paethPredictor(unsigned char a, unsigned char b, unsigned char c) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4242 /* the subtractions of unsigned char cast it to a signed type.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4243 With gcc, short is faster than int, with clang int is as fast (as of april 2023)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4244 short pa = (b - c) < 0 ? -(b - c) : (b - c);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4245 short pb = (a - c) < 0 ? -(a - c) : (a - c);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4246 /* writing it out like this compiles to something faster than introducing a temp variable*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4247 short pc = (a + b - c - c) < 0 ? -(a + b - c - c) : (a + b - c - c);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4248 /* return input value associated with smallest of pa, pb, pc (with certain priority if equal) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4249 if(pb < pa) { a = b; pa = pb; }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4250 return (pc < pa) ? c : a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4251 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4252
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4253 /*shared values used by multiple Adam7 related functions*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4254
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4255 static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4256 static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4257 static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4258 static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4259
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4260 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4261 Outputs various dimensions and positions in the image related to the Adam7 reduced images.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4262 passw: output containing the width of the 7 passes
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4263 passh: output containing the height of the 7 passes
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4264 filter_passstart: output containing the index of the start and end of each
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4265 reduced image with filter bytes
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4266 padded_passstart output containing the index of the start and end of each
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4267 reduced image when without filter bytes but with padded scanlines
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4268 passstart: output containing the index of the start and end of each reduced
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4269 image without padding between scanlines, but still padding between the images
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4270 w, h: width and height of non-interlaced image
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4271 bpp: bits per pixel
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4272 "padded" is only relevant if bpp is less than 8 and a scanline or image does not
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4273 end at a full byte
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4274 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4275 static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4276 size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4277 /*the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4278 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4279
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4280 /*calculate width and height in pixels of each pass*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4281 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4282 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4283 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4284 if(passw[i] == 0) passh[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4285 if(passh[i] == 0) passw[i] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4286 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4287
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4288 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4289 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4290 /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4291 filter_passstart[i + 1] = filter_passstart[i]
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4292 + ((passw[i] && passh[i]) ? passh[i] * (1u + (passw[i] * bpp + 7u) / 8u) : 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4293 /*bits padded if needed to fill full byte at end of each scanline*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4294 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7u) / 8u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4295 /*only padded at end of reduced image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4296 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4297 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4298 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4299
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4300 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4301
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4302 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4303 /* / PNG Decoder / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4304 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4305
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4306 /*read the information from the header and store it in the LodePNGInfo. return value is error*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4307 unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4308 const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4309 unsigned width, height;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4310 LodePNGInfo* info = &state->info_png;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4311 if(insize == 0 || in == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4312 CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4313 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4314 if(insize < 33) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4315 CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4316 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4317
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4318 /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4319 /* TODO: remove this. One should use a new LodePNGState for new sessions */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4320 lodepng_info_cleanup(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4321 lodepng_info_init(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4322
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4323 if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4324 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4325 CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4326 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4327 if(lodepng_chunk_length(in + 8) != 13) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4328 CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4329 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4330 if(!lodepng_chunk_type_equals(in + 8, "IHDR")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4331 CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4332 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4333
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4334 /*read the values given in the header*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4335 width = lodepng_read32bitInt(&in[16]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4336 height = lodepng_read32bitInt(&in[20]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4337 /*TODO: remove the undocumented feature that allows to give null pointers to width or height*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4338 if(w) *w = width;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4339 if(h) *h = height;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4340 info->color.bitdepth = in[24];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4341 info->color.colortype = (LodePNGColorType)in[25];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4342 info->compression_method = in[26];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4343 info->filter_method = in[27];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4344 info->interlace_method = in[28];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4345
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4346 /*errors returned only after the parsing so other values are still output*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4347
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4348 /*error: invalid image size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4349 if(width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4350 /*error: invalid colortype or bitdepth combination*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4351 state->error = checkColorValidity(info->color.colortype, info->color.bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4352 if(state->error) return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4353 /*error: only compression method 0 is allowed in the specification*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4354 if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4355 /*error: only filter method 0 is allowed in the specification*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4356 if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4357 /*error: only interlace methods 0 and 1 exist in the specification*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4358 if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4359
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4360 if(!state->decoder.ignore_crc) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4361 unsigned CRC = lodepng_read32bitInt(&in[29]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4362 unsigned checksum = lodepng_crc32(&in[12], 17);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4363 if(CRC != checksum) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4364 CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4365 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4366 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4367
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4368 return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4369 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4370
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4371 static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4372 size_t bytewidth, unsigned char filterType, size_t length) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4373 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4374 For PNG filter method 0
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4375 unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4376 the filter works byte per byte (bytewidth = 1)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4377 precon is the previous unfiltered scanline, recon the result, scanline the current one
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4378 the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4379 recon and scanline MAY be the same memory address! precon must be disjoint.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4380 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4381
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4382 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4383 switch(filterType) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4384 case 0:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4385 for(i = 0; i != length; ++i) recon[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4386 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4387 case 1: {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4388 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4389 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4390 for(i = bytewidth; i != length; ++i, ++j) recon[i] = scanline[i] + recon[j];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4391 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4392 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4393 case 2:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4394 if(precon) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4395 for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4396 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4397 for(i = 0; i != length; ++i) recon[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4398 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4399 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4400 case 3:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4401 if(precon) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4402 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4403 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4404 /* Unroll independent paths of this predictor. A 6x and 8x version is also possible but that adds
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4405 too much code. Whether this speeds up anything depends on compiler and settings. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4406 if(bytewidth >= 4) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4407 for(; i + 3 < length; i += 4, j += 4) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4408 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2], s3 = scanline[i + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4409 unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2], r3 = recon[j + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4410 unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2], p3 = precon[i + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4411 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4412 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4413 recon[i + 2] = s2 + ((r2 + p2) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4414 recon[i + 3] = s3 + ((r3 + p3) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4415 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4416 } else if(bytewidth >= 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4417 for(; i + 2 < length; i += 3, j += 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4418 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4419 unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4420 unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4421 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4422 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4423 recon[i + 2] = s2 + ((r2 + p2) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4424 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4425 } else if(bytewidth >= 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4426 for(; i + 1 < length; i += 2, j += 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4427 unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4428 unsigned char r0 = recon[j + 0], r1 = recon[j + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4429 unsigned char p0 = precon[i + 0], p1 = precon[i + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4430 recon[i + 0] = s0 + ((r0 + p0) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4431 recon[i + 1] = s1 + ((r1 + p1) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4432 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4433 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4434 for(; i != length; ++i, ++j) recon[i] = scanline[i] + ((recon[j] + precon[i]) >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4435 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4436 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4437 for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4438 for(i = bytewidth; i != length; ++i, ++j) recon[i] = scanline[i] + (recon[j] >> 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4439 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4440 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4441 case 4:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4442 if(precon) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4443 /* Unroll independent paths of this predictor. Whether this speeds up
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4444 anything depends on compiler and settings. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4445 if(bytewidth == 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4446 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4447 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4448 unsigned char a4, b4 = 0, c4, d4 = 0, a5, b5 = 0, c5, d5 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4449 unsigned char a6, b6 = 0, c6, d6 = 0, a7, b7 = 0, c7, d7 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4450 for(i = 0; i + 7 < length; i += 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4451 c0 = b0; c1 = b1; c2 = b2; c3 = b3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4452 c4 = b4; c5 = b5; c6 = b6; c7 = b7;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4453 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2]; b3 = precon[i + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4454 b4 = precon[i + 4]; b5 = precon[i + 5]; b6 = precon[i + 6]; b7 = precon[i + 7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4455 a0 = d0; a1 = d1; a2 = d2; a3 = d3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4456 a4 = d4; a5 = d5; a6 = d6; a7 = d7;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4457 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4458 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4459 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4460 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4461 d4 = scanline[i + 4] + paethPredictor(a4, b4, c4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4462 d5 = scanline[i + 5] + paethPredictor(a5, b5, c5);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4463 d6 = scanline[i + 6] + paethPredictor(a6, b6, c6);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4464 d7 = scanline[i + 7] + paethPredictor(a7, b7, c7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4465 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2; recon[i + 3] = d3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4466 recon[i + 4] = d4; recon[i + 5] = d5; recon[i + 6] = d6; recon[i + 7] = d7;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4467 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4468 } else if(bytewidth == 6) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4469 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4470 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4471 unsigned char a4, b4 = 0, c4, d4 = 0, a5, b5 = 0, c5, d5 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4472 for(i = 0; i + 5 < length; i += 6) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4473 c0 = b0; c1 = b1; c2 = b2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4474 c3 = b3; c4 = b4; c5 = b5;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4475 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4476 b3 = precon[i + 3]; b4 = precon[i + 4]; b5 = precon[i + 5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4477 a0 = d0; a1 = d1; a2 = d2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4478 a3 = d3; a4 = d4; a5 = d5;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4479 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4480 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4481 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4482 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4483 d4 = scanline[i + 4] + paethPredictor(a4, b4, c4);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4484 d5 = scanline[i + 5] + paethPredictor(a5, b5, c5);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4485 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4486 recon[i + 3] = d3; recon[i + 4] = d4; recon[i + 5] = d5;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4487 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4488 } else if(bytewidth == 4) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4489 unsigned char a0, b0 = 0, c0, d0 = 0, a1, b1 = 0, c1, d1 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4490 unsigned char a2, b2 = 0, c2, d2 = 0, a3, b3 = 0, c3, d3 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4491 for(i = 0; i + 3 < length; i += 4) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4492 c0 = b0; c1 = b1; c2 = b2; c3 = b3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4493 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2]; b3 = precon[i + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4494 a0 = d0; a1 = d1; a2 = d2; a3 = d3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4495 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4496 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4497 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4498 d3 = scanline[i + 3] + paethPredictor(a3, b3, c3);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4499 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2; recon[i + 3] = d3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4500 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4501 } else if(bytewidth == 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4502 unsigned char a0, b0 = 0, c0, d0 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4503 unsigned char a1, b1 = 0, c1, d1 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4504 unsigned char a2, b2 = 0, c2, d2 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4505 for(i = 0; i + 2 < length; i += 3) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4506 c0 = b0; c1 = b1; c2 = b2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4507 b0 = precon[i + 0]; b1 = precon[i + 1]; b2 = precon[i + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4508 a0 = d0; a1 = d1; a2 = d2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4509 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4510 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4511 d2 = scanline[i + 2] + paethPredictor(a2, b2, c2);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4512 recon[i + 0] = d0; recon[i + 1] = d1; recon[i + 2] = d2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4513 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4514 } else if(bytewidth == 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4515 unsigned char a0, b0 = 0, c0, d0 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4516 unsigned char a1, b1 = 0, c1, d1 = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4517 for(i = 0; i + 1 < length; i += 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4518 c0 = b0; c1 = b1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4519 b0 = precon[i + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4520 b1 = precon[i + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4521 a0 = d0; a1 = d1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4522 d0 = scanline[i + 0] + paethPredictor(a0, b0, c0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4523 d1 = scanline[i + 1] + paethPredictor(a1, b1, c1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4524 recon[i + 0] = d0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4525 recon[i + 1] = d1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4526 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4527 } else if(bytewidth == 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4528 unsigned char a, b = 0, c, d = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4529 for(i = 0; i != length; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4530 c = b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4531 b = precon[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4532 a = d;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4533 d = scanline[i] + paethPredictor(a, b, c);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4534 recon[i] = d;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4535 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4536 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4537 /* Normally not a possible case, but this would handle it correctly */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4538 for(i = 0; i != bytewidth; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4539 recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4540 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4541 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4542 /* finish any remaining bytes */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4543 for(; i != length; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4544 recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4545 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4546 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4547 size_t j = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4548 for(i = 0; i != bytewidth; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4549 recon[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4550 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4551 for(i = bytewidth; i != length; ++i, ++j) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4552 /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4553 recon[i] = (scanline[i] + recon[j]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4554 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4555 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4556 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4557 default: return 36; /*error: invalid filter type given*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4558 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4559 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4560 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4561
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4562 static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4563 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4564 For PNG filter method 0
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4565 this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4566 out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4567 w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4568 in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4569 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4570
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4571 unsigned y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4572 unsigned char* prevline = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4573
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4574 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4575 size_t bytewidth = (bpp + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4576 /*the width of a scanline in bytes, not including the filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4577 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4578
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4579 for(y = 0; y < h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4580 size_t outindex = linebytes * y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4581 size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4582 unsigned char filterType = in[inindex];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4583
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4584 CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4585
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4586 prevline = &out[outindex];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4587 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4588
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4589 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4590 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4591
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4592 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4593 in: Adam7 interlaced image, with no padding bits between scanlines, but between
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4594 reduced images so that each reduced image starts at a byte.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4595 out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4596 bpp: bits per pixel
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4597 out has the following size in bits: w * h * bpp.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4598 in is possibly bigger due to padding bits between reduced images.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4599 out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4600 (because that's likely a little bit faster)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4601 NOTE: comments about padding bits are only relevant if bpp < 8
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4602 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4603 static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4604 unsigned passw[7], passh[7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4605 size_t filter_passstart[8], padded_passstart[8], passstart[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4606 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4607
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4608 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4609
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4610 if(bpp >= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4611 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4612 unsigned x, y, b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4613 size_t bytewidth = bpp / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4614 for(y = 0; y < passh[i]; ++y)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4615 for(x = 0; x < passw[i]; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4616 size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4617 size_t pixeloutstart = ((ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * (size_t)w
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4618 + ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bytewidth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4619 for(b = 0; b < bytewidth; ++b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4620 out[pixeloutstart + b] = in[pixelinstart + b];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4621 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4622 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4623 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4624 } else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4625 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4626 unsigned x, y, b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4627 unsigned ilinebits = bpp * passw[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4628 unsigned olinebits = bpp * w;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4629 size_t obp, ibp; /*bit pointers (for out and in buffer)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4630 for(y = 0; y < passh[i]; ++y)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4631 for(x = 0; x < passw[i]; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4632 ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4633 obp = (ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bpp;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4634 for(b = 0; b < bpp; ++b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4635 unsigned char bit = readBitFromReversedStream(&ibp, in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4636 setBitOfReversedStream(&obp, out, bit);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4637 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4638 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4639 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4640 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4641 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4642
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4643 static void removePaddingBits(unsigned char* out, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4644 size_t olinebits, size_t ilinebits, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4645 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4646 After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4647 to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4648 for the Adam7 code, the color convert code and the output to the user.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4649 in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4650 have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4651 also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4652 only useful if (ilinebits - olinebits) is a value in the range 1..7
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4653 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4654 unsigned y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4655 size_t diff = ilinebits - olinebits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4656 size_t ibp = 0, obp = 0; /*input and output bit pointers*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4657 for(y = 0; y < h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4658 size_t x;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4659 for(x = 0; x < olinebits; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4660 unsigned char bit = readBitFromReversedStream(&ibp, in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4661 setBitOfReversedStream(&obp, out, bit);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4662 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4663 ibp += diff;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4664 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4665 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4666
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4667 /*out must be buffer big enough to contain full image, and in must contain the full decompressed data from
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4668 the IDAT chunks (with filter index bytes and possible padding bits)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4669 return value is error*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4670 static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4671 unsigned w, unsigned h, const LodePNGInfo* info_png) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4672 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4673 This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4674 Steps:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4675 *) if no Adam7: 1) unfilter 2) remove padding bits (= possible extra bits per scanline if bpp < 8)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4676 *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4677 NOTE: the in buffer will be overwritten with intermediate data!
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4678 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4679 unsigned bpp = lodepng_get_bpp(&info_png->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4680 if(bpp == 0) return 31; /*error: invalid colortype*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4681
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4682 if(info_png->interlace_method == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4683 if(bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4684 CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4685 removePaddingBits(out, in, w * bpp, ((w * bpp + 7u) / 8u) * 8u, h);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4686 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4687 /*we can immediately filter into the out buffer, no other steps needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4688 else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4689 } else /*interlace_method is 1 (Adam7)*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4690 unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4691 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4692
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4693 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4694
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4695 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4696 CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4697 /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4698 move bytes instead of bits or move not at all*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4699 if(bpp < 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4700 /*remove padding bits in scanlines; after this there still may be padding
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4701 bits between the different reduced images: each reduced image still starts nicely at a byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4702 removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4703 ((passw[i] * bpp + 7u) / 8u) * 8u, passh[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4704 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4705 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4706
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4707 Adam7_deinterlace(out, in, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4708 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4709
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4710 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4711 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4712
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4713 static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4714 unsigned pos = 0, i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4715 color->palettesize = chunkLength / 3u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4716 if(color->palettesize == 0 || color->palettesize > 256) return 38; /*error: palette too small or big*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4717 lodepng_color_mode_alloc_palette(color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4718 if(!color->palette && color->palettesize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4719 color->palettesize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4720 return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4721 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4722
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4723 for(i = 0; i != color->palettesize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4724 color->palette[4 * i + 0] = data[pos++]; /*R*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4725 color->palette[4 * i + 1] = data[pos++]; /*G*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4726 color->palette[4 * i + 2] = data[pos++]; /*B*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4727 color->palette[4 * i + 3] = 255; /*alpha*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4728 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4729
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4730 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4731 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4732
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4733 static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4734 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4735 if(color->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4736 /*error: more alpha values given than there are palette entries*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4737 if(chunkLength > color->palettesize) return 39;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4738
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4739 for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4740 } else if(color->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4741 /*error: this chunk must be 2 bytes for grayscale image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4742 if(chunkLength != 2) return 30;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4743
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4744 color->key_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4745 color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4746 } else if(color->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4747 /*error: this chunk must be 6 bytes for RGB image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4748 if(chunkLength != 6) return 41;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4749
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4750 color->key_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4751 color->key_r = 256u * data[0] + data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4752 color->key_g = 256u * data[2] + data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4753 color->key_b = 256u * data[4] + data[5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4754 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4755 else return 42; /*error: tRNS chunk not allowed for other color models*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4756
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4757 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4758 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4759
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4760
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4761 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4762 /*background color chunk (bKGD)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4763 static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4764 if(info->color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4765 /*error: this chunk must be 1 byte for indexed color image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4766 if(chunkLength != 1) return 43;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4767
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4768 /*error: invalid palette index, or maybe this chunk appeared before PLTE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4769 if(data[0] >= info->color.palettesize) return 103;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4770
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4771 info->background_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4772 info->background_r = info->background_g = info->background_b = data[0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4773 } else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4774 /*error: this chunk must be 2 bytes for grayscale image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4775 if(chunkLength != 2) return 44;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4776
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4777 /*the values are truncated to bitdepth in the PNG file*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4778 info->background_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4779 info->background_r = info->background_g = info->background_b = 256u * data[0] + data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4780 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4781 /*error: this chunk must be 6 bytes for grayscale image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4782 if(chunkLength != 6) return 45;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4783
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4784 /*the values are truncated to bitdepth in the PNG file*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4785 info->background_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4786 info->background_r = 256u * data[0] + data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4787 info->background_g = 256u * data[2] + data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4788 info->background_b = 256u * data[4] + data[5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4789 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4790
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4791 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4792 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4793
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4794 /*text chunk (tEXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4795 static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4796 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4797 char *key = 0, *str = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4798
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4799 while(!error) /*not really a while loop, only used to break on error*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4800 unsigned length, string2_begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4801
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4802 length = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4803 while(length < chunkLength && data[length] != 0) ++length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4804 /*even though it's not allowed by the standard, no error is thrown if
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4805 there's no null termination char, if the text is empty*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4806 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4807
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4808 key = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4809 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4810
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4811 lodepng_memcpy(key, data, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4812 key[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4813
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4814 string2_begin = length + 1; /*skip keyword null terminator*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4815
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4816 length = (unsigned)(chunkLength < string2_begin ? 0 : chunkLength - string2_begin);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4817 str = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4818 if(!str) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4819
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4820 lodepng_memcpy(str, data + string2_begin, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4821 str[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4822
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4823 error = lodepng_add_text(info, key, str);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4824
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4825 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4826 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4827
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4828 lodepng_free(key);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4829 lodepng_free(str);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4830
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4831 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4832 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4833
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4834 /*compressed text chunk (zTXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4835 static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecoderSettings* decoder,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4836 const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4837 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4838
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4839 /*copy the object to change parameters in it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4840 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4841
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4842 unsigned length, string2_begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4843 char *key = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4844 unsigned char* str = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4845 size_t size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4846
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4847 while(!error) /*not really a while loop, only used to break on error*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4848 for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4849 if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4850 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4851
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4852 key = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4853 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4854
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4855 lodepng_memcpy(key, data, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4856 key[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4857
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4858 if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4859
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4860 string2_begin = length + 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4861 if(string2_begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4862
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4863 length = (unsigned)chunkLength - string2_begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4864 zlibsettings.max_output_size = decoder->max_text_size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4865 /*will fail if zlib error, e.g. if length is too small*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4866 error = zlib_decompress(&str, &size, 0, &data[string2_begin],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4867 length, &zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4868 /*error: compressed text larger than decoder->max_text_size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4869 if(error && size > zlibsettings.max_output_size) error = 112;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4870 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4871 error = lodepng_add_text_sized(info, key, (char*)str, size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4872 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4873 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4874
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4875 lodepng_free(key);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4876 lodepng_free(str);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4877
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4878 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4879 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4880
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4881 /*international text chunk (iTXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4882 static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecoderSettings* decoder,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4883 const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4884 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4885 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4886
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4887 /*copy the object to change parameters in it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4888 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4889
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4890 unsigned length, begin, compressed;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4891 char *key = 0, *langtag = 0, *transkey = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4892
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4893 while(!error) /*not really a while loop, only used to break on error*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4894 /*Quick check if the chunk length isn't too small. Even without check
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4895 it'd still fail with other error checks below if it's too short. This just gives a different error code.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4896 if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4897
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4898 /*read the key*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4899 for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4900 if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4901 if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4902
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4903 key = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4904 if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4905
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4906 lodepng_memcpy(key, data, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4907 key[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4908
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4909 /*read the compression method*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4910 compressed = data[length + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4911 if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4912
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4913 /*even though it's not allowed by the standard, no error is thrown if
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4914 there's no null termination char, if the text is empty for the next 3 texts*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4915
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4916 /*read the langtag*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4917 begin = length + 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4918 length = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4919 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4920
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4921 langtag = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4922 if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4923
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4924 lodepng_memcpy(langtag, data + begin, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4925 langtag[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4926
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4927 /*read the transkey*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4928 begin += length + 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4929 length = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4930 for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4931
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4932 transkey = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4933 if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4934
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4935 lodepng_memcpy(transkey, data + begin, length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4936 transkey[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4937
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4938 /*read the actual text*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4939 begin += length + 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4940
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4941 length = (unsigned)chunkLength < begin ? 0 : (unsigned)chunkLength - begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4942
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4943 if(compressed) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4944 unsigned char* str = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4945 size_t size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4946 zlibsettings.max_output_size = decoder->max_text_size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4947 /*will fail if zlib error, e.g. if length is too small*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4948 error = zlib_decompress(&str, &size, 0, &data[begin],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4949 length, &zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4950 /*error: compressed text larger than decoder->max_text_size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4951 if(error && size > zlibsettings.max_output_size) error = 112;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4952 if(!error) error = lodepng_add_itext_sized(info, key, langtag, transkey, (char*)str, size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4953 lodepng_free(str);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4954 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4955 error = lodepng_add_itext_sized(info, key, langtag, transkey, (char*)(data + begin), length);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4956 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4957
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4958 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4959 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4960
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4961 lodepng_free(key);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4962 lodepng_free(langtag);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4963 lodepng_free(transkey);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4964
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4965 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4966 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4967
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4968 static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4969 if(chunkLength != 7) return 73; /*invalid tIME chunk size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4970
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4971 info->time_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4972 info->time.year = 256u * data[0] + data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4973 info->time.month = data[2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4974 info->time.day = data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4975 info->time.hour = data[4];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4976 info->time.minute = data[5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4977 info->time.second = data[6];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4978
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4979 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4980 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4981
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4982 static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4983 if(chunkLength != 9) return 74; /*invalid pHYs chunk size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4984
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4985 info->phys_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4986 info->phys_x = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4987 info->phys_y = 16777216u * data[4] + 65536u * data[5] + 256u * data[6] + data[7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4988 info->phys_unit = data[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4989
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4990 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4991 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4992
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4993 static unsigned readChunk_gAMA(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4994 if(chunkLength != 4) return 96; /*invalid gAMA chunk size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4995
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4996 info->gama_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4997 info->gama_gamma = 16777216u * data[0] + 65536u * data[1] + 256u * data[2] + data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4998
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
4999 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5000 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5001
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5002 static unsigned readChunk_cHRM(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5003 if(chunkLength != 32) return 97; /*invalid cHRM chunk size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5004
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5005 info->chrm_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5006 info->chrm_white_x = 16777216u * data[ 0] + 65536u * data[ 1] + 256u * data[ 2] + data[ 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5007 info->chrm_white_y = 16777216u * data[ 4] + 65536u * data[ 5] + 256u * data[ 6] + data[ 7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5008 info->chrm_red_x = 16777216u * data[ 8] + 65536u * data[ 9] + 256u * data[10] + data[11];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5009 info->chrm_red_y = 16777216u * data[12] + 65536u * data[13] + 256u * data[14] + data[15];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5010 info->chrm_green_x = 16777216u * data[16] + 65536u * data[17] + 256u * data[18] + data[19];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5011 info->chrm_green_y = 16777216u * data[20] + 65536u * data[21] + 256u * data[22] + data[23];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5012 info->chrm_blue_x = 16777216u * data[24] + 65536u * data[25] + 256u * data[26] + data[27];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5013 info->chrm_blue_y = 16777216u * data[28] + 65536u * data[29] + 256u * data[30] + data[31];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5014
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5015 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5016 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5017
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5018 static unsigned readChunk_sRGB(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5019 if(chunkLength != 1) return 98; /*invalid sRGB chunk size (this one is never ignored)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5020
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5021 info->srgb_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5022 info->srgb_intent = data[0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5023
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5024 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5025 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5026
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5027 static unsigned readChunk_iCCP(LodePNGInfo* info, const LodePNGDecoderSettings* decoder,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5028 const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5029 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5030 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5031 size_t size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5032 /*copy the object to change parameters in it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5033 LodePNGDecompressSettings zlibsettings = decoder->zlibsettings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5034
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5035 unsigned length, string2_begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5036
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5037 info->iccp_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5038 if(info->iccp_name) lodepng_clear_icc(info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5039
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5040 for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5041 if(length + 2 >= chunkLength) return 75; /*no null termination, corrupt?*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5042 if(length < 1 || length > 79) return 89; /*keyword too short or long*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5043
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5044 info->iccp_name = (char*)lodepng_malloc(length + 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5045 if(!info->iccp_name) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5046
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5047 info->iccp_name[length] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5048 for(i = 0; i != length; ++i) info->iccp_name[i] = (char)data[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5049
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5050 if(data[length + 1] != 0) return 72; /*the 0 byte indicating compression must be 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5051
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5052 string2_begin = length + 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5053 if(string2_begin > chunkLength) return 75; /*no null termination, corrupt?*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5054
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5055 length = (unsigned)chunkLength - string2_begin;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5056 zlibsettings.max_output_size = decoder->max_icc_size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5057 error = zlib_decompress(&info->iccp_profile, &size, 0,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5058 &data[string2_begin],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5059 length, &zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5060 /*error: ICC profile larger than decoder->max_icc_size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5061 if(error && size > zlibsettings.max_output_size) error = 113;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5062 info->iccp_profile_size = size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5063 if(!error && !info->iccp_profile_size) error = 100; /*invalid ICC profile size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5064 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5065 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5066
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5067 /*significant bits chunk (sBIT)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5068 static unsigned readChunk_sBIT(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5069 unsigned bitdepth = (info->color.colortype == LCT_PALETTE) ? 8 : info->color.bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5070 if(info->color.colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5071 /*error: this chunk must be 1 bytes for grayscale image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5072 if(chunkLength != 1) return 114;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5073 if(data[0] == 0 || data[0] > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5074 info->sbit_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5075 info->sbit_r = info->sbit_g = info->sbit_b = data[0]; /*setting g and b is not required, but sensible*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5076 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5077 /*error: this chunk must be 3 bytes for RGB and palette image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5078 if(chunkLength != 3) return 114;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5079 if(data[0] == 0 || data[1] == 0 || data[2] == 0) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5080 if(data[0] > bitdepth || data[1] > bitdepth || data[2] > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5081 info->sbit_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5082 info->sbit_r = data[0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5083 info->sbit_g = data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5084 info->sbit_b = data[2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5085 } else if(info->color.colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5086 /*error: this chunk must be 2 byte for grayscale with alpha image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5087 if(chunkLength != 2) return 114;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5088 if(data[0] == 0 || data[1] == 0) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5089 if(data[0] > bitdepth || data[1] > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5090 info->sbit_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5091 info->sbit_r = info->sbit_g = info->sbit_b = data[0]; /*setting g and b is not required, but sensible*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5092 info->sbit_a = data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5093 } else if(info->color.colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5094 /*error: this chunk must be 4 bytes for grayscale image*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5095 if(chunkLength != 4) return 114;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5096 if(data[0] == 0 || data[1] == 0 || data[2] == 0 || data[3] == 0) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5097 if(data[0] > bitdepth || data[1] > bitdepth || data[2] > bitdepth || data[3] > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5098 info->sbit_defined = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5099 info->sbit_r = data[0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5100 info->sbit_g = data[1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5101 info->sbit_b = data[2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5102 info->sbit_a = data[3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5103 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5104
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5105 return 0; /* OK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5106 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5107 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5108
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5109 unsigned lodepng_inspect_chunk(LodePNGState* state, size_t pos,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5110 const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5111 const unsigned char* chunk = in + pos;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5112 unsigned chunkLength;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5113 const unsigned char* data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5114 unsigned unhandled = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5115 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5116
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5117 if(pos + 4 > insize) return 30;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5118 chunkLength = lodepng_chunk_length(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5119 if(chunkLength > 2147483647) return 63;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5120 data = lodepng_chunk_data_const(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5121 if(chunkLength + 12 > insize - pos) return 30;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5122
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5123 if(lodepng_chunk_type_equals(chunk, "PLTE")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5124 error = readChunk_PLTE(&state->info_png.color, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5125 } else if(lodepng_chunk_type_equals(chunk, "tRNS")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5126 error = readChunk_tRNS(&state->info_png.color, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5127 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5128 } else if(lodepng_chunk_type_equals(chunk, "bKGD")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5129 error = readChunk_bKGD(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5130 } else if(lodepng_chunk_type_equals(chunk, "tEXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5131 error = readChunk_tEXt(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5132 } else if(lodepng_chunk_type_equals(chunk, "zTXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5133 error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5134 } else if(lodepng_chunk_type_equals(chunk, "iTXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5135 error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5136 } else if(lodepng_chunk_type_equals(chunk, "tIME")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5137 error = readChunk_tIME(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5138 } else if(lodepng_chunk_type_equals(chunk, "pHYs")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5139 error = readChunk_pHYs(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5140 } else if(lodepng_chunk_type_equals(chunk, "gAMA")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5141 error = readChunk_gAMA(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5142 } else if(lodepng_chunk_type_equals(chunk, "cHRM")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5143 error = readChunk_cHRM(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5144 } else if(lodepng_chunk_type_equals(chunk, "sRGB")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5145 error = readChunk_sRGB(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5146 } else if(lodepng_chunk_type_equals(chunk, "iCCP")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5147 error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5148 } else if(lodepng_chunk_type_equals(chunk, "sBIT")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5149 error = readChunk_sBIT(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5150 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5151 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5152 /* unhandled chunk is ok (is not an error) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5153 unhandled = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5154 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5155
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5156 if(!error && !unhandled && !state->decoder.ignore_crc) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5157 if(lodepng_chunk_check_crc(chunk)) return 57; /*invalid CRC*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5158 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5159
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5160 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5161 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5162
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5163 /*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5164 static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5165 LodePNGState* state,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5166 const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5167 unsigned char IEND = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5168 const unsigned char* chunk; /*points to beginning of next chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5169 unsigned char* idat; /*the data from idat chunks, zlib compressed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5170 size_t idatsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5171 unsigned char* scanlines = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5172 size_t scanlines_size = 0, expected_size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5173 size_t outsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5174
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5175 /*for unknown chunk order*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5176 unsigned unknown = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5177 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5178 unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5179 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5180
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5181
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5182 /* safe output values in case error happens */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5183 *out = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5184 *w = *h = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5185
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5186 state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5187 if(state->error) return;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5188
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5189 if(lodepng_pixel_overflow(*w, *h, &state->info_png.color, &state->info_raw)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5190 CERROR_RETURN(state->error, 92); /*overflow possible due to amount of pixels*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5191 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5192
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5193 /*the input filesize is a safe upper bound for the sum of idat chunks size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5194 idat = (unsigned char*)lodepng_malloc(insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5195 if(!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5196
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5197 chunk = &in[33]; /*first byte of the first chunk after the header*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5198
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5199 /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5200 IDAT data is put at the start of the in buffer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5201 while(!IEND && !state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5202 unsigned chunkLength;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5203 const unsigned char* data; /*the data in the chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5204 size_t pos = (size_t)(chunk - in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5205
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5206 /*error: next chunk out of bounds of the in buffer*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5207 if(chunk < in || pos + 12 > insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5208 if(state->decoder.ignore_end) break; /*other errors may still happen though*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5209 CERROR_BREAK(state->error, 30);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5210 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5211
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5212 /*length of the data of the chunk, excluding the 12 bytes for length, chunk type and CRC*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5213 chunkLength = lodepng_chunk_length(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5214 /*error: chunk length larger than the max PNG chunk size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5215 if(chunkLength > 2147483647) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5216 if(state->decoder.ignore_end) break; /*other errors may still happen though*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5217 CERROR_BREAK(state->error, 63);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5218 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5219
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5220 if(pos + (size_t)chunkLength + 12 > insize || pos + (size_t)chunkLength + 12 < pos) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5221 CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk (or int overflow)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5222 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5223
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5224 data = lodepng_chunk_data_const(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5225
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5226 unknown = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5227
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5228 /*IDAT chunk, containing compressed image data*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5229 if(lodepng_chunk_type_equals(chunk, "IDAT")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5230 size_t newsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5231 if(lodepng_addofl(idatsize, chunkLength, &newsize)) CERROR_BREAK(state->error, 95);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5232 if(newsize > insize) CERROR_BREAK(state->error, 95);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5233 lodepng_memcpy(idat + idatsize, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5234 idatsize += chunkLength;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5235 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5236 critical_pos = 3;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5237 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5238 } else if(lodepng_chunk_type_equals(chunk, "IEND")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5239 /*IEND chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5240 IEND = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5241 } else if(lodepng_chunk_type_equals(chunk, "PLTE")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5242 /*palette chunk (PLTE)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5243 state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5244 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5245 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5246 critical_pos = 2;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5247 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5248 } else if(lodepng_chunk_type_equals(chunk, "tRNS")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5249 /*palette transparency chunk (tRNS). Even though this one is an ancillary chunk , it is still compiled
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5250 in without 'LODEPNG_COMPILE_ANCILLARY_CHUNKS' because it contains essential color information that
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5251 affects the alpha channel of pixels. */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5252 state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5253 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5254 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5255 /*background color chunk (bKGD)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5256 } else if(lodepng_chunk_type_equals(chunk, "bKGD")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5257 state->error = readChunk_bKGD(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5258 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5259 } else if(lodepng_chunk_type_equals(chunk, "tEXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5260 /*text chunk (tEXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5261 if(state->decoder.read_text_chunks) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5262 state->error = readChunk_tEXt(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5263 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5264 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5265 } else if(lodepng_chunk_type_equals(chunk, "zTXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5266 /*compressed text chunk (zTXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5267 if(state->decoder.read_text_chunks) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5268 state->error = readChunk_zTXt(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5269 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5270 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5271 } else if(lodepng_chunk_type_equals(chunk, "iTXt")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5272 /*international text chunk (iTXt)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5273 if(state->decoder.read_text_chunks) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5274 state->error = readChunk_iTXt(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5275 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5276 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5277 } else if(lodepng_chunk_type_equals(chunk, "tIME")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5278 state->error = readChunk_tIME(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5279 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5280 } else if(lodepng_chunk_type_equals(chunk, "pHYs")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5281 state->error = readChunk_pHYs(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5282 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5283 } else if(lodepng_chunk_type_equals(chunk, "gAMA")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5284 state->error = readChunk_gAMA(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5285 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5286 } else if(lodepng_chunk_type_equals(chunk, "cHRM")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5287 state->error = readChunk_cHRM(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5288 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5289 } else if(lodepng_chunk_type_equals(chunk, "sRGB")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5290 state->error = readChunk_sRGB(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5291 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5292 } else if(lodepng_chunk_type_equals(chunk, "iCCP")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5293 state->error = readChunk_iCCP(&state->info_png, &state->decoder, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5294 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5295 } else if(lodepng_chunk_type_equals(chunk, "sBIT")) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5296 state->error = readChunk_sBIT(&state->info_png, data, chunkLength);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5297 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5298 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5299 } else /*it's not an implemented chunk type, so ignore it: skip over the data*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5300 /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5301 if(!state->decoder.ignore_critical && !lodepng_chunk_ancillary(chunk)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5302 CERROR_BREAK(state->error, 69);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5303 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5304
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5305 unknown = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5306 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5307 if(state->decoder.remember_unknown_chunks) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5308 state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5309 &state->info_png.unknown_chunks_size[critical_pos - 1], chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5310 if(state->error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5311 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5312 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5313 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5314
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5315 if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5316 if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5317 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5318
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5319 if(!IEND) chunk = lodepng_chunk_next_const(chunk, in + insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5320 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5321
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5322 if(!state->error && state->info_png.color.colortype == LCT_PALETTE && !state->info_png.color.palette) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5323 state->error = 106; /* error: PNG file must have PLTE chunk if color type is palette */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5324 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5325
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5326 if(!state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5327 /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5328 If the decompressed size does not match the prediction, the image must be corrupt.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5329 if(state->info_png.interlace_method == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5330 size_t bpp = lodepng_get_bpp(&state->info_png.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5331 expected_size = lodepng_get_raw_size_idat(*w, *h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5332 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5333 size_t bpp = lodepng_get_bpp(&state->info_png.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5334 /*Adam-7 interlaced: expected size is the sum of the 7 sub-images sizes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5335 expected_size = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5336 expected_size += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5337 if(*w > 4) expected_size += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5338 expected_size += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5339 if(*w > 2) expected_size += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5340 expected_size += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5341 if(*w > 1) expected_size += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5342 expected_size += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5343 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5344
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5345 state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, &state->decoder.zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5346 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5347 if(!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn't match prediction*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5348 lodepng_free(idat);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5349
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5350 if(!state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5351 outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5352 *out = (unsigned char*)lodepng_malloc(outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5353 if(!*out) state->error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5354 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5355 if(!state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5356 lodepng_memset(*out, 0, outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5357 state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5358 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5359 lodepng_free(scanlines);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5360 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5361
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5362 unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5363 LodePNGState* state,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5364 const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5365 *out = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5366 decodeGeneric(out, w, h, state, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5367 if(state->error) return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5368 if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5369 /*same color type, no copying or converting of data needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5370 /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5371 the raw image has to the end user*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5372 if(!state->decoder.color_convert) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5373 state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5374 if(state->error) return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5375 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5376 } else { /*color conversion needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5377 unsigned char* data = *out;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5378 size_t outsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5379
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5380 /*TODO: check if this works according to the statement in the documentation: "The converter can convert
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5381 from grayscale input color type, to 8-bit grayscale or grayscale with alpha"*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5382 if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5383 && !(state->info_raw.bitdepth == 8)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5384 return 56; /*unsupported color mode conversion*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5385 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5386
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5387 outsize = lodepng_get_raw_size(*w, *h, &state->info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5388 *out = (unsigned char*)lodepng_malloc(outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5389 if(!(*out)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5390 state->error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5391 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5392 else state->error = lodepng_convert(*out, data, &state->info_raw,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5393 &state->info_png.color, *w, *h);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5394 lodepng_free(data);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5395 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5396 return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5397 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5398
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5399 unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5400 size_t insize, LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5401 unsigned error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5402 LodePNGState state;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5403 lodepng_state_init(&state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5404 state.info_raw.colortype = colortype;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5405 state.info_raw.bitdepth = bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5406 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5407 /*disable reading things that this function doesn't output*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5408 state.decoder.read_text_chunks = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5409 state.decoder.remember_unknown_chunks = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5410 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5411 error = lodepng_decode(out, w, h, &state, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5412 lodepng_state_cleanup(&state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5413 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5414 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5415
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5416 unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5417 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5418 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5419
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5420 unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5421 return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5422 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5423
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5424 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5425 unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5426 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5427 unsigned char* buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5428 size_t buffersize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5429 unsigned error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5430 /* safe output values in case error happens */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5431 *out = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5432 *w = *h = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5433 error = lodepng_load_file(&buffer, &buffersize, filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5434 if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5435 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5436 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5437 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5438
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5439 unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5440 return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5441 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5442
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5443 unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5444 return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5445 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5446 #endif /*LODEPNG_COMPILE_DISK*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5447
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5448 void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5449 settings->color_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5450 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5451 settings->read_text_chunks = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5452 settings->remember_unknown_chunks = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5453 settings->max_text_size = 16777216;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5454 settings->max_icc_size = 16777216; /* 16MB is much more than enough for any reasonable ICC profile */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5455 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5456 settings->ignore_crc = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5457 settings->ignore_critical = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5458 settings->ignore_end = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5459 lodepng_decompress_settings_init(&settings->zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5460 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5461
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5462 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5463
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5464 #if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5465
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5466 void lodepng_state_init(LodePNGState* state) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5467 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5468 lodepng_decoder_settings_init(&state->decoder);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5469 #endif /*LODEPNG_COMPILE_DECODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5470 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5471 lodepng_encoder_settings_init(&state->encoder);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5472 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5473 lodepng_color_mode_init(&state->info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5474 lodepng_info_init(&state->info_png);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5475 state->error = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5476 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5477
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5478 void lodepng_state_cleanup(LodePNGState* state) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5479 lodepng_color_mode_cleanup(&state->info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5480 lodepng_info_cleanup(&state->info_png);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5481 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5482
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5483 void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5484 lodepng_state_cleanup(dest);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5485 *dest = *source;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5486 lodepng_color_mode_init(&dest->info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5487 lodepng_info_init(&dest->info_png);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5488 dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); if(dest->error) return;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5489 dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); if(dest->error) return;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5490 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5491
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5492 #endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5493
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5494 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5495
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5496 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5497 /* / PNG Encoder / */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5498 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5499
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5500
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5501 static unsigned writeSignature(ucvector* out) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5502 size_t pos = out->size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5503 const unsigned char signature[] = {137, 80, 78, 71, 13, 10, 26, 10};
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5504 /*8 bytes PNG signature, aka the magic bytes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5505 if(!ucvector_resize(out, out->size + 8)) return 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5506 lodepng_memcpy(out->data + pos, signature, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5507 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5508 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5509
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5510 static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5511 LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5512 unsigned char *chunk, *data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5513 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 13, "IHDR"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5514 data = chunk + 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5515
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5516 lodepng_set32bitInt(data + 0, w); /*width*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5517 lodepng_set32bitInt(data + 4, h); /*height*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5518 data[8] = (unsigned char)bitdepth; /*bit depth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5519 data[9] = (unsigned char)colortype; /*color type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5520 data[10] = 0; /*compression method*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5521 data[11] = 0; /*filter method*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5522 data[12] = interlace_method; /*interlace method*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5523
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5524 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5525 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5526 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5527
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5528 /* only adds the chunk if needed (there is a key or palette with alpha) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5529 static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5530 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5531 size_t i, j = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5532
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5533 if(info->palettesize == 0 || info->palettesize > 256) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5534 return 68; /*invalid palette size, it is only allowed to be 1-256*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5535 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5536
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5537 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, info->palettesize * 3, "PLTE"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5538
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5539 for(i = 0; i != info->palettesize; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5540 /*add all channels except alpha channel*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5541 chunk[j++] = info->palette[i * 4 + 0];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5542 chunk[j++] = info->palette[i * 4 + 1];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5543 chunk[j++] = info->palette[i * 4 + 2];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5544 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5545
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5546 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5547 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5548 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5549
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5550 static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5551 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5552
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5553 if(info->colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5554 size_t i, amount = info->palettesize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5555 /*the tail of palette values that all have 255 as alpha, does not have to be encoded*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5556 for(i = info->palettesize; i != 0; --i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5557 if(info->palette[4 * (i - 1) + 3] != 255) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5558 --amount;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5559 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5560 if(amount) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5561 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, amount, "tRNS"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5562 /*add the alpha channel values from the palette*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5563 for(i = 0; i != amount; ++i) chunk[8 + i] = info->palette[4 * i + 3];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5564 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5565 } else if(info->colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5566 if(info->key_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5567 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 2, "tRNS"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5568 chunk[8] = (unsigned char)(info->key_r >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5569 chunk[9] = (unsigned char)(info->key_r & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5570 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5571 } else if(info->colortype == LCT_RGB) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5572 if(info->key_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5573 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 6, "tRNS"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5574 chunk[8] = (unsigned char)(info->key_r >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5575 chunk[9] = (unsigned char)(info->key_r & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5576 chunk[10] = (unsigned char)(info->key_g >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5577 chunk[11] = (unsigned char)(info->key_g & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5578 chunk[12] = (unsigned char)(info->key_b >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5579 chunk[13] = (unsigned char)(info->key_b & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5580 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5581 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5582
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5583 if(chunk) lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5584 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5585 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5586
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5587 static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5588 LodePNGCompressSettings* zlibsettings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5589 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5590 unsigned char* zlib = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5591 size_t zlibsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5592
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5593 error = zlib_compress(&zlib, &zlibsize, data, datasize, zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5594 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5595 error = lodepng_chunk_createv(out, zlibsize, "IDAT", zlib);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5596 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5597 lodepng_free(zlib);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5598 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5599 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5600
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5601 static unsigned addChunk_IEND(ucvector* out) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5602 return lodepng_chunk_createv(out, 0, "IEND", 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5603 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5604
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5605 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5606
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5607 static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5608 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5609 size_t keysize = lodepng_strlen(keyword), textsize = lodepng_strlen(textstring);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5610 size_t size = keysize + 1 + textsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5611 if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5612 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, size, "tEXt"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5613 lodepng_memcpy(chunk + 8, keyword, keysize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5614 chunk[8 + keysize] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5615 lodepng_memcpy(chunk + 9 + keysize, textstring, textsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5616 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5617 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5618 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5619
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5620 static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* textstring,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5621 LodePNGCompressSettings* zlibsettings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5622 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5623 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5624 unsigned char* compressed = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5625 size_t compressedsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5626 size_t textsize = lodepng_strlen(textstring);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5627 size_t keysize = lodepng_strlen(keyword);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5628 if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5629
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5630 error = zlib_compress(&compressed, &compressedsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5631 (const unsigned char*)textstring, textsize, zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5632 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5633 size_t size = keysize + 2 + compressedsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5634 error = lodepng_chunk_init(&chunk, out, size, "zTXt");
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5635 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5636 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5637 lodepng_memcpy(chunk + 8, keyword, keysize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5638 chunk[8 + keysize] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5639 chunk[9 + keysize] = 0; /*compression method: 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5640 lodepng_memcpy(chunk + 10 + keysize, compressed, compressedsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5641 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5642 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5643
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5644 lodepng_free(compressed);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5645 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5646 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5647
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5648 static unsigned addChunk_iTXt(ucvector* out, unsigned compress, const char* keyword, const char* langtag,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5649 const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5650 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5651 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5652 unsigned char* compressed = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5653 size_t compressedsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5654 size_t textsize = lodepng_strlen(textstring);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5655 size_t keysize = lodepng_strlen(keyword), langsize = lodepng_strlen(langtag), transsize = lodepng_strlen(transkey);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5656
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5657 if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5658
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5659 if(compress) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5660 error = zlib_compress(&compressed, &compressedsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5661 (const unsigned char*)textstring, textsize, zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5662 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5663 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5664 size_t size = keysize + 3 + langsize + 1 + transsize + 1 + (compress ? compressedsize : textsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5665 error = lodepng_chunk_init(&chunk, out, size, "iTXt");
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5666 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5667 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5668 size_t pos = 8;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5669 lodepng_memcpy(chunk + pos, keyword, keysize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5670 pos += keysize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5671 chunk[pos++] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5672 chunk[pos++] = (compress ? 1 : 0); /*compression flag*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5673 chunk[pos++] = 0; /*compression method: 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5674 lodepng_memcpy(chunk + pos, langtag, langsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5675 pos += langsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5676 chunk[pos++] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5677 lodepng_memcpy(chunk + pos, transkey, transsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5678 pos += transsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5679 chunk[pos++] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5680 if(compress) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5681 lodepng_memcpy(chunk + pos, compressed, compressedsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5682 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5683 lodepng_memcpy(chunk + pos, textstring, textsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5684 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5685 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5686 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5687
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5688 lodepng_free(compressed);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5689 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5690 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5691
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5692 static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5693 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5694 if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5695 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 2, "bKGD"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5696 chunk[8] = (unsigned char)(info->background_r >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5697 chunk[9] = (unsigned char)(info->background_r & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5698 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5699 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 6, "bKGD"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5700 chunk[8] = (unsigned char)(info->background_r >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5701 chunk[9] = (unsigned char)(info->background_r & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5702 chunk[10] = (unsigned char)(info->background_g >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5703 chunk[11] = (unsigned char)(info->background_g & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5704 chunk[12] = (unsigned char)(info->background_b >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5705 chunk[13] = (unsigned char)(info->background_b & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5706 } else if(info->color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5707 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 1, "bKGD"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5708 chunk[8] = (unsigned char)(info->background_r & 255); /*palette index*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5709 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5710 if(chunk) lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5711 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5712 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5713
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5714 static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5715 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5716 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 7, "tIME"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5717 chunk[8] = (unsigned char)(time->year >> 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5718 chunk[9] = (unsigned char)(time->year & 255);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5719 chunk[10] = (unsigned char)time->month;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5720 chunk[11] = (unsigned char)time->day;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5721 chunk[12] = (unsigned char)time->hour;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5722 chunk[13] = (unsigned char)time->minute;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5723 chunk[14] = (unsigned char)time->second;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5724 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5725 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5726 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5727
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5728 static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5729 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5730 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 9, "pHYs"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5731 lodepng_set32bitInt(chunk + 8, info->phys_x);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5732 lodepng_set32bitInt(chunk + 12, info->phys_y);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5733 chunk[16] = info->phys_unit;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5734 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5735 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5736 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5737
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5738 static unsigned addChunk_gAMA(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5739 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5740 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 4, "gAMA"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5741 lodepng_set32bitInt(chunk + 8, info->gama_gamma);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5742 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5743 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5744 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5745
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5746 static unsigned addChunk_cHRM(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5747 unsigned char* chunk;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5748 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 32, "cHRM"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5749 lodepng_set32bitInt(chunk + 8, info->chrm_white_x);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5750 lodepng_set32bitInt(chunk + 12, info->chrm_white_y);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5751 lodepng_set32bitInt(chunk + 16, info->chrm_red_x);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5752 lodepng_set32bitInt(chunk + 20, info->chrm_red_y);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5753 lodepng_set32bitInt(chunk + 24, info->chrm_green_x);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5754 lodepng_set32bitInt(chunk + 28, info->chrm_green_y);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5755 lodepng_set32bitInt(chunk + 32, info->chrm_blue_x);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5756 lodepng_set32bitInt(chunk + 36, info->chrm_blue_y);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5757 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5758 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5759 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5760
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5761 static unsigned addChunk_sRGB(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5762 unsigned char data = info->srgb_intent;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5763 return lodepng_chunk_createv(out, 1, "sRGB", &data);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5764 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5765
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5766 static unsigned addChunk_iCCP(ucvector* out, const LodePNGInfo* info, LodePNGCompressSettings* zlibsettings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5767 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5768 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5769 unsigned char* compressed = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5770 size_t compressedsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5771 size_t keysize = lodepng_strlen(info->iccp_name);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5772
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5773 if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5774 error = zlib_compress(&compressed, &compressedsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5775 info->iccp_profile, info->iccp_profile_size, zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5776 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5777 size_t size = keysize + 2 + compressedsize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5778 error = lodepng_chunk_init(&chunk, out, size, "iCCP");
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5779 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5780 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5781 lodepng_memcpy(chunk + 8, info->iccp_name, keysize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5782 chunk[8 + keysize] = 0; /*null termination char*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5783 chunk[9 + keysize] = 0; /*compression method: 0*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5784 lodepng_memcpy(chunk + 10 + keysize, compressed, compressedsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5785 lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5786 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5787
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5788 lodepng_free(compressed);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5789 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5790 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5791
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5792 static unsigned addChunk_sBIT(ucvector* out, const LodePNGInfo* info) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5793 unsigned bitdepth = (info->color.colortype == LCT_PALETTE) ? 8 : info->color.bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5794 unsigned char* chunk = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5795 if(info->color.colortype == LCT_GREY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5796 if(info->sbit_r == 0 || info->sbit_r > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5797 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 1, "sBIT"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5798 chunk[8] = info->sbit_r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5799 } else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5800 if(info->sbit_r == 0 || info->sbit_g == 0 || info->sbit_b == 0) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5801 if(info->sbit_r > bitdepth || info->sbit_g > bitdepth || info->sbit_b > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5802 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 3, "sBIT"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5803 chunk[8] = info->sbit_r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5804 chunk[9] = info->sbit_g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5805 chunk[10] = info->sbit_b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5806 } else if(info->color.colortype == LCT_GREY_ALPHA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5807 if(info->sbit_r == 0 || info->sbit_a == 0) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5808 if(info->sbit_r > bitdepth || info->sbit_a > bitdepth) return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5809 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 2, "sBIT"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5810 chunk[8] = info->sbit_r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5811 chunk[9] = info->sbit_a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5812 } else if(info->color.colortype == LCT_RGBA) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5813 if(info->sbit_r == 0 || info->sbit_g == 0 || info->sbit_b == 0 || info->sbit_a == 0 ||
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5814 info->sbit_r > bitdepth || info->sbit_g > bitdepth ||
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5815 info->sbit_b > bitdepth || info->sbit_a > bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5816 return 115;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5817 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5818 CERROR_TRY_RETURN(lodepng_chunk_init(&chunk, out, 4, "sBIT"));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5819 chunk[8] = info->sbit_r;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5820 chunk[9] = info->sbit_g;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5821 chunk[10] = info->sbit_b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5822 chunk[11] = info->sbit_a;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5823 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5824 if(chunk) lodepng_chunk_generate_crc(chunk);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5825 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5826 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5827
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5828 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5829
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5830 static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5831 size_t length, size_t bytewidth, unsigned char filterType) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5832 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5833 switch(filterType) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5834 case 0: /*None*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5835 for(i = 0; i != length; ++i) out[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5836 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5837 case 1: /*Sub*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5838 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5839 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5840 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5841 case 2: /*Up*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5842 if(prevline) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5843 for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5844 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5845 for(i = 0; i != length; ++i) out[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5846 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5847 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5848 case 3: /*Average*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5849 if(prevline) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5850 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5851 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) >> 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5852 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5853 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5854 for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - (scanline[i - bytewidth] >> 1);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5855 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5856 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5857 case 4: /*Paeth*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5858 if(prevline) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5859 /*paethPredictor(0, prevline[i], 0) is always prevline[i]*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5860 for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5861 for(i = bytewidth; i < length; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5862 out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth]));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5863 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5864 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5865 for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5866 /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5867 for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5868 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5869 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5870 default: return; /*invalid filter type given*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5871 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5872 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5873
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5874 /* integer binary logarithm, max return value is 31 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5875 static size_t ilog2(size_t i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5876 size_t result = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5877 if(i >= 65536) { result += 16; i >>= 16; }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5878 if(i >= 256) { result += 8; i >>= 8; }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5879 if(i >= 16) { result += 4; i >>= 4; }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5880 if(i >= 4) { result += 2; i >>= 2; }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5881 if(i >= 2) { result += 1; /*i >>= 1;*/ }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5882 return result;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5883 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5884
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5885 /* integer approximation for i * log2(i), helper function for LFS_ENTROPY */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5886 static size_t ilog2i(size_t i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5887 size_t l;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5888 if(i == 0) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5889 l = ilog2(i);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5890 /* approximate i*log2(i): l is integer logarithm, ((i - (1u << l)) << 1u)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5891 linearly approximates the missing fractional part multiplied by i */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5892 return i * l + ((i - (1u << l)) << 1u);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5893 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5894
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5895 static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5896 const LodePNGColorMode* color, const LodePNGEncoderSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5897 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5898 For PNG filter method 0
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5899 out must be a buffer with as size: h + (w * h * bpp + 7u) / 8u, because there are
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5900 the scanlines with 1 extra byte per scanline
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5901 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5902
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5903 unsigned bpp = lodepng_get_bpp(color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5904 /*the width of a scanline in bytes, not including the filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5905 size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5906
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5907 /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5908 size_t bytewidth = (bpp + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5909 const unsigned char* prevline = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5910 unsigned x, y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5911 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5912 LodePNGFilterStrategy strategy = settings->filter_strategy;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5913
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5914 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5915 There is a heuristic called the minimum sum of absolute differences heuristic, suggested by the PNG standard:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5916 * If the image type is Palette, or the bit depth is smaller than 8, then do not filter the image (i.e.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5917 use fixed filtering, with the filter None).
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5918 * (The other case) If the image type is Grayscale or RGB (with or without Alpha), and the bit depth is
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5919 not smaller than 8, then use adaptive filtering heuristic as follows: independently for each row, apply
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5920 all five filters and select the filter that produces the smallest sum of absolute values per row.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5921 This heuristic is used if filter strategy is LFS_MINSUM and filter_palette_zero is true.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5922
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5923 If filter_palette_zero is true and filter_strategy is not LFS_MINSUM, the above heuristic is followed,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5924 but for "the other case", whatever strategy filter_strategy is set to instead of the minimum sum
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5925 heuristic is used.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5926 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5927 if(settings->filter_palette_zero &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5928 (color->colortype == LCT_PALETTE || color->bitdepth < 8)) strategy = LFS_ZERO;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5929
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5930 if(bpp == 0) return 31; /*error: invalid color type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5931
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5932 if(strategy >= LFS_ZERO && strategy <= LFS_FOUR) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5933 unsigned char type = (unsigned char)strategy;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5934 for(y = 0; y != h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5935 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5936 size_t inindex = linebytes * y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5937 out[outindex] = type; /*filter type byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5938 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5939 prevline = &in[inindex];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5940 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5941 } else if(strategy == LFS_MINSUM) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5942 /*adaptive filtering*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5943 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5944 size_t smallest = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5945 unsigned char type, bestType = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5946
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5947 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5948 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5949 if(!attempt[type]) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5950 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5951
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5952 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5953 for(y = 0; y != h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5954 /*try the 5 filter types*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5955 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5956 size_t sum = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5957 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5958
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5959 /*calculate the sum of the result*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5960 if(type == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5961 for(x = 0; x != linebytes; ++x) sum += (unsigned char)(attempt[type][x]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5962 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5963 for(x = 0; x != linebytes; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5964 /*For differences, each byte should be treated as signed, values above 127 are negative
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5965 (converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5966 This means filtertype 0 is almost never chosen, but that is justified.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5967 unsigned char s = attempt[type][x];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5968 sum += s < 128 ? s : (255U - s);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5969 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5970 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5971
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5972 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5973 if(type == 0 || sum < smallest) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5974 bestType = type;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5975 smallest = sum;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5976 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5977 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5978
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5979 prevline = &in[y * linebytes];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5980
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5981 /*now fill the out values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5982 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5983 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5984 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5985 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5986
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5987 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5988 } else if(strategy == LFS_ENTROPY) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5989 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5990 size_t bestSum = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5991 unsigned type, bestType = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5992 unsigned count[256];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5993
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5994 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5995 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5996 if(!attempt[type]) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5997 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5998
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
5999 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6000 for(y = 0; y != h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6001 /*try the 5 filter types*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6002 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6003 size_t sum = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6004 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6005 lodepng_memset(count, 0, 256 * sizeof(*count));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6006 for(x = 0; x != linebytes; ++x) ++count[attempt[type][x]];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6007 ++count[type]; /*the filter type itself is part of the scanline*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6008 for(x = 0; x != 256; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6009 sum += ilog2i(count[x]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6010 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6011 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6012 if(type == 0 || sum > bestSum) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6013 bestType = type;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6014 bestSum = sum;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6015 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6016 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6017
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6018 prevline = &in[y * linebytes];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6019
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6020 /*now fill the out values*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6021 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6022 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6023 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6024 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6025
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6026 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6027 } else if(strategy == LFS_PREDEFINED) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6028 for(y = 0; y != h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6029 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6030 size_t inindex = linebytes * y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6031 unsigned char type = settings->predefined_filters[y];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6032 out[outindex] = type; /*filter type byte*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6033 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6034 prevline = &in[inindex];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6035 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6036 } else if(strategy == LFS_BRUTE_FORCE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6037 /*brute force filter chooser.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6038 deflate the scanline after every filter attempt to see which one deflates best.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6039 This is very slow and gives only slightly smaller, sometimes even larger, result*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6040 size_t size[5];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6041 unsigned char* attempt[5]; /*five filtering attempts, one for each filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6042 size_t smallest = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6043 unsigned type = 0, bestType = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6044 unsigned char* dummy;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6045 LodePNGCompressSettings zlibsettings;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6046 lodepng_memcpy(&zlibsettings, &settings->zlibsettings, sizeof(LodePNGCompressSettings));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6047 /*use fixed tree on the attempts so that the tree is not adapted to the filtertype on purpose,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6048 to simulate the true case where the tree is the same for the whole image. Sometimes it gives
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6049 better result with dynamic tree anyway. Using the fixed tree sometimes gives worse, but in rare
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6050 cases better compression. It does make this a bit less slow, so it's worth doing this.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6051 zlibsettings.btype = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6052 /*a custom encoder likely doesn't read the btype setting and is optimized for complete PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6053 images only, so disable it*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6054 zlibsettings.custom_zlib = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6055 zlibsettings.custom_deflate = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6056 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6057 attempt[type] = (unsigned char*)lodepng_malloc(linebytes);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6058 if(!attempt[type]) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6059 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6060 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6061 for(y = 0; y != h; ++y) /*try the 5 filter types*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6062 for(type = 0; type != 5; ++type) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6063 unsigned testsize = (unsigned)linebytes;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6064 /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6065
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6066 filterScanline(attempt[type], &in[y * linebytes], prevline, linebytes, bytewidth, type);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6067 size[type] = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6068 dummy = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6069 zlib_compress(&dummy, &size[type], attempt[type], testsize, &zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6070 lodepng_free(dummy);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6071 /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6072 if(type == 0 || size[type] < smallest) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6073 bestType = type;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6074 smallest = size[type];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6075 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6076 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6077 prevline = &in[y * linebytes];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6078 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6079 for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType][x];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6080 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6081 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6082 for(type = 0; type != 5; ++type) lodepng_free(attempt[type]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6083 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6084 else return 88; /* unknown filter strategy */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6085
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6086 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6087 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6088
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6089 static void addPaddingBits(unsigned char* out, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6090 size_t olinebits, size_t ilinebits, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6091 /*The opposite of the removePaddingBits function
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6092 olinebits must be >= ilinebits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6093 unsigned y;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6094 size_t diff = olinebits - ilinebits;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6095 size_t obp = 0, ibp = 0; /*bit pointers*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6096 for(y = 0; y != h; ++y) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6097 size_t x;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6098 for(x = 0; x < ilinebits; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6099 unsigned char bit = readBitFromReversedStream(&ibp, in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6100 setBitOfReversedStream(&obp, out, bit);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6101 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6102 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6103 "Use of uninitialised value of size ###" warning from valgrind*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6104 for(x = 0; x != diff; ++x) setBitOfReversedStream(&obp, out, 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6105 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6106 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6107
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6108 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6109 in: non-interlaced image with size w*h
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6110 out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6111 no padding bits between scanlines, but between reduced images so that each
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6112 reduced image starts at a byte.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6113 bpp: bits per pixel
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6114 there are no padding bits, not between scanlines, not between reduced images
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6115 in has the following size in bits: w * h * bpp.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6116 out is possibly bigger due to padding bits between reduced images
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6117 NOTE: comments about padding bits are only relevant if bpp < 8
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6118 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6119 static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6120 unsigned passw[7], passh[7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6121 size_t filter_passstart[8], padded_passstart[8], passstart[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6122 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6123
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6124 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6125
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6126 if(bpp >= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6127 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6128 unsigned x, y, b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6129 size_t bytewidth = bpp / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6130 for(y = 0; y < passh[i]; ++y)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6131 for(x = 0; x < passw[i]; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6132 size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6133 size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6134 for(b = 0; b < bytewidth; ++b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6135 out[pixeloutstart + b] = in[pixelinstart + b];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6136 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6137 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6138 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6139 } else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6140 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6141 unsigned x, y, b;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6142 unsigned ilinebits = bpp * passw[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6143 unsigned olinebits = bpp * w;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6144 size_t obp, ibp; /*bit pointers (for out and in buffer)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6145 for(y = 0; y < passh[i]; ++y)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6146 for(x = 0; x < passw[i]; ++x) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6147 ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6148 obp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6149 for(b = 0; b < bpp; ++b) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6150 unsigned char bit = readBitFromReversedStream(&ibp, in);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6151 setBitOfReversedStream(&obp, out, bit);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6152 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6153 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6154 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6155 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6156 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6157
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6158 /*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6159 return value is error**/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6160 static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6161 unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6162 const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6163 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6164 This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6165 *) if no Adam7: 1) add padding bits (= possible extra bits per scanline if bpp < 8) 2) filter
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6166 *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6167 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6168 unsigned bpp = lodepng_get_bpp(&info_png->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6169 unsigned error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6170
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6171 if(info_png->interlace_method == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6172 *outsize = h + (h * ((w * bpp + 7u) / 8u)); /*image size plus an extra byte per scanline + possible padding bits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6173 *out = (unsigned char*)lodepng_malloc(*outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6174 if(!(*out) && (*outsize)) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6175
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6176 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6177 /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6178 if(bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6179 unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7u) / 8u));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6180 if(!padded) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6181 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6182 addPaddingBits(padded, in, ((w * bpp + 7u) / 8u) * 8u, w * bpp, h);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6183 error = filter(*out, padded, w, h, &info_png->color, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6184 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6185 lodepng_free(padded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6186 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6187 /*we can immediately filter into the out buffer, no other steps needed*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6188 error = filter(*out, in, w, h, &info_png->color, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6189 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6190 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6191 } else /*interlace_method is 1 (Adam7)*/ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6192 unsigned passw[7], passh[7];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6193 size_t filter_passstart[8], padded_passstart[8], passstart[8];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6194 unsigned char* adam7;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6195
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6196 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6197
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6198 *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6199 *out = (unsigned char*)lodepng_malloc(*outsize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6200 if(!(*out)) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6201
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6202 adam7 = (unsigned char*)lodepng_malloc(passstart[7]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6203 if(!adam7 && passstart[7]) error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6204
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6205 if(!error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6206 unsigned i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6207
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6208 Adam7_interlace(adam7, in, w, h, bpp);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6209 for(i = 0; i != 7; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6210 if(bpp < 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6211 unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6212 if(!padded) ERROR_BREAK(83); /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6213 addPaddingBits(padded, &adam7[passstart[i]],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6214 ((passw[i] * bpp + 7u) / 8u) * 8u, passw[i] * bpp, passh[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6215 error = filter(&(*out)[filter_passstart[i]], padded,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6216 passw[i], passh[i], &info_png->color, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6217 lodepng_free(padded);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6218 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6219 error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6220 passw[i], passh[i], &info_png->color, settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6221 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6222
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6223 if(error) break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6224 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6225 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6226
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6227 lodepng_free(adam7);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6228 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6229
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6230 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6231 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6232
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6233 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6234 static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6235 unsigned char* inchunk = data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6236 while((size_t)(inchunk - data) < datasize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6237 CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk));
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6238 out->allocsize = out->size; /*fix the allocsize again*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6239 inchunk = lodepng_chunk_next(inchunk, data + datasize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6240 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6241 return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6242 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6243
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6244 static unsigned isGrayICCProfile(const unsigned char* profile, unsigned size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6245 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6246 It is a gray profile if bytes 16-19 are "GRAY", rgb profile if bytes 16-19
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6247 are "RGB ". We do not perform any full parsing of the ICC profile here, other
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6248 than check those 4 bytes to grayscale profile. Other than that, validity of
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6249 the profile is not checked. This is needed only because the PNG specification
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6250 requires using a non-gray color model if there is an ICC profile with "RGB "
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6251 (sadly limiting compression opportunities if the input data is grayscale RGB
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6252 data), and requires using a gray color model if it is "GRAY".
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6253 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6254 if(size < 20) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6255 return profile[16] == 'G' && profile[17] == 'R' && profile[18] == 'A' && profile[19] == 'Y';
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6256 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6257
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6258 static unsigned isRGBICCProfile(const unsigned char* profile, unsigned size) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6259 /* See comment in isGrayICCProfile*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6260 if(size < 20) return 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6261 return profile[16] == 'R' && profile[17] == 'G' && profile[18] == 'B' && profile[19] == ' ';
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6262 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6263 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6264
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6265 unsigned lodepng_encode(unsigned char** out, size_t* outsize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6266 const unsigned char* image, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6267 LodePNGState* state) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6268 unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6269 size_t datasize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6270 ucvector outv = ucvector_init(NULL, 0);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6271 LodePNGInfo info;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6272 const LodePNGInfo* info_png = &state->info_png;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6273 LodePNGColorMode auto_color;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6274
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6275 lodepng_info_init(&info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6276 lodepng_color_mode_init(&auto_color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6277
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6278 /*provide some proper output values if error will happen*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6279 *out = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6280 *outsize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6281 state->error = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6282
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6283 /*check input values validity*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6284 if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6285 && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6286 /*this error is returned even if auto_convert is enabled and thus encoder could
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6287 generate the palette by itself: while allowing this could be possible in theory,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6288 it may complicate the code or edge cases, and always requiring to give a palette
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6289 when setting this color type is a simpler contract*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6290 state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6291 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6292 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6293 if(state->encoder.zlibsettings.btype > 2) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6294 state->error = 61; /*error: invalid btype*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6295 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6296 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6297 if(info_png->interlace_method > 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6298 state->error = 71; /*error: invalid interlace mode*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6299 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6300 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6301 state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6302 if(state->error) goto cleanup; /*error: invalid color type given*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6303 state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6304 if(state->error) goto cleanup; /*error: invalid color type given*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6305
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6306 /* color convert and compute scanline filter types */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6307 lodepng_info_copy(&info, &state->info_png);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6308 if(state->encoder.auto_convert) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6309 LodePNGColorStats stats;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6310 unsigned allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6311 lodepng_color_stats_init(&stats);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6312 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6313 if(info_png->iccp_defined &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6314 isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6315 /*the PNG specification does not allow to use palette with a GRAY ICC profile, even
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6316 if the palette has only gray colors, so disallow it.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6317 stats.allow_palette = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6318 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6319 if(info_png->iccp_defined &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6320 isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6321 /*the PNG specification does not allow to use grayscale color with RGB ICC profile, so disallow gray.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6322 stats.allow_greyscale = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6323 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6324 #endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6325 state->error = lodepng_compute_color_stats(&stats, image, w, h, &state->info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6326 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6327 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6328 if(info_png->background_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6329 /*the background chunk's color must be taken into account as well*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6330 unsigned r = 0, g = 0, b = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6331 LodePNGColorMode mode16 = lodepng_color_mode_make(LCT_RGB, 16);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6332 lodepng_convert_rgb(&r, &g, &b,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6333 info_png->background_r, info_png->background_g, info_png->background_b, &mode16, &info_png->color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6334 state->error = lodepng_color_stats_add(&stats, r, g, b, 65535);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6335 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6336 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6337 #endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6338 state->error = auto_choose_color(&auto_color, &state->info_raw, &stats);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6339 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6340 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6341 if(info_png->sbit_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6342 /*if sbit is defined, due to strict requirements of which sbit values can be present for which color modes,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6343 auto_convert can't be done in many cases. However, do support a few cases here.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6344 TODO: more conversions may be possible, and it may also be possible to get a more appropriate color type out of
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6345 auto_choose_color if knowledge about sbit is used beforehand
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6346 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6347 unsigned sbit_max = LODEPNG_MAX(LODEPNG_MAX(LODEPNG_MAX(info_png->sbit_r, info_png->sbit_g),
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6348 info_png->sbit_b), info_png->sbit_a);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6349 unsigned equal = (!info_png->sbit_g || info_png->sbit_g == info_png->sbit_r)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6350 && (!info_png->sbit_b || info_png->sbit_b == info_png->sbit_r)
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6351 && (!info_png->sbit_a || info_png->sbit_a == info_png->sbit_r);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6352 allow_convert = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6353 if(info.color.colortype == LCT_PALETTE &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6354 auto_color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6355 /* input and output are palette, and in this case it may happen that palette data is
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6356 expected to be copied from info_raw into the info_png */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6357 allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6358 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6359 /*going from 8-bit RGB to palette (or 16-bit as long as sbit_max <= 8) is possible
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6360 since both are 8-bit RGB for sBIT's purposes*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6361 if(info.color.colortype == LCT_RGB &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6362 auto_color.colortype == LCT_PALETTE && sbit_max <= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6363 allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6364 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6365 /*going from 8-bit RGBA to palette is also ok but only if sbit_a is exactly 8*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6366 if(info.color.colortype == LCT_RGBA && auto_color.colortype == LCT_PALETTE &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6367 info_png->sbit_a == 8 && sbit_max <= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6368 allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6369 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6370 /*going from 16-bit RGB(A) to 8-bit RGB(A) is ok if all sbit values are <= 8*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6371 if((info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA) && info.color.bitdepth == 16 &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6372 auto_color.colortype == info.color.colortype && auto_color.bitdepth == 8 &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6373 sbit_max <= 8) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6374 allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6375 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6376 /*going to less channels is ok if all bit values are equal (all possible values in sbit,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6377 as well as the chosen bitdepth of the result). Due to how auto_convert works,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6378 we already know that auto_color.colortype has less than or equal amount of channels than
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6379 info.colortype. Palette is not used here. This conversion is not allowed if
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6380 info_png->sbit_r < auto_color.bitdepth, because specifically for alpha, non-presence of
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6381 an sbit value heavily implies that alpha's bit depth is equal to the PNG bit depth (rather
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6382 than the bit depths set in the r, g and b sbit values, by how the PNG specification describes
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6383 handling tRNS chunk case with sBIT), so be conservative here about ignoring user input.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6384 if(info.color.colortype != LCT_PALETTE && auto_color.colortype != LCT_PALETTE &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6385 equal && info_png->sbit_r == auto_color.bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6386 allow_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6387 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6388 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6389 #endif
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6390 if(state->encoder.force_palette) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6391 if(info.color.colortype != LCT_GREY && info.color.colortype != LCT_GREY_ALPHA &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6392 (auto_color.colortype == LCT_GREY || auto_color.colortype == LCT_GREY_ALPHA)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6393 /*user speficially forced a PLTE palette, so cannot convert to grayscale types because
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6394 the PNG specification only allows writing a suggested palette in PLTE for truecolor types*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6395 allow_convert = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6396 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6397 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6398 if(allow_convert) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6399 lodepng_color_mode_copy(&info.color, &auto_color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6400 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6401 /*also convert the background chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6402 if(info_png->background_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6403 if(lodepng_convert_rgb(&info.background_r, &info.background_g, &info.background_b,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6404 info_png->background_r, info_png->background_g, info_png->background_b, &info.color, &info_png->color)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6405 state->error = 104;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6406 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6407 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6408 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6409 #endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6410 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6411 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6412 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6413 if(info_png->iccp_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6414 unsigned gray_icc = isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6415 unsigned rgb_icc = isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6416 unsigned gray_png = info.color.colortype == LCT_GREY || info.color.colortype == LCT_GREY_ALPHA;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6417 if(!gray_icc && !rgb_icc) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6418 state->error = 100; /* Disallowed profile color type for PNG */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6419 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6420 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6421 if(gray_icc != gray_png) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6422 /*Not allowed to use RGB/RGBA/palette with GRAY ICC profile or vice versa,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6423 or in case of auto_convert, it wasn't possible to find appropriate model*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6424 state->error = state->encoder.auto_convert ? 102 : 101;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6425 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6426 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6427 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6428 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6429 if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6430 unsigned char* converted;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6431 size_t size = ((size_t)w * (size_t)h * (size_t)lodepng_get_bpp(&info.color) + 7u) / 8u;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6432
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6433 converted = (unsigned char*)lodepng_malloc(size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6434 if(!converted && size) state->error = 83; /*alloc fail*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6435 if(!state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6436 state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6437 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6438 if(!state->error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6439 state->error = preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6440 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6441 lodepng_free(converted);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6442 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6443 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6444 state->error = preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6445 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6446 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6447
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6448 /* output all PNG chunks */ {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6449 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6450 size_t i;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6451 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6452 /*write signature and chunks*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6453 state->error = writeSignature(&outv);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6454 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6455 /*IHDR*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6456 state->error = addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6457 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6458 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6459 /*unknown chunks between IHDR and PLTE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6460 if(info.unknown_chunks_data[0]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6461 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6462 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6463 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6464 /*color profile chunks must come before PLTE */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6465 if(info.iccp_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6466 state->error = addChunk_iCCP(&outv, &info, &state->encoder.zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6467 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6468 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6469 if(info.srgb_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6470 state->error = addChunk_sRGB(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6471 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6472 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6473 if(info.gama_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6474 state->error = addChunk_gAMA(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6475 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6476 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6477 if(info.chrm_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6478 state->error = addChunk_cHRM(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6479 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6480 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6481 if(info_png->sbit_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6482 state->error = addChunk_sBIT(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6483 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6484 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6485 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6486 /*PLTE*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6487 if(info.color.colortype == LCT_PALETTE) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6488 state->error = addChunk_PLTE(&outv, &info.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6489 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6490 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6491 if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA)) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6492 /*force_palette means: write suggested palette for truecolor in PLTE chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6493 state->error = addChunk_PLTE(&outv, &info.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6494 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6495 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6496 /*tRNS (this will only add if when necessary) */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6497 state->error = addChunk_tRNS(&outv, &info.color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6498 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6499 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6500 /*bKGD (must come between PLTE and the IDAt chunks*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6501 if(info.background_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6502 state->error = addChunk_bKGD(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6503 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6504 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6505 /*pHYs (must come before the IDAT chunks)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6506 if(info.phys_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6507 state->error = addChunk_pHYs(&outv, &info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6508 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6509 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6510
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6511 /*unknown chunks between PLTE and IDAT*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6512 if(info.unknown_chunks_data[1]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6513 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6514 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6515 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6516 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6517 /*IDAT (multiple IDAT chunks must be consecutive)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6518 state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6519 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6520 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6521 /*tIME*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6522 if(info.time_defined) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6523 state->error = addChunk_tIME(&outv, &info.time);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6524 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6525 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6526 /*tEXt and/or zTXt*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6527 for(i = 0; i != info.text_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6528 if(lodepng_strlen(info.text_keys[i]) > 79) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6529 state->error = 66; /*text chunk too large*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6530 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6531 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6532 if(lodepng_strlen(info.text_keys[i]) < 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6533 state->error = 67; /*text chunk too small*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6534 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6535 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6536 if(state->encoder.text_compression) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6537 state->error = addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6538 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6539 } else {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6540 state->error = addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6541 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6542 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6543 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6544 /*LodePNG version id in text chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6545 if(state->encoder.add_id) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6546 unsigned already_added_id_text = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6547 for(i = 0; i != info.text_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6548 const char* k = info.text_keys[i];
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6549 /* Could use strcmp, but we're not calling or reimplementing this C library function for this use only */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6550 if(k[0] == 'L' && k[1] == 'o' && k[2] == 'd' && k[3] == 'e' &&
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6551 k[4] == 'P' && k[5] == 'N' && k[6] == 'G' && k[7] == '\0') {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6552 already_added_id_text = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6553 break;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6554 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6555 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6556 if(already_added_id_text == 0) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6557 state->error = addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6558 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6559 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6560 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6561 /*iTXt*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6562 for(i = 0; i != info.itext_num; ++i) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6563 if(lodepng_strlen(info.itext_keys[i]) > 79) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6564 state->error = 66; /*text chunk too large*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6565 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6566 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6567 if(lodepng_strlen(info.itext_keys[i]) < 1) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6568 state->error = 67; /*text chunk too small*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6569 goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6570 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6571 state->error = addChunk_iTXt(
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6572 &outv, state->encoder.text_compression,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6573 info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i],
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6574 &state->encoder.zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6575 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6576 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6577
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6578 /*unknown chunks between IDAT and IEND*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6579 if(info.unknown_chunks_data[2]) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6580 state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6581 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6582 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6583 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6584 state->error = addChunk_IEND(&outv);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6585 if(state->error) goto cleanup;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6586 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6587
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6588 cleanup:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6589 lodepng_info_cleanup(&info);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6590 lodepng_free(data);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6591 lodepng_color_mode_cleanup(&auto_color);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6592
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6593 /*instead of cleaning the vector up, give it to the output*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6594 *out = outv.data;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6595 *outsize = outv.size;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6596
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6597 return state->error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6598 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6599
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6600 unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsigned char* image,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6601 unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6602 unsigned error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6603 LodePNGState state;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6604 lodepng_state_init(&state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6605 state.info_raw.colortype = colortype;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6606 state.info_raw.bitdepth = bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6607 state.info_png.color.colortype = colortype;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6608 state.info_png.color.bitdepth = bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6609 lodepng_encode(out, outsize, image, w, h, &state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6610 error = state.error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6611 lodepng_state_cleanup(&state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6612 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6613 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6614
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6615 unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6616 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGBA, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6617 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6618
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6619 unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6620 return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGB, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6621 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6622
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6623 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6624 unsigned lodepng_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6625 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6626 unsigned char* buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6627 size_t buffersize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6628 unsigned error = lodepng_encode_memory(&buffer, &buffersize, image, w, h, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6629 if(!error) error = lodepng_save_file(buffer, buffersize, filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6630 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6631 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6632 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6633
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6634 unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6635 return lodepng_encode_file(filename, image, w, h, LCT_RGBA, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6636 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6637
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6638 unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6639 return lodepng_encode_file(filename, image, w, h, LCT_RGB, 8);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6640 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6641 #endif /*LODEPNG_COMPILE_DISK*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6642
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6643 void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6644 lodepng_compress_settings_init(&settings->zlibsettings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6645 settings->filter_palette_zero = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6646 settings->filter_strategy = LFS_MINSUM;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6647 settings->auto_convert = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6648 settings->force_palette = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6649 settings->predefined_filters = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6650 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6651 settings->add_id = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6652 settings->text_compression = 1;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6653 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6654 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6655
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6656 #endif /*LODEPNG_COMPILE_ENCODER*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6657 #endif /*LODEPNG_COMPILE_PNG*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6658
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6659 #ifdef LODEPNG_COMPILE_ERROR_TEXT
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6660 /*
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6661 This returns the description of a numerical error code in English. This is also
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6662 the documentation of all the error codes.
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6663 */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6664 const char* lodepng_error_text(unsigned code) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6665 switch(code) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6666 case 0: return "no error, everything went ok";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6667 case 1: return "nothing done yet"; /*the Encoder/Decoder has done nothing yet, error checking makes no sense yet*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6668 case 10: return "end of input memory reached without huffman end code"; /*while huffman decoding*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6669 case 11: return "error in code tree made it jump outside of huffman tree"; /*while huffman decoding*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6670 case 13: return "problem while processing dynamic deflate block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6671 case 14: return "problem while processing dynamic deflate block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6672 case 15: return "problem while processing dynamic deflate block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6673 /*this error could happen if there are only 0 or 1 symbols present in the huffman code:*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6674 case 16: return "invalid code while processing dynamic deflate block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6675 case 17: return "end of out buffer memory reached while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6676 case 18: return "invalid distance code while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6677 case 19: return "end of out buffer memory reached while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6678 case 20: return "invalid deflate block BTYPE encountered while decoding";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6679 case 21: return "NLEN is not ones complement of LEN in a deflate block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6680
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6681 /*end of out buffer memory reached while inflating:
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6682 This can happen if the inflated deflate data is longer than the amount of bytes required to fill up
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6683 all the pixels of the image, given the color depth and image dimensions. Something that doesn't
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6684 happen in a normal, well encoded, PNG image.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6685 case 22: return "end of out buffer memory reached while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6686 case 23: return "end of in buffer memory reached while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6687 case 24: return "invalid FCHECK in zlib header";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6688 case 25: return "invalid compression method in zlib header";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6689 case 26: return "FDICT encountered in zlib header while it's not used for PNG";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6690 case 27: return "PNG file is smaller than a PNG header";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6691 /*Checks the magic file header, the first 8 bytes of the PNG file*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6692 case 28: return "incorrect PNG signature, it's no PNG or corrupted";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6693 case 29: return "first chunk is not the header chunk";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6694 case 30: return "chunk length too large, chunk broken off at end of file";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6695 case 31: return "illegal PNG color type or bpp";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6696 case 32: return "illegal PNG compression method";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6697 case 33: return "illegal PNG filter method";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6698 case 34: return "illegal PNG interlace method";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6699 case 35: return "chunk length of a chunk is too large or the chunk too small";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6700 case 36: return "illegal PNG filter type encountered";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6701 case 37: return "illegal bit depth for this color type given";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6702 case 38: return "the palette is too small or too big"; /*0, or more than 256 colors*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6703 case 39: return "tRNS chunk before PLTE or has more entries than palette size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6704 case 40: return "tRNS chunk has wrong size for grayscale image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6705 case 41: return "tRNS chunk has wrong size for RGB image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6706 case 42: return "tRNS chunk appeared while it was not allowed for this color type";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6707 case 43: return "bKGD chunk has wrong size for palette image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6708 case 44: return "bKGD chunk has wrong size for grayscale image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6709 case 45: return "bKGD chunk has wrong size for RGB image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6710 case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6711 case 49: return "jumped past memory while generating dynamic huffman tree";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6712 case 50: return "jumped past memory while generating dynamic huffman tree";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6713 case 51: return "jumped past memory while inflating huffman block";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6714 case 52: return "jumped past memory while inflating";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6715 case 53: return "size of zlib data too small";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6716 case 54: return "repeat symbol in tree while there was no value symbol yet";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6717 /*jumped past tree while generating huffman tree, this could be when the
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6718 tree will have more leaves than symbols after generating it out of the
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6719 given lengths. They call this an oversubscribed dynamic bit lengths tree in zlib.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6720 case 55: return "jumped past tree while generating huffman tree";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6721 case 56: return "given output image colortype or bitdepth not supported for color conversion";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6722 case 57: return "invalid CRC encountered (checking CRC can be disabled)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6723 case 58: return "invalid ADLER32 encountered (checking ADLER32 can be disabled)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6724 case 59: return "requested color conversion not supported";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6725 case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6726 case 61: return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6727 /*LodePNG leaves the choice of RGB to grayscale conversion formula to the user.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6728 case 62: return "conversion from color to grayscale not supported";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6729 /*(2^31-1)*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6730 case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6731 /*this would result in the inability of a deflated block to ever contain an end code. It must be at least 1.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6732 case 64: return "the length of the END symbol 256 in the Huffman tree is 0";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6733 case 66: return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6734 case 67: return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6735 case 68: return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6736 case 69: return "unknown chunk type with 'critical' flag encountered by the decoder";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6737 case 71: return "invalid interlace mode given to encoder (must be 0 or 1)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6738 case 72: return "while decoding, invalid compression method encountering in zTXt or iTXt chunk (it must be 0)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6739 case 73: return "invalid tIME chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6740 case 74: return "invalid pHYs chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6741 /*length could be wrong, or data chopped off*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6742 case 75: return "no null termination char found while decoding text chunk";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6743 case 76: return "iTXt chunk too short to contain required bytes";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6744 case 77: return "integer overflow in buffer size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6745 case 78: return "failed to open file for reading"; /*file doesn't exist or couldn't be opened for reading*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6746 case 79: return "failed to open file for writing";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6747 case 80: return "tried creating a tree of 0 symbols";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6748 case 81: return "lazy matching at pos 0 is impossible";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6749 case 82: return "color conversion to palette requested while a color isn't in palette, or index out of bounds";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6750 case 83: return "memory allocation failed";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6751 case 84: return "given image too small to contain all pixels to be encoded";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6752 case 86: return "impossible offset in lz77 encoding (internal bug)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6753 case 87: return "must provide custom zlib function pointer if LODEPNG_COMPILE_ZLIB is not defined";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6754 case 88: return "invalid filter strategy given for LodePNGEncoderSettings.filter_strategy";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6755 case 89: return "text chunk keyword too short or long: must have size 1-79";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6756 /*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6757 case 90: return "windowsize must be a power of two";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6758 case 91: return "invalid decompressed idat size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6759 case 92: return "integer overflow due to too many pixels";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6760 case 93: return "zero width or height is invalid";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6761 case 94: return "header chunk must have a size of 13 bytes";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6762 case 95: return "integer overflow with combined idat chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6763 case 96: return "invalid gAMA chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6764 case 97: return "invalid cHRM chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6765 case 98: return "invalid sRGB chunk size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6766 case 99: return "invalid sRGB rendering intent";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6767 case 100: return "invalid ICC profile color type, the PNG specification only allows RGB or GRAY";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6768 case 101: return "PNG specification does not allow RGB ICC profile on gray color types and vice versa";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6769 case 102: return "not allowed to set grayscale ICC profile with colored pixels by PNG specification";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6770 case 103: return "invalid palette index in bKGD chunk. Maybe it came before PLTE chunk?";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6771 case 104: return "invalid bKGD color while encoding (e.g. palette index out of range)";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6772 case 105: return "integer overflow of bitsize";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6773 case 106: return "PNG file must have PLTE chunk if color type is palette";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6774 case 107: return "color convert from palette mode requested without setting the palette data in it";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6775 case 108: return "tried to add more than 256 values to a palette";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6776 /*this limit can be configured in LodePNGDecompressSettings*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6777 case 109: return "tried to decompress zlib or deflate data larger than desired max_output_size";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6778 case 110: return "custom zlib or inflate decompression failed";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6779 case 111: return "custom zlib or deflate compression failed";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6780 /*max text size limit can be configured in LodePNGDecoderSettings. This error prevents
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6781 unreasonable memory consumption when decoding due to impossibly large text sizes.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6782 case 112: return "compressed text unreasonably large";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6783 /*max ICC size limit can be configured in LodePNGDecoderSettings. This error prevents
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6784 unreasonable memory consumption when decoding due to impossibly large ICC profile*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6785 case 113: return "ICC profile unreasonably large";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6786 case 114: return "sBIT chunk has wrong size for the color type of the image";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6787 case 115: return "sBIT value out of range";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6788 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6789 return "unknown error code";
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6790 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6791 #endif /*LODEPNG_COMPILE_ERROR_TEXT*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6792
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6793 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6794 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6795 /* // C++ Wrapper // */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6796 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6797 /* ////////////////////////////////////////////////////////////////////////// */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6798
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6799 #ifdef LODEPNG_COMPILE_CPP
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6800 namespace lodepng {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6801
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6802 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6803 unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6804 long size = lodepng_filesize(filename.c_str());
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6805 if(size < 0) return 78;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6806 buffer.resize((size_t)size);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6807 return size == 0 ? 0 : lodepng_buffer_file(&buffer[0], (size_t)size, filename.c_str());
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6808 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6809
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6810 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6811 unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6812 return lodepng_save_file(buffer.empty() ? 0 : &buffer[0], buffer.size(), filename.c_str());
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6813 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6814 #endif /* LODEPNG_COMPILE_DISK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6815
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6816 #ifdef LODEPNG_COMPILE_ZLIB
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6817 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6818 unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6819 const LodePNGDecompressSettings& settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6820 unsigned char* buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6821 size_t buffersize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6822 unsigned error = zlib_decompress(&buffer, &buffersize, 0, in, insize, &settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6823 if(buffer) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6824 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6825 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6826 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6827 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6828 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6829
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6830 unsigned decompress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6831 const LodePNGDecompressSettings& settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6832 return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6833 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6834 #endif /* LODEPNG_COMPILE_DECODER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6835
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6836 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6837 unsigned compress(std::vector<unsigned char>& out, const unsigned char* in, size_t insize,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6838 const LodePNGCompressSettings& settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6839 unsigned char* buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6840 size_t buffersize = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6841 unsigned error = zlib_compress(&buffer, &buffersize, in, insize, &settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6842 if(buffer) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6843 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6844 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6845 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6846 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6847 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6848
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6849 unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned char>& in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6850 const LodePNGCompressSettings& settings) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6851 return compress(out, in.empty() ? 0 : &in[0], in.size(), settings);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6852 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6853 #endif /* LODEPNG_COMPILE_ENCODER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6854 #endif /* LODEPNG_COMPILE_ZLIB */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6855
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6856
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6857 #ifdef LODEPNG_COMPILE_PNG
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6858
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6859 State::State() {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6860 lodepng_state_init(this);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6861 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6862
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6863 State::State(const State& other) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6864 lodepng_state_init(this);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6865 lodepng_state_copy(this, &other);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6866 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6867
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6868 State::~State() {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6869 lodepng_state_cleanup(this);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6870 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6871
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6872 State& State::operator=(const State& other) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6873 lodepng_state_copy(this, &other);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6874 return *this;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6875 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6876
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6877 #ifdef LODEPNG_COMPILE_DECODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6878
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6879 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const unsigned char* in,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6880 size_t insize, LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6881 unsigned char* buffer = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6882 unsigned error = lodepng_decode_memory(&buffer, &w, &h, in, insize, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6883 if(buffer && !error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6884 State state;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6885 state.info_raw.colortype = colortype;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6886 state.info_raw.bitdepth = bitdepth;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6887 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6888 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6889 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6890 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6891 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6892 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6893
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6894 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6895 const std::vector<unsigned char>& in, LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6896 return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6897 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6898
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6899 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6900 State& state,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6901 const unsigned char* in, size_t insize) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6902 unsigned char* buffer = NULL;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6903 unsigned error = lodepng_decode(&buffer, &w, &h, &state, in, insize);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6904 if(buffer && !error) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6905 size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6906 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6907 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6908 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6909 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6910 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6911
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6912 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6913 State& state,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6914 const std::vector<unsigned char>& in) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6915 return decode(out, w, h, state, in.empty() ? 0 : &in[0], in.size());
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6916 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6917
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6918 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6919 unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6920 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6921 std::vector<unsigned char> buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6922 /* safe output values in case error happens */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6923 w = h = 0;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6924 unsigned error = load_file(buffer, filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6925 if(error) return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6926 return decode(out, w, h, buffer, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6927 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6928 #endif /* LODEPNG_COMPILE_DECODER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6929 #endif /* LODEPNG_COMPILE_DISK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6930
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6931 #ifdef LODEPNG_COMPILE_ENCODER
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6932 unsigned encode(std::vector<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6933 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6934 unsigned char* buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6935 size_t buffersize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6936 unsigned error = lodepng_encode_memory(&buffer, &buffersize, in, w, h, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6937 if(buffer) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6938 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6939 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6940 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6941 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6942 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6943
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6944 unsigned encode(std::vector<unsigned char>& out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6945 const std::vector<unsigned char>& in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6946 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6947 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6948 return encode(out, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6949 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6950
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6951 unsigned encode(std::vector<unsigned char>& out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6952 const unsigned char* in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6953 State& state) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6954 unsigned char* buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6955 size_t buffersize;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6956 unsigned error = lodepng_encode(&buffer, &buffersize, in, w, h, &state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6957 if(buffer) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6958 out.insert(out.end(), buffer, &buffer[buffersize]);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6959 lodepng_free(buffer);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6960 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6961 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6962 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6963
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6964 unsigned encode(std::vector<unsigned char>& out,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6965 const std::vector<unsigned char>& in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6966 State& state) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6967 if(lodepng_get_raw_size(w, h, &state.info_raw) > in.size()) return 84;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6968 return encode(out, in.empty() ? 0 : &in[0], w, h, state);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6969 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6970
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6971 #ifdef LODEPNG_COMPILE_DISK
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6972 unsigned encode(const std::string& filename,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6973 const unsigned char* in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6974 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6975 std::vector<unsigned char> buffer;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6976 unsigned error = encode(buffer, in, w, h, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6977 if(!error) error = save_file(buffer, filename);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6978 return error;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6979 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6980
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6981 unsigned encode(const std::string& filename,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6982 const std::vector<unsigned char>& in, unsigned w, unsigned h,
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6983 LodePNGColorType colortype, unsigned bitdepth) {
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6984 if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84;
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6985 return encode(filename, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth);
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6986 }
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6987 #endif /* LODEPNG_COMPILE_DISK */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6988 #endif /* LODEPNG_COMPILE_ENCODER */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6989 #endif /* LODEPNG_COMPILE_PNG */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6990 } /* namespace lodepng */
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
diff changeset
6991 #endif /*LODEPNG_COMPILE_CPP*/