Skip to content

Commit

Permalink
use uint32_t for image size (ispe limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Aug 10, 2024
1 parent 8840cc4 commit c130532
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
54 changes: 40 additions & 14 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,8 @@ struct heif_error heif_image_handle_decode_image_tile(const struct heif_image_ha


struct heif_error heif_context_add_pyramid_entity_group(struct heif_context* ctx,
uint32_t tile_width,
uint32_t tile_height,
uint16_t tile_width,
uint16_t tile_height,
uint32_t num_layers,
const heif_pyramid_layer_info* in_layers,
heif_item_id* out_group_id)
Expand Down Expand Up @@ -1456,30 +1456,41 @@ enum heif_chroma heif_image_get_chroma_format(const struct heif_image* img)
}


static int uint32_to_int(uint32_t v)
{
if (v==0 || v>std::numeric_limits<int>::max()) {
return -1;
}
else {
return static_cast<int>(v);
}
}


int heif_image_get_width(const struct heif_image* img, enum heif_channel channel)
{
return img->image->get_width(channel);
return uint32_to_int(img->image->get_width(channel));
}


int heif_image_get_height(const struct heif_image* img, enum heif_channel channel)
{
return img->image->get_height(channel);
return uint32_to_int(img->image->get_height(channel));
}


int heif_image_get_primary_width(const struct heif_image* img)
{
if (img->image->get_colorspace() == heif_colorspace_RGB) {
if (img->image->get_chroma_format() == heif_chroma_444) {
return img->image->get_width(heif_channel_G);
return uint32_to_int(img->image->get_width(heif_channel_G));
}
else {
return img->image->get_width(heif_channel_interleaved);
return uint32_to_int(img->image->get_width(heif_channel_interleaved));
}
}
else {
return img->image->get_width(heif_channel_Y);
return uint32_to_int(img->image->get_width(heif_channel_Y));
}
}

Expand All @@ -1488,14 +1499,14 @@ int heif_image_get_primary_height(const struct heif_image* img)
{
if (img->image->get_colorspace() == heif_colorspace_RGB) {
if (img->image->get_chroma_format() == heif_chroma_444) {
return img->image->get_height(heif_channel_G);
return uint32_to_int(img->image->get_height(heif_channel_G));
}
else {
return img->image->get_height(heif_channel_interleaved);
return uint32_to_int(img->image->get_height(heif_channel_interleaved));
}
}
else {
return img->image->get_height(heif_channel_Y);
return uint32_to_int(img->image->get_height(heif_channel_Y));
}
}

Expand All @@ -1505,10 +1516,17 @@ heif_error heif_image_crop(struct heif_image* img,
{
std::shared_ptr<HeifPixelImage> out_img;

int w = img->image->get_width();
int h = img->image->get_height();
uint32_t w = img->image->get_width();
uint32_t h = img->image->get_height();

if (w==0 || w>0x7FFFFFFF ||
h==0 || h>0x7FFFFFFF) {
return heif_error{heif_error_Usage_error,
heif_suberror_Invalid_image_size,
"Image size exceeds maximum supported size"};
}

Error err = img->image->crop(left, w - 1 - right, top, h - 1 - bottom, out_img);
Error err = img->image->crop(left, static_cast<int>(w) - 1 - right, top, static_cast<int>(h) - 1 - bottom, out_img);
if (err) {
return err.error_struct(img->image.get());
}
Expand Down Expand Up @@ -3115,6 +3133,11 @@ struct heif_error heif_context_add_grid_image(struct heif_context* ctx,
return Error(heif_error_Usage_error,
heif_suberror_Invalid_parameter_value).error_struct(ctx->context.get());
}
else if (tile_rows > 0xFFFF || tile_columns > 0xFFFF) {
return heif_error{heif_error_Usage_error,
heif_suberror_Invalid_image_size,
"Number of tile rows/columns may not exceed 65535"};
}


std::vector<heif_item_id> tiles(tile_rows * tile_columns);
Expand All @@ -3123,7 +3146,10 @@ struct heif_error heif_context_add_grid_image(struct heif_context* ctx,
}

std::shared_ptr<HeifContext::Image> gridimage;
Error error = ctx->context->add_grid_item(tiles, image_width, image_height, tile_rows, tile_columns, gridimage);
Error error = ctx->context->add_grid_item(tiles, image_width, image_height,
static_cast<uint16_t>(tile_rows),
static_cast<uint16_t>(tile_columns),
gridimage);

if (error != Error::Ok) {
return error.error_struct(ctx->context.get());
Expand Down
4 changes: 2 additions & 2 deletions libheif/api/libheif/heif.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,8 @@ struct heif_pyramid_layer_info {

LIBHEIF_API
struct heif_error heif_context_add_pyramid_entity_group(struct heif_context* ctx,
uint32_t tile_width,
uint32_t tile_height,
uint16_t tile_width,
uint16_t tile_height,
uint32_t num_layers,
const struct heif_pyramid_layer_info* layers,
heif_item_id* out_group_id);
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ Error UncompressedImageCodec::encode_uncompressed_image(const std::shared_ptr<He
uint8_t* src_data = src_image->get_plane(channel, &src_stride);
uint64_t out_size = src_image->get_height() * src_image->get_width();
data.resize(data.size() + out_size);
for (int y = 0; y < src_image->get_height(); y++) {
for (uint32_t y = 0; y < src_image->get_height(); y++) {
memcpy(data.data() + offset + y * src_image->get_width(), src_data + src_stride * y, src_image->get_width());
}
offset += out_size;
Expand Down Expand Up @@ -1319,7 +1319,7 @@ Error UncompressedImageCodec::encode_uncompressed_image(const std::shared_ptr<He
uint8_t* src_data = src_image->get_plane(heif_channel_interleaved, &src_stride);
uint64_t out_size = src_image->get_height() * src_image->get_width() * bytes_per_pixel;
data.resize(out_size);
for (int y = 0; y < src_image->get_height(); y++) {
for (uint32_t y = 0; y < src_image->get_height(); y++) {
memcpy(data.data() + y * src_image->get_width() * bytes_per_pixel, src_data + src_stride * y, src_image->get_width() * bytes_per_pixel);
}
heif_file->append_iloc_data(out_image->get_id(), data, 0);
Expand Down
4 changes: 2 additions & 2 deletions libheif/color-conversion/colorconversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ std::shared_ptr<HeifPixelImage> convert_colorspace(const std::shared_ptr<HeifPix
{
// --- check that input image is valid

int width = input->get_width();
int height = input->get_height();
uint32_t width = input->get_width();
uint32_t height = input->get_height();

// alpha image should have full image resolution

Expand Down
8 changes: 4 additions & 4 deletions libheif/pixelimage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,22 @@ bool HeifPixelImage::has_alpha() const
}


int HeifPixelImage::get_width(enum heif_channel channel) const
uint32_t HeifPixelImage::get_width(enum heif_channel channel) const
{
auto iter = m_planes.find(channel);
if (iter == m_planes.end()) {
return -1;
return 0;
}

return iter->second.m_width;
}


int HeifPixelImage::get_height(enum heif_channel channel) const
uint32_t HeifPixelImage::get_height(enum heif_channel channel) const
{
auto iter = m_planes.find(channel);
if (iter == m_planes.end()) {
return -1;
return 0;
}

return iter->second.m_height;
Expand Down
12 changes: 6 additions & 6 deletions libheif/pixelimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ class HeifPixelImage : public std::enable_shared_from_this<HeifPixelImage>,

void set_premultiplied_alpha(bool flag) { m_premultiplied_alpha = flag; }

int get_width() const { return m_width; }
uint32_t get_width() const { return m_width; }

int get_height() const { return m_height; }
uint32_t get_height() const { return m_height; }

int get_width(enum heif_channel channel) const;
uint32_t get_width(enum heif_channel channel) const;

int get_height(enum heif_channel channel) const;
uint32_t get_height(enum heif_channel channel) const;

heif_chroma get_chroma_format() const { return m_chroma; }

Expand Down Expand Up @@ -189,8 +189,8 @@ class HeifPixelImage : public std::enable_shared_from_this<HeifPixelImage>,
uint32_t stride = 0; // bytes per line
};

int m_width = 0;
int m_height = 0;
uint32_t m_width = 0;
uint32_t m_height = 0;
heif_colorspace m_colorspace = heif_colorspace_undefined;
heif_chroma m_chroma = heif_chroma_undefined;
bool m_premultiplied_alpha = false;
Expand Down

0 comments on commit c130532

Please sign in to comment.