summaryrefslogtreecommitdiff
path: root/unpremultiply-div.c
blob: 543149ceba9890818bcfb8cdc52b42dc3cc5834d (plain)
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
/* Reference implementation using divisions.  Since the slow path is
 * so very slow this version specialises runs of constant or solid
 * pixels. */
#include <stddef.h>
#include <stdint.h>

/* Pixel format config for a 32 bit pixel with 8 bit components.  Only
 * the location of alpha matters. */
#ifndef ASHIFT
# define ASHIFT 24
#endif
#define RSHIFT ((24 + ASHIFT) % 32)
#define GSHIFT ((16 + ASHIFT) % 32)
#define BSHIFT (( 8 + ASHIFT) % 32)

#define AMASK (255 << ASHIFT)
#define RMASK (255 << RSHIFT)
#define GMASK (255 << GSHIFT)
#define BMASK (255 << BSHIFT)

void
unpremultiply_with_div(
    uint32_t * restrict       dst,
    uint32_t const * restrict src,
    size_t                    n)
{
    size_t i;
    
    for (i=0; i<n; i++) {
	uint32_t rgba = src[i];
	if (rgba & AMASK) {
	    uint32_t a = (rgba >> ASHIFT) & 0xFF;
	    uint32_t r = (rgba >> RSHIFT) & 0xFF;
	    uint32_t g = (rgba >> GSHIFT) & 0xFF;
	    uint32_t b = (rgba >> BSHIFT) & 0xFF;
	    r = r*255 / a;
	    g = g*255 / a;
	    b = b*255 / a;
	    dst[i] = (r<<RSHIFT) | (g<<GSHIFT) | (b<<BSHIFT) | (a << ASHIFT);
	} else {
	    dst[i] = 0;
	}
    }
}