JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
IIOTextureWriter.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 *
32 * You acknowledge that this software is not designed or intended for use
33 * in the design, construction, operation or maintenance of any nuclear
34 * facility.
35 *
36 * Sun gratefully acknowledges that this software was originally authored
37 * and developed by Kenneth Bradley Russell and Christopher John Kline.
38 */
39
40package com.jogamp.opengl.util.texture.spi.awt;
41
42import java.awt.Graphics;
43import java.awt.image.*;
44import java.io.*;
45import java.nio.*;
46import javax.imageio.*;
47
48import com.jogamp.opengl.*;
49
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.*;
54
55public class IIOTextureWriter implements TextureWriter {
56 @Override
57 public boolean write(final File file,
58 final TextureData data) throws IOException {
59 final int pixelFormat = data.getPixelFormat();
60 final int pixelType = data.getPixelType();
61 if ((pixelFormat == GL.GL_RGB ||
62 pixelFormat == GL.GL_RGBA) &&
63 (pixelType == GL.GL_BYTE ||
64 pixelType == GL.GL_UNSIGNED_BYTE)) {
65 // Convert TextureData to appropriate BufferedImage
66 // FIXME: almost certainly not obeying correct pixel order
67 BufferedImage image = new BufferedImage(data.getWidth(), data.getHeight(),
68 (pixelFormat == GL.GL_RGB) ?
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();
73 if (buf == null) {
74 buf = (ByteBuffer) data.getMipmapData()[0];
75 }
76 buf.rewind();
77 buf.get(imageData);
78 buf.rewind();
79
80 // Swizzle image components to be correct
81 if (pixelFormat == GL.GL_RGB) {
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;
87 }
88 } else {
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;
98 }
99 }
100
101 // Flip image vertically for the user's convenience
103
104 // Happened to notice that writing RGBA images to JPEGS is broken
105 if (TextureIO.JPG.equals(IOUtil.getFileSuffix(file)) &&
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);
111 g.dispose();
112 image = tmpImage;
113 }
114
115 return ImageIO.write(image, IOUtil.getFileSuffix(file), file);
116 }
117
118 throw new IOException("ImageIO writer doesn't support this pixel format / type (only GL_RGB/A + bytes)");
119 }
120}
Utilities for dealing with images.
Definition: ImageUtil.java:47
static void flipImageVertically(final BufferedImage image)
Flips the supplied BufferedImage vertically.
Definition: ImageUtil.java:53
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}.
Definition: TextureIO.java:166
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 ...
Definition: GL.java:374
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...
Definition: GL.java:150
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...
Definition: GL.java:284
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...
Definition: GL.java:159
Plug-in interface to TextureIO to support writing OpenGL textures to new file formats.