diff options
author | Soumith Chintala <soumith@gmail.com> | 2015-09-04 00:56:04 +0300 |
---|---|---|
committer | Soumith Chintala <soumith@gmail.com> | 2015-09-04 00:56:04 +0300 |
commit | b17ed1930195d90b51a4343ef51f674494f1e803 (patch) | |
tree | 944f2beffd6241765348ae5211a2ad59f51d4ff8 | |
parent | 24753920d8a91cd2b8944bf60776f68e9f97577a (diff) | |
parent | b8a6fafda11cd408d964db8ea96f47ff8eb388bc (diff) |
Merge pull request #102 from etrulls/master
Fix boundary/quantization artefacts on rotation.
-rwxr-xr-x | generic/image.c | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/generic/image.c b/generic/image.c index 316a8cc..2e8223c 100755 --- a/generic/image.c +++ b/generic/image.c @@ -433,11 +433,11 @@ static int image_(Main_rotate)(lua_State *L) if( (Tsrc->nDimension!=Tdst->nDimension) ) luaL_error(L, "image.rotate: src and dst depths do not match"); - xc=src_width/2.0; - yc=src_height/2.0; + xc = (src_width-1)/2.0; + yc = (src_height-1)/2.0; - sin_theta = sinf(theta); - cos_theta = cosf(theta); + sin_theta = sin(theta); + cos_theta = cos(theta); for(j = 0; j < dst_height; j++) { jd=j; @@ -445,9 +445,8 @@ static int image_(Main_rotate)(lua_State *L) float val = -1; id= i; - ii=(long)( cos_theta*(id-xc)-sin_theta*(jd-yc) ); - jj=(long)( cos_theta*(jd-yc)+sin_theta*(id-xc) ); - ii+=(long) xc; jj+=(long) yc; + ii = (long) round(cos_theta*(id-xc) - sin_theta*(jd-yc) + xc); + jj = (long) round(cos_theta*(jd-yc) + sin_theta*(id-xc) + yc); /* rotated corners are blank */ if(ii>src_width-1) val=0; @@ -526,8 +525,8 @@ static int image_(Main_rotateBilinear)(lua_State *L) if( (Tsrc->nDimension!=Tdst->nDimension) ) luaL_error(L, "image.rotate: src and dst depths do not match"); - xc=src_width/2.0; - yc=src_height/2.0; + xc = (src_width-1)/2.0; + yc = (src_height-1)/2.0; for(j = 0; j < dst_height; j++) { jd=j; @@ -538,19 +537,22 @@ static int image_(Main_rotateBilinear)(lua_State *L) ri = cos(theta)*(id-xc)-sin(theta)*(jd-yc); rj = cos(theta)*(jd-yc)+sin(theta)*(id-xc); - ii_0=(long)floor(ri); - ii_1=ii_0 + 1; - jj_0=(long)floor(rj); - jj_1=jj_0 + 1; - wi = ri - ii_0; - wj = rj - jj_0; - ii_0+=(long) xc; ii_1+=(long) xc; jj_0+=(long) yc;jj_1+=(long) yc; - - /* rotated corners are blank */ - if(ii_1>src_width-1) val=0; - if(jj_1>src_height-1) val=0; - if(ii_0<0) val=0; - if(jj_0<0) val=0; + ii_0 = (long)floor(ri+xc); + ii_1 = ii_0 + 1; + jj_0 = (long)floor(rj+yc); + jj_1 = jj_0 + 1; + wi = ri+xc-ii_0; + wj = rj+yc-jj_0; + + /* default to the closest value when interpolating on image boundaries (either image pixel or 0) */ + if(ii_1==src_width && wi<0.5) ii_1 = ii_0; + else if(ii_1>=src_width) val=0; + if(jj_1==src_height && wj<0.5) jj_1 = jj_0; + else if(jj_1>=src_height) val=0; + if(ii_0==-1 && wi>0.5) ii_0 = ii_1; + else if(ii_0<0) val=0; + if(jj_0==-1 && wj>0.5) jj_0 = jj_1; + else if(jj_0<0) val=0; if(Tsrc->nDimension==2) { if(val==-1) |