40 #define VERSION_STRING "20110823"
64 #define CERROR_BREAK(errorvar, code)\
71 #define ERROR_BREAK(code) CERROR_BREAK(error, code)
82 #ifdef LODEPNG_COMPILE_ZLIB
83 #ifdef LODEPNG_COMPILE_ENCODER
93 static unsigned vector_resize(
vector*
p,
size_t size)
97 size_t newsize = size * p->
typesize * 2;
112 static unsigned vector_resized(
vector*
p,
size_t size,
void dtor(
void*))
117 for(i = size; i < p->
size; i++)
122 return vector_resize(p, size);
125 static void vector_cleanup(
void* p)
132 static void vector_cleanupd(
vector* p,
void dtor(
void*))
134 vector_resized(p, 0, dtor);
138 static void vector_init(
vector* p,
unsigned typesize)
164 #ifdef LODEPNG_COMPILE_ZLIB
173 static void uivector_cleanup(
void* p)
181 static unsigned uivector_resize(
uivector* p,
size_t size)
183 if(size *
sizeof(
unsigned) > p->
allocsize)
185 size_t newsize = size *
sizeof(unsigned) * 2;
190 p->
data = (
unsigned*)data;
200 static unsigned uivector_resizev(
uivector* p,
size_t size,
unsigned value)
202 size_t oldsize = p->
size, i;
203 if(!uivector_resize(p, size))
return 0;
204 for(i = oldsize; i <
size; i++) p->
data[i] = value;
208 static void uivector_init(
uivector* p)
214 #ifdef LODEPNG_COMPILE_ENCODER
216 static unsigned uivector_push_back(
uivector* p,
unsigned c)
218 if(!uivector_resize(p, p->
size + 1))
return 0;
227 if(!uivector_resize(p, q->
size))
return 0;
253 static void ucvector_cleanup(
void* p)
261 static unsigned ucvector_resize(
ucvector* p,
size_t size)
263 if(size *
sizeof(
unsigned char) > p->
allocsize)
265 size_t newsize = size *
sizeof(
unsigned char) * 2;
270 p->
data = (
unsigned char*)data;
279 #ifdef LODEPNG_COMPILE_DECODER
280 #ifdef LODEPNG_COMPILE_PNG
282 static unsigned ucvector_resizev(
ucvector* p,
size_t size,
unsigned char value)
284 size_t oldsize = p->
size, i;
285 if(!ucvector_resize(p, size))
return 0;
286 for(i = oldsize; i <
size; i++) p->
data[i] = value;
292 static void ucvector_init(
ucvector* p)
298 #ifdef LODEPNG_COMPILE_ZLIB
301 static void ucvector_init_buffer(
ucvector* p,
unsigned char*
buffer,
size_t size)
308 #ifdef LODEPNG_COMPILE_ENCODER
310 static unsigned ucvector_push_back(
ucvector* p,
unsigned char c)
312 if(!ucvector_resize(p, p->
size + 1))
return 0;
321 #ifdef LODEPNG_COMPILE_PNG
322 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
324 static unsigned string_resize(
char** out,
size_t size)
336 static void string_init(
char** out)
339 string_resize(out, 0);
343 static void string_cleanup(
char** out)
349 static void string_set(
char** out,
const char* in)
351 size_t insize = strlen(in), i = 0;
352 if(string_resize(out, insize))
354 for(i = 0; i < insize; i++)
367 return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
371 static void LodePNG_set32bitInt(
unsigned char*
buffer,
unsigned value)
373 buffer[0] = (
unsigned char)((value >> 24) & 0xff);
374 buffer[1] = (
unsigned char)((value >> 16) & 0xff);
375 buffer[2] = (
unsigned char)((value >> 8) & 0xff);
376 buffer[3] = (
unsigned char)((value ) & 0xff);
379 #ifdef LODEPNG_COMPILE_ENCODER
380 static void LodePNG_add32bitInt(
ucvector* buffer,
unsigned value)
382 ucvector_resize(buffer, buffer->
size + 4);
383 LodePNG_set32bitInt(&buffer->
data[buffer->
size - 4], value);
391 #ifdef LODEPNG_COMPILE_DISK
402 file = fopen(filename,
"rb");
406 fseek(file , 0 , SEEK_END);
413 if(size && (*out)) (*outsize) = fread(*out, 1, (
size_t)size, file);
416 if(!(*out) && size)
return 9900;
421 unsigned LodePNG_saveFile(
const unsigned char* buffer,
size_t buffersize,
const char* filename)
424 file = fopen(filename,
"wb" );
426 fwrite((
char*)buffer , 1 , buffersize, file);
439 #ifdef LODEPNG_COMPILE_ZLIB
445 #ifdef LODEPNG_COMPILE_ENCODER
447 static void addBitToStream(
size_t* bitpointer,
ucvector* bitstream,
unsigned char bit)
450 if((*bitpointer) % 8 == 0) ucvector_push_back(bitstream, (
unsigned char)0);
452 (bitstream->
data[bitstream->
size - 1]) |= (bit << ((*bitpointer) & 0x7));
456 static void addBitsToStream(
size_t* bitpointer,
ucvector* bitstream,
unsigned value,
size_t nbits)
459 for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1));
462 static void addBitsToStreamReversed(
size_t* bitpointer,
ucvector* bitstream,
unsigned value,
size_t nbits)
465 for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1));
469 #ifdef LODEPNG_COMPILE_DECODER
471 #define READBIT(bitpointer, bitstream) ((bitstream[bitpointer >> 3] >> (bitpointer & 0x7)) & (unsigned char)1)
473 static unsigned char readBitFromStream(
size_t* bitpointer,
const unsigned char* bitstream)
475 unsigned char result = (
unsigned char)(
READBIT(*bitpointer, bitstream));
480 static unsigned readBitsFromStream(
size_t* bitpointer,
const unsigned char* bitstream,
size_t nbits)
482 unsigned result = 0, i;
483 for(i = 0; i < nbits; i++)
485 result += ((unsigned)
READBIT(*bitpointer, bitstream)) << i;
496 #define FIRST_LENGTH_CODE_INDEX 257
497 #define LAST_LENGTH_CODE_INDEX 285
499 #define NUM_DEFLATE_CODE_SYMBOLS 288
501 #define NUM_DISTANCE_SYMBOLS 32
503 #define NUM_CODE_LENGTH_CODES 19
506 static const unsigned LENGTHBASE[29]
507 = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
508 67, 83, 99, 115, 131, 163, 195, 227, 258};
511 static const unsigned LENGTHEXTRA[29]
512 = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
513 4, 4, 4, 4, 5, 5, 5, 5, 0};
516 static const unsigned DISTANCEBASE[30]
517 = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
518 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
521 static const unsigned DISTANCEEXTRA[30]
522 = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
523 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
528 = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
559 uivector_init(&tree->
tree2d);
560 uivector_init(&tree->
tree1d);
566 uivector_cleanup(&tree->
tree2d);
567 uivector_cleanup(&tree->
tree1d);
568 uivector_cleanup(&tree->
lengths);
572 static unsigned HuffmanTree_make2DTree(
HuffmanTree* tree)
574 unsigned nodefilled = 0;
575 unsigned treepos = 0;
578 if(!uivector_resize(&tree->
tree2d, tree->
numcodes * 2))
return 9901;
590 for(n = 0; n < tree->
numcodes * 2; n++)
600 if(treepos > tree->
numcodes - 2)
return 55;
601 if(tree->
tree2d.
data[2 * treepos + bit] == 32767)
615 treepos = nodefilled;
622 for(n = 0; n < tree->
numcodes * 2; n++)
635 static unsigned HuffmanTree_makeFromLengths2(
HuffmanTree* tree)
639 unsigned bits,
n, error = 0;
641 uivector_init(&blcount);
642 uivector_init(&nextcode);
644 || !uivector_resizev(&blcount, tree->
maxbitlen + 1, 0)
645 || !uivector_resizev(&nextcode, tree->
maxbitlen + 1, 0))
653 for(bits = 1; bits <= tree->maxbitlen; bits++)
655 nextcode.
data[bits] = (nextcode.
data[bits - 1] + blcount.
data[bits - 1]) << 1;
664 uivector_cleanup(&blcount);
665 uivector_cleanup(&nextcode);
667 if(!error)
return HuffmanTree_make2DTree(tree);
676 static unsigned HuffmanTree_makeFromLengths(
HuffmanTree* tree,
const unsigned* bitlen,
677 size_t numcodes,
unsigned maxbitlen)
680 if(!uivector_resize(&tree->
lengths, numcodes))
return 9903;
681 for(i = 0; i < numcodes; i++) tree->
lengths.
data[i] = bitlen[i];
682 tree->
numcodes = (
unsigned)numcodes;
684 return HuffmanTree_makeFromLengths2(tree);
687 #ifdef LODEPNG_COMPILE_ENCODER
700 static void Coin_init(
Coin* c)
706 static void Coin_cleanup(
void* c)
708 uivector_cleanup(&((
Coin*)c)->symbols);
711 static void Coin_copy(
Coin* c1,
const Coin* c2)
717 static void addCoins(
Coin* c1,
const Coin* c2)
728 static void Coin_sort(
Coin* data,
size_t amount)
731 unsigned char swapped = 0;
732 while((gap > 1) || swapped)
735 gap = (gap * 10) / 13;
736 if(gap == 9 || gap == 10) gap = 11;
739 for(i = 0; i < amount - gap; i++)
742 if(data[j].weight < data[i].weight)
745 uivector_swap(&data[i].symbols, &data[j].symbols);
752 static unsigned HuffmanTree_fillInCoins(
vector* coins,
const unsigned* frequencies,
unsigned numcodes,
size_t sum)
755 for(i = 0; i < numcodes; i++)
758 if(frequencies[i] == 0)
continue;
759 if(!vector_resize(coins, coins->
size + 1))
761 vector_cleanup(coins);
764 coin = (
Coin*)(vector_get(coins, coins->
size - 1));
766 coin->
weight = frequencies[i] / (float)sum;
767 uivector_push_back(&coin->
symbols, i);
774 static unsigned HuffmanTree_makeFromFrequencies(
HuffmanTree* tree,
const unsigned* frequencies,
775 size_t numcodes,
unsigned maxbitlen)
778 size_t sum = 0, numpresent = 0;
786 for(i = 0; i < numcodes; i++)
788 if(frequencies[i] > 0)
791 sum += frequencies[i];
795 if(numcodes == 0)
return 80;
796 tree->
numcodes = (unsigned)numcodes;
797 uivector_resize(&tree->
lengths, 0);
804 return HuffmanTree_makeFromLengths2(tree);
808 else if(numpresent == 1)
810 for(i = 0; i < numcodes; i++)
if(frequencies[i]) tree->
lengths.
data[i] = 1;
811 return HuffmanTree_makeFromLengths2(tree);
814 vector_init(&coins,
sizeof(
Coin));
815 vector_init(&prev_row,
sizeof(
Coin));
821 error = HuffmanTree_fillInCoins(&coins, frequencies, tree->
numcodes, sum);
824 for(j = 1; j <= maxbitlen && !error; j++)
826 vector_swap(&coins, &prev_row);
827 if(!vector_resized(&coins, 0, Coin_cleanup))
ERROR_BREAK(9906 );
828 for(i = 0; i + 1 < prev_row.
size; i += 2)
831 Coin_init((
Coin*)vector_get(&coins, coins.
size - 1));
832 Coin_copy((
Coin*)vector_get(&coins, coins.
size - 1), (
Coin*)vector_get(&prev_row, i));
834 addCoins((
Coin*)vector_get(&coins, coins.
size - 1), (
Coin*)vector_get(&prev_row, i + 1));
838 error = HuffmanTree_fillInCoins(&coins, frequencies, tree->
numcodes, sum);
846 vector_resized(&coins, numpresent - 1, Coin_cleanup);
849 for(i = 0; i < coins.
size; i++)
851 Coin* coin = (
Coin*)vector_get(&coins, i);
855 error = HuffmanTree_makeFromLengths2(tree);
858 vector_cleanupd(&coins, Coin_cleanup);
859 vector_cleanupd(&prev_row, Coin_cleanup);
864 static unsigned HuffmanTree_getCode(
const HuffmanTree* tree,
unsigned index)
869 static unsigned HuffmanTree_getLength(
const HuffmanTree* tree,
unsigned index)
876 static unsigned generateFixedLitLenTree(
HuffmanTree* tree)
878 unsigned i, error = 0;
880 uivector_init(&bitlen);
886 for(i = 0; i <= 143; i++) bitlen.
data[i] = 8;
887 for(i = 144; i <= 255; i++) bitlen.
data[i] = 9;
888 for(i = 256; i <= 279; i++) bitlen.
data[i] = 7;
889 for(i = 280; i <= 287; i++) bitlen.
data[i] = 8;
894 uivector_cleanup(&bitlen);
899 static unsigned generateFixedDistanceTree(
HuffmanTree* tree)
901 unsigned i, error = 0;
903 uivector_init(&bitlen);
910 error = HuffmanTree_makeFromLengths(tree, bitlen.
data, NUM_DISTANCE_SYMBOLS, 15);
912 uivector_cleanup(&bitlen);
916 #ifdef LODEPNG_COMPILE_DECODER
922 static unsigned huffmanDecodeSymbol(
const unsigned char* in,
size_t* bp,
925 unsigned treepos = 0, ct;
928 if(*bp > inbitlength)
return (
unsigned)(-1);
936 if(ct < codetree->numcodes)
return ct;
937 else treepos = ct - codetree->
numcodes;
939 if(treepos >= codetree->
numcodes)
return (
unsigned)(-1);
944 #ifdef LODEPNG_COMPILE_DECODER
955 generateFixedLitLenTree(tree_ll);
956 generateFixedDistanceTree(tree_d);
961 const unsigned char* in,
size_t* bp,
size_t inlength)
965 unsigned n, HLIT, HDIST, HCLEN, i;
966 size_t inbitlength = inlength * 8;
975 if((*bp) >> 3 >= inlength - 2)
return 49;
978 HLIT = readBitsFromStream(bp, in, 5) + 257;
980 HDIST = readBitsFromStream(bp, in, 5) + 1;
982 HCLEN = readBitsFromStream(bp, in, 4) + 4;
984 HuffmanTree_init(&tree_cl);
985 uivector_init(&bitlen_ll);
986 uivector_init(&bitlen_d);
987 uivector_init(&bitlen_cl);
997 if(i < HCLEN) bitlen_cl.
data[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3);
998 else bitlen_cl.
data[CLCL_ORDER[i]] = 0;
1001 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl.
data, bitlen_cl.
size, 7);
1011 while(i < HLIT + HDIST)
1013 unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength);
1016 if(i < HLIT) bitlen_ll.
data[i] = code;
1017 else bitlen_d.
data[i - HLIT] = code;
1022 unsigned replength = 3;
1027 replength += readBitsFromStream(bp, in, 2);
1029 if((i - 1) < HLIT) value = bitlen_ll.
data[i - 1];
1030 else value = bitlen_d.
data[i - HLIT - 1];
1032 for(n = 0; n < replength; n++)
1042 unsigned replength = 3;
1045 replength += readBitsFromStream(bp, in, 3);
1048 for(n = 0; n < replength; n++)
1052 if(i < HLIT) bitlen_ll.
data[i] = 0;
1053 else bitlen_d.
data[i - HLIT] = 0;
1059 unsigned replength = 11;
1062 replength += readBitsFromStream(bp, in, 7);
1065 for(n = 0; n < replength; n++)
1069 if(i < HLIT) bitlen_ll.
data[i] = 0;
1070 else bitlen_d.
data[i - HLIT] = 0;
1076 if(code == (
unsigned)(-1))
1080 error = (*bp) > inlength * 8 ? 10 : 11;
1091 error = HuffmanTree_makeFromLengths(tree_ll, &bitlen_ll.
data[0], bitlen_ll.
size, 15);
1093 error = HuffmanTree_makeFromLengths(tree_d, &bitlen_d.
data[0], bitlen_d.
size, 15);
1098 uivector_cleanup(&bitlen_cl);
1099 uivector_cleanup(&bitlen_ll);
1100 uivector_cleanup(&bitlen_d);
1101 HuffmanTree_cleanup(&tree_cl);
1107 static unsigned inflateHuffmanBlock(
ucvector* out,
const unsigned char* in,
size_t* bp,
1108 size_t* pos,
size_t inlength,
unsigned btype)
1113 size_t inbitlength = inlength * 8;
1115 HuffmanTree_init(&tree_ll);
1116 HuffmanTree_init(&tree_d);
1118 if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d);
1121 error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength);
1127 unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength);
1130 if((*pos) >= out->
size)
1133 if(!ucvector_resize(out, ((*pos) + 1) * 2))
ERROR_BREAK(9913 );
1135 out->
data[(*pos)] = (
unsigned char)(code_ll);
1140 unsigned code_d, distance;
1141 unsigned numextrabits_l, numextrabits_d;
1142 size_t start, forward, backward,
length;
1150 length += readBitsFromStream(bp, in, numextrabits_l);
1153 code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength);
1156 if(code_ll == (
unsigned)(-1))
1160 error = (*bp) > inlength * 8 ? 10 : 11;
1165 distance = DISTANCEBASE[code_d];
1168 numextrabits_d = DISTANCEEXTRA[code_d];
1171 distance += readBitsFromStream(bp, in, numextrabits_d);
1175 backward = start - distance;
1176 if((*pos) + length >= out->
size)
1179 if(!ucvector_resize(out, ((*pos) + length) * 2))
ERROR_BREAK(9914 );
1182 for(forward = 0; forward <
length; forward++)
1184 out->
data[(*pos)] = out->
data[backward];
1187 if(backward >= start) backward = start - distance;
1190 else if(code_ll == 256)
1198 error = (*bp) > inlength * 8 ? 10 : 11;
1203 HuffmanTree_cleanup(&tree_ll);
1204 HuffmanTree_cleanup(&tree_d);
1209 static unsigned inflateNoCompression(
ucvector* out,
const unsigned char* in,
size_t* bp,
size_t* pos,
size_t inlength)
1213 unsigned LEN, NLEN,
n, error = 0;
1214 while(((*bp) & 0x7) != 0) (*bp)++;
1218 if(p >= inlength - 4)
return 52;
1219 LEN = in[
p] + 256 * in[p + 1]; p += 2;
1220 NLEN = in[
p] + 256 * in[p + 1]; p += 2;
1223 if(LEN + NLEN != 65535)
return 21;
1225 if((*pos) + LEN >= out->
size)
1227 if(!ucvector_resize(out, (*pos) + LEN))
return 9915;
1231 if(p + LEN > inlength)
return 23;
1232 for(n = 0; n < LEN; n++) out->
data[(*pos)++] = in[p++];
1240 static unsigned LodePNG_inflate(
ucvector* out,
const unsigned char* in,
size_t insize,
size_t inpos)
1244 unsigned BFINAL = 0;
1252 if(bp + 2 >= insize * 8)
return 52;
1253 BFINAL = readBitFromStream(&bp, &in[inpos]);
1254 BTYPE = 1 * readBitFromStream(&bp, &in[inpos]);
1255 BTYPE += 2 * readBitFromStream(&bp, &in[inpos]);
1257 if(BTYPE == 3)
return 20;
1258 else if(BTYPE == 0) error = inflateNoCompression(out, &in[inpos], &bp, &pos, insize);
1259 else error = inflateHuffmanBlock(out, &in[inpos], &bp, &pos, insize, BTYPE);
1261 if(error)
return error;
1265 if(!ucvector_resize(out, pos)) error = 9916;
1272 #ifdef LODEPNG_COMPILE_ENCODER
1278 static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258;
1281 static void addHuffmanSymbol(
size_t* bp,
ucvector* compressed,
unsigned code,
unsigned bitlen)
1283 addBitsToStreamReversed(bp, compressed, code, bitlen);
1288 static size_t searchCodeIndex(
const unsigned* array,
size_t array_size,
size_t value)
1296 size_t right = array_size - 1;
1297 while(left <= right)
1299 size_t mid = (left + right) / 2;
1300 if(array[mid] <= value) left = mid + 1;
1301 else if(array[mid - 1] > value) right = mid - 1;
1302 else return mid - 1;
1304 return array_size - 1;
1307 static void addLengthDistance(
uivector* values,
size_t length,
size_t distance)
1315 unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length);
1316 unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]);
1317 unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance);
1318 unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]);
1321 uivector_push_back(values, extra_length);
1322 uivector_push_back(values, dist_code);
1323 uivector_push_back(values, extra_distance);
1328 static unsigned encodeLZ77_brute(
uivector* out,
const unsigned char* in,
size_t insize,
unsigned windowSize)
1331 for(pos = 0; pos < insize; pos++)
1333 size_t length = 0,
offset = 0;
1334 size_t max_offset = pos < windowSize ? pos : windowSize;
1335 size_t current_offset;
1338 for(current_offset = 1; current_offset < max_offset; current_offset++)
1340 size_t backpos = pos - current_offset;
1341 if(in[backpos] == in[pos])
1344 size_t current_length = 1;
1345 size_t backtest = backpos + 1;
1346 size_t foretest = pos + 1;
1348 while(foretest < insize && in[backtest] == in[foretest] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH)
1353 backpos -= current_offset;
1359 if(current_length > length)
1361 length = current_length;
1364 if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH)
break;
1372 uivector_push_back(out, in[pos]);
1376 addLengthDistance(out, length,
offset);
1377 pos += (length - 1);
1384 static const unsigned HASH_NUM_VALUES = 2048;
1385 static const unsigned HASH_NUM_CHARACTERS = 3;
1386 static const unsigned HASH_SHIFT = 2;
1401 static unsigned getHash(
const unsigned char* data,
size_t size,
size_t pos,
size_t num)
1403 unsigned result = 0;
1405 if(pos >= size)
return 0;
1407 if(pos + amount >= size) amount = size - pos;
1408 for(i = 0; i < amount; i++) result ^= (data[pos + i] << (i * HASH_SHIFT));
1409 return result % HASH_NUM_VALUES;
1412 static unsigned countInitialZeros(
const unsigned char* data,
size_t size,
size_t pos)
1414 size_t max_count = MAX_SUPPORTED_DEFLATE_LENGTH;
1416 if(max_count > size - pos) max_count = size - pos;
1417 for(i = 0; i < max_count; i++)
1419 if(data[pos + i] != 0)
1430 static unsigned push_circular(
uivector*
v,
unsigned* pos,
unsigned value,
size_t maxsize,
unsigned updatepos)
1432 if(v->
size < maxsize)
1434 if(!uivector_push_back(v, value))
return 0;
1435 if(updatepos) (*pos)++;
1442 if((*pos) > maxsize) (*pos) = 1;
1458 static unsigned encodeLZ77(
uivector* out,
const unsigned char* in,
size_t insize,
unsigned windowSize)
1470 unsigned pos, i, error = 0;
1471 unsigned hash_num_characters = HASH_NUM_CHARACTERS;
1473 vector_init(&table,
sizeof(
uivector));
1474 if(!vector_resize(&table, HASH_NUM_VALUES))
return 9917;
1475 for(i = 0; i < HASH_NUM_VALUES; i++)
1482 uivector_init(&tablepos1);
1483 uivector_init(&tablepos2);
1484 uivector_init(&initialZerosTable);
1486 if(!uivector_resizev(&tablepos1, HASH_NUM_VALUES, 0)) error = 9918;
1487 if(!uivector_resizev(&tablepos2, HASH_NUM_VALUES, 0)) error = 9919;
1491 unsigned offset, max_offset;
1492 unsigned length, tablepos;
1493 unsigned hash, initialZeros = 0;
1494 unsigned backpos, current_offset, t1, t2, t11, current_length;
1495 const unsigned char *lastptr, *foreptr, *backptr;
1497 unsigned hashWindow = windowSize;
1498 unsigned numones = 0;
1500 for(pos = 0; pos < insize; pos++)
1502 length = 0, offset = 0;
1503 max_offset = pos < windowSize ? pos : windowSize;
1507 hash = getHash(in, insize, pos, hash_num_characters);
1508 v = (
uivector*)vector_get(&table, hash);
1509 if(!push_circular(v, &tablepos2.
data[hash], pos, hashWindow, 1))
ERROR_BREAK(9920 );
1513 initialZeros = countInitialZeros(in, insize, pos);
1514 if(!push_circular(&initialZerosTable, &tablepos2.
data[hash], initialZeros, hashWindow, 0))
1518 while(v->
data[tablepos1.
data[hash]] < pos - max_offset)
1523 if(tablepos1.
data[hash] >= hashWindow) tablepos1.
data[
hash] = 0;
1528 if(tablepos2.
data[hash] == 0) t2 = hashWindow - 1;
1530 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
1532 t11 = t1 == 0 ? hashWindow - 1 : t1 - 1;
1533 for(tablepos = t2 == 0 ? hashWindow - 1 : t2 - 1;
1534 tablepos != t2 && tablepos != t11 && tablepos < v->
size;
1535 tablepos = tablepos == 0 ? hashWindow - 1 : tablepos - 1)
1537 backpos = v->
data[tablepos];
1538 current_offset = pos - backpos;
1541 backptr = &in[backpos];
1545 unsigned skip = initialZerosTable.
data[tablepos];
1546 if(skip > initialZeros) skip = initialZeros;
1547 if(skip > insize - pos) skip = insize - pos;
1551 while(foreptr != lastptr && *backptr == *foreptr)
1556 current_length = (unsigned)(foreptr - &in[pos]);
1557 if(current_length > length)
1559 length = current_length;
1560 offset = current_offset;
1562 if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH)
break;
1569 if(!uivector_push_back(out, in[pos]))
ERROR_BREAK(9921 );
1573 unsigned j, local_hash;
1574 addLengthDistance(out, length, offset);
1575 for(j = 0; j < length - 1; j++)
1579 local_hash = getHash(in, insize, pos, hash_num_characters);
1580 t2p = &tablepos2.
data[local_hash];
1581 v = (
uivector*)vector_get(&table, local_hash);
1582 if(!push_circular(v, t2p, pos, hashWindow, 1))
ERROR_BREAK(9920 );
1586 initialZeros = countInitialZeros(in, insize, pos);
1587 if(!push_circular(&initialZerosTable, t2p, initialZeros, hashWindow, 0))
1590 if(local_hash == 1 && hash_num_characters == 3)
1602 if(numones > 8192 && numones > pos / 16) hash_num_characters = 6;
1611 for(i = 0; i < table.
size; i++)
1614 uivector_cleanup(v);
1616 vector_cleanup(&table);
1617 uivector_cleanup(&tablepos1);
1618 uivector_cleanup(&tablepos2);
1619 uivector_cleanup(&initialZerosTable);
1625 static unsigned deflateNoCompression(
ucvector* out,
const unsigned char* data,
size_t datasize)
1630 size_t i, j, numdeflateblocks = datasize / 65536 + 1;
1631 unsigned datapos = 0;
1632 for(i = 0; i < numdeflateblocks; i++)
1634 unsigned BFINAL, BTYPE, LEN, NLEN;
1635 unsigned char firstbyte;
1637 BFINAL = (i == numdeflateblocks - 1);
1640 firstbyte = (
unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1));
1641 ucvector_push_back(out, firstbyte);
1644 if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos;
1647 ucvector_push_back(out, (
unsigned char)(LEN % 256));
1648 ucvector_push_back(out, (
unsigned char)(LEN / 256));
1649 ucvector_push_back(out, (
unsigned char)(NLEN % 256));
1650 ucvector_push_back(out, (
unsigned char)(NLEN / 256));
1653 for(j = 0; j < 65535 && datapos < datasize; j++)
1655 ucvector_push_back(out, data[datapos++]);
1667 static void writeLZ77data(
size_t* bp,
ucvector* out,
const uivector* lz77_encoded,
1671 for(i = 0; i < lz77_encoded->
size; i++)
1673 unsigned val = lz77_encoded->
data[i];
1674 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val));
1678 unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
1679 unsigned length_extra_bits = lz77_encoded->
data[++i];
1681 unsigned distance_code = lz77_encoded->
data[++i];
1683 unsigned distance_index = distance_code;
1684 unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
1685 unsigned distance_extra_bits = lz77_encoded->
data[++i];
1687 addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits);
1688 addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code),
1689 HuffmanTree_getLength(tree_d, distance_code));
1690 addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits);
1696 static unsigned deflateDynamic(
ucvector* out,
const unsigned char* data,
size_t datasize,
1734 unsigned BFINAL = 1;
1735 size_t numcodes_ll, numcodes_d, i;
1737 unsigned HLIT, HDIST, HCLEN;
1739 uivector_init(&lz77_encoded);
1740 HuffmanTree_init(&tree_ll);
1741 HuffmanTree_init(&tree_d);
1742 HuffmanTree_init(&tree_cl);
1743 uivector_init(&frequencies_ll);
1744 uivector_init(&frequencies_d);
1745 uivector_init(&frequencies_cl);
1746 uivector_init(&bitlen_lld);
1747 uivector_init(&bitlen_lld_e);
1748 uivector_init(&bitlen_cl);
1756 error = encodeLZ77(&lz77_encoded, data, datasize, settings->
windowSize);
1761 if(!uivector_resize(&lz77_encoded, datasize))
ERROR_BREAK(9923 );
1762 for(i = 0; i < datasize; i++) lz77_encoded.
data[i] = data[i];
1765 if(!uivector_resizev(&frequencies_ll, 286, 0))
ERROR_BREAK(9924 );
1766 if(!uivector_resizev(&frequencies_d, 30, 0))
ERROR_BREAK(9925 );
1769 for(i = 0; i < lz77_encoded.
size; i++)
1771 unsigned symbol = lz77_encoded.
data[i];
1772 frequencies_ll.
data[symbol]++;
1775 unsigned dist = lz77_encoded.
data[i + 2];
1776 frequencies_d.
data[dist]++;
1780 frequencies_ll.
data[256] = 1;
1783 error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.
data, frequencies_ll.
size, 15);
1785 error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.
data, frequencies_d.
size, 15);
1788 numcodes_ll = tree_ll.
numcodes;
if(numcodes_ll > 286) numcodes_ll = 286;
1789 numcodes_d = tree_d.
numcodes;
if(numcodes_d > 30) numcodes_d = 30;
1791 for(i = 0; i < numcodes_ll; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (
unsigned)i));
1792 for(i = 0; i < numcodes_d; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (
unsigned)i));
1796 for(i = 0; i < (unsigned)bitlen_lld.
size; i++)
1799 while(i + j + 1 < (
unsigned)bitlen_lld.
size && bitlen_lld.
data[i + j + 1] == bitlen_lld.
data[i]) j++;
1801 if(bitlen_lld.
data[i] == 0 && j >= 2)
1806 uivector_push_back(&bitlen_lld_e, 17);
1807 uivector_push_back(&bitlen_lld_e, j - 3);
1811 if(j > 138) j = 138;
1812 uivector_push_back(&bitlen_lld_e, 18);
1813 uivector_push_back(&bitlen_lld_e, j - 11);
1820 unsigned num = j / 6, rest = j % 6;
1821 uivector_push_back(&bitlen_lld_e, bitlen_lld.
data[i]);
1822 for(k = 0; k < num; k++)
1824 uivector_push_back(&bitlen_lld_e, 16);
1825 uivector_push_back(&bitlen_lld_e, 6 - 3);
1829 uivector_push_back(&bitlen_lld_e, 16);
1830 uivector_push_back(&bitlen_lld_e, rest - 3);
1837 uivector_push_back(&bitlen_lld_e, bitlen_lld.
data[i]);
1843 if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0))
ERROR_BREAK(9926 );
1844 for(i = 0; i < bitlen_lld_e.
size; i++)
1846 frequencies_cl.
data[bitlen_lld_e.
data[i]]++;
1849 if(bitlen_lld_e.
data[i] >= 16) i++;
1852 error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.
data, frequencies_cl.
size, 7);
1855 if(!uivector_resize(&bitlen_cl, NUM_CODE_LENGTH_CODES))
ERROR_BREAK(9927 );
1859 bitlen_cl.
data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]);
1861 while(bitlen_cl.
data[bitlen_cl.
size - 1] == 0 && bitlen_cl.
size > 4)
1864 if(!uivector_resize(&bitlen_cl, bitlen_cl.
size - 1))
ERROR_BREAK(9928 );
1883 addBitToStream(&bp, out, BFINAL);
1884 addBitToStream(&bp, out, 0);
1885 addBitToStream(&bp, out, 1);
1888 HLIT = (unsigned)(numcodes_ll - 257);
1889 HDIST = (unsigned)(numcodes_d - 1);
1890 HCLEN = (unsigned)bitlen_cl.
size - 4;
1891 addBitsToStream(&bp, out, HLIT, 5);
1892 addBitsToStream(&bp, out, HDIST, 5);
1893 addBitsToStream(&bp, out, HCLEN, 4);
1896 for(i = 0; i < HCLEN + 4; i++) addBitsToStream(&bp, out, bitlen_cl.
data[i], 3);
1899 for(i = 0; i < bitlen_lld_e.
size; i++)
1901 addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.
data[i]),
1902 HuffmanTree_getLength(&tree_cl, bitlen_lld_e.
data[i]));
1904 if(bitlen_lld_e.
data[i] == 16) addBitsToStream(&bp, out, bitlen_lld_e.
data[++i], 2);
1905 else if(bitlen_lld_e.
data[i] == 17) addBitsToStream(&bp, out, bitlen_lld_e.
data[++i], 3);
1906 else if(bitlen_lld_e.
data[i] == 18) addBitsToStream(&bp, out, bitlen_lld_e.
data[++i], 7);
1910 writeLZ77data(&bp, out, &lz77_encoded, &tree_ll, &tree_d);
1912 if(HuffmanTree_getLength(&tree_ll, 256) == 0)
ERROR_BREAK(64);
1915 addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
1921 uivector_cleanup(&lz77_encoded);
1922 HuffmanTree_cleanup(&tree_ll);
1923 HuffmanTree_cleanup(&tree_d);
1924 HuffmanTree_cleanup(&tree_cl);
1925 uivector_cleanup(&frequencies_ll);
1926 uivector_cleanup(&frequencies_d);
1927 uivector_cleanup(&frequencies_cl);
1928 uivector_cleanup(&bitlen_lld_e);
1929 uivector_cleanup(&bitlen_lld);
1930 uivector_cleanup(&bitlen_cl);
1935 static unsigned deflateFixed(
ucvector* out,
const unsigned char* data,
1941 unsigned BFINAL = 1;
1945 HuffmanTree_init(&tree_ll);
1946 HuffmanTree_init(&tree_d);
1948 generateFixedLitLenTree(&tree_ll);
1949 generateFixedDistanceTree(&tree_d);
1951 addBitToStream(&bp, out, BFINAL);
1952 addBitToStream(&bp, out, 1);
1953 addBitToStream(&bp, out, 0);
1958 uivector_init(&lz77_encoded);
1959 error = encodeLZ77(&lz77_encoded, data, datasize, settings->
windowSize);
1960 if(!error) writeLZ77data(&bp, out, &lz77_encoded, &tree_ll, &tree_d);
1961 uivector_cleanup(&lz77_encoded);
1965 for(i = 0; i < datasize; i++)
1967 addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i]));
1971 if(!error) addHuffmanSymbol(&bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));
1974 HuffmanTree_cleanup(&tree_ll);
1975 HuffmanTree_cleanup(&tree_d);
1980 static unsigned LodePNG_deflate(
ucvector* out,
const unsigned char* data,
size_t datasize,
1984 if(settings->
btype == 0) error = deflateNoCompression(out, data, datasize);
1985 else if(settings->
btype == 1) error = deflateFixed(out, data, datasize, settings);
1986 else if(settings->
btype == 2) error = deflateDynamic(out, data, datasize, settings);
1997 static unsigned update_adler32(
unsigned adler,
const unsigned char* data,
unsigned len)
1999 unsigned s1 = adler & 0xffff;
2000 unsigned s2 = (adler >> 16) & 0xffff;
2005 unsigned amount = len > 5550 ? 5550 :
len;
2009 s1 = (s1 + *data++);
2017 return (s2 << 16) | s1;
2021 static unsigned adler32(
const unsigned char* data,
unsigned len)
2023 return update_adler32(1
L, data, len);
2030 #ifdef LODEPNG_COMPILE_DECODER
2036 unsigned CM, CINFO, FDICT;
2039 if(insize < 2)
return 53;
2041 if((in[0] * 256 + in[1]) % 31 != 0)
2048 CINFO = (in[0] >> 4) & 15;
2050 FDICT = (in[1] >> 5) & 1;
2053 if(CM != 8 || CINFO > 7)
2066 ucvector_init_buffer(&outv, *out, *outsize);
2067 error = LodePNG_inflate(&outv, in, insize, 2);
2069 *outsize = outv.
size;
2070 if(error)
return error;
2075 unsigned checksum = adler32(outv.
data, (
unsigned)outv.
size);
2076 if(checksum != ADLER32)
return 58;
2084 #ifdef LODEPNG_COMPILE_ENCODER
2098 unsigned FLEVEL = 0;
2100 unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
2101 unsigned FCHECK = 31 - CMFFLG % 31;
2105 ucvector_init_buffer(&outv, *out, *outsize);
2107 ucvector_push_back(&outv, (
unsigned char)(CMFFLG / 256));
2108 ucvector_push_back(&outv, (
unsigned char)(CMFFLG % 256));
2110 ucvector_init(&deflatedata);
2111 error = LodePNG_deflate(&deflatedata, in, insize, settings);
2115 ADLER32 = adler32(in, (
unsigned)insize);
2116 for(i = 0; i < deflatedata.
size; i++) ucvector_push_back(&outv, deflatedata.
data[i]);
2117 ucvector_cleanup(&deflatedata);
2118 LodePNG_add32bitInt(&outv, ADLER32);
2122 *outsize = outv.
size;
2141 #ifdef LODEPNG_COMPILE_DECODER
2148 #ifdef LODEPNG_COMPILE_ENCODER
2149 static unsigned LodePNG_zlib_compress(
unsigned char** out,
size_t* outsize,
const unsigned char* in,
2160 #ifdef LODEPNG_COMPILE_ENCODER
2163 #define DEFAULT_WINDOWSIZE 2048
2168 settings->
btype = 2;
2177 #ifdef LODEPNG_COMPILE_DECODER
2194 #ifdef LODEPNG_COMPILE_PNG
2200 static unsigned Crc32_crc_table_computed = 0;
2201 static unsigned Crc32_crc_table[256];
2204 static void Crc32_make_crc_table(
void)
2207 for(n = 0; n < 256; n++)
2210 for(k = 0; k < 8; k++)
2212 if(c & 1) c = 0xedb88320
L ^ (c >> 1);
2215 Crc32_crc_table[
n] = c;
2217 Crc32_crc_table_computed = 1;
2223 static unsigned Crc32_update_crc(
const unsigned char* buf,
unsigned crc,
size_t len)
2228 if(!Crc32_crc_table_computed) Crc32_make_crc_table();
2229 for(n = 0; n <
len; n++)
2231 c = Crc32_crc_table[(c ^ buf[
n]) & 0xff] ^ (c >> 8);
2237 static unsigned Crc32_crc(
const unsigned char* buf,
size_t len)
2239 return Crc32_update_crc(buf, 0xffffffffL, len) ^ 0xffffffff
L;
2246 static unsigned char readBitFromReversedStream(
size_t* bitpointer,
const unsigned char* bitstream)
2248 unsigned char result = (
unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
2253 static unsigned readBitsFromReversedStream(
size_t* bitpointer,
const unsigned char* bitstream,
size_t nbits)
2255 unsigned result = 0;
2257 for(i = nbits - 1; i < nbits; i--)
2259 result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i;
2264 #ifdef LODEPNG_COMPILE_DECODER
2265 static void setBitOfReversedStream0(
size_t* bitpointer,
unsigned char* bitstream,
unsigned char bit)
2271 bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7)));
2277 static void setBitOfReversedStream(
size_t* bitpointer,
unsigned char* bitstream,
unsigned char bit)
2280 if(bit == 0) bitstream[(*bitpointer) >> 3] &= (
unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
2281 else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
2297 for(i = 0; i < 4; i++) type[i] = chunk[4 + i];
2303 if(strlen(type) != 4)
return 0;
2304 return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
2309 return((chunk[4] & 32) == 0);
2314 return((chunk[6] & 32) != 0);
2319 return((chunk[7] & 32) != 0);
2337 unsigned checksum = Crc32_crc(&chunk[4], length + 4);
2338 if(CRC != checksum)
return 1;
2345 unsigned CRC = Crc32_crc(&chunk[4], length + 4);
2346 LodePNG_set32bitInt(chunk + 8 + length, CRC);
2352 return &chunk[total_chunk_length];
2358 return &chunk[total_chunk_length];
2365 unsigned char *chunk_start, *new_buffer;
2366 size_t new_length = (*outlength) + total_chunk_length;
2367 if(new_length < total_chunk_length || new_length < (*outlength))
return 77;
2369 new_buffer = (
unsigned char*)
realloc(*out, new_length);
2370 if(!new_buffer)
return 9929;
2371 (*out) = new_buffer;
2372 (*outlength) = new_length;
2373 chunk_start = &(*out)[new_length - total_chunk_length];
2375 for(i = 0; i < total_chunk_length; i++) chunk_start[i] = chunk[i];
2381 const char*
type,
const unsigned char* data)
2384 unsigned char *chunk, *new_buffer;
2385 size_t new_length = (*outlength) + length + 12;
2386 if(new_length < length + 12 || new_length < (*outlength))
return 77;
2387 new_buffer = (
unsigned char*)
realloc(*out, new_length);
2388 if(!new_buffer)
return 9930;
2389 (*out) = new_buffer;
2390 (*outlength) = new_length;
2391 chunk = &(*out)[(*outlength) - length - 12];
2394 LodePNG_set32bitInt(chunk, (
unsigned)length);
2403 for(i = 0; i <
length; i++) chunk[8 + i] = data[i];
2416 static unsigned checkColorValidity(
unsigned colorType,
unsigned bd)
2420 case 0:
if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16))
return 37;
break;
2421 case 2:
if(!( bd == 8 || bd == 16))
return 37;
break;
2422 case 3:
if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 ))
return 37;
break;
2423 case 4:
if(!( bd == 8 || bd == 16))
return 37;
break;
2424 case 6:
if(!( bd == 8 || bd == 16))
return 37;
break;
2430 static unsigned getNumColorChannels(
unsigned colorType)
2443 static unsigned getBpp(
unsigned colorType,
unsigned bitDepth)
2446 return getNumColorChannels(colorType) * bitDepth;
2473 unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a)
2475 unsigned char*
data;
2483 if(!data)
return 9931;
2502 return getNumColorChannels(info->
colorType);
2525 if(info->
palette[i * 4 + 3] < 255)
return 1;
2543 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2547 for(i = 0; i < 3; i++) chunks->
data[i] = 0;
2548 for(i = 0; i < 3; i++) chunks->
datasize[i] = 0;
2554 for(i = 0; i < 3; i++)
free(chunks->
data[i]);
2563 for(i = 0; i < 3; i++)
2576 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2595 for(i = 0; i < source->
num; i++)
2598 if(error)
return error;
2606 for(i = 0; i < text->
num; i++)
2608 string_cleanup(&text->
keys[i]);
2609 string_cleanup(&text->
strings[i]);
2617 char** new_keys = (
char**)(
realloc(text->
keys,
sizeof(
char*) * (text->
num + 1)));
2618 char** new_strings = (
char**)(
realloc(text->
strings,
sizeof(
char*) * (text->
num + 1)));
2619 if(!new_keys || !new_strings)
2627 text->
keys = new_keys;
2630 string_init(&text->
keys[text->
num - 1]);
2631 string_set(&text->
keys[text->
num - 1], key);
2634 string_set(&text->
strings[text->
num - 1], str);
2663 for(i = 0; i < source->
num; i++)
2667 if(error)
return error;
2675 for(i = 0; i < text->
num; i++)
2677 string_cleanup(&text->
keys[i]);
2678 string_cleanup(&text->
langtags[i]);
2680 string_cleanup(&text->
strings[i]);
2689 const char* transkey,
const char* str)
2691 char** new_keys = (
char**)(
realloc(text->
keys,
sizeof(
char*) * (text->
num + 1)));
2692 char** new_langtags = (
char**)(
realloc(text->
langtags,
sizeof(
char*) * (text->
num + 1)));
2693 char** new_transkeys = (
char**)(
realloc(text->
transkeys,
sizeof(
char*) * (text->
num + 1)));
2694 char** new_strings = (
char**)(
realloc(text->
strings,
sizeof(
char*) * (text->
num + 1)));
2695 if(!new_keys || !new_langtags || !new_transkeys || !new_strings)
2699 free(new_transkeys);
2705 text->
keys = new_keys;
2710 string_init(&text->
keys[text->
num - 1]);
2711 string_set(&text->
keys[text->
num - 1], key);
2714 string_set(&text->
langtags[text->
num - 1], langtag);
2720 string_set(&text->
strings[text->
num - 1], str);
2733 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2743 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2751 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2755 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2768 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2773 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2821 static unsigned LodePNG_convert_rgb_a_8(
unsigned char* out,
const unsigned char* in,
2824 size_t i, c, bp = 0;
2831 for(i = 0; i < numpixels; i++)
2833 out[bytes * i + 0] = out[bytes * i + 1] = out[bytes * i + 2] = in[i];
2834 if(alpha) out[bytes * i + 3] = infoIn->
key_defined && in[i] == infoIn->
key_r ? 0 : 255;
2838 for(i = 0; i < numpixels; i++)
2840 for(c = 0; c < 3; c++) out[bytes * i + c] = in[3 * i + c];
2844 && in[3 * i + 1] == infoIn->
key_g && in[3 * i + 2] == infoIn->
key_b)
2845 out[bytes * i + 3] = 0;
2846 else out[bytes * i + 3] = 255;
2851 for(i = 0; i < numpixels; i++)
2854 for(c = 0; c <
bytes; c++) out[bytes * i + c] = infoIn->
palette[4 * in[i] + c];
2858 for(i = 0; i < numpixels; i++)
2860 out[bytes * i + 0] = out[bytes * i + 1] = out[bytes * i + 2] = in[2 * i + 0];
2861 if(alpha) out[bytes * i + 3] = in[2 * i + 1];
2865 for(i = 0; i < numpixels; i++)
2867 for(c = 0; c <
bytes; c++) out[bytes * i + c] = in[4 * i + c];
2878 for(i = 0; i < numpixels; i++)
2880 out[bytes * i + 0] = out[bytes * i + 1] = out[bytes * i + 2] = in[2 * i];
2881 if(alpha) out[bytes * i + 3] = infoIn->
key_defined && 256U * in[i] + in[i + 1] == infoIn->
key_r ? 0 : 255;
2885 for(i = 0; i < numpixels; i++)
2887 if(alpha) out[bytes * i + 3] = 255;
2888 for(c = 0; c < 3; c++) out[bytes * i + c] = in[6 * i + 2 * c];
2891 if(infoIn->
key_defined && 256U * in[6 * i + 0] + in[6 * i + 1] == infoIn->
key_r
2892 && 256U * in[6 * i + 2] + in[6 * i + 3] == infoIn->
key_g
2893 && 256U * in[6 * i + 4] + in[6 * i + 5] == infoIn->
key_b)
2894 out[bytes * i + 3] = 0;
2895 else out[bytes * i + 3] = 255;
2900 for(i = 0; i < numpixels; i++)
2902 out[bytes * i + 0] = out[bytes * i + 1] = out[bytes * i + 2] = in[4 * i];
2903 if(alpha) out[bytes * i + 3] = in[4 * i + 2];
2907 for(i = 0; i < numpixels; i++)
2909 for(c = 0; c <
bytes; c++) out[bytes * i + c] = in[8 * i + 2 * c];
2920 for(i = 0; i < numpixels; i++)
2922 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
2923 unsigned highest = ((1U << infoIn->
bitDepth) - 1U);
2925 out[bytes * i + 0] = out[bytes * i + 1] = out[bytes * i + 2]
2926 = (
unsigned char)((value * 255) / highest);
2927 if(alpha) out[bytes * i + 3] = infoIn->
key_defined && value == infoIn->
key_r ? 0 : 255;
2931 for(i = 0; i < numpixels; i++)
2933 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
2935 for(c = 0; c <
bytes; c++) out[bytes * i + c] = infoIn->
palette[4 * value + c];
2945 static unsigned LodePNG_convert_grey_8(
unsigned char* out,
const unsigned char* in,
2955 for(i = 0; i < numpixels; i++)
2957 out[bytes * i] = in[i];
2958 if(alpha) out[bytes * i + 1] = infoIn->
key_defined && in[i] == infoIn->
key_r ? 0 : 255;
2962 for(i = 0; i < numpixels; i++)
2964 out[bytes * i + 0] = in[2 * i + 0];
2965 if(alpha) out[bytes * i + 1] = in[2 * i + 1];
2976 for(i = 0; i < numpixels; i++)
2978 if(alpha) out[bytes * i + 1] = 255;
2979 out[bytes * i] = in[2 * i];
2980 if(alpha && infoIn->
key_defined && 256U * in[i] + in[i + 1] == infoIn->
key_r)
2982 out[bytes * i + 1] = 0;
2987 for(i = 0; i < numpixels; i++)
2989 out[bytes * i] = in[4 * i];
2990 if(alpha) out[bytes * i + 1] = in[4 * i + 2];
2999 for(i = 0; i < numpixels; i++)
3001 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
3002 unsigned highest = ((1U << infoIn->
bitDepth) - 1U);
3003 out[bytes * i] = (
unsigned char)((value * 255) / highest);
3007 else out[bytes * i + 1] = 255;
3015 static unsigned LodePNG_convert_rgb_a_16(
unsigned char* out,
const unsigned char* in,
3018 size_t i, c, bp = 0;
3025 for(i = 0; i < numpixels; i++)
3027 out[bytes * i + 0] = out[bytes * i + 2] = out[bytes * i + 4] = in[i];
3028 out[bytes * i + 1] = out[bytes * i + 3] = out[bytes * i + 5] = in[i];
3031 if(alpha && infoIn->
key_defined && in[i] == infoIn->
key_r) out[bytes * i + 6] = out[bytes * i + 7] = 0;
3032 else out[bytes * i + 6] = out[bytes * i + 7] = 255;
3037 for(i = 0; i < numpixels; i++)
3039 out[bytes * i + 0] = out[bytes * i + 1] = in[3 * i + 0];
3040 out[bytes * i + 2] = out[bytes * i + 3] = in[3 * i + 1];
3041 out[bytes * i + 4] = out[bytes * i + 5] = in[3 * i + 2];
3045 && in[3 * i + 1] == infoIn->
key_g && in[3 * i + 2] == infoIn->
key_b)
3046 out[bytes * i + 6] = out[bytes * i + 7] = 0;
3047 else out[bytes * i + 6] = out[bytes * i + 7] = 255;
3052 for(i = 0; i < numpixels; i++)
3055 for(c = 0; c <
bytes; c++)
3057 out[bytes * i + 2 * c] = out[bytes * i + 2 * c + 1] = infoIn->
palette[4 * in[i] + c];
3062 for(i = 0; i < numpixels; i++)
3064 out[bytes * i + 0] = out[bytes * i + 2] = out[bytes * i + 4] = in[i];
3065 out[bytes * i + 1] = out[bytes * i + 3] = out[bytes * i + 5] = in[i];
3068 out[bytes * i + 6] = out[bytes * i + 7] = in[2 * i + 1];
3073 for(i = 0; i < numpixels; i++)
3075 for(c = 0; c < 3; c++)
3077 out[bytes * i + 2 * c] = out[bytes * i + 2 * c + 1] = in[4 * i + c];
3081 out[bytes * i + 6] = out[bytes * i + 7] = in[4 * i + c];
3093 for(i = 0; i < numpixels; i++)
3095 out[bytes * i + 0] = out[bytes * i + 2] = out[bytes * i + 4] = in[2 * i];
3096 out[bytes * i + 1] = out[bytes * i + 3] = out[bytes * i + 5] = in[2 * i + 1];
3100 out[bytes * i + 6] = out[bytes * i + 7] = 0;
3101 else out[bytes * i + 6] = out[bytes * i + 7] = 255;
3106 for(i = 0; i < numpixels; i++)
3108 for(c = 0; c < 6; c++) out[bytes * i + c] = in[6 * i + c];
3111 if(infoIn->
key_defined && 256U * in[6 * i + 0] + in[6 * i + 1] == infoIn->
key_r
3112 && 256U * in[6 * i + 2] + in[6 * i + 3] == infoIn->
key_g
3113 && 256U * in[6 * i + 4] + in[6 * i + 5] == infoIn->
key_b)
3114 out[bytes * i + 6] = out[bytes * i + 7] = 0;
3115 else out[bytes * i + 6] = out[bytes * i + 7] = 255;
3120 for(i = 0; i < numpixels; i++)
3122 out[bytes * i + 0] = out[bytes * i + 2] = out[bytes * i + 4] = in[4 * i + 0];
3123 out[bytes * i + 1] = out[bytes * i + 3] = out[bytes * i + 5] = in[4 * i + 1];
3126 out[bytes * i + 6] = in[4 * i + 2];
3127 out[bytes * i + 7] = in[4 * i + 3];
3132 for(i = 0; i < numpixels; i++)
3134 for(c = 0; c <
bytes; c++) out[bytes * i + c] = in[8 * i + 2 * c];
3145 for(i = 0; i < numpixels; i++)
3147 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
3148 unsigned highest = ((1U << infoIn->
bitDepth) - 1U);
3150 out[bytes * i + 0] = out[bytes * i + 2] = out[bytes * i + 4]
3151 = out[bytes * i + 1] = out[bytes * i + 3]
3152 = out[bytes * i + 5] = (
unsigned char)((value * 255) / highest);
3155 if(infoIn->
key_defined && value == infoIn->
key_r) out[bytes * i + 6] = out[bytes * i + 7] = 0;
3156 else out[bytes * i + 6] = out[bytes * i + 7] = 255;
3161 for(i = 0; i < numpixels; i++)
3163 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
3165 for(c = 0; c < bytes / 2; c++)
3167 out[bytes * i + c * 2 + 0] = out[bytes * i + c * 2 + 1] = infoIn->
palette[4 * value + c];
3178 static unsigned LodePNG_convert_grey_16(
unsigned char* out,
const unsigned char* in,
3181 size_t i, c, bp = 0;
3188 for(i = 0; i < numpixels; i++)
3190 out[bytes * i] = out[bytes * i + 1] = in[i];
3193 if(infoIn->
key_defined && in[i] == infoIn->
key_r) out[bytes * i + 2] = out[bytes * i + 3] = 0;
3194 else out[bytes * i + 2] = out[bytes * i + 3] = 255;
3199 for(i = 0; i < numpixels; i++)
3201 out[bytes * i + 0] = out[bytes * i + 1] = in[2 * i + 0];
3202 if(alpha) out[bytes * i + 2] = out[bytes * i + 3] = in[2 * i + 1];
3213 for(i = 0; i < numpixels; i++)
3215 out[bytes * i + 0] = in[2 * i + 0];
3216 out[bytes * i + 1] = in[2 * i + 1];
3220 out[bytes * i + 2] = out[bytes * i + 3] = 0;
3221 else out[bytes * i + 2] = out[bytes * i + 3] = 255;
3226 for(i = 0; i < numpixels; i++)
3228 for(c = 0; c <
bytes; c++) out[bytes * i + c] = in[4 * i + c];
3237 for(i = 0; i < numpixels; i++)
3239 unsigned value = readBitsFromReversedStream(&bp, in, infoIn->
bitDepth);
3240 unsigned highest = ((1U << infoIn->
bitDepth) - 1U);
3241 out[bytes * i] = out[bytes * i + 1] = (
unsigned char)((value * 255) / highest);
3244 if(infoIn->
key_defined && value == infoIn->
key_r) out[bytes * i + 2] = out[bytes * i + 3] = 0;
3245 else out[bytes * i + 2] = out[bytes * i + 3] = 255;
3260 size_t numpixels = w * h;
3269 for(i = 0; i <
size; i++) out[i] = in[i];
3274 LodePNG_convert_rgb_a_8(out, in, infoIn, numpixels, bytes, alpha);
3280 LodePNG_convert_grey_8(out, in, infoIn, numpixels, bytes, alpha);
3284 LodePNG_convert_rgb_a_16(out, in, infoIn, numpixels, bytes, alpha);
3290 LodePNG_convert_grey_16(out, in, infoIn, numpixels, bytes, alpha);
3301 static unsigned char paethPredictor(
short a,
short b,
short c)
3303 short pa =
abs(b - c);
3304 short pb =
abs(a - c);
3305 short pc =
abs(a + b - c - c);
3312 if(pa <= pb && pa <= pc)
return (
unsigned char)a;
3313 else if(pb <= pc)
return (
unsigned char)b;
3314 else return (
unsigned char)c;
3319 static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 };
3320 static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 };
3321 static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 };
3322 static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 };
3324 static void Adam7_getpassvalues(
unsigned passw[7],
unsigned passh[7],
size_t filter_passstart[8],
3325 size_t padded_passstart[8],
size_t passstart[8],
unsigned w,
unsigned h,
unsigned bpp)
3331 for(i = 0; i < 7; i++)
3333 passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
3334 passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
3335 if(passw[i] == 0) passh[i] = 0;
3336 if(passh[i] == 0) passw[i] = 0;
3339 filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
3340 for(i = 0; i < 7; i++)
3343 filter_passstart[i + 1] = filter_passstart[i]
3344 + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0);
3346 padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8);
3348 passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8;
3352 #ifdef LODEPNG_COMPILE_DECODER
3361 if(inlength == 0 || in == 0)
3363 decoder->
error = 48;
3368 decoder->
error = 27;
3377 if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71
3378 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10)
3380 decoder->
error = 28;
3383 if(in[12] !=
'I' || in[13] !=
'H' || in[14] !=
'D' || in[15] !=
'R')
3385 decoder->
error = 29;
3401 unsigned checksum = Crc32_crc(&in[12], 17);
3404 decoder->
error = 57;
3419 static unsigned unfilterScanline(
unsigned char* recon,
const unsigned char* scanline,
const unsigned char* precon,
3420 size_t bytewidth,
unsigned char filterType,
size_t length)
3435 for(i = 0; i <
length; i++) recon[i] = scanline[i];
3438 for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
3439 for(i = bytewidth; i <
length; i++) recon[i] = scanline[i] + recon[i - bytewidth];
3444 for(i = 0; i <
length; i++) recon[i] = scanline[i] + precon[i];
3448 for(i = 0; i <
length; i++) recon[i] = scanline[i];
3454 for(i = 0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2;
3455 for(i = bytewidth; i <
length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
3459 for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
3460 for(i = bytewidth; i <
length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
3466 for(i = 0; i < bytewidth; i++)
3468 recon[i] = (scanline[i] + precon[i]);
3470 for(i = bytewidth; i <
length; i++)
3472 recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
3477 for(i = 0; i < bytewidth; i++)
3479 recon[i] = scanline[i];
3481 for(i = bytewidth; i <
length; i++)
3484 recon[i] = (scanline[i] + recon[i - bytewidth]);
3493 static unsigned unfilter(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
unsigned bpp)
3504 unsigned char* prevline = 0;
3507 size_t bytewidth = (bpp + 7) / 8;
3508 size_t linebytes = (w * bpp + 7) / 8;
3510 for(y = 0; y < h; y++)
3512 size_t outindex = linebytes *
y;
3513 size_t inindex = (1 + linebytes) * y;
3514 unsigned char filterType = in[inindex];
3516 unsigned error = unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes);
3517 if(error)
return error;
3519 prevline = &out[outindex];
3525 static void Adam7_deinterlace(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
unsigned bpp)
3531 unsigned passw[7], passh[7];
3532 size_t filter_passstart[8], padded_passstart[8], passstart[8];
3535 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
3539 for(i = 0; i < 7; i++)
3542 size_t bytewidth = bpp / 8;
3543 for(y = 0; y < passh[i]; y++)
3544 for(x = 0; x < passw[i]; x++)
3546 size_t pixelinstart = passstart[i] + (y * passw[i] +
x) * bytewidth;
3547 size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
3548 for(b = 0; b < bytewidth; b++)
3550 out[pixeloutstart + b] = in[pixelinstart + b];
3557 for(i = 0; i < 7; i++)
3560 unsigned ilinebits = bpp * passw[i];
3561 unsigned olinebits = bpp * w;
3563 for(y = 0; y < passh[i]; y++)
3564 for(x = 0; x < passw[i]; x++)
3566 ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
3567 obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
3568 for(b = 0; b < bpp; b++)
3570 unsigned char bit = readBitFromReversedStream(&ibp, in);
3572 setBitOfReversedStream0(&obp, out, bit);
3579 static void removePaddingBits(
unsigned char* out,
const unsigned char* in,
3580 size_t olinebits,
size_t ilinebits,
unsigned h)
3592 size_t diff = ilinebits - olinebits;
3593 size_t ibp = 0, obp = 0;
3594 for(y = 0; y < h; y++)
3597 for(x = 0; x < olinebits; x++)
3599 unsigned char bit = readBitFromReversedStream(&ibp, in);
3600 setBitOfReversedStream(&obp, out, bit);
3609 static unsigned postProcessScanlines(
unsigned char* out,
unsigned char* in,
const LodePNG_InfoPng* infoPng)
3619 unsigned w = infoPng->
width;
3620 unsigned h = infoPng->
height;
3622 if(bpp == 0)
return 31;
3626 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
3628 error = unfilter(in, in, w, h, bpp);
3629 if(error)
return error;
3630 removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
3632 else error = unfilter(out, in, w, h, bpp);
3636 unsigned passw[7], passh[7];
size_t filter_passstart[8], padded_passstart[8], passstart[8];
3639 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
3641 for(i = 0; i < 7; i++)
3643 error = unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp);
3644 if(error)
return error;
3651 removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp,
3652 ((passw[i] * bpp + 7) / 8) * 8, passh[i]);
3656 Adam7_deinterlace(out, in, w, h, bpp);
3663 static void decodeGeneric(
LodePNG_Decoder* decoder,
unsigned char** out,
size_t* outsize,
3664 const unsigned char* in,
size_t insize)
3666 unsigned char IEND = 0;
3667 const unsigned char* chunk;
3672 unsigned unknown = 0;
3673 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
3674 unsigned critical_pos = 1;
3682 if(decoder->
error)
return;
3684 ucvector_init(&idat);
3692 unsigned chunkLength;
3693 const unsigned char*
data;
3696 if((
size_t)((chunk - in) + 12) > insize || chunk < in)
CERROR_BREAK(decoder->
error, 30);
3703 if((
size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in)
3713 size_t oldsize = idat.
size;
3714 if(!ucvector_resize(&idat, oldsize + chunkLength))
CERROR_BREAK(decoder->
error, 9936 );
3715 for(i = 0; i < chunkLength; i++) idat.
data[oldsize + i] = data[i];
3746 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
3781 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3800 = 256 * data[0] + data[1];
3818 char *key = 0, *str = 0;
3820 while(!decoder->
error)
3822 unsigned length, string2_begin;
3825 while(length < chunkLength && data[length] != 0) length++;
3829 key = (
char*)
malloc(length + 1);
3833 for(i = 0; i <
length; i++) key[i] = data[i];
3835 string2_begin = length + 1;
3839 length = chunkLength - string2_begin;
3840 str = (
char*)
malloc(length + 1);
3844 for(i = 0; i <
length; i++) str[i] = data[string2_begin + i];
3860 unsigned length, string2_begin;
3864 ucvector_init(&decoded);
3866 while(!decoder->
error)
3868 for(length = 0; length < chunkLength && data[
length] != 0; length++) ;
3871 key = (
char*)
malloc(length + 1);
3875 for(i = 0; i <
length; i++) key[i] = data[i];
3879 string2_begin = length + 2;
3882 length = chunkLength - string2_begin;
3884 (
unsigned char*)(&data[string2_begin]),
3886 if(decoder->
error)
break;
3887 ucvector_push_back(&decoded, 0);
3895 ucvector_cleanup(&decoded);
3896 if(decoder->
error)
break;
3904 unsigned length, begin, compressed;
3905 char *key = 0, *langtag = 0, *transkey = 0;
3907 ucvector_init(&decoded);
3909 while(!decoder->
error)
3916 for(length = 0; length < chunkLength && data[
length] != 0; length++) ;
3919 key = (
char*)
malloc(length + 1);
3923 for(i = 0; i <
length; i++) key[i] = data[i];
3926 compressed = data[length + 1];
3932 for(i = begin; i < chunkLength && data[i] != 0; i++) length++;
3935 langtag = (
char*)
malloc(length + 1);
3939 for(i = 0; i <
length; i++) langtag[i] = data[begin + i];
3942 begin += length + 1;
3944 for(i = begin; i < chunkLength && data[i] != 0; i++) length++;
3947 transkey = (
char*)
malloc(length + 1);
3951 for(i = 0; i <
length; i++) transkey[i] = data[begin + i];
3954 begin += length + 1;
3957 length = chunkLength - begin;
3962 (
unsigned char*)(&data[begin]),
3964 if(decoder->
error)
break;
3965 ucvector_push_back(&decoded, 0);
3972 for(i = 0; i <
length; i++) decoded.
data[i] = data[begin + i];
3983 ucvector_cleanup(&decoded);
3984 if(decoder->
error)
break;
4004 decoder->
infoPng.
phys_x = 16777216 * data[0] + 65536 * data[1] + 256 * data[2] + data[3];
4005 decoder->
infoPng.
phys_y = 16777216 * data[4] + 65536 * data[5] + 256 * data[6] + data[7];
4015 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
4020 &unknown->
datasize[critical_pos - 1], chunk);
4021 if(decoder->
error)
break;
4037 ucvector_init(&scanlines);
4043 decoder->
error = 9945;
4055 ucvector_init(&outv);
4060 *outsize = outv.
size;
4062 ucvector_cleanup(&scanlines);
4065 ucvector_cleanup(&idat);
4069 const unsigned char* in,
size_t insize)
4073 decodeGeneric(decoder, out, outsize, in, insize);
4074 if(decoder->
error)
return;
4083 if(decoder->
error)
return;
4089 unsigned char* data = *out;
4096 decoder->
error = 56;
4102 *out = (
unsigned char*)
malloc(*outsize);
4105 decoder->
error = 9947;
4114 unsigned LodePNG_decode(
unsigned char** out,
unsigned* w,
unsigned* h,
const unsigned char* in,
4115 size_t insize,
unsigned colorType,
unsigned bitDepth)
4124 error = decoder.
error;
4131 unsigned LodePNG_decode32(
unsigned char** out,
unsigned* w,
unsigned* h,
const unsigned char* in,
size_t insize)
4136 unsigned LodePNG_decode24(
unsigned char** out,
unsigned* w,
unsigned* h,
const unsigned char* in,
size_t insize)
4141 #ifdef LODEPNG_COMPILE_DISK
4143 unsigned colorType,
unsigned bitDepth)
4149 if(!error) error =
LodePNG_decode(out, w, h, buffer, buffersize, colorType, bitDepth);
4168 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4172 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
4204 #ifdef LODEPNG_COMPILE_ENCODER
4211 static unsigned addChunk(
ucvector* out,
const char* chunkName,
const unsigned char* data,
size_t length)
4214 if(error)
return error;
4219 static void writeSignature(
ucvector* out)
4222 ucvector_push_back(out, 137);
4223 ucvector_push_back(out, 80);
4224 ucvector_push_back(out, 78);
4225 ucvector_push_back(out, 71);
4226 ucvector_push_back(out, 13);
4227 ucvector_push_back(out, 10);
4228 ucvector_push_back(out, 26);
4229 ucvector_push_back(out, 10);
4232 static unsigned addChunk_IHDR(
ucvector* out,
unsigned w,
unsigned h,
unsigned bitDepth,
4233 unsigned colorType,
unsigned interlaceMethod)
4237 ucvector_init(&header);
4239 LodePNG_add32bitInt(&header, w);
4240 LodePNG_add32bitInt(&header, h);
4241 ucvector_push_back(&header, (
unsigned char)bitDepth);
4242 ucvector_push_back(&header, (
unsigned char)colorType);
4243 ucvector_push_back(&header, 0);
4244 ucvector_push_back(&header, 0);
4245 ucvector_push_back(&header, interlaceMethod);
4247 error = addChunk(out,
"IHDR", header.
data, header.
size);
4248 ucvector_cleanup(&header);
4258 ucvector_init(&PLTE);
4262 if(i % 4 != 3) ucvector_push_back(&PLTE, info->
palette[i]);
4264 error = addChunk(out,
"PLTE", PLTE.
data, PLTE.
size);
4265 ucvector_cleanup(&PLTE);
4275 ucvector_init(&tRNS);
4279 for(i = 0; i < info->
palettesize; i++) ucvector_push_back(&tRNS, info->
palette[4 * i + 3]);
4285 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_r / 256));
4286 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_r % 256));
4293 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_r / 256));
4294 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_r % 256));
4295 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_g / 256));
4296 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_g % 256));
4297 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_b / 256));
4298 ucvector_push_back(&tRNS, (
unsigned char)(info->
key_b % 256));
4302 error = addChunk(out,
"tRNS", tRNS.
data, tRNS.
size);
4303 ucvector_cleanup(&tRNS);
4308 static unsigned addChunk_IDAT(
ucvector* out,
const unsigned char* data,
size_t datasize,
4315 ucvector_init(&zlibdata);
4317 if(!error) error = addChunk(out,
"IDAT", zlibdata.
data, zlibdata.
size);
4318 ucvector_cleanup(&zlibdata);
4323 static unsigned addChunk_IEND(
ucvector* out)
4326 error = addChunk(out,
"IEND", 0, 0);
4330 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4332 static unsigned addChunk_tEXt(
ucvector* out,
const char* keyword,
const char* textstring)
4337 ucvector_init(&text);
4338 for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&text, (
unsigned char)keyword[i]);
4339 ucvector_push_back(&text, 0);
4340 for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&text, (
unsigned char)textstring[i]);
4341 error = addChunk(out,
"tEXt", text.
data, text.
size);
4342 ucvector_cleanup(&text);
4347 static unsigned addChunk_zTXt(
ucvector* out,
const char* keyword,
const char* textstring,
4352 size_t i, textsize = strlen(textstring);
4354 ucvector_init(&data);
4355 ucvector_init(&compressed);
4356 for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)keyword[i]);
4357 ucvector_push_back(&data, 0);
4358 ucvector_push_back(&data, 0);
4361 (
unsigned char*)textstring, textsize, zlibsettings);
4364 for(i = 0; i < compressed.
size; i++) ucvector_push_back(&data, compressed.
data[i]);
4365 error = addChunk(out,
"zTXt", data.
data, data.
size);
4368 ucvector_cleanup(&compressed);
4369 ucvector_cleanup(&data);
4373 static unsigned addChunk_iTXt(
ucvector* out,
unsigned compressed,
const char* keyword,
const char* langtag,
4378 size_t i, textsize = strlen(textstring);
4380 ucvector_init(&data);
4382 for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)keyword[i]);
4383 ucvector_push_back(&data, 0);
4384 ucvector_push_back(&data, compressed ? 1 : 0);
4385 ucvector_push_back(&data, 0);
4386 for(i = 0; langtag[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)langtag[i]);
4387 ucvector_push_back(&data, 0);
4388 for(i = 0; transkey[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)transkey[i]);
4389 ucvector_push_back(&data, 0);
4393 ucvector_init(&compressed_data);
4395 (
unsigned char*)textstring, textsize, zlibsettings);
4398 for(i = 0; i < compressed_data.
size; i++) ucvector_push_back(&data, compressed_data.
data[i]);
4399 for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)textstring[i]);
4404 for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&data, (
unsigned char)textstring[i]);
4407 if(!error) error = addChunk(out,
"iTXt", data.
data, data.
size);
4408 ucvector_cleanup(&data);
4416 ucvector_init(&bKGD);
4419 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_r / 256));
4420 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_r % 256));
4424 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_r / 256));
4425 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_r % 256));
4426 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_g / 256));
4427 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_g % 256));
4428 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_b / 256));
4429 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_b % 256));
4433 ucvector_push_back(&bKGD, (
unsigned char)(info->
background_r % 256));
4436 error = addChunk(out,
"bKGD", bKGD.
data, bKGD.
size);
4437 ucvector_cleanup(&bKGD);
4445 unsigned char* data = (
unsigned char*)
malloc(7);
4446 if(!data)
return 9948;
4447 data[0] = (
unsigned char)(time->
year / 256);
4448 data[1] = (
unsigned char)(time->
year % 256);
4449 data[2] = time->
month;
4450 data[3] = time->
day;
4451 data[4] = time->
hour;
4454 error = addChunk(out,
"tIME", data, 7);
4463 ucvector_init(&data);
4465 LodePNG_add32bitInt(&data, info->
phys_x);
4466 LodePNG_add32bitInt(&data, info->
phys_y);
4467 ucvector_push_back(&data, info->
phys_unit);
4469 error = addChunk(out,
"pHYs", data.
data, data.
size);
4470 ucvector_cleanup(&data);
4477 static void filterScanline(
unsigned char* out,
const unsigned char* scanline,
const unsigned char* prevline,
4478 size_t length,
size_t bytewidth,
unsigned char filterType)
4484 for(i = 0; i <
length; i++) out[i] = scanline[i];
4489 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
4490 for(i = bytewidth; i <
length ; i++) out[i] = scanline[i] - scanline[i - bytewidth];
4494 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
4495 for(i = bytewidth; i <
length; i++) out[i] = scanline[i] - scanline[i - bytewidth];
4501 for(i = 0; i <
length; i++) out[i] = scanline[i] - prevline[i];
4505 for(i = 0; i <
length; i++) out[i] = scanline[i];
4511 for(i = 0; i < bytewidth; i++) out[i] = scanline[i] - prevline[i] / 2;
4512 for(i = bytewidth; i <
length; i++) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) / 2);
4516 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
4517 for(i = bytewidth; i <
length; i++) out[i] = scanline[i] - scanline[i - bytewidth] / 2;
4524 for(i = 0; i < bytewidth; i++) out[i] = (scanline[i] - prevline[i]);
4525 for(i = bytewidth; i <
length; i++)
4527 out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth]));
4532 for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
4534 for(i = bytewidth; i <
length; i++) out[i] = (scanline[i] - scanline[i - bytewidth]);
4541 static unsigned filter(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
4552 size_t linebytes = (w * bpp + 7) / 8;
4554 size_t bytewidth = (bpp + 7) / 8;
4555 const unsigned char* prevline = 0;
4559 if(bpp == 0)
return 31;
4573 for(y = 0; y < h; y++)
4575 size_t outindex = (1 + linebytes) * y;
4576 size_t inindex = linebytes *
y;
4577 const unsigned TYPE = 0;
4578 out[outindex] = TYPE;
4579 filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, TYPE);
4580 prevline = &in[inindex];
4587 size_t smallest = 0;
4588 unsigned type, bestType = 0;
4590 for(type = 0; type < 5; type++) ucvector_init(&attempt[type]);
4591 for(type = 0; type < 5; type++)
4593 if(!ucvector_resize(&attempt[type], linebytes))
ERROR_BREAK(9949 );
4598 for(y = 0; y < h; y++)
4601 for(type = 0; type < 5; type++)
4603 filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type);
4608 for(x = 0; x < attempt[
type].
size; x+=3)
4613 if(type == 0) sum[
type] += (
unsigned char)(attempt[type].data[x]);
4616 signed char s = (
signed char)(attempt[type].data[x]);
4617 sum[
type] += s < 0 ? -s : s;
4622 if(type == 0 || sum[type] < smallest)
4625 smallest = sum[
type];
4629 prevline = &in[y * linebytes];
4632 out[y * (linebytes + 1)] = bestType;
4633 for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 +
x] = attempt[bestType].
data[
x];
4637 for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
4648 unsigned type = 0, bestType = 0;
4649 unsigned char* dummy;
4655 zlibsettings.
btype = 1;
4656 for(type = 0; type < 5; type++)
4658 ucvector_init(&attempt[type]);
4659 ucvector_resize(&attempt[type], linebytes);
4661 for(y = 0; y < h; y++)
4663 for(type = 0; type < 5; type++)
4665 unsigned testsize = attempt[
type].
size;
4669 filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type);
4675 if(type == 0 || size[type] < smallest)
4678 smallest = size[
type];
4681 prevline = &in[y * linebytes];
4682 out[y * (linebytes + 1)] = bestType;
4683 for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 +
x] = attempt[bestType].
data[
x];
4685 for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
4691 static void addPaddingBits(
unsigned char* out,
const unsigned char* in,
4692 size_t olinebits,
size_t ilinebits,
unsigned h)
4697 size_t diff = olinebits - ilinebits;
4698 size_t obp = 0, ibp = 0;
4699 for(y = 0; y < h; y++)
4702 for(x = 0; x < ilinebits; x++)
4704 unsigned char bit = readBitFromReversedStream(&ibp, in);
4705 setBitOfReversedStream(&obp, out, bit);
4709 for(x = 0; x < diff; x++) setBitOfReversedStream(&obp, out, 0);
4713 static void Adam7_interlace(
unsigned char* out,
const unsigned char* in,
unsigned w,
unsigned h,
unsigned bpp)
4717 unsigned passw[7], passh[7];
4718 size_t filter_passstart[8], padded_passstart[8], passstart[8];
4721 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
4725 for(i = 0; i < 7; i++)
4728 size_t bytewidth = bpp / 8;
4729 for(y = 0; y < passh[i]; y++)
4730 for(x = 0; x < passw[i]; x++)
4732 size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
4733 size_t pixeloutstart = passstart[i] + (y * passw[i] +
x) * bytewidth;
4734 for(b = 0; b < bytewidth; b++)
4736 out[pixeloutstart + b] = in[pixelinstart + b];
4743 for(i = 0; i < 7; i++)
4746 unsigned ilinebits = bpp * passw[i];
4747 unsigned olinebits = bpp * w;
4749 for(y = 0; y < passh[i]; y++)
4750 for(x = 0; x < passw[i]; x++)
4752 ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
4753 obp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
4754 for(b = 0; b < bpp; b++)
4756 unsigned char bit = readBitFromReversedStream(&ibp, in);
4757 setBitOfReversedStream(&obp, out, bit);
4766 static unsigned preProcessScanlines(
unsigned char** out,
size_t* outsize,
const unsigned char* in,
4775 unsigned w = infoPng->
width;
4776 unsigned h = infoPng->
height;
4781 *outsize = h + (h * ((w * bpp + 7) / 8));
4782 *out = (
unsigned char*)
malloc(*outsize);
4783 if(!(*out) && (*outsize)) error = 9950;
4788 if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
4791 ucvector_init(&padded);
4792 if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9951;
4795 addPaddingBits(padded.
data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
4796 error = filter(*out, padded.
data, w, h, &infoPng->
color, settings);
4798 ucvector_cleanup(&padded);
4803 error = filter(*out, in, w, h, &infoPng->
color, settings);
4809 unsigned char* adam7 = (
unsigned char*)
malloc((h * w * bpp + 7) / 8);
4810 if(!adam7 && ((h * w * bpp + 7) / 8)) error = 9952;
4814 unsigned passw[7], passh[7];
4815 size_t filter_passstart[8], padded_passstart[8], passstart[8];
4818 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
4820 *outsize = filter_passstart[7];
4821 *out = (
unsigned char*)
malloc(*outsize);
4824 Adam7_interlace(adam7, in, w, h, bpp);
4826 for(i = 0; i < 7; i++)
4831 ucvector_init(&padded);
4832 if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9954;
4835 addPaddingBits(&padded.
data[padded_passstart[i]], &adam7[passstart[i]],
4836 ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
4837 error = filter(&(*out)[filter_passstart[i]], &padded.
data[padded_passstart[i]],
4838 passw[i], passh[i], &infoPng->
color, settings);
4841 ucvector_cleanup(&padded);
4845 error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]],
4846 passw[i], passh[i], &infoPng->
color, settings);
4860 static unsigned isPaletteFullyOpaque(
const unsigned char* palette,
size_t palettesize)
4863 for(i = 0; i < palettesize; i++)
4865 if(palette[4 * i + 3] != 255)
return 0;
4871 static unsigned isFullyOpaque(
const unsigned char*
image,
unsigned w,
unsigned h,
const LodePNG_InfoColor* info)
4876 unsigned i, numpixels = w * h;
4881 for(i = 0; i < numpixels; i++)
4883 if(image[i * 4 + 3] != 255)
return 0;
4888 for(i = 0; i < numpixels; i++)
4890 if(image[i * 8 + 6] != 255 || image[i * 8 + 7] != 255)
return 0;
4899 for(i = 0; i < numpixels; i++)
4901 if(image[i * 2 + 1] != 255)
return 0;
4906 for(i = 0; i < numpixels; i++)
4908 if(image[i * 4 + 2] != 255 || image[i * 4 + 3] != 255)
return 0;
4923 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
4924 static unsigned addUnknownChunks(
ucvector* out,
unsigned char* data,
size_t datasize)
4926 unsigned char* inchunk =
data;
4927 while((
size_t)(inchunk - data) < datasize)
4930 if(error)
return error;
4939 const unsigned char* image,
unsigned w,
unsigned h)
4943 unsigned char* data = 0;
4944 size_t datasize = 0;
4966 encoder->
error = 60;
4971 encoder->
error = 61;
4976 encoder->
error = 71;
4986 unsigned char* converted;
4991 encoder->
error = 59;
4994 converted = (
unsigned char*)
malloc(size);
4995 if(!converted && size) encoder->
error = 9955;
5000 if(!encoder->
error) preProcessScanlines(&data, &datasize, converted, &info, &encoder->
settings);
5003 else preProcessScanlines(&data, &datasize, image, &info, &encoder->
settings);
5005 ucvector_init(&outv);
5006 while(!encoder->
error)
5008 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5012 writeSignature(&outv);
5015 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
5020 if(encoder->
error)
break;
5028 encoder->
error = 68;
5031 addChunk_PLTE(&outv, &info.
color);
5037 encoder->
error = 68;
5040 addChunk_PLTE(&outv, &info.
color);
5045 addChunk_tRNS(&outv, &info.
color);
5049 addChunk_tRNS(&outv, &info.
color);
5051 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5057 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
5062 if(encoder->
error)
break;
5067 if(encoder->
error)
break;
5068 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5072 for(i = 0; i < info.
text.
num; i++)
5076 encoder->
error = 66;
5081 encoder->
error = 67;
5092 unsigned alread_added_id_text = 0;
5093 for(i = 0; i < info.
text.
num; i++)
5095 if(!strcmp(info.
text.
keys[i],
"LodePNG"))
5097 alread_added_id_text = 1;
5101 if(alread_added_id_text == 0)
5105 for(i = 0; i < info.
itext.
num; i++)
5109 encoder->
error = 66;
5114 encoder->
error = 67;
5122 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
5127 if(encoder->
error)
break;
5131 addChunk_IEND(&outv);
5139 *outsize = outv.
size;
5142 unsigned LodePNG_encode(
unsigned char** out,
size_t* outsize,
const unsigned char* image,
5143 unsigned w,
unsigned h,
unsigned colorType,
unsigned bitDepth)
5153 error = encoder.
error;
5158 unsigned LodePNG_encode32(
unsigned char** out,
size_t* outsize,
const unsigned char* image,
unsigned w,
unsigned h)
5163 unsigned LodePNG_encode24(
unsigned char** out,
size_t* outsize,
const unsigned char* image,
unsigned w,
unsigned h)
5168 #ifdef LODEPNG_COMPILE_DISK
5170 unsigned colorType,
unsigned bitDepth)
5174 unsigned error =
LodePNG_encode(&buffer, &buffersize, image, w, h, colorType, bitDepth);
5197 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5224 if(dest->
error)
return;
5226 if(dest->
error)
return;
5233 #ifdef LODEPNG_COMPILE_ERROR_TEXT
5243 case 0:
return "no error, everything went ok";
5244 case 1:
return "nothing done yet";
5245 case 10:
return "end of input memory reached without huffman end code";
5246 case 11:
return "error in code tree made it jump outside of huffman tree";
5247 case 13:
return "problem while processing dynamic deflate block";
5248 case 14:
return "problem while processing dynamic deflate block";
5249 case 15:
return "problem while processing dynamic deflate block";
5250 case 16:
return "unexisting code while processing dynamic deflate block";
5251 case 17:
return "end of out buffer memory reached while inflating";
5252 case 18:
return "invalid distance code while inflating";
5253 case 19:
return "end of out buffer memory reached while inflating";
5254 case 20:
return "invalid deflate block BTYPE encountered while decoding";
5255 case 21:
return "NLEN is not ones complement of LEN in a deflate block";
5261 case 22:
return "end of out buffer memory reached while inflating";
5263 case 23:
return "end of in buffer memory reached while inflating";
5264 case 24:
return "invalid FCHECK in zlib header";
5265 case 25:
return "invalid compression method in zlib header";
5266 case 26:
return "FDICT encountered in zlib header while it's not used for PNG";
5267 case 27:
return "PNG file is smaller than a PNG header";
5269 case 28:
return "incorrect PNG signature, it's no PNG or corrupted";
5270 case 29:
return "first chunk is not the header chunk";
5271 case 30:
return "chunk length too large, chunk broken off at end of file";
5272 case 31:
return "illegal PNG color type or bpp";
5273 case 32:
return "illegal PNG compression method";
5274 case 33:
return "illegal PNG filter method";
5275 case 34:
return "illegal PNG interlace method";
5276 case 35:
return "chunk length of a chunk is too large or the chunk too small";
5277 case 36:
return "illegal PNG filter type encountered";
5278 case 37:
return "illegal bit depth for this color type given";
5279 case 38:
return "the palette is too big";
5280 case 39:
return "more palette alpha values given in tRNS chunk than there are colors in the palette";
5281 case 40:
return "tRNS chunk has wrong size for greyscale image";
5282 case 41:
return "tRNS chunk has wrong size for RGB image";
5283 case 42:
return "tRNS chunk appeared while it was not allowed for this color type";
5284 case 43:
return "bKGD chunk has wrong size for palette image";
5285 case 44:
return "bKGD chunk has wrong size for greyscale image";
5286 case 45:
return "bKGD chunk has wrong size for RGB image";
5288 case 46:
return "a value in indexed image is larger than the palette size (bitdepth = 8)";
5290 case 47:
return "a value in indexed image is larger than the palette size (bitdepth < 8)";
5292 case 48:
return "empty input or file doesn't exist";
5293 case 49:
return "jumped past memory while generating dynamic huffman tree";
5294 case 50:
return "jumped past memory while generating dynamic huffman tree";
5295 case 51:
return "jumped past memory while inflating huffman block";
5296 case 52:
return "jumped past memory while inflating";
5297 case 53:
return "size of zlib data too small";
5302 case 55:
return "jumped past tree while generating huffman tree";
5304 case 56:
return "given output image colorType or bitDepth not supported for color conversion";
5305 case 57:
return "invalid CRC encountered (checking CRC can be disabled)";
5306 case 58:
return "invalid ADLER32 encountered (checking ADLER32 can be disabled)";
5307 case 59:
return "conversion to unexisting color mode or color mode conversion not supported";
5308 case 60:
return "invalid window size given in the settings of the encoder (must be 0-32768)";
5309 case 61:
return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)";
5311 case 62:
return "conversion from RGB to greyscale not supported";
5312 case 63:
return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk";
5314 case 64:
return "the length of the END symbol 256 in the Huffman tree is 0";
5315 case 66:
return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes";
5316 case 67:
return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte";
5317 case 68:
return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors";
5318 case 69:
return "unknown chunk type with 'critical' flag encountered by the decoder";
5319 case 71:
return "unexisting interlace mode given to encoder (must be 0 or 1)";
5320 case 72:
return "while decoding, unexisting compression method encountering in zTXt or iTXt chunk (it must be 0)";
5321 case 73:
return "invalid tIME chunk size";
5322 case 74:
return "invalid pHYs chunk size";
5324 case 75:
return "no null termination char found while decoding text chunk";
5325 case 76:
return "iTXt chunk too short to contain required bytes";
5326 case 77:
return "integer overflow in buffer size";
5327 case 78:
return "failed to open file for reading";
5328 case 79:
return "failed to open file for writing";
5329 case 80:
return "tried creating a tree of 0 symbols";
5333 if(code >= 9900 && code <= 9999)
return "memory allocation failed";
5335 return "unknown error code";
5349 #ifdef LODEPNG_COMPILE_DISK
5350 void loadFile(std::vector<unsigned char>& buffer,
const std::string& filename)
5352 std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
5355 std::streamsize size = 0;
5356 if(file.seekg(0, std::ios::end).good()) size = file.tellg();
5357 if(file.seekg(0, std::ios::beg).good()) size -= file.tellg();
5360 buffer.resize(
size_t(size));
5361 if(size > 0) file.read((
char*)(&buffer[0]), size);
5365 void saveFile(
const std::vector<unsigned char>& buffer,
const std::string& filename)
5367 std::ofstream file(filename.c_str(), std::ios::out|std::ios::binary);
5368 file.write(buffer.empty() ? 0 : (
char*)&buffer[0], std::streamsize(buffer.size()));
5370 #endif //LODEPNG_COMPILE_DISK
5374 #ifdef LODEPNG_COMPILE_ZLIB
5375 #ifdef LODEPNG_COMPILE_DECODER
5376 unsigned decompress(std::vector<unsigned char>& out,
const unsigned char* in,
size_t insize,
5379 unsigned char* buffer = 0;
5380 size_t buffersize = 0;
5384 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
5390 unsigned decompress(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in,
5393 return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings);
5395 #endif //LODEPNG_COMPILE_DECODER
5397 #ifdef LODEPNG_COMPILE_ENCODER
5398 unsigned compress(std::vector<unsigned char>& out,
const unsigned char* in,
size_t insize,
5401 unsigned char* buffer = 0;
5402 size_t buffersize = 0;
5406 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
5412 unsigned compress(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in,
5415 return compress(out, in.empty() ? 0 : &in[0], in.size(), settings);
5417 #endif //LODEPNG_COMPILE_ENCODER
5418 #endif //LODEPNG_COMPILE_ZLIB
5420 #ifdef LODEPNG_COMPILE_PNG
5421 #ifdef LODEPNG_COMPILE_DECODER
5446 unsigned Decoder::getWidth()
const
5448 return infoPng.
width;
5451 unsigned Decoder::getHeight()
const
5456 unsigned Decoder::getBpp()
5461 unsigned Decoder::getChannels()
5466 unsigned Decoder::isGreyscaleType()
5471 unsigned Decoder::isAlphaType()
5476 void Decoder::decode(std::vector<unsigned char>& out,
const unsigned char* in,
size_t insize)
5483 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
5488 void Decoder::decode(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in)
5490 decode(out, in.empty() ? 0 : &in[0], in.size());
5493 void Decoder::inspect(
const unsigned char* in,
size_t insize)
5498 void Decoder::inspect(
const std::vector<unsigned char>& in)
5500 inspect(in.empty() ? 0 : &in[0], in.size());
5515 this->settings = settings;
5553 #endif //LODEPNG_COMPILE_DECODER
5557 #ifdef LODEPNG_COMPILE_ENCODER
5584 void Encoder::encode(std::vector<unsigned char>& out,
const unsigned char* image,
unsigned w,
unsigned h)
5591 out.insert(out.end(), &buffer[0], &buffer[buffersize]);
5596 void Encoder::encode(std::vector<unsigned char>& out,
const std::vector<unsigned char>& image,
5597 unsigned w,
unsigned h)
5599 encode(out, image.empty() ? 0 : &image[0], w, h);
5602 void Encoder::clearPalette()
5607 void Encoder::addPalette(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a)
5612 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
5613 void Encoder::clearText()
5618 void Encoder::addText(
const std::string& key,
const std::string& str)
5623 void Encoder::clearIText()
5628 void Encoder::addIText(
const std::string& key,
const std::string& langtag,
5629 const std::string& transkey,
const std::string& str)
5633 #endif //LODEPNG_COMPILE_ANCILLARY_CHUNKS
5647 this->settings = settings;
5684 #endif //LODEPNG_COMPILE_ENCODER
5688 #ifdef LODEPNG_COMPILE_DECODER
5690 unsigned decode(std::vector<unsigned char>& out,
unsigned& w,
unsigned& h,
const unsigned char* in,
5691 size_t insize,
unsigned colorType,
unsigned bitDepth)
5694 decoder.getInfoRaw().color.colorType = colorType;
5695 decoder.getInfoRaw().color.bitDepth = bitDepth;
5696 decoder.decode(out, in, insize);
5697 w = decoder.getWidth();
5698 h = decoder.getHeight();
5699 return decoder.getError();
5702 unsigned decode(std::vector<unsigned char>& out,
unsigned& w,
unsigned& h,
5703 const std::vector<unsigned char>& in,
unsigned colorType,
unsigned bitDepth)
5705 return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colorType, bitDepth);
5708 #ifdef LODEPNG_COMPILE_DISK
5709 unsigned decode(std::vector<unsigned char>& out,
unsigned& w,
unsigned& h,
const std::string& filename,
5710 unsigned colorType,
unsigned bitDepth)
5712 std::vector<unsigned char>
buffer;
5713 loadFile(buffer, filename);
5714 return decode(out, w, h, buffer, colorType, bitDepth);
5716 #endif //LODEPNG_COMPILE_DECODER
5717 #endif //LODEPNG_COMPILE_DISK
5719 #ifdef LODEPNG_COMPILE_ENCODER
5721 unsigned encode(std::vector<unsigned char>& out,
const unsigned char* in,
unsigned w,
unsigned h,
5722 unsigned colorType,
unsigned bitDepth)
5725 encoder.getInfoRaw().color.colorType = colorType;
5726 encoder.getInfoRaw().color.bitDepth = bitDepth;
5727 encoder.getInfoPng().color.colorType = colorType;
5728 encoder.getInfoPng().color.bitDepth = bitDepth;
5729 encoder.encode(out, in, w, h);
5730 return encoder.getError();
5733 unsigned encode(std::vector<unsigned char>& out,
const std::vector<unsigned char>& in,
unsigned w,
unsigned h,
5734 unsigned colorType,
unsigned bitDepth)
5736 return encode(out, in.empty() ? 0 : &in[0], w, h, colorType, bitDepth);
5739 #ifdef LODEPNG_COMPILE_DISK
5740 unsigned encode(
const std::string& filename,
const unsigned char* in,
unsigned w,
unsigned h,
5741 unsigned colorType,
unsigned bitDepth)
5743 std::vector<unsigned char>
buffer;
5745 encoder.getInfoRaw().color.colorType = colorType;
5746 encoder.getInfoRaw().color.bitDepth = bitDepth;
5747 encoder.encode(buffer, in, w, h);
5748 if(!encoder.hasError()) saveFile(buffer, filename);
5749 return encoder.getError();
5752 unsigned encode(
const std::string& filename,
const std::vector<unsigned char>& in,
unsigned w,
unsigned h,
5753 unsigned colorType,
unsigned bitDepth)
5755 return encode(filename, in.empty() ? 0 : &in[0], w, h, colorType, bitDepth);
5757 #endif //LODEPNG_COMPILE_DISK
5758 #endif //LODEPNG_COMPILE_ENCODER
5759 #endif //LODEPNG_COMPILE_PNG
unsigned autoLeaveOutAlphaChannel
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int offset
void LodePNG_Text_init(LodePNG_Text *text)
unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng *dest, const LodePNG_InfoPng *source)
unsigned LodePNG_loadFile(unsigned char **out, size_t *outsize, const char *filename)
#define ERROR_BREAK(code)
unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor *info)
unsigned bruteForceFilters
const LodePNG_CompressSettings LodePNG_defaultCompressSettings
unsigned LodePNG_zlib_compress(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNG_CompressSettings *settings)
void * malloc(size_t size)
void LodePNG_Decoder_cleanup(LodePNG_Decoder *decoder)
unsigned char * LodePNG_chunk_next(unsigned char *chunk)
void LodePNG_UnknownChunks_cleanup(LodePNG_UnknownChunks *chunks)
unsigned LodePNG_decode32(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize)
#define NUM_DEFLATE_CODE_SYMBOLS
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer
void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor *info)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid * data
unsigned LodePNG_create_chunk(unsigned char **out, size_t *outlength, unsigned length, const char *type, const unsigned char *data)
void LodePNG_IText_init(LodePNG_IText *text)
const char * LodePNG_error_text(unsigned code)
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor *info)
struct HuffmanTree HuffmanTree
void LodePNG_Decoder_init(LodePNG_Decoder *decoder)
unsigned LodePNG_InfoColor_getChannels(const LodePNG_InfoColor *info)
LodePNG_DecompressSettings zlibsettings
void LodePNG_InfoPng_swap(LodePNG_InfoPng *a, LodePNG_InfoPng *b)
unsigned LodePNG_encode24_file(const char *filename, const unsigned char *image, unsigned w, unsigned h)
unsigned LodePNG_decode32_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename)
LodePNG_DecodeSettings settings
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean GLsizei const GLvoid pointer GLuint GLuint const GLchar name GLenum GLsizei GLenum GLsizei GLsizei height GLenum GLuint renderbuffer GLenum GLenum GLint params GLuint GLsizei range GLuint GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLsizei GLsizei GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLenum GLsizei length
#define DEFAULT_WINDOWSIZE
unsigned char time_defined
void LodePNG_Encoder_init(LodePNG_Encoder *encoder)
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
unsigned LodePNG_chunk_check_crc(const unsigned char *chunk)
unsigned text_compression
LodePNG_CompressSettings zlibsettings
#define READBIT(bitpointer, bitstream)
void LodePNG_Text_cleanup(LodePNG_Text *text)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat x
unsigned LodePNG_decode(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize, unsigned colorType, unsigned bitDepth)
unsigned LodePNG_encode(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h, unsigned colorType, unsigned bitDepth)
void LodePNG_UnknownChunks_init(LodePNG_UnknownChunks *chunks)
unsigned LodePNG_encode_file(const char *filename, const unsigned char *image, unsigned w, unsigned h, unsigned colorType, unsigned bitDepth)
void LodePNG_DecompressSettings_init(LodePNG_DecompressSettings *settings)
FW_CUDA_FUNC T sum(const VectorBase< T, L, S > &v)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat y
unsigned LodePNG_InfoColor_hasPaletteAlpha(const LodePNG_InfoColor *info)
unsigned LodePNG_Text_copy(LodePNG_Text *dest, const LodePNG_Text *source)
unsigned LodePNG_InfoColor_isPaletteType(const LodePNG_InfoColor *info)
unsigned LodePNG_InfoColor_addPalette(LodePNG_InfoColor *info, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v
unsigned LodePNG_UnknownChunks_copy(LodePNG_UnknownChunks *dest, const LodePNG_UnknownChunks *src)
void LodePNG_Encoder_cleanup(LodePNG_Encoder *encoder)
const String & getError(void)
unsigned char LodePNG_chunk_critical(const unsigned char *chunk)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes
const unsigned char * LodePNG_chunk_data_const(const unsigned char *chunk)
unsigned char LodePNG_chunk_safetocopy(const unsigned char *chunk)
FW_CUDA_FUNC S32 abs(S32 a)
void * realloc(void *ptr, size_t size)
#define NUM_DISTANCE_SYMBOLS
const LodePNG_DecompressSettings LodePNG_defaultDecompressSettings
unsigned LodePNG_chunk_length(const unsigned char *chunk)
#define LAST_LENGTH_CODE_INDEX
void LodePNG_Decoder_inspect(LodePNG_Decoder *decoder, const unsigned char *in, size_t inlength)
void LodePNG_Text_clear(LodePNG_Text *text)
void LodePNG_CompressSettings_init(LodePNG_CompressSettings *settings)
const unsigned char * LodePNG_chunk_next_const(const unsigned char *chunk)
CUdevice int ordinal char int len
void LodePNG_IText_cleanup(LodePNG_IText *text)
unsigned LodePNG_saveFile(const unsigned char *buffer, size_t buffersize, const char *filename)
unsigned LodePNG_decode24(unsigned char **out, unsigned *w, unsigned *h, const unsigned char *in, size_t insize)
void LodePNG_Decoder_decode(LodePNG_Decoder *decoder, unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize)
unsigned LodePNG_IText_copy(LodePNG_IText *dest, const LodePNG_IText *source)
void LodePNG_InfoRaw_init(LodePNG_InfoRaw *info)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint index
void LodePNG_InfoPng_cleanup(LodePNG_InfoPng *info)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei n
unsigned LodePNG_read32bitInt(const unsigned char *buffer)
#define NUM_CODE_LENGTH_CODES
unsigned LodePNG_convert(unsigned char *out, const unsigned char *in, LodePNG_InfoColor *infoOut, LodePNG_InfoColor *infoIn, unsigned w, unsigned h)
unsigned rememberUnknownChunks
unsigned LodePNG_encode24(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
unsigned background_defined
unsigned char LodePNG_chunk_type_equals(const unsigned char *chunk, const char *type)
unsigned LodePNG_decode24_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename)
#define LODEPNG_COMPILE_UNKNOWN_CHUNKS
unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor *dest, const LodePNG_InfoColor *source)
LodePNG_EncodeSettings settings
unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor *info)
unsigned LodePNG_append_chunk(unsigned char **out, size_t *outlength, const unsigned char *chunk)
unsigned LodePNG_decode_file(unsigned char **out, unsigned *w, unsigned *h, const char *filename, unsigned colorType, unsigned bitDepth)
#define FIRST_LENGTH_CODE_INDEX
unsigned char * LodePNG_chunk_data(unsigned char *chunk)
unsigned LodePNG_zlib_decompress(unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodePNG_DecompressSettings *settings)
unsigned compressionMethod
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void * p
unsigned LodePNG_InfoColor_canHaveAlpha(const LodePNG_InfoColor *info)
void LodePNG_Decoder_copy(LodePNG_Decoder *dest, const LodePNG_Decoder *source)
unsigned LodePNG_encode32(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
void LodePNG_chunk_generate_crc(unsigned char *chunk)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type
void LodePNG_IText_clear(LodePNG_IText *text)
unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor *info1, const LodePNG_InfoColor *info2)
unsigned LodePNG_IText_add(LodePNG_IText *text, const char *key, const char *langtag, const char *transkey, const char *str)
unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor *info)
void LodePNG_Encoder_encode(LodePNG_Encoder *encoder, unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw *dest, const LodePNG_InfoRaw *source)
void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw *info)
unsigned LodePNG_encode32_file(const char *filename, const unsigned char *image, unsigned w, unsigned h)
void LodePNG_InfoPng_init(LodePNG_InfoPng *info)
void LodePNG_Encoder_copy(LodePNG_Encoder *dest, const LodePNG_Encoder *source)
unsigned LodePNG_Text_add(LodePNG_Text *text, const char *key, const char *str)
void LodePNG_chunk_type(char type[5], const unsigned char *chunk)
unsigned char LodePNG_chunk_private(const unsigned char *chunk)
LodePNG_UnknownChunks unknown_chunks
#define CERROR_BREAK(errorvar, code)
void LodePNG_EncodeSettings_init(LodePNG_EncodeSettings *settings)
void LodePNG_DecodeSettings_init(LodePNG_DecodeSettings *settings)