-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathencoded_image.cpp
More file actions
341 lines (309 loc) · 12.2 KB
/
Copy pathencoded_image.cpp
File metadata and controls
341 lines (309 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "encoded_image.hpp"
#include "exif.hpp"
#include <algorithm>
#include <climits>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <limits>
#include <memory>
#include <string>
namespace {
enum class DecodeAllocationFailure {
none,
budget_exceeded,
size_overflow,
system_allocation_failed,
accounting_error,
};
struct DecodeBudget {
std::size_t limit = 0;
std::size_t current = 0;
std::size_t peak = 0;
DecodeAllocationFailure failure = DecodeAllocationFailure::none;
};
struct alignas(std::max_align_t) AllocationHeader {
std::size_t size = 0;
};
thread_local DecodeBudget* active_decode_budget = nullptr;
class DecodeBudgetScope {
public:
explicit DecodeBudgetScope(DecodeBudget* budget)
: previous_(active_decode_budget) {
active_decode_budget = budget;
}
~DecodeBudgetScope() { active_decode_budget = previous_; }
DecodeBudgetScope(const DecodeBudgetScope&) = delete;
DecodeBudgetScope& operator=(const DecodeBudgetScope&) = delete;
private:
DecodeBudget* previous_ = nullptr;
};
bool allocation_size(std::size_t requested, std::size_t* total) {
if (requested > std::numeric_limits<std::size_t>::max() -
sizeof(AllocationHeader)) {
return false;
}
*total = requested + sizeof(AllocationHeader);
return true;
}
void mark_allocation_failure(DecodeAllocationFailure failure) {
if (active_decode_budget != nullptr &&
active_decode_budget->failure == DecodeAllocationFailure::none) {
active_decode_budget->failure = failure;
}
}
void* stbi_budget_malloc(std::size_t requested) {
std::size_t total = 0;
if (!allocation_size(requested, &total)) {
mark_allocation_failure(DecodeAllocationFailure::size_overflow);
return nullptr;
}
if (active_decode_budget != nullptr) {
if (active_decode_budget->current > active_decode_budget->limit ||
total > active_decode_budget->limit - active_decode_budget->current) {
mark_allocation_failure(DecodeAllocationFailure::budget_exceeded);
return nullptr;
}
}
auto* header = static_cast<AllocationHeader*>(std::malloc(total));
if (header == nullptr) {
mark_allocation_failure(DecodeAllocationFailure::system_allocation_failed);
return nullptr;
}
header->size = total;
if (active_decode_budget != nullptr) {
active_decode_budget->current += total;
active_decode_budget->peak =
std::max(active_decode_budget->peak, active_decode_budget->current);
}
return header + 1;
}
void stbi_budget_free(void* pointer) {
if (pointer == nullptr) return;
auto* header = static_cast<AllocationHeader*>(pointer) - 1;
if (active_decode_budget != nullptr) {
if (header->size > active_decode_budget->current) {
mark_allocation_failure(DecodeAllocationFailure::accounting_error);
active_decode_budget->current = 0;
} else {
active_decode_budget->current -= header->size;
}
}
std::free(header);
}
void* stbi_budget_realloc(void* pointer, std::size_t old_size,
std::size_t requested) {
(void)old_size;
if (pointer == nullptr) return stbi_budget_malloc(requested);
if (requested == 0) {
stbi_budget_free(pointer);
return nullptr;
}
auto* old_header = static_cast<AllocationHeader*>(pointer) - 1;
const std::size_t old_total = old_header->size;
std::size_t new_total = 0;
if (!allocation_size(requested, &new_total)) {
mark_allocation_failure(DecodeAllocationFailure::size_overflow);
return nullptr;
}
if (active_decode_budget != nullptr) {
if (old_total > active_decode_budget->current) {
mark_allocation_failure(DecodeAllocationFailure::accounting_error);
return nullptr;
}
if (new_total > old_total &&
(active_decode_budget->current > active_decode_budget->limit ||
new_total - old_total > active_decode_budget->limit -
active_decode_budget->current)) {
mark_allocation_failure(DecodeAllocationFailure::budget_exceeded);
return nullptr;
}
}
auto* new_header =
static_cast<AllocationHeader*>(std::realloc(old_header, new_total));
if (new_header == nullptr) {
mark_allocation_failure(DecodeAllocationFailure::system_allocation_failed);
return nullptr;
}
new_header->size = new_total;
if (active_decode_budget != nullptr) {
active_decode_budget->current =
active_decode_budget->current - old_total + new_total;
active_decode_budget->peak =
std::max(active_decode_budget->peak, active_decode_budget->current);
}
return new_header + 1;
}
} // namespace
#define STB_IMAGE_STATIC
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_NO_STDIO
#define STBI_FAILURE_USERMSG
#define STBI_MALLOC(size) stbi_budget_malloc(size)
#define STBI_REALLOC_SIZED(pointer, old_size, new_size) \
stbi_budget_realloc(pointer, old_size, new_size)
#define STBI_FREE(pointer) stbi_budget_free(pointer)
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
namespace light_ocr::node {
namespace {
Result<DecodedImage> failure(ErrorCode code, std::string message,
std::string detail = {}) {
return Result<DecodedImage>::failure(
Error{code, std::move(message), std::move(detail)});
}
std::string decoder_detail() {
const char* reason = stbi_failure_reason();
return reason == nullptr ? std::string{} : std::string(reason);
}
Result<DecodedImage> allocation_failure(const DecodeBudget& budget) {
switch (budget.failure) {
case DecodeAllocationFailure::budget_exceeded:
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image decoder exceeded its memory budget");
case DecodeAllocationFailure::size_overflow:
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image decoder allocation size overflowed");
case DecodeAllocationFailure::system_allocation_failed:
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image decoder ran out of memory");
case DecodeAllocationFailure::accounting_error:
return failure(ErrorCode::internal_error,
"Encoded image decoder memory accounting failed");
case DecodeAllocationFailure::none:
break;
}
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image decoder exceeded its memory budget");
}
} // namespace
Result<DecodedImage> decode_encoded_image(
const std::vector<std::uint8_t>& encoded,
const ResourceLimits& limits,
bool apply_exif) noexcept {
try {
if (encoded.empty()) {
return failure(ErrorCode::invalid_image, "Encoded image is empty");
}
if (encoded.size() > static_cast<std::size_t>(INT_MAX)) {
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image exceeds decoder limits");
}
int width = 0;
int height = 0;
int source_channels = 0;
const auto* data = encoded.data();
const int size = static_cast<int>(encoded.size());
const auto budget_limit = static_cast<std::size_t>(std::min<std::uint64_t>(
limits.max_temporary_bytes,
std::numeric_limits<std::size_t>::max()));
DecodeBudget budget{budget_limit};
DecodeBudgetScope budget_scope(&budget);
if (stbi_info_from_memory(data, size, &width, &height, &source_channels) == 0 ||
width <= 0 || height <= 0) {
if (budget.failure != DecodeAllocationFailure::none) {
return allocation_failure(budget);
}
return failure(ErrorCode::invalid_image,
"Input is not a supported JPEG or PNG image", decoder_detail());
}
const auto decoded_width = static_cast<std::uint64_t>(width);
const auto decoded_height = static_cast<std::uint64_t>(height);
if (decoded_width > limits.max_width || decoded_height > limits.max_height ||
decoded_width > std::numeric_limits<std::uint64_t>::max() / decoded_height ||
decoded_width * decoded_height > limits.max_pixels) {
return failure(ErrorCode::resource_limit_exceeded,
"Decoded image dimensions exceed engine limits");
}
constexpr std::uint64_t kOutputChannels = 3;
const std::uint64_t pixels = decoded_width * decoded_height;
if (pixels > std::numeric_limits<std::uint64_t>::max() / kOutputChannels) {
return failure(ErrorCode::resource_limit_exceeded,
"Decoded image byte size overflows");
}
const std::uint64_t decoded_bytes = pixels * kOutputChannels;
// The decoded RGB pixels coexist first with stb's output and later with
// Core's BGR conversion, so reserve for two full decoded buffers.
if (decoded_bytes > limits.max_temporary_bytes / 2 ||
decoded_bytes > std::numeric_limits<std::size_t>::max()) {
return failure(ErrorCode::resource_limit_exceeded,
"Decoded image exceeds the temporary memory budget");
}
using StbiPixels = std::unique_ptr<stbi_uc, decltype(&stbi_image_free)>;
StbiPixels pixels_data(
stbi_load_from_memory(data, size, &width, &height, &source_channels,
static_cast<int>(kOutputChannels)),
stbi_image_free);
if (!pixels_data) {
if (budget.failure != DecodeAllocationFailure::none) {
return allocation_failure(budget);
}
return failure(ErrorCode::invalid_image, "Failed to decode JPEG or PNG image",
decoder_detail());
}
if (static_cast<std::uint64_t>(width) != decoded_width ||
static_cast<std::uint64_t>(height) != decoded_height) {
return failure(ErrorCode::invalid_image,
"Encoded image dimensions changed during decoding");
}
DecodedImage result;
const auto decoded_size = static_cast<std::size_t>(decoded_bytes);
if (budget.current > budget.limit ||
decoded_size > budget.limit - budget.current) {
budget.failure = DecodeAllocationFailure::budget_exceeded;
return allocation_failure(budget);
}
result.bytes.assign(pixels_data.get(),
pixels_data.get() + decoded_size);
pixels_data.reset();
if (budget.current != 0) {
return failure(ErrorCode::internal_error,
"Encoded image decoder retained temporary allocations");
}
result.width = static_cast<std::uint32_t>(decoded_width);
result.height = static_cast<std::uint32_t>(decoded_height);
result.stride = static_cast<std::size_t>(decoded_width * kOutputChannels);
result.pixel_format = PixelFormat::rgb8;
// EXIF orientation correction (D106, D-N1-5): parse the orientation tag
// from the original JPEG bytes and apply the pixel transform before
// returning the decoded image to the recognition pipeline.
if (apply_exif) {
const std::uint16_t orientation = exif::parse_orientation(encoded);
if (orientation != 1) {
result = exif::apply_orientation(std::move(result), orientation);
}
}
return Result<DecodedImage>::success(std::move(result));
} catch (const std::bad_alloc&) {
return failure(ErrorCode::resource_limit_exceeded,
"Encoded image decoding ran out of memory");
} catch (const std::exception& exception) {
return failure(ErrorCode::internal_error,
"Unexpected encoded image decoding failure", exception.what());
} catch (...) {
return failure(ErrorCode::internal_error,
"Unknown encoded image decoding failure");
}
}
DecodedImage crop_decoded_image(DecodedImage image, const Rect& region) noexcept {
const std::uint32_t channels = 3; // rgb8
DecodedImage out;
out.width = region.width;
out.height = region.height;
out.stride = static_cast<std::size_t>(region.width) * channels;
out.pixel_format = PixelFormat::rgb8;
out.bytes.resize(static_cast<std::size_t>(region.width) * region.height * channels);
for (std::uint32_t row = 0; row < region.height; ++row) {
const std::uint8_t* src =
image.bytes.data() +
static_cast<std::size_t>(region.y + row) * image.stride +
static_cast<std::size_t>(region.x) * channels;
std::uint8_t* dst = out.bytes.data() +
static_cast<std::size_t>(row) * out.stride;
std::memcpy(dst, src, static_cast<std::size_t>(region.width) * channels);
}
return out;
}
} // namespace light_ocr::node