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

Testing: eliminate redundant code in Python IBA bindings #3615

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
144 changes: 13 additions & 131 deletions src/python/py_imagebufalgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,7 @@ IBA_add_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::add(A, values, roi, nthreads);
IBA_add_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -617,17 +607,7 @@ IBA_sub_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::sub(A, values, roi, nthreads);
IBA_sub_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -667,21 +647,11 @@ IBA_absdiff_images(ImageBuf& dst, const ImageBuf& A, const ImageBuf& B,
}

ImageBuf
IBA_absdiff_color_ref(const ImageBuf& A, py::object values_tuple,
IBA_absdiff_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::absdiff(A, values, roi, nthreads);
IBA_absdiff_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -744,17 +714,7 @@ IBA_mul_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::mul(A, values, roi, nthreads);
IBA_mul_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -800,17 +760,7 @@ IBA_div_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::div(A, values, roi, nthreads);
IBA_div_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -888,24 +838,7 @@ IBA_mad_color_ret(const ImageBuf& A, py::object Bvalues_tuple,
int nthreads = 0)
{
ImageBuf result;
std::vector<float> Bvalues, Cvalues;
py_to_stdvector(Bvalues, Bvalues_tuple);
if (roi.defined())
Bvalues.resize(roi.nchannels(), Bvalues.size() ? Bvalues.back() : 0.0f);
else if (A.initialized())
Bvalues.resize(A.nchannels(), Bvalues.size() ? Bvalues.back() : 0.0f);
else
return result;
py_to_stdvector(Cvalues, Cvalues_tuple);
if (roi.defined())
Cvalues.resize(roi.nchannels(), Cvalues.size() ? Cvalues.back() : 0.0f);
else if (A.initialized())
Cvalues.resize(A.nchannels(), Cvalues.size() ? Cvalues.back() : 0.0f);
else
return result;
OIIO_ASSERT(Bvalues.size() > 0 && Cvalues.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::mad(A, Bvalues, Cvalues, roi, nthreads);
IBA_mad_color(result, A, Bvalues_tuple, Cvalues_tuple, roi, nthreads);
return result;
}

Expand All @@ -914,17 +847,7 @@ IBA_mad_ici_ret(const ImageBuf& A, py::object Bvalues_tuple, const ImageBuf& C,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> Bvalues, Cvalues;
py_to_stdvector(Bvalues, Bvalues_tuple);
if (roi.defined())
Bvalues.resize(roi.nchannels(), Bvalues.size() ? Bvalues.back() : 0.0f);
else if (A.initialized())
Bvalues.resize(A.nchannels(), Bvalues.size() ? Bvalues.back() : 0.0f);
else
return result;
OIIO_ASSERT(Bvalues.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::mad(A, Bvalues, C, roi, nthreads);
IBA_mad_ici(result, A, Bvalues_tuple, C, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -986,17 +909,7 @@ IBA_pow_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::pow(A, values, roi, nthreads);
IBA_pow_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -1032,17 +945,7 @@ IBA_min_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::min(A, values, roi, nthreads);
IBA_min_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -1087,17 +990,7 @@ IBA_max_color_ret(const ImageBuf& A, py::object values_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> values;
py_to_stdvector(values, values_tuple);
if (roi.defined())
values.resize(roi.nchannels(), values.size() ? values.back() : 0.0f);
else if (A.initialized())
values.resize(A.nchannels(), values.size() ? values.back() : 0.0f);
else
return result;
OIIO_ASSERT(values.size() > 0);
py::gil_scoped_release gil;
result = ImageBufAlgo::max(A, values, roi, nthreads);
IBA_max_color(result, A, values_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -1206,18 +1099,7 @@ IBA_channel_sum_weight_ret(const ImageBuf& src, py::object weight_tuple,
ROI roi = ROI::All(), int nthreads = 0)
{
ImageBuf result;
std::vector<float> weight;
py_to_stdvector(weight, weight_tuple);
if (!src.initialized()) {
result.errorfmt("Uninitialized source image for channel_sum");
return result;
}
if (weight.size() == 0)
weight.resize(src.nchannels(), 1.0f); // no weights -> uniform
else
weight.resize(src.nchannels(), 0.0f); // missing weights -> 0
py::gil_scoped_release gil;
result = ImageBufAlgo::channel_sum(src, weight, roi, nthreads);
IBA_channel_sum_weight(result, src, weight_tuple, roi, nthreads);
return result;
}

Expand Down Expand Up @@ -2657,7 +2539,7 @@ declare_imagebufalgo(py::module& m)
"roi"_a = ROI::All(), "nthreads"_a = 0)
.def_static("absdiff", &IBA_absdiff_images_ret, "A"_a, "B"_a,
"roi"_a = ROI::All(), "nthreads"_a = 0)
.def_static("absdiff", IBA_absdiff_color_ref, "A"_a, "B"_a,
.def_static("absdiff", IBA_absdiff_color_ret, "A"_a, "B"_a,
"roi"_a = ROI::All(), "nthreads"_a = 0)

.def_static("abs", &IBA_abs, "dst"_a, "A"_a, "roi"_a = ROI::All(),
Expand Down
Binary file added testsuite/python-imagebufalgo/ref/csub2.exr
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagebufalgo/ref/out-freetype2.4.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagebufalgo/ref/out-python2-alt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagebufalgo/ref/out-python3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagebufalgo/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Comparing "add.exr" and "../../../testsuite/oiiotool/ref/add.exr"
PASS
Comparing "sub.exr" and "../../../testsuite/oiiotool/ref/sub.exr"
PASS
Comparing "csub2.exr" and "ref/csub2.exr"
PASS
Comparing "abs.exr" and "../../../testsuite/oiiotool/ref/abs.exr"
PASS
Comparing "absdiff.exr" and "../../../testsuite/oiiotool/ref/absdiff.exr"
Expand Down
3 changes: 2 additions & 1 deletion testsuite/python-imagebufalgo/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"warped.tif",
"flip.tif", "flop.tif", "reorient1.tif",
"transpose.tif",
"cshift.tif", "cadd1.exr", "cadd2.exr", "add.exr", "sub.exr",
"cshift.tif", "cadd1.exr", "cadd2.exr", "add.exr",
"sub.exr", "csub2.exr",
"abs.exr", "absdiff.exr",
"mul.exr", "cmul1.exr", "cmul2.exr",
"mad.exr", "mad2.exr", "mad3.exr",
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagebufalgo/src/test_imagebufalgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def dumpimg (image, fmt="{:.3f}", msg="") :
b = ImageBufAlgo.sub (make_constimage(64,64,3,oiio.HALF,(.1,.2,.3)),
make_constimage(64,64,3,oiio.HALF,(.1,.1,.1),20,20))
write (b, "sub.exr")
b = ImageBufAlgo.sub (gray128, (0.125, 0.5, 0.25))
write (b, "csub2.exr")

# Test --absdiff and --abs
# First, make a test image that's 0.5 on the left, -0.5 on the right
Expand Down