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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
* Copyright (c) 2009 M Joonas Pihlaja
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/* Basic unpremultiplier using reciprocal multiplication from a 1KB
* table of reciprocals. */
#include <stdint.h>
#include <stddef.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 (255U << ASHIFT)
#define RMASK (255U << RSHIFT)
#define GMASK (255U << GSHIFT)
#define BMASK (255U << BSHIFT)
/* Set to 1 if the input can have superluminant pixels. */
#define DO_CLAMP_INPUT 0
/* Shift x left by y bits. Supports negative y for right shifts. */
#define SHIFT(x, y) ((y) < 0 ? (x) >> (-(y)) : (x) << (y))
#define ceil_div(a,b) ((a) + (b)-1) / (b)
/* The reciprocal_table[i] entries are defined by
*
* 0 when i = 0
* 255 / i when i > 0
*
* represented in fixed point format with RECIPROCAL_BITS of
* precision and errors rounded up. */
#define RECIPROCAL_BITS 16
static uint32_t const reciprocal_table[256] = {
# define R(i) ((i) ? ceil_div(255*(1<<RECIPROCAL_BITS), (i)) : 0)
# define R1(i) R(i), R(i+1), R(i+2), R(i+3)
# define R2(i) R1(i), R1(i+4), R1(i+8), R1(i+12)
# define R3(i) R2(i), R2(i+16), R2(i+32), R2(i+48)
R3(0), R3(64), R3(128), R3(192)
};
void
unpremultiply_with_inv(
uint32_t * restrict dst,
uint32_t const * restrict src,
size_t num_pixels)
{
size_t i;
for (i = 0; i < num_pixels; i++) {
uint32_t rgba = src[i];
uint32_t a = (rgba >> ASHIFT) & 255;
uint32_t r = (rgba >> RSHIFT) & 255;
uint32_t g = (rgba >> GSHIFT) & 255;
uint32_t b = (rgba >> BSHIFT) & 255;
uint32_t recip = reciprocal_table[a];
#if DO_CLAMP_INPUT
r = r < a ? r : a;
g = g < a ? g : a;
b = b < a ? b : a;
#endif
r = SHIFT(r * recip, RSHIFT - RECIPROCAL_BITS);
g = SHIFT(g * recip, GSHIFT - RECIPROCAL_BITS);
b = SHIFT(b * recip, BSHIFT - RECIPROCAL_BITS);
dst[i] =
(r & RMASK) | (g & GMASK) | (b & BMASK) | (rgba & AMASK);
}
}
|