octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: images


From: Daniel J Sebald
Subject: Re: images
Date: Fri, 09 Feb 2007 04:40:30 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041020

Daniel J Sebald wrote:
John W. Eaton wrote:

On  8-Feb-2007, Daniel J Sebald wrote:

| Ok, some more questions.  I see the issue with imshow(r,g,b).
| Legacy code converts those r,g,b components to a color map.  That's
| not required and probably not preferred.  Gnuplot can handle r,g,b
| components.
| | Should there be a general function, say __img__.m to handle all the
| cases of imshow, image, and imagesc?

If there is duplication functionality, then yes it wouldn't hurt to
handle common cases in a single function.


I've done this. Since there is to be color components and a colormap, they differ very little. Limits are similar. Seems worth doing right now.


| I guess a more basic question is should there be another "image"
| type, e.g., "rgbimage", or do both fall under the class "image"?
| I.e., another case?
| |     case "image"
|       if (have_img_data)
|         warning ("an axis can only display one image");
|       endif
|       have_img_data = true;
|       img_data = obj.cdata;

As far as I know, there is only one "image" graphics object type in
Matlab, so for compatibility that's what we have.


Yes, cdata can probably be three dimensional for the RGB components. I've done that.


| Having multiple images shouldn't be too difficult.

What is needed to do that?  Do you mean it would not be too hard to do
this in Octave now, or that it would not be hard to make gnuplot
handle multiple images on the same plot?


Gnuplot can handle multiple images in the same command as far as I know. However, for now I'm leaving just one image per axis. So, there can be multiple images via subplot. That seems useful enough to satisfy most right now. Multiple images on the same axis shouldn't be difficult.

With that in mind. For the time being I'm keeping the colormap as part of the axis array. Doesn't it seem logical that in one subplot we could have one colormap and in another subplot a different colormap? Well, I've implemented this, but unfortunately there seems to be a bug in gnuplot where the color map changes for both images. I thought I'd fixed that quite a while ago. I'll investigate.

Oh, I recall the gnuplot issue there.  There can only be one palette for an X 
window.  gnuplot_x11 is set up so that each figure window is an X window.  I 
think we left off there wondering if each each subplot could be made its own X 
window.

So, there is a bug, but the bug is in gnuplot.  (Don't expect any imediate fix, 
it would be a project for this summer.)  However, the PostScript output should 
still work, so:

subplot(2,1,1)
image
colormap(ocean(64));
subplot(2,1,2)
image
print -depsc2 foo.eps

should give the correct EPS file.

The attached patch should be a step in the right direction.  The file diffs 
were created separately rather than recursively.  If I missed a file, let me 
know.

Dan

PS:  Should gray() be calling colormap()?  Or just creating the colormap that 
can be input to colormap(gray(64))?

--- image/__img__.m     1969-12-31 18:00:00.000000000 -0600
+++ /home/sebald/octave/__img__.m       2007-02-09 04:15:19.945242952 -0600
@@ -0,0 +1,73 @@
+## FIXME  HEADER INFO?
+##
+## Copyright (C) 1996, 1997 John W. Eaton
+##
+## This file is part of Octave.
+##
+## Octave 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, or (at your option)
+## any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, write to the Free
+## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+## 02110-1301, USA.
+
+## -*- texinfo -*-
+## FIXME FIXED NUMBER OF INPUT PARAMETERS
+## @deftypefn {Function File} {} __img__ (@var{img})
+## @deftypefnx {Function File} {} __img__ (@var{x}, @var{y}, @var{img})
+## Generic image creation.
+##
+## The axis values corresponding to the matrix elements are specified in
+## @var{x} and @var{y}. If you're not using gnuplot 4.2 or later, these
+## variables are ignored.
+## @end deftypefn
+
+## Author: Tony Richardson <address@hidden>
+## Created: July 1994
+## Adapted-By: jwe
+
+function h = __img__ (x, y, img)
+
+  newplot ();
+
+  if (isempty (img))
+    error ("__img__: matrix is empty");
+  endif
+
+  if (isempty (x))
+    x = [1, columns(img)];
+  endif
+
+  if (isempty (y))
+    y = [1, rows(img)];
+  endif
+
+  xlim = [x(1), x(end)];
+  ylim = [y(1), y(end)];
+
+  ca = gca ();
+
+  s = __uiobject_image_ctor__ (ca);
+
+  s.cdata = img;
+  s.lims = [xlim ylim];
+
+  tmp = __uiobject_make_handle__ (s);
+
+  __uiobject_adopt__ (ca, tmp);
+
+  set (ca, "view", [0, 90], "xlim", xlim, "ylim", ylim);
+
+  if (nargout > 0)
+    h = tmp;
+  endif
+
+endfunction
--- image/colormap.m    2007-02-08 11:50:52.000000000 -0600
+++ /home/sebald/octave/colormap.m      2007-02-09 04:25:16.561543488 -0600
@@ -61,12 +61,12 @@
         error ("colormap: map must have values in [0,1]");
       endif
       ## Set the new color map
