]> mj.ucw.cz Git - libucw.git/blob - images/alpha.c
f3a85c354a39eff48c0307bf7b461a9a19040066
[libucw.git] / images / alpha.c
1 /*
2  *      Image Library -- Alpha channels
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #define LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "images/images.h"
14 #include "images/color.h"
15
16 static inline uns
17 merge_func(uns value, uns alpha, uns acoef, uns bcoef)
18 {
19   return ((uns)(acoef + (int)alpha * (int)(value - bcoef)) * (0xffffffffU / 255 / 255)) >> 24;
20 }
21
22 int
23 image_apply_background(struct image_thread *thread UNUSED, struct image *dest, struct image *src, struct color *background)
24 {
25   DBG("image_apply_background()");
26
27   /* Grayscale */
28   if (src->pixel_size == 2)
29     {
30       ASSERT(dest->pixel_size == 1);
31       byte bg;
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"
44     }
45
46   /* RGBA to RGB or aligned RGB */
47   else if (src->pixel_size == 4)
48     {
49       ASSERT((src->flags & IMAGE_ALPHA) && dest->pixel_size >= 3 && !(dest->flags & IMAGE_ALPHA));
50       byte bg[3];
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); \
66         }while(0)
67 #     include "images/image-walk.h"
68     }
69   else
70     ASSERT(0);
71   return 1;
72 }