changeset 502:86fd5ef6ab60

isbw: IMPORTANT COMMIT BREAKS COMPATIBILITY * isbw is now matlab compatible and returns true if image is logical, non sparse matrix * new option `non-logical' was added to return to old behaviour * new help text to reflect changes * upgrade license to GPLv3+
author carandraug
date Mon, 05 Dec 2011 00:47:20 +0000
parents c977a1838342
children 5dd8356bd17c
files inst/isbw.m
diffstat 1 files changed, 38 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/inst/isbw.m
+++ b/inst/isbw.m
@@ -1,8 +1,9 @@
-## Copyright (C) 2000  Kai Habel
+## Copyright (C) 2000 Kai Habel <kai.habel@gmx.de>
+## Copyright (C) 2011 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 2 of the License, or
+## 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,
@@ -14,25 +15,45 @@
 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} @var{bool}= isbw (@var{BW})
-## Returns true for a black-white (binary) image.
-## All values must be either 0 or 1
+## @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.
+##
+## 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'.
+##
+## @seealso{isgray, isind, islogical, isrgb}
 ## @end deftypefn
 
-## Author:	Kai Habel <kai.habel@gmx.de>
-## Date:	20/03/2000
-
-function bool = isbw (BW)
-
-  bool = 0;	
-  if !(nargin == 1)
-    usage ("isbw(BW)");
+function bool = isbw (BW, logic = "logical")
+  ## this function has been removed from version 7.3 (R2011b) of
+  ## matlab's image processing toolbox
+  if (nargin < 1 || nargin > 2)
+    print_usage;
+  elseif (!ischar (logic) && any (strcmpi (logic, {"logical", "non-logical"})))
+    error ("second argument must either be a string 'logical' or 'non-logical'")
   endif
 
-  if !(ismatrix(BW))
-    return;
-  endif
+  ## an image cannot be a sparse matrix
+  if (!ismatrix (BW) || issparse (BW))
+    bool = false;
+  elseif (strcmpi (logic, "logical"))
+    ## this is the matlab compatible way (before they removed the function)
+    bool = islogical (BW);
 
-  bool = all (all ((BW == 1) + (BW == 0)));
+    ## the following block is just temporary since we are not being backwards compatible
+    if (!islogical (BW) && all (all ((BW == 1) + (BW == 0))))
+      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: old versions of the function would return true. Use the call isbw (img, \"non-logical\") instead.")
+      endif
+    endif
 
+  elseif (strcmpi (logic, "non-logical"))
+    bool = all (all ((BW == 1) + (BW == 0)));
+  endif
 endfunction