MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Image window via Mathlink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22830] Re: Image window via Mathlink
  • From: tgayley at linkobjects.com (Todd Gayley)
  • Date: Fri, 31 Mar 2000 01:01:32 -0500 (EST)
  • Organization: LinkObjects
  • References: <8av7i3$j2v@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 18 Mar 2000 01:26:11 -0500, "DIAMOND Mark" <noname at noname.com> wrote:

>Before I duplicate something that has already been done ...
>
>I would like to open a new (Win95) window and display a 2D matrix as an
>indexed-colour bitmap picture, *not* using the Mathematica postscript. The function
>would probably be something like
>
>ShowPicture( image_2DMatrix, colourmap_RGBMatrix)
>
>I wonder if anyone has written an executable, with a MathLink connection,
>that will do this?
>
>--
>Mark R Diamond
>Vision Research Laboratory
>The University of Western Australia
>no spam email: markd at psy dot uwa dot edu dot au


This is something that can be done easily with the freely-available J/Link package
(www.wolfram.com/solutions/mathlink/jlink). Actually, to do it "easily" requires a new
feature in the soon-to-be-released 1.0.2 version of J/Link. This is the MathCanvas class,
which I mentioned in a recent reply to an earlier question. Users will have to wait for
version 1.0.2 before trying this example.

As I will continue to emphasize in these "you can do that with J/Link" posts, any time you
find yourself wanting to do something beyond the bounds of the Mathematica language, you
should ask yourself "Can that be done in Java?" The answer in this case is yes, and with a
little help from the MathCanvas class, it is easy to get the image displayed in a window.

The old way of dealing with this question is to break out your Windows C++ compiler and
the Win32 API docs, or hope that someone else has already done it. With J/Link, you can do
these sorts of things in a way that works on any platform, and probably requires writing
only Mathematica code.

Java has indexed-color bitmaps, and to write this example code you would need to peruse
the documentation for creating Java Image objects, because that's what you need to create
and pass to the MathCanvas class for display. I'll use an 8-bit color table and a
400-by-400 pixel image. You could also use 16- 24- or 32-bit color tables.

Here's the color table. As requested, it is an array of {r,g,b} triplets, with each color
component being in the range 0..255. In this example, colors with low indices are mostly
blue, and ones with high indices are mostly red.

In[2] := colors = Table[{i, 0, 255 - i}, {i, 0, 255}];

The data is a 400x400 matrix of integers in the range 0..255 (because they are indices
into the 256-element color table).

In[3]:= data = Table[Round[255 (0.5 + Sin[x]Cos[y]/2)], {x, Pi/100., 4Pi, Pi/100.}, {y,
Pi/100., 4Pi, Pi/100.}];

Here's the code for ShowPicture. This will look unfamiliar to many, but we are just
creating a few Java objects and calling methods on them (using the standard J/Link '@'
notation). The IndexColorModel object is the color table, and the MemoryImageSource object
is the bitmap along with its color table.

In[4]:=

ShowPicture[data_?MatrixQ, colorMap:{{_Integer, _Integer, _Integer}..}] :=
    JavaBlock[
        Module[{frame, mathCanvas, colorModel, memImageSource, image},
            InstallJava[];
            frame = JavaNew["com.wolfram.jlink.MathFrame", "Indexed-Color Bitmap"];
            mathCanvas = JavaNew["com.wolfram.jlink.MathCanvas"];
            frame at add[mathCanvas];
            frame at setSize[500, 500];
            mathCanvas at setSize[500, 500];
            colorModel = JavaNew["java.awt.image.IndexColorModel", 8, 256,
Flatten[colorMap], 0, False];
            memImageSource = JavaNew["java.awt.image.MemoryImageSource", 400, 400,
colorModel, Flatten[data], 0, 400];
            image = frame at getToolkit[]@createImage[memImageSource];
            mathCanvas at setImage[image];
            JavaShow[frame];
        ]
    ]

Now, make the window appear:

In[5]:= ShowPicture[data, colors]

That's it.


--Todd

-------------------------------------------------
Todd Gayley
Wolfram Research, Inc.
tgayley at wolfram.com



  • Prev by Date: Re: Exporting formulae as GIFs
  • Next by Date: Re: Real-time plotting
  • Previous by thread: Image window via Mathlink
  • Next by thread: data structures