forked from victoroliv2/halide-casestudies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdrloader.cpp
191 lines (159 loc) · 3.79 KB
/
hdrloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***********************************************************************************
Created: 17:9:2002
FileName: hdrloader.cpp
Author: Igor Kravtchenko
Info: Load HDR image and convert to a set of float32 RGB triplet.
************************************************************************************/
#include "hdrloader.h"
#include <math.h>
#include <memory.h>
#include <stdio.h>
typedef unsigned char RGBE[4];
#define R 0
#define G 1
#define B 2
#define E 3
#define MINELEN 8 // minimum scanline length for encoding
#define MAXELEN 0x7fff // maximum scanline length for encoding
static void workOnRGBE(RGBE *scan, int len, float *cols);
static bool decrunch(RGBE *scanline, int len, FILE *file);
static bool oldDecrunch(RGBE *scanline, int len, FILE *file);
bool HDRLoader::load(const char *fileName, HDRLoaderResult &res)
{
int i;
char str[200];
FILE *file;
file = fopen(fileName, "rb");
if (!file)
return false;
fread(str, 10, 1, file);
if (memcmp(str, "#?RADIANCE", 10)) {
fclose(file);
return false;
}
fseek(file, 1, SEEK_CUR);
char cmd[200];
i = 0;
char c = 0, oldc;
while(true) {
oldc = c;
c = fgetc(file);
if (c == 0xa && oldc == 0xa)
break;
cmd[i++] = c;
}
char reso[200];
i = 0;
while(true) {
c = fgetc(file);
reso[i++] = c;
if (c == 0xa)
break;
}
long int w, h;
if (!sscanf(reso, "-Y %ld +X %ld", &h, &w)) {
fclose(file);
return false;
}
res.width = w;
res.height = h;
float *cols = new float[w * h * 3];
res.cols = cols;
RGBE *scanline = new RGBE[w];
if (!scanline) {
fclose(file);
return false;
}
// convert image
for (int y = h - 1; y >= 0; y--) {
if (decrunch(scanline, w, file) == false)
break;
workOnRGBE(scanline, w, cols);
cols += w * 3;
}
delete [] scanline;
fclose(file);
return true;
}
float convertComponent(int expo, int val)
{
float v = val / 256.0f;
float d = (float) pow(2, expo);
return v * d;
}
void workOnRGBE(RGBE *scan, int len, float *cols)
{
while (len-- > 0) {
int expo = scan[0][E] - 128;
cols[0] = convertComponent(expo, scan[0][R]);
cols[1] = convertComponent(expo, scan[0][G]);
cols[2] = convertComponent(expo, scan[0][B]);
cols += 3;
scan++;
}
}
bool decrunch(RGBE *scanline, int len, FILE *file)
{
int i, j;
if (len < MINELEN || len > MAXELEN)
return oldDecrunch(scanline, len, file);
i = fgetc(file);
if (i != 2) {
fseek(file, -1, SEEK_CUR);
return oldDecrunch(scanline, len, file);
}
scanline[0][G] = fgetc(file);
scanline[0][B] = fgetc(file);
i = fgetc(file);
if (scanline[0][G] != 2 || scanline[0][B] & 128) {
scanline[0][R] = 2;
scanline[0][E] = i;
return oldDecrunch(scanline + 1, len - 1, file);
}
// read each component
for (i = 0; i < 4; i++) {
for (j = 0; j < len; ) {
unsigned char code = fgetc(file);
if (code > 128) { // run
code &= 127;
unsigned char val = fgetc(file);
while (code--)
scanline[j++][i] = val;
}
else { // non-run
while(code--)
scanline[j++][i] = fgetc(file);
}
}
}
return feof(file) ? false : true;
}
bool oldDecrunch(RGBE *scanline, int len, FILE *file)
{
int i;
int rshift = 0;
while (len > 0) {
scanline[0][R] = fgetc(file);
scanline[0][G] = fgetc(file);
scanline[0][B] = fgetc(file);
scanline[0][E] = fgetc(file);
if (feof(file))
return false;
if (scanline[0][R] == 1 &&
scanline[0][G] == 1 &&
scanline[0][B] == 1) {
for (i = scanline[0][E] << rshift; i > 0; i--) {
memcpy(&scanline[0][0], &scanline[-1][0], 4);
scanline++;
len--;
}
rshift += 8;
}
else {
scanline++;
len--;
rshift = 0;
}
}
return true;
}