From fe5415cba7d734a12b5247adf759fbd25242d5a8 Mon Sep 17 00:00:00 2001 From: sz Date: Sun, 26 Nov 2023 22:29:59 -0600 Subject: [PATCH] Use 4 middle cols when decoding 8x8 colors Seems more consistent, without *too* much of a performance hit than the every-other 3 col optimization. --- src/lib/cimb_translator/CimbDecoder.cpp | 8 ++++---- src/lib/cimb_translator/test/CimbReaderTest.cpp | 13 +++++++------ src/lib/encoder/test/DecoderTest.cpp | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lib/cimb_translator/CimbDecoder.cpp b/src/lib/cimb_translator/CimbDecoder.cpp index f721aec..9534ea1 100644 --- a/src/lib/cimb_translator/CimbDecoder.cpp +++ b/src/lib/cimb_translator/CimbDecoder.cpp @@ -158,10 +158,9 @@ unsigned CimbDecoder::get_best_color(float r, float g, float b) const float max = std::max({r, g, b, 1.0f}); float min = std::min({r, g, b, BEST_COLOR_FLOOR}); - float adjust = 255.0; if (min >= max) min = 0; - adjust /= (max - min); + float adjust = 255.0/(max - min); std::tuple c = fix_color({r, g, b}, adjust, min); @@ -188,8 +187,9 @@ unsigned CimbDecoder::decode_color(const Cell& color_cell) const // TODO: check/enforce dimensions of color_cell? // limit dimensions to ignore outer row/col. We want to look at the middle 6x6, or 3x3... Cell center = color_cell; - center.crop(1, 1, color_cell.cols()-2, color_cell.rows()-2); - auto [r, g, b] = center.mean_rgb(center.cols() > 4? Cell::SKIP : 0); + int hcrop = color_cell.cols() > 6? 2 : 1; + center.crop(hcrop, 1, color_cell.cols()-1-hcrop, color_cell.rows()-2); + auto [r, g, b] = center.mean_rgb(center.cols() > 5? Cell::SKIP : 0); return get_best_color(r, g, b); } diff --git a/src/lib/cimb_translator/test/CimbReaderTest.cpp b/src/lib/cimb_translator/test/CimbReaderTest.cpp index 735a150..7d79d47 100644 --- a/src/lib/cimb_translator/test/CimbReaderTest.cpp +++ b/src/lib/cimb_translator/test/CimbReaderTest.cpp @@ -73,9 +73,9 @@ TEST_CASE( "CimbReaderTest/testSample", "[unit]" ) ++count; } - string expected = "0=0 99=8 600=33 710=28 711=30 821=8 822=22 823=19 934=55 11688=55 11799=25 " - "11900=28 12000=23 12100=0 12200=5 12201=0 12298=32 12299=34 12300=30 12301=32 " - "12398=10 12399=15"; + string expected = "0=0 99=8 11680=3 11681=32 11900=28 11901=25 11904=12 11995=2 11996=8 11998=6 " + "11999=54 12001=29 12004=6 12099=2 12195=57 12196=1 12200=5 12201=0 12298=32 " + "12299=34 12300=30 12399=15"; assertEquals( expected, turbo::str::join(res) ); PositionData pos; @@ -109,8 +109,9 @@ TEST_CASE( "CimbReaderTest/testSampleMessy", "[unit]" ) ++count; } - string expected = "0=0 99=8 600=33 711=30 11462=2 11573=33 11574=0 11685=52 11686=17 11687=41 " - "11688=55 11797=30 11798=15 11799=25 12200=5 12298=46 12299=34 12300=30 12301=32 12397=38 12398=10 12399=15"; + string expected = "0=0 1=28 99=8 100=28 600=33 601=38 711=30 712=57 11238=24 11350=32 11571=0 " + "11573=33 11574=0 11575=1 11685=52 11686=17 11687=41 11688=55 11798=15 11799=25 " + "12300=30 12399=15"; assertEquals( expected, turbo::str::join(res) ); PositionData pos; @@ -200,7 +201,7 @@ TEST_CASE( "CimbReaderTest/testCCM.VeryNecessary", "[unit]" ) " 0.023812667, 0.98703396, -0.0048082001;\n" " 0, 0, 1.0017186]", ss.str()); - std::array expectedColors = {0, 0, 0, 1, 2, 3}; // it's wrong, but it's consistent! + std::array expectedColors = {0, 0, 0, 0, 1, 0}; // it's wrong, but it's consistent! for (unsigned i = 0; i < expectedColors.size(); ++i) { PositionData pos; diff --git a/src/lib/encoder/test/DecoderTest.cpp b/src/lib/encoder/test/DecoderTest.cpp index 28f00eb..d5e3f25 100644 --- a/src/lib/encoder/test/DecoderTest.cpp +++ b/src/lib/encoder/test/DecoderTest.cpp @@ -57,5 +57,5 @@ TEST_CASE( "DecoderTest/testDecode.Sample", "[unit]" ) assertEquals( 9300, bytesDecoded ); if (CV_VERSION_MAJOR == 4) - assertEquals( "69b608d82083b5d22cd3ebf8785c2d0329a83b0cd084521c418ea97316b54cc6", get_hash(decodedFile) ); + assertEquals( "847dc20288c717b6f43b67b15fd2001d950ea5bcfe82de5c5d17bdc047f8631f", get_hash(decodedFile) ); }