Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding improvements, and (somewhat) more flexible config for alternate grid dimensions #73

Merged
merged 38 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1fd265d
Minimal changes for encoding with a grid of 5x5s
sz3 Jul 23, 2022
731dfb1
Update the bitextractor to deal with reads other than len=8
sz3 Jul 23, 2022
e2cab43
Small changes to average_hash(), decode_color()
sz3 Jul 23, 2022
ec62980
Changes to average_hash...
sz3 Jul 23, 2022
cdd2f5c
Update ahash_result for 5-bit reads + lots of test fixes for average_…
sz3 Jul 24, 2022
c34ae88
Calculate total_cells() and frame capacity, and ...
sz3 Jul 25, 2022
c4f8750
A 5x5 that works with both 4 bit and 5 bit
sz3 Jul 30, 2022
7766cfd
Attempt to use 988x988 grid...?
sz3 Aug 13, 2022
3616c36
Pass image_size/anchor_size to extractor...
sz3 Aug 28, 2022
f9a9dd8
Scale window size off the size of the image.
sz3 Aug 29, 2022
6849234
These should all be const...
sz3 Sep 5, 2022
f3cdabe
More const, and add more seed positions for the decode?
sz3 Dec 10, 2022
99e0f51
Drift cooldown?
sz3 Dec 31, 2022
3479d39
Better(?) threshold params, and a thought
sz3 Jan 30, 2023
92a33ef
More warnings \o/
sz3 Jan 30, 2023
f932602
Special case for running the "ALL" check (9-way compare) on seed loca…
sz3 Feb 5, 2023
cad107c
Minor tweaks to make it easier to switch between grid sizes
sz3 Feb 5, 2023
173087c
Move grid params into their own file?
sz3 Apr 2, 2023
24a291a
Add some compile-time magic to auto-generate the bit extract pattern
sz3 Apr 2, 2023
e2b55fc
+ do all bitextract index magic in "pattern()" function
sz3 Apr 2, 2023
02d4573
Have ahash_result take a template param for cell size
sz3 Apr 2, 2023
ec74f7c
For now, put the uint128s back
sz3 Apr 2, 2023
4e33841
Put us back on 8x8
sz3 Apr 5, 2023
1ec077b
Running afoul of constexpr rules
sz3 Apr 5, 2023
18e5480
Put back the skip param for mean_rgb on larger cell sizes
sz3 Apr 7, 2023
6daa947
3,-5 for adaptiveThreshhold on symbols seems better?
sz3 Apr 10, 2023
4d2943c
Experimenting with ways to improve the flood decode
sz3 Apr 11, 2023
9204892
Misc fix
sz3 Apr 16, 2023
55956de
Update more tests?
sz3 Apr 16, 2023
8c891d0
Put back the Config.cpp for broader compiler support...
sz3 Apr 19, 2023
d452652
Update regression tests
sz3 May 6, 2023
dce6833
Fix off-by-one type bug!
sz3 May 7, 2023
04528bb
Add the 5x5 tileset to bitmaps.h + missing include
sz3 May 8, 2023
9f11473
Fix wasm packaging script
sz3 May 10, 2023
6baf0c4
Odd emscripten workaround -- not sure if this is the "right way", but…
sz3 May 10, 2023
55fcadb
gh workflow updates
sz3 May 10, 2023
b9d9d03
Simplify calculate_cooldown() + a comment for FloodDecodePositions::u…
sz3 May 26, 2023
709a348
Add back a few test cases, add some comments, and check in the 5x5 ti…
sz3 Jun 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ jobs:
fail-fast: false
matrix:
config:
- name: "linux gcc7"
os: ubuntu-18.04
env: CXX="g++-7" CC="gcc-7"

- name: "linux gcc9"
os: ubuntu-20.04
env: CXX="g++-9" CC="gcc-9"

- name: "linux gcc-latest"
os: ubuntu-latest

