public void saveFrametoPNG(String filename) { final int frameWidth = _canvas.getWidth(); final int frameHeight = _canvas.getHeight(); final ByteBuffer pixelsRGB = Direct.newByteBuffer(frameWidth * frameHeight * 3); _canvas.runWithContext( new Runnable() { public void run() { // glPushAttrib(GL_PIXEL_MODE_BIT); glReadBuffer(GL_BACK); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, frameWidth, frameHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelsRGB); // glPopAttrib(); } }); int[] pixelInts = new int[frameWidth * frameHeight]; int p = frameWidth * frameHeight * 3; int q; // Index into ByteBuffer int i = 0; // Index into target int[] int w3 = frameWidth * 3; // Number of bytes in each row for (int row = 0; row < frameHeight; row++) { p -= w3; q = p; for (int col = 0; col < frameWidth; col++) { int iR = pixelsRGB.get(q++); int iG = pixelsRGB.get(q++); int iB = pixelsRGB.get(q++); pixelInts[i++] = 0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF); } } // Create a new BufferedImage from the pixeldata. BufferedImage bufferedImage = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, frameWidth, frameHeight, pixelInts, 0, frameWidth); try { javax.imageio.ImageIO.write(bufferedImage, "PNG", new File(filename)); } catch (IOException e) { System.out.println("Error: ImageIO.write."); e.printStackTrace(); } /* End code taken from: http://www.felixgers.de/teaching/jogl/imagingProg.html */ /* final BufferedImage image = new BufferedImage( this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics gr = image.getGraphics(); this.printAll(gr); gr.dispose(); try { ImageIO.write(image, "PNG", new File(filename)); } catch (IOException e) { System.out.println( "Error: ImageIO.write." ); e.printStackTrace(); } */ }
public static void main(String[] args) throws Exception { Convolution[] filters = { box, new Hanning(1), new Hanning(2), new Hamming(1), new Lanczos(2), new Lanczos(3), }; // BufferedImage in = Resource.loadimg("gfx/invobjs/herbs/crowberry"); BufferedImage in = javax.imageio.ImageIO.read(new java.io.File("/tmp/e.jpg")); Coord tsz = new Coord(300, 300); for (int i = 0; i < filters.length; i++) { long start = System.nanoTime(); BufferedImage out = convolvedown(in, tsz, filters[i]); System.err.println(System.nanoTime() - start); javax.imageio.ImageIO.write(out, "PNG", new java.io.File("/tmp/barda" + i + ".png")); } }
/** * Save an array of ARGB pixels to a PNG file. If flipY is true, flip the pixels on the Y axis * before saving. */ public static void savePixelsToPNG( int[] pixels, int width, int height, String imageFilename, boolean flipY) { if (pixels != null && imageFilename != null) { if (flipY) { // flip the pixels vertically (opengl has 0,0 at lower left, java is upper left) pixels = GLImage.flipPixels(pixels, width, height); } try { // Create a BufferedImage with the RGB pixels then save as PNG BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); image.setRGB(0, 0, width, height, pixels, 0, width); javax.imageio.ImageIO.write(image, "png", new File(imageFilename)); } catch (Exception e) { GLApp.err("GLImage.savePixelsToPNG(" + imageFilename + "): exception " + e); } } }