changeset 510:7d8494a6d8a6

isind: * big rewrite of function based on code from isgray * now returns false if argument is empty matrix * improved help text * upgrade license to GPLv3+
author carandraug
date Mon, 05 Dec 2011 04:40:59 +0000
parents 7e71f51a2df7
children 9135e5461550
files inst/isind.m
diffstat 1 files changed, 38 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/inst/isind.m
+++ b/inst/isind.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,26 +15,49 @@
 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} @var{bool}= isind (@var{X})
-## Returns true for an index image. All index values must
-## be intergers and greater than  or equal to 1.
+## @deftypefn {Function File} @var{bool} = isind (@var{img})
+## Return true if @var{img} is a RGB image.
+##
+## A variable is considereed to be an indexed image if it is 2-dimensional,
+## non-sparse matrix and:
+## @itemize @bullet
+## @item is of class double and all values are integers greater than or equal to 1;
+## @item is of class uint8 or uint16.
+## @end itemize
+##
+## Note that indexed time-series image have 4 dimensions (NxMx1xtime) but
+## isind will still return false.
+## @seealso{isbw, isgray, isrgb}
 ## @end deftypefn
 
-## Author:	Kai Habel <kai.habel@gmx.de>
-## Date:	20/03/2000
+function bool = isind (img)
 
-function ret = isind (X)
-
-  if nargin != 1
-    usage ("isind(X)");
+  if (nargin != 1)
+    print_usage;
   endif
 
-  ret =  isreal (X) && length (size (X)) == 2 ...
-	&& all ( X(:) == floor (X(:)) ) && all ( X(:) >= 1 ); 
+  bool = false;
+  if (ismatrix (img) && ndims (img) == 2 && !issparse (img) && isreal (img) && !isempty (img))
+    switch (class (img))
+      case "double"
+        ## to speed this up, we can look at a sample of the image first
+        bool = is_ind_double (img(1:ceil (rows (img) /100), 1:ceil (columns (img) /100)));
+        if (bool)
+          ## sample was true, we better make sure it's real
+          bool = is_ind_double (img);
+        endif
+      case {"uint8", "uint16"}
+        bool = true;
+    endswitch
+  endif
 
 endfunction
 
-%!assert(isind([]))
+function bool = is_ind_double (img)
+  bool = all (img(:) == fix (img(:))) && all (img(:) >= 1);
+endfunction
+
+%!fail(isind([]))         ## should fail for empty matrix
 %!assert(isind(1:10))
 %!assert(!isind(0:10))
 %!assert(isind(1))