28package com.jogamp.opengl.util.texture.spi;
30import java.io.IOException;
31import java.io.InputStream;
32import java.nio.ByteBuffer;
34import com.jogamp.opengl.GL;
36import jogamp.opengl.Debug;
37import jogamp.opengl.util.jpeg.JPEGDecoder;
39import com.jogamp.common.nio.Buffers;
40import com.jogamp.opengl.util.texture.TextureData.ColorSpace;
43 private static final boolean DEBUG = Debug.debug(
"JPEGImage");
63 private static class JPEGColorSink
implements JPEGDecoder.ColorSink {
64 int width=0, height=0;
65 int sourceComponents=0;
67 int storageComponents;
69 ByteBuffer data =
null;
72 this.storageCS = storageCM;
76 storageComponents = 3;
79 throw new IllegalArgumentException(
"Unsupported storage color-space: "+storageCS);
84 public final ColorSpace allocate(
final int width,
final int height,
final ColorSpace sourceCM,
final int sourceComponents)
throws RuntimeException {
87 this.sourceComponents = sourceComponents;
88 this.sourceCS = sourceCM;
89 this.data = Buffers.newDirectByteBuffer(width * height * storageComponents);
94 public final void storeRGB(
final int x,
final int y,
final byte r,
final byte g,
final byte b) {
95 int i = ( ( height - y - 1 ) * width + x ) * storageComponents;
103 public final void store2(
final int x,
final int y,
final byte c1,
final byte c2) {
104 throw new RuntimeException(
"not supported yet");
108 public final void storeYCbCr(
final int x,
final int y,
final byte Y,
final byte Cb,
final byte Cr) {
109 int i = ( ( height - y - 1 ) * width + x ) * storageComponents;
117 return "JPEGPixels["+width+
"x"+height+
", sourceComp "+sourceComponents+
", sourceCS "+sourceCS+
", storageCS "+storageCS+
", storageComp "+storageComponents+
"]";
121 private JPEGImage(
final InputStream in,
final ColorSpace cs)
throws IOException {
122 pixelStorage =
new JPEGColorSink(cs);
123 final JPEGDecoder decoder =
new JPEGDecoder();
125 pixelWidth = decoder.getWidth();
126 pixelHeight = decoder.getHeight();
127 decoder.getPixel(pixelStorage, pixelWidth, pixelHeight);
128 data = pixelStorage.data;
129 final boolean hasAlpha =
false;
132 glFormat = GL.GL_RGB;
133 reversedChannels =
false;
135 System.err.println(
"JPEGImage: alpha "+hasAlpha+
", bytesPerPixel "+bytesPerPixel+
136 ", pixels "+pixelWidth+
"x"+pixelHeight+
", glFormat 0x"+Integer.toHexString(glFormat));
137 System.err.println(
"JPEGImage: "+decoder);
138 System.err.println(
"JPEGImage: "+pixelStorage);
142 private final JPEGColorSink pixelStorage;
143 private final int pixelWidth, pixelHeight, glFormat, bytesPerPixel;
144 private final boolean reversedChannels;
145 private final ByteBuffer data;
176 public String
toString() {
return "JPEGImage["+pixelWidth+
"x"+pixelHeight+
", bytesPerPixel "+bytesPerPixel+
", reversedChannels "+reversedChannels+
", "+pixelStorage+
", "+data+
"]"; }
int getComponentCount()
Returns the number of components of the pixel data.
ByteBuffer getData()
Returns the raw data for this texture in the correct (bottom-to-top) order for calls to glTexImage2D.
int getBytesPerPixel()
Returns the bytes per pixel.
ColorSpace getColorSpace()
Returns the color space of the pixel data.
int getGLFormat()
Returns the OpenGL format for this texture; e.g.
int getWidth()
Returns the width of the image.
int getHeight()
Returns the height of the image.
int getGLType()
Returns the OpenGL data type: GL.GL_UNSIGNED_BYTE.
static JPEGImage read(final InputStream in, final ColorSpace cs)
Reads a JPEG image from the specified InputStream, using the given color space for storage.
boolean getHasReversedChannels()
Returns true if data has the channels reversed to BGR or BGRA, otherwise RGB or RGBA is expected.
static JPEGImage read(final InputStream in)
Reads a JPEG image from the specified InputStream, using the ColorSpace#RGB.
ColorSpace of pixel data.
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...