Skip to content

Commit

Permalink
Issues/0109 valgrind issues in ctlrender with 16bit exr output (ampas…
Browse files Browse the repository at this point in the history
…#116)

* add RGBA EXR tests + valgrind tests for cltrender

* rewrite exr_write16() code modeled after exr_write32() with additional change of Imf::PixelType pixelType = Imf::HALF and conversion from float to half

* fixes issue ampas#109
  • Loading branch information
michaeldsmith authored Jan 11, 2023
1 parent b23d48e commit 2483047
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
unittest/ctlrender/output
.DS_Store
.vscode
Testing
94 changes: 72 additions & 22 deletions ctlrender/exr_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,80 @@ void exr_write32(const char *name, float scale, const ctl::dpx::fb<float> &pixel
file.writePixels (height);
}

void exr_write16(const char *name, float scale, const ctl::dpx::fb<float> &pixels, Compression *compression) {
if (scale == 0.0) scale = 1.0;
void exr_write16(const char* name, float scale, const ctl::dpx::fb<float>& pixels, Compression* compression)
{
int depth = pixels.depth();
float width = pixels.width();
float height = pixels.height();

ctl::dpx::fb<half> scaled_pixels;
scaled_pixels.init(pixels.height(), pixels.width(), pixels.depth());
scaled_pixels.alpha(1.0);

const float *fIn=pixels.ptr();
half *out=scaled_pixels.ptr();

// Yes... I should lookup table this. I *know*!
for(uint64_t i=0; i<pixels.count(); i++) {
*(out++)=*(fIn++)/scale;
ctl::dpx::fb<half> scaled_pixels;
scaled_pixels.init(pixels.height(), pixels.width(), pixels.depth());
scaled_pixels.alpha(1.0);

const float* fIn = pixels.ptr();
half* out = scaled_pixels.ptr();

if (scale != 0.0 && scale != 1.0) {
for (uint64_t i = 0; i < pixels.count(); i++) {
*(out++) = half(*(fIn++) / scale);
}
}
else
{
for (uint64_t i = 0; i < pixels.count(); i++) {
*(out++) = half(*(fIn++));
}

uint8_t channels=scaled_pixels.depth();
const half *in=scaled_pixels.ptr();

Imf::RgbaOutputFile file(name, pixels.width(), pixels.height(),
channels==4 ? Imf::WRITE_RGBA : Imf::WRITE_RGB, 1, Imath::V2f (0, 0), 1,
Imf::INCREASING_Y, (Imf::Compression)compression->exrCompressionScheme);

file.setFrameBuffer((Imf::Rgba *)in, 1, pixels.width());
file.writePixels(pixels.height());
}

half const* pixelPtr = scaled_pixels.ptr();

depth = scaled_pixels.depth();
width = scaled_pixels.width();
height = scaled_pixels.height();
pixelPtr = scaled_pixels.ptr();

Imf::PixelType pixelType = Imf::HALF;

Imf::Header header(width, height);
header.compression() = (Imf::Compression)compression->exrCompressionScheme;

header.channels().insert("R", Imf::Channel(pixelType));
header.channels().insert("G", Imf::Channel(pixelType));
header.channels().insert("B", Imf::Channel(pixelType));

if (depth == 4)
header.channels().insert("A", Imf::Channel(pixelType));

Imf::OutputFile file(name, header);

Imf::FrameBuffer frameBuffer;

int xstride = sizeof(*pixelPtr) * depth;
int ystride = sizeof(*pixelPtr) * depth * width;

frameBuffer.insert("R",
Imf::Slice(pixelType,
(char*)pixelPtr,
xstride, ystride));

frameBuffer.insert("G",
Imf::Slice(pixelType,
(char*)(pixelPtr + 1),
xstride, ystride));

frameBuffer.insert("B",
Imf::Slice(pixelType,
(char*)(pixelPtr + 2),
xstride, ystride));

if (depth == 4)
frameBuffer.insert("A",
Imf::Slice(pixelType,
(char*)(pixelPtr + 3),
xstride, ystride));

file.setFrameBuffer(frameBuffer);
file.writePixels(height);
}

void exr_write(const char *name, float scale, const ctl::dpx::fb<float> &pixels,
Expand Down
74 changes: 73 additions & 1 deletion resources/test/scripts/run_valgrind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,43 @@ valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak
test_10_status=$?
test_10_label="ctlrender-exr-to-exr32"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr16 -ctl ../../../unittest/ctlrender/unity.ctl ../../../unittest/ctlrender/colorbars_nuke_rgb_exr16.exr out.exr
test_11_status=$?
test_11_label="ctlrender-rgb-exr16-to-exr16"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr32 -ctl ../../../unittest/ctlrender/unity.ctl ../../../unittest/ctlrender/colorbars_nuke_rgb_exr16.exr out.exr
test_12_status=$?
test_12_label="ctlrender-rgb-exr16-to-exr32"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr16 -ctl ../../../unittest/ctlrender/unity.ctl ../../../unittest/ctlrender/colorbars_nuke_rgb_exr32.exr out.exr
test_13_status=$?
test_13_label="ctlrender-rgb-exr32-to-exr16"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr32 -ctl ../../../unittest/ctlrender/unity.ctl ../../../unittest/ctlrender/colorbars_nuke_rgb_exr32.exr out.exr
test_14_status=$?
test_14_label="ctlrender-rgb-exr32-to-exr32"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr16 -ctl ../../../unittest/ctlrender/unity_with_alpha.ctl ../../../unittest/ctlrender/colorbars_nuke_rgba_exr16.exr out.exr
test_15_status=$?
test_15_label="ctlrender-rgba-exr16-to-exr16"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr32 -ctl ../../../unittest/ctlrender/unity_with_alpha.ctl ../../../unittest/ctlrender/colorbars_nuke_rgba_exr16.exr out.exr
test_16_status=$?
test_16_label="ctlrender-rgba-exr16-to-exr32"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr16 -ctl ../../../unittest/ctlrender/unity_with_alpha.ctl ../../../unittest/ctlrender/colorbars_nuke_rgba_exr32.exr out.exr
test_17_status=$?
test_17_label="ctlrender-rgba-exr32-to-exr16"

valgrind -s --error-exitcode=1 --leak-check=full --track-origins=yes --show-leak-kinds=all ../../ctlrender/ctlrender -force -format exr32 -ctl ../../../unittest/ctlrender/unity_with_alpha.ctl ../../../unittest/ctlrender/colorbars_nuke_rgba_exr32.exr out.exr
test_18_status=$?
test_18_label="ctlrender-rgba-exr32-to-exr32"

# go back to initial path
cd $SCRIPTPATH

# return valgrind exit codes
if [ $test_01_status -eq 0 ] && [ $test_02_status -eq 0 ] && [ $test_03_status -eq 0 ] && [ $test_04_status -eq 0 ] && [ $test_05_status -eq 0 ] && [ $test_06_status -eq 0 ] && [ $test_07_status -eq 0 ] && [ $test_08_status -eq 0 ] && [ $test_09_status -eq 0 ] && [ $test_10_status -eq 0 ]
if [ $test_01_status -eq 0 ] && [ $test_02_status -eq 0 ] && [ $test_03_status -eq 0 ] && [ $test_04_status -eq 0 ] && [ $test_05_status -eq 0 ] && [ $test_06_status -eq 0 ] && [ $test_07_status -eq 0 ] && [ $test_08_status -eq 0 ] && [ $test_09_status -eq 0 ] && [ $test_10_status -eq 0 ] && [ $test_11_status -eq 0 ] && [ $test_12_status -eq 0 ] && [ $test_13_status -eq 0 ] && [ $test_14_status -eq 0 ] && [ $test_15_status -eq 0 ] && [ $test_16_status -eq 0 ] && [ $test_17_status -eq 0 ] && [ $test_18_status -eq 0 ]
then
echo "Success: valgrind detected no errors"
exit 0
Expand Down Expand Up @@ -111,5 +143,45 @@ else
echo "$test_10_label: valgrind detected errors"
fi

if [ $test_11_status -ne 0 ]
then
echo "$test_11_label: valgrind detected errors"
fi

if [ $test_12_status -ne 0 ]
then
echo "$test_12_label: valgrind detected errors"
fi

if [ $test_13_status -ne 0 ]
then
echo "$test_13_label: valgrind detected errors"
fi

if [ $test_14_status -ne 0 ]
then
echo "$test_14_label: valgrind detected errors"
fi

if [ $test_15_status -ne 0 ]
then
echo "$test_15_label: valgrind detected errors"
fi

if [ $test_16_status -ne 0 ]
then
echo "$test_16_label: valgrind detected errors"
fi

if [ $test_17_status -ne 0 ]
then
echo "$test_17_label: valgrind detected errors"
fi

if [ $test_18_status -ne 0 ]
then
echo "$test_18_label: valgrind detected errors"
fi

exit 1
fi
Binary file added unittest/ctlrender/colorbars_nuke_rgb_exr16.exr
Binary file not shown.
Binary file added unittest/ctlrender/colorbars_nuke_rgb_exr32.exr
Binary file not shown.
Binary file not shown.
Binary file added unittest/ctlrender/colorbars_nuke_rgba_exr32.exr
Binary file not shown.
21 changes: 21 additions & 0 deletions unittest/ctlrender/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ if [[ $IS_OPENEXR_FOUND == 1 ]] ; then
$CTLRENDER -ctl unity.ctl -format exr16 -force bars_photoshop.exr output/bars_exr_exr16.exr
printf 'bars_photoshop.exr -> output/bars_exr_exr32.exr \n'
$CTLRENDER -ctl unity.ctl -format exr32 -force bars_photoshop.exr output/bars_exr_exr32.exr

# RGB tests
printf 'colorbars_nuke_rgb_exr16.exr -> output/bars_rgb_exr16_to_exr16.exr \n'
$CTLRENDER -force -format exr16 -ctl unity.ctl colorbars_nuke_rgb_exr16.exr output/bars_rgb_exr16_to_exr16.exr
printf 'colorbars_nuke_rgb_exr16.exr -> output/bars_rgb_exr16_to_exr32.exr \n'
$CTLRENDER -force -format exr32 -ctl unity.ctl colorbars_nuke_rgb_exr16.exr output/bars_rgb_exr16_to_exr32.exr
printf 'colorbars_nuke_rgb_exr32.exr -> output/bars_rgb_exr32_to_exr16.exr \n'
$CTLRENDER -force -format exr16 -ctl unity.ctl colorbars_nuke_rgb_exr32.exr output/bars_rgb_exr32_to_exr16.exr
printf 'colorbars_nuke_rgb_exr32.exr -> output/bars_rgb_exr32_to_exr32.exr \n'
$CTLRENDER -force -format exr32 -ctl unity.ctl colorbars_nuke_rgb_exr32.exr output/bars_rgb_exr32_to_exr32.exr

# RGBA tests
printf 'colorbars_nuke_rgba_exr16.exr -> output/bars_rgba_exr16_to_exr16.exr \n'
$CTLRENDER -force -format exr16 -ctl unity_with_alpha.ctl colorbars_nuke_rgba_exr16.exr output/bars_rgba_exr16_to_exr16.exr
printf 'colorbars_nuke_rgba_exr16.exr -> output/bars_rgba_exr16_to_exr32.exr \n'
$CTLRENDER -force -format exr32 -ctl unity_with_alpha.ctl colorbars_nuke_rgba_exr16.exr output/bars_rgba_exr16_to_exr32.exr
printf 'colorbars_nuke_rgba_exr32.exr -> output/bars_rgba_exr32_to_exr16.exr \n'
$CTLRENDER -force -format exr16 -ctl unity_with_alpha.ctl colorbars_nuke_rgba_exr32.exr output/bars_rgba_exr32_to_exr16.exr
printf 'colorbars_nuke_rgba_exr32.exr -> output/bars_rgba_exr32_to_exr32.exr \n'
$CTLRENDER -force -format exr32 -ctl unity_with_alpha.ctl colorbars_nuke_rgba_exr32.exr output/bars_rgba_exr32_to_exr32.exr

fi

# test TIFF32 to EXR and EXR to TIFF32 support
Expand Down
15 changes: 15 additions & 0 deletions unittest/ctlrender/unity_with_alpha.ctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void unity_with_alpha
(output varying half rOut,
output varying half gOut,
output varying half bOut,
output varying half aOut,
input varying half rIn,
input varying half gIn,
input varying half bIn,
input varying half aIn)
{
rOut=rIn;
gOut=gIn;
bOut=bIn;
aOut=aIn;
}

0 comments on commit 2483047

Please sign in to comment.