Coverage Report

Created: 2025-08-11 08:01

/src/libheif/libheif/image-items/overlay.h
Line
Count
Source (jump to first uncovered line)
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
29
30
class ImageOverlay
31
{
32
public:
33
  Error parse(size_t num_images, const std::vector<uint8_t>& data);
34
35
  std::vector<uint8_t> write() const;
36
37
  std::string dump() const;
38
39
  void get_background_color(uint16_t col[4]) const;
40
41
86
  uint32_t get_canvas_width() const { return m_width; }
42
43
86
  uint32_t get_canvas_height() const { return m_height; }
44
45
110
  size_t get_num_offsets() const { return m_offsets.size(); }
46
47
  void get_offset(size_t image_index, int32_t* x, int32_t* y) const;
48
49
  void set_background_color(const uint16_t rgba_color[4])
50
0
  {
51
0
    for (int i = 0; i < 4; i++) {
52
0
      m_background_color[i] = rgba_color[i];
53
0
    }
54
0
  }
55
56
  void set_canvas_size(uint32_t width, uint32_t height)
57
0
  {
58
0
    m_width = width;
59
0
    m_height = height;
60
0
  }
61
62
  void add_image_on_top(heif_item_id image_id, int32_t offset_x, int32_t offset_y)
63
0
  {
64
0
    m_offsets.emplace_back(ImageWithOffset{image_id, offset_x, offset_y});
65
0
  }
66
67
  struct ImageWithOffset
68
  {
69
    heif_item_id image_id;
70
    int32_t x, y;
71
  };
72
73
0
  const std::vector<ImageWithOffset>& get_overlay_stack() const { return m_offsets; }
74
75
private:
76
  uint8_t m_version = 0;
77
  uint8_t m_flags = 0;
78
  uint16_t m_background_color[4]{0, 0, 0, 0};
79
  uint32_t m_width = 0;
80
  uint32_t m_height = 0;
81
82
  std::vector<ImageWithOffset> m_offsets;
83
};
84
85
86
class ImageItem_Overlay : public ImageItem
87
{
88
public:
89
  ImageItem_Overlay(HeifContext* ctx, heif_item_id id);
90
91
  ImageItem_Overlay(HeifContext* ctx);
92
93
0
  uint32_t get_infe_type() const override { return fourcc("iovl"); }
94
95
  // TODO: nclx depends on contained format
96
  // const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }
97
98
  // heif_compression_format get_compression_format() const override { return heif_compression_HEVC; }
99
100
  Error initialize_decoder() override;
101
102
  int get_luma_bits_per_pixel() const override;
103
104
  int get_chroma_bits_per_pixel() const override;
105
106
  Error get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const override;
107
108
  heif_brand2 get_compatible_brand() const override;
109
110
  Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
111
                                         struct heif_encoder* encoder,
112
                                         const struct heif_encoding_options& options,
113
                                         enum heif_image_input_class input_class) override
114
0
  {
115
0
    return Error{heif_error_Unsupported_feature,
116
0
                 heif_suberror_Unspecified, "Cannot encode image to 'iovl'"};
117
0
  }
118
119
  Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const struct heif_decoding_options& options,
120
                                                                  bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0) const override;
121
122
123
  // --- iovl specific
124
125
  static Result<std::shared_ptr<ImageItem_Overlay>> add_new_overlay_item(HeifContext* ctx, const ImageOverlay& overlayspec);
126
127
private:
128
  ImageOverlay m_overlay_spec;
129
  std::vector<heif_item_id> m_overlay_image_ids;
130
131
  Error read_overlay_spec();
132
133
  Result<std::shared_ptr<HeifPixelImage>> decode_overlay_image(const heif_decoding_options& options) const;
134
};
135
136
137
#endif //LIBHEIF_OVERLAY_H