package demos.hdr; import java.io.*; import java.nio.*; import net.java.games.jogl.*; public class HDRTexture { private RGBE.Header header; private byte[] m_data; private float[] m_floatdata; private int m_width, m_height; private float m_max_r, m_max_g, m_max_b; private float m_min_r, m_min_g, m_min_b; private float m_max; private int m_target; public HDRTexture(String filename) throws IOException { this(new FileInputStream(filename)); } public HDRTexture(InputStream in) throws IOException { DataInputStream datain = new DataInputStream(new BufferedInputStream(in)); header = RGBE.readHeader(datain); m_width = header.getWidth(); m_height = header.getHeight(); m_data = new byte[m_width * m_height * 4]; RGBE.readPixelsRawRLE(datain, m_data, 0, m_width, m_height); System.err.println("Loaded HDR image " + m_width + " x " + m_height); } public byte[] getData() { return m_data; } public int getPixelIndex(int x, int y) { return ((m_width * (m_height - 1 - y)) + x) * 4; } public float[] getFloatData() { return m_floatdata; } public int getPixelFloatIndex(int x, int y) { return ((m_width * (m_height - 1 - y)) + x) * 3; } public void analyze() { m_max_r = m_max_g = m_max_b = 0.0f; m_min_r = m_min_g = m_min_b = 1e10f; int mine = 255; int maxe = 0; int ptr = 0; float[] rgb = new float[3]; for(int i=0; i maxe) maxe = e; RGBE.rgbe2float(rgb, m_data, ptr); float r = rgb[0]; float g = rgb[1]; float b = rgb[2]; if (r > m_max_r) m_max_r = r; if (g > m_max_g) m_max_g = g; if (b > m_max_b) m_max_b = b; if (r < m_min_r) m_min_r = r; if (g < m_min_g) m_min_g = g; if (b < m_min_b) m_min_b = b; ptr += 4; } System.err.println("max intensity: " + m_max_r + " " + m_max_g + " " + m_max_b); System.err.println("min intensity: " + m_min_r + " " + m_min_g + " " + m_min_b); System.err.println("max e: " + maxe + " = " + RGBE.ldexp(1.0, maxe-128)); System.err.println("min e: " + mine + " = " + RGBE.ldexp(1.0, mine-128)); m_max = m_max_r; if (m_max_g > m_max) m_max = m_max_g; if (m_max_b > m_max) m_max = m_max_b; System.err.println("max: " + m_max); } /** Converts from RGBE to floating-point RGB data. */ public void convert() { m_floatdata = new float [m_width*m_height*3]; int src = 0; int dest = 0; float[] rgb = new float[3]; for(int i=0; i max) x = max; return (float) Math.sqrt(x / max); } public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { HDRTexture tex = new HDRTexture(args[i]); tex.analyze(); } catch (IOException e) { e.printStackTrace(); } } } }