-
Notifications
You must be signed in to change notification settings - Fork 5
/
enlighten.c
179 lines (161 loc) · 4.76 KB
/
enlighten.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
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#define SHIFT_R (0)
#define SHIFT_G (8)
#define SHIFT_B (16)
#define SHIFT_A (24)
#define RVAL(l) ((int)(((l)>>SHIFT_R)&0xff))
#define GVAL(l) ((int)(((l)>>SHIFT_G)&0xff))
#define BVAL(l) ((int)(((l)>>SHIFT_B)&0xff))
#define AVAL(l) ((int)(((l)>>SHIFT_A)&0xff))
#define CPACK(r, g, b, a) (((r)<<SHIFT_R) | ((g)<<SHIFT_G) | ((b)<<SHIFT_B) | ((a)<<SHIFT_A))
typedef struct canvas {
unsigned int *data;
int sizex, sizey;
} canvas;
float flerp(float f0, float f1, float p)
{
if(f0==f1)
return f0;
return ((f0*(1.0-p))+(f1*p));
}
canvas *canvas_new(int sizex, int sizey)
{
canvas *c = (canvas *)malloc(sizeof(canvas));
c->sizex = sizex;
c->sizey = sizey;
c->data = (unsigned int *)malloc(sizex*sizey*sizeof(unsigned int));
return c;
}
void canvas_free(canvas *c)
{
if(!c)
return;
free(c->data);
free(c);
}
canvas *canvas_maxrgb(canvas *in)
{
canvas *out = canvas_new(in->sizex, in->sizey);
unsigned int *iptr = in->data;
unsigned int *optr = out->data;
int n = in->sizex*in->sizey;
while(n--) {
int r = RVAL(*iptr);
int g = GVAL(*iptr);
int b = BVAL(*iptr);
int max = r;
if(max < g) max = g;
if(max < b) max = b;
*optr = CPACK(max, max, max, 255);
iptr++;
optr++;
}
return out;
}
canvas *canvas_brighten(canvas *in, canvas *maxrgbblur, float param)
{
float illummin = 1.0/flerp(1.0, 10.0, param*param);
float illummax = 1.0/flerp(1.0, 1.111, param*param);
canvas *out = canvas_new(in->sizex, in->sizey);
unsigned int *iptr = in->data;
unsigned int *bptr = maxrgbblur->data;
unsigned int *optr = out->data;
int n = in->sizex*in->sizey;
while(n--) {
float illum = RVAL(*bptr)/255.0;
int r = RVAL(*iptr);
int g = GVAL(*iptr);
int b = BVAL(*iptr);
if(illum < illummin)
illum = illummin;
if(illum < illummax) {
float p = illum/illummax;
float scale = (0.4+p*0.6)*(illummax/illum);
r = scale*r;
if(r>255) r = 255;
g = scale*g;
if(g>255) g = 255;
b = scale*b;
if(b>255) b = 255;
}
*optr = CPACK(r, g, b, 255);
iptr++;
bptr++;
optr++;
}
return out;
}
float canvas_diameter(canvas *c)
{
return sqrt(c->sizex*c->sizex + c->sizey*c->sizey);
}
canvas *canvas_resize(canvas *in, int sizex, int sizey)
{
canvas *out = canvas_new(sizex, sizey);
stbir_resize_uint8((unsigned char *) in->data, in->sizex, in->sizey, 0,
(unsigned char *)out->data, out->sizex, out->sizey, 0, 4);
return out;
}
canvas *canvas_blur(canvas *in, float smalldiam)
{
float indiam = canvas_diameter(in);
float scaledown = smalldiam/indiam;
int smallsizex = round(scaledown*in->sizex);
int smallsizey = round(scaledown*in->sizey);
canvas *small = canvas_resize(in, smallsizex, smallsizey);
canvas *big = canvas_resize(small, in->sizex, in->sizey);
canvas_free(small);
return big;
}
canvas *canvas_frompng(const char *filename)
{
int sizex, sizey, n;
unsigned char *data = stbi_load(filename, &sizex, &sizey, &n, 0);
if(!data) {
fprintf(stderr, "canvas_frompng: error: problem reading %s\n", filename);
exit(1);
}
canvas *pic = canvas_new(sizex, sizey);
free(pic->data);
pic->data = (unsigned int *)data;
return pic;
}
void canvas_topng(canvas *in, const char *filename)
{
stbi_write_png(filename, in->sizex, in->sizey, 4, in->data, 4*in->sizex);
}
canvas *canvas_enlighten(canvas *in, float param)
{
// make a b/w image that has max of [r, g, b] of the input
canvas *maxrgb = canvas_maxrgb(in);
// blur this image. can do more or less - try 10.0 try 40.0 up to you
canvas *maxrgbblur = canvas_blur(maxrgb, 20.0);
canvas_free(maxrgb);
// divide the input image by maxrgbblur to brighten the image
// param is in the range 0.0 .. 1.0 and normally controlled by a slider.
canvas *ret = canvas_brighten(in, maxrgbblur, param);
canvas_free(maxrgbblur);
return ret;
}
int main(int argc, char **argv)
{
if(argc<4) {
fprintf(stderr, "usage: enlighten in.png out.png param\n");
exit(1);
}
float param = atof(argv[3]);
canvas *in = canvas_frompng(argv[1]);
canvas *out = canvas_enlighten(in, param);
canvas_free(in);
canvas_topng(out, argv[2]);
canvas_free(out);
return 0;
}