@@ -43,26 +43,33 @@ float compress(float dist, float lim, float thr, float pwr, bool invert)
4343{
4444 float compr_dist;
4545 float scl;
46+ float nd;
47+ float p;
48+
4649 if (dist < thr) {
47- compr_dist = dist;
50+ compr_dist = dist; // No compression below threshold
4851 }
4952 else {
5053 // Calculate scale factor for y = 1 intersect
5154 scl = (lim - thr) / pow(pow((1.0 - thr) / (lim - thr), -pwr) - 1.0 , 1.0 / pwr);
55+
56+ // Normalize distance outside threshold by scale factor
57+ nd = (dist - thr) / scl;
58+ p = pow(nd, pwr);
59+
5260 if (!invert) {
53- compr_dist = thr + scl * ((dist - thr) / scl)
54- / (pow(1.0 + pow((dist - thr) / scl, pwr), 1.0 / pwr)); // Compress
61+ compr_dist = thr + scl * nd / (pow(1.0 + p, 1.0 / pwr)); // Compress
5562 }
5663 else {
5764 if (dist > (thr + scl)) {
5865 compr_dist = dist; // Avoid singularity
5966 }
6067 else {
61- compr_dist = thr + scl * pow(-(pow((dist - thr) / scl, pwr)
62- / (pow((dist - thr) / scl, pwr) - 1.0 )), 1.0 / pwr); // Uncompress
68+ compr_dist = thr + scl * pow(-(p / (p - 1.0 )), 1.0 / pwr); // Uncompress
6369 }
6470 }
6571 }
72+
6673 return compr_dist;
6774}
6875
@@ -111,14 +118,14 @@ void main
111118 };
112119
113120 // Recalculate RGB from compressed distance and achromatic
114- float cLin_AP1 [3 ] = {
121+ float compr_lin_AP1 [3 ] = {
115122 ach - compr_dist[0 ] * fabs(ach),
116123 ach - compr_dist[1 ] * fabs(ach),
117124 ach - compr_dist[2 ] * fabs(ach)
118125 };
119126
120127 // Convert back to ACES2065-1
121- ACES = mult_f3_f44(cLin_AP1 , AP1_2_AP0_MAT);
128+ ACES = mult_f3_f44(compr_lin_AP1 , AP1_2_AP0_MAT);
122129
123130 // Write output
124131 rOut = ACES[0 ];
0 commit comments