zxing-cpp v3.0
Loading...
Searching...
No Matches
ImageView.h
Go to the documentation of this file.
1/*
2* Copyright 2019 Axel Waggershauser
3*/
4// SPDX-License-Identifier: Apache-2.0
5
6#pragma once
7
8#include <algorithm>
9#include <cstdint>
10#include <cstdio>
11#include <memory>
12#include <stdexcept>
13
14namespace ZXing {
15
17enum class ImageFormat : uint32_t
18{
19 None = 0,
20 Lum = 0x01000000,
21 LumA = 0x02000000,
22 RGB = 0x03000102,
23 BGR = 0x03020100,
24 RGBA = 0x04000102,
25 ARGB = 0x04010203,
26 BGRA = 0x04020100,
27 ABGR = 0x04030201,
28 RGBX [[deprecated("use RGBA")]] = RGBA,
29 XRGB [[deprecated("use ARGB")]] = ARGB,
30 BGRX [[deprecated("use BGRA")]] = BGRA,
31 XBGR [[deprecated("use ABGR")]] = ABGR,
32};
33
35constexpr inline int PixStride(ImageFormat format) { return (static_cast<uint32_t>(format) >> 3*8) & 0xFF; }
36constexpr inline int RedIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 2*8) & 0xFF; }
37constexpr inline int GreenIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 1*8) & 0xFF; }
38constexpr inline int BlueIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 0*8) & 0xFF; }
39
40constexpr inline uint8_t RGBToLum(unsigned r, unsigned g, unsigned b)
41{
42 // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
43 // (306*R) >> 10 is approximately equal to R*0.299, and so on.
44 // 0x200 >> 10 is 0.5, it implements rounding.
45 return static_cast<uint8_t>((306 * r + 601 * g + 117 * b + 0x200) >> 10);
46}
48
53{
55protected:
56 const uint8_t* _data = nullptr;
58 int _width = 0, _height = 0, _pixStride = 0, _rowStride = 0;
60
61public:
65 ImageView() = default;
66
77 ImageView(const uint8_t* data, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0)
78 : _data(data),
79 _format(format),
80 _width(width),
81 _height(height),
82 _pixStride(pixStride ? pixStride : PixStride(format)),
83 _rowStride(rowStride ? rowStride : width * _pixStride)
84 {
85 // TODO: [[deprecated]] this check is to prevent existing code from suddenly throwing, remove in 3.1
86 if (_data == nullptr && _width == 0 && _height == 0 && rowStride == 0 && pixStride == 0) {
87 fprintf(stderr, "zxing-cpp deprecation warning: ImageView(nullptr, ...) will throw in the future, use ImageView()\n");
88 return;
89 }
90
91 if (_data == nullptr)
92 throw std::invalid_argument("Can not construct an ImageView from a NULL pointer");
93
94 if (_width <= 0 || _height <= 0)
95 throw std::invalid_argument("Neither width nor height of ImageView can be less or equal to 0");
96 }
97
112 ImageView(const uint8_t* data, int size, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0)
114 {
115 if (_rowStride < 0 || _pixStride < 0 || size < _height * _rowStride)
116 throw std::invalid_argument("ImageView parameters are inconsistent (out of bounds)");
117 }
118
119 int width() const { return _width; }
120 int height() const { return _height; }
121 int pixStride() const { return _pixStride; }
122 int rowStride() const { return _rowStride; }
123 ImageFormat format() const { return _format; }
124
125 const uint8_t* data() const { return _data; }
126 const uint8_t* data(int x, int y) const { return _data + y * _rowStride + x * _pixStride; }
127
130 ImageView cropped(int left, int top, int width, int height) const
131 {
132 left = std::clamp(left, 0, _width - 1);
133 top = std::clamp(top, 0, _height - 1);
134 width = width <= 0 ? (_width - left) : std::min(_width - left, width);
135 height = height <= 0 ? (_height - top) : std::min(_height - top, height);
136 return {data(left, top), width, height, _format, _rowStride, _pixStride};
137 }
138
141 ImageView rotated(int degree) const
142 {
143 switch ((degree + 360) % 360) {
144 case 90: return {data(0, _height - 1), _height, _width, _format, _pixStride, -_rowStride};
145 case 180: return {data(_width - 1, _height - 1), _width, _height, _format, -_rowStride, -_pixStride};
146 case 270: return {data(_width - 1, 0), _height, _width, _format, -_pixStride, _rowStride};
147 default: return *this;
148 }
149 }
150
153 ImageView subsampled(int scale) const
154 {
155 return {_data, _width / scale, _height / scale, _format, _rowStride * scale, _pixStride * scale};
156 }
157
158};
159
163class Image : public ImageView
164{
165 std::unique_ptr<uint8_t[]> _memory;
166 Image(std::unique_ptr<uint8_t[]>&& data, int w, int h, ImageFormat f) : ImageView(data.get(), w, h, f), _memory(std::move(data)) {}
167
168public:
169 Image() = default;
170 Image(int w, int h, ImageFormat f = ImageFormat::Lum) : Image(std::make_unique<uint8_t[]>(w * h * PixStride(f)), w, h, f) {}
171};
172
173} // ZXing
174
Image()=default
Image(int w, int h, ImageFormat f=ImageFormat::Lum)
Definition ImageView.h:170
const uint8_t * data() const
Definition ImageView.h:125
int width() const
Definition ImageView.h:119
int pixStride() const
Definition ImageView.h:121
const uint8_t * data(int x, int y) const
Definition ImageView.h:126
int rowStride() const
Definition ImageView.h:122
ImageView rotated(int degree) const
Definition ImageView.h:141
ImageView(const uint8_t *data, int size, int width, int height, ImageFormat format, int rowStride=0, int pixStride=0)
Definition ImageView.h:112
int height() const
Definition ImageView.h:120
ImageFormat format() const
Definition ImageView.h:123
ImageView()=default
ImageView subsampled(int scale) const
Definition ImageView.h:153
ImageView(const uint8_t *data, int width, int height, ImageFormat format, int rowStride=0, int pixStride=0)
Definition ImageView.h:77
ImageView cropped(int left, int top, int width, int height) const
Definition ImageView.h:130
Definition Barcode.h:26
ImageFormat
Supported image formats for ImageView. The format encodes the pixel format and layout information.
Definition ImageView.h:18
@ RGBX
Definition ImageView.h:28
@ XRGB
Definition ImageView.h:29
@ BGR
Definition ImageView.h:23
@ Lum
Definition ImageView.h:20
@ XBGR
Definition ImageView.h:31
@ LumA
Definition ImageView.h:21
@ BGRA
Definition ImageView.h:26
@ None
Definition ImageView.h:19
@ RGB
Definition ImageView.h:22
@ ABGR
Definition ImageView.h:27
@ ARGB
Definition ImageView.h:25
@ RGBA
Definition ImageView.h:24
@ BGRX
Definition ImageView.h:30
@ None
Definition BarcodeFormat.h:100