import ij.IJ; import ij.ImagePlus; import ij.gui.ImageWindow; import ij.plugin.PlugIn; import ij.process.ImageProcessor; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.nio.IntBuffer; import javax.media.opengl.*; import javax.media.opengl.glu.GLU; // Note that a GLCanvas is not a window component ImageJ knows about, // so it will not appear in the ImageJ window menu. // Also, even though it extends Canvas, its paint and update methods can't // be used to draw on it, or the Graphics object be used to get at pixel values public class OpenGLExample2_ implements PlugIn, GLEventListener { public OpenGLExample2_() { } IntBuffer buffer = null; ImagePlus implus = null; public void run (String arg) { implus = ij.WindowManager.getCurrentImage(); if (implus == null) { IJ.showMessage("No image is open."); return; } if (implus.getType() != ImagePlus.COLOR_RGB) { IJ.showMessage("This plugin works with RGB images only."); return; } int width = implus.getWidth(); int height = implus.getHeight(); buffer = IntBuffer.allocate(3 * width * height); ImageProcessor proc = (ImageProcessor) implus.getProcessor(); for (int h=0; h> 16) & 255) << 23; int g = ((pixel >> 8) & 255) << 23; int b = ((pixel & 255) << 23); // if these bit operations look strange, it's because I don't fully // understand how OpenGL stores R, G and B values in integers buffer.put(r); buffer.put(g); buffer.put(b); } final Frame f = new Frame("OpenGL Canvas 2"); f.addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent evt) { f.dispose(); } }); GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(this); canvas.setSize(width, height); f.add(canvas); f.pack(); f.setVisible(true); } // The following methods implement the GLEventListener interface public void init (GLAutoDrawable drawable) { GL gl = drawable.getGL(); } public void reshape (GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); gl.glViewport(0, 0, width, height); gl.glLoadIdentity(); } public void display (GLAutoDrawable drawable) { buffer.rewind(); GL gl = drawable.getGL(); gl.glDrawPixels(implus.getWidth(), implus.getHeight(), GL.GL_RGB, GL.GL_INT, buffer); gl.glFinish(); IJ.showStatus("OpenGL image copied"); } public void displayChanged (GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } }