1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4 
5    This file is part of the SANE package.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20 
21 #define DEBUG_DECLARE_ONLY
22 
23 #include "image.h"
24 
25 #include <array>
26 
27 namespace genesys {
28 
29 struct PixelFormatDesc
30 {
31     PixelFormat format;
32     unsigned depth;
33     unsigned channels;
34     ColorOrder order;
35 };
36 
37 const PixelFormatDesc s_known_pixel_formats[] = {
38     { PixelFormat::I1, 1, 1, ColorOrder::RGB },
39     { PixelFormat::I8, 8, 1, ColorOrder::RGB },
40     { PixelFormat::I16, 16, 1, ColorOrder::RGB },
41     { PixelFormat::RGB111, 1, 3, ColorOrder::RGB },
42     { PixelFormat::RGB888, 8, 3, ColorOrder::RGB },
43     { PixelFormat::RGB161616, 16, 3, ColorOrder::RGB },
44     { PixelFormat::BGR888, 8, 3, ColorOrder::BGR },
45     { PixelFormat::BGR161616, 16, 3, ColorOrder::BGR },
46 };
47 
48 
get_pixel_format_color_order(PixelFormat format)49 ColorOrder get_pixel_format_color_order(PixelFormat format)
50 {
51     for (const auto& desc : s_known_pixel_formats) {
52         if (desc.format == format)
53             return desc.order;
54     }
55     throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
56 }
57 
58 
get_pixel_format_depth(PixelFormat format)59 unsigned get_pixel_format_depth(PixelFormat format)
60 {
61     for (const auto& desc : s_known_pixel_formats) {
62         if (desc.format == format)
63             return desc.depth;
64     }
65     throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
66 }
67 
get_pixel_channels(PixelFormat format)68 unsigned get_pixel_channels(PixelFormat format)
69 {
70     for (const auto& desc : s_known_pixel_formats) {
71         if (desc.format == format)
72             return desc.channels;
73     }
74     throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
75 }
76 
get_pixel_row_bytes(PixelFormat format, std::size_t width)77 std::size_t get_pixel_row_bytes(PixelFormat format, std::size_t width)
78 {
79     std::size_t depth = get_pixel_format_depth(format) * get_pixel_channels(format);
80     std::size_t total_bits = depth * width;
81     return total_bits / 8 + ((total_bits % 8 > 0) ? 1 : 0);
82 }
83 
get_pixels_from_row_bytes(PixelFormat format, std::size_t row_bytes)84 std::size_t get_pixels_from_row_bytes(PixelFormat format, std::size_t row_bytes)
85 {
86     std::size_t depth = get_pixel_format_depth(format) * get_pixel_channels(format);
87     return (row_bytes * 8) / depth;
88 }
89 
create_pixel_format(unsigned depth, unsigned channels, ColorOrder order)90 PixelFormat create_pixel_format(unsigned depth, unsigned channels, ColorOrder order)
91 {
92     for (const auto& desc : s_known_pixel_formats) {
93         if (desc.depth == depth && desc.channels == channels && desc.order == order) {
94             return desc.format;
95         }
96     }
97    throw SaneException("Unknown pixel format %d %d %d", depth, channels,
98                        static_cast<unsigned>(order));
99 }
100 
read_bit(const std::uint8_t* data, std::size_t x)101 static inline unsigned read_bit(const std::uint8_t* data, std::size_t x)
102 {
103     return (data[x / 8] >> (7 - (x % 8))) & 0x1;
104 }
105 
write_bit(std::uint8_t* data, std::size_t x, unsigned value)106 static inline void write_bit(std::uint8_t* data, std::size_t x, unsigned value)
107 {
108     value = (value & 0x1) << (7 - (x % 8));
109     std::uint8_t mask = 0x1 << (7 - (x % 8));
110 
111     data[x / 8] = (data[x / 8] & ~mask) | (value & mask);
112 }
113 
get_pixel_from_row(const std::uint8_t* data, std::size_t x, PixelFormat format)114 Pixel get_pixel_from_row(const std::uint8_t* data, std::size_t x, PixelFormat format)
115 {
116     switch (format) {
117         case PixelFormat::I1: {
118             std::uint16_t val = read_bit(data, x) ? 0xffff : 0x0000;
119             return Pixel(val, val, val);
120         }
121         case PixelFormat::RGB111: {
122             x *= 3;
123             std::uint16_t r = read_bit(data, x) ? 0xffff : 0x0000;
124             std::uint16_t g = read_bit(data, x + 1) ? 0xffff : 0x0000;
125             std::uint16_t b = read_bit(data, x + 2) ? 0xffff : 0x0000;
126             return Pixel(r, g, b);
127         }
128         case PixelFormat::I8: {
129             std::uint16_t val = std::uint16_t(data[x]) | (data[x] << 8);
130             return Pixel(val, val, val);
131         }
132         case PixelFormat::I16: {
133             x *= 2;
134             std::uint16_t val = std::uint16_t(data[x]) | (data[x + 1] << 8);
135             return Pixel(val, val, val);
136         }
137         case PixelFormat::RGB888: {
138             x *= 3;
139             std::uint16_t r = std::uint16_t(data[x]) | (data[x] << 8);
140             std::uint16_t g = std::uint16_t(data[x + 1]) | (data[x + 1] << 8);
141             std::uint16_t b = std::uint16_t(data[x + 2]) | (data[x + 2] << 8);
142             return Pixel(r, g, b);
143         }
144         case PixelFormat::BGR888: {
145             x *= 3;
146             std::uint16_t b = std::uint16_t(data[x]) | (data[x] << 8);
147             std::uint16_t g = std::uint16_t(data[x + 1]) | (data[x + 1] << 8);
148             std::uint16_t r = std::uint16_t(data[x + 2]) | (data[x + 2] << 8);
149             return Pixel(r, g, b);
150         }
151         case PixelFormat::RGB161616: {
152             x *= 6;
153             std::uint16_t r = std::uint16_t(data[x]) | (data[x + 1] << 8);
154             std::uint16_t g = std::uint16_t(data[x + 2]) | (data[x + 3] << 8);
155             std::uint16_t b = std::uint16_t(data[x + 4]) | (data[x + 5] << 8);
156             return Pixel(r, g, b);
157         }
158         case PixelFormat::BGR161616: {
159             x *= 6;
160             std::uint16_t b = std::uint16_t(data[x]) | (data[x + 1] << 8);
161             std::uint16_t g = std::uint16_t(data[x + 2]) | (data[x + 3] << 8);
162             std::uint16_t r = std::uint16_t(data[x + 4]) | (data[x + 5] << 8);
163             return Pixel(r, g, b);
164         }
165         default:
166             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
167     }
168 }
169 
set_pixel_to_row(std::uint8_t* data, std::size_t x, Pixel pixel, PixelFormat format)170 void set_pixel_to_row(std::uint8_t* data, std::size_t x, Pixel pixel, PixelFormat format)
171 {
172     switch (format) {
173         case PixelFormat::I1:
174             write_bit(data, x, pixel.r & 0x8000 ? 1 : 0);
175             return;
176         case PixelFormat::RGB111: {
177             x *= 3;
178             write_bit(data, x, pixel.r & 0x8000 ? 1 : 0);
179             write_bit(data, x + 1,pixel.g & 0x8000 ? 1 : 0);
180             write_bit(data, x + 2, pixel.b & 0x8000 ? 1 : 0);
181             return;
182         }
183         case PixelFormat::I8: {
184             float val = (pixel.r >> 8) * 0.3f;
185             val += (pixel.g >> 8) * 0.59f;
186             val += (pixel.b >> 8) * 0.11f;
187             data[x] = static_cast<std::uint16_t>(val);
188             return;
189         }
190         case PixelFormat::I16: {
191             x *= 2;
192             float val = pixel.r * 0.3f;
193             val += pixel.g * 0.59f;
194             val += pixel.b * 0.11f;
195             auto val16 = static_cast<std::uint16_t>(val);
196             data[x] = val16 & 0xff;
197             data[x + 1] = (val16 >> 8) & 0xff;
198             return;
199         }
200         case PixelFormat::RGB888: {
201             x *= 3;
202             data[x] = pixel.r >> 8;
203             data[x + 1] = pixel.g >> 8;
204             data[x + 2] = pixel.b >> 8;
205             return;
206         }
207         case PixelFormat::BGR888: {
208             x *= 3;
209             data[x] = pixel.b >> 8;
210             data[x + 1] = pixel.g >> 8;
211             data[x + 2] = pixel.r >> 8;
212             return;
213         }
214         case PixelFormat::RGB161616: {
215             x *= 6;
216             data[x] = pixel.r & 0xff;
217             data[x + 1] = (pixel.r >> 8) & 0xff;
218             data[x + 2] = pixel.g & 0xff;
219             data[x + 3] = (pixel.g >> 8) & 0xff;
220             data[x + 4] = pixel.b & 0xff;
221             data[x + 5] = (pixel.b >> 8) & 0xff;
222             return;
223         }
224         case PixelFormat::BGR161616:
225             x *= 6;
226             data[x] = pixel.b & 0xff;
227             data[x + 1] = (pixel.b >> 8) & 0xff;
228             data[x + 2] = pixel.g & 0xff;
229             data[x + 3] = (pixel.g >> 8) & 0xff;
230             data[x + 4] = pixel.r & 0xff;
231             data[x + 5] = (pixel.r >> 8) & 0xff;
232             return;
233         default:
234             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
235     }
236 }
237 
get_raw_pixel_from_row(const std::uint8_t* data, std::size_t x, PixelFormat format)238 RawPixel get_raw_pixel_from_row(const std::uint8_t* data, std::size_t x, PixelFormat format)
239 {
240     switch (format) {
241         case PixelFormat::I1:
242             return RawPixel(read_bit(data, x));
243         case PixelFormat::RGB111: {
244             x *= 3;
245             return RawPixel(read_bit(data, x) << 2 |
246                             (read_bit(data, x + 1) << 1) |
247                             (read_bit(data, x + 2)));
248         }
249         case PixelFormat::I8:
250             return RawPixel(data[x]);
251         case PixelFormat::I16: {
252             x *= 2;
253             return RawPixel(data[x], data[x + 1]);
254         }
255         case PixelFormat::RGB888:
256         case PixelFormat::BGR888: {
257             x *= 3;
258             return RawPixel(data[x], data[x + 1], data[x + 2]);
259         }
260         case PixelFormat::RGB161616:
261         case PixelFormat::BGR161616: {
262             x *= 6;
263             return RawPixel(data[x], data[x + 1], data[x + 2],
264                             data[x + 3], data[x + 4], data[x + 5]);
265         }
266         default:
267             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
268     }
269 }
270 
set_raw_pixel_to_row(std::uint8_t* data, std::size_t x, RawPixel pixel, PixelFormat format)271 void set_raw_pixel_to_row(std::uint8_t* data, std::size_t x, RawPixel pixel, PixelFormat format)
272 {
273     switch (format) {
274         case PixelFormat::I1:
275             write_bit(data, x, pixel.data[0] & 0x1);
276             return;
277         case PixelFormat::RGB111: {
278             x *= 3;
279             write_bit(data, x, (pixel.data[0] >> 2) & 0x1);
280             write_bit(data, x + 1, (pixel.data[0] >> 1) & 0x1);
281             write_bit(data, x + 2, (pixel.data[0]) & 0x1);
282             return;
283         }
284         case PixelFormat::I8:
285             data[x] = pixel.data[0];
286             return;
287         case PixelFormat::I16: {
288             x *= 2;
289             data[x] = pixel.data[0];
290             data[x + 1] = pixel.data[1];
291             return;
292         }
293         case PixelFormat::RGB888:
294         case PixelFormat::BGR888: {
295             x *= 3;
296             data[x] = pixel.data[0];
297             data[x + 1] = pixel.data[1];
298             data[x + 2] = pixel.data[2];
299             return;
300         }
301         case PixelFormat::RGB161616:
302         case PixelFormat::BGR161616: {
303             x *= 6;
304             data[x] = pixel.data[0];
305             data[x + 1] = pixel.data[1];
306             data[x + 2] = pixel.data[2];
307             data[x + 3] = pixel.data[3];
308             data[x + 4] = pixel.data[4];
309             data[x + 5] = pixel.data[5];
310             return;
311         }
312         default:
313             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
314     }
315 }
316 
get_raw_channel_from_row(const std::uint8_t* data, std::size_t x, unsigned channel, PixelFormat format)317 std::uint16_t get_raw_channel_from_row(const std::uint8_t* data, std::size_t x, unsigned channel,
318                                        PixelFormat format)
319 {
320     switch (format) {
321         case PixelFormat::I1:
322             return read_bit(data, x);
323         case PixelFormat::RGB111:
324             return read_bit(data, x * 3 + channel);
325         case PixelFormat::I8:
326             return data[x];
327         case PixelFormat::I16: {
328             x *= 2;
329             return data[x] | (data[x + 1] << 8);
330         }
331         case PixelFormat::RGB888:
332         case PixelFormat::BGR888:
333             return data[x * 3 + channel];
334         case PixelFormat::RGB161616:
335         case PixelFormat::BGR161616:
336             return data[x * 6 + channel * 2] | (data[x * 6 + channel * 2 + 1]) << 8;
337         default:
338             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
339     }
340 }
341 
set_raw_channel_to_row(std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel, PixelFormat format)342 void set_raw_channel_to_row(std::uint8_t* data, std::size_t x, unsigned channel,
343                             std::uint16_t pixel, PixelFormat format)
344 {
345     switch (format) {
346         case PixelFormat::I1:
347             write_bit(data, x, pixel & 0x1);
348             return;
349         case PixelFormat::RGB111: {
350             write_bit(data, x * 3 + channel, pixel & 0x1);
351             return;
352         }
353         case PixelFormat::I8:
354             data[x] = pixel;
355             return;
356         case PixelFormat::I16: {
357             x *= 2;
358             data[x] = pixel;
359             data[x + 1] = pixel >> 8;
360             return;
361         }
362         case PixelFormat::RGB888:
363         case PixelFormat::BGR888: {
364             x *= 3;
365             data[x + channel] = pixel;
366             return;
367         }
368         case PixelFormat::RGB161616:
369         case PixelFormat::BGR161616: {
370             x *= 6;
371             data[x + channel * 2] = pixel;
372             data[x + channel * 2 + 1] = pixel >> 8;
373             return;
374         }
375         default:
376             throw SaneException("Unknown pixel format %d", static_cast<unsigned>(format));
377     }
378 }
379 
380 template<PixelFormat Format>
get_pixel_from_row(const std::uint8_t* data, std::size_t x)381 Pixel get_pixel_from_row(const std::uint8_t* data, std::size_t x)
382 {
383     return get_pixel_from_row(data, x, Format);
384 }
385 
386 template<PixelFormat Format>
set_pixel_to_row(std::uint8_t* data, std::size_t x, Pixel pixel)387 void set_pixel_to_row(std::uint8_t* data, std::size_t x, Pixel pixel)
388 {
389     set_pixel_to_row(data, x, pixel, Format);
390 }
391 
392 template<PixelFormat Format>
get_raw_pixel_from_row(const std::uint8_t* data, std::size_t x)393 RawPixel get_raw_pixel_from_row(const std::uint8_t* data, std::size_t x)
394 {
395     return get_raw_pixel_from_row(data, x, Format);
396 }
397 
398 template<PixelFormat Format>
set_raw_pixel_to_row(std::uint8_t* data, std::size_t x, RawPixel pixel)399 void set_raw_pixel_to_row(std::uint8_t* data, std::size_t x, RawPixel pixel)
400 {
401     set_raw_pixel_to_row(data, x, pixel, Format);
402 }
403 
404 template<PixelFormat Format>
get_raw_channel_from_row(const std::uint8_t* data, std::size_t x, unsigned channel)405 std::uint16_t get_raw_channel_from_row(const std::uint8_t* data, std::size_t x, unsigned channel)
406 {
407     return get_raw_channel_from_row(data, x, channel, Format);
408 }
409 
410 template<PixelFormat Format>
set_raw_channel_to_row(std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel)411 void set_raw_channel_to_row(std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel)
412 {
413     set_raw_channel_to_row(data, x, channel, pixel, Format);
414 }
415 
416 template Pixel get_pixel_from_row<PixelFormat::I1>(const std::uint8_t* data, std::size_t x);
417 template Pixel get_pixel_from_row<PixelFormat::RGB111>(const std::uint8_t* data, std::size_t x);
418 template Pixel get_pixel_from_row<PixelFormat::I8>(const std::uint8_t* data, std::size_t x);
419 template Pixel get_pixel_from_row<PixelFormat::RGB888>(const std::uint8_t* data, std::size_t x);
420 template Pixel get_pixel_from_row<PixelFormat::BGR888>(const std::uint8_t* data, std::size_t x);
421 template Pixel get_pixel_from_row<PixelFormat::I16>(const std::uint8_t* data, std::size_t x);
422 template Pixel get_pixel_from_row<PixelFormat::RGB161616>(const std::uint8_t* data, std::size_t x);
423 template Pixel get_pixel_from_row<PixelFormat::BGR161616>(const std::uint8_t* data, std::size_t x);
424 
425 template RawPixel get_raw_pixel_from_row<PixelFormat::I1>(const std::uint8_t* data, std::size_t x);
426 template RawPixel get_raw_pixel_from_row<PixelFormat::RGB111>(const std::uint8_t* data, std::size_t x);
427 template RawPixel get_raw_pixel_from_row<PixelFormat::I8>(const std::uint8_t* data, std::size_t x);
428 template RawPixel get_raw_pixel_from_row<PixelFormat::RGB888>(const std::uint8_t* data, std::size_t x);
429 template RawPixel get_raw_pixel_from_row<PixelFormat::BGR888>(const std::uint8_t* data, std::size_t x);
430 template RawPixel get_raw_pixel_from_row<PixelFormat::I16>(const std::uint8_t* data, std::size_t x);
431 template RawPixel get_raw_pixel_from_row<PixelFormat::RGB161616>(const std::uint8_t* data, std::size_t x);
432 template RawPixel get_raw_pixel_from_row<PixelFormat::BGR161616>(const std::uint8_t* data, std::size_t x);
433 
434 template std::uint16_t get_raw_channel_from_row<PixelFormat::I1>(
435         const std::uint8_t* data, std::size_t x, unsigned channel);
436 template std::uint16_t get_raw_channel_from_row<PixelFormat::RGB111>(
437         const std::uint8_t* data, std::size_t x, unsigned channel);
438 template std::uint16_t get_raw_channel_from_row<PixelFormat::I8>(
439         const std::uint8_t* data, std::size_t x, unsigned channel);
440 template std::uint16_t get_raw_channel_from_row<PixelFormat::RGB888>(
441         const std::uint8_t* data, std::size_t x, unsigned channel);
442 template std::uint16_t get_raw_channel_from_row<PixelFormat::BGR888>(
443         const std::uint8_t* data, std::size_t x, unsigned channel);
444 template std::uint16_t get_raw_channel_from_row<PixelFormat::I16>(
445         const std::uint8_t* data, std::size_t x, unsigned channel);
446 template std::uint16_t get_raw_channel_from_row<PixelFormat::RGB161616>(
447         const std::uint8_t* data, std::size_t x, unsigned channel);
448 template std::uint16_t get_raw_channel_from_row<PixelFormat::BGR161616>
449         (const std::uint8_t* data, std::size_t x, unsigned channel);
450 
451 template void set_pixel_to_row<PixelFormat::I1>(std::uint8_t* data, std::size_t x, Pixel pixel);
452 template void set_pixel_to_row<PixelFormat::RGB111>(std::uint8_t* data, std::size_t x, Pixel pixel);
453 template void set_pixel_to_row<PixelFormat::I8>(std::uint8_t* data, std::size_t x, Pixel pixel);
454 template void set_pixel_to_row<PixelFormat::RGB888>(std::uint8_t* data, std::size_t x, Pixel pixel);
455 template void set_pixel_to_row<PixelFormat::BGR888>(std::uint8_t* data, std::size_t x, Pixel pixel);
456 template void set_pixel_to_row<PixelFormat::I16>(std::uint8_t* data, std::size_t x, Pixel pixel);
457 template void set_pixel_to_row<PixelFormat::RGB161616>(std::uint8_t* data, std::size_t x, Pixel pixel);
458 template void set_pixel_to_row<PixelFormat::BGR161616>(std::uint8_t* data, std::size_t x, Pixel pixel);
459 
460 template void set_raw_pixel_to_row<PixelFormat::I1>(std::uint8_t* data, std::size_t x, RawPixel pixel);
461 template void set_raw_pixel_to_row<PixelFormat::RGB111>(std::uint8_t* data, std::size_t x, RawPixel pixel);
462 template void set_raw_pixel_to_row<PixelFormat::I8>(std::uint8_t* data, std::size_t x, RawPixel pixel);
463 template void set_raw_pixel_to_row<PixelFormat::RGB888>(std::uint8_t* data, std::size_t x, RawPixel pixel);
464 template void set_raw_pixel_to_row<PixelFormat::BGR888>(std::uint8_t* data, std::size_t x, RawPixel pixel);
465 template void set_raw_pixel_to_row<PixelFormat::I16>(std::uint8_t* data, std::size_t x, RawPixel pixel);
466 template void set_raw_pixel_to_row<PixelFormat::RGB161616>(std::uint8_t* data, std::size_t x, RawPixel pixel);
467 template void set_raw_pixel_to_row<PixelFormat::BGR161616>(std::uint8_t* data, std::size_t x, RawPixel pixel);
468 
469 template void set_raw_channel_to_row<PixelFormat::I1>(
470         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
471 template void set_raw_channel_to_row<PixelFormat::RGB111>(
472         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
473 template void set_raw_channel_to_row<PixelFormat::I8>(
474         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
475 template void set_raw_channel_to_row<PixelFormat::RGB888>(
476         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
477 template void set_raw_channel_to_row<PixelFormat::BGR888>(
478         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
479 template void set_raw_channel_to_row<PixelFormat::I16>(
480         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
481 template void set_raw_channel_to_row<PixelFormat::RGB161616>(
482         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
483 template void set_raw_channel_to_row<PixelFormat::BGR161616>(
484         std::uint8_t* data, std::size_t x, unsigned channel, std::uint16_t pixel);
485 
486 } // namespace genesys
487