Radiance RGBE文件格式 C语言读写类

2.2  Radiance RGBE文件格式
         RGBE文件的扩展名为.hdr,RGBE正式名称为Radiance RGBE格式。这个本来是BR、FR等作为radiance材质的一种格式,也叫做radiance map,后来成为流行的一种HDR格式。所谓E,就是指数。Radiance RGBE文件每个通道为8bit BYTE数据类型,4个通道一共是32 bit。RGBE可以使用RLE压缩编码压缩,也可以不压缩。由文件头、RGBE数据组成。
         文件头如下:
         类型                                  输出格式
         char programtype[16];         //#?Radiance/n#Generated by still/n
         float gamma;                      //1.0
         float exposure;                   //1.0
         字符串常量                          //FORMAT=32-bit_rle_rgbe/n/n
         int nWidth, int nHeight         //-Y nHeight +X nWidth/n

         RGBE数据与HDR FP32(RGB)相互转换公式如下:
         1、rgbe->FP32(RGB)
             如果e为0, R = G = B = 0.0,否则:
             R = r * 2^(e – 128 - 8);
             G = g * 2^(e – 128 - 8);
             B = b * 2^(e – 128 - 8);
       
         2、FP32(RGB) -> rgbe
             v = max(R, G, B);
             如果v < 1e-32, r = g = b = e = 0, 否则:
             将v用科学计算法表示成 v = m * 2 ^ n ( 0 < m < 1):
             r = R * m *  256.0/v;
             g = G * m *  256.0/v;
             b = B * m *  256.0/v;
             e = n + 128;

     Still注:
     1、我们一般说HDR采用FP32,指的是HDR图象运算时候的内存数据类型,而Radiance RGBE文件采用8bit BYTE类型存储HDR数据。也就是说打开Radiance RGBE文件,要使用上面的公式1将Radiance RGBE文件的8bit BYTE文件数据转换为FP 32的HDR内存数据进行运算;保存为Radiance RGBE文件时,要使用上面的公式2将HDR的FP32内存数据转换为Radiance RGBE的8bit BYTE文件数据进行保存。同理,OpenEXR文件的读写也存在将其FP 16的文件数据到HDR的 FP32图象数据的转换;而下面将要讲的Float Tiff是不需要进行数据转换,直接将HDR的FP 32图象数据保存到TIFF文件中即可。
     2、Radiance有多种文件格式,其官方库包含内容比较复杂,所以,实际的读写没有使用其官方库,而是使用了网络上一个简单的C语言读写类,Still并对其进行了部分修改(在文件头写入“Generated  by Still”)。

    读写类链接地址:http://www.graphics.cornell.edu/~bjw/rgbe.html
    官方库链接地址:http://radsite.lbl.gov/radiance/

*************************************************************************

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

RGBE File Format

What is it?
RGBE is an image format invented by Greg Ward. It stores pixels as one byte rgb (red, green, and blue) values with a one byte shared exponent. Thus it store four bytes per pixel.

Why is it?
Its biggest advantage is that it allows to have pixels to have the extended range and precision of floating point values. Often when we generate images from light simulations, the range of pixels values is much greater than will nicely fit into the standard 0 to 255 range of standard 24-bit image formats. As a result we either truncate bright pixels to 255 or we end up losing all our precision in dimmer pixels. By using a shared exponent, the rgbe format gains some of the advantages floating point values without the 12 bytes per pixel needed for single precision IEEE values. It can handle very bright pixels without loss of precision for darker ones.

Is there code to read/write RGBE files?
Greg Ward provides code to handle RGBE files in his Radiance rendering system. However the RGBE format is very useful even if you are not using Radiance and deserves to become a standard format throughout the rendering community and beyond. To facilitate this, I have implemented a minimal set of routines to read and write RGBE files. This code is provided without any guarantees whatsoever, but please do send me a note if you find any bugs.

rgbe.txt - description of interface
rgbe.h - header file
rgbe.c - C code
Where can I find more information?
See "Real Pixels" by Greg Ward in Graphics Gems II .

************************************

rgbe.txt--description of interface

The rgbe image file format was invented by Greg Ward to reduce the precision problems inherent in normal 24bit file formats. Many programs including photorealistic renderers produce pixels with a wide range of values. Internally the programs usually handle this by representing colors by floating point values. However writing out the floating point values directly would produce very large files (~96 bits/pixel). The rgbe format works by having all channels (typically red, green, and blue) share a single exponent. Thus a pixel consists of three 8 bit mantissas and an 8 bit exponent for 32 bits per pixel. This makes for a compact format which can handle wide range of pixels values (from 0 to 2^127) with reasonable precision. See "Real Pixels" by Greg Ward in Graphics Gems II for more details. This code was written based on Greg Ward's code (available from the radiance home page ftp://radsite.lbl.gov/home.html) for reading and writing rgbe files. I have rewritten the code as ANSI C and tried to clean up the code and comment it to make it easier to understand. I've also tried to make the error detection a little more robust.
Here's the minimal code for using these files. sample minimal writing code:
f = fopen(image_filename,"wb");
RGBE_WriteHeader(f,image_width,image_h

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值