changeset 20392:a3bf503652b2

iscolormap: relax input check - specially in [0 1] range. * scripts/image/iscolormap.m: relax input check. Allow for colormap of class single and values outside the [0 1] range. What to do in the case of values outside the [0 1] will need to be a per-function choice, in order to meet Matlab compatibility. Also, not Matlab supported, a colormap could perfectly be of class integer. * scripts/image/cmpermute.m, scripts/image/cmunique.m, scripts/image/imshow.m: add input check for values outside the [0 1] range.
author Carnë Draug <carandraug@octave.org>
date Thu, 16 Jul 2015 18:21:27 +0100
parents d2c03beac955
children 84ca63c8a038
files scripts/image/cmpermute.m scripts/image/cmunique.m scripts/image/imshow.m scripts/image/iscolormap.m
diffstat 4 files changed, 15 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/cmpermute.m	Thu Jul 16 10:57:34 2015 -0400
+++ b/scripts/image/cmpermute.m	Thu Jul 16 18:21:27 2015 +0100
@@ -53,7 +53,7 @@
     error ("cmpermute: X must be an indexed image");
   endif
 
-  if (! iscolormap (map))
+  if (! iscolormap (map) || min (map(:)) < 0 || max (map(:)) > 1)
     error ("cmpermute: MAP must be a valid colormap");
   endif
 
--- a/scripts/image/cmunique.m	Thu Jul 16 10:57:34 2015 -0400
+++ b/scripts/image/cmunique.m	Thu Jul 16 18:21:27 2015 +0100
@@ -67,7 +67,7 @@
 
   if (nargin == 2)
     ## (X, map) case
-    if (! iscolormap (map))
+    if (! iscolormap (map) || min (map(:)) < 0 || max (map(:)) > 1)
       error ("cmunique: MAP must be a valid colormap");
     endif
     [newmap,i,j] = unique (map, "rows");  # calculate unique colormap
--- a/scripts/image/imshow.m	Thu Jul 16 10:57:34 2015 -0400
+++ b/scripts/image/imshow.m	Thu Jul 16 18:21:27 2015 +0100
@@ -114,7 +114,7 @@
         display_range = arg;
       elseif (columns (arg) == 3)
         indexed = true;
-        if (iscolormap (arg))
+        if (iscolormap (arg) && min (arg) >= 0 || max (arg) <= 1)
           colormap (arg);
         else
           error ("imshow: invalid colormap MAP");
@@ -129,7 +129,7 @@
           narg++;
         case "colormap"
           map = varargin{narg++};
-          if (iscolormap (map))
+          if (iscolormap (map) && min (map) >= 0 || max (map) <= 1)
             colormap (map);
           else
             error ("imshow: invalid colormap");
@@ -262,9 +262,9 @@
 %!test
 %! hf = figure ("visible", "off");
 %! unwind_protect
-%!   fail ("imshow ([1,1], [2 0 0])", "invalid colormap MAP");
+%!   fail ("imshow ([1,1], [2 0 0])", "all MAP values must be in the range");
 %!   fail ("imshow ([1,1], [1 0 0 0])", "argument number 2 is invalid");
-%!   fail ('imshow ([1,1], "colormap", [2 0 0])', "invalid colormap");
+%!   fail ('imshow ([1,1], "colormap", [2 0 0])', "all MAP values must be in the range");
 %!   fail ('imshow ([1,1], "parent", -1)', "must be an axes handle");
 %!   fail ('imshow ([1,1], "xdata", ones (2,2))', "xdata must be a vector");
 %!   fail ('imshow ([1,1], "ydata", ones (2,2))', "ydata must be a vector");
--- a/scripts/image/iscolormap.m	Thu Jul 16 10:57:34 2015 -0400
+++ b/scripts/image/iscolormap.m	Thu Jul 16 18:21:27 2015 +0100
@@ -22,7 +22,12 @@
 ##
 ## A colormap is a real matrix with @var{n} rows and 3 columns.  Each row
 ## represents a single color.  The columns contain red, green, and blue
-## intensities respectively.  All entries must be between 0 and 1 inclusive.
+## intensities respectively.
+##
+## All values in a colormap should be in the [0 1] range but this is not
+## enforced.  Each function must decide what to do for values outside this
+## range.
+##
 ## @seealso{colormap, rgbplot}
 ## @end deftypefn
 
@@ -34,19 +39,14 @@
     print_usage;
   endif
 
-  retval = (isnumeric (cmap) && isreal (cmap) && ndims (cmap) == 2
-            && columns (cmap) == 3 && isa (cmap, "double")
-            && min (cmap(:)) >= 0 && max (cmap(:)) <= 1);
+  retval = (isnumeric (cmap) && isreal (cmap)
+            && ndims (cmap) == 2 && columns (cmap) == 3
+            && isfloat (cmap));
 
 endfunction
 
-
 %!assert (iscolormap (jet (64)))
 %!assert (iscolormap ({0 1 0}), false)
 %!assert (iscolormap ([0 1i 0]), false)
 %!assert (iscolormap (ones (3,3,3)), false)
 %!assert (iscolormap (ones (3,4)), false)
-%!assert (iscolormap (single (jet (64))), false)
-%!assert (iscolormap ([0 0 -2]), false)
-%!assert (iscolormap ([0 0 2]), false)
-