JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
NetPbmTextureWriter.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;
41
42import java.io.*;
43import java.nio.*;
44import java.nio.channels.FileChannel;
45
46import com.jogamp.opengl.*;
47import com.jogamp.common.util.IOUtil;
48import com.jogamp.opengl.util.texture.*;
49
50public class NetPbmTextureWriter implements TextureWriter {
51 int magic;
52
54 this(0); // auto
55 }
56
57 /**
58 * supported magic values are:<br>
59 * <pre>
60 * magic 0 - detect by file suffix (TextureIO compliant)
61 * magic 6 - PPM binary RGB
62 * magic 7 - PAM binary RGB or RGBA
63 * </pre>
64 */
65 public NetPbmTextureWriter(final int magic) {
66 switch(magic) {
67 case 0:
68 case 6:
69 case 7:
70 break;
71 default:
72 throw new GLException("Unsupported magic: "+magic+", should be 0 (auto), 6 (PPM) or 7 (PAM)");
73 }
74 this.magic = magic;
75 }
76
77 public int getMagic() { return magic; }
78
79 /** @see TextureIO#PPM */
80 public static final String PPM = TextureIO.PPM;
81 /** @see TextureIO#PAM */
82 public static final String PAM = TextureIO.PAM;
83
84 public String getSuffix() { return (magic==6)?PPM:PAM; }
85
86 @Override
87 public boolean write(final File file, final TextureData data) throws IOException {
88 boolean res;
89 final int magic_old = magic;
90
91 // file suffix selection
92 if (0==magic) {
93 if (PPM.equals(IOUtil.getFileSuffix(file))) {
94 magic = 6;
95 } else if (PAM.equals(IOUtil.getFileSuffix(file))) {
96 magic = 7;
97 } else {
98 return false;
99 }
100 }
101 try {
102 res = writeImpl(file, data);
103 } finally {
104 magic = magic_old;
105 }
106 return res;
107 }
108
109 private boolean writeImpl(final File file, final TextureData data) throws IOException {
110 int pixelFormat = data.getPixelFormat();
111 final int pixelType = data.getPixelType();
112 if ((pixelFormat == GL.GL_RGB ||
113 pixelFormat == GL.GL_RGBA ||
114 pixelFormat == GL.GL_BGR ||
115 pixelFormat == GL.GL_BGRA ) &&
116 (pixelType == GL.GL_BYTE ||
117 pixelType == GL.GL_UNSIGNED_BYTE)) {
118
119 ByteBuffer buf = (ByteBuffer) data.getBuffer();
120 if (null == buf ) {
121 buf = (ByteBuffer) data.getMipmapData()[0];
122 }
123 buf.rewind();
124
125 final int comps = ( pixelFormat == GL.GL_RGBA || pixelFormat == GL.GL_BGRA ) ? 4 : 3 ;
126
127 if( pixelFormat == GL.GL_BGR || pixelFormat == GL.GL_BGRA ) {
128 // Must reverse order of red and blue channels to get correct results
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);
133 buf.put(i + 2, red);
134 }
135 pixelFormat = ( 4 == comps ) ? GL.GL_RGBA : GL.GL_RGB;
136 data.setPixelFormat(pixelFormat);
137 }
138
139 if(magic==6 && comps==4) {
140 throw new IOException("NetPbmTextureWriter magic 6 (PPM) doesn't RGBA pixel format, use magic 7 (PAM)");
141 }
142
143 final FileOutputStream fos = IOUtil.getFileOutputStream(file, true);
144
145 final StringBuilder header = new StringBuilder();
146 header.append("P");
147 header.append(magic);
148 header.append("\n");
149 if(7==magic) {
150 header.append("WIDTH ");
151 }
152 header.append(data.getWidth());
153 if(7==magic) {
154 header.append("\nHEIGHT ");
155 } else {
156 header.append(" ");
157 }
158 header.append(data.getHeight());
159 if(7==magic) {
160 header.append("\nDEPTH ");
161 header.append(comps);
162 header.append("\nMAXVAL 255\nTUPLTYPE ");
163 if(pixelFormat == GL.GL_RGBA) {
164 header.append("RGB_ALPHA");
165 } else {
166 header.append("RGB");
167 }
168 header.append("\nENDHDR\n");
169 } else {
170 header.append("\n255\n");
171 }
172
173 fos.write(header.toString().getBytes());
174
175 final FileChannel fosc = fos.getChannel();
176 fosc.write(buf);
177 fosc.force(true);
178 fosc.close();
179 fos.close();
180 buf.rewind();
181
182 return true;
183 }
184 throw new IOException("NetPbmTextureWriter writer doesn't support this pixel format / type (only GL_RGB/A + bytes)");
185 }
186}
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...
Definition: TextureIO.java:187
static final String PPM
Constant which can be used as a file suffix to indicate a PAM file, NetPbm magic 6 - binary RGB.
Definition: TextureIO.java:193
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,...
Definition: GL.java:404
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_BGR
GL_VERSION_1_2, GL_EXT_bgra Alias for: GL_BGR_EXT Define "GL_BGR" with expression '0x80E0',...
Definition: GL.java:399
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.