Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with unpacked size computation #1141

Merged
merged 3 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/lib/OpenEXRCore/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,42 @@ alloc_chunk_table (

/**************************************/

static uint64_t
compute_chunk_unpack_size (
int y,
int width,
int height,
int lpc,
const struct _internal_exr_part* part)
{
uint64_t unpacksize = 0;
if (part->chan_has_line_sampling || height != lpc)
{
const exr_attr_chlist_t* chanlist = part->channels->chlist;
for (int c = 0; c < chanlist->num_channels; ++c)
{
const exr_attr_chlist_entry_t* curc = (chanlist->entries + c);
uint64_t chansz = ((curc->pixel_type == EXR_PIXEL_HALF) ? 2 : 4);
chansz *= ((uint64_t) width);
if (curc->x_sampling > 1) chansz /= ((uint64_t) curc->x_sampling);
chansz *= ((uint64_t) height);
if (curc->y_sampling > 1)
{
if (height > 1)
chansz /= ((uint64_t) curc->y_sampling);
else if ((y % ((int) curc->y_sampling)) != 0)
chansz = 0;
}
unpacksize += chansz;
}
}
else
unpacksize = part->unpacked_size_per_chunk;
return unpacksize;
}

/**************************************/

exr_result_t
exr_read_scanline_chunk_info (
exr_const_context_t ctxt, int part_index, int y, exr_chunk_info_t* cinfo)
Expand Down Expand Up @@ -335,28 +371,8 @@ exr_read_scanline_chunk_info (
}
else
{
uint64_t unpacksize = 0;
if (cinfo->height == lpc)
{
unpacksize = part->unpacked_size_per_chunk;
}
else
{
const exr_attr_chlist_t* chanlist = part->channels->chlist;
for (int c = 0; c < chanlist->num_channels; ++c)
{
const exr_attr_chlist_entry_t* curc = (chanlist->entries + c);
if (curc->y_sampling > 1 || curc->x_sampling > 1)
unpacksize +=
(((uint64_t) (cinfo->height / curc->y_sampling)) *
((uint64_t) (cinfo->width / curc->x_sampling)) *
((curc->pixel_type == EXR_PIXEL_HALF) ? 2 : 4));
else
unpacksize +=
((uint64_t) cinfo->height) * ((uint64_t) cinfo->width) *
((curc->pixel_type == EXR_PIXEL_HALF) ? 2 : 4);
}
}
uint64_t unpacksize = compute_chunk_unpack_size (
y, cinfo->width, cinfo->height, lpc, part);

++rdcnt;
if (data[rdcnt] < 0 ||
Expand Down Expand Up @@ -846,8 +862,8 @@ exr_read_tile_chunk_info (
levelx,
levely,
cidx,
(int)tdata[4],
(int)unpacksize,
(int) tdata[4],
(int) unpacksize,
fsize);
}
cinfo->packed_size = (uint64_t) tdata[4];
Expand Down Expand Up @@ -1292,7 +1308,8 @@ exr_write_scanline_chunk_info (
cinfo->sample_count_table_size = 0;
cinfo->data_offset = 0;
cinfo->packed_size = 0;
cinfo->unpacked_size = part->unpacked_size_per_chunk;
cinfo->unpacked_size =
compute_chunk_unpack_size (y, cinfo->width, cinfo->height, lpc, part);

return EXR_UNLOCK_AND_RETURN_PCTXT (EXR_ERR_SUCCESS);
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/OpenEXRCore/internal_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ struct _internal_exr_part
int32_t* tile_level_tile_size_y;

uint64_t unpacked_size_per_chunk;
int32_t lines_per_chunk;
int16_t lines_per_chunk;
int16_t chan_has_line_sampling;

int32_t chunk_count;
uint64_t chunk_table_offset;
Expand Down
33 changes: 25 additions & 8 deletions src/lib/OpenEXRCore/parse_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,7 @@ internal_exr_compute_chunk_offset_size (struct _internal_exr_part* curpart)
const exr_attr_chlist_t* channels = curpart->channels->chlist;
uint64_t unpackedsize = 0;
int64_t w;
meshula marked this conversation as resolved.
Show resolved Hide resolved
int hasLineSample = 0;

w = ((int64_t) dw.max.x) - ((int64_t) dw.min.x) + 1;

Expand Down Expand Up @@ -2118,12 +2119,20 @@ internal_exr_compute_chunk_offset_size (struct _internal_exr_part* curpart)
cunpsz = 2;
else
cunpsz = 4;
unpackedsize +=
(cunpsz *
(uint64_t) (((uint64_t) tiledesc->x_size + (uint64_t) xsamp - 1) / (uint64_t) xsamp) *
(uint64_t) (((uint64_t) tiledesc->y_size + (uint64_t) ysamp - 1) / (uint64_t) ysamp));
cunpsz *=
(uint64_t) (((uint64_t) tiledesc->x_size + (uint64_t) xsamp - 1) / (uint64_t) xsamp);
if (ysamp > 1)
{
hasLineSample = 1;
cunpsz *=
(uint64_t) (((uint64_t) tiledesc->y_size + (uint64_t) ysamp - 1) / (uint64_t) ysamp);
}
else
cunpsz *= (uint64_t) tiledesc->y_size;
unpackedsize += cunpsz;
}
curpart->unpacked_size_per_chunk = unpackedsize;
curpart->chan_has_line_sampling = ((int16_t) hasLineSample);
}
else
{
Expand All @@ -2146,7 +2155,6 @@ internal_exr_compute_chunk_offset_size (struct _internal_exr_part* curpart)
return -1;
}

curpart->lines_per_chunk = linePerChunk;
for (int c = 0; c < channels->num_channels; ++c)
{
int32_t xsamp = channels->entries[c].x_sampling;
Expand All @@ -2156,11 +2164,20 @@ internal_exr_compute_chunk_offset_size (struct _internal_exr_part* curpart)
cunpsz = 2;
else
cunpsz = 4;
unpackedsize +=
(cunpsz * (uint64_t) ((w + xsamp - 1) / xsamp) *
(uint64_t) ((linePerChunk + ysamp - 1) / ysamp));
cunpsz *= (uint64_t) (w / xsamp);
cunpsz *= ((uint64_t) linePerChunk);
kdt3rd marked this conversation as resolved.
Show resolved Hide resolved
if (ysamp > 1)
{
hasLineSample = 1;
if (linePerChunk > 1)
cunpsz *= (uint64_t) (linePerChunk / ysamp);
}
unpackedsize += cunpsz;
}

curpart->unpacked_size_per_chunk = unpackedsize;
curpart->lines_per_chunk = ((int16_t) linePerChunk);
curpart->chan_has_line_sampling = ((int16_t) hasLineSample);

/* h = max - min + 1, but to do size / divide by round,
* we'd do linePerChunk - 1, so the math cancels */
Expand Down