/** Scale this GLImage so width and height are powers of 2. Recreate pixels and pixelBuffer. */ public void convertToPowerOf2() { // make BufferedImage from original pixels BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); image.setRGB(0, 0, w, h, pixels, 0, w); // scale into new image BufferedImage scaledImg = convertToPowerOf2(image); // resample pixel data w = scaledImg.getWidth(null); h = scaledImg.getHeight(null); pixels = getImagePixels(scaledImg); // pixels in default Java ARGB format pixelBuffer = convertImagePixelsRGBA(pixels, w, h, false); // convert to bytes in RGBA format textureW = GLApp.getPowerOfTwoBiggerThan(w); // the texture size big enough to hold this image textureH = GLApp.getPowerOfTwoBiggerThan(h); // the texture size big enough to hold this image }
/** * Scale the given BufferedImage to width and height that are powers of two. Return the new scaled * BufferedImage. */ public static BufferedImage convertToPowerOf2(BufferedImage bsrc) { // find powers of 2 equal to or greater than current dimensions int newW = GLApp.getPowerOfTwoBiggerThan(bsrc.getWidth()); int newH = GLApp.getPowerOfTwoBiggerThan(bsrc.getHeight()); if (newW == bsrc.getWidth() && newH == bsrc.getHeight()) { return bsrc; // no change necessary } else { AffineTransform at = AffineTransform.getScaleInstance( (double) newW / bsrc.getWidth(), (double) newH / bsrc.getHeight()); BufferedImage bdest = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bdest.createGraphics(); g.drawRenderedImage(bsrc, at); return bdest; } }
/** * Load an image from the given filename. If convertToPow2 is true then convert the image to a * power of two. Store pixels as ARGB ints in the pixels array and as RGBA bytes in the * pixelBuffer ByteBuffer. Hold onto image width/height. * * @param imgName */ public boolean makeGLImage(BufferedImage tmpi, boolean flipYaxis, boolean convertToPow2) { if (tmpi != null) { if (flipYaxis) { tmpi = flipY(tmpi); } if (convertToPow2) { tmpi = convertToPowerOf2(tmpi); } w = tmpi.getWidth(null); h = tmpi.getHeight(null); pixels = getImagePixels(tmpi); // pixels in default Java ARGB format pixelBuffer = convertImagePixelsRGBA(pixels, w, h, false); // convert to bytes in RGBA format textureW = GLApp.getPowerOfTwoBiggerThan(w); // the texture size big enough to hold this image textureH = GLApp.getPowerOfTwoBiggerThan(h); // the texture size big enough to hold this image // GLApp.msg("GLImage: loaded " + imgName + ", width=" + w + " height=" + h); return true; } else { // GLApp.err("GLImage: FAILED TO LOAD IMAGE " + imgName); pixels = null; pixelBuffer = null; h = w = 0; return false; } }