|
Blender V2.27
|
00001 00032 #include "MEM_guardedalloc.h" 00033 00034 #include "BLI_utildefines.h" 00035 00036 00037 00038 #include "IMB_imbuf_types.h" 00039 #include "IMB_imbuf.h" 00040 #include "IMB_filter.h" 00041 00042 #include "imbuf.h" 00043 00044 /************************************************************************/ 00045 /* FILTERS */ 00046 /************************************************************************/ 00047 00048 static void filtrow(unsigned char *point, int x) 00049 { 00050 unsigned int c1,c2,c3,error; 00051 00052 if (x>1){ 00053 c1 = c2 = *point; 00054 error = 2; 00055 for(x--;x>0;x--){ 00056 c3 = point[4]; 00057 c1 += (c2<<1) + c3 + error; 00058 error = c1 & 3; 00059 *point = c1 >> 2; 00060 point += 4; 00061 c1=c2; 00062 c2=c3; 00063 } 00064 *point = (c1 + (c2<<1) + c2 + error) >> 2; 00065 } 00066 } 00067 00068 static void filtrowf(float *point, int x) 00069 { 00070 float c1,c2,c3; 00071 00072 if (x>1){ 00073 c1 = c2 = *point; 00074 for(x--;x>0;x--){ 00075 c3 = point[4]; 00076 c1 += (c2 * 2) + c3; 00077 *point = 0.25f*c1; 00078 point += 4; 00079 c1=c2; 00080 c2=c3; 00081 } 00082 *point = 0.25f*(c1 + (c2 * 2) + c2); 00083 } 00084 } 00085 00086 00087 00088 static void filtcolum(unsigned char *point, int y, int skip) 00089 { 00090 unsigned int c1,c2,c3,error; 00091 unsigned char *point2; 00092 00093 if (y>1){ 00094 c1 = c2 = *point; 00095 point2 = point; 00096 error = 2; 00097 for(y--;y>0;y--){ 00098 point2 += skip; 00099 c3 = *point2; 00100 c1 += (c2<<1) + c3 +error; 00101 error = c1 & 3; 00102 *point = c1 >> 2; 00103 point=point2; 00104 c1=c2; 00105 c2=c3; 00106 } 00107 *point = (c1 + (c2<<1) + c2 + error) >> 2; 00108 } 00109 } 00110 00111 static void filtcolumf(float *point, int y, int skip) 00112 { 00113 float c1,c2,c3, *point2; 00114 00115 if (y>1){ 00116 c1 = c2 = *point; 00117 point2 = point; 00118 for(y--;y>0;y--){ 00119 point2 += skip; 00120 c3 = *point2; 00121 c1 += (c2 * 2) + c3; 00122 *point = 0.25f*c1; 00123 point=point2; 00124 c1=c2; 00125 c2=c3; 00126 } 00127 *point = 0.25f*(c1 + (c2 * 2) + c2); 00128 } 00129 } 00130 00131 void IMB_filtery(struct ImBuf *ibuf) 00132 { 00133 unsigned char *point; 00134 float *pointf; 00135 int x, y, skip; 00136 00137 point = (unsigned char *)ibuf->rect; 00138 pointf = ibuf->rect_float; 00139 00140 x = ibuf->x; 00141 y = ibuf->y; 00142 skip = x<<2; 00143 00144 for (;x>0;x--){ 00145 if (point) { 00146 if (ibuf->depth > 24) filtcolum(point,y,skip); 00147 point++; 00148 filtcolum(point,y,skip); 00149 point++; 00150 filtcolum(point,y,skip); 00151 point++; 00152 filtcolum(point,y,skip); 00153 point++; 00154 } 00155 if (pointf) { 00156 if (ibuf->depth > 24) filtcolumf(pointf,y,skip); 00157 pointf++; 00158 filtcolumf(pointf,y,skip); 00159 pointf++; 00160 filtcolumf(pointf,y,skip); 00161 pointf++; 00162 filtcolumf(pointf,y,skip); 00163 pointf++; 00164 } 00165 } 00166 } 00167 00168 00169 void imb_filterx(struct ImBuf *ibuf) 00170 { 00171 unsigned char *point; 00172 float *pointf; 00173 int x, y, skip; 00174 00175 point = (unsigned char *)ibuf->rect; 00176 pointf = ibuf->rect_float; 00177 00178 x = ibuf->x; 00179 y = ibuf->y; 00180 skip = (x<<2) - 3; 00181 00182 for (;y>0;y--){ 00183 if (point) { 00184 if (ibuf->depth > 24) filtrow(point,x); 00185 point++; 00186 filtrow(point,x); 00187 point++; 00188 filtrow(point,x); 00189 point++; 00190 filtrow(point,x); 00191 point+=skip; 00192 } 00193 if (pointf) { 00194 if (ibuf->depth > 24) filtrowf(pointf,x); 00195 pointf++; 00196 filtrowf(pointf,x); 00197 pointf++; 00198 filtrowf(pointf,x); 00199 pointf++; 00200 filtrowf(pointf,x); 00201 pointf+=skip; 00202 } 00203 } 00204 } 00205 00206 void IMB_filterN(ImBuf *out, ImBuf *in) 00207 { 00208 register char *row1, *row2, *row3; 00209 register char *cp, *r11, *r13, *r21, *r23, *r31, *r33; 00210 int rowlen, x, y; 00211 00212 rowlen= in->x; 00213 00214 for(y=0; y<in->y; y++) { 00215 /* setup rows */ 00216 row2= (char*)(in->rect + y*rowlen); 00217 row1= (y == 0)? row2: row2 - 4*rowlen; 00218 row3= (y == in->y-1)? row2: row2 + 4*rowlen; 00219 00220 cp= (char *)(out->rect + y*rowlen); 00221 00222 for(x=0; x<rowlen; x++) { 00223 if(x == 0) { 00224 r11 = row1; 00225 r21 = row1; 00226 r31 = row1; 00227 } 00228 else { 00229 r11 = row1-4; 00230 r21 = row1-4; 00231 r31 = row1-4; 00232 } 00233 00234 if(x == rowlen-1) { 00235 r13 = row1; 00236 r23 = row1; 00237 r33 = row1; 00238 } 00239 else { 00240 r13 = row1+4; 00241 r23 = row1+4; 00242 r33 = row1+4; 00243 } 00244 00245 cp[0]= (r11[0] + 2*row1[0] + r13[0] + 2*r21[0] + 4*row2[0] + 2*r23[0] + r31[0] + 2*row3[0] + r33[0])>>4; 00246 cp[1]= (r11[1] + 2*row1[1] + r13[1] + 2*r21[1] + 4*row2[1] + 2*r23[1] + r31[1] + 2*row3[1] + r33[1])>>4; 00247 cp[2]= (r11[2] + 2*row1[2] + r13[2] + 2*r21[2] + 4*row2[2] + 2*r23[2] + r31[2] + 2*row3[2] + r33[2])>>4; 00248 cp[3]= (r11[3] + 2*row1[3] + r13[3] + 2*r21[3] + 4*row2[3] + 2*r23[3] + r31[3] + 2*row3[3] + r33[3])>>4; 00249 cp+=4; row1+=4; row2+=4; row3+=4; 00250 } 00251 } 00252 } 00253 00254 void IMB_filter(struct ImBuf *ibuf) 00255 { 00256 IMB_filtery(ibuf); 00257 imb_filterx(ibuf); 00258 } 00259 00260 #define EXTEND_PIXEL(color, w) if((color)[3]) {r+= w*(color)[0]; g+= w*(color)[1]; b+= w*(color)[2]; a+= w*(color)[3]; tot+=w;} 00261 00262 /* if alpha is zero, it checks surrounding pixels and averages color. sets new alphas to 1.0 00263 * 00264 * When a mask is given, only effect pixels with a mask value of 1, defined as BAKE_MASK_MARGIN in rendercore.c 00265 * */ 00266 void IMB_filter_extend(struct ImBuf *ibuf, char *mask) 00267 { 00268 register char *row1, *row2, *row3; 00269 register char *cp; 00270 int rowlen, x, y; 00271 00272 rowlen= ibuf->x; 00273 00274 00275 if (ibuf->rect_float) { 00276 float *temprect; 00277 float *row1f, *row2f, *row3f; 00278 float *fp; 00279 temprect= MEM_dupallocN(ibuf->rect_float); 00280 00281 for(y=1; y<=ibuf->y; y++) { 00282 /* setup rows */ 00283 row1f= (float *)(temprect + (y-2)*rowlen*4); 00284 row2f= row1f + 4*rowlen; 00285 row3f= row2f + 4*rowlen; 00286 if(y==1) 00287 row1f= row2f; 00288 else if(y==ibuf->y) 00289 row3f= row2f; 00290 00291 fp= (float *)(ibuf->rect_float + (y-1)*rowlen*4); 00292 00293 for(x=0; x<rowlen; x++) { 00294 if((mask==NULL && fp[3]==0.0f) || (mask && mask[((y-1)*rowlen)+x]==1)) { 00295 int tot= 0; 00296 float r=0.0f, g=0.0f, b=0.0f, a=0.0f; 00297 00298 EXTEND_PIXEL(row1f, 1); 00299 EXTEND_PIXEL(row2f, 2); 00300 EXTEND_PIXEL(row3f, 1); 00301 EXTEND_PIXEL(row1f+4, 2); 00302 EXTEND_PIXEL(row3f+4, 2); 00303 if(x!=rowlen-1) { 00304 EXTEND_PIXEL(row1f+8, 1); 00305 EXTEND_PIXEL(row2f+8, 2); 00306 EXTEND_PIXEL(row3f+8, 1); 00307 } 00308 if(tot) { 00309 fp[0]= r/tot; 00310 fp[1]= g/tot; 00311 fp[2]= b/tot; 00312 fp[3]= a/tot; 00313 } 00314 } 00315 fp+=4; 00316 00317 if(x!=0) { 00318 row1f+=4; row2f+=4; row3f+=4; 00319 } 00320 } 00321 } 00322 00323 MEM_freeN(temprect); 00324 } 00325 else if(ibuf->rect) { 00326 int *temprect; 00327 00328 /* make a copy, to prevent flooding */ 00329 temprect= MEM_dupallocN(ibuf->rect); 00330 00331 for(y=1; y<=ibuf->y; y++) { 00332 /* setup rows */ 00333 row1= (char *)(temprect + (y-2)*rowlen); 00334 row2= row1 + 4*rowlen; 00335 row3= row2 + 4*rowlen; 00336 if(y==1) 00337 row1= row2; 00338 else if(y==ibuf->y) 00339 row3= row2; 00340 00341 cp= (char *)(ibuf->rect + (y-1)*rowlen); 00342 00343 for(x=0; x<rowlen; x++) { 00344 /*if(cp[3]==0) {*/ 00345 if((mask==NULL && cp[3]==0) || (mask && mask[((y-1)*rowlen)+x]==1)) { 00346 int tot= 0, r=0, g=0, b=0, a=0; 00347 00348 EXTEND_PIXEL(row1, 1); 00349 EXTEND_PIXEL(row2, 2); 00350 EXTEND_PIXEL(row3, 1); 00351 EXTEND_PIXEL(row1+4, 2); 00352 EXTEND_PIXEL(row3+4, 2); 00353 if(x!=rowlen-1) { 00354 EXTEND_PIXEL(row1+8, 1); 00355 EXTEND_PIXEL(row2+8, 2); 00356 EXTEND_PIXEL(row3+8, 1); 00357 } 00358 if(tot) { 00359 cp[0]= r/tot; 00360 cp[1]= g/tot; 00361 cp[2]= b/tot; 00362 cp[3]= a/tot; 00363 } 00364 } 00365 cp+=4; 00366 00367 if(x!=0) { 00368 row1+=4; row2+=4; row3+=4; 00369 } 00370 } 00371 } 00372 00373 MEM_freeN(temprect); 00374 } 00375 } 00376 00377 /* threadsafe version, only recreates existing maps */ 00378 void IMB_remakemipmap(ImBuf *ibuf, int use_filter) 00379 { 00380 ImBuf *hbuf = ibuf; 00381 int curmap = 0; 00382 00383 ibuf->miptot= 1; 00384 00385 while(curmap < IB_MIPMAP_LEVELS) { 00386 00387 if(ibuf->mipmap[curmap]) { 00388 00389 if(use_filter) { 00390 ImBuf *nbuf= IMB_allocImBuf(hbuf->x, hbuf->y, 32, IB_rect); 00391 IMB_filterN(nbuf, hbuf); 00392 imb_onehalf_no_alloc(ibuf->mipmap[curmap], nbuf); 00393 IMB_freeImBuf(nbuf); 00394 } 00395 else 00396 imb_onehalf_no_alloc(ibuf->mipmap[curmap], hbuf); 00397 } 00398 00399 ibuf->miptot= curmap+2; 00400 hbuf= ibuf->mipmap[curmap]; 00401 if(hbuf) 00402 hbuf->miplevel= curmap+1; 00403 00404 if(!hbuf || (hbuf->x <= 2 && hbuf->y <= 2)) 00405 break; 00406 00407 curmap++; 00408 } 00409 } 00410 00411 /* frees too (if there) and recreates new data */ 00412 void IMB_makemipmap(ImBuf *ibuf, int use_filter) 00413 { 00414 ImBuf *hbuf = ibuf; 00415 int curmap = 0; 00416 00417 imb_freemipmapImBuf(ibuf); 00418 00419 ibuf->miptot= 1; 00420 00421 while(curmap < IB_MIPMAP_LEVELS) { 00422 if(use_filter) { 00423 ImBuf *nbuf= IMB_allocImBuf(hbuf->x, hbuf->y, 32, IB_rect); 00424 IMB_filterN(nbuf, hbuf); 00425 ibuf->mipmap[curmap] = IMB_onehalf(nbuf); 00426 IMB_freeImBuf(nbuf); 00427 } 00428 else 00429 ibuf->mipmap[curmap] = IMB_onehalf(hbuf); 00430 00431 ibuf->miptot= curmap+2; 00432 hbuf= ibuf->mipmap[curmap]; 00433 hbuf->miplevel= curmap+1; 00434 00435 if(!hbuf || (hbuf->x == 1 && hbuf->y == 1)) 00436 break; 00437 00438 curmap++; 00439 } 00440 } 00441 00442 ImBuf *IMB_getmipmap(ImBuf *ibuf, int level) 00443 { 00444 CLAMP(level, 0, ibuf->miptot-1); 00445 return (level == 0)? ibuf: ibuf->mipmap[level-1]; 00446 } 00447 00448 void IMB_premultiply_rect(unsigned int *rect, int depth, int w, int h) 00449 { 00450 char *cp; 00451 int x, y, val; 00452 00453 if(depth == 24) { /* put alpha at 255 */ 00454 cp= (char *)(rect); 00455 00456 for(y=0; y<h; y++) 00457 for(x=0; x<w; x++, cp+=4) 00458 cp[3]= 255; 00459 } 00460 else { 00461 cp= (char *)(rect); 00462 00463 for(y=0; y<h; y++) { 00464 for(x=0; x<w; x++, cp+=4) { 00465 val= cp[3]; 00466 cp[0]= (cp[0]*val)>>8; 00467 cp[1]= (cp[1]*val)>>8; 00468 cp[2]= (cp[2]*val)>>8; 00469 } 00470 } 00471 } 00472 } 00473 00474 void IMB_premultiply_rect_float(float *rect_float, int depth, int w, int h) 00475 { 00476 float val, *cp; 00477 int x, y; 00478 00479 if(depth==24) { /* put alpha at 1.0 */ 00480 cp= rect_float; 00481 00482 for(y=0; y<h; y++) 00483 for(x=0; x<w; x++, cp+=4) 00484 cp[3]= 1.0; 00485 } 00486 else { 00487 cp= rect_float; 00488 for(y=0; y<h; y++) { 00489 for(x=0; x<w; x++, cp+=4) { 00490 val= cp[3]; 00491 cp[0]= cp[0]*val; 00492 cp[1]= cp[1]*val; 00493 cp[2]= cp[2]*val; 00494 } 00495 } 00496 } 00497 00498 } 00499 00500 void IMB_premultiply_alpha(ImBuf *ibuf) 00501 { 00502 if(ibuf==NULL) 00503 return; 00504 00505 if(ibuf->rect) 00506 IMB_premultiply_rect(ibuf->rect, ibuf->depth, ibuf->x, ibuf->y); 00507 00508 if(ibuf->rect_float) 00509 IMB_premultiply_rect_float(ibuf->rect_float, ibuf->depth, ibuf->x, ibuf->y); 00510 } 00511
1.7.3