Friction Ridge Image and Features
API for participating in NIST's Friction Ridge Image and Features Technology Evaluations.
Loading...
Searching...
No Matches
libfrif_common.cpp
Go to the documentation of this file.
1/*
2 * This software was developed at the National Institute of Standards and
3 * Technology (NIST) by employees of the Federal Government in the course
4 * of their official duties. Pursuant to title 17 Section 105 of the
5 * United States Code, this software is not subject to copyright protection
6 * and is in the public domain. NIST assumes no responsibility whatsoever for
7 * its use by other parties, and makes no guarantees, expressed or implied,
8 * about its quality, reliability, or any other characteristic.
9 */
10
11#include <exception>
12
13#include <frif/common.h>
14
16 const uint32_t x_,
17 const uint32_t y_) :
18 x{x_},
19 y{y_}
20{
21
22}
23
24auto
26 const = default;
27
28bool
30 const = default;
31
32FRIF::ReturnStatus::operator bool()
33 const
34 noexcept
35{
36 return (this->result == Result::Success);
37}
38
39static std::string imagePropertiesString(const FRIF::Image &image)
40{
41 return {std::to_string(image.width) + 'x' +
42 std::to_string(image.height) + ", colorspace = " +
43 std::to_string(static_cast<std::underlying_type_t<
44 FRIF::Image::Colorspace>>(image.colorspace)) + ", bpc = " +
45 std::to_string(static_cast<std::underlying_type_t<
46 FRIF::Image::BitsPerChannel>>(image.bpc)) + ", bpp = " +
47 std::to_string(static_cast<std::underlying_type_t<
49}
50
51FRIF::Image::Image() = default;
53 const uint8_t identifier_,
54 const uint16_t width_,
55 const uint16_t height_,
56 const uint16_t ppi_,
57 const Colorspace colorspace_,
58 const BitsPerChannel bpc_,
59 const BitsPerPixel bpp_,
60 const std::vector<std::byte> &pixels_) :
61 identifier{identifier_},
62 width{width_},
63 height{height_},
64 ppi{ppi_},
65 colorspace{colorspace_},
66 bpc{bpc_},
67 bpp{bpp_},
68 pixels{pixels_}
69{
70
71}
72
73void
75 const
76{
77 if (this->width == 0 || this->height == 0)
78 throw std::logic_error{"Unexpected dimensions (" +
79 imagePropertiesString(*this) + ')'};
80
81 decltype(pixels.size()) expected{};
82 switch (colorspace) {
83 case Colorspace::Grayscale:
84 switch (bpc) {
85 case BitsPerChannel::Sixteen:
86 switch (bpp) {
87 case BitsPerPixel::Sixteen:
88 break;
89
90 case BitsPerPixel::Eight:
91 [[fallthrough]];
92 case BitsPerPixel::TwentyFour:
93 [[fallthrough]];
94 case BitsPerPixel::FortyEight:
95 throw std::logic_error{"Unexpected "
96 "combination (" +
97 imagePropertiesString(*this) + ')'};
98 }
99
100 expected = width * height * 2ul;
101 if (pixels.size() != expected)
102 throw std::logic_error{"Unexpected pixel "
103 "length (expected = " +
104 std::to_string(expected) +", actual = " +
105 std::to_string(pixels.size()) + ')'};
106
107 return;
108 case BitsPerChannel::Eight:
109 switch (bpp) {
110 case BitsPerPixel::Eight:
111 break;
112
113 case BitsPerPixel::Sixteen:
114 [[fallthrough]];
115 case BitsPerPixel::TwentyFour:
116 [[fallthrough]];
117 case BitsPerPixel::FortyEight:
118 throw std::logic_error{"Unexpected "
119 "combination (" +
120 imagePropertiesString(*this) + ')'};
121 }
122
123 expected = width * height;
124 if (pixels.size() != expected)
125 throw std::logic_error{"Unexpected pixel "
126 "length (expected = " +
127 std::to_string(expected) +", actual = " +
128 std::to_string(this->pixels.size()) + ')'};
129
130 return;
131 }
132 break;
133
134 case Colorspace::RGB:
135 switch (bpc) {
136 case BitsPerChannel::Sixteen:
137 switch (bpp) {
138 case BitsPerPixel::FortyEight:
139 break;
140
141 case BitsPerPixel::Eight:
142 [[fallthrough]];
143 case BitsPerPixel::Sixteen:
144 [[fallthrough]];
145 case BitsPerPixel::TwentyFour:
146 throw std::logic_error{"Unexpected "
147 "combination (" +
148 imagePropertiesString(*this) + ')'};
149 }
150
151 expected = width * height * (3ul * 2ul);
152 if (pixels.size() != expected)
153 throw std::logic_error{"Unexpected pixel "
154 "length (expected = " +
155 std::to_string(expected) +", actual = " +
156 std::to_string(this->pixels.size()) + ')'};
157
158 return;
159 case BitsPerChannel::Eight:
160 switch (bpp) {
161 case BitsPerPixel::TwentyFour:
162 break;
163
164 case BitsPerPixel::Eight:
165 [[fallthrough]];
166 case BitsPerPixel::Sixteen:
167 [[fallthrough]];
168 case BitsPerPixel::FortyEight:
169 throw std::logic_error{"Unexpected "
170 "combination (" +
171 imagePropertiesString(*this) + ')'};
172 }
173
174 expected = width * height * 3ul;
175 if (pixels.size() != expected)
176 throw std::logic_error{"Unexpected pixel "
177 "length (expected = " +
178 std::to_string(expected) +", actual = " +
179 std::to_string(this->pixels.size()) + ')'};
180
181 return;
182 }
183 break;
184 }
185
186 throw std::logic_error{"Untested "
187 "combination (" + imagePropertiesString(*this) + ')'};
188}
Pixel location in an image.
Definition common.h:192
bool operator==(const Coordinate &) const
auto operator<=>(const Coordinate &) const
Coordinate(const uint32_t x={}, const uint32_t y={})
Coordinate constructor.
Data and metadata for an image.
Definition common.h:58
uint16_t height
Height of the image.
Definition common.h:157
BitsPerPixel bpp
Number of bits comprising a single pixel.
Definition common.h:165
BitsPerPixel
Number of bits comprising a single image pixel.
Definition common.h:61
uint16_t width
Width of the image.
Definition common.h:155
void sanityCheck() const
Validate that the properties of this Image appear to correspond to valid image data.
BitsPerChannel
Number of bits comprising a single color channel of a single pixel.
Definition common.h:74
Colorspace colorspace
Representation of color in each byte within pixels.
Definition common.h:161
BitsPerChannel bpc
Number of bits used by each color component.
Definition common.h:163