1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5 
6 #ifndef LIB_JXL_BLENDING_H_
7 #define LIB_JXL_BLENDING_H_
8 #include "lib/jxl/dec_cache.h"
9 #include "lib/jxl/dec_patch_dictionary.h"
10 #include "lib/jxl/image_bundle.h"
11 
12 namespace jxl {
13 
14 void PerformBlending(const float* const* bg, const float* const* fg,
15                      float* const* out, size_t x0, size_t xsize,
16                      const PatchBlending& color_blending,
17                      const PatchBlending* ec_blending,
18                      const std::vector<ExtraChannelInfo>& extra_channel_info);
19 
20 class ImageBlender {
21  public:
22   class RectBlender {
23    public:
24     // Does the blending for a given row of the rect passed to
25     // ImageBlender::PrepareRect.
26     Status DoBlending(size_t y);
27 
28     // If this returns true, then nothing needs to be done for this rect and
29     // DoBlending can be skipped (but does not have to).
done()30     bool done() const { return done_; }
31 
32    private:
33     friend class ImageBlender;
RectBlender(bool done)34     explicit RectBlender(bool done) : done_(done) {}
35 
36     bool done_;
37     Rect current_overlap_;
38     Rect current_cropbox_;
39     const std::vector<ExtraChannelInfo>* extra_channel_info_;
40     std::vector<const float*> fg_ptrs_;
41     std::vector<size_t> fg_strides_;
42     std::vector<const float*> bg_ptrs_;
43     std::vector<size_t> bg_strides_;
44     std::vector<float*> out_ptrs_;
45     std::vector<size_t> out_strides_;
46     std::vector<const float*> fg_row_ptrs_;
47     std::vector<const float*> bg_row_ptrs_;
48     std::vector<float*> out_row_ptrs_;
49     std::vector<PatchBlending> blending_info_;
50   };
51 
52   static bool NeedsBlending(PassesDecoderState* dec_state);
53 
54   Status PrepareBlending(
55       PassesDecoderState* dec_state, FrameOrigin foreground_origin,
56       size_t foreground_xsize, size_t foreground_ysize,
57       const std::vector<ExtraChannelInfo>* extra_channel_info,
58       const ColorEncoding& frame_color_encoding, const Rect& frame_rect,
59       Image3F* output, const Rect& output_rect,
60       std::vector<ImageF>* output_extra_channels,
61       std::vector<Rect> output_extra_channels_rects);
62   // rect is relative to the full decoded foreground.
63   // But foreground here can be a subset of the full foreground, and input_rect
64   // indicates where that rect is in that subset. For example, if rect =
65   // Rect(10, 10, 20, 20), and foreground is subrect (7, 7, 30, 30) of the full
66   // foreground, then input_rect should be (3, 3, 20, 20), because that is where
67   // rect is relative to the foreground crop.
68   ImageBlender::RectBlender PrepareRect(
69       const Rect& rect, const Image3F& foreground,
70       const std::vector<ImageF>& extra_channels, const Rect& input_rect) const;
71 
72   // If this returns true, then it is not necessary to call further methods on
73   // this ImageBlender to achieve blending, although it is not forbidden either
74   // (those methods will just return immediately in that case).
done()75   bool done() const { return done_; }
76 
77  private:
78   BlendingInfo info_;
79   const std::vector<ExtraChannelInfo>* extra_channel_info_;
80   Rect frame_rect_;
81   // Destination, as well as background before DoBlending is called.
82   Image3F* output_;
83   ImageBundle* bg_;
84   Rect output_rect_;
85   std::vector<ImageF>* output_extra_channels_;
86   std::vector<Rect> output_extra_channels_rects_;
87   Rect cropbox_;
88   Rect overlap_;
89   bool done_ = false;
90   const std::vector<BlendingInfo>* ec_info_;
91   FrameOrigin o_{};
92 };
93 
94 }  // namespace jxl
95 
96 #endif  // LIB_JXL_BLENDING_H_
97