Ocean
MicroQRCode.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
11 
12 namespace Ocean
13 {
14 
15 namespace CV
16 {
17 
18 namespace Detector
19 {
20 
21 namespace QRCodes
22 {
23 
24 /// Forward declaration
25 class MicroQRCode;
26 
27 /// Definition of a vector of Micro QR codes
28 typedef std::vector<MicroQRCode> MicroQRCodes;
29 
30 /**
31  * Definition of a Micro QR code
32  * @ingroup cvdetectorqrcodes
33  */
34 class MicroQRCode final : public QRCodeBase
35 {
36  friend class MicroQRCodeEncoder;
37 
38  public:
39 
40  /// Indicates the smallest valid version number of Micro QR codes.
41  static constexpr unsigned int MIN_VERSION = 1u;
42 
43  /// Indicates the largest valid version number of Micro QR codes.
44  static constexpr unsigned int MAX_VERSION = 4u;
45 
46  public:
47 
48  /**
49  * Creates an invalid Micro QR code instance
50  */
51  MicroQRCode() = default;
52 
53  /**
54  * Returns whether this is a valid QR code instance
55  * @return True if this is a valid QR code instance, otherwise false
56  */
57  inline bool isValid() const override;
58 
59  /**
60  * Returns the version of the Micro QR code as a string
61  * @return The version string
62  */
63  inline std::string versionString() const override;
64 
65  /**
66  * Returns the number of modules per side of the Micro QR code
67  * @return The number of modules per side
68  */
69  inline unsigned int modulesPerSide() const override;
70 
71  /**
72  * Computes the number of modules per side of a Micro QR code given its version
73  * @param version The version number for which the number of modules per side will be computed, range: [MIN_VERSION, MAX_VERSION]
74  * @return The number of modules per side
75  */
76  static inline unsigned int modulesPerSide(const unsigned int version);
77 
78  protected:
79 
80  /**
81  * Creates a Micro QR code instance
82  * @param data The plain data of this QR code, must be valid
83  * @param encodingMode The encoding mode that was used to encode the data, must not be `EM_INVALID_ENCODING_MODE`
84  * @param errorCorrectionCapacity The error correction capacity that was used to generate this Micro QR code, must not be `ECC_INVALID`
85  * @param modules The modules of the Micro QR code that store the data, must be valid
86  * @param version The version of the Micro QR code, range: [MIN_VERSION, MAX_VERSION]
87  * @sa MicroQRCodeEncoder, QRCodeDecoder
88  */
89  inline explicit MicroQRCode(std::vector<uint8_t>&& data, const EncodingMode encodingMode, const ErrorCorrectionCapacity errorCorrectionCapacity, std::vector<uint8_t>&& modules, const unsigned int version);
90 };
91 
92 inline MicroQRCode::MicroQRCode(std::vector<uint8_t>&& data, const EncodingMode encodingMode, const ErrorCorrectionCapacity errorCorrectionCapacity, std::vector<uint8_t>&& modules, const unsigned int version) :
93  QRCodeBase(CT_MICRO, std::move(data), encodingMode, errorCorrectionCapacity, std::move(modules), version)
94 {
95  ocean_assert(isValid());
96 }
97 
98 inline bool MicroQRCode::isValid() const
99 {
100  if (codeType_ != CT_MICRO)
101  {
102  return false;
103  }
104 
106  {
107  return false;
108  }
109 
110  if (data_.empty() || modules_.empty())
111  {
112  return false;
113  }
114 
115  // Version and version-specific format checks
116  if (version_ == 1u && encodingMode_ != EM_NUMERIC)
117  {
118  return false;
119  }
120 
122  {
123  return false;
124  }
125 
127  {
128  return false;
129  }
130 
132  {
133  return false;
134  }
135 
136  if (errorCorrectionCapacity_ == ECC_25 && version_ != 4u)
137  {
138  return false;
139  }
140 
142  {
143  return false;
144  }
145 
146  if (version_ < MIN_VERSION || version_ > MAX_VERSION)
147  {
148  ocean_assert(false && "This should never happen!");
149  return false;
150  }
151 
152  if (modules_.size() != modulesPerSide() * modulesPerSide())
153  {
154  ocean_assert(false && "This should never happen!");
155  return false;
156  }
157 
158  return true;
159 }
160 
161 inline std::string MicroQRCode::versionString() const
162 {
163  return "M" + QRCodeBase::versionString();
164 }
165 
166 inline unsigned int MicroQRCode::modulesPerSide() const
167 {
168  return modulesPerSide(version());
169 }
170 
171 inline unsigned int MicroQRCode::modulesPerSide(const unsigned int version)
172 {
173  if (version >= MIN_VERSION && version <= MAX_VERSION)
174  {
175  return 2u * version + 9u;
176  }
177 
178  return 0u;
179 }
180 
181 } // namespace QRCodes
182 
183 } // namespace Detector
184 
185 } // namespace CV
186 
187 } // namespace Ocean
This class implements an encoder and decoder for QR codes.
Definition: MicroQRCodeEncoder.h:37
Definition of a Micro QR code.
Definition: MicroQRCode.h:35
MicroQRCode()=default
Creates an invalid Micro QR code instance.
bool isValid() const override
Returns whether this is a valid QR code instance.
Definition: MicroQRCode.h:98
std::string versionString() const override
Returns the version of the Micro QR code as a string.
Definition: MicroQRCode.h:161
static constexpr unsigned int MIN_VERSION
Indicates the smallest valid version number of Micro QR codes.
Definition: MicroQRCode.h:41
unsigned int modulesPerSide() const override
Returns the number of modules per side of the Micro QR code.
Definition: MicroQRCode.h:166
static constexpr unsigned int MAX_VERSION
Indicates the largest valid version number of Micro QR codes.
Definition: MicroQRCode.h:44
Base class for QR code implementations.
Definition: QRCodeBase.h:32
CodeType codeType_
The variant of this QR code.
Definition: QRCodeBase.h:246
std::vector< uint8_t > data_
The plain data.
Definition: QRCodeBase.h:249
ErrorCorrectionCapacity errorCorrectionCapacity_
The error correction capacity that was used to generated this QR code.
Definition: QRCodeBase.h:255
const std::vector< uint8_t > & modules() const
Returns the modules of this QR code The modules are stored in a vector and will have modulesPerSide()...
Definition: QRCodeBase.h:290
std::vector< uint8_t > modules_
The modules of the QR code that store the data.
Definition: QRCodeBase.h:258
@ CT_MICRO
Indicates a Micro QR codes.
Definition: QRCodeBase.h:45
virtual std::string versionString() const
Returns the version of the QR code as a string.
Definition: QRCodeBase.h:310
unsigned int version() const
Returns the version of the QR code.
Definition: QRCodeBase.h:305
unsigned int version_
The version of the QR code.
Definition: QRCodeBase.h:261
const std::vector< uint8_t > & data() const
Returns a constant reference to the plain data of the QR code.
Definition: QRCodeBase.h:280
EncodingMode
Definition of encoding modes.
Definition: QRCodeBase.h:72
@ EM_ALPHANUMERIC
Mode that supports A-Z, 0-9 and a few others, cf. ALPHANUMERIC_CHARSET
Definition: QRCodeBase.h:76
@ EM_NUMERIC
Mode that supports digits 0-9.
Definition: QRCodeBase.h:74
@ EM_BYTE
Mode that represents data as a sequence of bytes.
Definition: QRCodeBase.h:78
@ EM_INVALID_ENCODING_MODE
Indicator for an invalid encoding mode.
Definition: QRCodeBase.h:92
ErrorCorrectionCapacity
Enumeration of the levels of error correction The value of the enums correspond to the standard-defin...
Definition: QRCodeBase.h:53
@ ECC_30
Indicates that 30% of the modules reserved error correction.
Definition: QRCodeBase.h:61
@ ECC_25
Indicates that 25% of the modules reserved error correction.
Definition: QRCodeBase.h:59
@ ECC_INVALID
Indicator for an invalid error correction capacity.
Definition: QRCodeBase.h:65
@ ECC_DETECTION_ONLY
Indicates that the capacity is limited to error detection only (used only by Micro QR Code version M1...
Definition: QRCodeBase.h:63
ErrorCorrectionCapacity errorCorrectionCapacity() const
Returns the error correction capacity of the QR code.
Definition: QRCodeBase.h:300
EncodingMode encodingMode_
The encoding mode that was used to generate this QR code.
Definition: QRCodeBase.h:252
EncodingMode encodingMode() const
Returns the encoding mode of the QR code.
Definition: QRCodeBase.h:295
std::vector< MicroQRCode > MicroQRCodes
Definition of a vector of Micro QR codes.
Definition: MicroQRCode.h:25
std::vector< QRCode > QRCodes
Definition of a vector of QR codes.
Definition: QRCode.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15