
#define COMPONENT_SIZE
#define MASK
#define ONE_HALF

#define A_SHIFT
#define R_SHIFT
#define G_SHIFT
#define A_MASK
#define R_MASK
#define G_MASK

#define RB_MASK
#define AG_MASK
#define RB_ONE_HALF
#define RB_MASK_PLUS_ONE

#define ALPHA_c(x) ((x) >> A_SHIFT)
#define RED_c(x) (((x) >> R_SHIFT) & MASK)
#define GREEN_c(x) (((x) >> G_SHIFT) & MASK)
#define BLUE_c(x) ((x) & MASK)

/*
 * Helper macros.
 */

#define MUL_UNc(a, b, t)						\
    ((t) = (a) * (b) + ONE_HALF, ((((t) >> G_SHIFT ) + (t) ) >> G_SHIFT ))

#define DIV_UNc(a, b)							\
    (((comp2_t) (a) * MASK) / (b))

#define ADD_UNc(x, y, t)				     \
    ((t) = (x) + (y),					     \
     (comp4_t) (comp1_t) ((t) | (0 - ((t) >> G_SHIFT))))

#define DIV_ONE_UNc(x)							\
    (((x) + ONE_HALF + (((x) + ONE_HALF) >> G_SHIFT)) >> G_SHIFT)

/*
 * The methods below use some tricks to be able to do two color
 * components at the same time.
 */

/*
 * x_rb = (x_rb * a) / 255
 */
#define UNc_rb_MUL_UNc(x, a, t)						\
    do									\
    {									\
	t  = ((x) & RB_MASK) * (a);					\
	t += RB_ONE_HALF;						\
	x = (t + ((t >> G_SHIFT) & RB_MASK)) >> G_SHIFT;		\
	x &= RB_MASK;							\
    } while (0)

/*
 * x_rb = min (x_rb + y_rb, 255)
 */
#define UNc_rb_ADD_UNc_rb(x, y, t)					\
    do									\
    {									\
	t = ((x) + (y));						\
	t |= RB_MASK_PLUS_ONE - ((t >> G_SHIFT) & RB_MASK);		\
	x = (t & RB_MASK);						\
    } while (0)

/*
 * x_rb = (x_rb * a_rb) / 255
 */
#define UNc_rb_MUL_UNc_rb(x, a, t)					\
    do									\
    {									\
	t  = (x & MASK) * (a & MASK);					\
	t |= (x & R_MASK) * ((a >> R_SHIFT) & MASK);			\
	t += RB_ONE_HALF;						\
	t = (t + ((t >> G_SHIFT) & RB_MASK)) >> G_SHIFT;		\
	x = t & RB_MASK;						\
    } while (0)

/*
 * x_c = (x_c * a) / 255
 */
#define UNcx4_MUL_UNc(x, a)						\
    do									\
    {									\
	comp4_t r1__, r2__, t__;					\
									\
	r1__ = (x);							\
	UNc_rb_MUL_UNc (r1__, (a), t__);				\
									\
	r2__ = (x) >> G_SHIFT;						\
	UNc_rb_MUL_UNc (r2__, (a), t__);				\
									\
	(x) = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
 * x_c = (x_c * a) / 255 + y_c
 */
#define UNcx4_MUL_UNc_ADD_UNcx4(x, a, y)				\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x);							\
	r2__ = (y) & RB_MASK;						\
	UNc_rb_MUL_UNc (r1__, (a), t__);				\
	UNc_rb_ADD_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = (x) >> G_SHIFT;						\
	r3__ = ((y) >> G_SHIFT) & RB_MASK;				\
	UNc_rb_MUL_UNc (r2__, (a), t__);				\
	UNc_rb_ADD_UNc_rb (r2__, r3__, t__);				\
									\
	(x) = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
 * x_c = (x_c * a + y_c * b) / 255
 */
#define UNcx4_MUL_UNc_ADD_UNcx4_MUL_UNc(x, a, y, b)			\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x);							\
	r2__ = (y);							\
	UNc_rb_MUL_UNc (r1__, (a), t__);				\
	UNc_rb_MUL_UNc (r2__, (b), t__);				\
	UNc_rb_ADD_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = ((x) >> G_SHIFT);					\
	r3__ = ((y) >> G_SHIFT);					\
	UNc_rb_MUL_UNc (r2__, (a), t__);				\
	UNc_rb_MUL_UNc (r3__, (b), t__);				\
	UNc_rb_ADD_UNc_rb (r2__, r3__, t__);				\
									\
	(x) = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
 * x_c = (x_c * a_c) / 255
 */
#define UNcx4_MUL_UNcx4(x, a)						\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x);							\
	r2__ = (a);							\
	UNc_rb_MUL_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = (x) >> G_SHIFT;						\
	r3__ = (a) >> G_SHIFT;						\
	UNc_rb_MUL_UNc_rb (r2__, r3__, t__);				\
									\
	(x) = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
 * x_c = (x_c * a_c) / 255 + y_c
 */
#define UNcx4_MUL_UNcx4_ADD_UNcx4(x, a, y)				\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x);							\
	r2__ = (a);							\
	UNc_rb_MUL_UNc_rb (r1__, r2__, t__);				\
	r2__ = (y) & RB_MASK;						\
	UNc_rb_ADD_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = ((x) >> G_SHIFT);					\
	r3__ = ((a) >> G_SHIFT);					\
	UNc_rb_MUL_UNc_rb (r2__, r3__, t__);				\
	r3__ = ((y) >> G_SHIFT) & RB_MASK;				\
	UNc_rb_ADD_UNc_rb (r2__, r3__, t__);				\
									\
	(x) = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
 * x_c = (x_c * a_c + y_c * b) / 255
 */
#define UNcx4_MUL_UNcx4_ADD_UNcx4_MUL_UNc(x, a, y, b)			\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x);							\
	r2__ = (a);							\
	UNc_rb_MUL_UNc_rb (r1__, r2__, t__);				\
	r2__ = (y);							\
	UNc_rb_MUL_UNc (r2__, (b), t__);				\
	UNc_rb_ADD_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = (x) >> G_SHIFT;						\
	r3__ = (a) >> G_SHIFT;						\
	UNc_rb_MUL_UNc_rb (r2__, r3__, t__);				\
	r3__ = (y) >> G_SHIFT;						\
	UNc_rb_MUL_UNc (r3__, (b), t__);				\
	UNc_rb_ADD_UNc_rb (r2__, r3__, t__);				\
									\
	x = r1__ | (r2__ << G_SHIFT);					\
    } while (0)

/*
  x_c = min(x_c + y_c, 255)
*/
#define UNcx4_ADD_UNcx4(x, y)						\
    do									\
    {									\
	comp4_t r1__, r2__, r3__, t__;					\
									\
	r1__ = (x) & RB_MASK;						\
	r2__ = (y) & RB_MASK;						\
	UNc_rb_ADD_UNc_rb (r1__, r2__, t__);				\
									\
	r2__ = ((x) >> G_SHIFT) & RB_MASK;				\
	r3__ = ((y) >> G_SHIFT) & RB_MASK;				\
	UNc_rb_ADD_UNc_rb (r2__, r3__, t__);				\
									\
	x = r1__ | (r2__ << G_SHIFT);					\
    } while (0)
