40package com.jogamp.opengl.util.texture.spi.awt;
42import java.awt.Graphics;
43import java.awt.image.*;
46import javax.imageio.*;
48import com.jogamp.opengl.*;
50import com.jogamp.common.util.IOUtil;
51import com.jogamp.opengl.util.awt.*;
52import com.jogamp.opengl.util.texture.*;
53import com.jogamp.opengl.util.texture.spi.*;
57 public boolean write(
final File file,
59 final int pixelFormat = data.getPixelFormat();
60 final int pixelType = data.getPixelType();
67 BufferedImage image =
new BufferedImage(data.getWidth(), data.getHeight(),
69 BufferedImage.TYPE_3BYTE_BGR :
70 BufferedImage.TYPE_4BYTE_ABGR);
71 final byte[] imageData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
72 ByteBuffer buf = (ByteBuffer) data.getBuffer();
74 buf = (ByteBuffer) data.getMipmapData()[0];
82 for (
int i = 0; i < imageData.length; i += 3) {
83 final byte red = imageData[i + 0];
84 final byte blue = imageData[i + 2];
85 imageData[i + 0] = blue;
86 imageData[i + 2] = red;
89 for (
int i = 0; i < imageData.length; i += 4) {
90 final byte red = imageData[i + 0];
91 final byte green = imageData[i + 1];
92 final byte blue = imageData[i + 2];
93 final byte alpha = imageData[i + 3];
94 imageData[i + 0] = alpha;
95 imageData[i + 1] = blue;
96 imageData[i + 2] = green;
97 imageData[i + 3] = red;
106 image.getType() == BufferedImage.TYPE_4BYTE_ABGR) {
107 final BufferedImage tmpImage =
new BufferedImage(image.getWidth(), image.getHeight(),
108 BufferedImage.TYPE_3BYTE_BGR);
109 final Graphics g = tmpImage.getGraphics();
110 g.drawImage(image, 0, 0,
null);
115 return ImageIO.write(image, IOUtil.getFileSuffix(file), file);
118 throw new IOException(
"ImageIO writer doesn't support this pixel format / type (only GL_RGB/A + bytes)");
Utilities for dealing with images.
static void flipImageVertically(final BufferedImage image)
Flips the supplied BufferedImage vertically.
Represents the data for an OpenGL texture.
static final String JPG
Constant which can be used as a file suffix to indicate a JPEG file, value {@value}.
boolean write(final File file, final TextureData data)
Writes the given TextureData to the passed file.
static final int GL_RGB
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGB" with expression ...
static final int GL_RGBA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGBA" with expression...
static final int GL_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
static final int GL_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_BYTE" with expression...
Plug-in interface to TextureIO to support writing OpenGL textures to new file formats.