Skip to content

Commit 86ade28

Browse files
committed
WIP: Preprocess degamma
1 parent 0510e14 commit 86ade28

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/ispc/kernels/lanczos3.ispc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,22 @@ static inline float frac(float f) {
5353
return absf - floor(absf);
5454
}
5555

56-
static inline float byte_to_float(uint8 b, uniform bool degamma) {
56+
static inline float byte_to_float(uint8 b) {
5757
const float inv_255 = rcp(255.0);
5858

5959
// floatbits(0x3f800000 | (b << (23 - 8))) - 1.0;
60-
float d = (float)b * inv_255;
61-
62-
if (degamma) {
63-
d = pow(d, GAMMA);
64-
}
65-
66-
return d;
60+
return (float)b * inv_255;
6761
}
6862

69-
static inline uint8 float_to_byte(float d, uniform bool gamma) {
63+
static inline uint8 float_to_byte(float d, bool gamma) {
7064
if (gamma) {
7165
d = pow(d, 1.0f / GAMMA);
7266
}
7367
int b = d * 255;
7468
return clamp(b, 0, 255);
7569
}
7670

77-
static inline uint8<4> resample_internal(uniform Parameters params, uniform Image src_image, float<2> uv, uniform uint8 num_channels) {
71+
static inline float<4> resample_internal(uniform Image src_image, float<2> uv, uniform uint8 num_channels) {
7872
float<4> col = 0.0;
7973
float weight = 0.0;
8074
// Truncate floating point coordinate to integer:
@@ -100,18 +94,14 @@ static inline uint8<4> resample_internal(uniform Parameters params, uniform Imag
10094
int addr = (src_kernel_coord.x + src_kernel_coord.y * src_image.size.x) * num_channels;
10195

10296
for (uniform int i = 0; i < num_channels; i++)
103-
col[i] += w * byte_to_float(src_image.data[addr + i], params.degamma);
97+
col[i] += w * byte_to_float(src_image.data[addr + i]);
10498

10599
weight += w;
106100
}
107101
}
108102
col /= weight;
103+
return col;
109104

110-
uint8<4> res;
111-
for (uniform int i = 0; i < num_channels; i++)
112-
res[i] = float_to_byte(col[i], params.gamma);
113-
114-
return res;
115105
}
116106

117107
export void resample(
@@ -123,16 +113,28 @@ export void resample(
123113
) {
124114
uniform float<2> inv_target_size = 1.0f / dst->size;
125115

116+
if (params->degamma) {
117+
foreach_tiled(y = 0 ... src->size.y, x = 0 ... src->size.x)
118+
{
119+
uint p = (x + y * src->size.x) * num_channels;
120+
for (uniform int i = 0; i < num_channels; i++) {
121+
uint c = p + i;
122+
// TODO: This texture should be writeonly!
123+
src->data[c] = float_to_byte(pow(byte_to_float(src->data[c]), GAMMA), false);
124+
}
125+
}
126+
}
127+
126128
foreach_tiled (y = 0 ... dst->size.y, x = 0 ... dst->size.x) {
127129
float<2> uv = {x, y};
128130
// Use the center of each pixel, not the top-left:
129131
uv += 0.5f;
130132
// Convert to uniform space:
131133
uv *= inv_target_size;
132134

133-
uint8<4> s = resample_internal(*params, *src, uv, num_channels);
135+
float<4> col = resample_internal(*src, uv, num_channels);
134136

135137
for (uniform int i = 0; i < num_channels; i++)
136-
dst->data[(x + y * dst->size.x) * num_channels + i] = s[i];
138+
dst->data[(x + y * dst->size.x) * num_channels + i] = float_to_byte(col[i], params->gamma);
137139
}
138140
}

0 commit comments

Comments
 (0)