top of page

1. HDR file and its read-in program by JAVA

  • Peng Chen
  • Oct 29, 2015
  • 3 min read

1. What is a HDR file?

In this writing, high dynamic range (HDR) file means the file whose expansion name is HDR. Actually, its real name is Radiance RGBE. RGBE is an image format created by Greg Ward[1]. As we known, there are three channels in the color image which are red, green and blue. If we use 32-bit floating point to store each channel, we need 96 bits to store one pixel's RGB information. However, Greg Ward created the rgbe way to store the 96 bits information we need. In the rgbe format, each pixel need 32 bits which is 4 bytes. One byte mantissa number for each red, green and blue channels and a shared one byte exponent. So for each pixel, 32 bits is enough which saves 64 bits for each pixel. It's a huge saving because of the huge pixel numbers in a decent resolution image (such as 256*256).[2-4]

In addition, for the aim of compression data, the run length encoding (RLE) way may be used when creating the .hdr file. It depends on the format.

2. What is in the HDR file?

There are three main parts in the HDR file. The first one is the header information. The second one is the resolution information. And the last part is pixel data.

(1) Header

The first bytes in a legal HDR file are "#?RADIANCE". After that, there are serveral magic numbers present the file's all kinds of information. It seems the format is necessary and others are optional.

  • Format

The format should be "32-bit_rle_rgbe". It means that this HDR file is used the RLE way to compress data. And each pixel has 32-bit data.

  • Exposure

  • Owner

  • Primaries

  • and so on

(2) Size information

After the first part's magic numbers, there is the image size information. The common format should be "-Y Rownum +X Colnum". "-Y 1024 +X 2048" means the image resolution is 1024*2048 and the stored data direction is from left to right and from up to down. This information is very important for getting the image size information without reading all the pixels. The java official image IO libarary cannot read in the HDR format file. However, sepecfic plug-in program can be added for reading HDR file. Therefore, if the client needs only the size information, there is no need to read in all the pixels.

(3) Pixel data

In this writing, we only talk about the HDR file whose format is "32-bit_rle_rgbe". As we mentioned, each pixel use 4 bytes to store data. Three bytes are the mantissas for red, green and blue channels and one byte is the shared exponent. In addition, the run length encoding (RLE) is used to compress the data. By the way, the data is written row by row. And in each row, the first four bytes are fixed. The first and second one is 2. And the third and fourth present the image's width information.

  • The relationship between the 32-bit rgbe and the 96 bits floating points for RGB.

I use the lowcase rgbe to present the byte number in 32-bit rgbe and RGB to present the 32-bit floating number in 96 bits RGB [5].

1. 32-bit rgbe -> 96 bits for RGB.

if the fourth byte e=0, R=G=B=0;

else:

R=r*2^(e-128-8);

G=g*2^(e-128-8);

B=b*2^(e-128-8);

2. 96 bits for RGB -> 32-bit rgbe

X=max(R,G,B); Then, X=m*2^n(0.5<m<=1)

if(X<1e-32) r=g=b=e=0;

else:

r=R*m*256/X;

g=G*m*256/X;

b=B*m*256/X;

e=n+128;

  • The run length encoding (RLE)

For the HDR file whose format is "32-bits_rle_rgbe", the RLE way is sepecific.

1. Duplicate:

If there are duplicate bytes, it uses two bytes to present them. If there are ten duplicate 99. The first presenting byte is (128+10) and the second byte is the duplicate number 99. So it uses (138) (99) to present ten consective 99.

2. Distinct:

If there are 50 distinct bytes, it uses 51 bytes to present them. The first byte is 50 which means that there are 50 consective distince numbers as following. And the following 50 bytes are the 50 distinct numbers. Please note that the first byte can not be larger than 128. If there are more than 128 consective distince numbers, we need divide them into several parts to present. Because if the first presenting byte is larger than 128, it means there are duplicate numbers.

  • The pixel data

The only thing need pay attention to is the first four bytes in a row's data. If the data is written row by row. And in each row, the first four bytes are fixed. The first and second one is 2. And the third and fourth present the image's width information. The third byte left shift 8 bits add the fourth byte is the width of the image.

3. Codes for reading in the HDR file in Java.

Github: https://github.com/aicp7/HDR_file_readin.git

4. References:

[1]. http://radsite.lbl.gov/radiance/

[2]. http://www.anyhere.com/gward/hdrenc/

[3]. http://paulbourke.net/dataformats/pic/

[4]. http://www.graphics.cornell.edu/~bjw/rgbe.html

[5]. http://blog.csdn.net/zzzgoogle/article/details/1615693

5. Some HDR files

 
 
 

Comments


bottom of page