Skip to content

Commit

Permalink
fix signed conversion warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeve committed Sep 22, 2018
1 parent 46ae9c5 commit 677f739
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
10 changes: 5 additions & 5 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3652,7 +3652,7 @@ unsigned lodepng_convert_rgb(
const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in)
{
unsigned r = 0, g = 0, b = 0;
unsigned mul = 65535 / ((1 << mode_in->bitdepth) - 1); /*65535, 21845, 4369, 257, 1*/
unsigned mul = 65535 / ((1u << mode_in->bitdepth) - 1u); /*65535, 21845, 4369, 257, 1*/
unsigned shift = 16 - mode_out->bitdepth;

if(mode_in->colortype == LCT_GREY || mode_in->colortype == LCT_GREY_ALPHA)
Expand All @@ -3668,9 +3668,9 @@ unsigned lodepng_convert_rgb(
else if(mode_in->colortype == LCT_PALETTE)
{
if(r_in >= mode_in->palettesize) return 82;
r = mode_in->palette[r_in * 4 + 0] * 257;
g = mode_in->palette[r_in * 4 + 1] * 257;
b = mode_in->palette[r_in * 4 + 2] * 257;
r = mode_in->palette[r_in * 4 + 0] * 257u;
g = mode_in->palette[r_in * 4 + 1] * 257u;
b = mode_in->palette[r_in * 4 + 2] * 257u;
}
else
{
Expand Down Expand Up @@ -3766,7 +3766,7 @@ unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
unsigned bits_done = (profile->bits == 1 && bpp == 1) ? 1 : 0;
unsigned sixteen = 0; /* whether the input image is 16 bit */
unsigned maxnumcolors = 257;
if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, profile->numcolors + (1 << bpp));
if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, profile->numcolors + (1u << bpp));

profile->numpixels += numpixels;

Expand Down
30 changes: 15 additions & 15 deletions lodepng_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Testing instructions:
*) Ensure no tests commented out below or early return in doMain
*) Compile with g++ or clang++ with all warnings and run the unit test
g++ lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wshadow -pedantic -ansi -O3 && ./a.out
clang++ lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wshadow -pedantic -ansi -O3 && ./a.out
g++ lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wsign-conversion -Wshadow -pedantic -ansi -O3 && ./a.out
clang++ lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wsign-conversion -Wshadow -pedantic -ansi -O3 && ./a.out
*) Compile with pure ISO C90 and all warnings:
mv lodepng.cpp lodepng.c ; gcc -I ./ lodepng.c examples/example_decode.c -ansi -pedantic -Wall -Wextra -O3 ; mv lodepng.c lodepng.cpp
Expand Down Expand Up @@ -301,7 +301,7 @@ void generateTestImageRequiringColorType16(Image& image, LodePNGColorType colorT

if(colorType == LCT_PALETTE)
{
w = 1 << bitDepth;
w = 1u << bitDepth;
h = 256; // ensure it'll really choose palette, not omit it due to small image size
image.data.resize(w * h * 8);
for(size_t y = 0; y < h; y++)
Expand Down Expand Up @@ -334,7 +334,7 @@ void generateTestImageRequiringColorType16(Image& image, LodePNGColorType colorT
else if(grey)
{
w = 2;
unsigned v = 255 / ((1 << bitDepth) - 1); // value that forces at least this bitdepth
unsigned v = 255u / ((1u << bitDepth) - 1u); // value that forces at least this bitdepth
image.data.resize(w * h * 8);
image.data[0] = v; image.data[1] = v;
image.data[2] = v; image.data[3] = v;
Expand Down Expand Up @@ -383,7 +383,7 @@ void generateTestImageRequiringColorType8(Image& image, LodePNGColorType colorTy

if(colorType == LCT_PALETTE)
{
w = 1 << bitDepth;
w = 1u << bitDepth;
h = 256; // ensure it'll really choose palette, not omit it due to small image size
image.data.resize(w * h * 4);
for(size_t y = 0; y < h; y++)
Expand All @@ -401,7 +401,7 @@ void generateTestImageRequiringColorType8(Image& image, LodePNGColorType colorTy
else if(grey)
{
w = 2;
unsigned v = 255 / ((1 << bitDepth) - 1); // value that forces at least this bitdepth
unsigned v = 255u / ((1u << bitDepth) - 1u); // value that forces at least this bitdepth
image.data.resize(w * h * 4);
image.data[0] = v;
image.data[1] = v;
Expand Down Expand Up @@ -1329,7 +1329,7 @@ void testInspectChunk()
chunk = lodepng_chunk_find(png.data(), png.data() + png.size(), "tIME");
ASSERT_NOT_EQUALS((const unsigned char*)0, chunk);
ASSERT_EQUALS(0, info.time_defined);
lodepng_inspect_chunk(&state, chunk - png.data(), png.data(), png.size());
lodepng_inspect_chunk(&state, (size_t)(chunk - png.data()), png.data(), png.size());
ASSERT_EQUALS(1, info.time_defined);
ASSERT_EQUALS(2012, state.info_png.time.year);
ASSERT_EQUALS(1, info.time.month);
Expand All @@ -1340,10 +1340,10 @@ void testInspectChunk()

ASSERT_EQUALS(0, info.text_num);
chunk = lodepng_chunk_find_const(png.data(), png.data() + png.size(), "zTXt");
lodepng_inspect_chunk(&state, chunk - png.data(), png.data(), png.size());
lodepng_inspect_chunk(&state, (size_t)(chunk - png.data()), png.data(), png.size());
ASSERT_EQUALS(1, info.text_num);
chunk = lodepng_chunk_find_const(chunk, png.data() + png.size(), "zTXt");
lodepng_inspect_chunk(&state, chunk - png.data(), png.data(), png.size());
lodepng_inspect_chunk(&state, (size_t)(chunk - png.data()), png.data(), png.size());
ASSERT_EQUALS(2, info.text_num);
}

Expand Down Expand Up @@ -2548,11 +2548,11 @@ void testBkgdChunk()
for(int i = 0; i < 200; i++) lodepng_palette_add(&pal, i, i / 2, 0, 255);
pal.colortype = LCT_PALETTE;
pal.bitdepth = 8;
int w = 200;
int h = 200;
unsigned w = 200;
unsigned h = 200;
std::vector<unsigned char> img(w * h);
for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
for(unsigned y = 0; y < h; y++)
for(unsigned x = 0; x < w; x++)
{
img[y * w + x] = x;
}
Expand All @@ -2562,8 +2562,8 @@ void testBkgdChunk()
testBkgdChunk(250, 0, 0, 250, 250, 250, img, w, h, pal, pal, true, true);

std::vector<unsigned char> fourcolor(w * h);
for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
for(unsigned y = 0; y < h; y++)
for(unsigned x = 0; x < w; x++)
{
fourcolor[y * w + x] = x & 3;
}
Expand Down
14 changes: 7 additions & 7 deletions lodepng_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ unsigned convertToXYZ(float* out, const unsigned char* in,
size_t n = w * h;
for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 4; c++)
for(unsigned c = 0; c < 4; c++)
{
size_t j = i * 8 + c * 2;
out[i * 4 + c] = (data[j + 0] * 256 + data[j + 1]) / 65535.0;
Expand All @@ -367,7 +367,7 @@ unsigned convertToXYZ(float* out, const unsigned char* in,
float gamma = 100000.0f / info->gama_gamma;
for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 3; c++)
for(unsigned c = 0; c < 3; c++)
{
out[i * 4 + c] = std::pow(out[i * 4 + c], gamma);
}
Expand All @@ -377,7 +377,7 @@ unsigned convertToXYZ(float* out, const unsigned char* in,
{
for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 3; c++)
for(unsigned c = 0; c < 3; c++)
{
// sRGB gamma expand
float& v = out[i * 4 + c];
Expand Down Expand Up @@ -461,7 +461,7 @@ unsigned convertFromXYZ(unsigned char* out, const float* in,
float gamma = info->gama_gamma / 100000.0f;
for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 3; c++)
for(unsigned c = 0; c < 3; c++)
{
im[i * 4 + c] = std::pow(im[i * 4 + c], gamma);
}
Expand All @@ -471,7 +471,7 @@ unsigned convertFromXYZ(unsigned char* out, const float* in,
{
for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 3; c++)
for(unsigned c = 0; c < 3; c++)
{
// sRGB gamma compress
float& v = im[i * 4 + c];
Expand All @@ -483,10 +483,10 @@ unsigned convertFromXYZ(unsigned char* out, const float* in,

for(unsigned i = 0; i < n; i++)
{
for(int c = 0; c < 4; c++)
for(unsigned c = 0; c < 4; c++)
{
size_t j = i * 8 + c * 2;
int i16 = (int)(0.5 + 65535 * std::min(std::max(0.0f, im[i * 4 + c]), 1.0f));
int i16 = (int)(0.5 + 65535.0 * std::min(std::max(0.0f, im[i * 4 + c]), 1.0f));
data[j + 0] = i16 >> 8;
data[j + 1] = i16 & 255;
}
Expand Down

0 comments on commit 677f739

Please sign in to comment.