From d6e061315e6df91a4d8025daefea769896190eef Mon Sep 17 00:00:00 2001 From: Lode Date: Wed, 19 Nov 2014 22:13:54 +0100 Subject: fix bug with encoding transparent single-pixel image --- lodepng_unittest.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'lodepng_unittest.cpp') diff --git a/lodepng_unittest.cpp b/lodepng_unittest.cpp index 1e4a49c..150511c 100644 --- a/lodepng_unittest.cpp +++ b/lodepng_unittest.cpp @@ -476,6 +476,68 @@ void testOtherPattern2() doCodecTest(image1); } +void testSinglePixel(int r, int g, int b, int a) +{ + std::cout << "codec single pixel " << r << " " << g << " " << b << " " << a << std::endl; + Image pixel; + pixel.width = 1; + pixel.height = 1; + pixel.colorType = LCT_RGBA; + pixel.bitDepth = 8; + pixel.data.resize(4); + pixel.data[0] = r; + pixel.data[1] = g; + pixel.data[2] = b; + pixel.data[3] = a; + + doCodecTest(pixel); +} + +void testColor(int r, int g, int b, int a) +{ + std::cout << "codec test color " << r << " " << g << " " << b << " " << a << std::endl; + Image image; + image.width = 100; + image.height = 100; + image.colorType = LCT_RGBA; + image.bitDepth = 8; + image.data.resize(100 * 100 * 4); + for(int y = 0; y < 100; y++) + for(int x = 0; x < 100; x++) + { + image.data[100 * 4 * y + 4 * x + 0] = r; + image.data[100 * 4 * y + 4 * x + 0] = g; + image.data[100 * 4 * y + 4 * x + 0] = b; + image.data[100 * 4 * y + 4 * x + 0] = a; + } + + doCodecTest(image); + + Image image2 = image; + image2.data[3] = 0; //one fully transparent pixel + doCodecTest(image2); + image2.data[3] = 128; //one semi transparent pixel + doCodecTest(image2); + + Image image3 = image; + // add 255 different colors + for(int i = 0; i < 255; i++) { + image.data[i * 4 + 0] = i; + image.data[i * 4 + 1] = i; + image.data[i * 4 + 2] = i; + image.data[i * 4 + 3] = 255; + } + doCodecTest(image3); + // a 256th color + image.data[255 * 4 + 0] = 255; + image.data[255 * 4 + 1] = 255; + image.data[255 * 4 + 2] = 255; + image.data[255 * 4 + 3] = 255; + doCodecTest(image3); + + testSinglePixel(r, g, b, a); +} + void testPNGCodec() { codecTest(1, 1); @@ -487,6 +549,28 @@ void testPNGCodec() testOtherPattern1(); testOtherPattern2(); + + testColor(255, 255, 255, 255); + testColor(0, 0, 0, 255); + testColor(1, 2, 3, 255); + testColor(255, 0, 0, 255); + testColor(0, 255, 0, 255); + testColor(0, 0, 255, 255); + testColor(0, 0, 0, 255); + testColor(1, 1, 1, 255); + testColor(1, 1, 1, 1); + testColor(0, 0, 0, 128); + testColor(255, 0, 0, 128); + testColor(127, 127, 127, 255); + testColor(128, 128, 128, 255); + testColor(127, 127, 127, 128); + testColor(128, 128, 128, 128); + //transparent single pixels + testColor(255, 0, 0, 0); + testColor(1, 2, 3, 0); + testColor(255, 255, 255, 0); + testColor(254, 254, 254, 0); + testColor(0, 0, 0, 0); } //Tests some specific color conversions with specific color bit combinations -- cgit v1.2.3 From b5eb75dc2d7c7f29509b1c0a74b0ab349372e767 Mon Sep 17 00:00:00 2001 From: Lode Date: Wed, 19 Nov 2014 22:22:33 +0100 Subject: unit test was a bit slow like that --- lodepng_unittest.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'lodepng_unittest.cpp') diff --git a/lodepng_unittest.cpp b/lodepng_unittest.cpp index 150511c..d3b3498 100644 --- a/lodepng_unittest.cpp +++ b/lodepng_unittest.cpp @@ -497,18 +497,18 @@ void testColor(int r, int g, int b, int a) { std::cout << "codec test color " << r << " " << g << " " << b << " " << a << std::endl; Image image; - image.width = 100; - image.height = 100; + image.width = 20; + image.height = 20; image.colorType = LCT_RGBA; image.bitDepth = 8; - image.data.resize(100 * 100 * 4); - for(int y = 0; y < 100; y++) - for(int x = 0; x < 100; x++) + image.data.resize(20 * 20 * 4); + for(int y = 0; y < 20; y++) + for(int x = 0; x < 20; x++) { - image.data[100 * 4 * y + 4 * x + 0] = r; - image.data[100 * 4 * y + 4 * x + 0] = g; - image.data[100 * 4 * y + 4 * x + 0] = b; - image.data[100 * 4 * y + 4 * x + 0] = a; + image.data[20 * 4 * y + 4 * x + 0] = r; + image.data[20 * 4 * y + 4 * x + 0] = g; + image.data[20 * 4 * y + 4 * x + 0] = b; + image.data[20 * 4 * y + 4 * x + 0] = a; } doCodecTest(image); -- cgit v1.2.3 From a2c1203a0cdafee8e89fd116f512e5d96d94d27b Mon Sep 17 00:00:00 2001 From: Lode Date: Thu, 20 Nov 2014 01:01:02 +0100 Subject: predict idat size correctly for interlaced images --- lodepng_unittest.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'lodepng_unittest.cpp') diff --git a/lodepng_unittest.cpp b/lodepng_unittest.cpp index d3b3498..a633a97 100644 --- a/lodepng_unittest.cpp +++ b/lodepng_unittest.cpp @@ -355,11 +355,44 @@ void doCodecTestCPP(Image& image) assertPixels(image, &decoded[0], "Pixels C++"); } +//Test LodePNG encoding and decoding the encoded result, using the C++ interface, with interlace +void doCodecTestInterlaced(Image& image) +{ + std::vector encoded; + std::vector decoded; + unsigned decoded_w; + unsigned decoded_h; + + lodepng::State state; + state.info_png.interlace_method = 1; + state.info_raw.colortype = image.colorType; + state.info_raw.bitdepth = image.bitDepth; + + unsigned error_enc = lodepng::encode(encoded, image.data, image.width, image.height, state); + + assertNoPNGError(error_enc, "encoder error C++"); + + //if the image is large enough, compressing it should result in smaller size + if(image.data.size() > 512) assertTrue(encoded.size() < image.data.size(), "compressed size"); + + state.info_raw.colortype = image.colorType; + state.info_raw.bitdepth = image.bitDepth; + unsigned error_dec = lodepng::decode(decoded, decoded_w, decoded_h, state, encoded); + + assertNoPNGError(error_dec, "decoder error C++"); + + ASSERT_EQUALS(image.width, decoded_w); + ASSERT_EQUALS(image.height, decoded_h); + ASSERT_EQUALS(image.data.size(), decoded.size()); + assertPixels(image, &decoded[0], "Pixels C++"); +} + //Test LodePNG encoding and decoding the encoded result void doCodecTest(Image& image) { doCodecTestC(image); doCodecTestCPP(image); + doCodecTestInterlaced(image); } @@ -538,6 +571,27 @@ void testColor(int r, int g, int b, int a) testSinglePixel(r, g, b, a); } +void testSize(int w, int h) +{ + std::cout << "codec test size " << w << " " << h << std::endl; + Image image; + image.width = w; + image.height = h; + image.colorType = LCT_RGBA; + image.bitDepth = 8; + image.data.resize(w * h * 4); + for(int y = 0; y < h; y++) + for(int x = 0; x < w; x++) + { + image.data[w * 4 * y + 4 * x + 0] = x % 256; + image.data[w * 4 * y + 4 * x + 0] = y % 256; + image.data[w * 4 * y + 4 * x + 0] = 255; + image.data[w * 4 * y + 4 * x + 0] = 255; + } + + doCodecTest(image); +} + void testPNGCodec() { codecTest(1, 1); @@ -566,11 +620,18 @@ void testPNGCodec() testColor(127, 127, 127, 128); testColor(128, 128, 128, 128); //transparent single pixels + testColor(0, 0, 0, 0); testColor(255, 0, 0, 0); testColor(1, 2, 3, 0); testColor(255, 255, 255, 0); testColor(254, 254, 254, 0); - testColor(0, 0, 0, 0); + + // This is mainly to test the Adam7 interlacing + for(int h = 1; h < 12; h++) + for(int w = 1; w < 12; w++) + { + testSize(w, h); + } } //Tests some specific color conversions with specific color bit combinations -- cgit v1.2.3 From 76577a213aead1da174b3afb234720438261a533 Mon Sep 17 00:00:00 2001 From: Lode Date: Thu, 20 Nov 2014 01:47:54 +0100 Subject: avoid too big pixel sizes --- lodepng_unittest.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lodepng_unittest.cpp') diff --git a/lodepng_unittest.cpp b/lodepng_unittest.cpp index a633a97..f2b1cf7 100644 --- a/lodepng_unittest.cpp +++ b/lodepng_unittest.cpp @@ -1166,20 +1166,23 @@ void testFuzzing() std::vector result; std::map errors; unsigned w, h; + lodepng::State state; + state.decoder.ignore_crc = 1; + state.decoder.zlibsettings.ignore_adler32 = 1; for(size_t i = 0; i < png.size(); i++) { result.clear(); broken[i] = ~png[i]; - errors[lodepng::decode(result, w, h, broken)]++; + errors[lodepng::decode(result, w, h, state, broken)]++; broken[i] = 0; - errors[lodepng::decode(result, w, h, broken)]++; + errors[lodepng::decode(result, w, h, state, broken)]++; for(int j = 0; j < 8; j++) { broken[i] = flipBit(png[i], j); - errors[lodepng::decode(result, w, h, broken)]++; + errors[lodepng::decode(result, w, h, state, broken)]++; } broken[i] = 255; - errors[lodepng::decode(result, w, h, broken)]++; + errors[lodepng::decode(result, w, h, state, broken)]++; broken[i] = png[i]; //fix it again for the next test } std::cout << "testFuzzing shrinking" << std::endl; @@ -1187,7 +1190,7 @@ void testFuzzing() while(broken.size() > 0) { broken.resize(broken.size() - 1); - errors[lodepng::decode(result, w, h, broken)]++; + errors[lodepng::decode(result, w, h, state, broken)]++; } //For fun, print the number of each error -- cgit v1.2.3 From 244501ad3c23a00a004121ee99ca36eaf6b8abb4 Mon Sep 17 00:00:00 2001 From: Lode Date: Thu, 20 Nov 2014 22:04:11 +0100 Subject: small tweaks --- lodepng_unittest.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lodepng_unittest.cpp') diff --git a/lodepng_unittest.cpp b/lodepng_unittest.cpp index f2b1cf7..e3dbc10 100644 --- a/lodepng_unittest.cpp +++ b/lodepng_unittest.cpp @@ -1754,6 +1754,10 @@ void testAutoColorModels() std::vector not16; addColor16(not16, 257, 257, 257, 0); testAutoColorModel(not16, 16, LCT_PALETTE, 1, false); + + std::vector alpha16; + addColor16(alpha16, 257, 0, 0, 10000); + testAutoColorModel(alpha16, 16, LCT_RGBA, 16, false); } void doMain() -- cgit v1.2.3