changeset 549:7dbbeef84707

im2double: added new private function that will be used for input checking of all im2_class functions
author carandraug
date Wed, 11 Apr 2012 17:01:36 +0000
parents e9221c0aa50e
children d6a7e7268581
files inst/im2double.m inst/private/imconversion.m
diffstat 2 files changed, 46 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/inst/im2double.m
+++ b/inst/im2double.m
@@ -36,35 +36,26 @@
 ## @seealso{im2bw, im2uint16, im2uint8}
 ## @end deftypefn
 
-function im2 = im2double (im1, ind = false)
-  ## Input checking
-  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
+function im = im2double (im, ind = false)
+
+  ## Input checking (private function that is used for all im2class functions)
+  im_class = imconversion (nargin, "im2double", ind, im);
 
-  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
-  in_class = class (im1);
-  switch in_class
+  switch im_class
     case "double"
-      im2 = im1;
+      ## do nothing, return the same
     case {"logical", "single"}
-      im2 = double (im1);
+      im = double (im);
     case {"uint8", "uint16"}
       if (ind)
-        im2 = double (im1) + 1;
-      elseif (isind (im1))
-        im2 = double (im1) / double (intmax (in_class));
+        im = double (im) + 1;
+      elseif (isind (im))
+        im = double (im) / double (intmax (im_class));
       endif
     case "int16"
-      im2 = (double (im1) + double (intmax (in_class)) + 1) / double (intmax ("uint16"));
+      im = (double (im) + double (intmax (im_class)) + 1) / double (intmax ("uint16"));
     otherwise
-      error ("unsupported image class");
+      error ("unsupported image class %s", im_class);
   endswitch
 endfunction
 
new file mode 100644
--- /dev/null
+++ b/inst/private/imconversion.m
@@ -0,0 +1,34 @@
+## 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/>.
+
+## This is a private fucntion for the common code between the functions that
+## convert an input image into another class. Mostly does the input checking for
+## all of them and returns the class of the input image
+
+function im_class = imconversion (nargs, fname, ind, im1)
+  ## Input checking
+  if (nargs < 1 || nargs > 2)
+    print_usage (fname);
+  elseif (nargs == 2 && (!ischar (ind) || !strcmpi (ind, "indexed")))
+    error ("%s: second argument must be a string with the word `indexed'\n", fname);
+  endif
+
+  if (ind && !isind (im1))
+    error ("%s: input should have been an indexed image but it is not.\n", fname);
+  endif
+
+  im_class = class (im1);
+
+endfunction