|
Blender V2.27
|
00001 00033 /* imageprocess.c MIXED MODEL 00034 * 00035 * april 95 00036 * 00037 * $Id: imageprocess.c 30446 2010-07-17 18:08:14Z campbellbarton $ 00038 */ 00039 00040 #include <stdlib.h> 00041 00042 #include "IMB_imbuf_types.h" 00043 #include "IMB_imbuf.h" 00044 #include "math.h" 00045 00046 /* This define should be relocated to a global header some where Kent Mein 00047 I stole it from util.h in the plugins api */ 00048 #define MAX2(x,y) ( (x)>(y) ? (x) : (y) ) 00049 00050 /* Only this one is used liberally here, and in imbuf */ 00051 void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf) 00052 { 00053 int size; 00054 unsigned char rt, *cp = (unsigned char *)ibuf->rect; 00055 float rtf, *cpf = ibuf->rect_float; 00056 00057 if (ibuf->rect) { 00058 size = ibuf->x * ibuf->y; 00059 00060 while(size-- > 0) { 00061 rt= cp[0]; 00062 cp[0]= cp[3]; 00063 cp[3]= rt; 00064 rt= cp[1]; 00065 cp[1]= cp[2]; 00066 cp[2]= rt; 00067 cp+= 4; 00068 } 00069 } 00070 00071 if (ibuf->rect_float) { 00072 size = ibuf->x * ibuf->y; 00073 00074 while(size-- > 0) { 00075 rtf= cpf[0]; 00076 cpf[0]= cpf[3]; 00077 cpf[3]= rtf; 00078 rtf= cpf[1]; 00079 cpf[1]= cpf[2]; 00080 cpf[2]= rtf; 00081 cpf+= 4; 00082 } 00083 } 00084 } 00085 static void pixel_from_buffer(struct ImBuf *ibuf, unsigned char **outI, float **outF, int x, int y) 00086 00087 { 00088 int offset = ibuf->x * y * 4 + 4*x; 00089 00090 if (ibuf->rect) 00091 *outI= (unsigned char *)ibuf->rect + offset; 00092 00093 if (ibuf->rect_float) 00094 *outF= (float *)ibuf->rect_float + offset; 00095 } 00096 00097 /************************************************************************** 00098 * INTERPOLATIONS 00099 * 00100 * Reference and docs: 00101 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms 00102 ***************************************************************************/ 00103 00104 /* BICUBIC Interpolation functions */ 00105 /* More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation 00106 */ 00107 /* function assumes out to be zero'ed, only does RGBA */ 00108 00109 static float P(float k){ 00110 float p1, p2, p3, p4; 00111 p1 = MAX2(k+2.0f,0); 00112 p2 = MAX2(k+1.0f,0); 00113 p3 = MAX2(k,0); 00114 p4 = MAX2(k-1.0f,0); 00115 return (float)(1.0f/6.0f)*( p1*p1*p1 - 4.0f * p2*p2*p2 + 6.0f * p3*p3*p3 - 4.0f * p4*p4*p4); 00116 } 00117 00118 00119 #if 0 00120 /* older, slower function, works the same as above */ 00121 static float P(float k){ 00122 return (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)); 00123 } 00124 #endif 00125 00126 void bicubic_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v) 00127 { 00128 int i,j,n,m,x1,y1; 00129 unsigned char *dataI; 00130 float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF; 00131 00132 /* ImBuf in must have a valid rect or rect_float, assume this is already checked */ 00133 00134 i= (int)floor(u); 00135 j= (int)floor(v); 00136 a= u - i; 00137 b= v - j; 00138 00139 outR = outG = outB = outA = 0.0f; 00140 00141 /* Optimized and not so easy to read */ 00142 00143 /* avoid calling multiple times */ 00144 wy[0] = P(b-(-1)); 00145 wy[1] = P(b- 0); 00146 wy[2] = P(b- 1); 00147 wy[3] = P(b- 2); 00148 00149 for(n= -1; n<= 2; n++){ 00150 x1= i+n; 00151 if (x1>0 && x1 < in->x) { 00152 wx = P(n-a); 00153 for(m= -1; m<= 2; m++){ 00154 y1= j+m; 00155 if (y1>0 && y1<in->y) { 00156 /* normally we could do this */ 00157 /* w = P(n-a) * P(b-m); */ 00158 /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */ 00159 w = wx * wy[m+1]; 00160 00161 if (outF) { 00162 dataF= in->rect_float + in->x * y1 * 4 + 4*x1; 00163 outR+= dataF[0] * w; 00164 outG+= dataF[1] * w; 00165 outB+= dataF[2] * w; 00166 outA+= dataF[3] * w; 00167 } 00168 if (outI) { 00169 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1; 00170 outR+= dataI[0] * w; 00171 outG+= dataI[1] * w; 00172 outB+= dataI[2] * w; 00173 outA+= dataI[3] * w; 00174 } 00175 } 00176 } 00177 } 00178 } 00179 00180 /* Done with optimized part */ 00181 00182 #if 0 00183 /* older, slower function, works the same as above */ 00184 for(n= -1; n<= 2; n++){ 00185 for(m= -1; m<= 2; m++){ 00186 x1= i+n; 00187 y1= j+m; 00188 if (x1>0 && x1 < in->x && y1>0 && y1<in->y) { 00189 if (do_float) { 00190 dataF= in->rect_float + in->x * y1 * 4 + 4*x1; 00191 outR+= dataF[0] * P(n-a) * P(b-m); 00192 outG+= dataF[1] * P(n-a) * P(b-m); 00193 outB+= dataF[2] * P(n-a) * P(b-m); 00194 outA+= dataF[3] * P(n-a) * P(b-m); 00195 } 00196 if (do_rect) { 00197 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1; 00198 outR+= dataI[0] * P(n-a) * P(b-m); 00199 outG+= dataI[1] * P(n-a) * P(b-m); 00200 outB+= dataI[2] * P(n-a) * P(b-m); 00201 outA+= dataI[3] * P(n-a) * P(b-m); 00202 } 00203 } 00204 } 00205 } 00206 #endif 00207 00208 if (outI) { 00209 outI[0]= (int)outR; 00210 outI[1]= (int)outG; 00211 outI[2]= (int)outB; 00212 outI[3]= (int)outA; 00213 } 00214 if (outF) { 00215 outF[0]= outR; 00216 outF[1]= outG; 00217 outF[2]= outB; 00218 outF[3]= outA; 00219 } 00220 } 00221 00222 00223 void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) 00224 { 00225 00226 unsigned char *outI = NULL; 00227 float *outF = NULL; 00228 00229 if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return; 00230 00231 pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */ 00232 00233 bicubic_interpolation_color(in, outI, outF, u, v); 00234 } 00235 00236 /* function assumes out to be zero'ed, only does RGBA */ 00237 /* BILINEAR INTERPOLATION */ 00238 void bilinear_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v) 00239 { 00240 float *row1, *row2, *row3, *row4, a, b; 00241 unsigned char *row1I, *row2I, *row3I, *row4I; 00242 float a_b, ma_b, a_mb, ma_mb; 00243 float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f}; 00244 unsigned char emptyI[4]= {0, 0, 0, 0}; 00245 int y1, y2, x1, x2; 00246 00247 00248 /* ImBuf in must have a valid rect or rect_float, assume this is already checked */ 00249 00250 x1= (int)floor(u); 00251 x2= (int)ceil(u); 00252 y1= (int)floor(v); 00253 y2= (int)ceil(v); 00254 00255 // sample area entirely outside image? 00256 if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return; 00257 00258 if (outF) { 00259 // sample including outside of edges of image 00260 if (x1<0 || y1<0) row1= empty; 00261 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1; 00262 00263 if (x1<0 || y2>in->y-1) row2= empty; 00264 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1; 00265 00266 if (x2>in->x-1 || y1<0) row3= empty; 00267 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2; 00268 00269 if (x2>in->x-1 || y2>in->y-1) row4= empty; 00270 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2; 00271 00272 a= u-floor(u); 00273 b= v-floor(v); 00274 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); 00275 00276 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0]; 00277 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1]; 00278 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2]; 00279 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3]; 00280 } 00281 if (outI) { 00282 // sample including outside of edges of image 00283 if (x1<0 || y1<0) row1I= emptyI; 00284 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1; 00285 00286 if (x1<0 || y2>in->y-1) row2I= emptyI; 00287 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1; 00288 00289 if (x2>in->x-1 || y1<0) row3I= emptyI; 00290 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2; 00291 00292 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI; 00293 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2; 00294 00295 a= u-floor(u); 00296 b= v-floor(v); 00297 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); 00298 00299 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush) 00300 * tested with white images and this should not wrap back to zero */ 00301 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f; 00302 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f; 00303 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f; 00304 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f; 00305 } 00306 } 00307 00308 /* function assumes out to be zero'ed, only does RGBA */ 00309 /* BILINEAR INTERPOLATION */ 00310 00311 /* Note about wrapping, the u/v still needs to be within the image bounds, 00312 * just the interpolation is wrapped. 00313 * This the same as bilinear_interpolation_color except it wraps rather then using empty and emptyI */ 00314 void bilinear_interpolation_color_wrap(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v) 00315 { 00316 float *row1, *row2, *row3, *row4, a, b; 00317 unsigned char *row1I, *row2I, *row3I, *row4I; 00318 float a_b, ma_b, a_mb, ma_mb; 00319 int y1, y2, x1, x2; 00320 00321 00322 /* ImBuf in must have a valid rect or rect_float, assume this is already checked */ 00323 00324 x1= (int)floor(u); 00325 x2= (int)ceil(u); 00326 y1= (int)floor(v); 00327 y2= (int)ceil(v); 00328 00329 // sample area entirely outside image? 00330 if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return; 00331 00332 /* wrap interpolation pixels - main difference from bilinear_interpolation_color */ 00333 if(x1<0)x1= in->x+x1; 00334 if(y1<0)y1= in->y+y1; 00335 00336 if(x2>=in->x)x2= x2-in->x; 00337 if(y2>=in->y)y2= y2-in->y; 00338 00339 if (outF) { 00340 // sample including outside of edges of image 00341 row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1; 00342 row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1; 00343 row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2; 00344 row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2; 00345 00346 a= u-floor(u); 00347 b= v-floor(v); 00348 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); 00349 00350 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0]; 00351 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1]; 00352 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2]; 00353 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3]; 00354 } 00355 if (outI) { 00356 // sample including outside of edges of image 00357 row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1; 00358 row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1; 00359 row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2; 00360 row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2; 00361 00362 a= u-floor(u); 00363 b= v-floor(v); 00364 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b); 00365 00366 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush) 00367 * tested with white images and this should not wrap back to zero */ 00368 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f; 00369 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f; 00370 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f; 00371 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f; 00372 } 00373 } 00374 00375 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout) 00376 { 00377 00378 unsigned char *outI = NULL; 00379 float *outF = NULL; 00380 00381 if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return; 00382 00383 pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */ 00384 00385 bilinear_interpolation_color(in, outI, outF, u, v); 00386 } 00387 00388 /* function assumes out to be zero'ed, only does RGBA */ 00389 /* NEAREST INTERPOLATION */ 00390 void neareast_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v) 00391 { 00392 float *dataF; 00393 unsigned char *dataI; 00394 int y1, x1; 00395 00396 /* ImBuf in must have a valid rect or rect_float, assume this is already checked */ 00397 00398 x1= (int)(u); 00399 y1= (int)(v); 00400 00401 // sample area entirely outside image? 00402 if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return; 00403 00404 // sample including outside of edges of image 00405 if (x1<0 || y1<0) { 00406 if (outI) { 00407 outI[0]= 0; 00408 outI[1]= 0; 00409 outI[2]= 0; 00410 outI[3]= 0; 00411 } 00412 if (outF) { 00413 outF[0]= 0.0f; 00414 outF[1]= 0.0f; 00415 outF[2]= 0.0f; 00416 outF[3]= 0.0f; 00417 } 00418 } else { 00419 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1; 00420 if (outI) { 00421 outI[0]= dataI[0]; 00422 outI[1]= dataI[1]; 00423 outI[2]= dataI[2]; 00424 outI[3]= dataI[3]; 00425 } 00426 dataF= in->rect_float + in->x * y1 * 4 + 4*x1; 00427 if (outF) { 00428 outF[0]= dataF[0]; 00429 outF[1]= dataF[1]; 00430 outF[2]= dataF[2]; 00431 outF[3]= dataF[3]; 00432 } 00433 } 00434 } 00435 00436 void neareast_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout) 00437 { 00438 00439 unsigned char *outI = NULL; 00440 float *outF = NULL; 00441 00442 if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return; 00443 00444 pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */ 00445 00446 neareast_interpolation_color(in, outI, outF, x, y); 00447 }
1.7.3