Repository navigation
Expand file tree
/
Copy pathImage.cpp
More file actions
333 lines (277 loc) · 7.27 KB
/
Copy pathImage.cpp
File metadata and controls
333 lines (277 loc) · 7.27 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
/**
*
* @file Image.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/rixcpp/pdf
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Rix
*
*/
#include <rix/pdf/document/Image.hpp>
#include <fstream>
#include <iterator>
#include <string>
#include <utility>
namespace rixlib::pdf
{
namespace
{
[[nodiscard]] PdfError invalid_image_error(std::string message)
{
return make_pdf_error(
PdfErrorCode::InvalidImage,
std::move(message));
}
[[nodiscard]] PdfError unsupported_image_error(std::string message)
{
return make_pdf_error(
PdfErrorCode::UnsupportedImageFormat,
std::move(message));
}
[[nodiscard]] PdfError file_open_error(std::string message)
{
return make_pdf_error(
PdfErrorCode::FileOpenFailed,
std::move(message));
}
[[nodiscard]] bool is_standalone_jpeg_marker(std::uint8_t marker) noexcept
{
return marker == 0x01 ||
(marker >= 0xD0 && marker <= 0xD9);
}
[[nodiscard]] bool is_jpeg_start_of_frame(std::uint8_t marker) noexcept
{
return (marker >= 0xC0 && marker <= 0xC3) ||
(marker >= 0xC5 && marker <= 0xC7) ||
(marker >= 0xC9 && marker <= 0xCB) ||
(marker >= 0xCD && marker <= 0xCF);
}
[[nodiscard]] std::uint16_t read_be_u16(
const std::vector<std::uint8_t> &data,
std::size_t offset) noexcept
{
return static_cast<std::uint16_t>(
(static_cast<std::uint16_t>(data[offset]) << 8U) |
static_cast<std::uint16_t>(data[offset + 1U]));
}
} // namespace
Image::Image(
ImageFormat format,
std::vector<std::uint8_t> data,
int width,
int height,
int components)
: format_(format),
data_(std::move(data)),
width_(width),
height_(height),
components_(components)
{
}
PdfResult<Image> Image::load_jpeg(std::string_view path)
{
if (path.empty())
{
return PdfResult<Image>::failure(
invalid_image_error("Image path cannot be empty."));
}
std::ifstream file(std::string(path), std::ios::binary);
if (!file)
{
return PdfResult<Image>::failure(
file_open_error("Cannot open image file."));
}
std::vector<std::uint8_t> bytes{
std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>()};
return from_jpeg_bytes(std::move(bytes));
}
PdfResult<Image> Image::from_jpeg_bytes(std::vector<std::uint8_t> bytes)
{
auto info = parse_jpeg_info(bytes);
if (info.failed())
{
return PdfResult<Image>::failure(info.error());
}
return PdfResult<Image>::success(
Image{
ImageFormat::Jpeg,
std::move(bytes),
info.value().width,
info.value().height,
info.value().components});
}
bool Image::valid() const noexcept
{
return !data_.empty() &&
width_ > 0 &&
height_ > 0 &&
components_ > 0;
}
ImageFormat Image::format() const noexcept
{
return format_;
}
const std::vector<std::uint8_t> &Image::data() const noexcept
{
return data_;
}
int Image::width() const noexcept
{
return width_;
}
int Image::height() const noexcept
{
return height_;
}
int Image::components() const noexcept
{
return components_;
}
ImageColorSpace Image::color_space() const noexcept
{
if (components_ == 1)
{
return ImageColorSpace::DeviceGray;
}
if (components_ == 4)
{
return ImageColorSpace::DeviceCMYK;
}
return ImageColorSpace::DeviceRGB;
}
float Image::aspect_ratio() const noexcept
{
if (width_ <= 0 || height_ <= 0)
{
return 0.0F;
}
return static_cast<float>(width_) / static_cast<float>(height_);
}
bool Image::grayscale() const noexcept
{
return components_ == 1;
}
bool Image::rgb() const noexcept
{
return components_ == 3;
}
bool Image::cmyk() const noexcept
{
return components_ == 4;
}
PdfResult<Image::JpegInfo> Image::parse_jpeg_info(
const std::vector<std::uint8_t> &data)
{
if (data.size() < 4)
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG data is too small."));
}
if (data[0] != 0xFF || data[1] != 0xD8)
{
return PdfResult<JpegInfo>::failure(
unsupported_image_error("Image data is not a JPEG file."));
}
std::size_t offset = 2;
while (offset + 1 < data.size())
{
if (data[offset] != 0xFF)
{
++offset;
continue;
}
while (offset < data.size() && data[offset] == 0xFF)
{
++offset;
}
if (offset >= data.size())
{
break;
}
const std::uint8_t marker = data[offset++];
if (marker == 0xD9 || marker == 0xDA)
{
break;
}
if (is_standalone_jpeg_marker(marker))
{
continue;
}
if (offset + 1 >= data.size())
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG marker length is truncated."));
}
const std::uint16_t segment_length = read_be_u16(data, offset);
if (segment_length < 2)
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG segment length is invalid."));
}
if (offset + segment_length > data.size())
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG segment is truncated."));
}
if (is_jpeg_start_of_frame(marker))
{
if (segment_length < 8)
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG frame header is truncated."));
}
const std::size_t frame = offset + 2;
JpegInfo info;
info.height = static_cast<int>(read_be_u16(data, frame + 1));
info.width = static_cast<int>(read_be_u16(data, frame + 3));
info.components = static_cast<int>(data[frame + 5]);
if (info.width <= 0 || info.height <= 0)
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG dimensions are invalid."));
}
if (info.components != 1 &&
info.components != 3 &&
info.components != 4)
{
return PdfResult<JpegInfo>::failure(
invalid_image_error("JPEG color component count is unsupported."));
}
return PdfResult<JpegInfo>::success(info);
}
offset += segment_length;
}
return PdfResult<JpegInfo>::failure(
invalid_image_error("Could not find JPEG dimensions."));
}
std::string_view to_string(ImageFormat format) noexcept
{
switch (format)
{
case ImageFormat::Jpeg:
return "Jpeg";
}
return "Jpeg";
}
std::string_view pdf_color_space_name(
ImageColorSpace color_space) noexcept
{
switch (color_space)
{
case ImageColorSpace::DeviceGray:
return "DeviceGray";
case ImageColorSpace::DeviceRGB:
return "DeviceRGB";
case ImageColorSpace::DeviceCMYK:
return "DeviceCMYK";
}
return "DeviceRGB";
}
} // namespace rixlib::pdf