summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2021-04-25 12:16:40 +0200
committercinap_lenrek <cinap_lenrek@felloff.net>2021-04-25 12:16:40 +0200
commite0cf0261d0f81eb1c54c4d4a7636c27de4b00d36 (patch)
treef309d94bbe629b169aee2f2459078aec6dae75a9
parentb0e2ea4e5ffaec335c2a61ee98d2c5d3f1052569 (diff)
downloadplan9front-e0cf0261d0f81eb1c54c4d4a7636c27de4b00d36.tar.xz
resample: improve performance (thanks José Miguel Sánchez García)
Resample is well known for taking a long time to resize an image. This patch brings an important performance boost (in my test image, time was reduced from ~2850ms to ~500ms). It does that by extracting FP multiplication and division out of the innermost loop of resamplex/resampley. The results differ slightly from the current implementation: in my test: ~0.3% of the bytes had a ±2 difference in their value, which I attribute to rounding errors. I'm personally not concerned with that deviation, given the performance gains. However, I recommend testing it just to be sure I didn't overlook anything. José Miguel Sánchez García
-rw-r--r--sys/src/cmd/resample.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/sys/src/cmd/resample.c b/sys/src/cmd/resample.c
index d11e29960..3e4c20f46 100644
--- a/sys/src/cmd/resample.c
+++ b/sys/src/cmd/resample.c
@@ -67,10 +67,10 @@ void
resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
{
int i, x, k;
- double X, xx, v, rat;
-
+ double X, xx, v, rat, rato10;
rat = (double)inx/(double)outx;
+ rato10 = rat/10.;
for(x=0; x<outx; x++){
if(inx == outx){
/* don't resample if size unchanged */
@@ -79,14 +79,15 @@ resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
}
v = 0.0;
X = x*rat;
+ xx = X + rato10*(-K2);
for(k=-K2; k<=K2; k++){
- xx = X + rat*k/10.;
i = xx;
if(i < 0)
i = 0;
if(i >= inx)
i = inx-1;
v += in[off+i*d] * K[K2+k];
+ xx += rato10;
}
out[off+x*d] = v;
}
@@ -96,9 +97,10 @@ void
resampley(uchar **in, int off, int iny, uchar **out, int outy)
{
int y, i, k;
- double Y, yy, v, rat;
+ double Y, yy, v, rat, rato10;
rat = (double)iny/(double)outy;
+ rato10 = rat/10.;
for(y=0; y<outy; y++){
if(iny == outy){
/* don't resample if size unchanged */
@@ -107,14 +109,15 @@ resampley(uchar **in, int off, int iny, uchar **out, int outy)
}
v = 0.0;
Y = y*rat;
+ yy = Y + rato10*(-K2);
for(k=-K2; k<=K2; k++){
- yy = Y + rat*k/10.;
i = yy;
if(i < 0)
i = 0;
if(i >= iny)
i = iny-1;
v += in[i][off] * K[K2+k];
+ yy += rato10;
}
out[y][off] = v;
}