-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
80 lines (69 loc) · 2.24 KB
/
main.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
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "color.h"
#include "tonemap.h"
#define CHANNELS 3
#define IMAGE_PATH "image.hdr"
#define OUT_IMAGE_PATH_FMT "out-%s.jpg"
float3 tonemap(int func, float3 color)
{
switch (func)
{
case NONE:
return color;
case REINHARD:
return tmo_reinhard(color);
case EXTENDED_REINHARD:
return tmo_extended_reinhard(color, 3.5f);
case EXTENDED_REINHARD_LUMINANCE:
return tmo_extended_reinhard_lum(color, 3.5f);
case REINHARD_JODIE:
return tmo_reinhard_jodie(color);
case UNCHARTED2_FILMIC:
return tmo_hable_filmic(color);
case ACES_FITTED:
return tmo_aces_fitted(color);
case ACES_APPROXIMATED:
return tmo_aces_approximated(color);
}
}
void main()
{
// load image
int width, height, channels;
const float *img = stbi_loadf(IMAGE_PATH, &width, &height, &channels, CHANNELS);
if (img == NULL)
{
fprintf(stderr, "could not load the image: %s\n", strerror(errno));
return;
}
if (channels != CHANNELS)
{
fprintf(stderr, "image must be in RGB format\n");
}
uint8_t *out = new uint8_t[width * height * CHANNELS];
for (int tm = 0; tm < _TM_FUNC_COUNT; tm++)
{
// process image
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < width * height; i++)
{
float3 color = float3(img[i * CHANNELS], img[i * CHANNELS + 1], img[i * CHANNELS + 2]);
color = tonemap(tm, color);
color = clamp(color, 0.0f, 1.0f);
float3_to_rgb(color, &out[i * CHANNELS]);
}
// write image
const char *tonemap_fn_name = tonemap_function_name(tm);
size_t out_image_path_len = strlen(OUT_IMAGE_PATH_FMT) - 2 + strlen(tonemap_fn_name) + 1;
char *out_image_path = (char *)malloc(out_image_path_len);
snprintf(out_image_path, out_image_path_len, OUT_IMAGE_PATH_FMT, tonemap_fn_name);
out_image_path[out_image_path_len - 1] = '\0';
stbi_write_jpg(out_image_path, width, height, CHANNELS, out, 0);
}
stbi_image_free((void *)img);
delete out;
}