summaryrefslogtreecommitdiffstats
path: root/src/base/ftbitmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/base/ftbitmap.c')
-rw-r--r--src/base/ftbitmap.c45
1 files changed, 17 insertions, 28 deletions
diff --git a/src/base/ftbitmap.c b/src/base/ftbitmap.c
index 182b1cc..6542c79 100644
--- a/src/base/ftbitmap.c
+++ b/src/base/ftbitmap.c
@@ -4,7 +4,7 @@
/* */
/* FreeType utility functions for bitmaps (body). */
/* */
-/* Copyright 2004-2009, 2011, 2013 by */
+/* Copyright 2004-2009, 2011, 2013, 2014 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
@@ -375,14 +375,11 @@
}
- FT_Byte
+ static FT_Byte
ft_gray_for_premultiplied_srgb_bgra( const FT_Byte* bgra )
{
- FT_Long a = bgra[3];
- FT_Long b = bgra[0];
- FT_Long g = bgra[1];
- FT_Long r = bgra[2];
- FT_Long l;
+ FT_Byte a = bgra[3];
+ FT_ULong l;
/* Short-circuit transparent color to avoid div-by-zero. */
@@ -397,38 +394,30 @@
*
* http://accessibility.kde.org/hsl-adjusted.php
*
- * We do the computation with integers only.
+ * We do the computation with integers only, applying a gamma of 2.0.
+ * The following will never overflow 32 bits; it is a scaled-up
+ * luminosity with premultiplication not yet undone.
+ *
*/
- /* Undo premultification, get the number in a 16.16 form. */
- b = FT_MulDiv( b, 65536, a );
- g = FT_MulDiv( g, 65536, a );
- r = FT_MulDiv( r, 65536, a );
- a = a * 256;
-
- /* Apply gamma of 2.0 instead of 2.2. */
- b = FT_MulFix( b, b );
- g = FT_MulFix( g, g );
- r = FT_MulFix( r, r );
-
- /* Apply coefficients. */
- b = FT_MulFix( b, 4731 /* 0.0722 * 65536 */ );
- g = FT_MulFix( g, 46871 /* 0.7152 * 65536 */ );
- r = FT_MulFix( r, 13933 /* 0.2126 * 65536 */ );
-
- l = r + g + b;
+ l = 4731UL /* 0.0722 * 65536 */ * bgra[0] * bgra[0] +
+ 46871UL /* 0.7152 * 65536 */ * bgra[1] * bgra[1] +
+ 13933UL /* 0.2126 * 65536 */ * bgra[2] * bgra[2];
/*
- * Final transparency can be determined this way:
+ * Final transparency can be determined as follows.
*
* - If alpha is zero, we want 0.
* - If alpha is zero and luminosity is zero, we want 255.
* - If alpha is zero and luminosity is one, we want 0.
*
- * So the formula is a * (1 - l).
+ * So the formula is a * (1 - l) = a - l * a.
+ *
+ * In the actual code, we undo premultiplication and scale down again.
+ *
*/
- return (FT_Byte)( FT_MulFix( 65535 - l, a ) >> 8 );
+ return a - (FT_Byte)( ( l / a ) >> 16 );
}