changeset 553:be52435a5794

im2single: new function for image package
author carandraug
date Wed, 11 Apr 2012 18:30:27 +0000
parents 30f0a2a06f02
children 18070036aca2
files NEWS inst/im2double.m inst/im2single.m
diffstat 3 files changed, 73 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@
       blockproc
       bwlabeln
       getrangefromclass
+      im2single
       imabsdiff
       imadd
       imbothat
--- a/inst/im2double.m
+++ b/inst/im2double.m
@@ -41,6 +41,10 @@
   ## Input checking (private function that is used for all im2class functions)
   im_class = imconversion (nargin, "im2double", indexed, im);
 
+  ## READ BEFORE MAKE CHANGES:
+  ## this function is pretty much the same as im2single. Any changes on this code
+  ## should most likely also be done there
+
   switch im_class
     case "double"
       ## do nothing, return the same
new file mode 100644
--- /dev/null
+++ b/inst/im2single.m
@@ -0,0 +1,68 @@
+## 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} = im2single (@var{im1})
+## @deftypefnx {Function File} @var{im2} = im2single (@var{im1}, "indexed")
+## Convert input image @var{im1} to single 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'.
+##
+## 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 single - output will be the same as input;
+## @item double - output will have the same values as input but the class will
+## single;
+## @item indexed, logical - converted to single class.
+## @end itemize
+##
+## @seealso{im2bw, im2uint16, im2uint8}
+## @end deftypefn
+
+function im = im2single (im, indexed = false)
+
+  ## Input checking (private function that is used for all im2class functions)
+  im_class = imconversion (nargin, "im2single", indexed, im);
+
+  ## READ BEFORE MAKE CHANGES:
+  ## this function is pretty much the same as im2double. Any changes on this code
+  ## should most likely also be done there
+
+  switch im_class
+    case "single"
+      ## do nothing, return the same
+    case {"logical", "double"}
+      im = single (im);
+    case {"uint8", "uint16"}
+      if (indexed)
+        im = single (im) + 1;
+      else
+        im = single (im) / single (intmax (im_class));
+      endif
+    case "int16"
+      im = (single (im) + single (intmax (im_class)) + 1) / single (intmax ("uint16"));
+    otherwise
+      error ("unsupported image class %s", im_class);
+  endswitch
+endfunction
+
+%!assert(im2single([1 2 3]), single([1 2 3]));                  # double returns the same
+%!assert(im2single(uint8([0 255])), single([0 1]));             # basic usage
+%!assert(im2single(uint8([1 25]), "indexed"), single([2 26]));  # test indexed
+%!assert(im2single(int16([-32768 32768])), single([0 1]));      # test signed integer