Skip to content

Commit

Permalink
ImageView: add data() member and new Image class
Browse files Browse the repository at this point in the history
  • Loading branch information
axxel committed Mar 1, 2024
1 parent 91a383d commit 8d7a5a2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions core/src/HybridBinarizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static std::shared_ptr<BitMatrix> ThresholdImage(const ImageView iv, const Matri
int yoffset = std::min(y * BLOCK_SIZE, iv.height() - BLOCK_SIZE);
for (int x = 0; x < thresholds.width(); x++) {
int xoffset = std::min(x * BLOCK_SIZE, iv.width() - BLOCK_SIZE);
ThresholdBlock(iv.data(0, 0), xoffset, yoffset, thresholds(x, y), iv.rowStride(), *matrix);
ThresholdBlock(iv.data(), xoffset, yoffset, thresholds(x, y), iv.rowStride(), *matrix);

#ifndef NDEBUG
for (int yy = 0; yy < 8; ++yy)
Expand Down Expand Up @@ -296,7 +296,7 @@ std::shared_ptr<const BitMatrix> HybridBinarizer::getBlackMatrix() const
auto thrs = SmoothThresholds(BlockThresholds(_buffer));
return ThresholdImage(_buffer, thrs);
#else
const uint8_t* luminances = _buffer.data(0, 0);
const uint8_t* luminances = _buffer.data();
int subWidth = (width() + BLOCK_SIZE - 1) / BLOCK_SIZE; // ceil(width/BS)
int subHeight = (height() + BLOCK_SIZE - 1) / BLOCK_SIZE; // ceil(height/BS)
auto blackPoints =
Expand Down
12 changes: 12 additions & 0 deletions core/src/ImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <stdexcept>

namespace ZXing {
Expand Down Expand Up @@ -104,6 +105,7 @@ class ImageView
int rowStride() const { return _rowStride; }
ImageFormat format() const { return _format; }

const uint8_t* data() const { return _data; }
const uint8_t* data(int x, int y) const { return _data + y * _rowStride + x * _pixStride; }

ImageView cropped(int left, int top, int width, int height) const
Expand Down Expand Up @@ -132,5 +134,15 @@ class ImageView

};

class Image : public ImageView
{
std::unique_ptr<uint8_t[]> _memory;
Image(std::unique_ptr<uint8_t[]>&& data, int w, int h, ImageFormat f) : ImageView(data.get(), w, h, f), _memory(std::move(data)) {}

public:
Image() = default;
Image(int w, int h, ImageFormat f = ImageFormat::Lum) : Image(std::make_unique<uint8_t[]>(w * h * PixStride(f)), w, h, f) {}
};

} // ZXing

14 changes: 4 additions & 10 deletions core/src/ReadBarcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

namespace ZXing {

class LumImage : public ImageView
class LumImage : public Image
{
std::unique_ptr<uint8_t[]> _memory;
LumImage(std::unique_ptr<uint8_t[]>&& data, int w, int h)
: ImageView(data.get(), w, h, ImageFormat::Lum), _memory(std::move(data))
{}

public:
LumImage() = default;
LumImage(int w, int h) : LumImage(std::make_unique<uint8_t[]>(w * h), w, h) {}
using Image::Image;

uint8_t* data() { return _memory.get(); }
uint8_t* data() { return const_cast<uint8_t*>(Image::data()); }
};

template<typename P>
Expand Down Expand Up @@ -148,7 +142,7 @@ Barcodes ReadBarcodes(const ImageView& _iv, const ReaderOptions& opts)
if (sizeof(PatternType) < 4 && (_iv.width() > 0xffff || _iv.height() > 0xffff))
throw std::invalid_argument("Maximum image width/height is 65535");

if (!_iv.data(0, 0) || _iv.width() * _iv.height() == 0)
if (!_iv.data() || _iv.width() * _iv.height() == 0)
throw std::invalid_argument("ImageView is null/empty");

LumImage lum;
Expand Down
2 changes: 1 addition & 1 deletion example/ZXingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ int main(int argc, char* argv[])
}

if (Size(cli.filePaths) == 1 && !cli.outPath.empty())
stbi_write_png(cli.outPath.c_str(), image.width(), image.height(), 3, image.data(0, 0), image.rowStride());
stbi_write_png(cli.outPath.c_str(), image.width(), image.height(), 3, image.data(), image.rowStride());

#ifdef NDEBUG
if (getenv("MEASURE_PERF")) {
Expand Down

0 comments on commit 8d7a5a2

Please sign in to comment.