-
Notifications
You must be signed in to change notification settings - Fork 1
/
rad2jpeg.c
309 lines (264 loc) · 9.98 KB
/
rad2jpeg.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Converts RADIANCE image file to JPEG image file.
*
* Radiance values from 0.0 to 1.0 remapped to 8-bit unsigned int using
* sRGB convention. Subtleties such as blackpoint and whitepoint are ignored.
*
* Ignores EXPOSURE record in Radiance header, which is consistent with
* the Radiance convension that the numeric values in the file are what
* the file creator thinks are appropriately scaled for display.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "devas-image.h"
#include "radianceIO.h"
#include "devas-jpeg.h"
#include "iccjpeg.h"
#include "devas-sRGB.h"
#include "sRGB_radiance.h"
#include "radiance/color.h"
#include "radiance-conversion-version.h"
#include "devas-license.h"
#define GLARE_LEVEL_RATIO 5.0 /* RADIANCE identifies glare sources */
/* as being brighter than 7 times the */
/* average luminance level. This is */
/* slightly more conservative. */
char *Usage =
"rad2jpeg [--exposure=stops] [--autoadjust] input.hdr output.jpg";
int args_needed = 2;
#include "sRGB_IEC61966-2-1_black_scaled.c" /* hardwired binary profile */
double find_glare_threshold ( DeVAS_RGBf_image *image );
double fmax3 ( double v1, double v2, double v3 );
void DeVAS_RGBf_rescale ( DeVAS_RGBf_image *image, float new_max,
float new_min );
int
main ( int argc, char *argv[] )
{
int exposure_flag = FALSE;
double exposure_stops;
double exposure_adjust = 1.0;
int autoadjust_flag = FALSE;
float adjust_max;
DeVAS_RGBf_image *input_image;
DeVAS_RGB_image *sRGB_image;
int row, col;
char *new_description = NULL;
RGBPRIMS radiance_prims = STDPRIMS;
RGBPRIMS sRGB_prims = sRGBPRIMS;
COLORMAT radrgb2sRGBmat;
COLOR radiance_pixel_in;
COLOR radiance_pixel_out;
DeVAS_RGBf DeVAS_pixel;
int argpt = 1;
while ( ( ( argc - argpt ) >= 1 ) && ( argv[argpt][0] == '-' ) ) {
if ( strcmp ( argv[argpt], "-" ) == 0 ) {
break; /* read from stdin */
} else if ( strncmp ( argv[argpt], "--exposure=",
strlen ( "--exposure=" ) ) == 0 ) {
exposure_stops = atof ( argv[argpt] + strlen ( "--exposure=" ) );
exposure_flag = TRUE;
argpt++;
} else if ( strncmp ( argv[argpt], "-exposure=",
strlen ( "-exposure=" ) ) == 0 ) {
exposure_stops = atof ( argv[argpt] + strlen ( "-exposure=" ) );
exposure_flag = TRUE;
argpt++;
} else if ( ( strcmp ( argv[argpt], "--autoadjust" ) == 0 ) ||
( strcmp ( argv[argpt], "-autoadjust" ) == 0 ) ) {
autoadjust_flag = TRUE;
argpt++;
/* hidden options */
} else if ( strncmp ( argv[argpt], "-description=",
strlen ( "-description=" ) ) == 0 ) {
new_description = argv[argpt] + strlen ( "-description=" );
argpt++;
} else if ( strncmp ( argv[argpt], "--description=",
strlen ( "--description=" ) ) == 0 ) {
new_description = argv[argpt] + strlen ( "--description=" );
argpt++;
} else {
fprintf ( stderr, "unknown argument!\n" );
return ( EXIT_FAILURE ); /* error return */
}
}
if ( ( argc - argpt ) != args_needed ) {
fprintf ( stderr, "%s\n", Usage );
return ( EXIT_FAILURE ); /* error return */
}
input_image = DeVAS_RGBf_image_from_radfilename ( argv[argpt++] );
/* convert to sRGB primaries */
comprgb2rgbWBmat ( radrgb2sRGBmat, radiance_prims, sRGB_prims );
for ( row = 0; row < DeVAS_image_n_rows ( input_image ); row++ ) {
for ( col = 0; col < DeVAS_image_n_cols ( input_image ); col++ ) {
DeVAS_pixel = DeVAS_image_data ( input_image, row, col );
colval ( radiance_pixel_in, RED ) = DeVAS_pixel.red;
colval ( radiance_pixel_in, GRN ) = DeVAS_pixel.green;
colval ( radiance_pixel_in, BLU ) = DeVAS_pixel.blue;
colortrans ( radiance_pixel_out, radrgb2sRGBmat,
radiance_pixel_in );
DeVAS_pixel.red = colval ( radiance_pixel_out, RED );
DeVAS_pixel.green = colval ( radiance_pixel_out, GRN );
DeVAS_pixel.blue = colval ( radiance_pixel_out, BLU );
DeVAS_image_data ( input_image, row, col ) = DeVAS_pixel;
}
}
if ( autoadjust_flag ) {
adjust_max = find_glare_threshold ( input_image );
if ( adjust_max > 0.0 ) {
DeVAS_RGBf_rescale ( input_image, adjust_max, 0 );
}
}
if ( exposure_flag ) {
exposure_adjust = pow ( 2.0, exposure_stops );
for ( row = 0; row < DeVAS_image_n_rows ( input_image ); row++ ) {
for ( col = 0; col < DeVAS_image_n_cols ( input_image ); col++ ) {
DeVAS_image_data ( input_image, row, col ).red *=
exposure_adjust;
DeVAS_image_data ( input_image, row, col ).green *=
exposure_adjust;
DeVAS_image_data ( input_image, row, col ).blue *=
exposure_adjust;
}
}
}
/* convert to 8-bit/color using sRGB non-linear encoding */
sRGB_image = DeVAS_RGB_image_new ( DeVAS_image_n_rows ( input_image ),
DeVAS_image_n_cols ( input_image ) );
for ( row = 0; row < DeVAS_image_n_rows ( input_image ); row++ ) {
for ( col = 0; col < DeVAS_image_n_cols ( input_image ); col++ ) {
DeVAS_image_data ( sRGB_image, row, col ) =
RGBf_to_sRGB ( DeVAS_image_data ( input_image, row, col ) );
}
}
DeVAS_RGB_image_to_filename_jpg ( argv[argpt++], sRGB_image,
new_description );
DeVAS_RGB_image_delete ( sRGB_image );
DeVAS_RGBf_image_delete ( input_image );
return ( EXIT_SUCCESS ); /* normal exit */
}
double
find_glare_threshold ( DeVAS_RGBf_image *image )
/*
* Suggests a luminance level above which pixel should be considered
* a glare source. (This version uses maximum over R, G, and B, instead
* of luminance.)
*
* Uses a variant of the RADIANCE glare identification heuristic. First,
* average luminance is computed and used to set a preliminary glare
* threshold value. This average is not a robust estimator, since it is
* strongly affected by very bright glare pixels or glare pixels covering
* a large portion of the image. To compensate for this, a second pass
* is done in which a revised average luminance is computed based only on
* pixels <= the preliminary glare threshold. This revised average luminance
* is then used to compute a revised glare threshold, which is returned as
* the value of the function.
*/
{
int row, col;
double max_pixel_value;
double max_value;
double average_value_initial;
double glare_cutoff_initial;
max_value = average_value_initial = 0.0;
for ( row = 0; row < DeVAS_image_n_rows ( image ); row++ ) {
for ( col = 0; col < DeVAS_image_n_cols ( image ); col++ ) {
max_pixel_value = fmax3 ( DeVAS_image_data (image, row, col ) . red,
DeVAS_image_data (image, row, col ) . green,
DeVAS_image_data (image, row, col ) . blue );
if ( max_value < max_pixel_value ) {
max_value = max_pixel_value;
}
average_value_initial += max_pixel_value;
}
}
average_value_initial /=
( ((double) DeVAS_image_n_rows ( image ) ) *
((double) DeVAS_image_n_cols ( image ) ) );
glare_cutoff_initial = GLARE_LEVEL_RATIO * average_value_initial;
if ( glare_cutoff_initial >= max_value ) {
/* no need for glare source clipping */
return ( max_value );
}
max_value = 0.0;
for ( row = 0; row < DeVAS_image_n_rows ( image ); row++ ) {
for ( col = 0; col < DeVAS_image_n_cols ( image ); col++ ) {
max_pixel_value = fmax3 ( DeVAS_image_data (image, row, col ) . red,
DeVAS_image_data (image, row, col ) . green,
DeVAS_image_data (image, row, col ) . blue );
if ( max_pixel_value <= glare_cutoff_initial ) {
if ( max_value < max_pixel_value ) {
max_value = max_pixel_value;
}
}
}
}
return ( max_value );
}
double
fmax3 ( double v1, double v2, double v3 )
{
return ( fmax ( v1, fmax ( v2, v3 ) ) );
}
void
DeVAS_RGBf_rescale ( DeVAS_RGBf_image *image, float new_max, float new_min )
{
int row, col;
int n_rows, n_cols;
double old_max, old_min;
double old_max_red, old_max_green, old_max_blue;
double old_min_red, old_min_green, old_min_blue;
double rescale;
n_rows = DeVAS_image_n_rows ( image );
n_cols = DeVAS_image_n_cols ( image );
old_max_red = old_min_red = DeVAS_image_data ( image, 0, 0 ).red;
old_max_green = old_min_green = DeVAS_image_data ( image, 0, 0 ).green;
old_max_blue = old_min_blue = DeVAS_image_data ( image, 0, 0 ).blue;
for ( row = 0; row < n_rows; row++ ) {
for ( col = 0; col < n_cols; col++ ) {
old_max_red =
fmax ( old_max_red, DeVAS_image_data ( image, row, col ).red );
old_max_green =
fmax ( old_max_green,
DeVAS_image_data ( image, row, col ).green );
old_max_blue =
fmax ( old_max_blue, DeVAS_image_data ( image, row, col ).blue );
old_min_red =
fmin ( old_min_red, DeVAS_image_data ( image, row, col ).red );
old_min_green =
fmin ( old_min_green,
DeVAS_image_data ( image, row, col ).green );
old_min_blue =
fmin ( old_min_blue, DeVAS_image_data ( image, row, col ).blue );
}
}
old_max = fmax ( old_max_red, fmax ( old_max_green, old_max_blue ) );
old_min = fmin ( old_min_red, fmin ( old_min_green, old_min_blue ) );
if ( old_max == old_min ) {
fprintf ( stderr,
"DeVAS_RGBf_rescale: no variability in values (warning)\n" );
for ( row = 0; row < n_rows; row++ ) {
for ( col = 0; col < n_cols; col++ ) {
DeVAS_image_data ( image, row, col ).red =
DeVAS_image_data ( image, row, col ).green =
DeVAS_image_data ( image, row, col ).blue =
0.5 * ( new_max + new_min );
}
}
return;
}
rescale = ( new_max - new_min ) / ( old_max - old_min );
for ( row = 0; row < n_rows; row++ ) {
for ( col = 0; col < n_cols; col++ ) {
DeVAS_image_data ( image, row, col ).red = ( rescale *
( DeVAS_image_data ( image, row, col ).red - old_min ) ) +
new_min;
DeVAS_image_data ( image, row, col ).green = ( rescale *
( DeVAS_image_data ( image, row, col ).green - old_min ) ) +
new_min;
DeVAS_image_data ( image, row, col ).blue = ( rescale *
( DeVAS_image_data ( image, row, col ).blue - old_min ) ) +
new_min;
}
}
}