changeset 10755:996fadda47e4 octave-forge

isbw: delegate checks to new shared private functions and add test blocks
author carandraug
date Sun, 02 Sep 2012 02:31:53 +0000
parents 4ca5fadf4f0c
children 42c5130baae2
files main/image/inst/isbw.m main/image/inst/private/isimage.m main/image/inst/private/ispart.m
diffstat 3 files changed, 69 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/main/image/inst/isbw.m	Sun Sep 02 01:53:19 2012 +0000
+++ b/main/image/inst/isbw.m	Sun Sep 02 02:31:53 2012 +0000
@@ -17,12 +17,12 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} @var{bool} = isbw (@var{img})
 ## @deftypefnx {Function File} @var{bool} = isbw (@var{img}, @var{logic})
-## Return true if @var{img} is a black-white image.
+## Return true if @var{img} is a black and white image.
 ##
-## The optional argument @var{logic} must be the string `logical' or
-## `non-logical'. The first defines a black and white image as a logical matrix,
-## while the second defines it as a matrix comprised of the values 1 and 0
-## only. Defaults to `logical'.
+## The optional argument @var{logic} defines what is considered a black and
+## white image. Possible values are the strings `logical' or `non-logical'. The
+## first defines it as a logical matrix, while the second defines it as a matrix
+## where the only values are 1 and 0. Defaults to `logical'.
 ##
 ## @seealso{isgray, isind, islogical, isrgb}
 ## @end deftypefn
@@ -36,32 +36,27 @@
     error ("second argument must either be a string 'logical' or 'non-logical'")
   endif
 
-  ## an image cannot be a sparse matrix
-  if (!ismatrix (BW) || issparse (BW) || isempty (BW))
+  bool = false;
+  if (!isimage (BW))
     bool = false;
   elseif (strcmpi (logic, "logical"))
     ## this is the matlab compatible way (before they removed the function)
     bool = islogical (BW);
 
-    ## the following block is just temporary to keep backwards compatibility
-    if (!islogical (BW) && is_bw_nonlogical (BW))
+    ## FIXME the following block is just temporary to keep backwards compatibility
+    if (nargin == 1 && !islogical (BW) && isbw (BW, "non-logical"))
       persistent warned = false;
       if (! warned)
         warned = true;
         warning ("isbw: image is not logical matrix and therefore not binary but all values are either 0 and 1.")
-        warning ("isbw: future versions of this function will return true. Consider using the call isbw (img, \"non-logical\").")
+        warning ("isbw: future versions of this function will return true. Consider using the call `isbw (img, \"non-logical\")'.")
       endif
       bool = true;
     endif
     ## end of temporary block for backwards compatibility
 
   elseif (strcmpi (logic, "non-logical"))
-    ## to speed this up, we can look at a sample of the image first
-    bool = is_bw_nonlogical (BW(1:ceil (rows (BW) /100), 1:ceil (columns (BW) /100)));
-      if (bool)
-        ## sample was true, we better make sure it's real
-        bool = is_bw_nonlogical (BW);
-      endif
+    bool = ispart (@is_bw_nonlogical, BW);
   endif
 
 endfunction
@@ -69,3 +64,12 @@
 function bool = is_bw_nonlogical (BW)
   bool = all (all ((BW == 1) + (BW == 0)));
 endfunction
+
+%!shared a
+%! a = round(rand(100));
+%!assert (isbw (a, "non-logical"), true);
+%!assert (isbw (a, "logical"), false);
+%!assert (isbw (logical(a), "logical"), true);
+%!assert (isbw (logical(a), "non-logical"), true);
+%! a(50, 50) = 2;
+%!assert (isbw (a, "non-logical"), false);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/inst/private/isimage.m	Sun Sep 02 02:31:53 2012 +0000
@@ -0,0 +1,24 @@
+## 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 a private function for the is... type of functions for the image package
+## It simply checks if the input really is an image.
+
+function bool = isimage (img)
+  bool = true;
+  if (!ismatrix (img) || issparse (img) || isempty (img))
+    bool = false;
+  endif
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/inst/private/ispart.m	Sun Sep 02 02:31:53 2012 +0000
@@ -0,0 +1,25 @@
+## 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 a private function for the is... type of functions for the image package
+## Rather than checking the whol image, there can be a speed up by checking only
+## a corner of the image first and then the rest if that part is true.
+
+function bool = ispart (foo, in)
+  bool = foo (in(1:ceil (rows (in) /100), 1:ceil (columns (in) /100)));
+  if (bool)
+    bool = foo (in);
+  endif
+endfunction