Coverage Report

Created: 2025-06-16 07:00

/src/libheif/libheif/image-items/tiled.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_TILED_H
22
#define LIBHEIF_TILED_H
23
24
25
#include "image_item.h"
26
#include "codecs/decoder.h"
27
#include "box.h"
28
#include <vector>
29
#include <string>
30
#include <memory>
31
#include <utility>
32
#include "libheif/heif_experimental.h"
33
34
35
uint64_t number_of_tiles(const heif_tiled_image_parameters& params);
36
37
uint32_t nTiles_h(const heif_tiled_image_parameters& params);
38
39
uint32_t nTiles_v(const heif_tiled_image_parameters& params);
40
41
42
class Box_tilC : public FullBox
43
{
44
  /*
45
   * Flags:
46
   * bit 0-1 - number of bits for offsets   (0: 32, 1: 40, 2: 48, 3: 64)
47
   * bit 2-3 - number of bits for tile size (0:  0, 1: 24; 2: 32, 3: 64)
48
   * bit 4   - sequential ordering hint
49
   * bit 5   - use 64 bit dimensions (currently unused because ispe is limited to 32 bit)
50
   */
51
public:
52
  Box_tilC()
53
0
  {
54
0
    set_short_type(fourcc("tilC"));
55
56
0
    init_heif_tiled_image_parameters(m_parameters);
57
0
  }
58
59
0
  bool is_essential() const override { return true; }
60
61
  void derive_box_version() override;
62
63
0
  void set_parameters(const heif_tiled_image_parameters& params) { m_parameters = params; }
64
65
0
  void set_compression_format(heif_compression_format format) { m_parameters.compression_format_fourcc = ImageItem::compression_format_to_fourcc_infe_type(format); }
66
67
0
  const heif_tiled_image_parameters& get_parameters() const { return m_parameters; }
68
69
  Error write(StreamWriter& writer) const override;
70
71
  std::string dump(Indent&) const override;
72
73
0
  std::vector<std::shared_ptr<Box>>& get_tile_properties() { return m_children; }
74
75
0
  const std::vector<std::shared_ptr<Box>>& get_tile_properties() const { return m_children; }
76
77
protected:
78
  Error parse(BitstreamRange& range, const heif_security_limits* limits) override;
79
80
private:
81
  heif_tiled_image_parameters m_parameters;
82
83
  static void init_heif_tiled_image_parameters(heif_tiled_image_parameters& params);
84
};
85
86
87
#define TILD_OFFSET_NOT_AVAILABLE 0
88
#define TILD_OFFSET_SEE_LOWER_RESOLUTION_LAYER 1
89
0
#define TILD_OFFSET_NOT_LOADED 10
90
91
class TiledHeader
92
{
93
public:
94
  Error set_parameters(const heif_tiled_image_parameters& params);
95
96
0
  const heif_tiled_image_parameters& get_parameters() const { return m_parameters; }
97
98
0
  void set_compression_format(heif_compression_format format) { m_parameters.compression_format_fourcc = ImageItem::compression_format_to_fourcc_infe_type(format); }
99
100
  Error read_full_offset_table(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id, const heif_security_limits* limits);
101
102
  Error read_offset_table_range(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id,
103
                                uint64_t start, uint64_t end);
104
105
  std::vector<uint8_t> write_offset_table();
106
107
  std::string dump() const;
108
109
  void set_tild_tile_range(uint32_t tile_x, uint32_t tile_y, uint64_t offset, uint32_t size);
110
111
  size_t get_header_size() const;
112
113
0
  uint64_t get_tile_offset(uint32_t idx) const { return m_offsets[idx].offset; }
114
115
0
  uint32_t get_tile_size(uint32_t idx) const { return m_offsets[idx].size; }
116
117
0
  bool is_tile_offset_known(uint32_t idx) const { return m_offsets[idx].offset != TILD_OFFSET_NOT_LOADED; }
118
119
  uint32_t get_offset_table_entry_size() const;
120
121
  // Assuming that we have to read offset table 'idx', but we'd like to read more entries at once to reduce
122
  // the load of small network transfers (preferred: 'nEntries'). Return a range of indices to read.
123
  // This may be less than 'nEntries'. The returned range is [start, end)
124
  [[nodiscard]] std::pair<uint32_t, uint32_t> get_tile_offset_table_range_to_read(uint32_t idx, uint32_t nEntries) const;
125
126
private:
127
  heif_tiled_image_parameters m_parameters;
128
129
  struct TileOffset {
130
    uint64_t offset = TILD_OFFSET_NOT_LOADED;
131
    uint32_t size = 0;
132
  };
133
134
  // TODO uint64_t m_start_of_offset_table_in_file = 0;
135
  std::vector<TileOffset> m_offsets;
136
137
  // TODO size_t m_offset_table_start = 0; // start of offset table (= number of bytes in header)
138
  size_t m_header_size = 0; // including offset table
139
};
140
141
142
class ImageItem_Tiled : public ImageItem
143
{
144
public:
145
  ImageItem_Tiled(HeifContext* ctx, heif_item_id id);
146
147
  ImageItem_Tiled(HeifContext* ctx);
148
149
0
  uint32_t get_infe_type() const override { return fourcc("tili"); }
150
151
  // TODO: nclx depends on contained format
152
  // const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }
153
154
  heif_compression_format get_compression_format() const override;
155
156
  static Result<std::shared_ptr<ImageItem_Tiled>> add_new_tiled_item(HeifContext* ctx, const heif_tiled_image_parameters* parameters,
157
                                                                     const heif_encoder* encoder);
158
159
  Error add_image_tile(uint32_t tile_x, uint32_t tile_y,
160
                       const std::shared_ptr<HeifPixelImage>& image,
161
                       struct heif_encoder* encoder);
162
163
164
  Error on_load_file() override;
165
166
  void process_before_write() override;
167
168
  Error get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const override;
169
170
  int get_luma_bits_per_pixel() const override;
171
172
  int get_chroma_bits_per_pixel() const override;
173
174
  Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
175
                                         struct heif_encoder* encoder,
176
                                         const struct heif_encoding_options& options,
177
                                         enum heif_image_input_class input_class) override
