LodePNG version 20160124 Copyright (c) 2005-2016 Lode Vandevenne LodePNG is a PNG image decoder and encoder, all in one, no dependency or linkage to zlib or libpng required. It's made for C (ISO C90), and has a C++ wrapper with a more convenient interface on top. https://github.com/lvandeve/lodepng

Committer:
unix_guru
Date:
Tue Feb 23 18:19:20 2016 +0000
Revision:
0:6b244485c156
First version commit
; LodePNG version 20160124
;
; Copyright (c) 2005-2016 Lode Vandevenne

Who changed what in which revision?

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