- name: "linux clang"
os: ubuntu-18.04
os: ubuntu-20.04
env: CXX="clang++" CC="clang"
extra-packages: "clang"

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
submodules: 'recursive'

Expand All @@ -50,10 +50,10 @@ jobs:
run: python3 -m unittest

cppcheck:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install dependencies
run: sudo apt-get install cppcheck
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Get the code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: 'recursive'

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ add_definitions("-DLIBCIMBAR_PROJECT_ROOT=\"${libcimbar_SOURCE_DIR}\"")
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
set(CMAKE_C_FLAGS "${CFLAGS} -Wall -std=c99 -Os -fPIC")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-Wall -g -O2 -fPIC")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-Wall -Wextra -g -O2 -fPIC")
endif()

if(DEFINED USE_WASM) # wasm build needs OPENCV_DIR defined
Expand Down
Binary file added bitmap/2/00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitmap/2/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitmap/2/02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitmap/2/03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions package-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
cd /usr/src/app

apt update
apt install python -y
apt install python3 -y

cd opencv4/
mkdir opencv-build-wasm && cd opencv-build-wasm
python ../platforms/js/build_js.py build_wasm --build_wasm --emscripten_dir=/emsdk/upstream/emscripten
python3 ../platforms/js/build_js.py build_wasm --build_wasm --emscripten_dir=/emsdk/upstream/emscripten

cd /usr/src/app
mkdir build-wasm && cd build-wasm
Expand All @@ -22,5 +22,5 @@ emcmake cmake .. -DUSE_WASM=2 -DOPENCV_DIR=/usr/src/app/opencv4
make -j5 install
(cd ../web/ && zip cimbar.asmjs.zip cimbar_js.js index.html main.js)

(cd ../ && python package-cimbar-html.py)
(cd ../ && python3 package-cimbar-html.py)

10 changes: 8 additions & 2 deletions src/exe/build_image_assets/build_image_assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ map<string, string> getFileBlobs(string dir_path)
blobs[file_path] = base91::encode(contents);
}

