Skip to content

Commit 796173e

Browse files
committed
add conversion of nan to zero for lossy compression
1 parent 6d083a2 commit 796173e

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/apps/common/ojph_img_io.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ namespace ojph {
922922
rgba_output_file = NULL;
923923
is_use_Rgba_interface = false;
924924
codestream_is_planar = false;
925+
codestream_is_reversible = false;
925926

926927
this->data_window.makeEmpty();
927928

@@ -933,7 +934,9 @@ namespace ojph {
933934
}
934935

935936
void open(const char* filename);
936-
void configure(ui32 width, ui32 height, ui32 num_components, bool* has_nlt, ui32* bitdepths, bool* is_signed, bool codestream_is_planar);
937+
void configure(ui32 width, ui32 height, ui32 num_components,
938+
bool* has_nlt, ui32* bitdepths, bool* is_signed,
939+
bool codestream_is_planar, bool codestream_is_reversible);
937940
virtual ui32 write(const line_buf* line, ui32 comp_num);
938941
virtual void close();
939942

@@ -948,6 +951,7 @@ namespace ojph {
948951
ui32 bit_depth[MAXIMUM_NUMBER_OF_COMPONENTS_EXR_OUT];
949952
bool is_signed[MAXIMUM_NUMBER_OF_COMPONENTS_EXR_OUT];
950953
bool codestream_is_planar;
954+
bool codestream_is_reversible;
951955

952956
Imf::RgbaOutputFile* rgba_output_file;
953957
Imf::Array2D<Imf::Rgba> pixels;

src/apps/ojph_expand/ojph_expand.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ int main(int argc, char *argv[]) {
400400
{
401401
ojph::param_siz siz = codestream.access_siz();
402402
ojph::param_nlt nlt = codestream.access_nlt();
403+
ojph::param_cod cod = codestream.access_cod();
403404
ojph::ui32 num_components = siz.get_num_components();
404405

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

465-
exr.configure(width, height, num_components, has_nlt, bitdepths, is_signed, codestream.is_planar());
466+
exr.configure(width, height, num_components, has_nlt, bitdepths, is_signed, codestream.is_planar(), cod.is_reversible());
466467
exr.open(output_filename);
467468
base = &exr;
468469

src/apps/others/ojph_img_io.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,9 @@ namespace ojph {
23912391
return;
23922392
}
23932393

2394-
void exr_out::configure(ui32 width, ui32 height, ui32 num_components, bool *has_nlt, ui32 *bitdepths, bool* is_signed, bool codestream_is_planar)
2394+
void exr_out::configure(ui32 width, ui32 height, ui32 num_components,
2395+
bool *has_nlt, ui32 *bitdepths, bool* is_signed,
2396+
bool codestream_is_planar, bool codestream_is_reversible)
23952397
{
23962398
this->num_components = num_components;
23972399
this->width = width;
@@ -2427,23 +2429,35 @@ namespace ojph {
24272429
}
24282430

24292431
this->codestream_is_planar = codestream_is_planar;
2432+
this->codestream_is_reversible = codestream_is_reversible;
24302433

24312434
return;
24322435
}
24332436

2434-
si16 convert_si32_to_si16(const si32 si32_value)
2437+
si16 convert_si32_to_si16(const si32 si32_value, bool convert_nan_to_zero = false)
24352438
{
24362439
if (si32_value > INT16_MAX)
24372440
return INT16_MAX;
24382441
else if (si32_value < INT16_MIN)
24392442
return INT16_MIN;
2443+
else if (true == convert_nan_to_zero)
2444+
{
2445+
const si16 si16_value = (si16)si32_value;
2446+
Imath::half half_value;
2447+
half_value.setBits(si16_value);
2448+
if (half_value.isNan())
2449+
half_value = 0.0;
2450+
2451+
return half_value.bits();
2452+
}
24402453
else
24412454
return (si16)si32_value;
24422455
}
24432456

24442457
ui32 exr_out::write(const line_buf* line, ui32 comp_num)
24452458
{
24462459
const int y = codestream_is_planar == false ? 0 : cur_line;
2460+
const bool convert_nan_to_zero = codestream_is_reversible ? false : true;
24472461
if (true == this->is_use_Rgba_interface)
24482462
{
24492463
// if interleaved - set framebuffer for first component of the line
@@ -2457,37 +2471,25 @@ namespace ojph {
24572471
case 0:
24582472
for (ui32 i = 0; i < width; i++)
24592473
{
2460-
//pixels[y][i].r.setBits((si16)line->i32[i]);
2461-
const si32 si32_value = line->i32[i];
2462-
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
2463-
pixels[y][i].r.setBits(si16_value);
2474+
pixels[y][i].r.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
24642475
}
24652476
break;
24662477
case 1:
24672478
for (ui32 i = 0; i < width; i++)
24682479
{
2469-
//pixels[y][i].g.setBits((si16)line->i32[i]);
2470-
const si32 si32_value = line->i32[i];
2471-
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
2472-
pixels[y][i].g.setBits(si16_value);
2480+
pixels[y][i].g.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
24732481
}
24742482
break;
24752483
case 2:
24762484
for (ui32 i = 0; i < width; i++)
24772485
{
2478-
//pixels[y][i].b.setBits((si16)line->i32[i]);
2479-
const si32 si32_value = line->i32[i];
2480-
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
2481-
pixels[y][i].b.setBits(si16_value);
2486+
pixels[y][i].b.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
24822487
}
24832488
break;
24842489
case 3:
24852490
for (ui32 i = 0; i < width; i++)
24862491
{
2487-
//pixels[y][i].a.setBits((si16)line->i32[i]);
2488-
const si32 si32_value = line->i32[i];
2489-
const si16 si16_value = si32_value > INT16_MAX ? INT16_MAX : (si32_value < INT16_MIN ? INT16_MIN : (si16)si32_value);
2490-
pixels[y][i].a.setBits(si16_value);
2492+
pixels[y][i].a.setBits(convert_si32_to_si16(line->i32[i], convert_nan_to_zero));
24912493
}
24922494
break;
24932495
default:

0 commit comments

Comments
 (0)