changeset 16707:4c11e9bcb796

ind2rgb: add support for multidimensional indexed images.
author Carnë Draug <carandraug@octave.org>
date Fri, 31 May 2013 13:27:04 +0100
parents 736dca8371ee
children 08f0f274de36
files scripts/image/ind2rgb.m scripts/image/private/ind2x.m
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/ind2rgb.m	Thu May 30 14:19:59 2013 -0400
+++ b/scripts/image/ind2rgb.m	Fri May 31 13:27:04 2013 +0100
@@ -31,6 +31,8 @@
 ## channels).  Alternatively, the individual red, green, and blue color matrices
 ## of size MxN may be returned.
 ##
+## Multidimensional indexed images (of size MxNx1xK) are also supported.
+##
 ## @seealso{rgb2ind, ind2gray, hsv2rgb, ntsc2rgb}
 ## @end deftypefn
 
@@ -53,7 +55,14 @@
 
   ## Use ND array if only one output is requested.
   if (nargout <= 1)
-    R = reshape ([R(:); G(:); B(:)], [sz, 3]);
+    if (ndims (x) == 2)
+      R = reshape ([R(:); G(:); B(:)], [sz, 3]);
+    elseif (ndims (x) == 4)
+      R = permute (reshape ([R(:); G(:); B(:)], [sz(1) sz(2) sz(4) 3]), [1 2 4 3]);
+    else
+      ## we should never reach here since ind2x() should filter them out
+      error ("ind2rgb: an indexed image must have 2 or 4 dimensions.");
+    endif
   endif
 
 endfunction
--- a/scripts/image/private/ind2x.m	Thu May 30 14:19:59 2013 -0400
+++ b/scripts/image/private/ind2x.m	Fri May 31 13:27:04 2013 +0100
@@ -22,7 +22,11 @@
 function [x, map] = ind2x (caller, x, map)
 
   ## Check if X is an indexed image.
-  if (ndims (x) < 2 || issparse (x) || (isfloat (x) && ! isindex (x)) ||
+  ## an indexed image is defined has having only 2D, and that's how matlab
+  ## behaves. But we want to support ND images, so we will allow up to 4D
+  ## and check that the 3rd is a singleton
+  if (all (ndims (x) != [2 4]) || size (x, 3) != 1 || issparse (x) ||
+      (isfloat (x) && ! isindex (x)) ||
       ! any (strcmp (class (x), {"uint8", "uint16", "single", "double"})))
     error ("%s: X must be an indexed image", caller);
   endif