diff scripts/image/ind2rgb.m @ 15683:806ea52af230

Overhaul m-files in image directory to provide better support for images stored as integers. * NEWS: Add note about overhaul of image scripts to support integer classes. * brighten.m: Add demo. * colormap.m: Better input validation. * contrast.m: Re-position window in demo. * gray2ind.m: Redo docstring. Match variables in docstring to function prototype. Better input validation. Return integer class outputs as Matlab does. Add %!tests. * hsv2rgb.m: Add to docstring. Better input validation. Redo algorithm to get rido of obsolete matrix-fill methods like kron(). Add %!tests. * image.m: Redo docstring. Match variables in docstring to function prototype. Better input validation without using for loops. * imagesc.m: Redo docstring. Match variables in docstring to function prototype. Remove DEPRECATEDZOOM functionality. Add demos. * imfinfo.m: Redo docstring. * ind2gray.m: Redo docstring. Match variables in docstring to function prototype. Redo algorithm to directly calculate luminance value rather than getting it from rgb2ntsc. Add %!tests. * ind2rgb.m: Redo docstring. Better input validation. Add some %!tests. * ntsc2rgb.m: Redo docstring. Better input validation. Add %!tests. * rgb2hsv.m: Better input validation. Add %!tests. * rgb2ind.m: Better input validation. Code algorithm in cleaner method for ease of understanding. * rgb2ntsc.m: Redo docstring: Better input validation. Add some %!tests.
author Rik <rik@octave.org>
date Tue, 27 Nov 2012 16:38:13 -0800
parents 4beb3a4bd440
children a1b634240352
line wrap: on
line diff
--- a/scripts/image/ind2rgb.m	Mon Nov 26 12:09:09 2012 -0500
+++ b/scripts/image/ind2rgb.m	Tue Nov 27 16:38:13 2012 -0800
@@ -21,9 +21,14 @@
 ## @deftypefnx {Function File} {@var{rgb} =} ind2rgb (@var{x}, @var{map})
 ## @deftypefnx {Function File} {[@var{R}, @var{G}, @var{B}] =} ind2rgb (@dots{})
 ## Convert an indexed image to red, green, and blue color components.
-## If the colormap doesn't contain enough colors, pad it with the
-## last color in the map.
 ## If @var{map} is omitted, the current colormap is used for the conversion.
+## When the colormap does not contain enough colors it is padded to the
+## required length using the last color in the map.
+##
+## The output may be a single MxNx3 matrix where M is the number of rows in
+## @var{x} and N is the number of columns in @var{x}.  Alternatively,
+## individual red, green, and blue color matrices of size MxN may be
+## returned.
 ## @seealso{rgb2ind, ind2gray, hsv2rgb, ntsc2rgb}
 ## @end deftypefn
 
@@ -33,20 +38,22 @@
 
 function [R, G, B] = ind2rgb (x, map)
 
-  ## Do we have the right number of inputs?
   if (nargin < 1 || nargin > 2)
     print_usage ();
-  elseif (nargin == 1)
-    map = colormap ();
   endif
 
-  ## Check if X is an indexed image.
-  if (ndims (x) != 2 || any (x(:) != fix (x(:))) || min (x(:)) < 1)
+  if (! isreal (x) || issparse (x)
+      || (isfloat (x) && (any (x(:) < 1 || any (x(:) != fix (x(:)))))))
     error ("ind2rgb: X must be an indexed image");
   endif
+  cls = class (x);
+  if (! any (isa (x, {"logical", "uint8", "uint16", "single", "double"})))
+    error ("ind2rgb: invalid data type '%s'", cls);
+  endif
 
-  ## Check the color map.
-  if (! iscolormap (map))
+  if (nargin == 1)
+    map = colormap ();
+  elseif (! iscolormap (map))
     error ("ind2rgb: MAP must be a valid colormap");
   endif
 
@@ -60,14 +67,34 @@
   endif
 
   ## Compute result
-  [hi, wi] = size (x);
-  R = reshape (map (x(:), 1), hi, wi);
-  G = reshape (map (x(:), 2), hi, wi);
-  B = reshape (map (x(:), 3), hi, wi);
+  [row, col] = size (x);
+  R = reshape (map(x(:), 1), row, col);
+  G = reshape (map(x(:), 2), row, col);
+  B = reshape (map(x(:), 3), row, col);
 
   ## Use 3D array if only one output is requested.
   if (nargout <= 1)
+    R(:,:,2) = G;
     R(:,:,3) = B;
-    R(:,:,2) = G;
   endif
+
 endfunction
+
+
+%% FIXME: Need some functional tests or %!demo blocks
+
+%%test input validation
+%!error ind2rgb ()
+%!error ind2rgb (1,2,3)
+%!error <X must be an indexed image> ind2rgb ({1})
+%!error <X must be an indexed image> ind2rgb (1+i)
+%!error <X must be an indexed image> ind2rgb (sparse (1))
+%!error <X must be an indexed image> ind2rgb (0)
+%!error <X must be an indexed image> ind2rgb (1.1)
+%!error <MAP must be a valid colormap> ind2rgb (1, {1})
+%!error <MAP must be a valid colormap> ind2rgb (1, 1+i)
+%!error <MAP must be a valid colormap> ind2rgb (1, ones (2,2,2))
+%!error <MAP must be a valid colormap> ind2rgb (1, ones (2,4))
+%!error <MAP must be a valid colormap> ind2rgb (1, [-1])
+%!error <MAP must be a valid colormap> ind2rgb (1, [2])
+