diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2011-06-04 11:37:24 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2011-06-04 11:37:24 (GMT) |
| commit | 8a5e566b77ae4e86946661d1965d424c3be26d9b (patch) | |
| tree | 95257260783a5c119fa57a363c2181cf589a39a6 /src | |
| parent | b850abe347f4db35c694eec253c510a869db20f1 (diff) | |
| download | powder-8a5e566b77ae4e86946661d1965d424c3be26d9b.zip powder-8a5e566b77ae4e86946661d1965d424c3be26d9b.tar.gz | |
Stairstepping for downscaling
Diffstat (limited to 'src')
| -rw-r--r-- | src/graphics.c | 58 |
1 files changed, 45 insertions, 13 deletions
diff --git a/src/graphics.c b/src/graphics.c index 990fae5..a5593a2 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -144,13 +144,12 @@ pixel *resample_img(pixel *src, int sw, int sh, int rw, int rh) { int y, x; //int i,j,x,y,w,h,r,g,b,c; - pixel *q; - q = malloc(rw*rh*PIXELSIZE); + pixel *q = NULL; //TODO: Actual resampling, this is just cheap nearest pixel crap - //if(rw > sw && rh > sh){ - //if(1){ + if(rw > sw && rh > sh){ float fx, fy, fyc, fxc, intp; pixel tr, tl, br, bl; + q = malloc(rw*rh*PIXELSIZE); //Bilinear interpolation for upscaling for (y=0; y<rh; y++) for (x=0; x<rw; x++) @@ -169,15 +168,48 @@ pixel *resample_img(pixel *src, int sw, int sh, int rw, int rh) (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) ); } - //} else { - // for (y=0; y<rh; y++) - // for (x=0; x<rw; x++) - // { - // q[rw*y+x] = src[sw*(y*sh/rh)+(x*sw/rw)]; - // } - //} - //*qw = w; - //*qh = h; + } else { + //Stairstepping + float fx, fy, fyc, fxc, intp; + pixel tr, tl, br, bl; + int rrw = rw, rrh = rh; + pixel * oq; + oq = malloc(sw*sh*PIXELSIZE); + memcpy(oq, src, sw*sh*PIXELSIZE); + rw = sw; + rh = sh; + while(rrw != rw && rrh != rh){ + rw *= 0.7; + rh *= 0.7; + if(rw <= rrw || rh <= rrh){ + rw = rrw; + rh = rrh; + } + q = malloc(rw*rh*PIXELSIZE); + //Bilinear interpolation for upscaling + for (y=0; y<rh; y++) + for (x=0; x<rw; x++) + { + fx = ((float)x)*((float)sw)/((float)rw); + fy = ((float)y)*((float)sh)/((float)rh); + fxc = modf(fx, &intp); + fyc = modf(fy, &intp); + tr = oq[sw*(int)floor(fy)+(int)ceil(fx)]; + tl = oq[sw*(int)floor(fy)+(int)floor(fx)]; + br = oq[sw*(int)ceil(fy)+(int)ceil(fx)]; + bl = oq[sw*(int)ceil(fy)+(int)floor(fx)]; + q[rw*y+x] = PIXRGB( + (int)(((((float)PIXR(tl))*(1.0f-fxc))+(((float)PIXR(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXR(bl))*(1.0f-fxc))+(((float)PIXR(br))*(fxc)))*(fyc)), + (int)(((((float)PIXG(tl))*(1.0f-fxc))+(((float)PIXG(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXG(bl))*(1.0f-fxc))+(((float)PIXG(br))*(fxc)))*(fyc)), + (int)(((((float)PIXB(tl))*(1.0f-fxc))+(((float)PIXB(tr))*(fxc)))*(1.0f-fyc) + ((((float)PIXB(bl))*(1.0f-fxc))+(((float)PIXB(br))*(fxc)))*(fyc)) + ); + } + free(oq); + oq = q; + sw = rw; + sh = rh; + } + } return q; } |
