1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Thebes gfx.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2007
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Vladimir Vukicevic <vladimir@pobox.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef _GFXALPHARECOVERY_H_
39 : #define _GFXALPHARECOVERY_H_
40 :
41 : #include "gfxContext.h"
42 : #include "gfxImageSurface.h"
43 : #include "mozilla/SSE.h"
44 : #include "nsRect.h"
45 :
46 : class THEBES_API gfxAlphaRecovery {
47 : public:
48 : struct Analysis {
49 : bool uniformColor;
50 : bool uniformAlpha;
51 : gfxFloat alpha;
52 : gfxFloat r, g, b;
53 : };
54 :
55 : /**
56 : * Some SIMD fast-paths only can be taken if the relative
57 : * byte-alignment of images' pointers and strides meets certain
58 : * criteria. Aligning image pointers and strides by
59 : * |GoodAlignmentLog2()| below will ensure that fast-paths aren't
60 : * skipped because of misalignment. Fast-paths may still be taken
61 : * even if GoodAlignmentLog2() is not met, in some conditions.
62 : */
63 65 : static PRUint32 GoodAlignmentLog2() { return 4; /* for SSE2 */ }
64 :
65 : /* Given two surfaces of equal size with the same rendering, one onto a
66 : * black background and the other onto white, recovers alpha values from
67 : * the difference and sets the alpha values on the black surface.
68 : * The surfaces must have format RGB24 or ARGB32.
69 : * Returns true on success.
70 : */
71 : static bool RecoverAlpha (gfxImageSurface *blackSurface,
72 : const gfxImageSurface *whiteSurface,
73 : Analysis *analysis = nsnull);
74 :
75 : #ifdef MOZILLA_MAY_SUPPORT_SSE2
76 : /* This does the same as the previous function, but uses SSE2
77 : * optimizations. Usually this should not be called directly. Be sure to
78 : * check mozilla::supports_sse2() before calling this function.
79 : */
80 : static bool RecoverAlphaSSE2 (gfxImageSurface *blackSurface,
81 : const gfxImageSurface *whiteSurface);
82 :
83 : /**
84 : * A common use-case for alpha recovery is to paint into a
85 : * temporary "white image", then paint onto a subrect of the
86 : * surface, the "black image", into which alpha-recovered pixels
87 : * are eventually to be written. This function returns a rect
88 : * aligned so that recovering alpha for that rect will hit SIMD
89 : * fast-paths, if possible. It's not always possible to align
90 : * |aRect| so that fast-paths will be taken.
91 : *
92 : * The returned rect is always a superset of |aRect|.
93 : */
94 : static nsIntRect AlignRectForSubimageRecovery(const nsIntRect& aRect,
95 : gfxImageSurface* aSurface);
96 : #else
97 : static nsIntRect AlignRectForSubimageRecovery(const nsIntRect& aRect,
98 : gfxImageSurface*)
99 : { return aRect; }
100 : #endif
101 :
102 : /** from cairo-xlib-utils.c, modified */
103 : /**
104 : * Given the RGB data for two image surfaces, one a source image composited
105 : * with OVER onto a black background, and one a source image composited with
106 : * OVER onto a white background, reconstruct the original image data into
107 : * black_data.
108 : *
109 : * Consider a single color channel and a given pixel. Suppose the original
110 : * premultiplied color value was C and the alpha value was A. Let the final
111 : * on-black color be B and the final on-white color be W. All values range
112 : * over 0-255.
113 : *
114 : * Then B=C and W=(255*(255 - A) + C*255)/255. Solving for A, we get
115 : * A=255 - (W - C). Therefore it suffices to leave the black_data color
116 : * data alone and set the alpha values using that simple formula. It shouldn't
117 : * matter what color channel we pick for the alpha computation, but we'll
118 : * pick green because if we went through a color channel downsample the green
119 : * bits are likely to be the most accurate.
120 : *
121 : * This function needs to be in the header file since it's used by both
122 : * gfxRecoverAlpha.cpp and gfxRecoverAlphaSSE2.cpp.
123 : */
124 :
125 : static inline PRUint32
126 0 : RecoverPixel(PRUint32 black, PRUint32 white)
127 : {
128 0 : const PRUint32 GREEN_MASK = 0x0000FF00;
129 0 : const PRUint32 ALPHA_MASK = 0xFF000000;
130 :
131 : /* |diff| here is larger when the source image pixel is more transparent.
132 : If both renderings are from the same source image composited with OVER,
133 : then the color values on white will always be greater than those on
134 : black, so |diff| would not overflow. However, overflow may happen, for
135 : example, when a plugin plays a video and the image is rapidly changing.
136 : If there is overflow, then behave as if we limit to the difference to
137 : >= 0, which will make the rendering opaque. (Without this overflow
138 : will make the rendering transparent.) */
139 0 : PRUint32 diff = (white & GREEN_MASK) - (black & GREEN_MASK);
140 : /* |diff| is 0xFFFFxx00 on overflow and 0x0000xx00 otherwise, so use this
141 : to limit the transparency. */
142 0 : PRUint32 limit = diff & ALPHA_MASK;
143 : /* The alpha bits of the result */
144 0 : PRUint32 alpha = (ALPHA_MASK - (diff << 16)) | limit;
145 :
146 0 : return alpha | (black & ~ALPHA_MASK);
147 : }
148 : };
149 :
150 : #endif /* _GFXALPHARECOVERY_H_ */
|