Coverage Report

Created: 2026-02-14 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/image-items/overlay.h
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef LIBHEIF_OVERLAY_H
22
#define LIBHEIF_OVERLAY_H
23
24
#include "image_item.h"
25
#include <vector>
26
#include <string>
27
#include <memory>
28
#include <set>
29
30
31
class ImageOverlay
32
{
33
public:
34
  Error parse(size_t num_images, const std::vector<uint8_t>& data);
35
36
  std::vector<uint8_t> write() const;
37
38
  std::string dump() const;
39
40
  void get_background_color(uint16_t col[4]) const;
41
42
269
  uint32_t get_canvas_width() const { return m_width; }
43
44
269
  uint32_t get_canvas_height() const { return m_height; }
45
46
286
  size_t get_num_offsets() const { return m_offsets.size(); }
47
48
  void get_offset(size_t image_index, int32_t* x, int32_t* y) const;
49
50
  void set_background_color(const uint16_t rgba_color[4])
51
0
  {
52
0
    for (int i = 0; i < 4; i++) {
53
0
      m_background_color[i] = rgba_color[i];
54
0
    }
55
0
  }
56
57
  void set_canvas_size(uint32_t width, uint32_t height)
58
0
  {
59
0
    m_width = width;
60
0
    m_height = height;
61
0
  }
62
63
  void add_image_on_top(heif_item_id image_id, int32_t offset_x, int32_t offset_y)
64
0
  {
65
0
    m_offsets.emplace_back(ImageWithOffset{image_id, offset_x, offset_y});
66
0
  }
67
68
  struct ImageWithOffset
69
  {
70
    heif_item_id image_id;
71
    int32_t x, y;
72
  };
73
74
0
  const std::vector<ImageWithOffset>& get_overlay_stack() const { return m_offsets; }
75
76
private:
77
  uint8_t m_version = 0;
78
  uint8_t m_flags = 0;
79
  uint16_t m_background_color[4]{0, 0, 0, 0};
80
  uint32_t m_width = 0;
81
  uint32_t m_height = 0;
82
83
  std::vector<ImageWithOffset> m_offsets;
84
};
85
86
87
class ImageItem_Overlay : public ImageItem
88
{
89
public:
90
  ImageItem_Overlay(HeifContext* ctx, heif_item_id id);
91
92
  ImageItem_Overlay(HeifContext* ctx);
93
94
34
  uint32_t get_infe_type() const override { return fourcc("iovl"); }
95
96
  // TODO: nclx depends on contained format
97
  // const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }
98
99
  // heif_compression_format get_compression_format() const override { return heif_compression_HEVC; }
100
101
  Error initialize_decoder() override;
102
103
  int get_luma_bits_per_pixel() const override;
104
105
  int get_chroma_bits_per_pixel() const override;
106
107
  Error get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const override;
108
109
  heif_brand2 get_compatible_brand() const override;
110
111
  Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
112
                                         heif_encoder* encoder,
113
                                         const heif_encoding_options& options,
114
                                         heif_image_input_class input_class) override
115
0
  {
116
0
    return Error{heif_error_Unsupported_feature,
117
0
                 heif_suberror_Unspecified, "Cannot encode image to 'iovl'"};
118
0
  }
119
120
  Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const heif_decoding_options& options,
121
                                                                  bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0,
122
                                                                  std::set<heif_item_id> processed_ids) const override;
123
124
125
  // --- iovl specific
126
127
  static Result<std::shared_ptr<ImageItem_Overlay>> add_new_overlay_item(HeifContext* ctx, const ImageOverlay& overlayspec);
128
129
private:
130
  ImageOverlay m_overlay_spec;
131
  std::vector<heif_item_id> m_overlay_image_ids;
132
133
  Error read_overlay_spec();
134
135
  Result<std::shared_ptr<HeifPixelImage>> decode_overlay_image(const heif_decoding_options& options,
136
                                                               std::set<heif_item_id> processed_ids) const;
137
};
138
139
140
#endif //LIBHEIF_OVERLAY_H