1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
/**
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
* allocimbuf.c
*
* $Id$
*/
#include "BLI_blenlib.h"
#include "imbuf.h"
#include "imbuf_patch.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "IMB_allocimbuf.h"
/* blend modes */
static void blend_color_mix(char *cp, char *cp1, char *cp2, int fac)
{
/* this and other blending modes previously used >>8 instead of /255. both
are not equivalent (>>8 is /256), and the former results in rounding
errors that can turn colors black fast after repeated blending */
int mfac= 255-fac;
cp[0]= (mfac*cp1[0]+fac*cp2[0])/255;
cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
}
static void blend_color_add(char *cp, char *cp1, char *cp2, int fac)
{
int temp;
temp= cp1[0] + ((fac*cp2[0])/255);
if(temp>254) cp[0]= 255; else cp[0]= temp;
temp= cp1[1] + ((fac*cp2[1])/255);
if(temp>254) cp[1]= 255; else cp[1]= temp;
temp= cp1[2] + ((fac*cp2[2])/255);
if(temp>254) cp[2]= 255; else cp[2]= temp;
}
static void blend_color_sub(char *cp, char *cp1, char *cp2, int fac)
{
int temp;
temp= cp1[0] - ((fac*cp2[0])/255);
if(temp<0) cp[0]= 0; else cp[0]= temp;
temp= cp1[1] - ((fac*cp2[1])/255);
if(temp<0) cp[1]= 0; else cp[1]= temp;
temp= cp1[2] - ((fac*cp2[2])/255);
if(temp<0) cp[2]= 0; else cp[2]= temp;
}
static void blend_color_mul(char *cp, char *cp1, char *cp2, int fac)
{
int mfac= 255-fac;
/* first mul, then blend the fac */
cp[0]= (mfac*cp1[0] + fac*((cp1[0]*cp2[0])/255))/255;
cp[1]= (mfac*cp1[1] + fac*((cp1[1]*cp2[1])/255))/255;
cp[2]= (mfac*cp1[2] + fac*((cp1[2]*cp2[2])/255))/255;
}
static void blend_color_lighten(char *cp, char *cp1, char *cp2, int fac)
{
/* See if are lighter, if so mix, else dont do anything.
if the paint col is darker then the original, then ignore */
if (cp1[0]+cp1[1]+cp1[2] > cp2[0]+cp2[1]+cp2[2]) {
cp[0]= cp1[0];
cp[1]= cp1[1];
cp[2]= cp1[2];
}
else
blend_color_mix(cp, cp1, cp2, fac);
}
static void blend_color_darken(char *cp, char *cp1, char *cp2, int fac)
{
/* See if were darker, if so mix, else dont do anything.
if the paint col is brighter then the original, then ignore */
if (cp1[0]+cp1[1]+cp1[2] < cp2[0]+cp2[1]+cp2[2]) {
cp[0]= cp1[0];
cp[1]= cp1[1];
cp[2]= cp1[2];
}
else
blend_color_mix(cp, cp1, cp2, fac);
}
unsigned int IMB_blend_color(unsigned int src1, unsigned int src2, int fac, IMB_BlendMode mode)
{
unsigned int dst;
int temp;
char *cp, *cp1, *cp2;
if (fac==0)
return src1;
cp = (char*)&dst;
cp1 = (char*)&src1;
cp2 = (char*)&src2;
switch (mode) {
case IMB_BLEND_MIX:
blend_color_mix(cp, cp1, cp2, fac); break;
case IMB_BLEND_ADD:
blend_color_add(cp, cp1, cp2, fac); break;
case IMB_BLEND_SUB:
blend_color_sub(cp, cp1, cp2, fac); break;
case IMB_BLEND_MUL:
blend_color_mul(cp, cp1, cp2, fac); break;
case IMB_BLEND_LIGHTEN:
blend_color_lighten(cp, cp1, cp2, fac); break;
case IMB_BLEND_DARKEN:
blend_color_darken(cp, cp1, cp2, fac); break;
default:
cp[0]= cp1[0];
cp[1]= cp1[1];
cp[2]= cp1[2];
}
if (mode == IMB_BLEND_ERASE_ALPHA) {
temp= (cp1[3] - fac*cp2[3]/255);
cp[3]= (temp < 0)? 0: temp;
}
else { /* this does ADD_ALPHA also */
temp= (cp1[3] + fac*cp2[3]/255);
cp[3]= (temp > 255)? 255: temp;
}
return dst;
}
static void blend_color_mix_float(float *cp, float *cp1, float *cp2, float fac)
{
float mfac= 1.0-fac;
cp[0]= mfac*cp1[0] + fac*cp2[0];
cp[1]= mfac*cp1[1] + fac*cp2[1];
cp[2]= mfac*cp1[2] + fac*cp2[2];
}
static void blend_color_add_float(float *cp, float *cp1, float *cp2, float fac)
{
cp[0] = cp1[0] + fac*cp2[0];
cp[1] = cp1[1] + fac*cp2[1];
cp[2] = cp1[2] + fac*cp2[2];
if (cp[0] > 1.0f) cp[0]= 1.0f;
if (cp[1] > 1.0f) cp[1]= 1.0f;
if (cp[2] > 1.0f) cp[2]= 1.0f;
}
static void blend_color_sub_float(float *cp, float *cp1, float *cp2, float fac)
{
cp[0] = cp1[0] - fac*cp2[0];
cp[1] = cp1[1] - fac*cp2[1];
cp[2] = cp1[2] - fac*cp2[2];
if (cp[0] < 0.0f) cp[0]= 0.0f;
if (cp[1] < 0.0f) cp[1]= 0.0f;
if (cp[2] < 0.0f) cp[2]= 0.0f;
}
static void blend_color_mul_float(float *cp, float *cp1, float *cp2, float fac)
{
float mfac= 1.0-fac;
cp[0]= mfac*cp1[0] + fac*(cp1[0]*cp2[0]);
cp[1]= mfac*cp1[1] + fac*(cp1[1]*cp2[1]);
cp[2]= mfac*cp1[2] + fac*(cp1[2]*cp2[2]);
}
static void blend_color_lighten_float(float *cp, float *cp1, float *cp2, float fac)
{
/* See if are lighter, if so mix, else dont do anything.
if the pafloat col is darker then the original, then ignore */
if (cp1[0]+cp1[1]+cp1[2] > cp2[0]+cp2[1]+cp2[2]) {
cp[0]= cp1[0];
cp[1]= cp1[1];
cp[2]= cp1[2];
}
else
blend_color_mix_float(cp, cp1, cp2, fac);
}
static void blend_color_darken_float(float *cp, float *cp1, float *cp2, float fac)
{
/* See if were darker, if so mix, else dont do anything.
if the pafloat col is brighter then the original, then ignore */
if (cp1[0]+cp1[1]+cp1[2] < cp2[0]+cp2[1]+cp2[2]) {
cp[0]= cp1[0];
cp[1]= cp1[1];
cp[2]= cp1[2];
}
else
blend_color_mix_float(cp, cp1, cp2, fac);
}
void IMB_blend_color_float(float *dst, float *src1, float *src2, float fac, IMB_BlendMode mode)
{
if (fac==0) {
dst[0]= src1[0];
dst[1]= src1[1];
dst[2]= src1[2];
dst[3]= src1[3];
return;
}
switch (mode) {
case IMB_BLEND_MIX:
blend_color_mix_float(dst, src1, src2, fac); break;
case IMB_BLEND_ADD:
blend_color_add_float(dst, src1, src2, fac); break;
case IMB_BLEND_SUB:
blend_color_sub_float(dst, src1, src2, fac); break;
case IMB_BLEND_MUL:
blend_color_mul_float(dst, src1, src2, fac); break;
case IMB_BLEND_LIGHTEN:
blend_color_lighten_float(dst, src1, src2, fac); break;
case IMB_BLEND_DARKEN:
blend_color_darken_float(dst, src1, src2, fac); break;
default:
dst[0]= src1[0];
dst[1]= src1[1];
dst[2]= src1[2];
}
if (mode == IMB_BLEND_ERASE_ALPHA) {
dst[3]= (src1[3] - fac*src2[3]);
if (dst[3] < 0.0f) dst[3] = 0.0f;
}
else { /* this does ADD_ALPHA also */
dst[3]= (src1[3] + fac*src2[3]);
if (dst[3] > 1.0f) dst[3] = 1.0f;
}
}
/* clipping */
void IMB_rectclip(struct ImBuf *dbuf, struct ImBuf *sbuf, int *destx,
int *desty, int *srcx, int *srcy, int *width, int *height)
{
int tmp;
if (dbuf == NULL) return;
if (*destx < 0) {
*srcx -= *destx;
*width += *destx;
*destx = 0;
}
if (*srcx < 0) {
*destx -= *srcx;
*width += *destx;
*srcx = 0;
}
if (*desty < 0) {
*srcy -= *desty;
*height += *desty;
*desty = 0;
}
if (*srcy < 0) {
*desty -= *srcy;
*height += *desty;
*srcy = 0;
}
tmp = dbuf->x - *destx;
if (*width > tmp) *width = tmp;
tmp = dbuf->y - *desty;
if (*height > tmp) *height = tmp;
if (sbuf) {
tmp = sbuf->x - *srcx;
if (*width > tmp) *width = tmp;
tmp = sbuf->y - *srcy;
if (*height > tmp) *height = tmp;
}
if ((*height <= 0) || (*width <= 0)) {
*width = 0;
*height = 0;
}
}
/* copy and blend */
void IMB_rectcpy(struct ImBuf *dbuf, struct ImBuf *sbuf, int destx,
int desty, int srcx, int srcy, int width, int height)
{
IMB_rectblend(dbuf, sbuf, destx, desty, srcx, srcy, width, height,
IMB_BLEND_COPY);
}
void IMB_rectblend(struct ImBuf *dbuf, struct ImBuf *sbuf, int destx,
int desty, int srcx, int srcy, int width, int height, IMB_BlendMode mode)
{
unsigned int *drect = NULL, *srect = NULL, *dr, *sr;
float *drectf = NULL, *srectf = NULL, *drf, *srf;
int do_float, do_char, srcskip, destskip, x;
if (dbuf == NULL) return;
IMB_rectclip(dbuf, sbuf, &destx, &desty, &srcx, &srcy, &width, &height);
if (width == 0 || height == 0) return;
if (sbuf && sbuf->channels!=4) return;
if (dbuf->channels!=4) return;
do_char = (sbuf && sbuf->rect && dbuf->rect);
do_float = (sbuf && sbuf->rect_float && dbuf->rect_float);
if (do_char) drect = dbuf->rect + desty * dbuf->x + destx;
if (do_float) drectf = dbuf->rect_float + (desty * dbuf->x + destx)*4;
destskip = dbuf->x;
if (sbuf) {
if (do_char) srect = sbuf->rect + srcy * sbuf->x + srcx;
if (do_float) srectf = sbuf->rect_float + (srcy * sbuf->x + srcx)*4;
srcskip = sbuf->x;
} else {
srect = drect;
srectf = drectf;
srcskip = destskip;
}
if (mode == IMB_BLEND_COPY) {
/* copy */
for (;height > 0; height--) {
if (do_char) {
memcpy(drect,srect, width * sizeof(int));
drect += destskip;
srect += srcskip;
}
if (do_float) {
memcpy(drectf,srectf, width * sizeof(float) * 4);
drectf += destskip*4;
srectf += srcskip*4;
}
}
}
else if (mode == IMB_BLEND_COPY_RGB) {
/* copy rgb only */
for (;height > 0; height--) {
if (do_char) {
dr = drect;
sr = srect;
for (x=width; x > 0; x--, dr++, sr++) {
((char*)dr)[0]= ((char*)sr)[0];
((char*)dr)[1]= ((char*)sr)[1];
((char*)dr)[2]= ((char*)sr)[2];
}
drect += destskip;
srect += srcskip;
}
if (do_float) {
drf = drectf;
srf = srectf;
for (x=width; x > 0; x--, drf+=4, srf+=4) {
drf[0]= srf[0];
drf[1]= srf[1];
drf[2]= srf[2];
}
drectf += destskip*4;
srectf += srcskip*4;
}
}
}
else if (mode == IMB_BLEND_COPY_ALPHA) {
/* copy alpha only */
for (;height > 0; height--) {
if (do_char) {
dr = drect;
sr = srect;
for (x=width; x > 0; x--, dr++, sr++)
((char*)dr)[3]= ((char*)sr)[3];
drect += destskip;
srect += srcskip;
}
if (do_float) {
drf = drectf;
srf = srectf;
for (x=width; x > 0; x--, drf+=4, srf+=4)
drf[3]= srf[3];
drectf += destskip*4;
srectf += srcskip*4;
}
}
}
else {
/* blend */
for (;height > 0; height--) {
if (do_char) {
dr = drect;
sr = srect;
for (x=width; x > 0; x--, dr++, sr++)
*dr = IMB_blend_color(*dr, *sr, ((char*)sr)[3], mode);
drect += destskip;
srect += srcskip;
}
if (do_float) {
drf = drectf;
srf = srectf;
for (x=width; x > 0; x--, drf+=4, srf+=4)
IMB_blend_color_float(drf, drf, srf, srf[3], mode);
drectf += destskip*4;
srectf += srcskip*4;
}
}
}
}
void IMB_rectblend_torus(struct ImBuf *dbuf, struct ImBuf *sbuf, int destx,
int desty, int srcx, int srcy, int width, int height, IMB_BlendMode mode)
{
int origw, origh, w, h;
if (dbuf->x == 0 || dbuf->y == 0 || sbuf->x == 0 || sbuf->y == 0)
return;
/* convert destination and source coordinates too be withing image */
destx = destx % dbuf->x;
if (destx < 0) destx += dbuf->x;
desty = desty % dbuf->y;
if (desty < 0) desty += dbuf->y;
srcx = srcx % sbuf->x;
if (srcx < 0) srcx += sbuf->x;
srcy = srcy % sbuf->y;
if (srcy < 0) srcy += sbuf->y;
/* clip width of blending area to destination imbuf, to avoid writing the
same pixel twice */
origw = w = (width > dbuf->x)? dbuf->x: width;
origh = h = (height > dbuf->y)? dbuf->y: height;
/* clip and blend */
IMB_rectclip(dbuf, sbuf, &destx, &desty, &srcx, &srcy, &w, &h);
IMB_rectblend(dbuf, sbuf, destx, desty, srcx, srcy, w, h, mode);
/* do 3 other rects if needed */
if (w < origw)
IMB_rectblend(dbuf, sbuf, (destx+w)%dbuf->x, desty, (srcx+w)%sbuf->x, srcy,
origw-w, h, mode);
if (h < origh)
IMB_rectblend(dbuf, sbuf, destx, (desty+h)%dbuf->y, srcx, (srcy+h)%sbuf->y,
w, origh-h, mode);
if ((w < origw) && (h < origh))
IMB_rectblend(dbuf, sbuf, (destx+w)%dbuf->x, (desty+h)%dbuf->y,
(srcx+w)%sbuf->x, (srcy+h)%sbuf->y, origw-w, origh-h, mode);
}
/* fill */
void IMB_rectfill(struct ImBuf *drect, float col[4])
{
int num;
unsigned int *rrect = drect->rect;
char ccol[4];
ccol[0]= (int)(col[0]*255);
ccol[1]= (int)(col[1]*255);
ccol[2]= (int)(col[2]*255);
ccol[3]= (int)(col[3]*255);
num = drect->x * drect->y;
for (;num > 0; num--)
*rrect++ = *((unsigned int*)ccol);
if(drect->rect_float) {
float *rrectf = drect->rect_float;
num = drect->x * drect->y;
for (;num > 0; num--) {
*rrectf++ = col[0];
*rrectf++ = col[1];
*rrectf++ = col[2];
*rrectf++ = col[3];
}
}
}
|