Skip to content

Commit e5eaba9

Browse files
committed
Switch lodepng_compute_color_stats to void, other minor changes
lodepng_compute_color_stats doesn't return any errors as is, so the function type is changed to void to reflect this behavior
1 parent 17434f6 commit e5eaba9

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/lodepng/lodepng.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void* lodepng_realloc(void* ptr, size_t new_size);
103103
#define LODEPNG_RESTRICT /* not available */
104104
#endif
105105

106-
#define LODEPNG_ABS(x) ((x) < 0 ? -(x) : (x))
107106
#define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b))
108107

109108
#if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_DECODER)
@@ -228,7 +227,7 @@ static char* alloc_string(const char* in) {
228227
#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)
229228
static unsigned lodepng_read32bitInt(const unsigned char* buffer) {
230229
return (((unsigned)buffer[0] << 24u) | ((unsigned)buffer[1] << 16u) |
231-
((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]);
230+
((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]);
232231
}
233232
#endif /*defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_PNG)*/
234233

@@ -664,11 +663,11 @@ static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) {
664663

665664
static unsigned getNumColorChannels(LodePNGColorType colortype) {
666665
switch(colortype) {
667-
case LCT_GREY: return 1;
668-
case LCT_RGB: return 3;
669-
case LCT_PALETTE: return 1;
670-
case LCT_GREY_ALPHA: return 2;
671-
case LCT_RGBA: return 4;
666+
case LCT_GREY: return 1;
667+
case LCT_RGB: return 3;
668+
case LCT_PALETTE: return 1;
669+
case LCT_GREY_ALPHA: return 2;
670+
case LCT_RGBA: return 4;
672671
case LCT_MAX_OCTET_VALUE: return 0; /* invalid color type */
673672
default: return 0; /*invalid color type*/
674673
}
@@ -1612,11 +1611,10 @@ static unsigned getValueRequiredBits(unsigned char value) {
16121611
}
16131612

16141613
/*stats must already have been inited. */
1615-
unsigned lodepng_compute_color_stats(LodePNGColorStats* stats,
1616-
const unsigned char* in, const size_t numpixels,
1617-
const LodePNGColorMode* mode_in) {
1614+
void lodepng_compute_color_stats(LodePNGColorStats* stats,
1615+
const unsigned char* in, const size_t numpixels,
1616+
const LodePNGColorMode* mode_in) {
16181617
size_t i;
1619-
unsigned error = 0;
16201618

16211619
/* mark things as done already if it would be impossible to have a more expensive case */
16221620
unsigned colored_done = lodepng_is_greyscale_type(mode_in) ? 1 : 0;
@@ -2047,9 +2045,9 @@ The parameters are of type short, but should come from unsigned chars, the short
20472045
are only needed to make the paeth calculation correct.
20482046
*/
20492047
static unsigned char paethPredictor(short a, short b, short c) {
2050-
short pa = LODEPNG_ABS(b - c);
2051-
short pb = LODEPNG_ABS(a - c);
2052-
short pc = LODEPNG_ABS(a + b - c - c);
2048+
short pa = abs(b - c);
2049+
short pb = abs(a - c);
2050+
short pc = abs(a + b - c - c);
20532051
/* return input value associated with smallest of pa, pb, pc (with certain priority if equal) */
20542052
if(pb < pa) { a = b; pa = pb; }
20552053
return (pc < pa) ? c : a;
@@ -3179,7 +3177,7 @@ static void filterScanline(unsigned char* out, const unsigned char* scanline, co
31793177
break;
31803178
case 1: { /*Sub*/
31813179
size_t j = 0;
3182-
memcpy(out, scanline, bytewidth);
3180+
for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
31833181
for(i = bytewidth; i != length; ++i, ++j) out[i] = scanline[i] - scanline[j];
31843182
break;
31853183
}
@@ -3196,7 +3194,7 @@ static void filterScanline(unsigned char* out, const unsigned char* scanline, co
31963194
for(i = 0; i != bytewidth; ++i) out[i] = scanline[i] - (prevline[i] >> 1u);
31973195
for(i = bytewidth; i < length; ++i, ++j) out[i] = scanline[i] - ((scanline[j] + prevline[i]) >> 1u);
31983196
} else {
3199-
memcpy(out, scanline, bytewidth);
3197+
for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
32003198
for(i = bytewidth; i < length; ++i, ++j) out[i] = scanline[i] - (scanline[j] >> 1u);
32013199
}
32023200
break;
@@ -3210,7 +3208,7 @@ static void filterScanline(unsigned char* out, const unsigned char* scanline, co
32103208
out[i] = scanline[i] - paethPredictor(scanline[j], prevline[i], prevline[j]);
32113209
}
32123210
} else {
3213-
memcpy(out, scanline, bytewidth);
3211+
for(i = 0; i != bytewidth; ++i) out[i] = scanline[i];
32143212
/*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/
32153213
for(i = bytewidth; i != length; ++i, ++j) out[i] = scanline[i] - scanline[j];
32163214
}
@@ -4039,9 +4037,8 @@ static unsigned lodepng_encode(unsigned char** out, size_t* outsize,
40394037
LodePNGColorStats stats;
40404038
lodepng_color_stats_init(&stats);
40414039

4042-
state->error = lodepng_compute_color_stats(&stats, image, numpixels, &state->info_raw);
4043-
if(state->error) goto cleanup;
4044-
else { /*check if image is white only if no error is detected in previous function*/
4040+
lodepng_compute_color_stats(&stats, image, numpixels, &state->info_raw);
4041+
/* check if image is fully white */ {
40454042
unsigned char r = 0, g = 0, b = 0, a = 0;
40464043
getPixelColorRGBA8(&r, &g, &b, &a, image, 0, &state->info_raw);
40474044
stats.white = stats.numcolors == 1 && stats.colored == 0 && r == 255 && w > 20 && h > 20

src/lodepng/lodepng.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ void lodepng_color_stats_init(LodePNGColorStats* stats);
489489

490490
/*Get a LodePNGColorStats of the image. The stats must already have been inited.
491491
Returns error code (e.g. alloc fail) or 0 if ok.*/
492-
unsigned lodepng_compute_color_stats(LodePNGColorStats* stats,
493-
const unsigned char* image, unsigned w, unsigned h,
494-
const LodePNGColorMode* mode_in);
492+
void lodepng_compute_color_stats(LodePNGColorStats* stats,
493+
const unsigned char* image, const size_t numpixels,
494+
const LodePNGColorMode* mode_in);
495495

496496
/*Settings for the encoder.*/
497497
typedef struct LodePNGEncoderSettings {

0 commit comments

Comments
 (0)