summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lodepng.cpp589
1 files changed, 334 insertions, 255 deletions
diff --git a/lodepng.cpp b/lodepng.cpp
index d7486f3..b5d0b42 100644
--- a/lodepng.cpp
+++ b/lodepng.cpp
@@ -181,7 +181,7 @@ static unsigned uivector_resizev(uivector* p, size_t size, unsigned value)
{
size_t oldsize = p->size, i;
if(!uivector_resize(p, size)) return 0;
- for(i = oldsize; i < size; i++) p->data[i] = value;
+ for(i = oldsize; i < size; ++i) p->data[i] = value;
return 1;
}
@@ -205,7 +205,7 @@ static unsigned uivector_copy(uivector* p, const uivector* q)
{
size_t i;
if(!uivector_resize(p, q->size)) return 0;
- for(i = 0; i < q->size; i++) p->data[i] = q->data[i];
+ for(i = 0; i != q->size; ++i) p->data[i] = q->data[i];
return 1;
}
#endif /*LODEPNG_COMPILE_ENCODER*/
@@ -267,7 +267,7 @@ static unsigned ucvector_resizev(ucvector* p, size_t size, unsigned char value)
{
size_t oldsize = p->size, i;
if(!ucvector_resize(p, size)) return 0;
- for(i = oldsize; i < size; i++) p->data[i] = value;
+ for(i = oldsize; i < size; ++i) p->data[i] = value;
return 1;
}
#endif /*LODEPNG_COMPILE_DECODER*/
@@ -326,10 +326,17 @@ static void string_cleanup(char** out)
static void string_set(char** out, const char* in)
{
- size_t insize = strlen(in), i = 0;
+ size_t insize = strlen(in);
+#if !(__STDC_VERSION__ >= 199901L)
+ size_t i;
+#endif
if(string_resize(out, insize))
{
- for(i = 0; i < insize; i++)
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = 0; i != insize; ++i)
+#else
+ for(i = 0; i != insize; ++i)
+#endif
{
(*out)[i] = in[i];
}
@@ -425,19 +432,19 @@ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const
if(((*bitpointer) & 7) == 0) ucvector_push_back(bitstream, (unsigned char)0);\
/*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/\
(bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7));\
- (*bitpointer)++;\
+ ++(*bitpointer);\
}
static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
{
size_t i;
- for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1));
+ for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1));
}
static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits)
{
size_t i;
- for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1));
+ for(i = 0; i != nbits; ++i) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1));
}
#endif /*LODEPNG_COMPILE_ENCODER*/
@@ -448,17 +455,17 @@ static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, uns
static unsigned char readBitFromStream(size_t* bitpointer, const unsigned char* bitstream)
{
unsigned char result = (unsigned char)(READBIT(*bitpointer, bitstream));
- (*bitpointer)++;
+ ++(*bitpointer);
return result;
}
static unsigned readBitsFromStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
{
unsigned result = 0, i;
- for(i = 0; i < nbits; i++)
+ for(i = 0; i != nbits; ++i)
{
result += ((unsigned)READBIT(*bitpointer, bitstream)) << i;
- (*bitpointer)++;
+ ++(*bitpointer);
}
return result;
}
@@ -521,7 +528,7 @@ typedef struct HuffmanTree
static void HuffmanTree_draw(HuffmanTree* tree)
{
std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl;
- for(size_t i = 0; i < tree->tree1d.size; i++)
+ for(size_t i = 0; i != tree->tree1d.size; ++i)
{
if(tree->lengths.data[i])
std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl;
@@ -563,14 +570,14 @@ static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
There is only memory for such good tree currently, if there are more nodes
(due to too long length codes), error 55 will happen
*/
- for(n = 0; n < tree->numcodes * 2; n++)
+ for(n = 0; n < tree->numcodes * 2; ++n)
{
tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/
}
- for(n = 0; n < tree->numcodes; n++) /*the codes*/
+ for(n = 0; n < tree->numcodes; ++n) /*the codes*/
{
- for(i = 0; i < tree->lengths[n]; i++) /*the bits for this code*/
+ for(i = 0; i != tree->lengths[n]; ++i) /*the bits for this code*/
{
unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1);
/*oversubscribed, see comment in lodepng_error_text*/
@@ -586,7 +593,7 @@ static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
{
/*put address of the next step in here, first that address has to be found of course
(it's just nodefilled + 1)...*/
- nodefilled++;
+ ++nodefilled;
/*addresses encoded with numcodes added to it*/
tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes;
treepos = nodefilled;
@@ -596,7 +603,7 @@ static unsigned HuffmanTree_make2DTree(HuffmanTree* tree)
}
}
- for(n = 0; n < tree->numcodes * 2; n++)
+ for(n = 0; n < tree->numcodes * 2; ++n)
{
if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/
}
@@ -613,7 +620,10 @@ static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
{
uivector blcount;
uivector nextcode;
- unsigned bits, n, error = 0;
+ unsigned error = 0;
+#if !(__STDC_VERSION__ >= 199901L)
+ unsigned bits, n;
+#endif
uivector_init(&blcount);
uivector_init(&nextcode);
@@ -628,14 +638,26 @@ static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
if(!error)
{
/*step 1: count number of instances of each code length*/
- for(bits = 0; bits < tree->numcodes; bits++) blcount.data[tree->lengths[bits]]++;
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]];
+#else
+ for(bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]];
+#endif
/*step 2: generate the nextcode values*/
- for(bits = 1; bits <= tree->maxbitlen; bits++)
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned bits = 1; bits <= tree->maxbitlen; ++bits)
+#else
+ for(bits = 1; bits <= tree->maxbitlen; ++bits)
+#endif
{
nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1;
}
/*step 3: generate all the codes*/
- for(n = 0; n < tree->numcodes; n++)
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned n = 0; n != tree->numcodes; ++n)
+#else
+ for(n = 0; n != tree->numcodes; ++n)
+#endif
{
if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++;
}
@@ -659,7 +681,7 @@ static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* b
unsigned i;
tree->lengths = (unsigned*)lodepng_malloc(numcodes * sizeof(unsigned));
if(!tree->lengths) return 83; /*alloc fail*/
- for(i = 0; i < numcodes; i++) tree->lengths[i] = bitlen[i];
+ for(i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
tree->numcodes = (unsigned)numcodes; /*number of symbols*/
tree->maxbitlen = maxbitlen;
return HuffmanTree_makeFromLengths2(tree);
@@ -698,20 +720,20 @@ static void coin_copy(Coin* c1, const Coin* c2)
static void add_coins(Coin* c1, const Coin* c2)
{
size_t i;
- for(i = 0; i < c2->symbols.size; i++) uivector_push_back(&c1->symbols, c2->symbols.data[i]);
+ for(i = 0; i != c2->symbols.size; ++i) uivector_push_back(&c1->symbols, c2->symbols.data[i]);
c1->weight += c2->weight;
}
static void init_coins(Coin* coins, size_t num)
{
size_t i;
- for(i = 0; i < num; i++) coin_init(&coins[i]);
+ for(i = 0; i != num; ++i) coin_init(&coins[i]);
}
static void cleanup_coins(Coin* coins, size_t num)
{
size_t i;
- for(i = 0; i < num; i++) coin_cleanup(&coins[i]);
+ for(i = 0; i != num; ++i) coin_cleanup(&coins[i]);
}
static int coin_compare(const void* a, const void* b) {
@@ -724,13 +746,13 @@ static unsigned append_symbol_coins(Coin* coins, const unsigned* frequencies, un
{
unsigned i;
unsigned j = 0; /*index of present symbols*/
- for(i = 0; i < numcodes; i++)
+ for(i = 0; i != numcodes; ++i)
{
if(frequencies[i] != 0) /*only include symbols that are present*/
{
coins[j].weight = frequencies[i] / (float)sum;
uivector_push_back(&coins[j].symbols, i);
- j++;
+ ++j;
}
}
return 0;
@@ -749,16 +771,16 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
- for(i = 0; i < numcodes; i++)
+ for(i = 0; i != numcodes; ++i)
{
if(frequencies[i] > 0)
{
- numpresent++;
+ ++numpresent;
sum += frequencies[i];
}
}
- for(i = 0; i < numcodes; i++) lengths[i] = 0;
+ for(i = 0; i != numcodes; ++i) lengths[i] = 0;
/*ensure at least two present symbols. There should be at least one symbol
according to RFC 1951 section 3.2.7. To decoders incorrectly require two. To
@@ -771,7 +793,7 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
}
else if(numpresent == 1)
{
- for(i = 0; i < numcodes; i++)
+ for(i = 0; i != numcodes; ++i)
{
if(frequencies[i])
{
@@ -805,7 +827,7 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
if(!error)
{
unsigned numprev = 0;
- for(j = 1; j <= maxbitlen && !error; j++) /*each of the remaining rows*/
+ for(j = 1; j <= maxbitlen && !error; ++j) /*each of the remaining rows*/
{
unsigned tempnum;
Coin* tempcoins;
@@ -838,11 +860,11 @@ unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequen
if(!error)
{
- /*calculate the lenghts of each symbol, as the amount of times a coin of each symbol is used*/
- for(i = 0; i + 1 < numpresent; i++)
+ /*calculate the lengths of each symbol, as the amount of times a coin of each symbol is used*/
+ for(i = 0; i + 1 < numpresent; ++i)
{
Coin* coin = &coins[i];
- for(j = 0; j < coin->symbols.size; j++) lengths[coin->symbols.data[j]]++;
+ for(j = 0; j < coin->symbols.size; ++j) ++lengths[coin->symbols.data[j]];
}
}
@@ -860,7 +882,7 @@ static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigne
size_t mincodes, size_t numcodes, unsigned maxbitlen)
{
unsigned error = 0;
- while(!frequencies[numcodes - 1] && numcodes > mincodes) numcodes--; /*trim zeroes*/
+ while(!frequencies[numcodes - 1] && numcodes > mincodes) --numcodes; /*trim zeroes*/
tree->maxbitlen = maxbitlen;
tree->numcodes = (unsigned)numcodes; /*number of symbols*/
tree->lengths = (unsigned*)lodepng_realloc(tree->lengths, numcodes * sizeof(unsigned));
@@ -892,10 +914,10 @@ static unsigned generateFixedLitLenTree(HuffmanTree* tree)
if(!bitlen) return 83; /*alloc fail*/
/*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
- for(i = 0; i <= 143; i++) bitlen[i] = 8;
- for(i = 144; i <= 255; i++) bitlen[i] = 9;
- for(i = 256; i <= 279; i++) bitlen[i] = 7;
- for(i = 280; i <= 287; i++) bitlen[i] = 8;
+ for(i = 0; i <= 143; ++i) bitlen[i] = 8;
+ for(i = 144; i <= 255; ++i) bitlen[i] = 9;
+ for(i = 256; i <= 279; ++i) bitlen[i] = 7;
+ for(i = 280; i <= 287; ++i) bitlen[i] = 8;
error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
@@ -911,7 +933,7 @@ static unsigned generateFixedDistanceTree(HuffmanTree* tree)
if(!bitlen) return 83; /*alloc fail*/
/*there are 32 distance codes, but 30-31 are unused*/
- for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen[i] = 5;
+ for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
lodepng_free(bitlen);
@@ -936,7 +958,7 @@ static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp,
the expression below because this is the biggest bottleneck while decoding
*/
ct = codetree->tree2d[(treepos << 1) + READBIT(*bp, in)];
- (*bp)++;
+ ++(*bp);
if(ct < codetree->numcodes) return ct; /*the symbol is decoded, return it*/
else treepos = ct - codetree->numcodes; /*symbol not yet decoded, instead move tree position*/
@@ -995,7 +1017,7 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/);
- for(i = 0; i < NUM_CODE_LENGTH_CODES; i++)
+ for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i)
{
if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3);
else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/
@@ -1008,8 +1030,8 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
- for(i = 0; i < NUM_DEFLATE_CODE_SYMBOLS; i++) bitlen_ll[i] = 0;
- for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen_d[i] = 0;
+ for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0;
+ for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0;
/*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
i = 0;
@@ -1020,7 +1042,7 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
{
if(i < HLIT) bitlen_ll[i] = code;
else bitlen_d[i - HLIT] = code;
- i++;
+ ++i;
}
else if(code == 16) /*repeat previous*/
{
@@ -1035,12 +1057,12 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
if(i < HLIT + 1) value = bitlen_ll[i - 1];
else value = bitlen_d[i - HLIT - 1];
/*repeat this value in the next lengths*/
- for(n = 0; n < replength; n++)
+ for(n = 0; n < replength; ++n)
{
if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
if(i < HLIT) bitlen_ll[i] = value;
else bitlen_d[i - HLIT] = value;
- i++;
+ ++i;
}
}
else if(code == 17) /*repeat "0" 3-10 times*/
@@ -1050,13 +1072,13 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
replength += readBitsFromStream(bp, in, 3);
/*repeat this value in the next lengths*/
- for(n = 0; n < replength; n++)
+ for(n = 0; n < replength; ++n)
{
if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/
if(i < HLIT) bitlen_ll[i] = 0;
else bitlen_d[i - HLIT] = 0;
- i++;
+ ++i;
}
}
else if(code == 18) /*repeat "0" 11-138 times*/
@@ -1066,13 +1088,13 @@ static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
replength += readBitsFromStream(bp, in, 7);
/*repeat this value in the next lengths*/
- for(n = 0; n < replength; n++)
+ for(n = 0; n < replength; ++n)
{
if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/
if(i < HLIT) bitlen_ll[i] = 0;
else bitlen_d[i - HLIT] = 0;
- i++;
+ ++i;
}
}
else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
@@ -1131,7 +1153,7 @@ static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size
/*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/
if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/);
out->data[*pos] = (unsigned char)code_ll;
- (*pos)++;
+ ++(*pos);
}
else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/
{
@@ -1173,11 +1195,11 @@ static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size
backward = start - distance;
if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/);
- for(forward = 0; forward < length; forward++)
+ for(forward = 0; forward < length; ++forward)
{
out->data[(*pos)] = out->data[backward];
- (*pos)++;
- backward++;
+ ++(*pos);
+ ++backward;
if(backward >= start) backward = start - distance;
}
}
@@ -1206,7 +1228,7 @@ static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, siz
unsigned LEN, NLEN, n, error = 0;
/*go to first boundary of byte*/
- while(((*bp) & 0x7) != 0) (*bp)++;
+ while(((*bp) & 0x7) != 0) ++(*bp);
p = (*bp) / 8; /*byte position*/
/*read LEN (2 bytes) and NLEN (2 bytes)*/
@@ -1221,7 +1243,7 @@ static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, siz
/*read the literal data: LEN bytes are now stored in the out buffer*/
if(p + LEN > inlength) return 23; /*error: reading outside of in buffer*/
- for(n = 0; n < LEN; n++) out->data[(*pos)++] = in[p++];
+ for(n = 0; n < LEN; ++n) out->data[(*pos)++] = in[p++];
(*bp) = p * 8;
@@ -1306,7 +1328,7 @@ given array must be sorted (if no value is smaller, it returns the size of the g
static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value)
{
/*linear search implementation*/
- /*for(size_t i = 1; i < array_size; i++) if(array[i] > value) return i - 1;
+ /*for(size_t i = 1; i < array_size; ++i) if(array[i] > value) return i - 1;
return array_size - 1;*/
/*binary search implementation (not that much faster) (precondition: array_size > 0)*/
@@ -1377,12 +1399,12 @@ static unsigned hash_init(Hash* hash, unsigned windowsize)
}
/*initialize hash table*/
- for(i = 0; i < HASH_NUM_VALUES; i++) hash->head[i] = -1;
- for(i = 0; i < windowsize; i++) hash->val[i] = -1;
- for(i = 0; i < windowsize; i++) hash->chain[i] = i; /*same value as index indicates uninitialized*/
+ for(i = 0; i != HASH_NUM_VALUES; ++i) hash->head[i] = -1;
+ for(i = 0; i != windowsize; ++i) hash->val[i] = -1;
+ for(i = 0; i != windowsize; ++i) hash->chain[i] = i; /*same value as index indicates uninitialized*/
- for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; i++) hash->headz[i] = -1;
- for(i = 0; i < windowsize; i++) hash->chainz[i] = i; /*same value as index indicates uninitialized*/
+ for(i = 0; i <= MAX_SUPPORTED_DEFLATE_LENGTH; ++i) hash->headz[i] = -1;
+ for(i = 0; i != windowsize; ++i) hash->chainz[i] = i; /*same value as index indicates uninitialized*/
return 0;
}
@@ -1416,7 +1438,7 @@ static unsigned getHash(const unsigned char* data, size_t size, size_t pos)
size_t amount, i;
if(pos >= size) return 0;
amount = size - pos;
- for(i = 0; i < amount; i++) result ^= (unsigned)(data[pos + i] << (i * 8u));
+ for(i = 0; i != amount; ++i) result ^= (unsigned)(data[pos + i] << (i * 8u));
}
return result & HASH_BIT_MASK;
}
@@ -1427,7 +1449,7 @@ static unsigned countZeros(const unsigned char* data, size_t size, size_t pos)
const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH;
if(end > data + size) end = data + size;
data = start;
- while (data != end && *data == 0) data++;
+ while (data != end && *data == 0) ++data;
/*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/
return (unsigned)(data - start);
}
@@ -1481,7 +1503,7 @@ static unsigned encodeLZ77(uivector* out, Hash* hash,
if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
- for(pos = inpos; pos < insize; pos++)
+ for(pos = inpos; pos < insize; ++pos)
{
size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
unsigned chainlength = 0;
@@ -1491,7 +1513,7 @@ static unsigned encodeLZ77(uivector* out, Hash* hash,
if(usezeros && hashval == 0)
{
if (numzeros == 0) numzeros = countZeros(in, insize, pos);
- else if (pos + numzeros > insize || in[pos + numzeros - 1] != 0) numzeros--;
+ else if (pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
}
else
{
@@ -1585,7 +1607,7 @@ static unsigned encodeLZ77(uivector* out, Hash* hash,
offset = lazyoffset;
hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/
hash->headz[numzeros] = -1; /*idem*/
- pos--;
+ --pos;
}
}
}
@@ -1605,15 +1627,15 @@ static unsigned encodeLZ77(uivector* out, Hash* hash,
else
{
addLengthDistance(out, length, offset);
- for(i = 1; i < length; i++)
+ for(i = 1; i < length; ++i)
{
- pos++;
+ ++pos;
wpos = pos & (windowsize - 1);
hashval = getHash(in, insize, pos);
if(usezeros && hashval == 0)
{
if (numzeros == 0) numzeros = countZeros(in, insize, pos);
- else if (pos + numzeros > insize || in[pos + numzeros - 1] != 0) numzeros--;
+ else if (pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
}
else
{
@@ -1636,7 +1658,7 @@ static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, s
size_t i, j, numdeflateblocks = (datasize + 65534) / 65535;
unsigned datapos = 0;
- for(i = 0; i < numdeflateblocks; i++)
+ for(i = 0; i != numdeflateblocks; ++i)
{
unsigned BFINAL, BTYPE, LEN, NLEN;
unsigned char firstbyte;
@@ -1657,7 +1679,7 @@ static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, s
ucvector_push_back(out, (unsigned char)(NLEN / 256));
/*Decompressed data*/
- for(j = 0; j < 65535 && datapos < datasize; j++)
+ for(j = 0; j < 65535 && datapos < datasize; ++j)
{
ucvector_push_back(out, data[datapos++]);
}
@@ -1675,7 +1697,7 @@ static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encode
const HuffmanTree* tree_ll, const HuffmanTree* tree_d)
{
size_t i = 0;
- for(i = 0; i < lz77_encoded->size; i++)
+ for(i = 0; i != lz77_encoded->size; ++i)
{
unsigned val = lz77_encoded->data[i];
addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val));
@@ -1768,21 +1790,21 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
else
{
if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/);
- for(i = datapos; i < dataend; i++) lz77_encoded.data[i] = data[i]; /*no LZ77, but still will be Huffman compressed*/
+ for(i = datapos; i < dataend; ++i) lz77_encoded.data[i] = data[i]; /*no LZ77, but still will be Huffman compressed*/
}
if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/);
if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/);
/*Count the frequencies of lit, len and dist codes*/
- for(i = 0; i < lz77_encoded.size; i++)
+ for(i = 0; i != lz77_encoded.size; ++i)
{
unsigned symbol = lz77_encoded.data[i];
- frequencies_ll.data[symbol]++;
+ ++frequencies_ll.data[symbol];
if(symbol > 256)
{
unsigned dist = lz77_encoded.data[i + 2];
- frequencies_d.data[dist]++;
+ ++frequencies_d.data[dist];
i += 3;
}
}
@@ -1798,19 +1820,19 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286;
numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30;
/*store the code lengths of both generated trees in bitlen_lld*/
- for(i = 0; i < numcodes_ll; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i));
- for(i = 0; i < numcodes_d; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i));
+ for(i = 0; i != numcodes_ll; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i));
+ for(i = 0; i != numcodes_d; ++i) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i));
/*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times),
17 (3-10 zeroes), 18 (11-138 zeroes)*/
- for(i = 0; i < (unsigned)bitlen_lld.size; i++)
+ for(i = 0; i != (unsigned)bitlen_lld.size; ++i)
{
unsigned j = 0; /*amount of repititions*/
- while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) j++;
+ while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) ++j;
if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/
{
- j++; /*include the first zero*/
+ ++j; /*include the first zero*/
if(j <= 10) /*repeat code 17 supports max 10 zeroes*/
{
uivector_push_back(&bitlen_lld_e, 17);
@@ -1829,7 +1851,7 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
size_t k;
unsigned num = j / 6, rest = j % 6;
uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]);
- for(k = 0; k < num; k++)
+ for(k = 0; k < num; ++k)
{
uivector_push_back(&bitlen_lld_e, 16);
uivector_push_back(&bitlen_lld_e, 6 - 3);
@@ -1851,12 +1873,12 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
/*generate tree_cl, the huffmantree of huffmantrees*/
if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/);
- for(i = 0; i < bitlen_lld_e.size; i++)
+ for(i = 0; i != bitlen_lld_e.size; ++i)
{
- frequencies_cl.data[bitlen_lld_e.data[i]]++;
+ ++frequencies_cl.data[bitlen_lld_e.data[i]];
/*after a repeat code come the bits that specify the number of repetitions,
those don't need to be in the frequencies_cl calculation*/
- if(bitlen_lld_e.data[i] >= 16) i++;
+ if(bitlen_lld_e.data[i] >= 16) ++i;
}
error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data,
@@ -1864,7 +1886,7 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
if(error) break;
if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/);
- for(i = 0; i < tree_cl.numcodes; i++)
+ for(i = 0; i != tree_cl.numcodes; ++i)
{
/*lenghts of code length tree is in the order as specified by deflate*/
bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]);
@@ -1900,16 +1922,16 @@ static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash,
HDIST = (unsigned)(numcodes_d - 1);
HCLEN = (unsigned)bitlen_cl.size - 4;
/*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/
- while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) HCLEN--;
+ while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) --HCLEN;
addBitsToStream(bp, out, HLIT, 5);
addBitsToStream(bp, out, HDIST, 5);
addBitsToStream(bp, out, HCLEN, 4);
/*write the code lenghts of the code length alphabet*/
- for(i = 0; i < HCLEN + 4; i++) addBitsToStream(bp, out, bitlen_cl.data[i], 3);
+ for(i = 0; i != HCLEN + 4; ++i) addBitsToStream(bp, out, bitlen_cl.data[i], 3);
/*write the lenghts of the lit/len AND the dist alphabet*/
- for(i = 0; i < bitlen_lld_e.size; i++)
+ for(i = 0; i != bitlen_lld_e.size; ++i)
{
addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]),
HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i]));
@@ -1955,7 +1977,9 @@ static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash,
unsigned BFINAL = final;
unsigned error = 0;
+#if !(__STDC_VERSION__ >= 199901L)
size_t i;
+#endif
HuffmanTree_init(&tree_ll);
HuffmanTree_init(&tree_d);
@@ -1978,7 +2002,11 @@ static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash,
}
else /*no LZ77, but still will be Huffman compressed*/
{
- for(i = datapos; i < dataend; i++)
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = datapos; i < dataend; ++i)
+#else
+ for(i = datapos; i < dataend; ++i)
+#endif
{
addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i]));
}
@@ -2016,7 +2044,7 @@ static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t
error = hash_init(&hash, settings->windowsize);
if(error) return error;
- for(i = 0; i < numdeflateblocks && !error; i++)
+ for(i = 0; i != numdeflateblocks && !error; ++i)
{
unsigned final = (i == numdeflateblocks - 1);
size_t start = i * blocksize;
@@ -2079,7 +2107,7 @@ static unsigned update_adler32(unsigned adler, const unsigned char* data, unsign
{
s1 += (*data++);
s2 += s1;
- amount--;
+ --amount;
}
s1 %= 65521;
s2 %= 65521;
@@ -2168,12 +2196,13 @@ unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsig
/*initially, *out must be NULL and outsize 0, if you just give some random *out
that's pointing to a non allocated buffer, this'll crash*/
ucvector outv;
- size_t i;
unsigned error;
unsigned char* deflatedata = 0;
size_t deflatesize = 0;
+#if !(__STDC_VERSION__ >= 199901L)
+ size_t i;
+#endif
- unsigned ADLER32;
/*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
unsigned FLEVEL = 0;
@@ -2192,8 +2221,12 @@ unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsig
if(!error)
{
- ADLER32 = adler32(in, (unsigned)insize);
- for(i = 0; i < deflatesize; i++) ucvector_push_back(&outv, deflatedata[i]);
+ unsigned ADLER32 = adler32(in, (unsigned)insize);
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]);
+#else
+ for(i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]);
+#endif
lodepng_free(deflatedata);
lodepng_add32bitInt(&outv, ADLER32);
}
@@ -2337,7 +2370,7 @@ unsigned lodepng_crc32(const unsigned char* buf, size_t len)
unsigned c = 0xffffffffL;
size_t n;
- for(n = 0; n < len; n++)
+ for(n = 0; n < len; ++n)
{
c = lodepng_crc32_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
}
@@ -2351,15 +2384,19 @@ unsigned lodepng_crc32(const unsigned char* buf, size_t len)
static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream)
{
unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
- (*bitpointer)++;
+ ++(*bitpointer);
return result;
}
static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
{
unsigned result = 0;
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = nbits - 1; i < nbits; --i)
+#else
size_t i;
- for(i = nbits - 1; i < nbits; i--)
+ for(i = nbits - 1; i < nbits; --i)
+#endif
{
result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i;
}
@@ -2375,7 +2412,7 @@ static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream
/*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/
bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7)));
}
- (*bitpointer)++;
+ ++(*bitpointer);
}
#endif /*LODEPNG_COMPILE_DECODER*/
@@ -2384,7 +2421,7 @@ static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream,
/*the current bit in bitstream may be 0 or 1 for this to work*/
if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7))));
else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7)));
- (*bitpointer)++;
+ ++(*bitpointer);
}
/* ////////////////////////////////////////////////////////////////////////// */
@@ -2399,7 +2436,7 @@ unsigned lodepng_chunk_length(const unsigned char* chunk)
void lodepng_chunk_type(char type[5], const unsigned char* chunk)
{
unsigned i;
- for(i = 0; i < 4; i++) type[i] = (char)chunk[4 + i];
+ for(i = 0; i != 4; ++i) type[i] = (char)chunk[4 + i];
type[4] = 0; /*null termination char*/
}
@@ -2477,7 +2514,7 @@ unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsi
(*outlength) = new_length;
chunk_start = &(*out)[new_length - total_chunk_length];
- for(i = 0; i < total_chunk_length; i++) chunk_start[i] = chunk[i];
+ for(i = 0; i != total_chunk_length; ++i) chunk_start[i] = chunk[i];
return 0;
}
@@ -2505,7 +2542,7 @@ unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned l
chunk[7] = (unsigned char)type[3];
/*3: the data*/
- for(i = 0; i < length; i++) chunk[8 + i] = data[i];
+ for(i = 0; i != length; ++i) chunk[8 + i] = data[i];
/*4: CRC (of the chunkname characters and the data)*/
lodepng_chunk_generate_crc(chunk);
@@ -2570,14 +2607,20 @@ void lodepng_color_mode_cleanup(LodePNGColorMode* info)
unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source)
{
+#if !(__STDC_VERSION__ >= 199901L)
size_t i;
+#endif
lodepng_color_mode_cleanup(dest);
*dest = *source;
if(source->palette)
{
dest->palette = (unsigned char*)lodepng_malloc(1024);
if(!dest->palette && source->palettesize) return 83; /*alloc fail*/
- for(i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i];
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i];
+#else
+ for(i = 0; i != source->palettesize * 4; ++i) dest->palette[i] = source->palette[i];
+#endif
}
return 0;
}
@@ -2595,7 +2638,7 @@ static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColo
if(a->key_b != b->key_b) return 0;
}
if(a->palettesize != b->palettesize) return 0;
- for(i = 0; i < a->palettesize * 4; i++)
+ for(i = 0; i != a->palettesize * 4; ++i)
{
if(a->palette[i] != b->palette[i]) return 0;
}
@@ -2626,7 +2669,7 @@ unsigned lodepng_palette_add(LodePNGColorMode* info,
info->palette[4 * info->palettesize + 1] = g;
info->palette[4 * info->palettesize + 2] = b;
info->palette[4 * info->palettesize + 3] = a;
- info->palettesize++;
+ ++info->palettesize;
return 0;
}
@@ -2659,7 +2702,7 @@ unsigned lodepng_is_palette_type(const LodePNGColorMode* info)
unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info)
{
size_t i;
- for(i = 0; i < info->palettesize; i++)
+ for(i = 0; i != info->palettesize; ++i)
{
if(info->palette[i * 4 + 3] < 255) return 1;
}
@@ -2699,14 +2742,14 @@ static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, const LodePNGCol
static void LodePNGUnknownChunks_init(LodePNGInfo* info)
{
unsigned i;
- for(i = 0; i < 3; i++) info->unknown_chunks_data[i] = 0;
- for(i = 0; i < 3; i++) info->unknown_chunks_size[i] = 0;
+ for(i = 0; i != 3; ++i) info->unknown_chunks_data[i] = 0;
+ for(i = 0; i != 3; ++i) info->unknown_chunks_size[i] = 0;
}
static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info)
{
unsigned i;
- for(i = 0; i < 3; i++) lodepng_free(info->unknown_chunks_data[i]);
+ for(i = 0; i != 3; ++i) lodepng_free(info->unknown_chunks_data[i]);
}
static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src)
@@ -2715,13 +2758,13 @@ static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo*
LodePNGUnknownChunks_cleanup(dest);
- for(i = 0; i < 3; i++)
+ for(i = 0; i != 3; ++i)
{
size_t j;
dest->unknown_chunks_size[i] = src->unknown_chunks_size[i];
dest->unknown_chunks_data[i] = (unsigned char*)lodepng_malloc(src->unknown_chunks_size[i]);
if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/
- for(j = 0; j < src->unknown_chunks_size[i]; j++)
+ for(j = 0; j < src->unknown_chunks_size[i]; ++j)
{
dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j];
}
@@ -2742,7 +2785,7 @@ static void LodePNGText_init(LodePNGInfo* info)
static void LodePNGText_cleanup(LodePNGInfo* info)
{
size_t i;
- for(i = 0; i < info->text_num; i++)
+ for(i = 0; i != info->text_num; ++i)
{
string_cleanup(&info->text_keys[i]);
string_cleanup(&info->text_strings[i]);
@@ -2757,7 +2800,7 @@ static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source)
dest->text_keys = 0;
dest->text_strings = 0;
dest->text_num = 0;
- for(i = 0; i < source->text_num; i++)
+ for(i = 0; i != source->text_num; ++i)
{
CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i]));
}
@@ -2780,7 +2823,7 @@ unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str)
return 83; /*alloc fail*/
}
- info->text_num++;
+ ++info->text_num;
info->text_keys = new_keys;
info->text_strings = new_strings;
@@ -2807,7 +2850,7 @@ static void LodePNGIText_init(LodePNGInfo* info)
static void LodePNGIText_cleanup(LodePNGInfo* info)
{
size_t i;
- for(i = 0; i < info->itext_num; i++)
+ for(i = 0; i != info->itext_num; ++i)
{
string_cleanup(&info->itext_keys[i]);
string_cleanup(&info->itext_langtags[i]);
@@ -2828,7 +2871,7 @@ static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source)
dest->itext_transkeys = 0;
dest->itext_strings = 0;
dest->itext_num = 0;
- for(i = 0; i < source->itext_num; i++)
+ for(i = 0; i != source->itext_num; ++i)
{
CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i],
source->itext_transkeys[i], source->itext_strings[i]));
@@ -2857,7 +2900,7 @@ unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langt
return 83; /*alloc fail*/
}
- info->itext_num++;
+ ++info->itext_num;
info->itext_keys = new_keys;
info->itext_langtags = new_langtags;
info->itext_transkeys = new_transkeys;
@@ -2965,14 +3008,14 @@ struct ColorTree
static void color_tree_init(ColorTree* tree)
{
int i;
- for(i = 0; i < 16; i++) tree->children[i] = 0;
+ for(i = 0; i != 16; ++i) tree->children[i] = 0;
tree->index = -1;
}
static void color_tree_cleanup(ColorTree* tree)
{
int i;
- for(i = 0; i < 16; i++)
+ for(i = 0; i != 16; ++i)
{
if(tree->children[i])
{
@@ -2986,7 +3029,7 @@ static void color_tree_cleanup(ColorTree* tree)
static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
int bit = 0;
- for(bit = 0; bit < 8; bit++)
+ for(bit = 0; bit < 8; ++bit)
{
int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
if(!tree->children[i]) return -1;
@@ -3008,7 +3051,7 @@ static void color_tree_add(ColorTree* tree,
unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index)
{
int bit;
- for(bit = 0; bit < 8; bit++)
+ for(bit = 0; bit < 8; ++bit)
{
int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
if(!tree->children[i])
@@ -3257,7 +3300,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
if(mode->bitdepth == 8)
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = buffer[1] = buffer[2] = in[i];
if(has_alpha) buffer[3] = mode->key_defined && in[i] == mode->key_r ? 0 : 255;
@@ -3265,7 +3308,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
}
else if(mode->bitdepth == 16)
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = buffer[1] = buffer[2] = in[i * 2];
if(has_alpha) buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255;
@@ -3275,7 +3318,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
size_t j = 0;
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
@@ -3287,7 +3330,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
if(mode->bitdepth == 8)
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = in[i * 3 + 0];
buffer[1] = in[i * 3 + 1];
@@ -3298,7 +3341,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
}
else
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = in[i * 6 + 0];
buffer[1] = in[i * 6 + 2];
@@ -3314,7 +3357,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
unsigned index;
size_t j = 0;
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
if(mode->bitdepth == 8) index = in[i];
else index = readBitsFromReversedStream(&j, in, mode->bitdepth);
@@ -3339,7 +3382,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
if(mode->bitdepth == 8)
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
if(has_alpha) buffer[3] = in[i * 2 + 1];
@@ -3347,7 +3390,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
}
else
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
if(has_alpha) buffer[3] = in[i * 4 + 2];
@@ -3358,7 +3401,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
{
if(mode->bitdepth == 8)
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = in[i * 4 + 0];
buffer[1] = in[i * 4 + 1];
@@ -3368,7 +3411,7 @@ static void getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels,
}
else
{
- for(i = 0; i < numpixels; i++, buffer += num_channels)
+ for(i = 0; i != numpixels; ++i, buffer += num_channels)
{
buffer[0] = in[i * 8 + 0];
buffer[1] = in[i * 8 + 2];
@@ -3425,7 +3468,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
if(lodepng_color_mode_equal(mode_out, mode_in))
{
size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
- for(i = 0; i < numbytes; i++) out[i] = in[i];
+ for(i = 0; i != numbytes; ++i) out[i] = in[i];
return 0;
}
@@ -3434,7 +3477,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
size_t palsize = 1u << mode_out->bitdepth;
if(mode_out->palettesize < palsize) palsize = mode_out->palettesize;
color_tree_init(&tree);
- for(i = 0; i < palsize; i++)
+ for(i = 0; i != palsize; ++i)
{
unsigned char* p = &mode_out->palette[i * 4];
color_tree_add(&tree, p[0], p[1], p[2], p[3], i);
@@ -3443,7 +3486,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16)
{
- for(i = 0; i < numpixels; i++)
+ for(i = 0; i != numpixels; ++i)
{
unsigned short r = 0, g = 0, b = 0, a = 0;
getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
@@ -3461,7 +3504,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
else
{
unsigned char r = 0, g = 0, b = 0, a = 0;
- for(i = 0; i < numpixels; i++)
+ for(i = 0; i != numpixels; ++i)
{
getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
@@ -3536,7 +3579,7 @@ unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
if(mode->bitdepth == 16)
{
unsigned short r, g, b, a;
- for(i = 0; i < numpixels; i++)
+ for(i = 0; i != numpixels; ++i)
{
getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
if((r & 255u) != ((r >> 8) & 255u) || (g & 255u) != ((g >> 8) & 255u) ||
@@ -3554,7 +3597,7 @@ unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
profile->bits = 16;
bits_done = numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/
- for(i = 0; i < numpixels; i++)
+ for(i = 0; i != numpixels; ++i)
{
getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
@@ -3593,7 +3636,7 @@ unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
}
else /* < 16-bit */
{
- for(i = 0; i < numpixels; i++)
+ for(i = 0; i != numpixels; ++i)
{
unsigned char r = 0, g = 0, b = 0, a = 0;
getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode);
@@ -3652,7 +3695,7 @@ unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
p[n * 4 + 2] = b;
p[n * 4 + 3] = a;
}
- profile->numcolors++;
+ ++profile->numcolors;
numcolors_done = profile->numcolors >= maxnumcolors;
}
}
@@ -3681,7 +3724,10 @@ unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out,
{
LodePNGColorProfile prof;
unsigned error = 0;
- unsigned i, n, palettebits, grey_ok, palette_ok;
+ unsigned n, palettebits, grey_ok, palette_ok;
+#if !(__STDC_VERSION__ >= 199901L)
+ unsigned i;
+#endif
lodepng_color_profile_init(&prof);
error = lodepng_get_color_profile(&prof, image, w, h, mode_in);
@@ -3703,7 +3749,11 @@ unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out,
{
unsigned char* p = prof.palette;
lodepng_palette_clear(mode_out); /*remove potential earlier palette*/
- for(i = 0; i < prof.numcolors; i++)
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned i = 0; i != prof.numcolors; ++i)
+#else
+ for(i = 0; i != prof.numcolors; ++i)
+#endif
{
error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
if(error) break;
@@ -3786,7 +3836,7 @@ static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t fil
unsigned i;
/*calculate width and height in pixels of each pass*/
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
@@ -3795,7 +3845,7 @@ static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t fil
}
filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
/*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/
filter_passstart[i + 1] = filter_passstart[i]
@@ -3892,53 +3942,53 @@ static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scan
switch(filterType)
{
case 0:
- for(i = 0; i < length; i++) recon[i] = scanline[i];
+ for(i = 0; i != length; ++i) recon[i] = scanline[i];
break;
case 1:
- for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
- for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth];
+ for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
+ for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth];
break;
case 2:
if(precon)
{
- for(i = 0; i < length; i++) recon[i] = scanline[i] + precon[i];
+ for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i];
}
else
{
- for(i = 0; i < length; i++) recon[i] = scanline[i];
+ for(i = 0; i != length; ++i) recon[i] = scanline[i];
}
break;
case 3:
if(precon)
{
- for(i = 0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2;
- for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
+ for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + precon[i] / 2;
+ for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
}
else
{
- for(i = 0; i < bytewidth; i++) recon[i] = scanline[i];
- for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
+ for(i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
+ for(i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
}
break;
case 4:
if(precon)
{
- for(i = 0; i < bytewidth; i++)
+ for(i = 0; i != bytewidth; ++i)
{
recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/
}
- for(i = bytewidth; i < length; i++)
+ for(i = bytewidth; i < length; ++i)
{
recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
}
}
else
{
- for(i = 0; i < bytewidth; i++)
+ for(i = 0; i != bytewidth; ++i)
{
recon[i] = scanline[i];
}
- for(i = bytewidth; i < length; i++)
+ for(i = bytewidth; i < length; ++i)
{
/*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/
recon[i] = (scanline[i] + recon[i - bytewidth]);
@@ -3967,7 +4017,7 @@ static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w
size_t bytewidth = (bpp + 7) / 8;
size_t linebytes = (w * bpp + 7) / 8;
- for(y = 0; y < h; y++)
+ for(y = 0; y < h; ++y)
{
size_t outindex = linebytes * y;
size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
@@ -4002,16 +4052,16 @@ static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsig
if(bpp >= 8)
{
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
unsigned x, y, b;
size_t bytewidth = bpp / 8;
- for(y = 0; y < passh[i]; y++)
- for(x = 0; x < passw[i]; x++)
+ for(y = 0; y < passh[i]; ++y)
+ for(x = 0; x < passw[i]; ++x)
{
size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
- for(b = 0; b < bytewidth; b++)
+ for(b = 0; b < bytewidth; ++b)
{
out[pixeloutstart + b] = in[pixelinstart + b];
}
@@ -4020,18 +4070,18 @@ static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsig
}
else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
{
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
unsigned x, y, b;
unsigned ilinebits = bpp * passw[i];
unsigned olinebits = bpp * w;
size_t obp, ibp; /*bit pointers (for out and in buffer)*/
- for(y = 0; y < passh[i]; y++)
- for(x = 0; x < passw[i]; x++)
+ for(y = 0; y < passh[i]; ++y)
+ for(x = 0; x < passw[i]; ++x)
{
ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
- for(b = 0; b < bpp; b++)
+ for(b = 0; b < bpp; ++b)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
/*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/
@@ -4057,10 +4107,10 @@ static void removePaddingBits(unsigned char* out, const unsigned char* in,
unsigned y;
size_t diff = ilinebits - olinebits;
size_t ibp = 0, obp = 0; /*input and output bit pointers*/
- for(y = 0; y < h; y++)
+ for(y = 0; y < h; ++y)
{
size_t x;
- for(x = 0; x < olinebits; x++)
+ for(x = 0; x < olinebits; ++x)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
setBitOfReversedStream(&obp, out, bit);
@@ -4102,7 +4152,7 @@ static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
/*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline,
@@ -4124,7 +4174,10 @@ static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
{
- unsigned pos = 0, i;
+ unsigned pos = 0;
+#if !(__STDC_VERSION__ >= 199901L)
+ unsigned i;
+#endif
if(color->palette) lodepng_free(color->palette);
color->palettesize = chunkLength / 3;
color->palette = (unsigned char*)lodepng_malloc(4 * color->palettesize);
@@ -4135,7 +4188,11 @@ static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* dat
}
if(color->palettesize > 256) return 38; /*error: palette too big*/
- for(i = 0; i < color->palettesize; i++)
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned i = 0; i != color->palettesize; ++i)
+#else
+ for(i = 0; i != color->palettesize; ++i)
+#endif
{
color->palette[4 * i + 0] = data[pos++]; /*R*/
color->palette[4 * i + 1] = data[pos++]; /*G*/
@@ -4148,13 +4205,19 @@ static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* dat
static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
{
+#if !(__STDC_VERSION__ >= 199901L)
unsigned i;
+#endif
if(color->colortype == LCT_PALETTE)
{
/*error: more alpha values given than there are palette entries*/
if(chunkLength > color->palettesize) return 38;
- for(i = 0; i < chunkLength; i++) color->palette[4 * i + 3] = data[i];
+#if __STDC_VERSION__ >= 199901L
+ for(unsigned i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i];
+#else
+ for(i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i];
+#endif
}
else if(color->colortype == LCT_GREY)
{
@@ -4226,7 +4289,7 @@ static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, siz
unsigned length, string2_begin;
length = 0;
- while(length < chunkLength && data[length] != 0) length++;
+ while(length < chunkLength && data[length] != 0) ++length;
/*even though it's not allowed by the standard, no error is thrown if
there's no null termination char, if the text is empty*/
if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
@@ -4235,7 +4298,7 @@ static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, siz
if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
key[length] = 0;
- for(i = 0; i < length; i++) key[i] = (char)data[i];
+ for(i = 0; i != length; ++i) key[i] = (char)data[i];
string2_begin = length + 1; /*skip keyword null terminator*/
@@ -4244,7 +4307,7 @@ static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, siz
if(!str) CERROR_BREAK(error, 83); /*alloc fail*/
str[length] = 0;
- for(i = 0; i < length; i++) str[i] = (char)data[string2_begin + i];
+ for(i = 0; i != length; ++i) str[i] = (char)data[string2_begin + i];
error = lodepng_add_text(info, key, str);
@@ -4272,7 +4335,7 @@ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSetting
while(!error) /*not really a while loop, only used to break on error*/
{
- for(length = 0; length < chunkLength && data[length] != 0; length++) ;
+ for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/
if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
@@ -4280,7 +4343,7 @@ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSetting
if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
key[length] = 0;
- for(i = 0; i < length; i++) key[i] = (char)data[i];
+ for(i = 0; i != length; ++i) key[i] = (char)data[i];
if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/
@@ -4325,7 +4388,7 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting
if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/
/*read the key*/
- for(length = 0; length < chunkLength && data[length] != 0; length++) ;
+ for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/
if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/
@@ -4333,7 +4396,7 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting
if(!key) CERROR_BREAK(error, 83); /*alloc fail*/
key[length] = 0;
- for(i = 0; i < length; i++) key[i] = (char)data[i];
+ for(i = 0; i != length; ++i) key[i] = (char)data[i];
/*read the compression method*/
compressed = data[length + 1];
@@ -4345,24 +4408,24 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting
/*read the langtag*/
begin = length + 3;
length = 0;
- for(i = begin; i < chunkLength && data[i] != 0; i++) length++;
+ for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
langtag = (char*)lodepng_malloc(length + 1);
if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/
langtag[length] = 0;
- for(i = 0; i < length; i++) langtag[i] = (char)data[begin + i];
+ for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i];
/*read the transkey*/
begin += length + 1;
length = 0;
- for(i = begin; i < chunkLength && data[i] != 0; i++) length++;
+ for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
transkey = (char*)lodepng_malloc(length + 1);
if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/
transkey[length] = 0;
- for(i = 0; i < length; i++) transkey[i] = (char)data[begin + i];
+ for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i];
/*read the actual text*/
begin += length + 1;
@@ -4384,7 +4447,7 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting
if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/);
decoded.data[length] = 0;
- for(i = 0; i < length; i++) decoded.data[i] = data[begin + i];
+ for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i];
}
error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data);
@@ -4491,7 +4554,7 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
{
size_t oldsize = idat.size;
if(!ucvector_resize(&idat, oldsize + chunkLength)) CERROR_BREAK(state->error, 83 /*alloc fail*/);
- for(i = 0; i < chunkLength; i++) idat.data[oldsize + i] = data[i];
+ for(i = 0; i != chunkLength; ++i) idat.data[oldsize + i] = data[i];
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
critical_pos = 3;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
@@ -4816,10 +4879,16 @@ static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h,
static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info)
{
unsigned error = 0;
+#if !(__STDC_VERSION__ >= 199901L)
size_t i;
+#endif
ucvector PLTE;
ucvector_init(&PLTE);
- for(i = 0; i < info->palettesize * 4; i++)
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = 0; i != info->palettesize * 4; ++i)
+#else
+ for(i = 0; i != info->palettesize * 4; ++i)
+#endif
{
/*add all channels except alpha channel*/
if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]);
@@ -4833,20 +4902,30 @@ static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info)
static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info)
{
unsigned error = 0;
+#if !(__STDC_VERSION__ >= 199901L)
size_t i;
+#endif
ucvector tRNS;
ucvector_init(&tRNS);
if(info->colortype == LCT_PALETTE)
{
size_t amount = info->palettesize;
/*the tail of palette values that all have 255 as alpha, does not have to be encoded*/
- for(i = info->palettesize; i > 0; i--)
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = info->palettesize; i != 0; --i)
+#else
+ for(i = info->palettesize; i != 0; --i)
+#endif
{
- if(info->palette[4 * (i - 1) + 3] == 255) amount--;
+ if(info->palette[4 * (i - 1) + 3] == 255) --amount;
else break;
}
/*add only alpha channel*/
- for(i = 0; i < amount; i++) ucvector_push_back(&tRNS, info->palette[4 * i + 3]);
+#if __STDC_VERSION__ >= 199901L
+ for(size_t i = 0; i != amount; ++i) ucvector_push_back(&tRNS, info->palette[4 * i + 3]);
+#else
+ for(i = 0; i != amount; ++i) ucvector_push_back(&tRNS, info->palette[4 * i + 3]);
+#endif
}
else if(info->colortype == LCT_GREY)
{
@@ -4905,10 +4984,10 @@ static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* te
size_t i;
ucvector text;
ucvector_init(&text);
- for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&text, (unsigned char)keyword[i]);
+ for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)keyword[i]);
if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
ucvector_push_back(&text, 0); /*0 termination char*/
- for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&text, (unsigned char)textstring[i]);
+ for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&text, (unsigned char)textstring[i]);
error = addChunk(out, "tEXt", text.data, text.size);
ucvector_cleanup(&text);
@@ -4924,7 +5003,7 @@ static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* te
ucvector_init(&data);
ucvector_init(&compressed);
- for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (unsigned char)keyword[i]);
+ for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]);
if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
ucvector_push_back(&data, 0); /*0 termination char*/
ucvector_push_back(&data, 0); /*compression method: 0*/
@@ -4933,7 +5012,7 @@ static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* te
(unsigned char*)textstring, textsize, zlibsettings);
if(!error)
{
- for(i = 0; i < compressed.size; i++) ucvector_push_back(&data, compressed.data[i]);
+ for(i = 0; i != compressed.size; ++i) ucvector_push_back(&data, compressed.data[i]);
error = addChunk(out, "zTXt", data.data, data.size);
}
@@ -4951,14 +5030,14 @@ static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* ke
ucvector_init(&data);
- for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (unsigned char)keyword[i]);
+ for(i = 0; keyword[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)keyword[i]);
if(i < 1 || i > 79) return 89; /*error: invalid keyword size*/
ucvector_push_back(&data, 0); /*null termination char*/
ucvector_push_back(&data, compressed ? 1 : 0); /*compression flag*/
ucvector_push_back(&data, 0); /*compression method*/
- for(i = 0; langtag[i] != 0; i++) ucvector_push_back(&data, (unsigned char)langtag[i]);
+ for(i = 0; langtag[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)langtag[i]);
ucvector_push_back(&data, 0); /*null termination char*/
- for(i = 0; transkey[i] != 0; i++) ucvector_push_back(&data, (unsigned char)transkey[i]);
+ for(i = 0; transkey[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)transkey[i]);
ucvector_push_back(&data, 0); /*null termination char*/
if(compressed)
@@ -4969,13 +5048,13 @@ static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* ke
(unsigned char*)textstring, textsize, zlibsettings);
if(!error)
{
- for(i = 0; i < compressed_data.size; i++) ucvector_push_back(&data, compressed_data.data[i]);
+ for(i = 0; i != compressed_data.size; ++i) ucvector_push_back(&data, compressed_data.data[i]);
}
ucvector_cleanup(&compressed_data);
}
else /*not compressed*/
{
- for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&data, (unsigned char)textstring[i]);
+ for(i = 0; textstring[i] != 0; ++i) ucvector_push_back(&data, (unsigned char)textstring[i]);
}
if(!error) error = addChunk(out, "iTXt", data.data, data.size);
@@ -5055,49 +5134,49 @@ static void filterScanline(unsigned char* out, const unsigned char* scanline, co
switch(filterType)
{
case 0: /*None*/
- for(i = 0; i < length; i++) out[i] = scanline[i];
+ for(i = 0; i != length; ++i) out[i] = scanline[i];
break;
case 1: /*Sub*/
- for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
- for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth];
+ for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
+ for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth];
break;
case 2: /*Up*/
if(prevline)
{
- for(i = 0; i < length; i++) out[i] = scanline[i] - prevline[i];
+ for(i = 0; i != length; ++i) out[i] = scanline[i] - prevline[i];
}
else
{
- for(i = 0; i < length; i++) out[i] = scanline[i];
+ for(i = 0; i != length; ++i) out[i] = scanline[i];
}
break;
case 3: /*Average*/
if(prevline)
{
- for(i = 0; i < bytewidth; i++) out[i] = scanline[i] - prevline[i] / 2;
- for(i = bytewidth; i < length; i++) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) / 2);
+ for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - prevline[i] / 2;
+ for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) / 2);
}
else
{
- for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
- for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth] / 2;
+ for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
+ for(i = bytewidth; i < length; ++i) out[i] = scanline[i] - scanline[i - bytewidth] / 2;
}
break;
case 4: /*Paeth*/
if(prevline)
{
/*paethPredictor(0, prevline[i], 0) is always prevline[i]*/
- for(i = 0; i < bytewidth; i++) out[i] = (scanline[i] - prevline[i]);
- for(i = bytewidth; i < length; i++)
+ for(i = 0; i != bytewidth; ++i) out[i] = (scanline[i] - prevline[i]);
+ for(i = bytewidth; i < length; ++i)
{
out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth]));
}
}
else
{
- for(i = 0; i < bytewidth; i++) out[i] = scanline[i];
+ for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
/*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/
- for(i = bytewidth; i < length; i++) out[i] = (scanline[i] - scanline[i - bytewidth]);
+ for(i = bytewidth; i < length; ++i) out[i] = (scanline[i] - scanline[i - bytewidth]);
}
break;
default: return; /*unexisting filter type given*/
@@ -5109,7 +5188,7 @@ static float flog2(float f)
{
float result = 0;
while(f > 32) { result += 4; f /= 16; }
- while(f > 2) { result++; f /= 2; }
+ while(f > 2) { ++result; f /= 2; }
return result + 1.442695f * (f * f * f / 3 - 3 * f * f / 2 + 3 * f - 1.83333f);
}
@@ -5152,7 +5231,7 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
if(strategy == LFS_ZERO)
{
- for(y = 0; y < h; y++)
+ for(y = 0; y != h; ++y)
{
size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
size_t inindex = linebytes * y;
@@ -5169,7 +5248,7 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
size_t smallest = 0;
unsigned char type, bestType = 0;
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
ucvector_init(&attempt[type]);
if(!ucvector_resize(&attempt[type], linebytes)) return 83; /*alloc fail*/
@@ -5177,10 +5256,10 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
if(!error)
{
- for(y = 0; y < h; y++)
+ for(y = 0; y != h; ++y)
{
/*try the 5 filter types*/
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type);
@@ -5188,11 +5267,11 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
sum[type] = 0;
if(type == 0)
{
- for(x = 0; x < linebytes; x++) sum[type] += (unsigned char)(attempt[type].data[x]);
+ for(x = 0; x != linebytes; ++x) sum[type] += (unsigned char)(attempt[type].data[x]);
}
else
{
- for(x = 0; x < linebytes; x++)
+ for(x = 0; x != linebytes; ++x)
{
/*For differences, each byte should be treated as signed, values above 127 are negative
(converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there.
@@ -5214,11 +5293,11 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
/*now fill the out values*/
out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
- for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
+ for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
}
}
- for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
+ for(type = 0; type != 5; ++type) ucvector_cleanup(&attempt[type]);
}
else if(strategy == LFS_ENTROPY)
{
@@ -5228,23 +5307,23 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
unsigned type, bestType = 0;
unsigned count[256];
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
ucvector_init(&attempt[type]);
if(!ucvector_resize(&attempt[type], linebytes)) return 83; /*alloc fail*/
}
- for(y = 0; y < h; y++)
+ for(y = 0; y != h; ++y)
{
/*try the 5 filter types*/
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type);
- for(x = 0; x < 256; x++) count[x] = 0;
- for(x = 0; x < linebytes; x++) count[attempt[type].data[x]]++;
- count[type]++; /*the filter type itself is part of the scanline*/
+ for(x = 0; x != 256; ++x) count[x] = 0;
+ for(x = 0; x != linebytes; ++x) ++count[attempt[type].data[x]];
+ ++count[type]; /*the filter type itself is part of the scanline*/
sum[type] = 0;
- for(x = 0; x < 256; x++)
+ for(x = 0; x != 256; ++x)
{
float p = count[x] / (float)(linebytes + 1);
sum[type] += count[x] == 0 ? 0 : flog2(1 / p) * p;
@@ -5261,14 +5340,14 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
/*now fill the out values*/
out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
- for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
+ for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
}
- for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
+ for(type = 0; type != 5; ++type) ucvector_cleanup(&attempt[type]);
}
else if(strategy == LFS_PREDEFINED)
{
- for(y = 0; y < h; y++)
+ for(y = 0; y != h; ++y)
{
size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
size_t inindex = linebytes * y;
@@ -5298,14 +5377,14 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
images only, so disable it*/
zlibsettings.custom_zlib = 0;
zlibsettings.custom_deflate = 0;
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
ucvector_init(&attempt[type]);
ucvector_resize(&attempt[type], linebytes); /*todo: give error if resize failed*/
}
- for(y = 0; y < h; y++) /*try the 5 filter types*/
+ for(y = 0; y != h; ++y) /*try the 5 filter types*/
{
- for(type = 0; type < 5; type++)
+ for(type = 0; type != 5; ++type)
{
unsigned testsize = attempt[type].size;
/*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/
@@ -5324,9 +5403,9 @@ static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w,
}
prevline = &in[y * linebytes];
out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/
- for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
+ for(x = 0; x != linebytes; ++x) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x];
}
- for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]);
+ for(type = 0; type != 5; ++type) ucvector_cleanup(&attempt[type]);
}
else return 88; /* unknown filter strategy */
@@ -5341,17 +5420,17 @@ static void addPaddingBits(unsigned char* out, const unsigned char* in,
unsigned y;
size_t diff = olinebits - ilinebits;
size_t obp = 0, ibp = 0; /*bit pointers*/
- for(y = 0; y < h; y++)
+ for(y = 0; y != h; ++y)
{
size_t x;
- for(x = 0; x < ilinebits; x++)
+ for(x = 0; x < ilinebits; ++x)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
setBitOfReversedStream(&obp, out, bit);
}
/*obp += diff; --> no, fill in some value in the padding bits too, to avoid
"Use of uninitialised value of size ###" warning from valgrind*/
- for(x = 0; x < diff; x++) setBitOfReversedStream(&obp, out, 0);
+ for(x = 0; x != diff; ++x) setBitOfReversedStream(&obp, out, 0);
}
}
@@ -5376,16 +5455,16 @@ static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigne
if(bpp >= 8)
{
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
unsigned x, y, b;
size_t bytewidth = bpp / 8;
- for(y = 0; y < passh[i]; y++)
- for(x = 0; x < passw[i]; x++)
+ for(y = 0; y < passh[i]; ++y)
+ for(x = 0; x < passw[i]; ++x)
{
size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth;
- for(b = 0; b < bytewidth; b++)
+ for(b = 0; b < bytewidth; ++b)
{
out[pixeloutstart + b] = in[pixelinstart + b];
}
@@ -5394,18 +5473,18 @@ static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigne
}
else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
{
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
unsigned x, y, b;
unsigned ilinebits = bpp * passw[i];
unsigned olinebits = bpp * w;
size_t obp, ibp; /*bit pointers (for out and in buffer)*/
- for(y = 0; y < passh[i]; y++)
- for(x = 0; x < passw[i]; x++)
+ for(y = 0; y < passh[i]; ++y)
+ for(x = 0; x < passw[i]; ++x)
{
ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
obp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
- for(b = 0; b < bpp; b++)
+ for(b = 0; b < bpp; ++b)
{
unsigned char bit = readBitFromReversedStream(&ibp, in);
setBitOfReversedStream(&obp, out, bit);
@@ -5476,7 +5555,7 @@ static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const
unsigned i;
Adam7_interlace(adam7, in, w, h, bpp);
- for(i = 0; i < 7; i++)
+ for(i = 0; i != 7; ++i)
{
if(bpp < 8)
{
@@ -5515,7 +5594,7 @@ static unsigned getPaletteTranslucency(const unsigned char* palette, size_t pale
size_t i;
unsigned key = 0;
unsigned r = 0, g = 0, b = 0; /*the value of the color with alpha 0, so long as color keying is possible*/
- for(i = 0; i < palettesize; i++)
+ for(i = 0; i != palettesize; ++i)
{
if(!key && palette[4 * i + 3] == 0)
{
@@ -5660,7 +5739,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
/*tIME*/
if(info.time_defined) addChunk_tIME(&outv, &info.time);
/*tEXt and/or zTXt*/
- for(i = 0; i < info.text_num; i++)
+ for(i = 0; i != info.text_num; ++i)
{
if(strlen(info.text_keys[i]) > 79)
{
@@ -5685,7 +5764,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
if(state->encoder.add_id)
{
unsigned alread_added_id_text = 0;
- for(i = 0; i < info.text_num; i++)
+ for(i = 0; i != info.text_num; ++i)
{
if(!strcmp(info.text_keys[i], "LodePNG"))
{
@@ -5699,7 +5778,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
}
}
/*iTXt*/
- for(i = 0; i < info.itext_num; i++)
+ for(i = 0; i != info.itext_num; ++i)
{
if(strlen(info.itext_keys[i]) > 79)
{