1 /****************************************************************************
2  *
3  *      hdrUtils.h: Radiance RGBE format utilities
4  *      This is part of the yafray package
5  *      Copyright (C) 2010 George Laskowsky Ziguilinsky (Glaskows)
6  *						   Rodrigo Placencia Vazquez (DarkTide)
7  *
8  *      This library is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU Lesser General Public
10  *      License as published by the Free Software Foundation; either
11  *      version 2.1 of the License, or (at your option) any later version.
12  *
13  *      This library is distributed in the hope that it will be useful,
14  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      Lesser General Public License for more details.
17  *
18  *      You should have received a copy of the GNU Lesser General Public
19  *      License along with this library; if not, write to the Free Software
20  *      Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 
24 #include <core_api/color.h>
25 #include <utilities/stringUtils.h>
26 
27 __BEGIN_YAFRAY
28 
29 #pragma pack(push, 1)
30 
31 struct rgbeHeader_t
32 {
rgbeHeader_trgbeHeader_t33 	rgbeHeader_t()
34 	{
35 		programType = "RADIANCE";
36 		exposure = 1.0f;
37 		yFirst = true;
38 	}
39 
40 	float exposure; // in an image corresponds to <exposure> watts/steradian/m^2. Defaults to 1.0
41 	std::string programType; // A string that usually contains "RADIANCE"
42 	int min[2], max[2], step[2]; // Image boundaries and iteration stepping
43 	bool yFirst; // Indicates if the image scanlines are saved starting by y axis (Default: true)
44 };
45 
46 struct rgbePixel_t
47 {
48 	rgbePixel_t &operator = (const color_t &c)
49 	{
50 		int e;
51 		float v = c.maximum();
52 
53 		if (v < 1e-32)
54 		{
55 			R = G = B = E = 0;
56 		}
57 		else
58 		{
59 			v = frexp(v, &e) * 255.9999 / v;
60 			R = (yByte) (c.getR() * v);
61 			G = (yByte) (c.getG() * v);
62 			B = (yByte) (c.getB() * v);
63 			E = (yByte) (e + 128);
64 		}
65 
66 		return *this;
67 	}
68 
69 	rgbePixel_t &operator = (const rgbePixel_t &c)
70 	{
71 		R = c.R;
72 		G = c.G;
73 		B = c.B;
74 		E = c.E;
75 
76 		return *this;
77 	}
78 
79 	yByte &operator [] (int i)
80 	{
81 		return (&R)[i];
82 	}
83 
getRGBArgbePixel_t84 	colorA_t getRGBA() const
85 	{
86 		float f;
87 
88 		if(E)
89 		{   /*nonzero pixel*/
90 			f = fLdexp(1.0, E - (int) (128+8));
91 			return colorA_t(f * R, f * G, f * B, 1.0f);
92 		}
93 
94 		return colorA_t(0.f, 0.f, 0.f, 1.0f);
95 	}
96 
isORLEDescrgbePixel_t97 	bool isORLEDesc()
98 	{
99 		return ((R == 1) && (G == 1) && (B == 1));
100 	}
101 
isARLEDescrgbePixel_t102 	bool isARLEDesc()
103 	{
104 		return ((R == 2) && (G == 2) && ((int)(B << 8 | E) < 0x8000));
105 	}
106 
getORLECountrgbePixel_t107 	int getORLECount(int rshift)
108 	{
109 		return ((int)E << rshift);
110 	}
111 
getARLECountrgbePixel_t112 	int getARLECount()
113 	{
114 		return ((int)(B << 8 | E));
115 	}
116 
setScanlineStartrgbePixel_t117 	void setScanlineStart(int w)
118 	{
119 		R = 2;
120 		G = 2;
121 		B = w >> 8;
122 		E = w & 0xFF;
123 	}
124 
125 	yByte R;
126 	yByte G;
127 	yByte B;
128 	yByte E;
129 };
130 
131 #pragma pack(pop)
132 
133 __END_YAFRAY
134