Skip to content

Commit

Permalink
enable deep file checks for core
Browse files Browse the repository at this point in the history
Signed-off-by: Kimball Thurston <kdt3rd@gmail.com>
  • Loading branch information
kdt3rd committed Feb 3, 2024
1 parent 8743203 commit e892a23
Showing 1 changed file with 143 additions and 34 deletions.
177 changes: 143 additions & 34 deletions src/lib/OpenEXRUtil/ImfCheckFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,13 +1200,85 @@ runChecks (T& source, bool reduceMemory, bool reduceTime)
return threw;
}

static exr_result_t
realloc_deepdata(exr_decode_pipeline_t* decode)
{
int32_t w = decode->chunk.width;
int32_t h = decode->chunk.height;
uint64_t totsamps = 0, bytes = 0;
const int32_t *sampbuffer = decode->sample_count_table;
std::vector<uint8_t>* ud = static_cast<std::vector<uint8_t>*>(
decode->decoding_user_data);

if ( ! ud )
{
for (int c = 0; c < decode->channel_count; c++)
{
exr_coding_channel_info_t& outc = decode->channels[c];
outc.decode_to_ptr = NULL;
outc.user_pixel_stride = outc.user_bytes_per_element;
outc.user_line_stride = 0;
}
return EXR_ERR_SUCCESS;
}

if ((decode->decode_flags &
EXR_DECODE_SAMPLE_COUNTS_AS_INDIVIDUAL))
{
for (int32_t y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
totsamps += sampbuffer[x];
sampbuffer += w;
}
}
else
{
for (int32_t y = 0; y < h; ++y)
totsamps += sampbuffer[y*w + w - 1];
}

for (int c = 0; c < decode->channel_count; c++)
{
exr_coding_channel_info_t& outc = decode->channels[c];
bytes += totsamps * outc.user_bytes_per_element;
}

if (bytes >= gMaxBytesPerDeepScanline * h)
{
for (int c = 0; c < decode->channel_count; c++)
{
exr_coding_channel_info_t& outc = decode->channels[c];
outc.decode_to_ptr = NULL;
outc.user_pixel_stride = outc.user_bytes_per_element;
outc.user_line_stride = 0;
}
return EXR_ERR_SUCCESS;
}

if (ud->size () < bytes)
ud->resize (bytes);

uint8_t* dptr = &((*ud)[0]);
for (int c = 0; c < decode->channel_count; c++)
{
exr_coding_channel_info_t& outc = decode->channels[c];
outc.decode_to_ptr = dptr;
outc.user_pixel_stride = outc.user_bytes_per_element;
outc.user_line_stride = 0;

dptr += totsamps * (uint64_t) outc.user_bytes_per_element;
}
return EXR_ERR_SUCCESS;
}

////////////////////////////////////////

bool
readCoreScanlinePart (
exr_context_t f, int part, bool reduceMemory, bool reduceTime)
{
exr_result_t rv;
exr_result_t rv, frv;
exr_attr_box2i_t datawin;
rv = exr_get_data_window (f, part, &datawin);
if (rv != EXR_ERR_SUCCESS) return true;
Expand All @@ -1224,6 +1296,8 @@ readCoreScanlinePart (
rv = exr_get_scanlines_per_chunk (f, part, &lines_per_chunk);
if (rv != EXR_ERR_SUCCESS) return true;

frv = rv;

for (uint64_t chunk = 0; chunk < height; chunk += lines_per_chunk)
{
exr_chunk_info_t cinfo = {0};
Expand All @@ -1232,6 +1306,7 @@ readCoreScanlinePart (
rv = exr_read_scanline_chunk_info (f, part, y, &cinfo);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime) break;
continue;
}
Expand All @@ -1253,48 +1328,66 @@ readCoreScanlinePart (
(uint64_t) lines_per_chunk;
}

// TODO: check we are supposed to multiple by lines per chunk above
doread = true;
if (reduceMemory && bytes >= gMaxBytesPerScanline) doread = false;

if (doread) imgdata.resize (bytes);
if (cinfo.type == EXR_STORAGE_DEEP_SCANLINE)
{
decoder.decoding_user_data = &imgdata;
decoder.realloc_nonimage_data_fn = &realloc_deepdata;
}
else
{
if (reduceMemory && bytes >= gMaxBytesPerScanline) doread = false;

if (doread) imgdata.resize (bytes);
}
rv = exr_decoding_choose_default_routines (f, part, &decoder);
if (rv != EXR_ERR_SUCCESS) break;
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
break;
}
}
else
{
rv = exr_decoding_update (f, part, &cinfo, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime) break;
continue;
}
}

if (doread)
{
uint8_t* dptr = &(imgdata[0]);
for (int c = 0; c < decoder.channel_count; c++)
if (cinfo.type != EXR_STORAGE_DEEP_SCANLINE)
{
exr_coding_channel_info_t& outc = decoder.channels[c];
outc.decode_to_ptr = dptr;
outc.user_pixel_stride = outc.user_bytes_per_element;
outc.user_line_stride = outc.user_pixel_stride * width;
dptr += width * (uint64_t) outc.user_bytes_per_element *
uint8_t* dptr = &(imgdata[0]);
for (int c = 0; c < decoder.channel_count; c++)
{
exr_coding_channel_info_t& outc = decoder.channels[c];
outc.decode_to_ptr = dptr;
outc.user_pixel_stride = outc.user_bytes_per_element;
outc.user_line_stride = outc.user_pixel_stride * width;

dptr += width * (uint64_t) outc.user_bytes_per_element *
(uint64_t) lines_per_chunk;
}
}

rv = exr_decoding_run (f, part, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime) break;
}
}
}

