import java.nio.Buffer; import java.nio.FloatBuffer; public class OpenExrReaderMain { static { System.loadLibrary("OpenExrReader"); } private OpenExrReader openExrReader; private int imgHeight; private int imgWidth; private Texture redTexture; private Texture greenTexture; private Texture blueTexture; private Texture rgbTexture; private FloatBuffer redPixelBuffer; private FloatBuffer greenPixelBuffer; private FloatBuffer bluePixelBuffer; private FloatBuffer rgbPixelBuffer; public static final int RGB_CHANNEL = 0; public static final int R_CHANNEL = 1; public static final int G_CHANNEL = 2; public static final int B_CHANNEL = 3; public OpenExrReaderMain() { openExrReader = new OpenExrReader(); imgHeight = 0; imgWidth = 0; } public void readExrFile(String filePath) { openExrReader.readExrFile(filePath); imgHeight = openExrReader.getHeight(); imgWidth = openExrReader.getWidth(); } public int getWidth() { return imgWidth; } public int getHeight() { return imgHeight; } private void fillBuffer(int startX, int startY, int height, int width, boolean storeAlphaChannel) { int valuesPerPixel = 0; if(storeAlphaChannel) { valuesPerPixel = 4; } else { valuesPerPixel = 3; } redPixelBuffer = FloatBuffer.allocate(height * width * valuesPerPixel); greenPixelBuffer = FloatBuffer.allocate(height * width * valuesPerPixel); bluePixelBuffer = FloatBuffer.allocate(height * width * valuesPerPixel); rgbPixelBuffer = FloatBuffer.allocate(height * width * valuesPerPixel); if(startX + width <= imgWidth && startY + height <= imgHeight) { for(int i = startY; i < height; i++) { for (int j = startX; j < width; j++) { Rgba pixelData = openExrReader.getScruct(i, j); redPixelBuffer.put(pixelData.getR()); redPixelBuffer.put(0.0f); redPixelBuffer.put(0.0f); greenPixelBuffer.put(0.0f); greenPixelBuffer.put(pixelData.getG()); greenPixelBuffer.put(0.0f); bluePixelBuffer.put(0,0f); bluePixelBuffer.put(0.0f); bluePixelBuffer.put(pixelData.getB()); rgbPixelBuffer.put(pixelData.getR()); rgbPixelBuffer.put(pixelData.getG()); rgbPixelBuffer.put(pixelData.getB()); if(storeAlphaChannel) { redPixelBuffer.put(pixelData.getA()); greenPixelBuffer.put(pixelData.getA()); bluePixelBuffer.put(pixelData.getA()); rgbPixelBuffer.put(pixelData.getA()); } } } } } public void createOpenExrTextures(int startX, int startY, int height, int width, boolean storeAlphaChannel) { this.fillBuffer(startX, startY, height, width, storeAlphaChannel); if(redPixelBuffer.capacity() > 0) { redTexture = new Texture(redPixelBuffer, width, height); } if(greenPixelBuffer.capacity() > 0) { greenTexture = new Texture(greenPixelBuffer, width, height); } if(bluePixelBuffer.capacity() > 0) { blueTexture = new Texture(bluePixelBuffer, width, height); } if(rgbPixelBuffer.capacity() > 0) { rgbTexture = new Texture(rgbPixelBuffer, width, height); } } public Texture getRedTexture() { return redTexture; } public Texture getGreenTexture() { return greenTexture; } public Texture getBlueTexture() { return blueTexture; } public Texture getRgbTexture() { return rgbTexture; } private FloatBuffer getImageTile(int startX, int startY, int height, int width, boolean storeAlphaChannel, int channel) { int valuesPerPixel = 0; if(storeAlphaChannel) { valuesPerPixel = 4; } else { valuesPerPixel = 3; } FloatBuffer pixelBuffer = FloatBuffer.allocate(height * width * valuesPerPixel); if(startX + width <= imgWidth && startY + height <= imgHeight) { for(int i = startY; i < height; i++) { for (int j = startX; j < width; j++) { Rgba pixelData = openExrReader.getScruct(i, j); switch(channel) { case RGB_CHANNEL: pixelBuffer.put(pixelData.getR()); pixelBuffer.put(pixelData.getG()); pixelBuffer.put(pixelData.getB()); break; case R_CHANNEL: pixelBuffer.put(pixelData.getR()); pixelBuffer.put(0.0f); pixelBuffer.put(0.0f); break; case G_CHANNEL: pixelBuffer.put(0.0f); pixelBuffer.put(pixelData.getG()); pixelBuffer.put(0.0f); break; case B_CHANNEL: pixelBuffer.put(0.0f); pixelBuffer.put(0.0f); pixelBuffer.put(pixelData.getB()); break; } if(storeAlphaChannel) { pixelBuffer.put(pixelData.getA()); } } } } return pixelBuffer; } public OpenExrReaderMain.Texture getOpenExrTexture(int startX, int startY, int height, int width, boolean storeAlphaChannel, int channel) { FloatBuffer pixelBuffer = this.getImageTile(startX, startY, height, width, storeAlphaChannel, channel); if(pixelBuffer.capacity() > 0) { return new Texture(pixelBuffer, width, height); } else { return null; } } public static class Texture { private Buffer pixels; private int width; private int height; public Texture(Buffer pixels, int width, int height) { this.height = height; this.pixels = pixels; this.width = width; } public int getHeight() { return height; } public Buffer getPixels() { return pixels; } public int getWidth() { return width; } } }