# HG changeset patch # User Rik # Date 1475250678 25200 # Node ID cd2a8d0c0b562ce7bf374a2c3df46d6683809264 # Parent 202c6871f07d55f3aee8ae07b0b62f6f433a9b6c Better Matlab compatibility for images created with gnuplot (bug #49130). * __gnuplot_draw_axes__.m (mapcdata): Don't do conversion to double->uint8->double to achieve clamping of RGB values. Keep cdata as double and use indexing to find and clamp values outside range [0, 255]. For scaled data, use scaling factor of colormap_size rather than colormap_size-1 for compatibility. For "direct" integer data, convert range from zero-based indices to ones-based. For "direct" float data, truncate using "fix" rather than "round" for compatibility. diff -r 202c6871f07d -r cd2a8d0c0b56 scripts/plot/util/private/__gnuplot_draw_axes__.m --- a/scripts/plot/util/private/__gnuplot_draw_axes__.m Wed Sep 28 15:57:05 2016 -0500 +++ b/scripts/plot/util/private/__gnuplot_draw_axes__.m Fri Sep 30 08:51:18 2016 -0700 @@ -2849,21 +2849,27 @@ function retval = mapcdata (cdata, mode, clim, cmap_sz) if (ndims (cdata) == 3) - # True color, clamp data to 8-bit + ## True Color, clamp data to 8-bit cdata = double (cdata); - cdata = uint8 (255*(cdata-clim(1))/(clim(2)-clim(1))); - # Scale using inverse of gnuplot's cbrange mapping - retval = 1 + double (cdata)*(cmap_sz-1)/255; + cdata = 255 * (cdata - clim(1)) / (clim(2)-clim(1)); + cdata(cdata < 0) = 0; cdata(cdata > 255) = 255; + ## Scale using inverse of gnuplot's cbrange mapping + retval = 1 + cdata * (cmap_sz-1)/255; else if (islogical (cdata)) cdata += 1; elseif (strcmp (mode, "scaled")) cdata = double (cdata); clim = double (clim); - cdata = 1 + fix ((cmap_sz-1)*(cdata-clim(1))/(clim(2)-clim(1))); + cdata = 1 + fix (cmap_sz * (cdata - clim(1)) / (clim(2) - clim(1))); else - cdata = round (cdata); + if (isinteger (cdata)) + cdata += 1; + else + cdata = fix (cdata); + endif endif retval = max (1, min (cdata, cmap_sz)); endif endfunction +