File tree Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -912,15 +912,17 @@ void GraphicsContextRenderer::draw_image(
912
912
for (auto i = 0 ; i < height; ++i) {
913
913
auto ptr = reinterpret_cast <uint32_t *>(data + i * stride);
914
914
for (auto j = 0 ; j < width; ++j) {
915
- auto const & r = im_raw (i, j, 0 ),
916
- & g = im_raw (i, j, 1 ),
917
- & b = im_raw (i, j, 2 ),
918
- & a = im_raw (i, j, 3 );
919
- *ptr++ =
920
- (uint8_t (a) << 24 )
921
- | (uint8_t (a / 255 . * r) << 16 )
922
- | (uint8_t (a / 255 . * g) << 8 )
923
- | (uint8_t (a / 255 . * b) << 0 );
915
+ auto r = im_raw (i, j, 0 ),
916
+ g = im_raw (i, j, 1 ),
917
+ b = im_raw (i, j, 2 ),
918
+ a = im_raw (i, j, 3 );
919
+ if (a != 0xff ) {
920
+ auto subtable = &detail::premultiplication_table[a << 8 ];
921
+ r = subtable[r];
922
+ g = subtable[g];
923
+ b = subtable[b];
924
+ }
925
+ *ptr++ = (a << 24 ) + (r << 16 ) + (g << 8 ) + (b << 0 );
924
926
}
925
927
}
926
928
cairo_surface_mark_dirty (surface);
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ std::unordered_map<FT_Error, std::string> const ft_errors
28
28
;
29
29
FT_Library ft_library{};
30
30
bool has_pycairo{};
31
+ std::array<uint8_t , 0x10000 > premultiplication_table{[]() {
32
+ auto table = decltype (premultiplication_table){};
33
+ for (auto a = 1 ; a < 0x100 ; ++a) {
34
+ for (auto c = 0 ; c < 0x100 ; ++c) {
35
+ table[(a << 8 ) + c] = a / 255 . * c;
36
+ }
37
+ }
38
+ return table;
39
+ } ()};
31
40
std::array<uint8_t , 0x10000 > unpremultiplication_table{[]() {
32
41
auto table = decltype (unpremultiplication_table){};
33
42
// As in cairo-png.c (unpremultiply 0 => 0).
Original file line number Diff line number Diff line change @@ -21,7 +21,8 @@ namespace detail {
21
21
extern std::unordered_map<FT_Error, std::string> const ft_errors;
22
22
extern FT_Library ft_library;
23
23
extern bool has_pycairo;
24
- extern std::array<uint8_t , 0x10000 > unpremultiplication_table;
24
+ extern std::array<uint8_t , 0x10000 >
25
+ premultiplication_table, unpremultiplication_table;
25
26
26
27
// Optional parts of cairo.
27
28
You can’t perform that action at this time.
0 commit comments