aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/opengl/util/texture/spi
diff options
context:
space:
mode:
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util/texture/spi')
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java78
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java13
2 files changed, 50 insertions, 41 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java
index ae9618490..216c994c0 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java
@@ -41,6 +41,7 @@ package com.jogamp.opengl.util.texture.spi;
import java.io.*;
import java.nio.*;
+import java.nio.channels.FileChannel;
import javax.media.opengl.*;
@@ -81,10 +82,11 @@ public class NetPbmTextureWriter implements TextureWriter {
public String getSuffix() { return (magic==6)?PPM:PAM; }
- public boolean write(File file,
- TextureData data) throws IOException {
-
- // file suffix selection
+ public boolean write(File file, TextureData data) throws IOException {
+ boolean res;
+ final int magic_old = magic;
+
+ // file suffix selection
if (0==magic) {
if (PPM.equals(IOUtil.getFileSuffix(file))) {
magic = 6;
@@ -93,24 +95,52 @@ public class NetPbmTextureWriter implements TextureWriter {
} else {
return false;
}
+ }
+ try {
+ res = writeImpl(file, data);
+ } finally {
+ magic = magic_old;
}
-
- final int pixelFormat = data.getPixelFormat();
+ return res;
+ }
+
+ private boolean writeImpl(File file, TextureData data) throws IOException {
+ int pixelFormat = data.getPixelFormat();
final int pixelType = data.getPixelType();
if ((pixelFormat == GL.GL_RGB ||
- pixelFormat == GL.GL_RGBA) &&
+ pixelFormat == GL.GL_RGBA ||
+ pixelFormat == GL2.GL_BGR ||
+ pixelFormat == GL.GL_BGRA ) &&
(pixelType == GL.GL_BYTE ||
pixelType == GL.GL_UNSIGNED_BYTE)) {
- int comps = ( pixelFormat == GL.GL_RGBA ) ? 4 : 3 ;
+ ByteBuffer buf = (ByteBuffer) data.getBuffer();
+ if (null == buf ) {
+ buf = (ByteBuffer) data.getMipmapData()[0];
+ }
+ buf.rewind();
+
+ int comps = ( pixelFormat == GL.GL_RGBA || pixelFormat == GL.GL_BGRA ) ? 4 : 3 ;
+
+ if( pixelFormat == GL2.GL_BGR || pixelFormat == GL.GL_BGRA ) {
+ // Must reverse order of red and blue channels to get correct results
+ for (int i = 0; i < buf.remaining(); i += comps) {
+ byte red = buf.get(i + 0);
+ byte blue = buf.get(i + 2);
+ buf.put(i + 0, blue);
+ buf.put(i + 2, red);
+ }
+ pixelFormat = ( 4 == comps ) ? GL.GL_RGBA : GL.GL_RGB;
+ data.setPixelFormat(pixelFormat);
+ }
if(magic==6 && comps==4) {
throw new IOException("NetPbmTextureWriter magic 6 (PPM) doesn't RGBA pixel format, use magic 7 (PAM)");
}
FileOutputStream fos = new FileOutputStream(file);
-
- StringBuffer header = new StringBuffer();
+
+ StringBuilder header = new StringBuilder();
header.append("P");
header.append(magic);
header.append("\n");
@@ -139,30 +169,16 @@ public class NetPbmTextureWriter implements TextureWriter {
}
fos.write(header.toString().getBytes());
-
- ByteBuffer buf = (ByteBuffer) data.getBuffer();
- if (buf == null) {
- buf = (ByteBuffer) data.getMipmapData()[0];
- }
- buf.rewind();
-
- byte[] bufArray = null;
-
- try {
- bufArray = buf.array();
- } catch (Throwable t) {}
- if(null==bufArray) {
- bufArray = new byte[data.getWidth()*data.getHeight()*comps];
- buf.get(bufArray);
- buf.rewind();
- }
-
- fos.write(bufArray);
+
+ FileChannel fosc = fos.getChannel();
+ fosc.write(buf);
+ fosc.force(true);
+ fosc.close();
fos.close();
+ buf.rewind();
return true;
- }
-
+ }
throw new IOException("NetPbmTextureWriter writer doesn't support this pixel format / type (only GL_RGB/A + bytes)");
}
}
diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java
index 16ba538b5..cf35df464 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java
@@ -43,9 +43,6 @@ import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import javax.media.opengl.*;
-import com.jogamp.opengl.util.*;
-import com.jogamp.opengl.util.texture.spi.*;
-import com.jogamp.opengl.util.texture.*;
/**
* Targa image reader and writer adapted from sources of the <a href =
@@ -144,8 +141,6 @@ public class TGAImage {
}
Header(LEDataInputStream in) throws IOException {
- int ret;
-
tgaType = TYPE_OLD; // dont try and get footer.
// initial header fields
@@ -289,9 +284,7 @@ public class TGAImage {
*/
private void decodeRGBImageU24_32(LEDataInputStream dIn) throws IOException {
int i; // row index
- int j; // column index
int y; // output row index
- int raw; // index through the raw input buffer
int rawWidth = header.width() * (header.pixelDepth() / 8);
byte[] rawBuf = new byte[rawWidth];
byte[] tmpData = new byte[rawWidth * header.height()];
@@ -320,8 +313,8 @@ public class TGAImage {
assert header.pixelDepth() == 32;
bpp=4;
- if(gl.isGL2GL3()) {
- format = GL2GL3.GL_BGRA;
+ if( gl.getContext().isTextureFormatBGRA8888Available() ) {
+ format = GL.GL_BGRA;
} else {
format = GL.GL_RGBA;
swapBGR(tmpData, rawWidth, header.height(), bpp);
@@ -391,10 +384,10 @@ public class TGAImage {
buf.rewind();
chan.write(buf);
chan.write(data);
- data.rewind();
chan.force(true);
chan.close();
stream.close();
+ data.rewind();
}
/** Creates a TGAImage from data supplied by the end user. Shares