Ocean
QRCode.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 QRCode;
26 
27 /// Definition of a vector of QR codes
28 typedef std::vector<QRCode> QRCodes;
29 
30 /**
31  * Definition of a QR code
32  * @ingroup cvdetectorqrcodes
33  */
34 class QRCode final : public QRCodeBase
35 {
36  friend class QRCodeEncoder;
37  friend class QRCodeDecoder;
38 
39  public:
40 
41  /// Indicates the smallest valid version number of QR codes.
42  static constexpr unsigned int MIN_VERSION = 1u;
43 
44  /// Indicates the largest valid version number of QR codes.
45  static constexpr unsigned int MAX_VERSION = 40u;
46 
47  public:
48 
49  /**
50  * Creates an invalid QR code instance
51  */
52  QRCode() = default;
53 
54  /**
55  * Returns whether this is a valid QR code instance
56  * @return True if this is a valid QR code instance, otherwise false
57  */
58  inline bool isValid() const override;
59 
60  /**
61  * Returns the number of modules per side of the QR code
62  * @return The number of modules per side
63  */
64  inline unsigned int modulesPerSide() const override;
65 
66  /**
67  * Computes the number of modules per side of a QR code given its version
68  * @param version The version number for which the number of modules per side will be computed, range: [MIN_VERSION, MAX_VERSION]
69  * @return The number of modules per side
70  */
71  static inline unsigned int modulesPerSide(const unsigned int version);
72 
73  protected:
74 
75  /**
76  * Creates an QR code instance
77  * @param data The plain data of this QR code, must be valid
78  * @param encodingMode The encoding mode that was used to encode the data, must not be `EM_INVALID_ENCODING_MODE`
79  * @param errorCorrectionCapacity The error correction capacity that was used to generate this QR code, must not be `ECC_INVALID`
80  * @param modules The modules of the QR code that store the data, must be valid
81  * @param version The version of the QR code, range: [MIN_VERSION, MAX_VERSION]
82  * @sa QRCodeEncoder, QRCodeDecoder
83  */
84  inline explicit QRCode(std::vector<uint8_t>&& data, const EncodingMode encodingMode, const ErrorCorrectionCapacity errorCorrectionCapacity, std::vector<uint8_t>&& modules, const unsigned int version);
85 };
86 
87 inline QRCode::QRCode(std::vector<uint8_t>&& data, const EncodingMode encodingMode, const ErrorCorrectionCapacity errorCorrectionCapacity, std::vector<uint8_t>&& modules, const unsigned int version) :
88  QRCodeBase(CT_STANDARD, std::move(data), encodingMode, errorCorrectionCapacity, std::move(modules), version)
89 {
90  ocean_assert(isValid());
91 }
92 
93 inline bool QRCode::isValid() const
94 {
95  if (codeType_ != CT_STANDARD)
96  {
97  return false;
98  }
99 
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  if (version_ < MIN_VERSION || version_ > MAX_VERSION)
116  {
117  return false;
118  }
119 
120  if (modules_.size() != modulesPerSide() * modulesPerSide())
121  {
122  ocean_assert(false && "This should never happen!");
123  return false;
124  }
125 
126  return true;
127 }
128 
129 inline unsigned int QRCode::modulesPerSide() const
130 {
131  return modulesPerSide(version());
132 }
133 
134 inline unsigned int QRCode::modulesPerSide(const unsigned int version)
135 {
136  if (version >= MIN_VERSION && version <= MAX_VERSION)
137  {
138  return 4u * version + 17u;
139  }
140 
141  return 0u;
142 }
143 
144 } // namespace QRCodes
145 
146 } // namespace Detector
147 
148 } // namespace CV
149 
150 } // namespace Ocean
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_STANDARD
Indicates a standard QR codes.
Definition: QRCodeBase.h:43
unsigned int version() const
Returns the version of the QR code.
Definition: QRCodeBase.h:305
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_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_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
Definition of a QR code decoder.
Definition: QRCodeDecoder.h:31
This class implements an encoder and decoder for QR codes.
Definition: QRCodeEncoder.h:37
Definition of a QR code.
Definition: QRCode.h:35
static constexpr unsigned int MIN_VERSION
Indicates the smallest valid version number of QR codes.
Definition: QRCode.h:42
static constexpr unsigned int MAX_VERSION
Indicates the largest valid version number of QR codes.
Definition: QRCode.h:45
unsigned int modulesPerSide() const override
Returns the number of modules per side of the QR code.
Definition: QRCode.h:129
bool isValid() const override
Returns whether this is a valid QR code instance.
Definition: QRCode.h:93
QRCode()=default
Creates an invalid QR code instance.
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