40package com.jogamp.opengl.util.texture.spi;
44import java.nio.channels.FileChannel;
46import com.jogamp.opengl.*;
47import com.jogamp.common.util.IOUtil;
48import com.jogamp.opengl.util.texture.*;
72 throw new GLException(
"Unsupported magic: "+magic+
", should be 0 (auto), 6 (PPM) or 7 (PAM)");
89 final int magic_old = magic;
93 if (
PPM.equals(IOUtil.getFileSuffix(file))) {
95 }
else if (
PAM.equals(IOUtil.getFileSuffix(file))) {
102 res = writeImpl(file, data);
109 private boolean writeImpl(
final File file,
final TextureData data)
throws IOException {
110 int pixelFormat = data.getPixelFormat();
111 final int pixelType = data.getPixelType();
119 ByteBuffer buf = (ByteBuffer) data.getBuffer();
121 buf = (ByteBuffer) data.getMipmapData()[0];
125 final int comps = ( pixelFormat == GL.GL_RGBA || pixelFormat == GL.GL_BGRA ) ? 4 : 3 ;
129 for (
int i = 0; i < buf.remaining(); i += comps) {
130 final byte red = buf.get(i + 0);
131 final byte blue = buf.get(i + 2);
132 buf.put(i + 0, blue);
136 data.setPixelFormat(pixelFormat);
139 if(magic==6 && comps==4) {
140 throw new IOException(
"NetPbmTextureWriter magic 6 (PPM) doesn't RGBA pixel format, use magic 7 (PAM)");
143 final FileOutputStream fos = IOUtil.getFileOutputStream(file,
true);
145 final StringBuilder header =
new StringBuilder();
147 header.append(magic);
150 header.append(
"WIDTH ");
152 header.append(data.getWidth());
154 header.append(
"\nHEIGHT ");
158 header.append(data.getHeight());
160 header.append(
"\nDEPTH ");
161 header.append(comps);
162 header.append(
"\nMAXVAL 255\nTUPLTYPE ");
164 header.append(
"RGB_ALPHA");
166 header.append(
"RGB");
168 header.append(
"\nENDHDR\n");
170 header.append(
"\n255\n");
173 fos.write(header.toString().getBytes());
175 final FileChannel fosc = fos.getChannel();
184 throw new IOException(
"NetPbmTextureWriter writer doesn't support this pixel format / type (only GL_RGB/A + bytes)");
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Represents the data for an OpenGL texture.
static final String PAM
Constant which can be used as a file suffix to indicate a PAM file, NetPbm magic 7 - binary RGB and R...
static final String PPM
Constant which can be used as a file suffix to indicate a PAM file, NetPbm magic 6 - binary RGB.
NetPbmTextureWriter(final int magic)
supported magic values are:
boolean write(final File file, final TextureData data)
Writes the given TextureData to the passed file.
static final int GL_BGRA
GL_VERSION_1_2, GL_IMG_read_format, GL_APPLE_texture_format_BGRA8888, GL_EXT_texture_format_BGRA8888,...
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_BGR
GL_VERSION_1_2, GL_EXT_bgra Alias for: GL_BGR_EXT Define "GL_BGR" with expression '0x80E0',...
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.