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 color_put_grayscale(&bg, background);
33 uns a = 255 * bg, b = bg;
34 # define IMAGE_WALK_PREFIX(x) walk_##x
35 # define IMAGE_WALK_INLINE
36 # define IMAGE_WALK_DOUBLE
37 # define IMAGE_WALK_UNROLL 4
38 # define IMAGE_WALK_IMAGE dest
39 # define IMAGE_WALK_SEC_IMAGE src
40 # define IMAGE_WALK_COL_STEP 1
41 # define IMAGE_WALK_SEC_COL_STEP 2
42 # define IMAGE_WALK_DO_STEP do{ walk_pos[0] = merge_func(walk_sec_pos[0], walk_sec_pos[1], a, b); }while(0)
43 # include "images/image-walk.h"
46 /* RGBA to RGB or aligned RGB */
47 else if (src->pixel_size == 4)
49 ASSERT((src->flags & IMAGE_ALPHA) && dest->pixel_size >= 3 && !(dest->flags & IMAGE_ALPHA));
51 color_put_rgb(bg, background);
52 uns a0 = 255 * bg[0], b0 = bg[0];
53 uns a1 = 255 * bg[1], b1 = bg[1];
54 uns a2 = 255 * bg[2], b2 = bg[2];
55 # define IMAGE_WALK_PREFIX(x) walk_##x
56 # define IMAGE_WALK_INLINE
57 # define IMAGE_WALK_DOUBLE
58 # define IMAGE_WALK_UNROLL 2
59 # define IMAGE_WALK_IMAGE dest
60 # define IMAGE_WALK_SEC_IMAGE src
61 # define IMAGE_WALK_SEC_COL_STEP 4
62 # define IMAGE_WALK_DO_STEP do{ \
63 walk_pos[0] = merge_func(walk_sec_pos[0], walk_sec_pos[3], a0, b0); \
64 walk_pos[1] = merge_func(walk_sec_pos[1], walk_sec_pos[3], a1, b1); \
65 walk_pos[2] = merge_func(walk_sec_pos[2], walk_sec_pos[3], a2, b2); \
67 # include "images/image-walk.h"