Blender V2.27

rectop.c

00001 
00032 #include "BLI_blenlib.h"
00033 #include "BLI_utildefines.h"
00034 
00035 #include "imbuf.h"
00036 #include "IMB_imbuf_types.h"
00037 #include "IMB_imbuf.h"
00038 
00039 #include "IMB_allocimbuf.h"
00040 
00041 
00042 /* blend modes */
00043 
00044 static void blend_color_mix(char *cp, char *cp1, char *cp2, int fac)
00045 {
00046         /* this and other blending modes previously used >>8 instead of /255. both
00047            are not equivalent (>>8 is /256), and the former results in rounding
00048            errors that can turn colors black fast after repeated blending */
00049         int mfac= 255-fac;
00050 
00051         cp[0]= (mfac*cp1[0]+fac*cp2[0])/255;
00052         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
00053         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
00054 }
00055 
00056 static void blend_color_add(char *cp, char *cp1, char *cp2, int fac)
00057 {
00058         int temp;
00059 
00060         temp= cp1[0] + ((fac*cp2[0])/255);
00061         if(temp>254) cp[0]= 255; else cp[0]= temp;
00062         temp= cp1[1] + ((fac*cp2[1])/255);
00063         if(temp>254) cp[1]= 255; else cp[1]= temp;
00064         temp= cp1[2] + ((fac*cp2[2])/255);
00065         if(temp>254) cp[2]= 255; else cp[2]= temp;
00066 }
00067 
00068 static void blend_color_sub(char *cp, char *cp1, char *cp2, int fac)
00069 {
00070         int temp;
00071 
00072         temp= cp1[0] - ((fac*cp2[0])/255);
00073         if(temp<0) cp[0]= 0; else cp[0]= temp;
00074         temp= cp1[1] - ((fac*cp2[1])/255);
00075         if(temp<0) cp[1]= 0; else cp[1]= temp;
00076         temp= cp1[2] - ((fac*cp2[2])/255);
00077         if(temp<0) cp[2]= 0; else cp[2]= temp;
00078 }
00079 
00080 static void blend_color_mul(char *cp, char *cp1, char *cp2, int fac)
00081 {
00082         int mfac= 255-fac;
00083         
00084         /* first mul, then blend the fac */
00085         cp[0]= (mfac*cp1[0] + fac*((cp1[0]*cp2[0])/255))/255;
00086         cp[1]= (mfac*cp1[1] + fac*((cp1[1]*cp2[1])/255))/255;
00087         cp[2]= (mfac*cp1[2] + fac*((cp1[2]*cp2[2])/255))/255;
00088 }
00089 
00090 static void blend_color_lighten(char *cp, char *cp1, char *cp2, int fac)
00091 {
00092         /* See if are lighter, if so mix, else dont do anything.
00093         if the paint col is darker then the original, then ignore */
00094         if (cp1[0]+cp1[1]+cp1[2] > cp2[0]+cp2[1]+cp2[2]) {
00095                 cp[0]= cp1[0];
00096                 cp[1]= cp1[1];
00097                 cp[2]= cp1[2];
00098         }
00099         else
00100                 blend_color_mix(cp, cp1, cp2, fac);
00101 }
00102 
00103 static void blend_color_darken(char *cp, char *cp1, char *cp2, int fac)
00104 {
00105         /* See if were darker, if so mix, else dont do anything.
00106         if the paint col is brighter then the original, then ignore */
00107         if (cp1[0]+cp1[1]+cp1[2] < cp2[0]+cp2[1]+cp2[2]) {
00108                 cp[0]= cp1[0];
00109                 cp[1]= cp1[1];
00110                 cp[2]= cp1[2];
00111         }
00112         else
00113                 blend_color_mix(cp, cp1, cp2, fac);
00114 }
00115 
00116 unsigned int IMB_blend_color(unsigned int src1, unsigned int src2, int fac, IMB_BlendMode mode)
00117 {
00118         unsigned int dst;
00119         int temp;
00120         char *cp, *cp1, *cp2;
00121 
00122         if (fac==0)
00123                 return src1;
00124 
00125         cp = (char*)&dst;
00126         cp1 = (char*)&src1;
00127         cp2 = (char*)&src2;
00128 
00129         switch (mode) {
00130                 case IMB_BLEND_MIX:
00131                         blend_color_mix(cp, cp1, cp2, fac); break;
00132                 case IMB_BLEND_ADD:
00133                         blend_color_add(cp, cp1, cp2, fac); break;
00134                 case IMB_BLEND_SUB:
00135                         blend_color_sub(cp, cp1, cp2, fac); break;
00136                 case IMB_BLEND_MUL:
00137                         blend_color_mul(cp, cp1, cp2, fac); break;
00138                 case IMB_BLEND_LIGHTEN:
00139                         blend_color_lighten(cp, cp1, cp2, fac); break;
00140                 case IMB_BLEND_DARKEN:
00141                         blend_color_darken(cp, cp1, cp2, fac); break;
00142                 default:
00143                         cp[0]= cp1[0];
00144                         cp[1]= cp1[1];
00145                         cp[2]= cp1[2];
00146         }
00147 
00148         if (mode == IMB_BLEND_ERASE_ALPHA) {
00149                 temp= (cp1[3] - fac*cp2[3]/255);
00150                 cp[3]= (temp < 0)? 0: temp;
00151         }
00152         else { /* this does ADD_ALPHA also */
00153                 temp= (cp1[3] + fac*cp2[3]/255);
00154                 cp[3]= (temp > 255)? 255: temp;
00155         }
00156 
00157         return dst;
00158 }
00159 
00160 static void blend_color_mix_float(float *cp, float *cp1, float *cp2, float fac)
00161 {
00162         float mfac= 1.0-fac;
00163         cp[0]= mfac*cp1[0] + fac*cp2[0];
00164         cp[1]= mfac*cp1[1] + fac*cp2[1];
00165         cp[2]= mfac*cp1[2] + fac*cp2[2];
00166 }
00167 
00168 static void blend_color_add_float(float *cp, float *cp1, float *cp2, float fac)
00169 {
00170         cp[0] = cp1[0] + fac*cp2[0];
00171         cp[1] = cp1[1] + fac*cp2[1];
00172         cp[2] = cp1[2] + fac*cp2[2];
00173 
00174         if (cp[0] > 1.0f) cp[0]= 1.0f;
00175         if (cp[1] > 1.0f) cp[1]= 1.0f;
00176         if (cp[2] > 1.0f) cp[2]= 1.0f;
00177 }
00178 
00179 static void blend_color_sub_float(float *cp, float *cp1, float *cp2, float fac)
00180 {
00181         cp[0] = cp1[0] - fac*cp2[0];
00182         cp[1] = cp1[1] - fac*cp2[1];
00183         cp[2] = cp1[2] - fac*cp2[2];
00184 
00185         if (cp[0] < 0.0f) cp[0]= 0.0f;
00186         if (cp[1] < 0.0f) cp[1]= 0.0f;
00187         if (cp[2] < 0.0f) cp[2]= 0.0f;
00188 }
00189 
00190 static void blend_color_mul_float(float *cp, float *cp1, float *cp2, float fac)
00191 {
00192         float mfac= 1.0-fac;
00193         
00194         cp[0]= mfac*cp1[0] + fac*(cp1[0]*cp2[0]);
00195         cp[1]= mfac*cp1[1] + fac*(cp1[1]*cp2[1]);
00196         cp[2]= mfac*cp1[2] + fac*(cp1[2]*cp2[2]);
00197 }
00198 
00199 static void blend_color_lighten_float(float *cp, float *cp1, float *cp2, float fac)
00200 {
00201         /* See if are lighter, if so mix, else dont do anything.
00202         if the pafloat col is darker then the original, then ignore */
00203         if (cp1[0]+cp1[1]+cp1[2] > cp2[0]+cp2[1]+cp2[2]) {
00204                 cp[0]= cp1[0];
00205                 cp[1]= cp1[1];
00206                 cp[2]= cp1[2];
00207         }
00208         else
00209                 blend_color_mix_float(cp, cp1, cp2, fac);
00210 }
00211 
00212 static void blend_color_darken_float(float *cp, float *cp1, float *cp2, float fac)
00213 {
00214         /* See if were darker, if so mix, else dont do anything.
00215         if the pafloat col is brighter then the original, then ignore */
00216         if (cp1[0]+cp1[1]+cp1[2] < cp2[0]+cp2[1]+cp2[2]) {
00217                 cp[0]= cp1[0];
00218                 cp[1]= cp1[1];
00219                 cp[2]= cp1[2];
00220         }
00221         else
00222                 blend_color_mix_float(cp, cp1, cp2, fac);
00223 }
00224 
00225 void IMB_blend_color_float(float *dst, float *src1, float *src2, float fac, IMB_BlendMode mode)
00226 {
00227         if (fac==0) {
00228                 dst[0]= src1[0];
00229                 dst[1]= src1[1];
00230                 dst[2]= src1[2];
00231                 dst[3]= src1[3];
00232                 return;
00233         }
00234 
00235         switch (mode) {
00236                 case IMB_BLEND_MIX:
00237                         blend_color_mix_float(dst, src1, src2, fac); break;
00238                 case IMB_BLEND_ADD:
00239                         blend_color_add_float(dst, src1, src2, fac); break;
00240                 case IMB_BLEND_SUB:
00241                         blend_color_sub_float(dst, src1, src2, fac); break;
00242                 case IMB_BLEND_MUL:
00243                         blend_color_mul_float(dst, src1, src2, fac); break;
00244                 case IMB_BLEND_LIGHTEN:
00245                         blend_color_lighten_float(dst, src1, src2, fac); break;
00246                 case IMB_BLEND_DARKEN:
00247                         blend_color_darken_float(dst, src1, src2, fac); break;
00248                 default:
00249                         dst[0]= src1[0];
00250                         dst[1]= src1[1];
00251                         dst[2]= src1[2];
00252         }
00253 
00254         if (mode == IMB_BLEND_ERASE_ALPHA) {
00255                 dst[3]= (src1[3] - fac*src2[3]);
00256                 if (dst[3] < 0.0f) dst[3] = 0.0f;
00257         }
00258         else { /* this does ADD_ALPHA also */
00259                 dst[3]= (src1[3] + fac*src2[3]);
00260                 if (dst[3] > 1.0f) dst[3] = 1.0f;
00261         }
00262 }
00263 
00264 /* clipping */
00265 
00266 void IMB_rectclip(struct ImBuf *dbuf, struct ImBuf *sbuf, int *destx, 
00267         int *desty, int *srcx, int *srcy, int *width, int *height)
00268 {
00269         int tmp;
00270 
00271         if (dbuf == NULL) return;
00272         
00273         if (*destx < 0) {
00274                 *srcx -= *destx;
00275                 *width += *destx;
00276                 *destx = 0;
00277         }
00278         if (*srcx < 0) {
00279                 *destx -= *srcx;
00280                 *width += *srcx;
00281                 *srcx = 0;
00282         }
00283         if (*desty < 0) {
00284                 *srcy -= *desty;
00285                 *height += *desty;
00286                 *desty = 0;
00287         }
00288         if (*srcy < 0) {
00289                 *desty -= *srcy;
00290                 *height += *srcy;
00291                 *srcy = 0;
00292         }
00293 
00294         tmp = dbuf->x - *destx;
00295         if (*width > tmp) *width = tmp;
00296         tmp = dbuf->y - *desty;
00297         if (*height > tmp) *height = tmp;
00298 
00299         if (sbuf) {
00300                 tmp = sbuf->x - *srcx;
00301                 if (*width > tmp) *width = tmp;
00302                 tmp = sbuf->y - *srcy;
00303                 if (*height > tmp) *height = tmp;
00304         }
00305 
00306         if ((*height <= 0) || (*width <= 0)) {
00307                 *width = 0;
00308                 *height = 0;
00309         }
00310 }
00311 
00312 /* copy and blend */
00313 
00314 void IMB_rectcpy(struct ImBuf *dbuf, struct ImBuf *sbuf, int destx, 
00315         int desty, int srcx, int srcy, int width, int height)
00316 {
00317         IMB_rectblend(dbuf, sbuf, destx, desty, srcx, srcy, width, height,
00318                 IMB_BLEND_COPY);
00319 }
00320 
00321 void IMB_rectblend(struct ImBuf *dbuf, struct ImBuf *sbuf, int destx, 
00322         int desty, int srcx, int srcy, int width, int height, IMB_BlendMode mode)
00323 {
00324         unsigned int *drect = NULL, *srect = NULL, *dr, *sr;
00325         float *drectf = NULL, *srectf = NULL, *drf, *srf;
00326         int do_float, do_char, srcskip, destskip, x;
00327 
00328         if (dbuf == NULL) return;
00329 
00330         IMB_rectclip(dbuf, sbuf, &destx, &desty, &srcx, &srcy, &width, &height);
00331 
00332         if (width == 0 || height == 0) return;
00333         if (sbuf && sbuf->channels!=4) return;
00334         if (dbuf->channels!=4) return;
00335         
00336         do_char = (sbuf && sbuf->rect && dbuf->rect);
00337         do_float = (sbuf && sbuf->rect_float && dbuf->rect_float);
00338 
00339         if (do_char) drect = dbuf->rect + desty * dbuf->x + destx;
00340         if (do_float) drectf = dbuf->rect_float + (desty * dbuf->x + destx)*4;
00341 
00342         destskip = dbuf->x;
00343 
00344         if (sbuf) {
00345                 if (do_char) srect = sbuf->rect + srcy * sbuf->x + srcx;
00346                 if (do_float) srectf = sbuf->rect_float + (srcy * sbuf->x + srcx)*4;
00347                 srcskip = sbuf->x;
00348         } else {
00349                 srect = drect;
00350                 srectf = drectf;
00351                 srcskip = destskip;
00352         }
00353 
00354         if (mode == IMB_BLEND_COPY) {
00355                 /* copy */
00356                 for (;height > 0; height--) {
00357                         if (do_char) {
00358                                 memcpy(drect,srect, width * sizeof(int));
00359                                 drect += destskip;
00360                                 srect += srcskip;
00361                         }
00362 
00363                         if (do_float) {
00364                                 memcpy(drectf,srectf, width * sizeof(float) * 4);
00365                                 drectf += destskip*4;
00366                                 srectf += srcskip*4;
00367                         }
00368                 }
00369         }
00370         else if (mode == IMB_BLEND_COPY_RGB) {
00371                 /* copy rgb only */
00372                 for (;height > 0; height--) {
00373                         if (do_char) {
00374                                 dr = drect;
00375                                 sr = srect;
00376                                 for (x=width; x > 0; x--, dr++, sr++) {
00377                                         ((char*)dr)[0]= ((char*)sr)[0];
00378                                         ((char*)dr)[1]= ((char*)sr)[1];
00379                                         ((char*)dr)[2]= ((char*)sr)[2];
00380                                 }
00381                                 drect += destskip;
00382                                 srect += srcskip;
00383                         }
00384 
00385                         if (do_float) {
00386                                 drf = drectf;
00387                                 srf = srectf;
00388                                 for (x=width; x > 0; x--, drf+=4, srf+=4) {
00389                                         drf[0]= srf[0];
00390                                         drf[1]= srf[1];
00391                                         drf[2]= srf[2];
00392                                 }
00393                                 drectf += destskip*4;
00394                                 srectf += srcskip*4;
00395                         }
00396                 }
00397         }
00398         else if (mode == IMB_BLEND_COPY_ALPHA) {
00399                 /* copy alpha only */
00400                 for (;height > 0; height--) {
00401                         if (do_char) {
00402                                 dr = drect;
00403                                 sr = srect;
00404                                 for (x=width; x > 0; x--, dr++, sr++)
00405                                         ((char*)dr)[3]= ((char*)sr)[3];
00406                                 drect += destskip;
00407                                 srect += srcskip;
00408                         }
00409 
00410                         if (do_float) {
00411                                 drf = drectf;
00412                                 srf = srectf;
00413                                 for (x=width; x > 0; x--, drf+=4, srf+=4)
00414                                         drf[3]= srf[3];
00415                                 drectf += destskip*4;
00416                                 srectf += srcskip*4;
00417                         }
00418                 }
00419         }
00420         else {
00421                 /* blend */
00422                 for (;height > 0; height--) {
00423                         if (do_char) {
00424                                 dr = drect;
00425                                 sr = srect;
00426                                 for (x=width; x > 0; x--, dr++, sr++)
00427                                         *dr = IMB_blend_color(*dr, *sr, ((char*)sr)[3], mode);
00428 
00429                                 drect += destskip;
00430                                 srect += srcskip;
00431                         }
00432 
00433                         if (do_float) {
00434                                 drf = drectf;
00435                                 srf = srectf;
00436                                 for (x=width; x > 0; x--, drf+=4, srf+=4)
00437                                         IMB_blend_color_float(drf, drf, srf, srf[3], mode);
00438 
00439                                 drectf += destskip*4;
00440                                 srectf += srcskip*4;
00441                         }               
00442                 }
00443         }
00444 }
00445 
00446 /* fill */
00447 
00448 void IMB_rectfill(struct ImBuf *drect, float col[4])
00449 {
00450         int num;
00451 
00452         if(drect->rect) {
00453                 unsigned int *rrect = drect->rect;
00454                 char ccol[4];
00455                 
00456                 ccol[0]= (int)(col[0]*255);
00457                 ccol[1]= (int)(col[1]*255);
00458                 ccol[2]= (int)(col[2]*255);
00459                 ccol[3]= (int)(col[3]*255);
00460                 
00461                 num = drect->x * drect->y;
00462                 for (;num > 0; num--)
00463                         *rrect++ = *((unsigned int*)ccol);
00464         }
00465         
00466         if(drect->rect_float) {
00467                 float *rrectf = drect->rect_float;
00468                 
00469                 num = drect->x * drect->y;
00470                 for (;num > 0; num--) {
00471                         *rrectf++ = col[0];
00472                         *rrectf++ = col[1];
00473                         *rrectf++ = col[2];
00474                         *rrectf++ = col[3];
00475                 }
00476         }       
00477 }
00478 
00479 
00480 void buf_rectfill_area(unsigned char *rect, float *rectf, int width, int height, float *col, int x1, int y1, int x2, int y2)
00481 {
00482         int i, j;
00483         float a; /* alpha */
00484         float ai; /* alpha inverted */
00485         float aich; /* alpha, inverted, ai/255.0 - Convert char to float at the same time */
00486         if ((!rect && !rectf) || (!col) || col[3]==0.0)
00487                 return;
00488         
00489         /* sanity checks for coords */
00490         CLAMP(x1, 0, width);
00491         CLAMP(x2, 0, width);
00492         CLAMP(y1, 0, height);
00493         CLAMP(y2, 0, height);
00494 
00495         if (x1>x2) SWAP(int,x1,x2);
00496         if (y1>y2) SWAP(int,y1,y2);
00497         if (x1==x2 || y1==y2) return;
00498         
00499         a = col[3];
00500         ai = 1-a;
00501         aich = ai/255.0f;
00502 
00503         if (rect) {
00504                 unsigned char *pixel; 
00505                 unsigned char chr=0, chg=0, chb=0;
00506                 float fr=0, fg=0, fb=0;
00507                 
00508                 if (a == 1.0) {
00509                         chr = FTOCHAR(col[0]);
00510                         chg = FTOCHAR(col[1]);
00511                         chb = FTOCHAR(col[2]);
00512                 } else {
00513                         fr = col[0]*a;
00514                         fg = col[1]*a;
00515                         fb = col[2]*a;
00516                 }
00517                 for (j = 0; j < y2-y1; j++) {
00518                         for (i = 0; i < x2-x1; i++) {
00519                                 pixel = rect + 4 * (((y1 + j) * width) + (x1 + i));
00520                                 if (pixel >= rect && pixel < rect+ (4 * (width * height))) {
00521                                         if (a == 1.0) {
00522                                                 pixel[0] = chr;
00523                                                 pixel[1] = chg;
00524                                                 pixel[2] = chb;
00525                                         } else {
00526                                                 pixel[0] = (char)((fr + ((float)pixel[0]*aich))*255.0f);
00527                                                 pixel[1] = (char)((fg + ((float)pixel[1]*aich))*255.0f);
00528                                                 pixel[2] = (char)((fb + ((float)pixel[2]*aich))*255.0f);
00529                                         }
00530                                 }
00531                         }
00532                 }
00533         }
00534         
00535         if (rectf) {
00536                 float *pixel;
00537                 for (j = 0; j < y2-y1; j++) {
00538                         for (i = 0; i < x2-x1; i++) {
00539                                 pixel = rectf + 4 * (((y1 + j) * width) + (x1 + i));
00540                                 if (a == 1.0) {
00541                                         pixel[0] = col[0];
00542                                         pixel[1] = col[1];
00543                                         pixel[2] = col[2];
00544                                 } else {
00545                                         pixel[0] = (col[0]*a) + (pixel[0]*ai);
00546                                         pixel[1] = (col[1]*a) + (pixel[1]*ai);
00547                                         pixel[2] = (col[2]*a) + (pixel[2]*ai);
00548                                 }
00549                         }
00550                 }
00551         }
00552 }
00553 
00554 void IMB_rectfill_area(struct ImBuf *ibuf, float *col, int x1, int y1, int x2, int y2)
00555 {
00556         if (!ibuf) return;
00557         buf_rectfill_area((unsigned char *) ibuf->rect, ibuf->rect_float, ibuf->x, ibuf->y, col, x1, y1, x2, y2);
00558 }