2 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Thebes gfx.
18 * The Initial Developer of the Original Code is Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Vladimir Vukicevic <vladimir@pobox.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "gfxAlphaRecovery.h"
41 #include "gfxImageSurface.h"
44 struct gfxAlphaRecoveryResult
{
45 gfxAlphaRecoveryResult()
46 : uniformColor(PR_FALSE
),
47 uniformAlpha(PR_FALSE
)
55 static void _compute_alpha_values (unsigned int *black_data
,
56 unsigned int *white_data
,
57 gfxIntSize dimensions
,
58 gfxAlphaRecoveryResult
*result
);
60 already_AddRefed
<gfxImageSurface
>
61 gfxAlphaRecovery::RecoverAlpha (gfxImageSurface
*blackSurf
,
62 gfxImageSurface
*whiteSurf
,
63 gfxIntSize dimensions
)
66 nsRefPtr
<gfxImageSurface
> resultSurf
;
67 resultSurf
= new gfxImageSurface(dimensions
, gfxASurface::ImageFormatARGB32
);
69 // copy blackSurf into resultSurf
70 gfxContext
ctx(resultSurf
);
71 ctx
.SetSource(blackSurf
);
72 ctx
.SetOperator(gfxContext::OPERATOR_SOURCE
);
75 gfxAlphaRecoveryResult result
;
76 _compute_alpha_values ((unsigned int*) resultSurf
->Data(),
77 (unsigned int*) whiteSurf
->Data(),
81 // XX use result, maybe return pattern, etc.
83 NS_ADDREF(resultSurf
.get());
84 return resultSurf
.get();
87 /** from cairo-xlib-utils.c, modified */
89 * Given the RGB data for two image surfaces, one a source image composited
90 * with OVER onto a black background, and one a source image composited with
91 * OVER onto a white background, reconstruct the original image data into
94 * Consider a single color channel and a given pixel. Suppose the original
95 * premultiplied color value was C and the alpha value was A. Let the final
96 * on-black color be B and the final on-white color be W. All values range
98 * Then B=C and W=(255*(255 - A) + C*255)/255. Solving for A, we get
99 * A=255 - (W - C). Therefore it suffices to leave the black_data color
100 * data alone and set the alpha values using that simple formula. It shouldn't
101 * matter what color channel we pick for the alpha computation, but we'll
102 * pick green because if we went through a color channel downsample the green
103 * bits are likely to be the most accurate.
106 #define SET_ALPHA(v, a) (((v) & ~(0xFF << 24)) | ((a) << 24))
107 #define GREEN_OF(v) (((v) >> 8) & 0xFF)
110 _compute_alpha_values (unsigned int *black_data
,
111 unsigned int *white_data
,
112 gfxIntSize dimensions
,
113 gfxAlphaRecoveryResult
*result
)
115 int num_pixels
= dimensions
.width
* dimensions
.height
;
118 unsigned int deltas
= 0;
119 unsigned char first_alpha
;
121 if (num_pixels
== 0) {
123 result
->uniformAlpha
= PR_TRUE
;
124 result
->uniformColor
= PR_TRUE
;
125 /* whatever we put here will be true */
127 result
->r
= result
->g
= result
->b
= 0.0;
132 first_alpha
= 255 - (GREEN_OF(*white_data
) - GREEN_OF(*black_data
));
133 /* set the alpha value of 'first' */
134 first
= SET_ALPHA(*black_data
, first_alpha
);
136 for (i
= 0; i
< num_pixels
; ++i
) {
137 unsigned int black
= *black_data
;
138 unsigned int white
= *white_data
;
139 unsigned char pixel_alpha
= 255 - (GREEN_OF(white
) - GREEN_OF(black
));
141 black
= SET_ALPHA(black
, pixel_alpha
);
143 deltas
|= (first
^ black
);
150 result
->uniformAlpha
= (deltas
>> 24) == 0;
151 if (result
->uniformAlpha
) {
152 result
->alpha
= first_alpha
/255.0;
153 /* we only set uniformColor when the alpha is already uniform.
154 it's only useful in that case ... and if the alpha was nonuniform
155 then computing whether the color is uniform would require unpremultiplying
157 result
->uniformColor
= (deltas
& ~(0xFF << 24)) == 0;
158 if (result
->uniformColor
) {
159 if (first_alpha
== 0) {
160 /* can't unpremultiply, this is OK */
161 result
->r
= result
->g
= result
->b
= 0.0;
163 double d_first_alpha
= first_alpha
;
164 result
->r
= (first
& 0xFF)/d_first_alpha
;
165 result
->g
= ((first
>> 8) & 0xFF)/d_first_alpha
;
166 result
->b
= ((first
>> 16) & 0xFF)/d_first_alpha
;