178
0
  {
179
0
    return Error{heif_error_Unsupported_feature,
180
0
                 heif_suberror_Unspecified, "Cannot encode image to 'tild'"};
181
0
  }
182
183
  Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const struct heif_decoding_options& options,
184
                                                                  bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0) const override;
185
186
  heif_brand2 get_compatible_brand() const override;
187
188
  // --- tild
189
190
0
  void set_tild_header(const TiledHeader& header) { m_tild_header = header; }
191
192
0
  TiledHeader& get_tild_header() { return m_tild_header; }
193
194
0
  uint64_t get_next_tild_position() const { return m_next_tild_position; }
195
196
0
  void set_next_tild_position(uint64_t pos) { m_next_tild_position = pos; }
197
198
  heif_image_tiling get_heif_image_tiling() const override;
199
200
  void get_tile_size(uint32_t& w, uint32_t& h) const override;
201
202
private:
203
  TiledHeader m_tild_header;
204
  uint64_t m_next_tild_position = 0;
205
206
  uint32_t mReadChunkSize_bytes = 64*1024; // 64 kiB
207
  bool m_preload_offset_table = false;
208
209
  std::shared_ptr<ImageItem> m_tile_item;
210
  std::shared_ptr<class Decoder> m_tile_decoder;
211
212
  Result<DataExtent>
213
  get_compressed_data_for_tile(uint32_t tx, uint32_t ty) const;
214
215
  Result<std::shared_ptr<HeifPixelImage>> decode_grid_tile(const heif_decoding_options& options, uint32_t tx, uint32_t ty) const;
216
217
  Error load_tile_offset_entry(uint32_t idx);
218
219
  Error append_compressed_tile_data(std::vector<uint8_t>& data, uint32_t tx, uint32_t ty) const;
220
};
221
222
223
#endif //LIBHEIF_TILED_H