diff options
author | Kent Mein <mein@cs.umn.edu> | 2007-07-10 23:13:03 +0400 |
---|---|---|
committer | Kent Mein <mein@cs.umn.edu> | 2007-07-10 23:13:03 +0400 |
commit | 9298357670f5a1acc5515a771276fa444245aeb3 (patch) | |
tree | c9491ee4a1a0a821b996b228b5f09bd446a29d89 /source/blender/imbuf | |
parent | 209407a7e1e6078c2b5d23ba0183338850bcb1a1 (diff) |
This is patch [#6929] Interpolation Methods
http://projects.blender.org/tracker/index.php?func=detail&aid=6929&group_id=9&atid=127
Its a subset of patch [#6766] Transform Sequencer effect
It adds 3 image functions for different types of interpolation.
For people that like pictures:
http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
Patch provided by David Millán Escrivá (damiles)
Kent
Diffstat (limited to 'source/blender/imbuf')
-rw-r--r-- | source/blender/imbuf/IMB_imbuf.h | 8 | ||||
-rw-r--r-- | source/blender/imbuf/intern/imageprocess.c | 227 |
2 files changed, 234 insertions, 1 deletions
diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h index d6ca21830aa..840d39ea0bd 100644 --- a/source/blender/imbuf/IMB_imbuf.h +++ b/source/blender/imbuf/IMB_imbuf.h @@ -395,7 +395,13 @@ void IMB_float_from_rect(struct ImBuf *ibuf); * @attention Defined in imageprocess.c */ void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf); - +/** + * + * @attention defined in imageprocess.c + */ +void bicubic_interpolation(struct ImBuf *in, struct ImBuf *out, float x, float y, int xout, int yout); +void neareast_interpolation(struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); +void bilinear_interpolation(struct ImBuf *in, struct ImBuf *out, float u, float v, int xout, int yout); /** * Change the ordering of the color bytes pointed to by rect from * rgba to abgr. size * 4 color bytes are reordered. diff --git a/source/blender/imbuf/intern/imageprocess.c b/source/blender/imbuf/intern/imageprocess.c index f98244c5a0f..855c80c820d 100644 --- a/source/blender/imbuf/intern/imageprocess.c +++ b/source/blender/imbuf/intern/imageprocess.c @@ -42,6 +42,11 @@ #include "IMB_imbuf_types.h" #include "IMB_imbuf.h" +#include "math.h" + +/* This define should be relocated to a global header some where Kent Mein +I stole it from util.h in the plugins api */ +#define MAX2(x,y) ( (x)>(y) ? (x) : (y) ) /* Only this one is used liberally here, and in imbuf */ void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf) @@ -73,3 +78,225 @@ void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf) } } +/************************************************************************** +* INTERPOLATIONS +* +* Reference and docs: +* http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms +***************************************************************************/ + +/* BICUBIC */ + +/* Bicubic formula: +* +* A’(px,py) = Σn=-1..2Σm=-1..2 A(i+n,j+m)·P(n-a)·P(b-m) +* with: P(k) = 1/6(C(k+2)^3-4C(k+1)^3+6C(k)^3-4C(k-1)^3) +* and C(k) = max(0, k) +* +* Where A= in and A'=out +* +* More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation +*/ +/* function assumes out to be zero'ed, only does RGBA */ +static float P(float k){ + float aux; + aux=(float)(1.0f/6.0f)*( pow( MAX2(k+2.0f,0) , 3.0f ) - 4.0f * pow( MAX2(k+1.0f,0) , 3.0f ) + 6.0f * pow( MAX2(k,0) , 3.0f ) - 4.0f * pow( MAX2(k-1.0f,0) , 3.0f)); + return aux ; +} + +void bicubic_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout) +{ + int i,j,n,m,x1,y1; + unsigned char *dataI,*outI; + float a,b, outR,outG,outB,outA,*dataF,*outF; + int do_rect, do_float; + + if (in == NULL) return; + if (in->rect == NULL && in->rect_float == NULL) return; + + do_rect= (in->rect != NULL); + do_float= (in->rect_float != NULL); + + i= (int)floor(x); + j= (int)floor(y); + a= x - i; + b= y - j; + + outR= 0.0f; + outG= 0.0f; + outB= 0.0f; + outA= 0.0f; + for(n= -1; n<= 2; n++){ + for(m= -1; m<= 2; m++){ + x1= i+n; + y1= j+m; + if (x1>0 && x1 < in->x && y1>0 && y1<in->y) { + if (do_float) { + dataF= in->rect_float + in->x * y1 * 4 + 4*x1; + outR+= dataF[0] * P(n-a) * P(b-m); + outG+= dataF[1] * P(n-a) * P(b-m); + outB+= dataF[2] * P(n-a) * P(b-m); + outA+= dataF[3] * P(n-a) * P(b-m); + } + if (do_rect) { + dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1; + outR+= dataI[0] * P(n-a) * P(b-m); + outG+= dataI[1] * P(n-a) * P(b-m); + outB+= dataI[2] * P(n-a) * P(b-m); + outA+= dataI[3] * P(n-a) * P(b-m); + } + } + } + } + if (do_rect) { + outI= (unsigned char *)out->rect + out->x * yout * 4 + 4*xout; + outI[0]= (int)outR; + outI[1]= (int)outG; + outI[2]= (int)outB; + outI[3]= (int)outA; + } + if (do_float) { + outF= (float *)out->rect_float + out->x * yout * 4 + 4*xout; + outF[0]= outR; + outF[1]= outG; + outF[2]= outB; + outF[3]= outA; + } +} + +/* function assumes out to be zero'ed, only does RGBA */ +/* BILINEAR INTERPOLATION */ +void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) +{ + float *row1, *row2, *row3, *row4, a, b, *outF; + unsigned char *row1I, *row2I, *row3I, *row4I, *outI; + float a_b, ma_b, a_mb, ma_mb; + float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f}; + unsigned char emptyI[4]= {0, 0, 0, 0}; + int y1, y2, x1, x2; + int do_rect, do_float; + + if (in==NULL) return; + if (in->rect==NULL && in->rect_float==NULL) return; + + do_rect= (in->rect != NULL); + do_float= (in->rect_float != NULL); + + x1= (int)floor(u); + x2= (int)ceil(u); + y1= (int)floor(v); + y2= (int)ceil(v); + + // sample area entirely outside image? + if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return; + + if (do_rect) + outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout; + if (do_float) + outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout; + + if (do_float) { + // sample including outside of edges of image + if (x1<0 || y1<0) row1= empty; + else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1; + + if (x1<0 || y2>in->y-1) row2= empty; + else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1; + + if (x2>in->x-1 || y1<0) row3= empty; + else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2; + + if (x2>in->x-1 || y2>in->y-1) row4= empty; + else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2; + + a= u-floor(u); + b= v-floor(v); + a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); + + outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0]; + outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1]; + outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2]; + outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3]; + } + if (do_rect) { + // sample including outside of edges of image + if (x1<0 || y1<0) row1I= emptyI; + else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1; + + if (x1<0 || y2>in->y-1) row2I= emptyI; + else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1; + + if (x2>in->x-1 || y1<0) row3I= emptyI; + else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2; + + if (x2>in->x-1 || y2>in->y-1) row4I= emptyI; + else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2; + + a= u-floor(u); + b= v-floor(v); + a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); + + outI[0]= ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]; + outI[1]= ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]; + outI[2]= ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]; + outI[3]= ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]; + } +} + +/* function assumes out to be zero'ed, only does RGBA */ +/* NEAREST INTERPOLATION */ +void neareast_interpolation(ImBuf *in, ImBuf *out, float u, float v,int xout, int yout) +{ + float *outF,*dataF; + unsigned char *dataI,*outI; + int y1, x1; + int do_rect, do_float; + + if (in==NULL) return; + if (in->rect==NULL && in->rect_float==NULL) return; + + do_rect= (in->rect != NULL); + do_float= (in->rect_float != NULL); + + x1= (int)(u); + y1= (int)(v); + + if (do_rect) + outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout; + if (do_float) + outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout; + + // sample area entirely outside image? + if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return; + + // sample including outside of edges of image + if (x1<0 || y1<0) { + if (do_rect) { + outI[0]= 0; + outI[1]= 0; + outI[2]= 0; + outI[3]= 0; + } + if (do_float) { + outF[0]= 0.0f; + outF[1]= 0.0f; + outF[2]= 0.0f; + outF[3]= 0.0f; + } + } else { + dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1; + if (do_rect) { + outI[0]= dataI[0]; + outI[1]= dataI[1]; + outI[2]= dataI[2]; + outI[3]= dataI[3]; + } + dataF= in->rect_float + in->x * y1 * 4 + 4*x1; + if (do_float) { + outF[0]= dataF[0]; + outF[1]= dataF[1]; + outF[2]= dataF[2]; + outF[3]= dataF[3]; + } + } +} |