Skip to content

Commit

Permalink
fix various minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 22, 2024
1 parent 480b371 commit 7616daf
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 50 deletions.
10 changes: 5 additions & 5 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3317,7 +3317,7 @@ int heif_encoder_has_default(struct heif_encoder* encoder,
}


static void set_default_options(heif_encoding_options& options)
void set_default_encoding_options(heif_encoding_options& options)
{
options.version = 7;

Expand Down Expand Up @@ -3368,7 +3368,7 @@ heif_encoding_options* heif_encoding_options_alloc()
{
auto options = new heif_encoding_options;

set_default_options(*options);
set_default_encoding_options(*options);

return options;
}
Expand Down Expand Up @@ -3396,7 +3396,7 @@ struct heif_error heif_context_encode_image(struct heif_context* ctx,

heif_encoding_options options;
heif_color_profile_nclx nclx;
set_default_options(options);
set_default_encoding_options(options);
if (input_options) {
copy_options(options, *input_options);

Expand Down Expand Up @@ -3462,7 +3462,7 @@ struct heif_error heif_context_encode_grid(struct heif_context* ctx,
// TODO: Don't repeat this code from heif_context_encode_image()
heif_encoding_options options;
heif_color_profile_nclx nclx;
set_default_options(options);
set_default_encoding_options(options);
if (input_options) {
copy_options(options, *input_options);

Expand Down Expand Up @@ -3710,7 +3710,7 @@ struct heif_error heif_context_encode_thumbnail(struct heif_context* ctx,
std::shared_ptr<ImageItem> thumbnail_image;

heif_encoding_options options;
set_default_options(options);
set_default_encoding_options(options);

if (input_options != nullptr) {
copy_options(options, *input_options);
Expand Down
2 changes: 1 addition & 1 deletion libheif/bitstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool StreamReader_memory::read(void* data, size_t size)

bool StreamReader_memory::seek(uint64_t position)
{
if (position > m_length || position < 0)
if (position > m_length)
return false;

m_position = position;
Expand Down
8 changes: 4 additions & 4 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ std::string Box_Error::dump(Indent& indent) const
sstr << indent << '\'' << fourcc_to_string(m_box_type_with_parse_error) << "' parse error: " << m_error.message << "\n";
sstr << indent << "fatality: ";
switch (m_fatality) {
case parse_error_fatality::fatal: sstr << "fatal\n";
case parse_error_fatality::ignorable: sstr << "ignorable\n";
case parse_error_fatality::optional: sstr << "optional\n";
case parse_error_fatality::fatal: sstr << "fatal\n"; break;
case parse_error_fatality::ignorable: sstr << "ignorable\n"; break;
case parse_error_fatality::optional: sstr << "optional\n"; break;
}

return sstr.str();
Expand Down Expand Up @@ -3643,7 +3643,7 @@ void Box_iref::overwrite_reference(heif_item_id from_id, uint32_t type, uint32_t
{
for (auto& ref : m_references) {
if (ref.from_item_ID == from_id && ref.header.get_short_type() == type) {
assert(reference_idx >= 0 && reference_idx < ref.to_item_ID.size());
assert(reference_idx < ref.to_item_ID.size());

ref.to_item_ID[reference_idx] = to_item;
return;
Expand Down
18 changes: 9 additions & 9 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -1293,15 +1293,15 @@ class Box_cclv : public Box
Error parse(BitstreamRange& range, const heif_security_limits*) override;

private:
bool m_ccv_primaries_valid;
int32_t m_ccv_primaries_x[3];
int32_t m_ccv_primaries_y[3];
bool m_ccv_min_luminance_valid;
uint32_t m_ccv_min_luminance_value;
bool m_ccv_max_luminance_valid;
uint32_t m_ccv_max_luminance_value;
bool m_ccv_avg_luminance_valid;
uint32_t m_ccv_avg_luminance_value;
bool m_ccv_primaries_valid = false;
int32_t m_ccv_primaries_x[3] {};
int32_t m_ccv_primaries_y[3] {};
bool m_ccv_min_luminance_valid = false;
uint32_t m_ccv_min_luminance_value = 0;
bool m_ccv_max_luminance_valid = false;
uint32_t m_ccv_max_luminance_value = 0;
bool m_ccv_avg_luminance_valid = false;
uint32_t m_ccv_avg_luminance_value = 0;
};


Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/uncompressed/decoder_abstract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void AbstractDecoder::processComponentRow(ChannelListEntry& entry, UncompressedB

void AbstractDecoder::processComponentTileSample(UncompressedBitReader& srcBits, const ChannelListEntry& entry, uint64_t dst_offset, uint32_t tile_x)
{
uint64_t dst_sample_offset = tile_x * entry.bytes_per_component_sample;
uint64_t dst_sample_offset = uint64_t{tile_x} * entry.bytes_per_component_sample;
int val = srcBits.get_bits(entry.bits_per_component_sample);
memcpy(entry.dst_plane + dst_offset + dst_sample_offset, &val, entry.bytes_per_component_sample);
}
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed/decoder_component_interleave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Error ComponentInterleaveDecoder::decode_tile(const HeifContext* context,

uint32_t bytes_per_tile_row = (bits_per_component * entry.tile_width + 7) / 8;
skip_to_alignment(bytes_per_tile_row, m_uncC->get_row_align_size());
uint64_t bytes_per_tile = bytes_per_tile_row * entry.tile_height;
uint64_t bytes_per_tile = uint64_t{bytes_per_tile_row} * entry.tile_height;
total_tile_size += bytes_per_tile;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ Error ComponentInterleaveDecoder::decode_tile(const HeifContext* context,
for (uint32_t y = 0; y < entry.tile_height; y++) {
srcBits.markRowStart();
if (entry.use_channel) {
uint64_t dst_row_offset = (out_y0 + y) * entry.dst_plane_stride;
uint64_t dst_row_offset = uint64_t{(out_y0 + y)} * entry.dst_plane_stride;
processComponentTileRow(entry, srcBits, dst_row_offset + out_x0 * entry.bytes_per_component_sample);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed/decoder_mixed_interleave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Error MixedInterleaveDecoder::decode_tile(const HeifContext* context,
uint32_t bits_per_row = entry.bits_per_component_sample * entry.tile_width;
bits_per_row = (bits_per_row + 7) & ~7U; // align to byte boundary

tile_size += bits_per_row / 8 * entry.tile_height;
tile_size += uint64_t{bits_per_row} / 8 * entry.tile_height;
}
else {
uint32_t bits_per_component = entry.bits_per_component_sample;
Expand All @@ -60,7 +60,7 @@ Error MixedInterleaveDecoder::decode_tile(const HeifContext* context,
uint32_t bits_per_row = bits_per_component * entry.tile_width;
bits_per_row = (bits_per_row + 7) & ~7U; // align to byte boundary

tile_size += bits_per_row / 8 * entry.tile_height;
tile_size += uint64_t{bits_per_row} / 8 * entry.tile_height;
}
}

Expand Down
10 changes: 5 additions & 5 deletions libheif/codecs/uncompressed/unc_boxes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ uint64_t Box_uncC::compute_tile_data_size_bytes(uint32_t tile_width, uint32_t ti
if (m_profile != 0) {
switch (m_profile) {
case fourcc("rgba"):
return 4 * tile_width * tile_height;
return 4 * uint64_t{tile_width} * tile_height;

case fourcc("rgb3"):
return 3 * tile_width * tile_height;
return 3 * uint64_t{tile_width} * tile_height;

default:
assert(false);
Expand All @@ -433,7 +433,7 @@ uint64_t Box_uncC::compute_tile_data_size_bytes(uint32_t tile_width, uint32_t ti
bytes_per_pixel += comp.component_bit_depth / 8;
}

return bytes_per_pixel * tile_width * tile_height;
return bytes_per_pixel * uint64_t{tile_width} * tile_height;
}
default:
assert(false);
Expand Down Expand Up @@ -713,7 +713,7 @@ Error Box_cpat::parse(BitstreamRange& range, const heif_security_limits* limits)
"Maximum Bayer pattern size exceeded."};
}

m_components.resize(m_pattern_width * m_pattern_height);
m_components.resize(size_t{m_pattern_width} * m_pattern_height);

for (uint16_t i = 0; i < m_pattern_height; i++) {
for (uint16_t j = 0; j < m_pattern_width; j++) {
Expand Down Expand Up @@ -747,7 +747,7 @@ Error Box_cpat::write(StreamWriter& writer) const
{
size_t box_start = reserve_box_header_space(writer);

if (m_pattern_width * m_pattern_height != m_components.size()) {
if (m_pattern_width * size_t{m_pattern_height} != m_components.size()) {
// needs to be rectangular
return {heif_error_Usage_error,
heif_suberror_Invalid_parameter_value,
Expand Down
5 changes: 5 additions & 0 deletions libheif/image-items/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,20 @@ std::string ImageGrid::dump() const
}


extern void set_default_encoding_options(heif_encoding_options& options);


ImageItem_Grid::ImageItem_Grid(HeifContext* ctx)
: ImageItem(ctx)
{
set_default_encoding_options(m_encoding_options);
}


ImageItem_Grid::ImageItem_Grid(HeifContext* ctx, heif_item_id id)
: ImageItem(ctx, id)
{
set_default_encoding_options(m_encoding_options);
}


Expand Down
2 changes: 1 addition & 1 deletion libheif/image-items/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ImageItem_Grid : public ImageItem

const ImageGrid& get_grid_spec() const { return m_grid_spec; }

void set_grid_spec(const ImageGrid& grid) { m_grid_spec = grid; m_grid_tile_ids.resize(grid.get_rows() * grid.get_columns()); }
void set_grid_spec(const ImageGrid& grid) { m_grid_spec = grid; m_grid_tile_ids.resize(size_t{grid.get_rows()} * grid.get_columns()); }

const std::vector<heif_item_id>& get_grid_tiles() const { return m_grid_tile_ids; }

Expand Down
45 changes: 33 additions & 12 deletions libheif/image-items/tiled.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ uint32_t nTiles_v(const heif_tiled_image_parameters& params)
}


void Box_tilC::init_heif_tiled_image_parameters(heif_tiled_image_parameters& params)
{
params.version = 1;

params.image_width = 0;
params.image_height = 0;
params.tile_width = 0;
params.tile_height = 0;
params.compression_format_fourcc = 0;
params.offset_field_length = 40;
params.size_field_length = 24;
params.number_of_extra_dimensions = 0;

for (uint32_t& dim : params.extra_dimensions) {
dim = 0;
}

params.tiles_are_sequential = false;
}


void Box_tilC::derive_box_version()
{
set_version(1);
Expand Down Expand Up @@ -249,7 +270,7 @@ Error Box_tilC::parse(BitstreamRange& range, const heif_security_limits* limits)
}


Error TildHeader::set_parameters(const heif_tiled_image_parameters& params)
Error TiledHeader::set_parameters(const heif_tiled_image_parameters& params)
{
m_parameters = params;

Expand All @@ -271,7 +292,7 @@ Error TildHeader::set_parameters(const heif_tiled_image_parameters& params)
}


Error TildHeader::read_full_offset_table(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id, const heif_security_limits* limits)
Error TiledHeader::read_full_offset_table(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id, const heif_security_limits* limits)
{
auto max_tiles = heif_get_global_security_limits()->max_number_of_tiles;

Expand All @@ -286,8 +307,8 @@ Error TildHeader::read_full_offset_table(const std::shared_ptr<HeifFile>& file,
}


Error TildHeader::read_offset_table_range(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id,
uint64_t start, uint64_t end)
Error TiledHeader::read_offset_table_range(const std::shared_ptr<HeifFile>& file, heif_item_id tild_id,
uint64_t start, uint64_t end)
{
const Error eofError(heif_error_Invalid_input,
heif_suberror_Unspecified,
Expand Down Expand Up @@ -328,20 +349,20 @@ Error TildHeader::read_offset_table_range(const std::shared_ptr<HeifFile>& file,
}


size_t TildHeader::get_header_size() const
size_t TiledHeader::get_header_size() const
{
assert(m_header_size);
return m_header_size;
}


uint32_t TildHeader::get_offset_table_entry_size() const
uint32_t TiledHeader::get_offset_table_entry_size() const
{
return (m_parameters.offset_field_length + m_parameters.size_field_length) / 8;
}


std::pair<uint32_t, uint32_t> TildHeader::get_tile_offset_table_range_to_read(uint32_t idx, uint32_t nEntries) const
std::pair<uint32_t, uint32_t> TiledHeader::get_tile_offset_table_range_to_read(uint32_t idx, uint32_t nEntries) const
{
uint32_t start = idx;
uint32_t end = idx+1;
Expand Down Expand Up @@ -369,9 +390,9 @@ std::pair<uint32_t, uint32_t> TildHeader::get_tile_offset_table_range_to_read(ui
}


void TildHeader::set_tild_tile_range(uint32_t tile_x, uint32_t tile_y, uint64_t offset, uint32_t size)
void TiledHeader::set_tild_tile_range(uint32_t tile_x, uint32_t tile_y, uint64_t offset, uint32_t size)
{
uint64_t idx = tile_y * nTiles_h(m_parameters) + tile_x;
uint64_t idx = uint64_t{tile_y} * nTiles_h(m_parameters) + tile_x;
m_offsets[idx].offset = offset;
m_offsets[idx].size = size;
}
Expand All @@ -388,7 +409,7 @@ void writevec(uint8_t* data, size_t& idx, I value, int len)
}


std::vector<uint8_t> TildHeader::write_offset_table()
std::vector<uint8_t> TiledHeader::write_offset_table()
{
uint64_t nTiles = number_of_tiles(m_parameters);

Expand Down Expand Up @@ -416,7 +437,7 @@ std::vector<uint8_t> TildHeader::write_offset_table()
}


std::string TildHeader::dump() const
std::string TiledHeader::dump() const
{
std::stringstream sstr;

Expand Down Expand Up @@ -529,7 +550,7 @@ ImageItem_Tiled::add_new_tiled_item(HeifContext* ctx, const heif_tiled_image_par

// Create header + offset table

TildHeader tild_header;
TiledHeader tild_header;
tild_header.set_parameters(*parameters);
tild_header.set_compression_format(encoder->plugin->compression_format);

Expand Down
12 changes: 8 additions & 4 deletions libheif/image-items/tiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Box_tilC : public FullBox
Box_tilC()
{
set_short_type(fourcc("tilC"));

init_heif_tiled_image_parameters(m_parameters);
}

bool is_essential() const override { return true; }
Expand All @@ -72,14 +74,16 @@ class Box_tilC : public FullBox

private:
heif_tiled_image_parameters m_parameters;

static void init_heif_tiled_image_parameters(heif_tiled_image_parameters& params);
};


#define TILD_OFFSET_NOT_AVAILABLE 0
#define TILD_OFFSET_SEE_LOWER_RESOLUTION_LAYER 1
#define TILD_OFFSET_NOT_LOADED 10

class TildHeader
class TiledHeader
{
public:
Error set_parameters(const heif_tiled_image_parameters& params);
Expand Down Expand Up @@ -175,9 +179,9 @@ class ImageItem_Tiled : public ImageItem

// --- tild

void set_tild_header(const TildHeader& header) { m_tild_header = header; }
void set_tild_header(const TiledHeader& header) { m_tild_header = header; }

TildHeader& get_tild_header() { return m_tild_header; }
TiledHeader& get_tild_header() { return m_tild_header; }

uint64_t get_next_tild_position() const { return m_next_tild_position; }

Expand All @@ -188,7 +192,7 @@ class ImageItem_Tiled : public ImageItem
void get_tile_size(uint32_t& w, uint32_t& h) const override;

private:
TildHeader m_tild_header;
TiledHeader m_tild_header;
uint64_t m_next_tild_position = 0;

uint32_t mReadChunkSize_bytes = 64*1024; // 64 kiB
Expand Down
2 changes: 2 additions & 0 deletions libheif/image-items/unc_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ Result<std::shared_ptr<ImageItem_uncompressed>> ImageItem_uncompressed::add_unci

const unciHeaders& headers = *genHeadersResult;

assert(headers.uncC);

if (headers.uncC) {
file->add_property(unci_id, headers.uncC, true);
}
Expand Down
Loading

0 comments on commit 7616daf

Please sign in to comment.