Skip to content

Commit c13606f

Browse files
committed
Python: implement ROI.copy()
Somehow I never implemented a copy() method for ROI, which of course is super useful if you want to make an actual copy of an ROI and modify the copy. I also realized that ImageSpec.copy() unnecessarily/incorrectly set return_value_policy to reference_internal, but actually it should be a real copy, which it has to be anyway because the implementation creates a separate copy.
1 parent 7d8fd72 commit c13606f

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

src/doc/imagebufalgo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ zero() -- create a black image
213213
ImageBufAlgo.zero (A, ROI (0, 100, 0, 100))
214214

215215
# Zero out just the green channel, leave everything else the same
216-
roi = A.roi ()
216+
roi = A.roi
217217
roi.chbegin = 1 # green
218218
roi.chend = 2 # one past the end of the channel region
219219
ImageBufAlgo.zero (A, roi)

src/python/py_imagespec.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ declare_imagespec(py::module& m)
117117
.def(py::init<const ROI&, TypeDesc>())
118118
.def(py::init<TypeDesc>())
119119
.def(py::init<const ImageSpec&>())
120-
.def(
121-
"copy", [](const ImageSpec& self) { return ImageSpec(self); },
122-
py::return_value_policy::reference_internal)
120+
.def("copy", [](const ImageSpec& self) { return ImageSpec(self); })
123121
.def("set_format",
124122
[](ImageSpec& self, TypeDesc t) { self.set_format(t); })
125123
.def("default_channel_names", &ImageSpec::default_channel_names)

src/python/py_roi.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ declare_roi(py::module& m)
6464
.def("__str__",
6565
[](const ROI& roi) { return PY_STR(Strutil::sprintf("%s", roi)); })
6666

67+
// Copy
68+
.def("copy", [](const ROI& self) -> ROI { return self; })
69+
6770
// roi_union, roi_intersection, get_roi(spec), get_roi_full(spec)
6871
// set_roi(spec,newroi), set_roi_full(newroi)
6972

testsuite/python-roi/ref/out.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ ROI.intersection(A,B) = 5 10 0 8 0 1 0 4
3636
Spec's roi is 0 640 0 480 0 1 0 3
3737
After set, roi is 3 5 7 9 0 1 0 3
3838
After set, roi_full is 13 15 17 19 0 1 0 3
39+
r1 = 0 640 0 480 0 1 0 4
40+
r2 = 42 640 0 480 0 1 0 4
3941

4042
Done.

testsuite/python-roi/src/test_roi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
print ("After set, roi is", oiio.get_roi(spec))
6666
print ("After set, roi_full is", oiio.get_roi_full(spec))
6767

68+
r1 = oiio.ROI(0, 640, 0, 480, 0, 1, 0, 4)
69+
r2 = r1.copy()
70+
r2.xbegin = 42
71+
print ("r1 =", r1)
72+
print ("r2 =", r2)
73+
6874
print ("")
6975

7076
print ("Done.")

0 commit comments

Comments
 (0)