for (int i = 0; i < 4; ++i)
{
string file_path = fmt::format("{}/2/{:02x}.png", dir_path, i);
sz3 marked this conversation as resolved.
Show resolved Hide resolved
string contents = File(file_path).read_all();
blobs[file_path] = base91::encode(contents);
}
for (int i = 0; i < 16; ++i)
{
string file_path = fmt::format("{}/4/{:02x}.png", dir_path, i);
Expand All @@ -43,8 +49,8 @@ int main(int argc, char** argv)
cxxopts::Options options("build_image_assets", "Build a C++ class file (header-only) that contains base91 png asset data.");

options.add_options()
("b,bitmap", "Bitmap directory", cxxopts::value<std::string>())
("h,help", "Print usage")
("b,bitmap", "Bitmap directory", cxxopts::value<std::string>())
("h,help", "Print usage")
;

auto result = options.parse(argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion src/exe/cimbar_extract/cimbar_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using std::string;

int main(int argc, char** argv)
{
cxxopts::Options options("cimbar scan", "Scan/extract cimbar code from a source image.");
cxxopts::Options options("cimbar_extract", "Scan/extract cimbar code from a source image.");

options.add_options()
("i,in", "Encoded png/jpg/etc", cxxopts::value<std::string>())
Expand Down
14 changes: 7 additions & 7 deletions src/exe/cimbar_send/send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ int main(int argc, char** argv)
unsigned ecc = cimbar::Config::ecc_bytes();
unsigned defaultFps = 15;
options.add_options()
("i,in", "Source file", cxxopts::value<vector<string>>())
("c,colorbits", "Color bits. [0-3]", cxxopts::value<int>()->default_value(turbo::str::str(colorBits)))
("e,ecc", "ECC level", cxxopts::value<unsigned>()->default_value(turbo::str::str(ecc)))
("f,fps", "Target FPS", cxxopts::value<unsigned>()->default_value(turbo::str::str(defaultFps)))
("z,compression", "Compression level. 0 == no compression.", cxxopts::value<int>()->default_value(turbo::str::str(compressionLevel)))
("h,help", "Print usage")
("i,in", "Source file", cxxopts::value<vector<string>>())
("c,colorbits", "Color bits. [0-3]", cxxopts::value<int>()->default_value(turbo::str::str(colorBits)))
("e,ecc", "ECC level", cxxopts::value<unsigned>()->default_value(turbo::str::str(ecc)))
("f,fps", "Target FPS", cxxopts::value<unsigned>()->default_value(turbo::str::str(defaultFps)))
("z,compression", "Compression level. 0 == no compression.", cxxopts::value<int>()->default_value(turbo::str::str(compressionLevel)))
("h,help", "Print usage")
;
options.show_positional_help();
options.parse_positional({"in"});
Expand All @@ -66,7 +66,7 @@ int main(int argc, char** argv)
fps = defaultFps;
unsigned delay = 1000 / fps;

int window_size = 1080;
int window_size = cimbar::Config::image_size() + 32;
if (!initialize_GL(window_size, window_size))
{
std::cerr << "failed to create window :(" << std::endl;
Expand Down
36 changes: 22 additions & 14 deletions src/lib/bit_file/bitmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,41 @@ class bitmatrix
unsigned size = img.cols * img.rows;
while (size >= 8)
{
const uint64_t* hax = reinterpret_cast<const uint64_t*>(p);
uint64_t mval = (*hax) & 0x101010101010101ULL;
// we're turning 1 uint64_t into 8 uint8_ts
uint64_t mval;
memcpy(&mval, p, sizeof mval);
mval = mval & 0x101010101010101ULL;
const uint8_t* cv = reinterpret_cast<const uint8_t*>(&mval);
uint8_t val = cv[0] << 7 | cv[1] << 6 | cv[2] << 5 | cv[3] << 4 | cv[4] << 3 | cv[5] << 2 | cv[6] << 1 | cv[7];
// TODO: what about endianness???
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

uint8_t val = (
cv[0] << 7 | cv[1] << 6 | cv[2] << 5 | cv[3] << 4 | cv[4] << 3 | cv[5] << 2 |
cv[6] << 1 | cv[7]
);
writer << val;
p += 8;
size -= 8;
}

// remainder
uint8_t val = 0;
while (size > 0)
if (size > 0)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug 🙃

{
val |= (*p > 0) << size;
++p;
--size;
uint8_t val = 0;
while (size > 0) {
val |= (*p > 0) << size;
++p;
--size;
}
writer << val;
}
writer << val;
}

public:
bitmatrix(const bitbuffer& buff, unsigned width, unsigned height, unsigned xstart=0, unsigned ystart=0)
: _buff(buff)
, _xstart(xstart)
, _ystart(ystart)
, _width(width)
, _height(height)
: _buff(buff)
, _xstart(xstart)
, _ystart(ystart)
, _width(width)
, _height(height)
{
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/cimb_translator/AdjacentCellFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "AdjacentCellFinder.h"

AdjacentCellFinder::AdjacentCellFinder(const CellPositions::positions_list& positions, int dimensions, int marker_size)
: _positions(positions)
, _dimensions(dimensions)
, _markerSize(marker_size)
: _positions(positions)
, _dimensions(dimensions)
, _markerSize(marker_size)
{
int midDimensions = dimensions - marker_size - marker_size;
int midCells = dimensions * midDimensions;
Expand All @@ -13,10 +13,10 @@ AdjacentCellFinder::AdjacentCellFinder(const CellPositions::positions_list& posi
_firstBottom = edgeCells + midCells;
}

std::array<int, 4> AdjacentCellFinder::find(int index)
std::array<int, 4> AdjacentCellFinder::find(int index) const
{
std::array<int,4> adj = {
right(index), left(index), bottom(index), top(index)
right(index), left(index), bottom(index), top(index)
};
return adj;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/cimb_translator/AdjacentCellFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class AdjacentCellFinder
public:
AdjacentCellFinder(const CellPositions::positions_list& positions, int dimensions, int marker_size);

std::array<int, 4> find(int index);
std::array<int, 4> find(int index) const;

int dimensions() const;
int marker_size() const;

protected:
int in_row_with_margin(int index) const;

int right(int index) const;
int left(int index) const;
int bottom(int index) const;
int top(int index) const;

protected:
int in_row_with_margin(int index) const;

protected:
const CellPositions::positions_list& _positions;
int _dimensions;
Expand Down
24 changes: 12 additions & 12 deletions src/lib/cimb_translator/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ class Cell

public:
Cell(const cv::Mat& img)
: _img(img)
, _cols(img.cols)
, _rows(img.rows)
: _img(img)
, _cols(img.cols)
, _rows(img.rows)
{
}

Cell(const cv::Mat& img, int xstart, int ystart, int cols, int rows)
: _img(img)
, _xstart(xstart)
, _ystart(ystart)
, _cols(cols)
, _rows(rows)
: _img(img)
, _xstart(xstart)
, _ystart(ystart)
, _cols(cols)
, _rows(rows)
{}

// it would be nice to use a cropped cv::Mat to get the contiguous memory pointer...
std::tuple<uchar,uchar,uchar> mean_rgb_continuous(bool skip)
std::tuple<uchar,uchar,uchar> mean_rgb_continuous(bool skip) const
{
uint16_t blue = 0;
uint16_t green = 0;
Expand Down Expand Up @@ -61,7 +61,7 @@ class Cell
return std::tuple<uchar,uchar,uchar>(red/count, green/count, blue/count);
}

std::tuple<uchar,uchar,uchar> mean_rgb(bool skip=false)
std::tuple<uchar,uchar,uchar> mean_rgb(bool skip=false) const
{
int channels = _img.channels();
if (channels < 3)
Expand Down Expand Up @@ -93,7 +93,7 @@ class Cell
return std::tuple<uchar,uchar,uchar>(red/count, green/count, blue/count);
}

uchar mean_grayscale_continuous()
uchar mean_grayscale_continuous() const
{
uint16_t total = 0;
uint16_t count = 0;
Expand All @@ -114,7 +114,7 @@ class Cell
return (uchar)(total/count);
}

uchar mean_grayscale()
uchar mean_grayscale() const
{
if (_img.channels() > 1)
return 0;
Expand Down
16 changes: 14 additions & 2 deletions src/lib/cimb_translator/CellDrift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace {
}

CellDrift::CellDrift(int x, int y)
: _x(x)
, _y(y)
: _x(x)
, _y(y)
{}

int CellDrift::x() const
Expand All @@ -29,3 +29,15 @@ void CellDrift::updateDrift(int dx, int dy)
_y = std::min(_y, _limit);
_y = std::max(_y, 0-_limit);
}

// static
uint8_t CellDrift::calculate_cooldown(uint8_t previous, uint8_t idx)
{
if (idx == 4)
return 4;
if (idx % 2 == 0)
return 0xFF;
if ((previous xor idx) == 6) // 1 and 7, 3 and 5
Copy link
Owner Author

@sz3 sz3 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be written as four (more) if statements. I'm not sure which is better. Probably could improve the comment.

return 0xFF;
return idx;
}
5 changes: 4 additions & 1 deletion src/lib/cimb_translator/CellDrift.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <array>
#include <cstdint>
#include <utility>

class CellDrift
Expand All @@ -10,9 +11,11 @@ class CellDrift
CellDrift(int x=0, int y=0);

inline static const std::array<std::pair<int, int>, 9> driftPairs = {{
{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {0, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}
{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {0, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}
}};

static uint8_t calculate_cooldown(uint8_t previous, uint8_t idx);

int x() const;
int y() const;

Expand Down
Loading