/** * Create GLImage from image file bytes (the contents of a jpg, gif or png file). Flip Y axis. * * @param pixels * @param w * @param h */ public GLImage(byte[] bytes) { BufferedImage img = makeBufferedImage(bytes); if (makeGLImage(img, true, false)) { GLApp.msg("GLImage(byte[]): loaded image from bytes[" + bytes.length + "]"); } else { GLApp.err("GLImage(byte[]): could not create Image from bytes[" + bytes.length + "]"); } }
/** * Create GLImage from image file bytes (the contents of a jpg, gif or png file). * * @param pixels * @param w * @param h */ public GLImage(byte[] bytes, boolean flipYaxis, boolean convertPow2) { BufferedImage img = makeBufferedImage(bytes); if (makeGLImage(img, flipYaxis, convertPow2)) { GLApp.msg("GLImage(byte[],bool,bool): loaded image from bytes[" + bytes.length + "]"); } else { GLApp.err( "GLImage(byte[],bool,bool): could not create Image from bytes[" + bytes.length + "]"); } }
/** * Load a BufferedImage from the given image file name. File can be in the local filesytem, in the * applet folder, or in a jar. */ public BufferedImage loadJavaImage(String imgName) { BufferedImage tmpi = null; try { tmpi = ImageIO.read(GLApp.getInputStream(imgName)); } catch (Exception e) { GLApp.err("GLImage.loadJavaImage() exception: FAILED TO LOAD IMAGE " + e); } return tmpi; }
/** * Make a BufferedImage from the contents of an image file. * * @param imageFileContents byte array containing the guts of a JPG, GIF, or PNG */ public BufferedImage makeBufferedImage(byte[] imageFileContents) { BufferedImage bi = null; try { InputStream in = new ByteArrayInputStream(imageFileContents); bi = javax.imageio.ImageIO.read(in); } catch (IOException ioe) { GLApp.err("GLImage.makeBufferedImage(): " + ioe); } return bi; }
/** * Return the Image pixels in default Java int ARGB format. * * @return */ public static int[] getImagePixels(Image image) { int[] pixelsARGB = null; if (image != null) { int imgw = image.getWidth(null); int imgh = image.getHeight(null); pixelsARGB = new int[imgw * imgh]; PixelGrabber pg = new PixelGrabber(image, 0, 0, imgw, imgh, pixelsARGB, 0, imgw); try { pg.grabPixels(); } catch (Exception e) { GLApp.err("Pixel Grabbing interrupted!"); return null; } } return pixelsARGB; }
/** * 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); } } }