forked from ckohnert/msdfgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-sdf.cpp
107 lines (94 loc) · 3.41 KB
/
render-sdf.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
#include "render-sdf.h"
#include "arithmetics.hpp"
namespace msdfgen {
template <typename S>
inline FloatRGB mix(FloatRGB a, FloatRGB b, S weight) {
FloatRGB output = {
mix(a.r, b.r, weight),
mix(a.g, b.g, weight),
mix(a.b, b.b, weight)
};
return output;
}
template <typename T>
static T sample(const Bitmap<T> &bitmap, Point2 pos) {
int w = bitmap.width(), h = bitmap.height();
double x = pos.x*w-.5;
double y = pos.y*h-.5;
int l = (int) floor(x);
int b = (int) floor(y);
int r = l+1;
int t = b+1;
double lr = x-l;
double bt = y-b;
l = clamp(l, w-1), r = clamp(r, w-1);
b = clamp(b, h-1), t = clamp(t, h-1);
return mix(mix(bitmap(l, b), bitmap(r, b), lr), mix(bitmap(l, t), bitmap(r, t), lr), bt);
}
static float distVal(float dist, double pxRange) {
if (!pxRange)
return dist > .5;
return (float) clamp((dist-.5)*pxRange+.5);
}
void renderSDF(Bitmap<float> &output, const Bitmap<float> &sdf, double pxRange) {
int w = output.width(), h = output.height();
pxRange *= (double) (w+h)/(sdf.width()+sdf.height());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
float s = sample(sdf, Point2((x+.5)/w, (y+.5)/h));
output(x, y) = distVal(s, pxRange);
}
}
void renderSDF(Bitmap<FloatRGB> &output, const Bitmap<float> &sdf, double pxRange) {
int w = output.width(), h = output.height();
pxRange *= (double) (w+h)/(sdf.width()+sdf.height());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
float s = sample(sdf, Point2((x+.5)/w, (y+.5)/h));
float v = distVal(s, pxRange);
output(x, y).r = v;
output(x, y).g = v;
output(x, y).b = v;
}
}
void renderSDF(Bitmap<float> &output, const Bitmap<FloatRGB> &sdf, double pxRange) {
int w = output.width(), h = output.height();
pxRange *= (double) (w+h)/(sdf.width()+sdf.height());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
FloatRGB s = sample(sdf, Point2((x+.5)/w, (y+.5)/h));
output(x, y) = distVal(median(s.r, s.g, s.b), pxRange);
}
}
void renderSDF(Bitmap<FloatRGB> &output, const Bitmap<FloatRGB> &sdf, double pxRange) {
int w = output.width(), h = output.height();
pxRange *= (double) (w+h)/(sdf.width()+sdf.height());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
FloatRGB s = sample(sdf, Point2((x+.5)/w, (y+.5)/h));
output(x, y).r = distVal(s.r, pxRange);
output(x, y).g = distVal(s.g, pxRange);
output(x, y).b = distVal(s.b, pxRange);
}
}
void simulate8bit(Bitmap<float> &bitmap) {
int w = bitmap.width(), h = bitmap.height();
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
unsigned char v = clamp(int(bitmap(x, y)*0x100), 0xff);
bitmap(x, y) = v/255.f;
}
}
void simulate8bit(Bitmap<FloatRGB> &bitmap) {
int w = bitmap.width(), h = bitmap.height();
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
unsigned char r = clamp(int(bitmap(x, y).r*0x100), 0xff);
unsigned char g = clamp(int(bitmap(x, y).g*0x100), 0xff);
unsigned char b = clamp(int(bitmap(x, y).b*0x100), 0xff);
bitmap(x, y).r = r/255.f;
bitmap(x, y).g = g/255.f;
bitmap(x, y).b = b/255.f;
}
}
}