-      set (gcf (), "colormap", map);
+      set (gca (), "colormap", map);
     endif
 
   endif
 
   ## Return current color map.
-  cmap = get (gcf (), "colormap");
+  cmap = get (gca (), "colormap");
 
 endfunction
--- image/image.m       2007-02-08 11:50:52.000000000 -0600
+++ /home/sebald/octave/image.m 2007-02-09 04:14:16.427899056 -0600
@@ -56,33 +56,7 @@
     print_usage ();
   endif
 
-  if (isempty (img))
-    error ("image: matrix is empty");
-  endif
-
-  ## Use the newly added mode of "plot" called "with image".
-  if (isempty (x))
-    x = [1, columns(img)];
-  endif
-
-  if (isempty (y))
-    y = [1, rows(img)];
-  endif
-
-  ca = gca ();
-
-  s = __uiobject_image_ctor__ (ca);
-
-  s.cdata = img;
-
-  tmp = __uiobject_make_handle__ (s);
-
-  __uiobject_adopt__ (ca, tmp);
-
-  xlim = [x(1), x(end)];
-  ylim = [y(1), y(end)];
-
-  set (ca, "view", [0, 90], "xlim", xlim, "ylim", ylim);
+  tmp = __img__ (x, y, img);
 
   if (nargout > 0)
     h = tmp;
--- image/imshow.m      2007-02-08 11:50:52.000000000 -0600
+++ /home/sebald/octave/imshow.m        2007-02-09 04:07:11.000000000 -0600
@@ -146,21 +146,20 @@
     im(im > 1) = 1;
   endif
   
-  ## Convert to indexed image.
   dim = ndims (im);
   if (dim == 2)
     im = round ((size (color_map, 1) - 1) * im);
+    colormap (color_map);
+    image (im, initial_magnification/100);
+    colormap (old_colormap);
   elseif (dim == 3 && size (im, 3) == 3)
-    [im, color_map] = rgb2ind (im);
+    __img__ ([] , [], im);
+##FIXME NEEDED ANYMORE FOR A SPECIAL CASE?    ## Convert to indexed image.
+##    [im, color_map] = rgb2ind (im);
   else
     error ("imshow: input image must be a 2D or 3D matrix");
   endif
   
-  ## And now, we show the image.
-  colormap (color_map);
-  image (im, initial_magnification/100);
-  colormap (old_colormap);
-
 endfunction
 
 %!error imshow ()                           # no arguments
--- plot/__uiobject_image_ctor__.m      2007-02-08 11:50:56.000000000 -0600
+++ /home/sebald/octave/__uiobject_image_ctor__.m       2007-02-09 
04:13:32.576565472 -0600
@@ -32,6 +32,7 @@
     s.children = [];
 
     s.cdata = [];
+    s.lims = [];
 
     ## XXX FIXME XXX -- need to intialize all properties to default
     ## values here.
--- plot/__uiobject_draw_axes__.m       2007-02-08 11:50:56.000000000 -0600
+++ /home/sebald/octave/__uiobject_draw_axes__.m        2007-02-09 
03:52:21.000000000 -0600
@@ -222,7 +222,6 @@
     data = cell ();
 
     have_img_data = false;
