changeset 573:9a6addcc1c55

isgray: use shared private functions for checks and added test blocks
author carandraug
date Sun, 02 Sep 2012 02:37:12 +0000
parents b12b120ecbc1
children b64d36b203a2
files inst/isbw.m inst/isgray.m
diffstat 2 files changed, 15 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/inst/isbw.m
+++ b/inst/isbw.m
@@ -62,7 +62,7 @@
 endfunction
 
 function bool = is_bw_nonlogical (BW)
-  bool = all ((BW == 1)(:) + (BW == 0)(:));
+  bool = all ((BW(:) == 1) + (BW(:) == 0));
 endfunction
 
 %!shared a
--- a/inst/isgray.m
+++ b/inst/isgray.m
@@ -38,15 +38,12 @@
   endif
 
   bool = false;
-  if (ismatrix (img) && ndims (img) == 2 && !issparse (img) && !isempty (img))
+  if (!isimage (img))
+    bool = false;
+  elseif (ndims (img) == 2)
     switch (class (img))
       case "double"
-        ## to speed this up, we can look at a sample of the image first
-        bool = is_gray_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_gray_double (img);
-        endif
+        bool = ispart (@is_gray_double, img);
       case {"uint8", "uint16"}
         bool = true;
     endswitch
@@ -57,3 +54,13 @@
 function bool = is_gray_double (img)
   bool = all ((img(:) >= 0 & img(:) <= 1) | isnan (img(:)));
 endfunction
+
+%!shared a
+%! a = rand (100);
+%!assert (isgray (a), true);
+%! a(50, 50) = 2;
+%!assert (isgray (a), false);
+%! a = uint8 (randi (255, 100));
+%!assert (isgray (a), true);
+%! a = int8 (a);
+%!assert (isgray (a), false);