/** * 将LImage转化为LTextureData * * @param image * @return */ private void create(LImage image) { if (image == null) { return; } int srcWidth = image.getWidth(); int srcHeight = image.getHeight(); this.hasAlpha = image.hasAlpha(); if (GLEx.isPowerOfTwo(srcWidth) && GLEx.isPowerOfTwo(srcHeight)) { this.width = srcWidth; this.height = srcHeight; this.texHeight = srcHeight; this.texWidth = srcWidth; this.source = image.getByteBuffer(); this.pixels = image.getPixels(); if (image.isAutoDispose()) { image.dispose(); image = null; } return; } int texWidth = GLEx.toPowerOfTwo(srcWidth); int texHeight = GLEx.toPowerOfTwo(srcHeight); this.width = srcWidth; this.height = srcHeight; this.texHeight = texHeight; this.texWidth = texWidth; LImage texImage = new LImage(texWidth, texHeight, hasAlpha); LGraphics g = texImage.getLGraphics(); g.drawImage(image, 0, 0); if (this.height < texHeight - 1) { copyArea(texImage, g, 0, 0, width, 1, 0, texHeight - 1); copyArea(texImage, g, 0, height - 1, width, 1, 0, 1); } if (this.width < texWidth - 1) { copyArea(texImage, g, 0, 0, 1, height, texWidth - 1, 0); copyArea(texImage, g, width - 1, 0, 1, height, 1, 0); } this.source = texImage.getByteBuffer(); this.pixels = texImage.getPixels(); if (image.isAutoDispose()) { image.dispose(); image = null; } }
/** * 将BufferedImage转化为LTextureData * * @param image */ private void create(BufferedImage image) { int srcWidth = image.getWidth(); int srcHeight = image.getHeight(); this.hasAlpha = image.getColorModel().hasAlpha(); if (GLEx.isPowerOfTwo(srcWidth) && GLEx.isPowerOfTwo(srcHeight)) { this.width = srcWidth; this.height = srcHeight; this.texHeight = srcHeight; this.texWidth = srcWidth; this.source = BufferUtils.createByteBuffer( (byte[]) image .getRaster() .getDataElements(0, 0, image.getWidth(), image.getHeight(), null)); this.pixels = GraphicsUtils.getPixels(image); return; } int texWidth = GLEx.toPowerOfTwo(srcWidth); int texHeight = GLEx.toPowerOfTwo(srcHeight); this.width = srcWidth; this.height = srcHeight; this.texHeight = texHeight; this.texWidth = texWidth; BufferedImage texImage = new BufferedImage( texWidth, texHeight, hasAlpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = texImage.createGraphics(); g.drawImage(image, 0, 0, null); if (height < texHeight - 1) { copyArea(texImage, g, 0, 0, width, 1, 0, texHeight - 1); copyArea(texImage, g, 0, height - 1, width, 1, 0, 1); } if (width < texWidth - 1) { copyArea(texImage, g, 0, 0, 1, height, texWidth - 1, 0); copyArea(texImage, g, width - 1, 0, 1, height, 1, 0); } source = BufferUtils.createByteBuffer( (byte[]) texImage .getRaster() .getDataElements(0, 0, texImage.getWidth(), texImage.getHeight(), null)); this.pixels = GraphicsUtils.getPixels(texImage); if (texImage != null) { texImage.flush(); texImage = null; } }