-    img_data = [];
 
     xminp = yminp = zminp = Inf;
     xmax = ymax = zmax = -Inf;
@@ -239,7 +238,8 @@
          endif
          have_img_data = true;
          img_data = obj.cdata;
-
+         img_colormap = axis_obj.colormap;
+         img_lims = obj.lims;
 
        case "line"
          data_idx++;
@@ -537,28 +537,52 @@
       if (ischar (view_fcn) && strcmp (view_fcn, "gnuplot_internal"))
        have_data = true;
 
-       [y_dim, x_dim] = size (img_data);
+       [y_dim, x_dim] = size (img_data(:,:,1));
        if (x_dim > 1)
-         dx = abs (xlim(2)-xlim(1))/(x_dim-1);
+         dx = abs (img_lims(2)-img_lims(1))/(x_dim-1);
        else
          dx = 1;
        endif
        if (y_dim > 1)
-         dy = abs (ylim(2)-ylim(1))/(y_dim-1);
+         dy = abs (img_lims(4)-img_lims(3))/(y_dim-1);
        else
          dy = 1;
        endif
-       x_origin = min (xlim);
-       y_origin = min (ylim);
+       x_origin = min (img_lims(1:2));
+       y_origin = min (img_lims(3:4));
 
        ## Let the file be deleted when Octave exits or `purge_tmp_files'
        ## is called.
        [fid, fname] = mkstemp (strcat (P_tmpdir, "/gpimageXXXXXX"), 1);
-       fwrite (fid, img_data(:), "float");
+       if (ndims (img_data) == 3)
+         fwrite (fid, [img_data(:,:,1)(:) img_data(:,:,2)(:) 
img_data(:,:,3)(:)]'(:), "float");
+         format = "1:2:3";
+         imagetype = "rgbimage";
+       else
+         fwrite (fid, img_data(:), "float");
+         format = "1";
+         imagetype = "image";
+         palette_size = size(img_colormap,1);
+         fprintf (plot_stream, "set palette positive color model RGB maxcolors 
%i;\n", palette_size);
+         if (palette_size <= 128)
+           ## Break up command to avoid buffer overflow.
+           fprintf (plot_stream, "set palette file \"-\" using 1:2:3:4;\n");
+           for i = 1:palette_size
+             fprintf (plot_stream, "%g %g %g 
%g;\n",1e-3*round(1e3*[(i-1)/(palette_size-1) img_colormap(i,:)]));
+           end
+           fprintf (plot_stream, "e;\n");
+         else
+           # Let the file be deleted when Octave exits or `purge_tmp_files' is 
called.
+           [fid, binary_file_name, msg] = mkstemp([P_tmpdir,"/gpimageXXXXXX"], 
1);
+           fwrite(fid, img_colormap', "float32", 0, "ieee-le");
+           fclose(fid);
+           fprintf (plot_stream, "set palette file \"%s\" binary record=%d 
using 1:2:3;\n",binary_file_name,palette_size);
+         endif
+       endif
        fclose (fid);
 
-       fprintf (plot_stream, "plot \"%s\" binary array=%dx%d scan=yx flipy 
origin=(%g,%g) dx=%g dy=%g using 1 with image",
-                fname, x_dim, y_dim, x_origin, y_origin, dx, dy);
+       fprintf (plot_stream, "plot \"%s\" binary array=%dx%d scan=yx flipy 
origin=(%g,%g) dx=%g dy=%g using %s with %s",
+                fname, x_dim, y_dim, x_origin, y_origin, dx, dy, format, 
imagetype);
 
        plot_cmd = ",";
       else
--- plot/__uiobject_axes_ctor__.m       2007-02-08 11:50:56.000000000 -0600
+++ /home/sebald/octave/__uiobject_axes_ctor__.m        2007-02-09 
02:30:20.000000000 -0600
@@ -32,6 +32,7 @@
     s.parent = p;
     s.children = [];
     s.position = [];
+    s.colormap = __default_colormap__ ();
 
     h = __uiobject_make_handle__ (s);
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]