zxing-cpp v3.2
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 <cstdlib>
12#include <memory>
13#include <stdexcept>
14
15namespace ZXing {
16
18enum class ImageFormat : uint32_t
19{
20 None = 0,
21 Lum = 0x01000000,
22 LumA = 0x02000000,
23 RGB = 0x03000102,
24 BGR = 0x03020100,
25 RGBA = 0x04000102,
26 ARGB = 0x04010203,
27 BGRA = 0x04020100,
28 ABGR = 0x04030201,
29 RGBX [[deprecated("use RGBA")]] = RGBA,
30 XRGB [[deprecated("use ARGB")]] = ARGB,
31 BGRX [[deprecated("use BGRA")]] = BGRA,
32 XBGR [[deprecated("use ABGR")]] = ABGR,
33};
34
36constexpr inline int PixStride(ImageFormat format) { return (static_cast<uint32_t>(format) >> 3*8) & 0xFF; }
37constexpr inline int RedIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 2*8) & 0xFF; }
38constexpr inline int GreenIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 1*8) & 0xFF; }
39constexpr inline int BlueIndex(ImageFormat format) { return (static_cast<uint32_t>(format) >> 0*8) & 0xFF; }
40
41constexpr inline uint8_t RGBToLum(unsigned r, unsigned g, unsigned b)
42{
43 // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
44 // (306*R) >> 10 is approximately equal to R*0.299, and so on.
45 // 0x200 >> 10 is 0.5, it implements rounding.
46 return static_cast<uint8_t>((306 * r + 601 * g + 117 * b + 0x200) >> 10);
47}
49
54{
56protected:
57 const uint8_t* _data = nullptr;
59 int _width = 0, _height = 0, _pixStride = 0, _rowStride = 0;
61
62public:
66 ImageView() = default;
67
78 ImageView(const uint8_t* data, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0)
79 : _data(data),
80 _format(format),
81 _width(width),
82 _height(height),
83 _pixStride(pixStride ? pixStride : PixStride(format)),
84 _rowStride(rowStride ? rowStride : int64_t(width) * _pixStride) // prevent UB sanitizer issues due to integer overflow
85 {
86 if (_data == nullptr)
87 throw std::invalid_argument("Can not construct an ImageView from a NULL pointer");
88
89 if (_width <= 0 || _height <= 0)
90 throw std::invalid_argument("Neither width nor height of ImageView can be less or equal to 0");
91
92 if (std::abs(_pixStride) < PixStride(format))
93 throw std::invalid_argument("Pixel stride can not be smaller than required by the format");
94 }
95
110 ImageView(const uint8_t* data, int size, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0)
112 {
113 if (_rowStride < 0 || _pixStride < 0 || size < int64_t(_height) * _rowStride || _rowStride < int64_t(_width) * _pixStride)
114 throw std::invalid_argument("ImageView parameters are inconsistent (out of bounds)");
115 }
116
117 int width() const { return _width; }
118 int height() const { return _height; }
119 int pixStride() const { return _pixStride; }
120 int rowStride() const { return _rowStride; }
121 ImageFormat format() const { return _format; }
122
123 const uint8_t* data() const { return _data; }
124 const uint8_t* data(int x, int y) const { return _data + y * _rowStride + x * _pixStride; }
125
128 ImageView cropped(int left, int top, int width, int height) const
129 {
130 left = std::clamp(left, 0, _width - 1);
131 top = std::clamp(top, 0, _height - 1);
132 width = width <= 0 ? (_width - left) : std::min(_width - left, width);
133 height = height <= 0 ? (_height - top) : std::min(_height - top, height);
134 return {data(left, top), width, height, _format, _rowStride, _pixStride};
135 }
136
139 ImageView rotated(int degree) const
140 {
141 switch ((degree + 360) % 360) {
142 case 90: return {data(0, _height - 1), _height, _width, _format, _pixStride, -_rowStride};
143 case 180: return {data(_width - 1, _height - 1), _width, _height, _format, -_rowStride, -_pixStride};
144 case 270: return {data(_width - 1, 0), _height, _width, _format, -_pixStride, _rowStride};
145 default: return *this;
146 }
147 }
148
151 ImageView subsampled(int scale) const
152 {
153 return {_data, _width / scale, _height / scale, _format, _rowStride * scale, _pixStride * scale};
154 }
155
156};
157
161class Image : public ImageView
162{
163 std::unique_ptr<uint8_t[]> _memory;
164 Image(std::unique_ptr<uint8_t[]>&& data, int w, int h, ImageFormat f) : ImageView(data.get(), w, h, f), _memory(std::move(data)) {}
165
166public:
167 Image() = default;
168 Image(int w, int h, ImageFormat f = ImageFormat::Lum) : Image(std::make_unique<uint8_t[]>(w * h * PixStride(f)), w, h, f) {}
169};
170
171} // ZXing
172
Image()=default
Image(int w, int h, ImageFormat f=ImageFormat::Lum)
Definition ImageView.h:168
const uint8_t * data() const
Definition ImageView.h:123
int width() const
Definition ImageView.h:117
int pixStride() const
Definition ImageView.h:119
const uint8_t * data(int x, int y) const
Definition ImageView.h:124
int rowStride() const
Definition ImageView.h:120
ImageView rotated(int degree) const
Definition ImageView.h:139
ImageView(const uint8_t *data, int size, int width, int height, ImageFormat format, int rowStride=0, int pixStride=0)
Definition ImageView.h:110
int height() const
Definition ImageView.h:118
ImageFormat format() const
Definition ImageView.h:121
ImageView()=default
ImageView subsampled(int scale) const
Definition ImageView.h:151
ImageView(const uint8_t *data, int width, int height, ImageFormat format, int rowStride=0, int pixStride=0)
Definition ImageView.h:78
ImageView cropped(int left, int top, int width, int height) const
Definition ImageView.h:128
Definition Barcode.h:26
ImageFormat
Supported image formats for ImageView. The format encodes the pixel format and layout information.
Definition ImageView.h:19
@ RGBX
Definition ImageView.h:29
@ XRGB
Definition ImageView.h:30
@ BGR
Definition ImageView.h:24
@ Lum
Definition ImageView.h:21
@ XBGR
Definition ImageView.h:32
@ LumA
Definition ImageView.h:22
@ BGRA
Definition ImageView.h:27
@ None
Definition ImageView.h:20
@ RGB
Definition ImageView.h:23
@ ABGR
Definition ImageView.h:28
@ ARGB
Definition ImageView.h:26
@ RGBA
Definition ImageView.h:25
@ BGRX
Definition ImageView.h:31
@ None
Definition BarcodeFormat.h:103