changeset 15689:14b7679891dd

ind2sub ind2gray: merge common code in private function, expanding fix for images when integers, input check, and expansion of colormap to ind2gray.
author Carnë Draug <carandraug+dev@gmail.com>
date Mon, 12 Nov 2012 04:53:42 +0000
parents 4db08f52a6ed
children 7d21456c09d1
files scripts/image/ind2gray.m scripts/image/ind2rgb.m scripts/image/private/ind2x.m
diffstat 3 files changed, 62 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/ind2gray.m	Mon Nov 12 04:21:52 2012 +0000
+++ b/scripts/image/ind2gray.m	Mon Nov 12 04:53:42 2012 +0000
@@ -20,6 +20,8 @@
 ## @deftypefn  {Function File} {} ind2gray (@var{x})
 ## @deftypefnx {Function File} {} ind2gray (@var{x}, @var{map})
 ## Convert a color indexed image to a gray scale intensity image.
+## 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 to determine the
 ## intensities.
 ## @seealso{gray2ind, ind2rgb}
@@ -31,20 +33,20 @@
 
 function y = ind2gray (x, map)
 
+  ## Do we have the right number of inputs?
   if (nargin < 1 || nargin > 2)
     print_usage ();
   elseif (nargin == 1)
     map = colormap ();
   endif
 
-  [rows, cols] = size (x);
+  [x, map] = ind2x ("ind2gray", x, map);
 
   ## Convert colormap to intensity values (the first column of the
   ## result of the call to rgb2ntsc) and then replace indices in
   ## the input matrix with indexed values in the output matrix (indexed
   ## values are the result of indexing the intensity values by the
   ## elements of x(:)).
-
-  y = reshape (((rgb2ntsc (map))(:,1))(x(:)), rows, cols);
+  y = reshape (((rgb2ntsc (map))(:,1))(x(:)), rows (x), columns (x));
 
 endfunction
--- a/scripts/image/ind2rgb.m	Mon Nov 12 04:21:52 2012 +0000
+++ b/scripts/image/ind2rgb.m	Mon Nov 12 04:53:42 2012 +0000
@@ -40,36 +40,7 @@
     map = colormap ();
   endif
 
-  ## Check if X is an indexed image.
-  if (ndims (x) != 2 || (isfloat (x) && ! isindex (x)) ||
-      ! ismember (class (x), {"double", "single", "uint8", "uint16"}))
-    error ("ind2rgb: X must be an indexed image");
-  endif
-
-  ## Check the color map.
-  if (! iscolormap (map))
-    error ("ind2rgb: MAP must be a valid colormap");
-  endif
-
-  ## Do we have enough colors in the color map?
-  ## there's an offset of 1 when the indexed image is an integer class so we fix
-  ## it now and convert it to float only if really necessary and even then only
-  ## to single precision since its enough for both uint8 and uint16
-  maxidx = max (x(:));
-  if (isinteger (x))
-    if (maxidx == intmax (class (x)))
-      x = single (x);
-    endif
-    x      += 1;
-    maxidx += 1;
-  endif
-
-  rm = rows (map);
-  if (rm < maxidx)
-    ## Pad with the last color in the map.
-    pad = repmat (map(end,:), maxidx-rm, 1);
-    map(end+1:maxidx, :) = pad;
-  endif
+  [x, map] = ind2x ("ind2rgb", x, map);
 
   ## Compute result
   [hi, wi] = size (x);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/image/private/ind2x.m	Mon Nov 12 04:53:42 2012 +0000
@@ -0,0 +1,56 @@
+## Copyright (C) 1994-2012 John W. Eaton
+## Copyright (C) 2012 Carnë Draug
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## private function for the ind2something functions which have a lot of code
+## in common
+
+function [x, map] = ind2x (name, x, map)
+
+  ## Check if X is an indexed image.
+  if (ndims (x) != 2 || (isfloat (x) && ! isindex (x)) ||
+      ! ismember (class (x), {"double", "single", "uint8", "uint16"}))
+    error ("%s: X must be an indexed image", name);
+  endif
+
+  ## Check the color map.
+  if (! iscolormap (map))
+    error ("%s: MAP must be a valid colormap", name);
+  endif
+
+  ## Do we have enough colors in the color map?
+  ## there's an offset of 1 when the indexed image is an integer class so we fix
+  ## it now and convert it to float only if really necessary and even then only
+  ## to single precision since its enough for both uint8 and uint16
+  maxidx = max (x(:));
+  if (isinteger (x))
+    if (maxidx == intmax (class (x)))
+      x = single (x);
+    endif
+    x      += 1;
+    maxidx += 1;
+  endif
+
+  rm = rows (map);
+  if (rm < maxidx)
+    ## Pad with the last color in the map.
+    pad = repmat (map(end,:), maxidx-rm, 1);
+    map(end+1:maxidx, :) = pad;
+  endif
+
+endfunction