summaryrefslogtreecommitdiff
path: root/backend/genesys/image_pipeline.h
blob: 39aed71ee8f77647fd7b85ee8ba12d49d9fda190 (plain)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* sane - Scanner Access Now Easy.

   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef BACKEND_GENESYS_IMAGE_PIPELINE_H
#define BACKEND_GENESYS_IMAGE_PIPELINE_H

#include "image.h"
#include "image_pixel.h"
#include "image_buffer.h"

#include <algorithm>
#include <functional>
#include <memory>

namespace genesys {

class ImagePipelineNode
{
public:
    virtual ~ImagePipelineNode();

    virtual std::size_t get_width() const = 0;
    virtual std::size_t get_height() const = 0;
    virtual PixelFormat get_format() const = 0;

    std::size_t get_row_bytes() const
    {
        return get_pixel_row_bytes(get_format(), get_width());
    }

    virtual bool eof() const = 0;

    // returns true if the row was filled successfully, false otherwise (e.g. if not enough data
    // was available.
    virtual bool get_next_row_data(std::uint8_t* out_data) = 0;
};

// A pipeline node that produces data from a callable
class ImagePipelineNodeCallableSource : public ImagePipelineNode
{
public:
    using ProducerCallback = std::function<bool(std::size_t size, std::uint8_t* out_data)>;

    ImagePipelineNodeCallableSource(std::size_t width, std::size_t height, PixelFormat format,
                                    ProducerCallback producer) :
        producer_{producer},
        width_{width},
        height_{height},
        format_{format}
    {}

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return format_; }

    bool eof() const override { return eof_; }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ProducerCallback producer_;
    std::size_t width_ = 0;
    std::size_t height_ = 0;
    PixelFormat format_ = PixelFormat::UNKNOWN;
    bool eof_ = false;
};

// A pipeline node that produces data from a callable requesting fixed-size chunks.
class ImagePipelineNodeBufferedCallableSource : public ImagePipelineNode
{
public:
    using ProducerCallback = std::function<bool(std::size_t size, std::uint8_t* out_data)>;

    ImagePipelineNodeBufferedCallableSource(std::size_t width, std::size_t height,
                                            PixelFormat format, std::size_t input_batch_size,
                                            ProducerCallback producer);

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return format_; }

    bool eof() const override { return eof_; }

    bool get_next_row_data(std::uint8_t* out_data) override;

    std::size_t remaining_bytes() const { return buffer_.remaining_size(); }
    void set_remaining_bytes(std::size_t bytes) { buffer_.set_remaining_size(bytes); }
    void set_last_read_multiple(std::size_t bytes) { buffer_.set_last_read_multiple(bytes); }

private:
    ProducerCallback producer_;
    std::size_t width_ = 0;
    std::size_t height_ = 0;
    PixelFormat format_ = PixelFormat::UNKNOWN;

    bool eof_ = false;
    std::size_t curr_row_ = 0;

    ImageBuffer buffer_;
};

// A pipeline node that produces data from the given array.
class ImagePipelineNodeArraySource : public ImagePipelineNode
{
public:
    ImagePipelineNodeArraySource(std::size_t width, std::size_t height, PixelFormat format,
                                 std::vector<std::uint8_t> data);

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return format_; }

    bool eof() const override { return eof_; }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    std::size_t width_ = 0;
    std::size_t height_ = 0;
    PixelFormat format_ = PixelFormat::UNKNOWN;

    bool eof_ = false;

    std::vector<std::uint8_t> data_;
    std::size_t next_row_ = 0;
};


/// A pipeline node that produces data from the given image
class ImagePipelineNodeImageSource : public ImagePipelineNode
{
public:
    ImagePipelineNodeImageSource(const Image& source);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return next_row_ >= get_height(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    const Image& source_;
    std::size_t next_row_ = 0;
};

// A pipeline node that converts between pixel formats
class ImagePipelineNodeFormatConvert : public ImagePipelineNode
{
public:
    ImagePipelineNodeFormatConvert(ImagePipelineNode& source, PixelFormat dst_format) :
        source_(source),
        dst_format_{dst_format}
    {}

    ~ImagePipelineNodeFormatConvert() override = default;

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return dst_format_; }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    PixelFormat dst_format_;
    std::vector<std::uint8_t> buffer_;
};

// A pipeline node that handles data that comes out of segmented sensors. Note that the width of
// the output data does not necessarily match the input data width, because in many cases almost
// all width of the image needs to be read in order to desegment it.
class ImagePipelineNodeDesegment : public ImagePipelineNode
{
public:
    ImagePipelineNodeDesegment(ImagePipelineNode& source,
                               std::size_t output_width,
                               const std::vector<unsigned>& segment_order,
                               std::size_t segment_pixels,
                               std::size_t interleaved_lines,
                               std::size_t pixels_per_chunk);

