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 : * Bas Schouten <bschouten@mozilla.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "gfxAlphaRecovery.h"
40 :
41 : #include "gfxImageSurface.h"
42 :
43 : #define MOZILLA_SSE_INCLUDE_HEADER_FOR_SSE2
44 : #include "mozilla/SSE.h"
45 :
46 : /* static */ bool
47 0 : gfxAlphaRecovery::RecoverAlpha(gfxImageSurface* blackSurf,
48 : const gfxImageSurface* whiteSurf,
49 : Analysis* analysis)
50 : {
51 0 : gfxIntSize size = blackSurf->GetSize();
52 :
53 0 : if (size != whiteSurf->GetSize() ||
54 0 : (blackSurf->Format() != gfxASurface::ImageFormatARGB32 &&
55 0 : blackSurf->Format() != gfxASurface::ImageFormatRGB24) ||
56 0 : (whiteSurf->Format() != gfxASurface::ImageFormatARGB32 &&
57 0 : whiteSurf->Format() != gfxASurface::ImageFormatRGB24))
58 0 : return false;
59 :
60 : #ifdef MOZILLA_MAY_SUPPORT_SSE2
61 0 : if (!analysis && mozilla::supports_sse2() &&
62 0 : RecoverAlphaSSE2(blackSurf, whiteSurf)) {
63 0 : return true;
64 : }
65 : #endif
66 :
67 0 : blackSurf->Flush();
68 0 : whiteSurf->Flush();
69 :
70 0 : unsigned char* blackData = blackSurf->Data();
71 0 : unsigned char* whiteData = whiteSurf->Data();
72 :
73 : /* Get the alpha value of 'first' */
74 : PRUint32 first;
75 0 : if (size.width == 0 || size.height == 0) {
76 0 : first = 0;
77 : } else {
78 0 : if (!blackData || !whiteData)
79 0 : return false;
80 :
81 : first = RecoverPixel(*reinterpret_cast<PRUint32*>(blackData),
82 0 : *reinterpret_cast<PRUint32*>(whiteData));
83 : }
84 :
85 0 : PRUint32 deltas = 0;
86 0 : for (PRInt32 i = 0; i < size.height; ++i) {
87 0 : PRUint32* blackPixel = reinterpret_cast<PRUint32*>(blackData);
88 0 : const PRUint32* whitePixel = reinterpret_cast<PRUint32*>(whiteData);
89 0 : for (PRInt32 j = 0; j < size.width; ++j) {
90 0 : PRUint32 recovered = RecoverPixel(blackPixel[j], whitePixel[j]);
91 0 : blackPixel[j] = recovered;
92 0 : deltas |= (first ^ recovered);
93 : }
94 0 : blackData += blackSurf->Stride();
95 0 : whiteData += whiteSurf->Stride();
96 : }
97 :
98 0 : blackSurf->MarkDirty();
99 :
100 0 : if (analysis) {
101 0 : analysis->uniformAlpha = (deltas >> 24) == 0;
102 0 : analysis->uniformColor = false;
103 0 : if (analysis->uniformAlpha) {
104 0 : double d_first_alpha = first >> 24;
105 0 : analysis->alpha = d_first_alpha/255.0;
106 : /* we only set uniformColor when the alpha is already uniform.
107 : it's only useful in that case ... and if the alpha was nonuniform
108 : then computing whether the color is uniform would require unpremultiplying
109 : every pixel */
110 0 : analysis->uniformColor = deltas == 0;
111 0 : if (analysis->uniformColor) {
112 0 : if (d_first_alpha == 0.0) {
113 : /* can't unpremultiply, this is OK */
114 0 : analysis->r = analysis->g = analysis->b = 0.0;
115 : } else {
116 0 : analysis->r = (first & 0xFF)/d_first_alpha;
117 0 : analysis->g = ((first >> 8) & 0xFF)/d_first_alpha;
118 0 : analysis->b = ((first >> 16) & 0xFF)/d_first_alpha;
119 : }
120 : }
121 : }
122 : }
123 :
124 0 : return true;
125 : }
|