- u_async.c: process immediate_queue BEFORE epoll_wait in uasync_poll - stcp_link.c: defensive NULL etcp_conn/etcp_link in stcp_link_close - utun_instance.c: move stcp_server_list_destroy_all to Phase J (before ETCP), add deferred drain in both Phase J and L - stcp.h/c: STCP_HS_ENC_CLIENT 6→38, STCP_HS_ENC_SERVER 7→39, exchange ed25519_pubkey in both handshake directions - etcp_connections.c: copy peer_ed25519 from STCP to ETCP_CONN in etcp_link_enter_ready_tcp; guard link->etcp in tcp_link_close_cb - chat_sync.c: fix lk->conn NULL dereference for TCP links - test_stcp.c: update stcp_server_create/stcp_client_connect callsites - tools/chat_tcp_test: change transport udp→tcp
112 lines
2.9 KiB
C++
112 lines
2.9 KiB
C++
/*
|
|
* Copyright 2020 Axel Waggershauser
|
|
*/
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
#include "BinaryBitmap.h"
|
|
|
|
#include "BitMatrix.h"
|
|
|
|
namespace ZXing {
|
|
|
|
struct BinaryBitmap::Cache
|
|
{
|
|
std::shared_ptr<const BitMatrix> matrix, transposed;
|
|
};
|
|
|
|
BitMatrix BinaryBitmap::binarize(const uint8_t threshold) const
|
|
{
|
|
BitMatrix res(width(), height());
|
|
|
|
if (_buffer.pixStride() == 1 && _buffer.rowStride() == _buffer.width()) {
|
|
// Specialize for a packed buffer with pixStride 1 to support auto vectorization (16x speedup on AVX2)
|
|
auto dst = res.row(0).begin();
|
|
for (auto src = _buffer.data(0, 0), end = _buffer.data(0, height()); src != end; ++src, ++dst)
|
|
*dst = (*src <= threshold) * BitMatrix::SET_V;
|
|
} else {
|
|
auto processLine = [&res, threshold](int y, const auto* src, const int stride) {
|
|
for (auto& dst : res.row(y)) {
|
|
dst = (*src <= threshold) * BitMatrix::SET_V;
|
|
src += stride;
|
|
}
|
|
};
|
|
for (int y = 0; y < res.height(); ++y) {
|
|
auto src = _buffer.data(0, y) + GreenIndex(_buffer.format());
|
|
// Specialize the inner loop for strides 1 and 4 to support auto vectorization
|
|
switch (_buffer.pixStride()) {
|
|
case 1: processLine(y, src, 1); break;
|
|
case 4: processLine(y, src, 4); break;
|
|
default: processLine(y, src, _buffer.pixStride()); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
BinaryBitmap::BinaryBitmap(const ImageView& buffer) : _cache(new Cache), _buffer(buffer) {}
|
|
|
|
BinaryBitmap::~BinaryBitmap() = default;
|
|
|
|
const BitMatrix* BinaryBitmap::getBitMatrix(bool transposed) const
|
|
{
|
|
if (!_cache->matrix) {
|
|
_cache->matrix = getBlackMatrix();
|
|
}
|
|
if (transposed) {
|
|
if (!_cache->transposed) {
|
|
auto copy = std::make_shared<BitMatrix>(_cache->matrix->copy());
|
|
copy->rotate90();
|
|
_cache->transposed = std::move(copy);
|
|
}
|
|
return _cache->transposed.get();
|
|
}
|
|
return _cache->matrix.get();
|
|
}
|
|
|
|
void BinaryBitmap::invert()
|
|
{
|
|
if (_cache->matrix)
|
|
const_cast<BitMatrix*>(_cache->matrix.get())->flipAll();
|
|
|
|
if (_cache->transposed)
|
|
const_cast<BitMatrix*>(_cache->transposed.get())->flipAll();
|
|
|
|
_inverted = !_inverted;
|
|
}
|
|
|
|
template <typename F>
|
|
void SumFilter(const BitMatrix& in, BitMatrix& out, F func)
|
|
{
|
|
assert(in.height() >= 3);
|
|
|
|
const auto* in0 = in.row(0).begin();
|
|
const auto* in1 = in.row(1).begin();
|
|
const auto* in2 = in.row(2).begin();
|
|
|
|
for (auto *out1 = out.row(1).begin() + 1, *end = out.row(out.height() - 1).begin() - 1; out1 != end; ++in0, ++in1, ++in2, ++out1) {
|
|
int sum = 0;
|
|
for (int j = 0; j < 3; ++j)
|
|
sum += in0[j] + in1[j] + in2[j];
|
|
|
|
*out1 = func(sum);
|
|
}
|
|
}
|
|
|
|
void BinaryBitmap::close()
|
|
{
|
|
if (_cache->matrix) {
|
|
auto& matrix = *const_cast<BitMatrix*>(_cache->matrix.get());
|
|
BitMatrix tmp(matrix.width(), matrix.height());
|
|
|
|
// dilate
|
|
SumFilter(matrix, tmp, [](int sum) { return (sum > 0 * BitMatrix::SET_V) * BitMatrix::SET_V; });
|
|
// erode
|
|
SumFilter(tmp, matrix, [](int sum) { return (sum == 9 * BitMatrix::SET_V) * BitMatrix::SET_V; });
|
|
}
|
|
_cache->transposed.reset();
|
|
_closed = true;
|
|
}
|
|
|
|
} // ZXing
|