    ImagePipelineNodeDesegment(ImagePipelineNode& source,
                               std::size_t output_width,
                               std::size_t segment_count,
                               std::size_t segment_pixels,
                               std::size_t interleaved_lines,
                               std::size_t pixels_per_chunk);

    ~ImagePipelineNodeDesegment() override = default;

    std::size_t get_width() const override { return output_width_; }
    std::size_t get_height() const override { return source_.get_height() / interleaved_lines_; }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t output_width_;
    std::vector<unsigned> segment_order_;
    std::size_t segment_pixels_ = 0;
    std::size_t interleaved_lines_ = 0;
    std::size_t pixels_per_chunk_ = 0;

    RowBuffer buffer_;
};

// A pipeline node that deinterleaves data on multiple lines
class ImagePipelineNodeDeinterleaveLines : public ImagePipelineNodeDesegment
{
public:
    ImagePipelineNodeDeinterleaveLines(ImagePipelineNode& source,
                                       std::size_t interleaved_lines,
                                       std::size_t pixels_per_chunk);
};

// A pipeline that swaps bytes in 16-bit components and does nothing otherwise.
class ImagePipelineNodeSwap16BitEndian : public ImagePipelineNode
{
public:
    ImagePipelineNodeSwap16BitEndian(ImagePipelineNode& source);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    bool needs_swapping_ = false;
};

class ImagePipelineNodeInvert : public ImagePipelineNode
{
public:
    ImagePipelineNodeInvert(ImagePipelineNode& source);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
};

// A pipeline node that merges 3 mono lines into a color channel
class ImagePipelineNodeMergeMonoLinesToColor : public ImagePipelineNode
{
public:
    ImagePipelineNodeMergeMonoLinesToColor(ImagePipelineNode& source,
                                           ColorOrder color_order);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height() / 3; }
    PixelFormat get_format() const override { return output_format_; }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    static PixelFormat get_output_format(PixelFormat input_format, ColorOrder order);

    ImagePipelineNode& source_;
    PixelFormat output_format_ = PixelFormat::UNKNOWN;

    RowBuffer buffer_;
};

// A pipeline node that splits a color channel into 3 mono lines
class ImagePipelineNodeSplitMonoLines : public ImagePipelineNode
{
public:
    ImagePipelineNodeSplitMonoLines(ImagePipelineNode& source);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height() * 3; }
    PixelFormat get_format() const override { return output_format_; }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    static PixelFormat get_output_format(PixelFormat input_format);

    ImagePipelineNode& source_;
    PixelFormat output_format_ = PixelFormat::UNKNOWN;

    std::vector<std::uint8_t> buffer_;
    unsigned next_channel_ = 0;
};


// A pipeline node that merges 3 mono lines into a gray channel
class ImagePipelineNodeMergeColorToGray : public ImagePipelineNode
{
public:
    ImagePipelineNodeMergeColorToGray(ImagePipelineNode& source);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return output_format_; }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    static PixelFormat get_output_format(PixelFormat input_format);

    ImagePipelineNode& source_;
    PixelFormat output_format_ = PixelFormat::UNKNOWN;
    float ch0_mult_ = 0;
    float ch1_mult_ = 0;
    float ch2_mult_ = 0;

    std::vector<std::uint8_t> temp_buffer_;
};

// A pipeline node that shifts colors across lines by the given offsets
class ImagePipelineNodeComponentShiftLines : public ImagePipelineNode
{
public:
    ImagePipelineNodeComponentShiftLines(ImagePipelineNode& source,
                                         unsigned shift_r, unsigned shift_g, unsigned shift_b);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t extra_height_ = 0;
    std::size_t height_ = 0;

    std::array<unsigned, 3> channel_shifts_;

    RowBuffer buffer_;
};

// A pipeline node that shifts pixels across lines by the given offsets (performs vertical
// unstaggering)
class ImagePipelineNodePixelShiftLines : public ImagePipelineNode
{
public:
    ImagePipelineNodePixelShiftLines(ImagePipelineNode& source,
                                     const std::vector<std::size_t>& shifts);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t extra_height_ = 0;
    std::size_t height_ = 0;

    std::vector<std::size_t> pixel_shifts_;

    RowBuffer buffer_;
};

// A pipeline node that shifts pixels across columns by the given offsets. Each row is divided
// into pixel groups of shifts.size() pixels. For each output group starting at position xgroup,
// the i-th pixel will be set to the input pixel at position xgroup + shifts[i].
class ImagePipelineNodePixelShiftColumns : public ImagePipelineNode
{
public:
    ImagePipelineNodePixelShiftColumns(ImagePipelineNode& source,
                                       const std::vector<std::size_t>& shifts);

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t width_ = 0;
    std::size_t extra_width_ = 0;

