2 * Image Library -- Alpha channels
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "images/images.h"
14 #include "images/color.h"
17 merge_func(uns value, uns alpha, uns acoef, uns bcoef)
19 return ((uns)(acoef + (int)alpha * (int)(value - bcoef)) * (0xffffffffU / 255 / 255)) >> 24;
23 image_apply_background(struct image_thread *thread UNUSED, struct image *dest, struct image *src, struct color *background)
25 DBG("image_apply_background()");
28 if (src->pixel_size == 2)
30 ASSERT(dest->pixel_size == 1);
32 if (background->color_space)
33 color_put_grayscale(&bg, background);
36 uns a = 255 * bg, b = bg;
37 # define IMAGE_WALK_PREFIX(x) walk_##x
38 # define IMAGE_WALK_INLINE
39 # define IMAGE_WALK_DOUBLE
40 # define IMAGE_WALK_UNROLL 4
41 # define IMAGE_WALK_IMAGE dest
42 # define IMAGE_WALK_SEC_IMAGE src
43 # define IMAGE_WALK_COL_STEP 1
44 # define IMAGE_WALK_SEC_COL_STEP 2
45 # define IMAGE_WALK_DO_STEP do{ walk_pos[0] = merge_func(walk_sec_pos[0], walk_sec_pos[1], a, b); }while(0)
46 # include "images/image-walk.h"
49 /* RGBA to RGB or aligned RGB */
50 else if (src->pixel_size == 4)
52 ASSERT((src->flags & IMAGE_ALPHA) && dest->pixel_size >= 3 && !(dest->flags & IMAGE_ALPHA));
54 if (background->color_space)
55 color_put_rgb(bg, background);
57 bg[0] = bg[1] = bg[2] = 0;
58 uns a0 = 255 * bg[0], b0 = bg[0];
59 uns a1 = 255 * bg[1], b1 = bg[1];
60 uns a2 = 255 * bg[2], b2 = bg[2];
61 # define IMAGE_WALK_PREFIX(x) walk_##x
62 # define IMAGE_WALK_INLINE
63 # define IMAGE_WALK_DOUBLE
64 # define IMAGE_WALK_UNROLL 2
65 # define IMAGE_WALK_IMAGE dest
66 # define IMAGE_WALK_SEC_IMAGE src
67 # define IMAGE_WALK_SEC_COL_STEP 4
68 # define IMAGE_WALK_DO_STEP do{ \
69 walk_pos[0] = merge_func(walk_sec_pos[0], walk_sec_pos[3], a0, b0); \
70 walk_pos[1] = merge_func(walk_sec_pos[1], walk_sec_pos[3], a1, b1); \
71 walk_pos[2] = merge_func(walk_sec_pos[2], walk_sec_pos[3], a2, b2); \
73 # include "images/image-walk.h"