Skip to content

Commit d82a76d

Browse files
committed
handle all channel formats for 8, 16 and 32 bps output
1 parent b30eb79 commit d82a76d

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

plotoptix/_load_lib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _load_optix_win():
6666
optix.save_image_to_file_32bps.argtypes = [c_wchar_p]
6767
optix.save_image_to_file_32bps.restype = c_bool
6868

69-
optix.get_output.argtypes = [c_void_p, c_int, c_int]
69+
optix.get_output.argtypes = [c_void_p, c_int, c_int, c_int]
7070
optix.get_output.restype = c_bool
7171

7272
optix.get_fps.restype = c_float
@@ -565,8 +565,8 @@ def save_image_to_file_16bps(self, fname): return self._optix.save_image_to_file
565565

566566
def save_image_to_file_32bps(self, fname): return self._optix.save_image_to_file_32bps(fname)
567567

568-
def get_output(self, buf_ptr, buf_size, depth):
569-
return self._optix.get_output_ptr(IntPtr.__overloads__[Int64](buf_ptr), buf_size, depth)
568+
def get_output(self, buf_ptr, buf_size, depth, channels):
569+
return self._optix.get_output_ptr(IntPtr.__overloads__[Int64](buf_ptr), buf_size, depth, channels)
570570

571571

572572
def get_fps(self): return self._optix.get_fps()

plotoptix/bin/RnD.SharpOptiX.dll

1 KB
Binary file not shown.

plotoptix/npoptix.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,55 +329,73 @@ def close(self) -> None:
329329
def is_started(self) -> bool: return self._is_started
330330
def is_closed(self) -> bool: return self._is_closed
331331

332-
def get_rt_output(self, bps: Union[ChannelDepth, str] = ChannelDepth.Bps8) -> np.ndarray:
332+
def get_rt_output(self,
333+
bps: Union[ChannelDepth, str] = ChannelDepth.Bps8,
334+
channels: Union[ChannelOrder, str] = ChannelOrder.RGBA) -> Optional[np.ndarray]:
333335
"""Return a copy of the output image.
334336
335337
The image data type is specified with the ``bps`` argument. 8 bit per channel data,
336338
``numpy.uint8``, is returned by default. Use ``Bps16`` value to read the image in
337339
16 bit per channel depth, ``numpy.uint16``. Use ``Bps32`` value to read the HDR image
338340
in 32 bit per channel format, ``numpy.float32``.
339341
340-
Channels ordering is RGBA, with constant values in the alpha channel (100% opaque,
341-
to be used in the future releases).
342+
If channels ordering includes alpha channel then it is a constant, 100% opaque value,
343+
to be used in the future releases.
342344
343345
Safe to call at any time, from any thread.
344346
345347
Parameters
346348
----------
347349
bps : ChannelDepth enum or string, optional
348350
Color depth.
351+
channels : ChannelOrder enum or string, optional
352+
Color channels ordering.
349353
350354
Returns
351355
-------
352356
out : ndarray
353-
RGBA array of shape (height, width, 4) and type corresponding to ``bps`` argument.
357+
RGB(A) array of shape (height, width, 3) or (height, width, 4) and type corresponding
358+
to ``bps`` argument. ``None`` in case of errors.
354359
355360
See Also
356361
--------
357-
:class:`plotoptix.enums.ChannelDepth`
362+
:class:`plotoptix.enums.ChannelDepth`, :class:`plotoptix.enums.ChannelOrder`
358363
"""
359364
assert self._is_started, "Raytracing output not running."
360365

361366
if isinstance(bps, str): bps = ChannelDepth[bps]
367+
if isinstance(channels, str): channels = ChannelOrder[channels]
362368

363369
a = None
364370

365371
try:
366372
self._padlock.acquire()
367373

368-
ok = True
374+
if bps == ChannelDepth.Bps8 and channels == ChannelOrder.RGBA:
375+
return self._img_rgba.copy()
369376

370377
if bps == ChannelDepth.Bps8:
371-
a = self._img_rgba.copy()
378+
if channels == ChannelOrder.BGRA:
379+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 4), dtype=np.uint8))
380+
elif channels == ChannelOrder.RGB or channels == ChannelOrder.BGR:
381+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 3), dtype=np.uint8))
382+
372383
elif bps == ChannelDepth.Bps16:
373-
a = np.ascontiguousarray(np.zeros((self._height, self._width, 4), dtype=np.uint16))
374-
ok = self._optix.get_output(a.ctypes.data, a.nbytes, bps.value)
384+
if channels == ChannelOrder.RGBA or channels == ChannelOrder.BGRA:
385+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 4), dtype=np.uint16))
386+
elif channels == ChannelOrder.RGB or channels == ChannelOrder.BGR:
387+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 3), dtype=np.uint16))
388+
375389
elif bps == ChannelDepth.Bps32:
376-
a = np.ascontiguousarray(np.zeros((self._height, self._width, 4), dtype=np.float32))
377-
ok = self._optix.get_output(a.ctypes.data, a.nbytes, bps.value)
390+
if channels == ChannelOrder.RGBA or channels == ChannelOrder.BGRA:
391+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 4), dtype=np.float32))
392+
elif channels == ChannelOrder.RGB or channels == ChannelOrder.BGR:
393+
a = np.ascontiguousarray(np.zeros((self._height, self._width, 3), dtype=np.float32))
378394

379-
if not ok:
380-
msg = "Image not saveed."
395+
else: return a
396+
397+
if not self._optix.get_output(a.ctypes.data, a.nbytes, bps.value, channels.value):
398+
msg = "Image not copied."
381399
self._logger.error(msg)
382400
if self._raise_on_error: raise ValueError(msg)
383401

@@ -1740,7 +1758,7 @@ def save_image(self, file_name: str,
17401758
ok = False
17411759

17421760
if not ok:
1743-
msg = "Image not saveed."
1761+
msg = "Image not saved."
17441762
self._logger.error(msg)
17451763
if self._raise_on_error: raise ValueError(msg)
17461764

0 commit comments

Comments
 (0)