changeset 10005:0da880b38258 octave-forge

im2double: added support for indexed images and matlab compatibility
author carandraug
date Wed, 11 Apr 2012 15:22:19 +0000
parents ebea8994debd
children bfec87972219
files main/image/inst/im2double.m
diffstat 1 files changed, 53 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/main/image/inst/im2double.m	Wed Apr 11 14:57:31 2012 +0000
+++ b/main/image/inst/im2double.m	Wed Apr 11 15:22:19 2012 +0000
@@ -1,48 +1,69 @@
-## Copyright (C) 2007  Søren Hauberg
-## 
-## This program 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 2, or (at your option)
-## any later version.
-## 
-## This program 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 this file.  If not, see <http://www.gnu.org/licenses/>.
+## Copyright (C) 2007 Søren Hauberg <soren@hauberg.org>
+## Copyright (C) 2012 Carnë Draug <carandraug+dev@gmail.com>
+##
+## This program 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.
+##
+## This program 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
+## this program; if not, see <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} @var{im2} = im2double(@var{im1})
-## Converts the input image to an image of class double.
+## @deftypefnx {Function File} @var{im2} = im2double(@var{im1}, "indexed")
+## Convert input image @var{im1} to double precision.
+##
+## The following images type are supported: double, single, uint8, uint16, int16,
+## binary (logical), indexed. If @var{im1} is an indexed images, the second
+## argument must be a string with the value `indexed'.
 ##
-## If the input image is of class double the output is unchanged.
-## If the input is of class uint8 the result will be converted to doubles
-## and divided by 255, and if the input is of class uint16 the image will
-## be converted to doubles and divided by 65535.
+## Processing will depend on the class of the input image @var{im1}:
+## @itemize @bullet
+## @item uint8, uint16, int16 - output will be rescaled for the interval [0 1]
+## with the limits of the class;
+## @item double - output will be the same as input;
+## @item single - output will have the same values as input but the class will
+## double;
+## @item indexed, logical - converted to double class.
+## @end itemize
+##
 ## @seealso{im2bw, im2uint16, im2uint8}
 ## @end deftypefn
 
-function im2 = im2double(im1)
+function im2 = im2double (im1, ind = false)
   ## Input checking
-  if (nargin < 1)
-    print_usage();
-  elseif (!isgray(im1) && !isrgb(im1) && !isbw(im1))
-    error("im2double: input must be an image");
+  if (nargin < 1 || nargin > 2)
+    print_usage;
+  elseif (nargin == 2 && (!ischar (ind) || !strcmpi (ind, "indexed")))
+    error ("second argument must be a string with the word `indexed'");
+  endif
+
+  if (ind && !isind (im1))
+    error ("input should have been an indexed image but it is not");
   endif
 
   ## Take action depending on the class of the data
-  switch (class(im1))
+  in_class = class (im1);
+  switch in_class
     case "double"
       im2 = im1;
-    case "logical"
-      im2 = double(im1);
-    case "uint8"
-      im2 = double(im1) / 255;
-    case "uint16"
-      im2 = double(im1) / (pow2(16)-1);
+    case {"logical", "single"}
+      im2 = double (im1);
+    case {"uint8", "uint16"}
+      if (ind)
+        im2 = double (im1) + 1;
+      elseif (isind (im1))
+        im2 = double (im1) / double (intmax (in_class));
+      endif
+    case "int16"
+      im2 = (double (im1) + double (intmax (in_class)) + 1) / double (intmax ("uint16"));
     otherwise
-      error("im2double: unsupported image class");
+      error ("unsupported image class");
   endswitch
 endfunction