exr_decoding_destroy (f, &decoder);

return (rv != EXR_ERR_SUCCESS);
return (frv != EXR_ERR_SUCCESS);
}

////////////////////////////////////////
Expand All @@ -1303,7 +1396,7 @@ bool
readCoreTiledPart (
exr_context_t f, int part, bool reduceMemory, bool reduceTime)
{
exr_result_t rv;
exr_result_t rv, frv;

exr_attr_box2i_t datawin;
rv = exr_get_data_window (f, part, &datawin);
Expand All @@ -1321,6 +1414,7 @@ readCoreTiledPart (
rv = exr_get_tile_levels (f, part, &levelsx, &levelsy);
if (rv != EXR_ERR_SUCCESS) return true;

frv = rv;
bool keepgoing = true;
for (int32_t ylevel = 0; keepgoing && ylevel < levelsy; ++ylevel)
{
Expand All @@ -1330,6 +1424,7 @@ readCoreTiledPart (
rv = exr_get_level_sizes (f, part, xlevel, ylevel, &levw, &levh);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime)
{
keepgoing = false;
Expand All @@ -1342,6 +1437,7 @@ readCoreTiledPart (
rv = exr_get_tile_sizes (f, part, xlevel, ylevel, &curtw, &curth);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime)
{
keepgoing = false;
Expand Down Expand Up @@ -1371,6 +1467,7 @@ readCoreTiledPart (
f, part, tx, ty, xlevel, ylevel, &cinfo);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime)
{
keepgoing = false;
Expand All @@ -1385,6 +1482,7 @@ readCoreTiledPart (
exr_decoding_initialize (f, part, &cinfo, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
keepgoing = false;
break;
}
Expand All @@ -1406,14 +1504,23 @@ readCoreTiledPart (
}

doread = true;
if (reduceMemory && bytes >= gMaxTileBytes)
doread = false;
if (cinfo.type == EXR_STORAGE_DEEP_TILED)
{
decoder.decoding_user_data = &tiledata;
decoder.realloc_nonimage_data_fn = &realloc_deepdata;
}
else
{
if (reduceMemory && bytes >= gMaxTileBytes)
doread = false;

if (doread) tiledata.resize (bytes);
if (doread) tiledata.resize (bytes);
}
rv = exr_decoding_choose_default_routines (
f, part, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
keepgoing = false;
break;
}
Expand All @@ -1423,6 +1530,7 @@ readCoreTiledPart (
rv = exr_decoding_update (f, part, &cinfo, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime)
{
keepgoing = false;
Expand All @@ -1434,24 +1542,28 @@ readCoreTiledPart (

if (doread)
{
uint8_t* dptr = &(tiledata[0]);
for (int c = 0; c < decoder.channel_count; c++)
if (cinfo.type != EXR_STORAGE_DEEP_TILED)
{
exr_coding_channel_info_t& outc =
decoder.channels[c];
outc.decode_to_ptr = dptr;
outc.user_pixel_stride =
outc.user_bytes_per_element;
outc.user_line_stride =
outc.user_pixel_stride * curtw;
dptr += (uint64_t) curtw *
uint8_t* dptr = &(tiledata[0]);
for (int c = 0; c < decoder.channel_count; c++)
{
exr_coding_channel_info_t& outc =
decoder.channels[c];
outc.decode_to_ptr = dptr;
outc.user_pixel_stride =
outc.user_bytes_per_element;
outc.user_line_stride =
outc.user_pixel_stride * curtw;
dptr += (uint64_t) curtw *
(uint64_t) outc.user_bytes_per_element *
(uint64_t) curth;
}
}

rv = exr_decoding_run (f, part, &decoder);
if (rv != EXR_ERR_SUCCESS)
{
frv = rv;
if (reduceTime)
{
keepgoing = false;
Expand Down Expand Up @@ -1486,17 +1598,14 @@ checkCoreFile (exr_context_t f, bool reduceMemory, bool reduceTime)
rv = exr_get_storage (f, p, &store);
if (rv != EXR_ERR_SUCCESS) return true;

// TODO: Need to fill this in
if (store == EXR_STORAGE_DEEP_SCANLINE ||
store == EXR_STORAGE_DEEP_TILED)
continue;

if (store == EXR_STORAGE_SCANLINE)
if (store == EXR_STORAGE_SCANLINE ||
store == EXR_STORAGE_DEEP_SCANLINE)
{
if (readCoreScanlinePart (f, p, reduceMemory, reduceTime))
return true;
}
else if (store == EXR_STORAGE_TILED)
else if (store == EXR_STORAGE_TILED ||
store == EXR_STORAGE_DEEP_TILED)
{
if (readCoreTiledPart (f, p, reduceMemory, reduceTime)) return true;
}
Expand Down

0 comments on commit e892a23

Please sign in to comment.