Skip to content

Commit

Permalink
add conversion of nan to zero for lossy compression
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldsmith committed Jan 19, 2025
1 parent 6d083a2 commit 796173e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/apps/common/ojph_img_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ namespace ojph {
rgba_output_file = NULL;
is_use_Rgba_interface = false;
codestream_is_planar = false;
codestream_is_reversible = false;

this->data_window.makeEmpty();

Expand All @@ -933,7 +934,9 @@ namespace ojph {
}

void open(const char* filename);
void configure(ui32 width, ui32 height, ui32 num_components, bool* has_nlt, ui32* bitdepths, bool* is_signed, bool codestream_is_planar);
void configure(ui32 width, ui32 height, ui32 num_components,
bool* has_nlt, ui32* bitdepths, bool* is_signed,
bool codestream_is_planar, bool codestream_is_reversible);
virtual ui32 write(const line_buf* line, ui32 comp_num);
virtual void close();

Expand All @@ -948,6 +951,7 @@ namespace ojph {
ui32 bit_depth[MAXIMUM_NUMBER_OF_COMPONENTS_EXR_OUT];
bool is_signed[MAXIMUM_NUMBER_OF_COMPONENTS_EXR_OUT];
bool codestream_is_planar;
bool codestream_is_reversible;

Imf::RgbaOutputFile* rgba_output_file;
Imf::Array2D<Imf::Rgba> pixels;
Expand Down
3 changes: 2 additions & 1 deletion src/apps/ojph_expand/ojph_expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ int main(int argc, char *argv[]) {
{
ojph::param_siz siz = codestream.access_siz();
ojph::param_nlt nlt = codestream.access_nlt();
ojph::param_cod cod = codestream.access_cod();
ojph::ui32 num_components = siz.get_num_components();

ojph::ui32 *bitdepths = (ojph::ui32*)calloc(num_components, sizeof(ojph::ui32));
Expand Down Expand Up @@ -462,7 +463,7 @@ int main(int argc, char *argv[]) {
ojph::ui32 width = siz.get_recon_width(0);
ojph::ui32 height = siz.get_recon_height(0);

exr.configure(width, height, num_components, has_nlt, bitdepths, is_signed, codestream.is_planar());
exr.configure(width, height, num_components, has_nlt, bitdepths, is_signed, codestream.is_planar(), cod.is_reversible());
exr.open(output_filename);
base = &exr;

Expand Down
38 changes: 20 additions & 18 deletions src/apps/others/ojph_img_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,9 @@ namespace ojph {
return;
}

void exr_out::configure(ui32 width, ui32 height, ui32 num_components, bool *has_nlt, ui32 *bitdepths, bool* is_signed, bool codestream_is_planar)
void exr_out::configure(ui32 width, ui32 height, ui32 num_components,
bool *has_nlt, ui32 *bitdepths, bool* is_signed,
bool codestream_is_planar, bool codestream_is_reversible)
{
this->num_components = num_components;
this->width = width;
Expand Down Expand Up @@ -2427,23 +2429,35 @@ namespace ojph {
}

this->codestream_is_planar = codestream_is_planar;
this->codestream_is_reversible = codestream_is_reversible;

return;
}

si16 convert_si32_to_si16(const si32 si32_value)
si16 convert_si32_to_si16(const si32 si32_value, bool convert_nan_to_zero = false)
{
if (si32_value > INT16_MAX)
return INT16_MAX;
else if (si32_value < INT16_MIN)
return INT16_MIN;
else if (true == convert_nan_to_zero)
{
const si16 si16_value = (si16)si32_value;
Imath::half half_value;
half_value.setBits(si16_value);
if (half_value.isNan())
half_value = 0.0;

return half_value.bits();
}
else
return (si16)si32_value;
}

ui32 exr_out::write(const line_buf* line, ui32 comp_num)
{
const int y = codestream_is_planar == false ? 0 : cur_line;
const bool convert_nan_to_zero = codestream_is_reversible ? false : true;
if (true == this->is_use_Rgba_interface)
{
// if interleaved - set framebuffer for first component of the line
Expand All @@ -2457,37 +2471,25 @@ namespace ojph {
case 0:
for (ui32 i = 0; i < width; i++)
{
//pixels[y][i].r.setBits((si16)line->i32[i]);
const si32 si32_value = line->i32[i];
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
pixels[y][i].r.setBits(si16_value);
pixels[y][i].r.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
}
break;
case 1:
for (ui32 i = 0; i < width; i++)
{
//pixels[y][i].g.setBits((si16)line->i32[i]);
const si32 si32_value = line->i32[i];
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
pixels[y][i].g.setBits(si16_value);
pixels[y][i].g.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
}
break;
case 2:
for (ui32 i = 0; i < width; i++)
{
//pixels[y][i].b.setBits((si16)line->i32[i]);
const si32 si32_value = line->i32[i];
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
pixels[y][i].b.setBits(si16_value);
pixels[y][i].b.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
}
break;
case 3:
for (ui32 i = 0; i < width; i++)
{
//pixels[y][i].a.setBits((si16)line->i32[i]);
const si32 si32_value = line->i32[i];
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
pixels[y][i].a.setBits(si16_value);
pixels[y][i].a.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
}
break;
default:
Expand Down

0 comments on commit 796173e

Please sign in to comment.