1 /*
2 LodePNG version 20080927
3
4 Copyright (c) 2005-2008 Lode Vandevenne
5
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
13
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21
22 3. This notice may not be removed or altered from any source
23 distribution.
24 */
25
26 /*
27 The manual and changelog can be found in the header file "lodepng.h"
28 You are free to name this file lodepng.cpp or lodepng.c depending on your usage.
29 */
30
31 #include "lodepng.h"
32
33 #define USE_BRUTE_FORCE_ENCODING 1
34
35 #define VERSION_STRING "20080927"
36
37 /* ////////////////////////////////////////////////////////////////////////// */
38 /* / Tools For C / */
39 /* ////////////////////////////////////////////////////////////////////////// */
40
41 /*
42 About these tools (vector, uivector, ucvector and string):
43 -LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version.
44 -The string tools are made to avoid problems with compilers that declare things like strncat as deprecated.
45 -They're not used in the interface, only internally in this file, so all their functions are made static.
46 */
47
48 //--------------------------------------------------------------------------------------------
49
50
51 /*LodePNG_chunk functions: These functions need as input a large enough amount of allocated memory.*/
52
53 static unsigned LodePNG_chunk_length(const unsigned char* chunk); /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/
54
55 static void LodePNG_chunk_generate_crc(unsigned char* chunk); /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/
56
57 /*add chunks to out buffer. It reallocs the buffer to append the data. returns error code*/
58 static unsigned LodePNG_create_chunk(unsigned char** out, size_t* outlength, unsigned length, const char* type, const unsigned char* data); /*appends new chunk to out. Returns pointer to start of appended chunk, or NULL if error happened; may change memory address of out buffer*/
59
60 static void LodePNG_InfoColor_init(LodePNG_InfoColor* info);
61 static void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info);
62 static unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor* dest, const LodePNG_InfoColor* source);
63
64 /*Use these functions instead of allocating palette manually*/
65 static void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor* info);
66
67 /*additional color info*/
68 static unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor* info); /*bits per pixel*/
69 static unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor* info); /*is it a greyscale type? (colorType 0 or 4)*/
70 static unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor* info); /*has it an alpha channel? (colorType 2 or 6)*/
71
72 static void LodePNG_InfoPng_init(LodePNG_InfoPng* info);
73 static void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info);
74 static unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng* dest, const LodePNG_InfoPng* source);
75
76 static void LodePNG_InfoRaw_init(LodePNG_InfoRaw* info);
77 static void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw* info);
78 static unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw* dest, const LodePNG_InfoRaw* source);
79
80 /*
81 LodePNG_convert: Converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code
82 The out buffer must have (w * h * bpp + 7) / 8, where bpp is the bits per pixel of the output color type (LodePNG_InfoColor_getBpp)
83 */
84 static unsigned LodePNG_convert(unsigned char* out, const unsigned char* in, LodePNG_InfoColor* infoOut, LodePNG_InfoColor* infoIn, unsigned w, unsigned h);
85
86 static void LodeZlib_DeflateSettings_init(LodeZlib_DeflateSettings* settings);
87
88 /* ////////////////////////////////////////////////////////////////////////// */
89 /* LodeFlate & LodeZlib */
90 /* ////////////////////////////////////////////////////////////////////////// */
91
92 /*This function reallocates the out buffer and appends the data.
93 Either, *out must be NULL and *outsize must be 0, or, *out must be a valid buffer and *outsize its size in bytes.*/
94 //unsigned LodeZlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DeflateSettings* settings);
95
96 //--------------------------------------------------------------------------------------------
97
98 typedef struct vector /*this one is used only by the deflate compressor*/
99 {
100 void* data;
101 size_t size; /*in groups of bytes depending on type*/
102 size_t allocsize; /*in bytes*/
103 unsigned typesize; /*sizeof the type you store in data*/
104 } vector;
105
vector_resize(vector * p,size_t size)106 static unsigned vector_resize(vector* p, size_t size) /*returns 1 if success, 0 if failure ==> nothing done*/
107 {
108 if(size * p->typesize > p->allocsize)
109 {
110 size_t newsize = size * p->typesize * 2;
111 void* data = realloc(p->data, newsize);
112 if(data)
113 {
114 p->allocsize = newsize;
115 p->data = data;
116 p->size = size;
117 }
118 else return 0;
119 }
120 else p->size = size;
121 return 1;
122 }
123
vector_resized(vector * p,size_t size,void dtor (void *))124 static unsigned vector_resized(vector* p, size_t size, void dtor(void*)) /*resize and use destructor on elements if it gets smaller*/
125 {
126 size_t i;
127 if(size < p->size) for(i = size; i < p->size; i++) dtor(&((char*)(p->data))[i * p->typesize]);
128 return vector_resize(p, size);
129 }
130
vector_cleanup(void * p)131 static void vector_cleanup(void* p)
132 {
133 ((vector*)p)->size = ((vector*)p)->allocsize = 0;
134 free(((vector*)p)->data);
135 ((vector*)p)->data = NULL;
136 }
137
vector_cleanupd(vector * p,void dtor (void *))138 static void vector_cleanupd(vector* p, void dtor(void*)) /*clear and use destructor on elements*/
139 {
140 vector_resized(p, 0, dtor);
141 vector_cleanup(p);
142 }
143
vector_init(vector * p,unsigned typesize)144 static void vector_init(vector* p, unsigned typesize)
145 {
146 p->data = NULL;
147 p->size = p->allocsize = 0;
148 p->typesize = typesize;
149 }
150
vector_swap(vector * p,vector * q)151 static void vector_swap(vector* p, vector* q) /*they're supposed to have the same typesize*/
152 {
153 size_t tmp;
154 void* tmpp;
155 tmp = p->size; p->size = q->size; q->size = tmp;
156 tmp = p->allocsize; p->allocsize = q->allocsize; q->allocsize = tmp;
157 tmpp = p->data; p->data = q->data; q->data = tmpp;
158 }
159
vector_get(vector * p,size_t index)160 static void* vector_get(vector* p, size_t index)
161 {
162 return &((char*)p->data)[index * p->typesize];
163 }
164
165 /* /////////////////////////////////////////////////////////////////////////// */
166
167 typedef struct uivector
168 {
169 unsigned* data;
170 size_t size; /*size in number of unsigned longs*/
171 size_t allocsize; /*allocated size in bytes*/
172 } uivector;
173
uivector_cleanup(void * p)174 static void uivector_cleanup(void* p)
175 {
176 ((uivector*)p)->size = ((uivector*)p)->allocsize = 0;
177 free(((uivector*)p)->data);
178 ((uivector*)p)->data = NULL;
179 }
180
uivector_resize(uivector * p,size_t size)181 static unsigned uivector_resize(uivector* p, size_t size) /*returns 1 if success, 0 if failure ==> nothing done*/
182 {
183 if(size * sizeof(unsigned) > p->allocsize)
184 {
185 size_t newsize = size * sizeof(unsigned) * 2;
186 void* data = realloc(p->data, newsize);
187 if(data)
188 {
189 p->allocsize = newsize;
190 p->data = (unsigned*)data;
191 p->size = size;
192 }
193 else return 0;
194 }
195 else p->size = size;
196 return 1;
197 }
198
uivector_resizev(uivector * p,size_t size,unsigned value)199 static unsigned uivector_resizev(uivector* p, size_t size, unsigned value) /*resize and give all new elements the value*/
200 {
201 size_t oldsize = p->size, i;
202 if(!uivector_resize(p, size)) return 0;
203 for(i = oldsize; i < size; i++) p->data[i] = value;
204 return 1;
205 }
206
uivector_init(uivector * p)207 static void uivector_init(uivector* p)
208 {
209 p->data = NULL;
210 p->size = p->allocsize = 0;
211 }
212
uivector_push_back(uivector * p,unsigned c)213 static unsigned uivector_push_back(uivector* p, unsigned c) /*returns 1 if success, 0 if failure ==> nothing done*/
214 {
215 if(!uivector_resize(p, p->size + 1)) return 0;
216 p->data[p->size - 1] = c;
217 return 1;
218 }
219
uivector_copy(uivector * p,const uivector * q)220 static unsigned uivector_copy(uivector* p, const uivector* q) /*copy q to p, returns 1 if success, 0 if failure ==> nothing done*/
221 {
222 size_t i;
223 if(!uivector_resize(p, q->size)) return 0;
224 for(i = 0; i < q->size; i++) p->data[i] = q->data[i];
225 return 1;
226 }
227
uivector_swap(uivector * p,uivector * q)228 static void uivector_swap(uivector* p, uivector* q)
229 {
230 size_t tmp;
231 unsigned* tmpp;
232 tmp = p->size; p->size = q->size; q->size = tmp;
233 tmp = p->allocsize; p->allocsize = q->allocsize; q->allocsize = tmp;
234 tmpp = p->data; p->data = q->data; q->data = tmpp;
235 }
236
237 /* /////////////////////////////////////////////////////////////////////////// */
238
239 typedef struct ucvector
240 {
241 unsigned char* data;
242 size_t size; /*used size*/
243 size_t allocsize; /*allocated size*/
244 } ucvector;
245
ucvector_cleanup(void * p)246 static void ucvector_cleanup(void* p)
247 {
248 ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0;
249 free(((ucvector*)p)->data);
250 ((ucvector*)p)->data = NULL;
251 }
252
ucvector_resize(ucvector * p,size_t size)253 static unsigned ucvector_resize(ucvector* p, size_t size) /*returns 1 if success, 0 if failure ==> nothing done*/
254 {
255 if(size * sizeof(unsigned) > p->allocsize)
256 {
257 size_t newsize = size * sizeof(unsigned) * 2;
258 void* data = realloc(p->data, newsize);
259 if(data)
260 {
261 p->allocsize = newsize;
262 p->data = (unsigned char*)data;
263 p->size = size;
264 }
265 else return 0; /*error: not enough memory*/
266 }
267 else p->size = size;
268 return 1;
269 }
270
271
ucvector_init(ucvector * p)272 static void ucvector_init(ucvector* p)
273 {
274 p->data = NULL;
275 p->size = p->allocsize = 0;
276 }
277
278 /*you can both convert from vector to buffer&size and vica versa*/
ucvector_init_buffer(ucvector * p,unsigned char * buffer,size_t size)279 static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size)
280 {
281 p->data = buffer;
282 p->allocsize = p->size = size;
283 }
284
ucvector_push_back(ucvector * p,unsigned char c)285 static unsigned ucvector_push_back(ucvector* p, unsigned char c) /*returns 1 if success, 0 if failure ==> nothing done*/
286 {
287 if(!ucvector_resize(p, p->size + 1)) return 0;
288 p->data[p->size - 1] = c;
289 return 1;
290 }
291
292 /* ////////////////////////////////////////////////////////////////////////// */
293 /* / Reading and writing single bits and bytes from/to stream for Deflate / */
294 /* ////////////////////////////////////////////////////////////////////////// */
295
addBitToStream(size_t * bitpointer,ucvector * bitstream,unsigned char bit)296 static void addBitToStream(size_t* bitpointer, ucvector* bitstream, unsigned char bit)
297 {
298 if((*bitpointer) % 8 == 0) ucvector_push_back(bitstream, 0); /*add a new byte at the end*/
299 (bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7)); /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/
300 (*bitpointer)++;
301 }
302
addBitsToStream(size_t * bitpointer,ucvector * bitstream,unsigned value,size_t nbits)303 static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
304 {
305 size_t i;
306 for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1));
307 }
308
addBitsToStreamReversed(size_t * bitpointer,ucvector * bitstream,unsigned value,size_t nbits)309 static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
310 {
311 size_t i;
312 for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1));
313 }
314
315
316 /* ////////////////////////////////////////////////////////////////////////// */
317 /* / Deflate - Huffman / */
318 /* ////////////////////////////////////////////////////////////////////////// */
319
320 #define FIRST_LENGTH_CODE_INDEX 257
321 #define LAST_LENGTH_CODE_INDEX 285
322 #define NUM_DEFLATE_CODE_SYMBOLS 288 /*256 literals, the end code, some length codes, and 2 unused codes*/
323 #define NUM_DISTANCE_SYMBOLS 32 /*the distance codes have their own symbols, 30 used, 2 unused*/
324 #define NUM_CODE_LENGTH_CODES 19 /*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/
325
326 static const unsigned LENGTHBASE[29] /*the base lengths represented by codes 257-285*/
327 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
328 static const unsigned LENGTHEXTRA[29] /*the extra bits used by codes 257-285 (added to base length)*/
329 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
330 static const unsigned DISTANCEBASE[30] /*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/
331 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
332 static const unsigned DISTANCEEXTRA[30] /*the extra bits of backwards distances (added to base)*/
333 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
334 static const unsigned CLCL[NUM_CODE_LENGTH_CODES] /*the order in which "code length alphabet code lengths" are stored, out of this the huffman tree of the dynamic huffman tree lengths is generated*/
335 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
336
337 /* /////////////////////////////////////////////////////////////////////////// */
338
339 /*terminology used for the package-merge algorithm and the coin collector's problem*/
340 typedef struct Coin /*a coin can be multiple coins (when they're merged)*/
341 {
342 uivector symbols;
343 float weight; /*the sum of all weights in this coin*/
344 } Coin;
345
Coin_init(Coin * c)346 static void Coin_init(Coin* c)
347 {
348 uivector_init(&c->symbols);
349 }
350
Coin_cleanup(void * c)351 static void Coin_cleanup(void* c) /*void* so that this dtor can be given as function pointer to the vector resize function*/
352 {
353 uivector_cleanup(&((Coin*)c)->symbols);
354 }
355
Coin_copy(Coin * c1,const Coin * c2)356 static void Coin_copy(Coin* c1, const Coin* c2)
357 {
358 c1->weight = c2->weight;
359 uivector_copy(&c1->symbols, &c2->symbols);
360 }
361
addCoins(Coin * c1,const Coin * c2)362 static void addCoins(Coin* c1, const Coin* c2)
363 {
364 unsigned i;
365 for(i = 0; i < c2->symbols.size; i++) uivector_push_back(&c1->symbols, c2->symbols.data[i]);
366 c1->weight += c2->weight;
367 }
368
Coin_sort(Coin * data,size_t amount)369 static void Coin_sort(Coin* data, size_t amount) /*combsort*/
370 {
371 size_t gap = amount;
372 unsigned char swapped = 0;
373 while(gap > 1 || swapped)
374 {
375 size_t i;
376 gap = (gap * 10) / 13; /*shrink factor 1.3*/
377 if(gap == 9 || gap == 10) gap = 11; /*combsort11*/
378 if(gap < 1) gap = 1;
379 swapped = 0;
380 for(i = 0; i < amount - gap; i++)
381 {
382 size_t j = i + gap;
383 if(data[j].weight < data[i].weight)
384 {
385 float temp = data[j].weight; data[j].weight = data[i].weight; data[i].weight = temp;
386 uivector_swap(&data[i].symbols, &data[j].symbols);
387 swapped = 1;
388 }
389 }
390 }
391 }
392
393 typedef struct HuffmanTree
394 {
395 uivector tree2d;
396 uivector tree1d;
397 uivector lengths; /*the lengths of the codes of the 1d-tree*/
398 unsigned maxbitlen; /*maximum number of bits a single code can get*/
399 unsigned numcodes; /*number of symbols in the alphabet = number of codes*/
400 } HuffmanTree;
401
402 /*function used for debug purposes*/
403 /*#include <iostream>
404 static void HuffmanTree_draw(HuffmanTree* tree)
405 {
406 std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl;
407 for(size_t i = 0; i < tree->tree1d.size; i++)
408 {
409 if(tree->lengths.data[i])
410 std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl;
411 }
412 std::cout << std::endl;
413 }*/
414
HuffmanTree_init(HuffmanTree * tree)415 static void HuffmanTree_init(HuffmanTree* tree)
416 {
417 uivector_init(&tree->tree2d);
418 uivector_init(&tree->tree1d);
419 uivector_init(&tree->lengths);
420 }
421
HuffmanTree_cleanup(HuffmanTree * tree)422 static void HuffmanTree_cleanup(HuffmanTree* tree)
423 {
424 uivector_cleanup(&tree->tree2d);
425 uivector_cleanup(&tree->tree1d);
426 uivector_cleanup(&tree->lengths);
427 }
428
429 /*the tree representation used by the decoder. return value is error*/
HuffmanTree_make2DTree(HuffmanTree * tree)430 static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
431 {
432 unsigned nodefilled = 0; /*up to which node it is filled*/
433 unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/
434 unsigned n, i;
435
436 if(!uivector_resize(&tree->tree2d, tree->numcodes * 2)) return 9901; /*if failed return not enough memory error*/
437 /*convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means uninited, a value >= numcodes is an address to another bit, a value < numcodes is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as many columns as codes - 1
438 a good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. Here, the internal nodes are stored (what their 0 and 1 option point to). There is only memory for such good tree currently, if there are more nodes (due to too long length codes), error 55 will happen*/
439 for(n = 0; n < tree->numcodes * 2; n++) tree->tree2d.data[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/
440
441 for(n = 0; n < tree->numcodes; n++) /*the codes*/
442 for(i = 0; i < tree->lengths.data[n]; i++) /*the bits for this code*/
443 {
444 unsigned char bit = (unsigned char)((tree->tree1d.data[n] >> (tree->lengths.data[n] - i - 1)) & 1);
445 if(treepos > tree->numcodes - 2) return 55; /*error 55: oversubscribed; see description in header*/
446 if(tree->tree2d.data[2 * treepos + bit] == 32767) /*not yet filled in*/
447 {
448 if(i + 1 == tree->lengths.data[n]) /*last bit*/
449 {
450 tree->tree2d.data[2 * treepos + bit] = n; /*put the current code in it*/
451 treepos = 0;
452 }
453 else /*put address of the next step in here, first that address has to be found of course (it's just nodefilled + 1)...*/
454 {
455 nodefilled++;
456 tree->tree2d.data[2 * treepos + bit] = nodefilled + tree->numcodes; /*addresses encoded with numcodes added to it*/
457 treepos = nodefilled;
458 }
459 }
460 else treepos = tree->tree2d.data[2 * treepos + bit] - tree->numcodes;
461 }
462 for(n = 0; n < tree->numcodes * 2; n++) if(tree->tree2d.data[n] == 32767) tree->tree2d.data[n] = 0; /*remove possible remaining 32767's*/
463
464 return 0;
465 }
466
HuffmanTree_makeFromLengths2(HuffmanTree * tree)467 static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) /*given that numcodes, lengths and maxbitlen are already filled in correctly. return value is error.*/
468 {
469 uivector blcount;
470 uivector nextcode;
471 unsigned bits, n, error = 0;
472
473 uivector_init(&blcount);
474 uivector_init(&nextcode);
475 if(!uivector_resize(&tree->tree1d, tree->numcodes)
476 || !uivector_resizev(&blcount, tree->maxbitlen + 1, 0)
477 || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0))
478 error = 9902;
479
480 if(!error)
481 {
482 /*step 1: count number of instances of each code length*/
483 for(bits = 0; bits < tree->numcodes; bits++) blcount.data[tree->lengths.data[bits]]++;
484 /*step 2: generate the nextcode values*/
485 for(bits = 1; bits <= tree->maxbitlen; bits++) nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1;
486 /*step 3: generate all the codes*/
487 for(n = 0; n < tree->numcodes; n++) if(tree->lengths.data[n] != 0) tree->tree1d.data[n] = nextcode.data[tree->lengths.data[n]]++;
488 }
489
490 uivector_cleanup(&blcount);
491 uivector_cleanup(&nextcode);
492
493 if(!error) return HuffmanTree_make2DTree(tree);
494 else return error;
495 }
496
497 /*given the code lengths (as stored in the PNG file), generate the tree as defined by Deflate. maxbitlen is the maximum bits that a code in the tree can have. return value is error.*/
HuffmanTree_makeFromLengths(HuffmanTree * tree,const unsigned * bitlen,size_t numcodes,unsigned maxbitlen)498 static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, size_t numcodes, unsigned maxbitlen)
499 {
500 unsigned i;
501 if(!uivector_resize(&tree->lengths, numcodes)) return 9903;
502 for(i = 0; i < numcodes; i++) tree->lengths.data[i] = bitlen[i];
503 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
504 tree->maxbitlen = maxbitlen;
505 return HuffmanTree_makeFromLengths2(tree);
506 }
507
HuffmanTree_fillInCoins(vector * coins,const unsigned * frequencies,unsigned numcodes,size_t sum)508 static unsigned HuffmanTree_fillInCoins(vector* coins, const unsigned* frequencies, unsigned numcodes, size_t sum)
509 {
510 unsigned i;
511 for(i = 0; i < numcodes; i++)
512 {
513 Coin* coin;
514 if(frequencies[i] == 0) continue; /*it's important to exclude symbols that aren't present*/
515 if(!vector_resize(coins, coins->size + 1)) { vector_cleanup(coins); return 9904; }
516 coin = (Coin*)(vector_get(coins, coins->size - 1));
517 Coin_init(coin);
518 coin->weight = frequencies[i] / (float)sum;
519 uivector_push_back(&coin->symbols, i);
520 }
521 if(coins->size) Coin_sort((Coin*)coins->data, coins->size);
522 return 0;
523 }
524
HuffmanTree_makeFromFrequencies(HuffmanTree * tree,const unsigned * frequencies,size_t numcodes,unsigned maxbitlen)525 static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies, size_t numcodes, unsigned maxbitlen)
526 {
527 unsigned i, j;
528 size_t sum = 0, numpresent = 0;
529 unsigned error = 0;
530
531 vector prev_row; /*type Coin, the previous row of coins*/
532 vector coins; /*type Coin, the coins of the currently calculated row*/
533
534 tree->maxbitlen = maxbitlen;
535
536 for(i = 0; i < numcodes; i++)
537 {
538 if(frequencies[i] > 0)
539 {
540 numpresent++;
541 sum += frequencies[i];
542 }
543 }
544
545 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
546 tree->numcodes = (unsigned)numcodes; /*number of symbols*/
547 if (!uivector_resize(&tree->lengths, 0)) return 9955;
548 if(!uivector_resizev(&tree->lengths, tree->numcodes, 0)) return 9905;
549
550 if(numpresent == 0) /*there are no symbols at all, in that case add one symbol of value 0 to the tree (see RFC 1951 section 3.2.7) */
551 {
552 tree->lengths.data[0] = 1;
553 return HuffmanTree_makeFromLengths2(tree);
554 }
555 else if(numpresent == 1) /*the package merge algorithm gives wrong results if there's only one symbol (theoretically 0 bits would then suffice, but we need a proper symbol for zlib)*/
556 {
557 for(i = 0; i < numcodes; i++) if(frequencies[i]) tree->lengths.data[i] = 1;
558 return HuffmanTree_makeFromLengths2(tree);
559 }
560
561 vector_init(&coins, sizeof(Coin));
562 vector_init(&prev_row, sizeof(Coin));
563
564 /*Package-Merge algorithm represented by coin collector's problem
565 For every symbol, maxbitlen coins will be created*/
566
567 /*first row, lowest denominator*/
568 error = HuffmanTree_fillInCoins(&coins, frequencies, tree->numcodes, sum);
569 if(!error)
570 {
571 for(j = 1; j <= maxbitlen && !error; j++) /*each of the remaining rows*/
572 {
573 vector_swap(&coins, &prev_row); /*swap instead of copying*/
574 if(!vector_resized(&coins, 0, Coin_cleanup)) { error = 9906; break; }
575
576 for(i = 0; i + 1 < prev_row.size; i += 2)
577 {
578 if(!vector_resize(&coins, coins.size + 1)) { error = 9907; break; }
579 Coin_init((Coin*)vector_get(&coins, coins.size - 1));
580 Coin_copy((Coin*)vector_get(&coins, coins.size - 1), (Coin*)vector_get(&prev_row, i));
581 addCoins((Coin*)vector_get(&coins, coins.size - 1), (Coin*)vector_get(&prev_row, i + 1)); /*merge the coins into packages*/
582 }
583 if(j < maxbitlen)
584 {
585 error = HuffmanTree_fillInCoins(&coins, frequencies, tree->numcodes, sum);
586 }
587 }
588 }
589
590 if(!error)
591 {
592 /*keep the coins with lowest weight, so that they add up to the amount of symbols - 1*/
593 vector_resized(&coins, numpresent - 1, Coin_cleanup);
594
595 /*calculate the lengths of each symbol, as the amount of times a coin of each symbol is used*/
596 for(i = 0; i < coins.size; i++)
597 {
598 Coin* coin = (Coin*)vector_get(&coins, i);
599 for(j = 0; j < coin->symbols.size; j++) tree->lengths.data[coin->symbols.data[j]]++;
600 }
601
602 error = HuffmanTree_makeFromLengths2(tree);
603 }
604
605 vector_cleanupd(&coins, Coin_cleanup);
606 vector_cleanupd(&prev_row, Coin_cleanup);
607
608 return error;
609 }
610
HuffmanTree_getCode(const HuffmanTree * tree,unsigned index)611 static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index) { return tree->tree1d.data[index]; }
HuffmanTree_getLength(const HuffmanTree * tree,unsigned index)612 static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index) { return tree->lengths.data[index]; }
613
614 /*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/
generateFixedTree(HuffmanTree * tree)615 static unsigned generateFixedTree(HuffmanTree* tree)
616 {
617 unsigned i, error = 0;
618 uivector bitlen;
619 uivector_init(&bitlen);
620 if(!uivector_resize(&bitlen, NUM_DEFLATE_CODE_SYMBOLS)) error = 9909;
621
622 if(!error)
623 {
624 /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
625 for(i = 0; i <= 143; i++) bitlen.data[i] = 8;
626 for(i = 144; i <= 255; i++) bitlen.data[i] = 9;
627 for(i = 256; i <= 279; i++) bitlen.data[i] = 7;
628 for(i = 280; i <= 287; i++) bitlen.data[i] = 8;
629
630 error = HuffmanTree_makeFromLengths(tree, bitlen.data, NUM_DEFLATE_CODE_SYMBOLS, 15);
631 }
632
633 uivector_cleanup(&bitlen);
634 return error;
635 }
636
generateDistanceTree(HuffmanTree * tree)637 static unsigned generateDistanceTree(HuffmanTree* tree)
638 {
639 unsigned i, error = 0;
640 uivector bitlen;
641 uivector_init(&bitlen);
642 if(!uivector_resize(&bitlen, NUM_DISTANCE_SYMBOLS)) error = 9910;
643
644 /*there are 32 distance codes, but 30-31 are unused*/
645 if(!error)
646 {
647 for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen.data[i] = 5;
648 error = HuffmanTree_makeFromLengths(tree, bitlen.data, NUM_DISTANCE_SYMBOLS, 15);
649 }
650 uivector_cleanup(&bitlen);
651 return error;
652 }
653
654 /* ////////////////////////////////////////////////////////////////////////// */
655 /* / Deflator / */
656 /* ////////////////////////////////////////////////////////////////////////// */
657
658 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
659
660 /*bitlen is the size in bits of the code*/
addHuffmanSymbol(size_t * bp,ucvector * compressed,unsigned code,unsigned bitlen)661 static void addHuffmanSymbol(size_t* bp, ucvector* compressed, unsigned code, unsigned bitlen)
662 {
663 addBitsToStreamReversed(bp, compressed, code, bitlen);
664 }
665
666 /*search the index in the array, that has the largest value smaller than or equal to the given value, given array must be sorted (if no value is smaller, it returns the size of the given array)*/
searchCodeIndex(const unsigned * array,size_t array_size,size_t value)667 static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value)
668 {
669 /*linear search implementation*/
670 /*for(size_t i = 1; i < array_size; i++) if(array[i] > value) return i - 1;
671 return array_size - 1;*/
672
673 /*binary search implementation (not that much faster) (precondition: array_size > 0)*/
674 size_t left = 1;
675 size_t right = array_size - 1;
676 while(left <= right)
677 {
678 size_t mid = (left + right) / 2;
679 if(array[mid] <= value) left = mid + 1; /*the value to find is more to the right*/
680 else if(array[mid - 1] > value) right = mid - 1; /*the value to find is more to the left*/
681 else return mid - 1;
682 }
683 return array_size - 1;
684 }
685
addLengthDistance(uivector * values,size_t length,size_t distance)686 static void addLengthDistance(uivector* values, size_t length, size_t distance)
687 {
688 /*values in encoded vector are those used by deflate:
689 0-255: literal bytes
690 256: end
691 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits)
692 286-287: invalid*/
693
694 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length);
695 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]);
696 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
697 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
698
699 uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX);
700 uivector_push_back(values, extra_length);
701 uivector_push_back(values, dist_code);
702 uivector_push_back(values, extra_distance);
703 }
704
705 #if USE_BRUTE_FORCE_ENCODING
706 #define encodeLZ77 encodeLZ77_brute
707 /*the "brute force" version of the encodeLZ7 algorithm, not used anymore, kept here for reference*/
encodeLZ77_brute(uivector * out,const unsigned char * in,size_t size,unsigned windowSize)708 static unsigned encodeLZ77_brute(uivector* out, const unsigned char* in, size_t size, unsigned windowSize)
709 {
710 size_t pos;
711 /*using pointer instead of vector for input makes it faster when NOT using optimization when compiling; no influence if optimization is used*/
712 for(pos = 0; pos < size; pos++)
713 {
714 /*Phase 1: doxygen images often have long runs of the same color, try to find them*/
715 const int minLength = 4; // Minimum length for a run to make sense
716
717 if(pos < size - minLength * 4)
718 {
719 size_t p, fp;
720 size_t current_length;
721
722 /*RGBA pixel run?*/
723 p = pos;
724 fp = pos + 4;
725 current_length = 0;
726
727 while(fp < size && in[p] == in[fp] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH)
728 {
729 ++p;
730 ++fp;
731 ++current_length;
732 }
733
734 if (current_length > (minLength - 1 ) * 4) /*worth using?*/
735 {
736 uivector_push_back(out, in[pos ]);
737 uivector_push_back(out, in[pos + 1]);
738 uivector_push_back(out, in[pos + 2]);
739 uivector_push_back(out, in[pos + 3]);
740 addLengthDistance(out, current_length, 4);
741
742 pos += current_length + 4 - 1; /*-1 for loop's pos++*/
743 continue;
744 }
745
746 /*RGB pixel run?*/
747 p = pos;
748 fp = pos + 3;
749 current_length = 0;
750
751 while(fp < size && in[p] == in[fp] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH)
752 {
753 ++p;
754 ++fp;
755 ++current_length;
756 }
757
758 if (current_length > (minLength - 1 ) * 3) /*worth using?*/
759 {
760 uivector_push_back(out, in[pos ]);
761 uivector_push_back(out, in[pos + 1]);
762 uivector_push_back(out, in[pos + 2]);
763 addLengthDistance(out, current_length, 3);
764
765 pos += current_length + 3 - 1; /*-1 for loop's pos++*/
766 continue;
767 }
768 }
769
770 size_t length = 0, offset = 0; /*the length and offset found for the current position*/
771 size_t max_offset = pos < windowSize ? pos : windowSize; /*how far back to test*/
772 size_t current_offset;
773
774 /**search for the longest string**/
775 for(current_offset = 1; current_offset < max_offset; current_offset++) /*search backwards through all possible distances (=offsets)*/
776 {
777 size_t backpos = pos - current_offset;
778 if(in[backpos] == in[pos])
779 {
780 /*test the next characters*/
781 size_t current_length = 1;
782 size_t backtest = backpos + 1;
783 size_t foretest = pos + 1;
784 while(foretest < size && in[backtest] == in[foretest] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH) /*maximum support length by deflate is max length*/
785 {
786 if(backpos >= pos) backpos -= current_offset; /*continue as if we work on the decoded bytes after pos by jumping back before pos*/
787 current_length++;
788 backtest++;
789 foretest++;
790 }
791 if(current_length > length)
792 {
793 length = current_length; /*the longest length*/
794 offset = current_offset; /*the offset that is related to this longest length*/
795 if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH) break; /*you can jump out of this for loop once a length of max length is found (gives significant speed gain)*/
796 }
797 }
798 }
799
800 /**encode it as length/distance pair or literal value**/
801 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/
802 {
803 uivector_push_back(out, in[pos]);
804 }
805 else
806 {
807 addLengthDistance(out, length, offset);
808 pos += (length - 1);
809 }
810 } /*end of the loop through each character of input*/
811
812 return 0;
813 }
814 #endif
815
816 /*
817 static const unsigned HASH_NUM_VALUES = 65536;
818 static const unsigned HASH_NUM_CHARACTERS = 6;
819 static const unsigned HASH_SHIFT = 2;
820 Good and fast values: HASH_NUM_VALUES=65536, HASH_NUM_CHARACTERS=6, HASH_SHIFT=2
821 making HASH_NUM_CHARACTERS larger (like 8), makes the file size larger but is a bit faster
822 making HASH_NUM_CHARACTERS smaller (like 3), makes the file size smaller but is slower
823 */
824
825 #if !defined(USE_BRUTE_FORCE_ENCODING)
getHash(const unsigned char * data,size_t size,size_t pos)826 static unsigned getHash(const unsigned char* data, size_t size, size_t pos)
827 {
828 unsigned result = 0;
829 size_t amount, i;
830 if(pos >= size) return 0;
831 amount = HASH_NUM_CHARACTERS; if(pos + amount >= size) amount = size - pos;
832 for(i = 0; i < amount; i++) result ^= (data[pos + i] << (i * HASH_SHIFT));
833 return result % HASH_NUM_VALUES;
834 }
835
836 /*LZ77-encode the data using a hash table technique to let it encode faster. Return value is error code*/
encodeLZ77(uivector * out,const unsigned char * in,size_t size,unsigned windowSize)837 static unsigned encodeLZ77(uivector* out, const unsigned char* in, size_t size, unsigned windowSize)
838 {
839 /**generate hash table**/
840 vector table; /*HASH_NUM_VALUES uivectors; this represents what would be an std::vector<std::vector<unsigned> > in C++*/
841 uivector tablepos1, tablepos2;
842 unsigned pos, i, error = 0;
843
844 vector_init(&table, sizeof(uivector));
845 if(!vector_resize(&table, HASH_NUM_VALUES)) return 9917;
846 for(i = 0; i < HASH_NUM_VALUES; i++)
847 {
848 uivector* v = (uivector*)vector_get(&table, i);
849 uivector_init(v);
850 }
851
852 /*remember start and end positions in the tables to searching in*/
853 uivector_init(&tablepos1);
854 uivector_init(&tablepos2);
855 if(!uivector_resizev(&tablepos1, HASH_NUM_VALUES, 0)) error = 9918;
856 if(!uivector_resizev(&tablepos2, HASH_NUM_VALUES, 0)) error = 9919;
857
858 if(!error)
859 {
860 for(pos = 0; pos < size; pos++)
861 {
862 unsigned length = 0, offset = 0; /*the length and offset found for the current position*/
863 unsigned max_offset = pos < windowSize ? pos : windowSize; /*how far back to test*/
864 unsigned tablepos;
865
866 /*/search for the longest string*/
867 /*first find out where in the table to start (the first value that is in the range from "pos - max_offset" to "pos")*/
868 unsigned hash = getHash(in, size, pos);
869 if(!uivector_push_back((uivector*)vector_get(&table, hash), pos)) { error = 9920; break; }
870
871 while(((uivector*)vector_get(&table, hash))->data[tablepos1.data[hash]] < pos - max_offset) tablepos1.data[hash]++; /*it now points to the first value in the table for which the index is larger than or equal to pos - max_offset*/
872 while(((uivector*)vector_get(&table, hash))->data[tablepos2.data[hash]] < pos) tablepos2.data[hash]++; /*it now points to the first value in the table for which the index is larger than or equal to pos*/
873
874 for(tablepos = tablepos2.data[hash] - 1; tablepos >= tablepos1.data[hash] && tablepos < tablepos2.data[hash]; tablepos--)
875 {
876 unsigned backpos = ((uivector*)vector_get(&table, hash))->data[tablepos];
877 unsigned current_offset = pos - backpos;
878
879 /*test the next characters*/
880 unsigned current_length = 0;
881 unsigned backtest = backpos;
882 unsigned foretest = pos;
883 while(foretest < size && in[backtest] == in[foretest] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH) /*maximum support length by deflate is max length*/
884 {
885 if(backpos >= pos) backpos -= current_offset; /*continue as if we work on the decoded bytes after pos by jumping back before pos*/
886 current_length++;
887 backtest++;
888 foretest++;
889 }
890 if(current_length > length)
891 {
892 length = current_length; /*the longest length*/
893 offset = current_offset; /*the offset that is related to this longest length*/
894 if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH) break; /*you can jump out of this for loop once a length of max length is found (gives significant speed gain)*/
895 }
896 }
897
898 /**encode it as length/distance pair or literal value**/
899 if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/
900 {
901 if(!uivector_push_back(out, in[pos])) { error = 9921; break; }
902 }
903 else
904 {
905 unsigned j;
906 addLengthDistance(out, length, offset);
907 for(j = 0; j < length - 1; j++)
908 {
909 pos++;
910 if(!uivector_push_back((uivector*)vector_get(&table, getHash(in, size, pos)), pos)) { error = 9922; break; }
911 }
912 }
913 } /*end of the loop through each character of input*/
914 } /*end of "if(!error)"*/
915
916 /*cleanup*/
917 for(i = 0; i < table.size; i++)
918 {
919 uivector* v = (uivector*)vector_get(&table, i);
920 uivector_cleanup(v);
921 }
922 vector_cleanup(&table);
923 uivector_cleanup(&tablepos1);
924 uivector_cleanup(&tablepos2);
925 return error;
926 }
927 #endif
928
929 /* /////////////////////////////////////////////////////////////////////////// */
930
deflateNoCompression(ucvector * out,const unsigned char * data,size_t datasize)931 static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize)
932 {
933 /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte, 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/
934
935 size_t i, j, numdeflateblocks = datasize / 65536 + 1;
936 unsigned datapos = 0;
937 for(i = 0; i < numdeflateblocks; i++)
938 {
939 unsigned BFINAL, BTYPE, LEN, NLEN;
940 unsigned char firstbyte;
941
942 BFINAL = (i == numdeflateblocks - 1);
943 BTYPE = 0;
944
945 firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1));
946 ucvector_push_back(out, firstbyte);
947
948 LEN = 65535;
949 if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos;
950 NLEN = 65535 - LEN;
951
952 ucvector_push_back(out, (unsigned char)(LEN % 256));
953 ucvector_push_back(out, (unsigned char)(LEN / 256));
954 ucvector_push_back(out, (unsigned char)(NLEN % 256));
955 ucvector_push_back(out, (unsigned char)(NLEN / 256));
956
957 /*Decompressed data*/
958 for(j = 0; j < 65535 && datapos < datasize; j++)
959 {
960 ucvector_push_back(out, data[datapos++]);
961 }
962 }
963
964 return 0;
965 }
966
967 /*write the encoded data, using lit/len as well as distance codes*/
writeLZ77data(size_t * bp,ucvector * out,const uivector * lz77_encoded,const HuffmanTree * codes,const HuffmanTree * codesD)968 static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encoded, const HuffmanTree* codes, const HuffmanTree* codesD)
969 {
970 size_t i = 0;
971 for(i = 0; i < lz77_encoded->size; i++)
972 {
973 unsigned val = lz77_encoded->data[i];
974 addHuffmanSymbol(bp, out, HuffmanTree_getCode(codes, val), HuffmanTree_getLength(codes, val));
975 if(val > 256) /*for a length code, 3 more things have to be added*/
976 {
977 unsigned length_index = val - FIRST_LENGTH_CODE_INDEX;
978 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
979 unsigned length_extra_bits = lz77_encoded->data[++i];
980
981 unsigned distance_code = lz77_encoded->data[++i];
982
983 unsigned distance_index = distance_code;
984 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
985 unsigned distance_extra_bits = lz77_encoded->data[++i];
986
987 addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits);
988 addHuffmanSymbol(bp, out, HuffmanTree_getCode(codesD, distance_code), HuffmanTree_getLength(codesD, distance_code));
989 addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits);
990 }
991 }
992 }
993
deflateDynamic(ucvector * out,const unsigned char * data,size_t datasize,const LodeZlib_DeflateSettings * settings)994 static unsigned deflateDynamic(ucvector* out, const unsigned char* data, size_t datasize, const LodeZlib_DeflateSettings* settings)
995 {
996 /*
997 after the BFINAL and BTYPE, the dynamic block consists out of the following:
998 - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN
999 - (HCLEN+4)*3 bits code lengths of code length alphabet
1000 - HLIT + 257 code lengths of lit/length alphabet (encoded using the code length alphabet, + possible repetition codes 16, 17, 18)
1001 - HDIST + 1 code lengths of distance alphabet (encoded using the code length alphabet, + possible repetition codes 16, 17, 18)
1002 - compressed data
1003 - 256 (end code)
1004 */
1005
1006 unsigned error = 0;
1007
1008 uivector lz77_encoded;
1009 HuffmanTree codes; /*tree for literal values and length codes*/
1010 HuffmanTree codesD; /*tree for distance codes*/
1011 HuffmanTree codelengthcodes;
1012 uivector frequencies;
1013 uivector frequenciesD;
1014 uivector amounts; /*the amounts in the "normal" order*/
1015 uivector lldl;
1016 uivector lldll; /*lit/len & dist code lengths*/
1017 uivector clcls;
1018
1019 unsigned BFINAL = 1; /*make only one block... the first and final one*/
1020 size_t numcodes, numcodesD, i, bp = 0; /*the bit pointer*/
1021 unsigned HLIT, HDIST, HCLEN;
1022
1023 uivector_init(&lz77_encoded);
1024 HuffmanTree_init(&codes);
1025 HuffmanTree_init(&codesD);
1026 HuffmanTree_init(&codelengthcodes);
1027 uivector_init(&frequencies);
1028 uivector_init(&frequenciesD);
1029 uivector_init(&amounts);
1030 uivector_init(&lldl);
1031 uivector_init(&lldll);
1032 uivector_init(&clcls);
1033
1034 while(!error) /*the goto-avoiding while construct: break out to go to the cleanup phase, a break at the end makes sure the while is never repeated*/
1035 {
1036 if(settings->useLZ77)
1037 {
1038 error = encodeLZ77(&lz77_encoded, data, datasize, settings->windowSize); /*LZ77 encoded*/
1039 if(error) break;
1040 }
1041 else
1042 {
1043 if(!uivector_resize(&lz77_encoded, datasize)) { error = 9923; break; }
1044 for(i = 0; i < datasize; i++) lz77_encoded.data[i] = data[i]; /*no LZ77, but still will be Huffman compressed*/
1045 }
1046
1047 if(!uivector_resizev(&frequencies, 286, 0)) { error = 9924; break; }
1048 if(!uivector_resizev(&frequenciesD, 30, 0)) { error = 9925; break; }
1049 for(i = 0; i < lz77_encoded.size; i++)
1050 {
1051 unsigned symbol = lz77_encoded.data[i];
1052 frequencies.data[symbol]++;
1053 if(symbol > 256)
1054 {
1055 unsigned dist = lz77_encoded.data[i + 2];
1056 frequenciesD.data[dist]++;
1057 i += 3;
1058 }
1059 }
1060 frequencies.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/
1061
1062 error = HuffmanTree_makeFromFrequencies(&codes, frequencies.data, frequencies.size, 15);
1063 if(error) break;
1064 error = HuffmanTree_makeFromFrequencies(&codesD, frequenciesD.data, frequenciesD.size, 15);
1065 if(error) break;
1066
1067 addBitToStream(&bp, out, BFINAL);
1068 addBitToStream(&bp, out, 0); /*first bit of BTYPE "dynamic"*/
1069 addBitToStream(&bp, out, 1); /*second bit of BTYPE "dynamic"*/
1070
1071 numcodes = codes.numcodes; if(numcodes > 286) numcodes = 286;
1072 numcodesD = codesD.numcodes; if(numcodesD > 30) numcodesD = 30;
1073 for(i = 0; i < numcodes; i++) uivector_push_back(&lldll, HuffmanTree_getLength(&codes, (unsigned)i));
1074 for(i = 0; i < numcodesD; i++) uivector_push_back(&lldll, HuffmanTree_getLength(&codesD, (unsigned)i));
1075
1076 /*make lldl smaller by using repeat codes 16 (copy length 3-6 times), 17 (3-10 zeros), 18 (11-138 zeros)*/
1077 for(i = 0; i < (unsigned)lldll.size; i++)
1078 {
1079 unsigned j = 0;
1080 while(i + j + 1 < (unsigned)lldll.size && lldll.data[i + j + 1] == lldll.data[i]) j++;
1081
1082 if(lldll.data[i] == 0 && j >= 2)
1083 {
1084 j++; /*include the first zero*/
1085 if(j <= 10) { uivector_push_back(&lldl, 17); uivector_push_back(&lldl, j - 3); }
1086 else
1087 {
1088 if(j > 138) j = 138;
1089 uivector_push_back(&lldl, 18); uivector_push_back(&lldl, j - 11);
1090 }
1091 i += (j - 1);
1092 }
1093 else if(j >= 3)
1094 {
1095 size_t k;
1096 unsigned num = j / 6, rest = j % 6;
1097 uivector_push_back(&lldl, lldll.data[i]);
1098 for(k = 0; k < num; k++) { uivector_push_back(&lldl, 16); uivector_push_back(&lldl, 6 - 3); }
1099 if(rest >= 3) { uivector_push_back(&lldl, 16); uivector_push_back(&lldl, rest - 3); }
1100 else j -= rest;
1101 i += j;
1102 }
1103 else uivector_push_back(&lldl, lldll.data[i]);
1104 }
1105
1106 /*generate huffmantree for the length codes of lit/len and dist codes*/
1107 if(!uivector_resizev(&amounts, 19, 0)) { error = 9926; break; } /*16 possible lengths (0-15) and 3 repeat codes (16, 17 and 18)*/
1108 for(i = 0; i < lldl.size; i++)
1109 {
1110 amounts.data[lldl.data[i]]++;
1111 if(lldl.data[i] >= 16) i++; /*after a repeat code come the bits that specify the amount, those don't need to be in the amounts calculation*/
1112 }
1113
1114 error = HuffmanTree_makeFromFrequencies(&codelengthcodes, amounts.data, amounts.size, 7);
1115 if(error) break;
1116
1117 if(!uivector_resize(&clcls, 19)) { error = 9927; break; }
1118 for(i = 0; i < 19; i++) clcls.data[i] = HuffmanTree_getLength(&codelengthcodes, CLCL[i]); /*lengths of code length tree is in the order as specified by deflate*/
1119 while(clcls.data[clcls.size - 1] == 0 && clcls.size > 4)
1120 {
1121 if(!uivector_resize(&clcls, clcls.size - 1)) { error = 9928; break; } /*remove zeros at the end, but minimum size must be 4*/
1122 }
1123 if(error) break;
1124
1125 /*write the HLIT, HDIST and HCLEN values*/
1126 HLIT = (unsigned)(numcodes - 257);
1127 HDIST = (unsigned)(numcodesD - 1);
1128 HCLEN = (unsigned)clcls.size - 4;
1129 addBitsToStream(&bp, out, HLIT, 5);
1130 addBitsToStream(&bp, out, HDIST, 5);
1131 addBitsToStream(&bp, out, HCLEN, 4);
1132
1133 /*write the code lengths of the code length alphabet*/
1134 for(i = 0; i < HCLEN + 4; i++) addBitsToStream(&bp, out, clcls.data[i], 3);
1135
1136 /*write the lengths of the lit/len AND the dist alphabet*/
1137 for(i = 0; i < lldl.size; i++)
1138 {
1139 addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&codelengthcodes, lldl.data[i]), HuffmanTree_getLength(&codelengthcodes, lldl.data[i]));
1140 /*extra bits of repeat codes*/
1141 if(lldl.data[i] == 16) addBitsToStream(&bp, out, lldl.data[++i], 2);
1142 else if(lldl.data[i] == 17) addBitsToStream(&bp, out, lldl.data[++i], 3);
1143 else if(lldl.data[i] == 18) addBitsToStream(&bp, out, lldl.data[++i], 7);
1144 }
1145
1146 /*write the compressed data symbols*/
1147 writeLZ77data(&bp, out, &lz77_encoded, &codes, &codesD);
1148 if(HuffmanTree_getLength(&codes, 256) == 0) { error = 64; break; } /*the length of the end code 256 must be larger than 0*/
1149 addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&codes, 256), HuffmanTree_getLength(&codes, 256)); /*end code*/
1150
1151 break; /*end of error-while*/
1152 }
1153
1154 /*cleanup*/
1155 uivector_cleanup(&lz77_encoded);
1156 HuffmanTree_cleanup(&codes);
1157 HuffmanTree_cleanup(&codesD);
1158 HuffmanTree_cleanup(&codelengthcodes);
1159 uivector_cleanup(&frequencies);
1160 uivector_cleanup(&frequenciesD);
1161 uivector_cleanup(&amounts);
1162 uivector_cleanup(&lldl);
1163 uivector_cleanup(&lldll);
1164 uivector_cleanup(&clcls);
1165
1166 return error;
1167 }
1168
deflateFixed(ucvector * out,const unsigned char * data,size_t datasize,const LodeZlib_DeflateSettings * settings)1169 static unsigned deflateFixed(ucvector* out, const unsigned char* data, size_t datasize, const LodeZlib_DeflateSettings* settings)
1170 {
1171 HuffmanTree codes; /*tree for literal values and length codes*/
1172 HuffmanTree codesD; /*tree for distance codes*/
1173
1174 unsigned BFINAL = 1; /*make only one block... the first and final one*/
1175 unsigned error = 0;
1176 size_t i, bp = 0; /*the bit pointer*/
1177
1178 HuffmanTree_init(&codes);
1179 HuffmanTree_init(&codesD);
1180
1181 generateFixedTree(&codes);
1182 generateDistanceTree(&codesD);
1183
1184 addBitToStream(&bp, out, BFINAL);
1185 addBitToStream(&bp, out, 1); /*first bit of BTYPE*/
1186 addBitToStream(&bp, out, 0); /*second bit of BTYPE*/
1187
1188 if(settings->useLZ77) /*LZ77 encoded*/
1189 {
1190 uivector lz77_encoded;
1191 uivector_init(&lz77_encoded);
1192 error = encodeLZ77(&lz77_encoded, data, datasize, settings->windowSize);
1193 if(!error) writeLZ77data(&bp, out, &lz77_encoded, &codes, &codesD);
1194 uivector_cleanup(&lz77_encoded);
1195 }
1196 else /*no LZ77, but still will be Huffman compressed*/
1197 {
1198 for(i = 0; i < datasize; i++) addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&codes, data[i]), HuffmanTree_getLength(&codes, data[i]));
1199 }
1200 if(!error) addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&codes, 256), HuffmanTree_getLength(&codes, 256)); /*"end" code*/
1201
1202 /*cleanup*/
1203 HuffmanTree_cleanup(&codes);
1204 HuffmanTree_cleanup(&codesD);
1205
1206 return error;
1207 }
1208
LodeFlate_deflate(ucvector * out,const unsigned char * data,size_t datasize,const LodeZlib_DeflateSettings * settings)1209 unsigned LodeFlate_deflate(ucvector* out, const unsigned char* data, size_t datasize, const LodeZlib_DeflateSettings* settings)
1210 {
1211 unsigned error = 0;
1212 if(settings->btype == 0) error = deflateNoCompression(out, data, datasize);
1213 else if(settings->btype == 1) error = deflateFixed(out, data, datasize, settings);
1214 else if(settings->btype == 2) error = deflateDynamic(out, data, datasize, settings);
1215 else error = 61;
1216 return error;
1217 }
1218
1219
1220 /* ////////////////////////////////////////////////////////////////////////// */
1221 /* / Adler32 */
1222 /* ////////////////////////////////////////////////////////////////////////// */
1223
update_adler32(unsigned adler,const unsigned char * data,unsigned len)1224 static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len)
1225 {
1226 unsigned s1 = adler & 0xffff;
1227 unsigned s2 = (adler >> 16) & 0xffff;
1228
1229 while(len > 0)
1230 {
1231 /*at least 5550 sums can be done before the sums overflow, saving us from a lot of module divisions*/
1232 unsigned amount = len > 5550 ? 5550 : len;
1233 len -= amount;
1234 while(amount > 0)
1235 {
1236 s1 = (s1 + *data++);
1237 s2 = (s2 + s1);
1238 amount--;
1239 }
1240 s1 %= 65521;
1241 s2 %= 65521;
1242 }
1243
1244 return (s2 << 16) | s1;
1245 }
1246
1247 /*Return the adler32 of the bytes data[0..len-1]*/
adler32(const unsigned char * data,unsigned len)1248 static unsigned adler32(const unsigned char* data, unsigned len)
1249 {
1250 return update_adler32(1L, data, len);
1251 }
1252
1253 /* ////////////////////////////////////////////////////////////////////////// */
1254 /* / Reading and writing single bits and bytes from/to stream for Zlib / */
1255 /* ////////////////////////////////////////////////////////////////////////// */
1256
LodeZlib_add32bitInt(ucvector * buffer,unsigned value)1257 static void LodeZlib_add32bitInt(ucvector* buffer, unsigned value)
1258 {
1259 ucvector_push_back(buffer, (unsigned char)((value >> 24) & 0xff));
1260 ucvector_push_back(buffer, (unsigned char)((value >> 16) & 0xff));
1261 ucvector_push_back(buffer, (unsigned char)((value >> 8) & 0xff));
1262 ucvector_push_back(buffer, (unsigned char)((value ) & 0xff));
1263 }
1264
1265 /* ////////////////////////////////////////////////////////////////////////// */
1266 /* / Zlib / */
1267 /* ////////////////////////////////////////////////////////////////////////// */
1268
LodeZlib_compress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodeZlib_DeflateSettings * settings)1269 static unsigned LodeZlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DeflateSettings* settings)
1270 {
1271 /*initially, *out must be NULL and outsize 0, if you just give some random *out that's pointing to a non allocated buffer, this'll crash*/
1272 ucvector deflatedata, outv;
1273 size_t i;
1274 unsigned error;
1275
1276 unsigned ADLER32;
1277 /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
1278 unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
1279 unsigned FLEVEL = 0;
1280 unsigned FDICT = 0;
1281 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
1282 unsigned FCHECK = 31 - CMFFLG % 31;
1283 CMFFLG += FCHECK;
1284
1285 ucvector_init_buffer(&outv, *out, *outsize); /*ucvector-controlled version of the output buffer, for dynamic array*/
1286
1287 ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256));
1288 ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256));
1289
1290 ucvector_init(&deflatedata);
1291 error = LodeFlate_deflate(&deflatedata, in, insize, settings);
1292
1293 if(!error)
1294 {
1295 ADLER32 = adler32(in, (unsigned)insize);
1296 for(i = 0; i < deflatedata.size; i++) ucvector_push_back(&outv, deflatedata.data[i]);
1297 ucvector_cleanup(&deflatedata);
1298 LodeZlib_add32bitInt(&outv, ADLER32);
1299 }
1300
1301 *out = outv.data;
1302 *outsize = outv.size;
1303
1304 return error;
1305 }
1306
1307 /* ////////////////////////////////////////////////////////////////////////// */
1308
LodeZlib_DeflateSettings_init(LodeZlib_DeflateSettings * settings)1309 void LodeZlib_DeflateSettings_init(LodeZlib_DeflateSettings* settings)
1310 {
1311 settings->btype = 2; /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/
1312 settings->useLZ77 = 1;
1313 settings->windowSize = 2048; /*this is a good tradeoff between speed and compression ratio*/
1314 }
1315
1316 /* ////////////////////////////////////////////////////////////////////////// */
1317 /* ////////////////////////////////////////////////////////////////////////// */
1318 /* ////////////////////////////////////////////////////////////////////////// */
1319 /* ////////////////////////////////////////////////////////////////////////// */
1320 /* ////////////////////////////////////////////////////////////////////////// */
1321 /* // End of Zlib related code, now comes the PNG related code that uses it// */
1322 /* ////////////////////////////////////////////////////////////////////////// */
1323 /* ////////////////////////////////////////////////////////////////////////// */
1324 /* ////////////////////////////////////////////////////////////////////////// */
1325 /* ////////////////////////////////////////////////////////////////////////// */
1326 /* ////////////////////////////////////////////////////////////////////////// */
1327
1328 /*
1329 The two functions below (LodePNG_decompress and LodePNG_compress) directly call the
1330 LodeZlib_decompress and LodeZlib_compress functions. The only purpose of the functions
1331 below, is to provide the ability to let LodePNG use a different Zlib encoder by only
1332 changing the two functions below, instead of changing it inside the various places
1333 in the other LodePNG functions.
1334
1335 *out must be NULL and *outsize must be 0 initially, and after the function is done,
1336 *out must point to the decompressed data, *outsize must be the size of it, and must
1337 be the size of the useful data in bytes, not the alloc size.
1338 */
1339
LodePNG_compress(unsigned char ** out,size_t * outsize,const unsigned char * in,size_t insize,const LodeZlib_DeflateSettings * settings)1340 static unsigned LodePNG_compress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DeflateSettings* settings)
1341 {
1342 return LodeZlib_compress(out, outsize, in, insize, settings);
1343 }
1344
1345 /* ////////////////////////////////////////////////////////////////////////// */
1346 /* / CRC32 / */
1347 /* ////////////////////////////////////////////////////////////////////////// */
1348
1349 static unsigned Crc32_crc_table_computed = 0;
1350 static unsigned Crc32_crc_table[256];
1351
1352 /*Make the table for a fast CRC.*/
Crc32_make_crc_table(void)1353 static void Crc32_make_crc_table(void)
1354 {
1355 unsigned int c, k, n;
1356 for(n = 0; n < 256; n++)
1357 {
1358 c = n;
1359 for(k = 0; k < 8; k++)
1360 {
1361 if(c & 1) c = (unsigned int)(0xedb88320L ^ (c >> 1));
1362 else c = c >> 1;
1363 }
1364 Crc32_crc_table[n] = c;
1365 }
1366 Crc32_crc_table_computed = 1;
1367 }
1368
1369 /*Update a running CRC with the bytes buf[0..len-1]--the CRC should be
1370 initialized to all 1's, and the transmitted value is the 1's complement of the
1371 final running CRC (see the crc() routine below).*/
Crc32_update_crc(const unsigned char * buf,unsigned int crc,size_t len)1372 static unsigned Crc32_update_crc(const unsigned char* buf, unsigned int crc, size_t len)
1373 {
1374 unsigned int c = crc;
1375 size_t n;
1376
1377 if(!Crc32_crc_table_computed) Crc32_make_crc_table();
1378 for(n = 0; n < len; n++)
1379 {
1380 c = Crc32_crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
1381 }
1382 return c;
1383 }
1384
1385 /*Return the CRC of the bytes buf[0..len-1].*/
Crc32_crc(const unsigned char * buf,size_t len)1386 static unsigned Crc32_crc(const unsigned char* buf, size_t len)
1387 {
1388 return Crc32_update_crc(buf, 0xffffffffu, len) ^ 0xffffffffu;
1389 }
1390
1391 /* ////////////////////////////////////////////////////////////////////////// */
1392 /* / Reading and writing single bits and bytes from/to stream for LodePNG / */
1393 /* ////////////////////////////////////////////////////////////////////////// */
1394
readBitFromReversedStream(size_t * bitpointer,const unsigned char * bitstream)1395 static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream)
1396 {
1397 unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
1398 (*bitpointer)++;
1399 return result;
1400 }
1401
readBitsFromReversedStream(size_t * bitpointer,const unsigned char * bitstream,size_t nbits)1402 static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
1403 {
1404 unsigned result = 0;
1405 size_t i;
1406 for(i = nbits - 1; i < nbits; i--) result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i;
1407 return result;
1408 }
1409
setBitOfReversedStream(size_t * bitpointer,unsigned char * bitstream,unsigned char bit)1410 static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
1411 {
1412 /*the current bit in bitstream may be 0 or 1 for this to work*/
1413 if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
1414 else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
1415 (*bitpointer)++;
1416 }
1417
LodePNG_read32bitInt(const unsigned char * buffer)1418 static unsigned LodePNG_read32bitInt(const unsigned char* buffer)
1419 {
1420 return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
1421 }
1422
LodePNG_set32bitInt(unsigned char * buffer,unsigned value)1423 static void LodePNG_set32bitInt(unsigned char* buffer, unsigned value) /*buffer must have at least 4 allocated bytes available*/
1424 {
1425 buffer[0] = (unsigned char)((value >> 24) & 0xff);
1426 buffer[1] = (unsigned char)((value >> 16) & 0xff);
1427 buffer[2] = (unsigned char)((value >> 8) & 0xff);
1428 buffer[3] = (unsigned char)((value ) & 0xff);
1429 }
1430
LodePNG_add32bitInt(ucvector * buffer,unsigned value)1431 static void LodePNG_add32bitInt(ucvector* buffer, unsigned value)
1432 {
1433 ucvector_resize(buffer, buffer->size + 4);
1434 LodePNG_set32bitInt(&buffer->data[buffer->size - 4], value);
1435 }
1436
1437 /* ////////////////////////////////////////////////////////////////////////// */
1438 /* / PNG chunks / */
1439 /* ////////////////////////////////////////////////////////////////////////// */
1440
LodePNG_chunk_length(const unsigned char * chunk)1441 static unsigned LodePNG_chunk_length(const unsigned char* chunk) /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/
1442 {
1443 return LodePNG_read32bitInt(&chunk[0]);
1444 }
1445
LodePNG_chunk_generate_crc(unsigned char * chunk)1446 static void LodePNG_chunk_generate_crc(unsigned char* chunk) /*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/
1447 {
1448 unsigned length = LodePNG_chunk_length(chunk);
1449 unsigned CRC = Crc32_crc(&chunk[4], length + 4);
1450 LodePNG_set32bitInt(chunk + 8 + length, CRC);
1451 }
1452
LodePNG_create_chunk(unsigned char ** out,size_t * outlength,unsigned length,const char * type,const unsigned char * data)1453 static unsigned LodePNG_create_chunk(unsigned char** out, size_t* outlength, unsigned length, const char* type, const unsigned char* data) /*appends new chunk to out. Returns error code; may change memory address of out buffer*/
1454 {
1455 unsigned i;
1456 unsigned char *chunk, *new_buffer;
1457 size_t new_length = (*outlength) + length + 12;
1458 if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/
1459 new_buffer = (unsigned char*)realloc(*out, new_length);
1460 if(!new_buffer) return 9930;
1461 (*out) = new_buffer;
1462 (*outlength) = new_length;
1463 chunk = &(*out)[(*outlength) - length - 12];
1464
1465 /*1: length*/
1466 LodePNG_set32bitInt(chunk, (unsigned)length);
1467
1468 /*2: chunk name (4 letters)*/
1469 chunk[4] = type[0];
1470 chunk[5] = type[1];
1471 chunk[6] = type[2];
1472 chunk[7] = type[3];
1473
1474 /*3: the data*/
1475 for(i = 0; i < length; i++) chunk[8 + i] = data[i];
1476
1477 /*4: CRC (of the chunkname characters and the data)*/
1478 LodePNG_chunk_generate_crc(chunk);
1479
1480 return 0;
1481 }
1482
1483 /* ////////////////////////////////////////////////////////////////////////// */
1484 /* / Color types and such / */
1485 /* ////////////////////////////////////////////////////////////////////////// */
1486
1487 /*return type is a LodePNG error code*/
checkColorValidity(unsigned colorType,unsigned bd)1488 static unsigned checkColorValidity(unsigned colorType, unsigned bd) /*bd = bitDepth*/
1489 {
1490 switch(colorType)
1491 {
1492 case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/
1493 case 2: if(!( bd == 8 || bd == 16)) return 37; break; /*RGB*/
1494 case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/
1495 case 4: if(!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/
1496 case 6: if(!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/
1497 default: return 31;
1498 }
1499 return 0; /*allowed color type / bits combination*/
1500 }
1501
getNumColorChannels(unsigned colorType)1502 static unsigned getNumColorChannels(unsigned colorType)
1503 {
1504 switch(colorType)
1505 {
1506 case 0: return 1; /*grey*/
1507 case 2: return 3; /*RGB*/
1508 case 3: return 1; /*palette*/
1509 case 4: return 2; /*grey + alpha*/
1510 case 6: return 4; /*RGBA*/
1511 }
1512 return 0; /*nonexistent color type*/
1513 }
1514
getBpp(unsigned colorType,unsigned bitDepth)1515 static unsigned getBpp(unsigned colorType, unsigned bitDepth)
1516 {
1517 return getNumColorChannels(colorType) * bitDepth; /*bits per pixel is amount of channels * bits per channel*/
1518 }
1519
1520 /* ////////////////////////////////////////////////////////////////////////// */
1521
LodePNG_InfoColor_init(LodePNG_InfoColor * info)1522 static void LodePNG_InfoColor_init(LodePNG_InfoColor* info)
1523 {
1524 info->key_defined = 0;
1525 info->key_r = info->key_g = info->key_b = 0;
1526 info->colorType = 6;
1527 info->bitDepth = 8;
1528 info->palette = 0;
1529 info->palettesize = 0;
1530 }
1531
LodePNG_InfoColor_cleanup(LodePNG_InfoColor * info)1532 static void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info)
1533 {
1534 LodePNG_InfoColor_clearPalette(info);
1535 }
1536
LodePNG_InfoColor_clearPalette(LodePNG_InfoColor * info)1537 static void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor* info)
1538 {
1539 if(info->palette) free(info->palette);
1540 info->palettesize = 0;
1541 }
1542
LodePNG_InfoColor_addPalette(LodePNG_InfoColor * info,unsigned char r,unsigned char g,unsigned char b,unsigned char a)1543 unsigned LodePNG_InfoColor_addPalette(LodePNG_InfoColor* info, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
1544 {
1545 unsigned char* data;
1546 /*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with the max of 256 colors, it'll have the exact alloc size*/
1547 if(!(info->palettesize & (info->palettesize - 1))) /*if palettesize is 0 or a power of two*/
1548 {
1549 /*allocated data must be at least 4* palettesize (for 4 color bytes)*/
1550 size_t alloc_size = info->palettesize == 0 ? 4 : info->palettesize * 4 * 2;
1551 data = (unsigned char*)realloc(info->palette, alloc_size);
1552 if(!data) return 9931;
1553 else info->palette = data;
1554 }
1555 info->palette[4 * info->palettesize + 0] = r;
1556 info->palette[4 * info->palettesize + 1] = g;
1557 info->palette[4 * info->palettesize + 2] = b;
1558 info->palette[4 * info->palettesize + 3] = a;
1559 info->palettesize++;
1560 return 0;
1561 }
1562
LodePNG_InfoColor_getBpp(const LodePNG_InfoColor * info)1563 static unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor* info) { return getBpp(info->colorType, info->bitDepth); } /*calculate bits per pixel out of colorType and bitDepth*/
LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor * info)1564 static unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor* info) { return info->colorType == 0 || info->colorType == 4; }
LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor * info)1565 static unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor* info) { return (info->colorType & 4) != 0; }
1566
LodePNG_InfoColor_equal(const LodePNG_InfoColor * info1,const LodePNG_InfoColor * info2)1567 static unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor* info1, const LodePNG_InfoColor* info2)
1568 {
1569 return info1->colorType == info2->colorType
1570 && info1->bitDepth == info2->bitDepth; /*palette and color key not compared*/
1571 }
1572
1573
LodePNG_InfoPng_init(LodePNG_InfoPng * info)1574 static void LodePNG_InfoPng_init(LodePNG_InfoPng* info)
1575 {
1576 info->width = info->height = 0;
1577 LodePNG_InfoColor_init(&info->color);
1578 info->interlaceMethod = 0;
1579 info->compressionMethod = 0;
1580 info->filterMethod = 0;
1581 }
1582
LodePNG_InfoPng_cleanup(LodePNG_InfoPng * info)1583 static void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info)
1584 {
1585 LodePNG_InfoColor_cleanup(&info->color);
1586 }
1587
LodePNG_InfoPng_copy(LodePNG_InfoPng * dest,const LodePNG_InfoPng * source)1588 static unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng* dest, const LodePNG_InfoPng* source)
1589 {
1590 unsigned error = 0;
1591 LodePNG_InfoPng_cleanup(dest);
1592 *dest = *source;
1593 LodePNG_InfoColor_init(&dest->color);
1594 error = LodePNG_InfoColor_copy(&dest->color, &source->color); if(error) return error;
1595 return error;
1596 }
1597
LodePNG_InfoColor_copy(LodePNG_InfoColor * dest,const LodePNG_InfoColor * source)1598 static unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor* dest, const LodePNG_InfoColor* source)
1599 {
1600 size_t i;
1601 LodePNG_InfoColor_cleanup(dest);
1602 *dest = *source;
1603 dest->palette = (unsigned char*)malloc(source->palettesize * 4);
1604 if(!dest->palette && source->palettesize) return 9935;
1605 for(i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i];
1606 return 0;
1607 }
1608
LodePNG_InfoRaw_init(LodePNG_InfoRaw * info)1609 static void LodePNG_InfoRaw_init(LodePNG_InfoRaw* info)
1610 {
1611 LodePNG_InfoColor_init(&info->color);
1612 }
1613
LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw * info)1614 static void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw* info)
1615 {
1616 LodePNG_InfoColor_cleanup(&info->color);
1617 }
1618
LodePNG_InfoRaw_copy(LodePNG_InfoRaw * dest,const LodePNG_InfoRaw * source)1619 static unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw* dest, const LodePNG_InfoRaw* source)
1620 {
1621 unsigned error = 0;
1622 LodePNG_InfoRaw_cleanup(dest);
1623 *dest = *source;
1624 LodePNG_InfoColor_init(&dest->color);
1625 error = LodePNG_InfoColor_copy(&dest->color, &source->color);
1626 return error;
1627 }
1628
1629 /* ////////////////////////////////////////////////////////////////////////// */
1630
1631 /*
1632 converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code
1633 the out buffer must have (w * h * bpp + 7) / 8 bytes, where bpp is the bits per pixel of the output color type (LodePNG_InfoColor_getBpp)
1634 for < 8 bpp images, there may _not_ be padding bits at the end of scanlines.
1635 */
LodePNG_convert(unsigned char * out,const unsigned char * in,LodePNG_InfoColor * infoOut,LodePNG_InfoColor * infoIn,unsigned w,unsigned h)1636 static unsigned LodePNG_convert(unsigned char* out, const unsigned char* in, LodePNG_InfoColor* infoOut, LodePNG_InfoColor* infoIn, unsigned w, unsigned h)
1637 {
1638 const size_t numpixels = w * h; /*amount of pixels*/
1639 const unsigned OUT_BYTES = LodePNG_InfoColor_getBpp(infoOut) / 8; /*bytes per pixel in the output image*/
1640 const unsigned OUT_ALPHA = LodePNG_InfoColor_isAlphaType(infoOut); /*use 8-bit alpha channel*/
1641 size_t i, c, bp = 0; /*bitpointer, used by less-than-8-bit color types*/
1642
1643 /*cases where in and out already have the same format*/
1644 if(LodePNG_InfoColor_equal(infoIn, infoOut))
1645 {
1646 size_t i, size = (w * h * LodePNG_InfoColor_getBpp(infoIn) + 7) / 8;
1647 for(i = 0; i < size; i++) out[i] = in[i];
1648 return 0;
1649 }
1650
1651 if((infoOut->colorType == 2 || infoOut->colorType == 6) && infoOut->bitDepth == 8)
1652 {
1653 if(infoIn->bitDepth == 8)
1654 {
1655 switch(infoIn->colorType)
1656 {
1657 case 0: /*greyscale color*/
1658 for(i = 0; i < numpixels; i++)
1659 {
1660 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1661 out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[i];
1662 if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
1663 }
1664 break;
1665 case 2: /*RGB color*/
1666 for(i = 0; i < numpixels; i++)
1667 {
1668 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1669 for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[3 * i + c];
1670 if(OUT_ALPHA && infoIn->key_defined == 1 && in[3 * i + 0] == infoIn->key_r && in[3 * i + 1] == infoIn->key_g && in[3 * i + 2] == infoIn->key_b) out[OUT_BYTES * i + 3] = 0;
1671 }
1672 break;
1673 case 3: /*indexed color (palette)*/
1674 for(i = 0; i < numpixels; i++)
1675 {
1676 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1677 if(in[i] >= infoIn->palettesize) return 46;
1678 for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * in[i] + c]; /*get rgb colors from the palette*/
1679 }
1680 break;
1681 case 4: /*greyscale with alpha*/
1682 for(i = 0; i < numpixels; i++)
1683 {
1684 out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i + 0];
1685 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[2 * i + 1];
1686 }
1687 break;
1688 case 6: /*RGB with alpha*/
1689 for(i = 0; i < numpixels; i++)
1690 {
1691 for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[4 * i + c];
1692 }
1693 break;
1694 default: break;
1695 }
1696 }
1697 else if(infoIn->bitDepth == 16)
1698 {
1699 switch(infoIn->colorType)
1700 {
1701 case 0: /*greyscale color*/
1702 for(i = 0; i < numpixels; i++)
1703 {
1704 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1705 out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i];
1706 if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
1707 }
1708 break;
1709 case 2: /*RGB color*/
1710 for(i = 0; i < numpixels; i++)
1711 {
1712 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1713 for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[6 * i + 2 * c];
1714 if(OUT_ALPHA && infoIn->key_defined && 256U * in[6 * i + 0] + in[6 * i + 1] == infoIn->key_r && 256U * in[6 * i + 2] + in[6 * i + 3] == infoIn->key_g && 256U * in[6 * i + 4] + in[6 * i + 5] == infoIn->key_b) out[OUT_BYTES * i + 3] = 0;
1715 }
1716 break;
1717 case 4: /*greyscale with alpha*/
1718 for(i = 0; i < numpixels; i++)
1719 {
1720 out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[4 * i]; /*most significant byte*/
1721 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[4 * i + 2];
1722 }
1723 break;
1724 case 6: /*RGB with alpha*/
1725 for(i = 0; i < numpixels; i++)
1726 {
1727 for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[8 * i + 2 * c];
1728 }
1729 break;
1730 default: break;
1731 }
1732 }
1733 else /*infoIn->bitDepth is less than 8 bit per channel*/
1734 {
1735 switch(infoIn->colorType)
1736 {
1737 case 0: /*greyscale color*/
1738 for(i = 0; i < numpixels; i++)
1739 {
1740 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
1741 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1742 if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 3] = 0;
1743 value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
1744 out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = (unsigned char)(value);
1745 }
1746 break;
1747 case 3: /*indexed color (palette)*/
1748 for(i = 0; i < numpixels; i++)
1749 {
1750 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
1751 if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
1752 if(value >= infoIn->palettesize) return 47;
1753 for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * value + c]; /*get rgb colors from the palette*/
1754 }
1755 break;
1756 default: break;
1757 }
1758 }
1759 }
1760 else if(LodePNG_InfoColor_isGreyscaleType(infoOut) && infoOut->bitDepth == 8) /*conversion from greyscale to greyscale*/
1761 {
1762 if(!LodePNG_InfoColor_isGreyscaleType(infoIn)) return 62;
1763 if(infoIn->bitDepth == 8)
1764 {
1765 switch(infoIn->colorType)
1766 {
1767 case 0: /*greyscale color*/
1768 for(i = 0; i < numpixels; i++)
1769 {
1770 if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
1771 out[OUT_BYTES * i] = in[i];
1772 if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
1773 }
1774 break;
1775 case 4: /*greyscale with alpha*/
1776 for(i = 0; i < numpixels; i++)
1777 {
1778 out[OUT_BYTES * i + 0] = in[2 * i + 0];
1779 if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[2 * i + 1];
1780 }
1781 break;
1782 default: return 31;
1783 }
1784 }
1785 else if(infoIn->bitDepth == 16)
1786 {
1787 switch(infoIn->colorType)
1788 {
1789 case 0: /*greyscale color*/
1790 for(i = 0; i < numpixels; i++)
1791 {
1792 if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
1793 out[OUT_BYTES * i] = in[2 * i];
1794 if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
1795 }
1796 break;
1797 case 4: /*greyscale with alpha*/
1798 for(i = 0; i < numpixels; i++)
1799 {
1800 out[OUT_BYTES * i] = in[4 * i]; /*most significant byte*/
1801 if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[4 * i + 2]; /*most significant byte*/
1802 }
1803 break;
1804 default: return 31;
1805 }
1806 }
1807 else /*infoIn->bitDepth is less than 8 bit per channel*/
1808 {
1809 if(infoIn->colorType != 0) return 31; /*colorType 0 is the only greyscale type with < 8 bits per channel*/
1810 for(i = 0; i < numpixels; i++)
1811 {
1812 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
1813 if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
1814 if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 1] = 0;
1815 value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
1816 out[OUT_BYTES * i] = (unsigned char)(value);
1817 }
1818 }
1819 }
1820 else return 59;
1821
1822 return 0;
1823 }
1824
1825 /*Path predictor, used by PNG filter type 4*/
paethPredictor(int a,int b,int c)1826 static int paethPredictor(int a, int b, int c)
1827 {
1828 int p = a + b - c;
1829 int pa = p > a ? p - a : a - p;
1830 int pb = p > b ? p - b : b - p;
1831 int pc = p > c ? p - c : c - p;
1832
1833 if(pa <= pb && pa <= pc) return a;
1834 else if(pb <= pc) return b;
1835 else return c;
1836 }
1837
1838 /*shared values used by multiple Adam7 related functions*/
1839
1840 static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
1841 static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
1842 static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
1843 static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
1844
Adam7_getpassvalues(unsigned passw[7],unsigned passh[7],size_t filter_passstart[8],size_t padded_passstart[8],size_t passstart[8],unsigned w,unsigned h,unsigned bpp)1845 static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp)
1846 {
1847 /*the passstart values have 8 values: the 8th one actually indicates the byte after the end of the 7th (= last) pass*/
1848 unsigned i;
1849
1850 /*calculate width and height in pixels of each pass*/
1851 for(i = 0; i < 7; i++)
1852 {
1853 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
1854 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
1855 if(passw[i] == 0) passh[i] = 0;
1856 if(passh[i] == 0) passw[i] = 0;
1857 }
1858
1859 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
1860 for(i = 0; i < 7; i++)
1861 {
1862 filter_passstart[i + 1] = filter_passstart[i] + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/
1863 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); /*bits padded if needed to fill full byte at end of each scanline*/
1864 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; /*only padded at end of reduced image*/
1865 }
1866 }
1867
1868
1869 /* ////////////////////////////////////////////////////////////////////////// */
1870 /* / PNG Encoder / */
1871 /* ////////////////////////////////////////////////////////////////////////// */
1872
1873 /*chunkName must be string of 4 characters*/
addChunk(ucvector * out,const char * chunkName,const unsigned char * data,size_t length)1874 static unsigned addChunk(ucvector* out, const char* chunkName, const unsigned char* data, size_t length)
1875 {
1876 unsigned error = LodePNG_create_chunk(&out->data, &out->size, (unsigned)length, chunkName, data);
1877 if(error) return error;
1878 out->allocsize = out->size; /*fix the allocsize again*/
1879 return 0;
1880 }
1881
writeSignature(ucvector * out)1882 static void writeSignature(ucvector* out)
1883 {
1884 /*8 bytes PNG signature*/
1885 ucvector_push_back(out, 137);
1886 ucvector_push_back(out, 80);
1887 ucvector_push_back(out, 78);
1888 ucvector_push_back(out, 71);
1889 ucvector_push_back(out, 13);
1890 ucvector_push_back(out, 10);
1891 ucvector_push_back(out, 26);
1892 ucvector_push_back(out, 10);
1893 }
1894
addChunk_IHDR(ucvector * out,unsigned w,unsigned h,unsigned bitDepth,unsigned colorType,unsigned interlaceMethod)1895 static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h, unsigned bitDepth, unsigned colorType, unsigned interlaceMethod)
1896 {
1897 unsigned error = 0;
1898 ucvector header;
1899 ucvector_init(&header);
1900
1901 LodePNG_add32bitInt(&header, w); /*width*/
1902 LodePNG_add32bitInt(&header, h); /*height*/
1903 ucvector_push_back(&header, (unsigned char)bitDepth); /*bit depth*/
1904 ucvector_push_back(&header, (unsigned char)colorType); /*color type*/
1905 ucvector_push_back(&header, 0); /*compression method*/
1906 ucvector_push_back(&header, 0); /*filter method*/
1907 ucvector_push_back(&header, interlaceMethod); /*interlace method*/
1908
1909 error = addChunk(out, "IHDR", header.data, header.size);
1910 ucvector_cleanup(&header);
1911
1912 return error;
1913 }
1914
addChunk_PLTE(ucvector * out,const LodePNG_InfoColor * info)1915 static unsigned addChunk_PLTE(ucvector* out, const LodePNG_InfoColor* info)
1916 {
1917 unsigned error = 0;
1918 size_t i;
1919 ucvector PLTE;
1920 ucvector_init(&PLTE);
1921 for(i = 0; i < info->palettesize * 4; i++) if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]); /*add all channels except alpha channel*/
1922 error = addChunk(out, "PLTE", PLTE.data, PLTE.size);
1923 ucvector_cleanup(&PLTE);
1924
1925 return error;
1926 }
1927
addChunk_tRNS(ucvector * out,const LodePNG_InfoColor * info)1928 static unsigned addChunk_tRNS(ucvector* out, const LodePNG_InfoColor* info)
1929 {
1930 unsigned error = 0;
1931 size_t i;
1932 ucvector tRNS;
1933 ucvector_init(&tRNS);
1934 if(info->colorType == 3)
1935 {
1936 for(i = 0; i < info->palettesize; i++) ucvector_push_back(&tRNS, info->palette[4 * i + 3]); /*add only alpha channel*/
1937 }
1938 else if(info->colorType == 0)
1939 {
1940 if(info->key_defined)
1941 {
1942 ucvector_push_back(&tRNS, (unsigned char)(info->key_r / 256));
1943 ucvector_push_back(&tRNS, (unsigned char)(info->key_r % 256));
1944 }
1945 }
1946 else if(info->colorType == 2)
1947 {
1948 if(info->key_defined)
1949 {
1950 ucvector_push_back(&tRNS, (unsigned char)(info->key_r / 256));
1951 ucvector_push_back(&tRNS, (unsigned char)(info->key_r % 256));
1952 ucvector_push_back(&tRNS, (unsigned char)(info->key_g / 256));
1953 ucvector_push_back(&tRNS, (unsigned char)(info->key_g % 256));
1954 ucvector_push_back(&tRNS, (unsigned char)(info->key_b / 256));
1955 ucvector_push_back(&tRNS, (unsigned char)(info->key_b % 256));
1956 }
1957 }
1958
1959 error = addChunk(out, "tRNS", tRNS.data, tRNS.size);
1960 ucvector_cleanup(&tRNS);
1961
1962 return error;
1963 }
1964
addChunk_IDAT(ucvector * out,const unsigned char * data,size_t datasize,LodeZlib_DeflateSettings * zlibsettings)1965 static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize, LodeZlib_DeflateSettings* zlibsettings)
1966 {
1967 ucvector zlibdata;
1968 unsigned error = 0;
1969
1970 /*compress with the Zlib compressor*/
1971 ucvector_init(&zlibdata);
1972 error = LodePNG_compress(&zlibdata.data, &zlibdata.size, data, datasize, zlibsettings);
1973 if(!error) error = addChunk(out, "IDAT", zlibdata.data, zlibdata.size);
1974 ucvector_cleanup(&zlibdata);
1975
1976 return error;
1977 }
1978
addChunk_IEND(ucvector * out)1979 static unsigned addChunk_IEND(ucvector* out)
1980 {
1981 unsigned error = 0;
1982 error = addChunk(out, "IEND", 0, 0);
1983 return error;
1984 }
1985
filterScanline(unsigned char * out,const unsigned char * scanline,const unsigned char * prevline,size_t length,size_t bytewidth,unsigned char filterType)1986 static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline, size_t length, size_t bytewidth, unsigned char filterType)
1987 {
1988 size_t i;
1989 switch(filterType)
1990 {
1991 case 0:
1992 for(i = 0; i < length; i++) out[i] = scanline[i];
1993 break;
1994 case 1:
1995 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
1996 for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth];
1997 break;
1998 case 2:
1999 if(prevline) for(i = 0; i < length; i++) out[i] = scanline[i] - prevline[i];
2000 else for(i = 0; i < length; i++) out[i] = scanline[i];
2001 break;
2002 case 3:
2003 if(prevline)
2004 {
2005 for(i = 0; i < bytewidth; i++) out[i] = scanline[i] - prevline[i] / 2;
2006 for(i = bytewidth; i < length; i++) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) / 2);
2007 }
2008 else
2009 {
2010 for(i = 0; i < length; i++) out[i] = scanline[i];
2011 for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth] / 2;
2012 }
2013 break;
2014 case 4:
2015 if(prevline)
2016 {
2017 for(i = 0; i < bytewidth; i++) out[i] = (unsigned char)(scanline[i] - paethPredictor(0, prevline[i], 0));
2018 for(i = bytewidth; i < length; i++) out[i] = (unsigned char)(scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth]));
2019 }
2020 else
2021 {
2022 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
2023 for(i = bytewidth; i < length; i++) out[i] = (unsigned char)(scanline[i] - paethPredictor(scanline[i - bytewidth], 0, 0));
2024 }
2025 break;
2026 default: return; /*nonexistent filter type given*/
2027 }
2028 }
2029
filter(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,const LodePNG_InfoColor * info)2030 static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, const LodePNG_InfoColor* info)
2031 {
2032 /*
2033 For PNG filter method 0
2034 out must be a buffer with as size: h + (w * h * bpp + 7) / 8, because there are the scanlines with 1 extra byte per scanline
2035
2036 There is a nice heuristic described here: http://www.cs.toronto.edu/~cosmin/pngtech/optipng.html. It says:
2037 * If the image type is Palette, or the bit depth is smaller than 8, then do not filter the image (i.e. use fixed filtering, with the filter None).
2038 * (The other case) If the image type is Grayscale or RGB (with or without Alpha), and the bit depth is not smaller than 8, then use adaptive filtering heuristic as follows: independently for each row, apply all five filters and select the filter that produces the smallest sum of absolute values per row.
2039
2040 Here the above method is used mostly. Note though that it appears to be better to use the adaptive filtering on the plasma 8-bit palette example, but that image isn't the best reference for palette images in general.
2041 */
2042
2043 unsigned bpp = LodePNG_InfoColor_getBpp(info);
2044 size_t linebytes = (w * bpp + 7) / 8; /*the width of a scanline in bytes, not including the filter type*/
2045 size_t bytewidth = (bpp + 7) / 8; /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
2046 const unsigned char* prevline = 0;
2047 unsigned x, y;
2048 unsigned heuristic;
2049 unsigned error = 0;
2050
2051 if(bpp == 0) return 31; /*invalid color type*/
2052
2053 /*choose heuristic as described above*/
2054 if(info->colorType == 3 || info->bitDepth < 8) heuristic = 0;
2055 else heuristic = 1;
2056
2057 if(heuristic == 0) /*None filtertype for everything*/
2058 {
2059 for(y = 0; y < h; y++)
2060 {
2061 size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
2062 size_t inindex = linebytes * y;
2063 const unsigned TYPE = 0;
2064 out[outindex] = TYPE; /*filter type byte*/
2065 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, TYPE);
2066 prevline = &in[inindex];
2067 }
2068 }
2069 else if(heuristic == 1) /*adaptive filtering*/
2070 {
2071 size_t sum[5];
2072 ucvector attempt[5]; /*five filtering attempts, one for each filter type*/
2073 size_t smallest = 0;
2074 unsigned type, bestType = 0;
2075
2076 for(type = 0; type < 5; type++) ucvector_init(&attempt[type]);
2077 for(type = 0; type < 5; type++)
2078 {
2079 if(!ucvector_resize(&attempt[type], linebytes)) { error = 9949; break; }
2080 }
2081
2082 if(!error)
2083 {
2084 for(y = 0; y < h; y++)
2085 {
2086 /*try the 5 filter types*/
2087 for(type = 0; type < 5; type++)
2088 {
2089 filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type);
2090
2091 /*calculate the sum of the result*/
2092 sum[type] = 0;
2093 for(x = 0; x < attempt[type].size; x+=3) sum[type] += attempt[type].data[x]; /*note that not all pixels are checked to speed this up while still having probably the best choice*/
2094
2095 /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/
2096 if(type == 0 || sum[type] < smallest)
2097 {
2098 bestType = type;
2099 smallest = sum[type];
2100 }
2101 }
2102
2103 prevline = &in[y * linebytes];
2104
2105 /*now fill the out values*/
2106 out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
2107 for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
2108 }
2109 }
2110
2111 for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
2112 }
2113
2114 return error;
2115 }
2116
addPaddingBits(unsigned char * out,const unsigned char * in,size_t olinebits,size_t ilinebits,unsigned h)2117 static void addPaddingBits(unsigned char* out, const unsigned char* in, size_t olinebits, size_t ilinebits, unsigned h)
2118 {
2119 /*The opposite of the removePaddingBits function
2120 olinebits must be >= ilinebits*/
2121 unsigned y;
2122 size_t diff = olinebits - ilinebits;
2123 size_t obp = 0, ibp = 0; /*bit pointers*/
2124 for(y = 0; y < h; y++)
2125 {
2126 size_t x;
2127 for(x = 0; x < ilinebits; x++)
2128 {
2129 unsigned char bit = readBitFromReversedStream(&ibp, in);
2130 setBitOfReversedStream(&obp, out, bit);
2131 }
2132 /*obp += diff; --> no, fill in some value in the padding bits too, to avoid "Use of uninitialised value of size ###" warning from valgrind*/
2133 for(x = 0; x < diff; x++) setBitOfReversedStream(&obp, out, 0);
2134 }
2135 }
2136
Adam7_interlace(unsigned char * out,const unsigned char * in,unsigned w,unsigned h,unsigned bpp)2137 static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
2138 {
2139 /*Note: this function works on image buffers WITHOUT padding bits at end of scanlines with non-multiple-of-8 bit amounts, only between reduced images is padding*/
2140 unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
2141 unsigned i;
2142
2143 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
2144
2145 if(bpp >= 8)
2146 {
2147 for(i = 0; i < 7; i++)
2148 {
2149 unsigned x, y, b;
2150 size_t bytewidth = bpp / 8;
2151 for(y = 0; y < passh[i]; y++)
2152 for(x = 0; x < passw[i]; x++)
2153 {
2154 size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
2155 size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth;
2156 for(b = 0; b < bytewidth; b++)
2157 {
2158 out[pixeloutstart + b] = in[pixelinstart + b];
2159 }
2160 }
2161 }
2162 }
2163 else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
2164 {
2165 for(i = 0; i < 7; i++)
2166 {
2167 unsigned x, y, b;
2168 unsigned ilinebits = bpp * passw[i];
2169 unsigned olinebits = bpp * w;
2170 size_t obp, ibp; /*bit pointers (for out and in buffer)*/
2171 for(y = 0; y < passh[i]; y++)
2172 for(x = 0; x < passw[i]; x++)
2173 {
2174 ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
2175 obp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
2176 for(b = 0; b < bpp; b++)
2177 {
2178 unsigned char bit = readBitFromReversedStream(&ibp, in);
2179 setBitOfReversedStream(&obp, out, bit);
2180 }
2181 }
2182 }
2183 }
2184 }
2185
2186 /*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image*/
preProcessScanlines(unsigned char ** out,size_t * outsize,const unsigned char * in,const LodePNG_InfoPng * infoPng)2187 static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, const LodePNG_InfoPng* infoPng) /*return value is error*/
2188 {
2189 /*
2190 This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
2191 *) if no Adam7: 1) add padding bits (= possible extra bits per scanline if bpp < 8) 2) filter
2192 *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
2193 */
2194 unsigned bpp = LodePNG_InfoColor_getBpp(&infoPng->color);
2195 unsigned w = infoPng->width;
2196 unsigned h = infoPng->height;
2197 unsigned error = 0;
2198
2199 if(infoPng->interlaceMethod == 0)
2200 {
2201 *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/
2202 *out = (unsigned char*)malloc(*outsize);
2203 if(!(*out) && (*outsize)) error = 9950;
2204
2205 if(!error)
2206 {
2207 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
2208 {
2209 ucvector padded;
2210 ucvector_init(&padded);
2211 if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9951;
2212 if(!error)
2213 {
2214 addPaddingBits(padded.data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
2215 error = filter(*out, padded.data, w, h, &infoPng->color);
2216 }
2217 ucvector_cleanup(&padded);
2218 }
2219 else error = filter(*out, in, w, h, &infoPng->color); /*we can immediately filter into the out buffer, no other steps needed*/
2220 }
2221 }
2222 else /*interlaceMethod is 1 (Adam7)*/
2223 {
2224 unsigned char* adam7 = (unsigned char*)malloc((h * w * bpp + 7) / 8);
2225 if(!adam7 && ((h * w * bpp + 7) / 8)) error = 9952; /*malloc failed*/
2226
2227 while(!error) /*not a real while loop, used to break out to cleanup to avoid a goto*/
2228 {
2229 unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
2230 unsigned i;
2231
2232 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
2233
2234 *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
2235 *out = (unsigned char*)malloc(*outsize);
2236 if(!(*out) && (*outsize)) { error = 9953; break; }
2237
2238 Adam7_interlace(adam7, in, w, h, bpp);
2239
2240 for(i = 0; i < 7; i++)
2241 {
2242 if(bpp < 8)
2243 {
2244 ucvector padded;
2245 ucvector_init(&padded);
2246 if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9954;
2247 if(!error)
2248 {
2249 addPaddingBits(&padded.data[padded_passstart[i]], &adam7[passstart[i]], ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
2250 error = filter(&(*out)[filter_passstart[i]], &padded.data[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
2251 }
2252
2253 ucvector_cleanup(&padded);
2254 }
2255 else
2256 {
2257 error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
2258 }
2259 }
2260
2261 break;
2262 }
2263
2264 free(adam7);
2265 }
2266
2267 return error;
2268 }
2269
2270 /*palette must have 4 * palettesize bytes allocated*/
isPaletteFullyOpaque(const unsigned char * palette,size_t palettesize)2271 static unsigned isPaletteFullyOpaque(const unsigned char* palette, size_t palettesize) /*palette given in format RGBARGBARGBARGBA...*/
2272 {
2273 size_t i;
2274 for(i = 0; i < palettesize; i++)
2275 {
2276 if(palette[4 * i + 3] != 255) return 0;
2277 }
2278 return 1;
2279 }
2280
2281 /*this function checks if the input image given by the user has no transparent pixels*/
isFullyOpaque(const unsigned char * image,unsigned w,unsigned h,const LodePNG_InfoColor * info)2282 static unsigned isFullyOpaque(const unsigned char* image, unsigned w, unsigned h, const LodePNG_InfoColor* info)
2283 {
2284 /*TODO: When the user specified a color key for the input image, then this function must also check for pixels that are the same as the color key and treat those as transparent.*/
2285
2286 unsigned i, numpixels = w * h;
2287 if(info->colorType == 6)
2288 {
2289 if(info->bitDepth == 8)
2290 {
2291 for(i = 0; i < numpixels; i++) if(image[i * 4 + 3] != 255) return 0;
2292 }
2293 else
2294 {
2295 for(i = 0; i < numpixels; i++) if(image[i * 8 + 6] != 255 || image[i * 8 + 7] != 255) return 0;
2296 }
2297 return 1; /*no single pixel with alpha channel other than 255 found*/
2298 }
2299 else if(info->colorType == 4)
2300 {
2301 if(info->bitDepth == 8)
2302 {
2303 for(i = 0; i < numpixels; i++) if(image[i * 2 + 1] != 255) return 0;
2304 }
2305 else
2306 {
2307 for(i = 0; i < numpixels; i++) if(image[i * 4 + 2] != 255 || image[i * 4 + 3] != 255) return 0;
2308 }
2309 return 1; /*no single pixel with alpha channel other than 255 found*/
2310 }
2311 else if(info->colorType == 3)
2312 {
2313 /*when there's a palette, we could check every pixel for translucency, but much quicker is to just check the palette*/
2314 return(isPaletteFullyOpaque(info->palette, info->palettesize));
2315 }
2316
2317 return 0; /*color type that isn't supported by this function yet, so assume there is transparency to be safe*/
2318 }
2319
LodePNG_encode(LodePNG_Encoder * encoder,unsigned char ** out,size_t * outsize,const unsigned char * image,unsigned w,unsigned h)2320 void LodePNG_encode(LodePNG_Encoder* encoder, unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h)
2321 {
2322 LodePNG_InfoPng info;
2323 ucvector outv;
2324 unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
2325 size_t datasize = 0;
2326
2327 /*provide some proper output values if error will happen*/
2328 *out = 0;
2329 *outsize = 0;
2330 encoder->error = 0;
2331
2332 info = encoder->infoPng; /*UNSAFE copy to avoid having to cleanup! but we will only change primitive parameters, and not invoke the cleanup function nor touch the palette's buffer so we use it safely*/
2333 info.width = w;
2334 info.height = h;
2335
2336 if(encoder->settings.autoLeaveOutAlphaChannel && isFullyOpaque(image, w, h, &encoder->infoRaw.color))
2337 {
2338 /*go to a color type without alpha channel*/
2339 if(info.color.colorType == 6) info.color.colorType = 2;
2340 else if(info.color.colorType == 4) info.color.colorType = 0;
2341 }
2342
2343 if(encoder->settings.zlibsettings.windowSize > 32768) { encoder->error = 60; return; } /*error: windowsize larger than allowed*/
2344 if(encoder->settings.zlibsettings.btype > 2) { encoder->error = 61; return; } /*error: nonexistent btype*/
2345 if(encoder->infoPng.interlaceMethod > 1) { encoder->error = 71; return; } /*error: nonexistent interlace mode*/
2346 if((encoder->error = checkColorValidity(info.color.colorType, info.color.bitDepth))) return; /*error: nonexistent color type given*/
2347 if((encoder->error = checkColorValidity(encoder->infoRaw.color.colorType, encoder->infoRaw.color.bitDepth))) return; /*error: nonexistent color type given*/
2348
2349 if(!LodePNG_InfoColor_equal(&encoder->infoRaw.color, &info.color))
2350 {
2351 unsigned char* converted;
2352 size_t size = (w * h * LodePNG_InfoColor_getBpp(&info.color) + 7) / 8;
2353
2354 if((info.color.colorType != 6 && info.color.colorType != 2) || (info.color.bitDepth != 8)) { encoder->error = 59; return; } /*for the output image, only these types are supported*/
2355 converted = (unsigned char*)malloc(size);
2356 if(!converted && size) encoder->error = 9955; /*error: malloc failed*/
2357 if(!encoder->error) encoder->error = LodePNG_convert(converted, image, &info.color, &encoder->infoRaw.color, w, h);
2358 if(!encoder->error) preProcessScanlines(&data, &datasize, converted, &info);/*filter(data.data, converted.data, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
2359 free(converted);
2360 }
2361 else preProcessScanlines(&data, &datasize, image, &info);/*filter(data.data, image, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
2362
2363 ucvector_init(&outv);
2364 while(!encoder->error) /*not really a while loop, this is only used to break out if an error happens to avoid goto's to do the ucvector cleanup*/
2365 {
2366 /*write signature and chunks*/
2367 writeSignature(&outv);
2368 /*IHDR*/
2369 addChunk_IHDR(&outv, w, h, info.color.bitDepth, info.color.colorType, info.interlaceMethod);
2370 /*PLTE*/
2371 if(info.color.colorType == 3)
2372 {
2373 if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
2374 addChunk_PLTE(&outv, &info.color);
2375 }
2376 if(encoder->settings.force_palette && (info.color.colorType == 2 || info.color.colorType == 6))
2377 {
2378 if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
2379 addChunk_PLTE(&outv, &info.color);
2380 }
2381 /*tRNS*/
2382 if(info.color.colorType == 3 && !isPaletteFullyOpaque(info.color.palette, info.color.palettesize)) addChunk_tRNS(&outv, &info.color);
2383 if((info.color.colorType == 0 || info.color.colorType == 2) && info.color.key_defined) addChunk_tRNS(&outv, &info.color);
2384 /*IDAT (multiple IDAT chunks must be consecutive)*/
2385 encoder->error = addChunk_IDAT(&outv, data, datasize, &encoder->settings.zlibsettings);
2386 if(encoder->error) break;
2387 /*IEND*/
2388 addChunk_IEND(&outv);
2389
2390 break; /*this isn't really a while loop; no error happened so break out now!*/
2391 }
2392
2393 free(data);
2394 /*instead of cleaning the vector up, give it to the output*/
2395 *out = outv.data;
2396 *outsize = outv.size;
2397 }
2398
LodePNG_EncodeSettings_init(LodePNG_EncodeSettings * settings)2399 void LodePNG_EncodeSettings_init(LodePNG_EncodeSettings* settings)
2400 {
2401 LodeZlib_DeflateSettings_init(&settings->zlibsettings);
2402 settings->autoLeaveOutAlphaChannel = 1;
2403 settings->force_palette = 0;
2404 }
2405
LodePNG_Encoder_init(LodePNG_Encoder * encoder)2406 void LodePNG_Encoder_init(LodePNG_Encoder* encoder)
2407 {
2408 LodePNG_EncodeSettings_init(&encoder->settings);
2409 LodePNG_InfoPng_init(&encoder->infoPng);
2410 LodePNG_InfoRaw_init(&encoder->infoRaw);
2411 encoder->error = 1;
2412 }
2413
LodePNG_Encoder_cleanup(LodePNG_Encoder * encoder)2414 void LodePNG_Encoder_cleanup(LodePNG_Encoder* encoder)
2415 {
2416 LodePNG_InfoPng_cleanup(&encoder->infoPng);
2417 LodePNG_InfoRaw_cleanup(&encoder->infoRaw);
2418 }
2419
LodePNG_Encoder_copy(LodePNG_Encoder * dest,const LodePNG_Encoder * source)2420 void LodePNG_Encoder_copy(LodePNG_Encoder* dest, const LodePNG_Encoder* source)
2421 {
2422 LodePNG_Encoder_cleanup(dest);
2423 *dest = *source;
2424 LodePNG_InfoPng_init(&dest->infoPng);
2425 LodePNG_InfoRaw_init(&dest->infoRaw);
2426 dest->error = LodePNG_InfoPng_copy(&dest->infoPng, &source->infoPng); if(dest->error) return;
2427 dest->error = LodePNG_InfoRaw_copy(&dest->infoRaw, &source->infoRaw); if(dest->error) return;
2428 }
2429
2430 /* ////////////////////////////////////////////////////////////////////////// */
2431 /* / File IO / */
2432 /* ////////////////////////////////////////////////////////////////////////// */
2433
2434 /*write given buffer to the file, overwriting the file, it doesn't append to it.*/
LodePNG_saveFile(const unsigned char * buffer,size_t buffersize,const char * filename)2435 unsigned LodePNG_saveFile(const unsigned char* buffer, size_t buffersize, const char* filename)
2436 {
2437 FILE* file = fopen(filename,"wb");
2438 if(!file) return 79;
2439 fwrite((char*)buffer , 1 , buffersize, file);
2440 fclose(file);
2441 return 0;
2442 }
2443
2444