    std::vector<std::size_t> pixel_shifts_;

    std::vector<std::uint8_t> temp_buffer_;
};

// exposed for tests
std::size_t compute_pixel_shift_extra_width(std::size_t source_width,
                                            const std::vector<std::size_t>& shifts);

// A pipeline node that extracts a sub-image from the image. Padding and cropping is done as needed.
// The class can't pad to the left of the image currently, as only positive offsets are accepted.
class ImagePipelineNodeExtract : public ImagePipelineNode
{
public:
    ImagePipelineNodeExtract(ImagePipelineNode& source,
                             std::size_t offset_x, std::size_t offset_y,
                             std::size_t width, std::size_t height);

    ~ImagePipelineNodeExtract() override;

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return height_; }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t offset_x_ = 0;
    std::size_t offset_y_ = 0;
    std::size_t width_ = 0;
    std::size_t height_ = 0;

    std::size_t current_line_ = 0;
    std::vector<std::uint8_t> cached_line_;
};

// A pipeline node that scales rows to the specified width by using a point filter
class ImagePipelineNodeScaleRows : public ImagePipelineNode
{
public:
    ImagePipelineNodeScaleRows(ImagePipelineNode& source, std::size_t width);

    std::size_t get_width() const override { return width_; }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::size_t width_ = 0;

    std::vector<std::uint8_t> cached_line_;
};

// A pipeline node that mimics the calibration behavior on Genesys chips
class ImagePipelineNodeCalibrate : public ImagePipelineNode
{
public:

    ImagePipelineNodeCalibrate(ImagePipelineNode& source, const std::vector<std::uint16_t>& bottom,
                               const std::vector<std::uint16_t>& top, std::size_t x_start);

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;

    std::vector<float> offset_;
    std::vector<float> multiplier_;
};

class ImagePipelineNodeDebug : public ImagePipelineNode
{
public:
    ImagePipelineNodeDebug(ImagePipelineNode& source, const std::string& path);
    ~ImagePipelineNodeDebug() override;

    std::size_t get_width() const override { return source_.get_width(); }
    std::size_t get_height() const override { return source_.get_height(); }
    PixelFormat get_format() const override { return source_.get_format(); }

    bool eof() const override { return source_.eof(); }

    bool get_next_row_data(std::uint8_t* out_data) override;

private:
    ImagePipelineNode& source_;
    std::string path_;
    RowBuffer buffer_;
};

class ImagePipelineStack
{
public:
    ImagePipelineStack() {}
    ImagePipelineStack(ImagePipelineStack&& other)
    {
        clear();
        nodes_ = std::move(other.nodes_);
    }

    ImagePipelineStack& operator=(ImagePipelineStack&& other)
    {
        clear();
        nodes_ = std::move(other.nodes_);
        return *this;
    }

    ~ImagePipelineStack() { clear(); }

    std::size_t get_input_width() const;
    std::size_t get_input_height() const;
    PixelFormat get_input_format() const;
    std::size_t get_input_row_bytes() const;

    std::size_t get_output_width() const;
    std::size_t get_output_height() const;
    PixelFormat get_output_format() const;
    std::size_t get_output_row_bytes() const;

    ImagePipelineNode& front() { return *(nodes_.front().get()); }

    bool eof() const { return nodes_.back()->eof(); }

    void clear();

    template<class Node, class... Args>
    Node& push_first_node(Args&&... args)
    {
        if (!nodes_.empty()) {
            throw SaneException("Trying to append first node when there are existing nodes");
        }
        nodes_.emplace_back(std::unique_ptr<Node>(new Node(std::forward<Args>(args)...)));
        return static_cast<Node&>(*nodes_.back());
    }

    template<class Node, class... Args>
    Node& push_node(Args&&... args)
    {
        ensure_node_exists();
        nodes_.emplace_back(std::unique_ptr<Node>(new Node(*nodes_.back(),
                                                           std::forward<Args>(args)...)));
        return static_cast<Node&>(*nodes_.back());
    }

    bool get_next_row_data(std::uint8_t* out_data)
    {
        return nodes_.back()->get_next_row_data(out_data);
    }

    std::vector<std::uint8_t> get_all_data();

    Image get_image();

private:
    void ensure_node_exists() const;

    std::vector<std::unique_ptr<ImagePipelineNode>> nodes_;
};

} // namespace genesys

#endif // ifndef BACKEND_GENESYS_IMAGE_PIPELINE_H