2007-03-03 03:18:34 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
#include "gfxAlphaRecovery.h"
|
|
|
|
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
2010-08-18 07:43:49 +04:00
|
|
|
#define MOZILLA_SSE_INCLUDE_HEADER_FOR_SSE2
|
|
|
|
#include "mozilla/SSE.h"
|
2007-03-03 03:18:34 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* static */ bool
|
2010-08-09 06:19:17 +04:00
|
|
|
gfxAlphaRecovery::RecoverAlpha(gfxImageSurface* blackSurf,
|
|
|
|
const gfxImageSurface* whiteSurf,
|
|
|
|
Analysis* analysis)
|
2007-03-03 03:18:34 +03:00
|
|
|
{
|
2010-08-09 06:19:17 +04:00
|
|
|
gfxIntSize size = blackSurf->GetSize();
|
|
|
|
|
|
|
|
if (size != whiteSurf->GetSize() ||
|
|
|
|
(blackSurf->Format() != gfxASurface::ImageFormatARGB32 &&
|
|
|
|
blackSurf->Format() != gfxASurface::ImageFormatRGB24) ||
|
|
|
|
(whiteSurf->Format() != gfxASurface::ImageFormatARGB32 &&
|
|
|
|
whiteSurf->Format() != gfxASurface::ImageFormatRGB24))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-08-09 06:19:17 +04:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_SSE2
|
|
|
|
if (!analysis && mozilla::supports_sse2() &&
|
|
|
|
RecoverAlphaSSE2(blackSurf, whiteSurf)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-08-18 07:43:49 +04:00
|
|
|
}
|
2010-12-05 11:38:53 +03:00
|
|
|
#endif
|
2010-08-18 07:43:49 +04:00
|
|
|
|
2010-08-09 06:22:56 +04:00
|
|
|
blackSurf->Flush();
|
|
|
|
whiteSurf->Flush();
|
|
|
|
|
2010-08-09 06:19:17 +04:00
|
|
|
unsigned char* blackData = blackSurf->Data();
|
|
|
|
unsigned char* whiteData = whiteSurf->Data();
|
|
|
|
|
2010-08-09 06:22:56 +04:00
|
|
|
/* Get the alpha value of 'first' */
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t first;
|
2010-08-09 06:22:56 +04:00
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
if (!blackData || !whiteData)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-08-09 06:19:17 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
first = RecoverPixel(*reinterpret_cast<uint32_t*>(blackData),
|
|
|
|
*reinterpret_cast<uint32_t*>(whiteData));
|
2010-08-09 06:22:56 +04:00
|
|
|
}
|
2010-08-09 06:19:17 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t deltas = 0;
|
|
|
|
for (int32_t i = 0; i < size.height; ++i) {
|
|
|
|
uint32_t* blackPixel = reinterpret_cast<uint32_t*>(blackData);
|
|
|
|
const uint32_t* whitePixel = reinterpret_cast<uint32_t*>(whiteData);
|
|
|
|
for (int32_t j = 0; j < size.width; ++j) {
|
|
|
|
uint32_t recovered = RecoverPixel(blackPixel[j], whitePixel[j]);
|
2010-08-09 06:22:56 +04:00
|
|
|
blackPixel[j] = recovered;
|
|
|
|
deltas |= (first ^ recovered);
|
2010-08-09 06:19:17 +04:00
|
|
|
}
|
|
|
|
blackData += blackSurf->Stride();
|
|
|
|
whiteData += whiteSurf->Stride();
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
2010-08-09 06:19:17 +04:00
|
|
|
|
|
|
|
blackSurf->MarkDirty();
|
2007-03-03 03:18:34 +03:00
|
|
|
|
2010-08-09 06:19:17 +04:00
|
|
|
if (analysis) {
|
|
|
|
analysis->uniformAlpha = (deltas >> 24) == 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
analysis->uniformColor = false;
|
2010-08-09 06:19:17 +04:00
|
|
|
if (analysis->uniformAlpha) {
|
2010-08-09 06:22:56 +04:00
|
|
|
double d_first_alpha = first >> 24;
|
|
|
|
analysis->alpha = d_first_alpha/255.0;
|
2007-03-03 03:18:34 +03:00
|
|
|
/* we only set uniformColor when the alpha is already uniform.
|
|
|
|
it's only useful in that case ... and if the alpha was nonuniform
|
|
|
|
then computing whether the color is uniform would require unpremultiplying
|
|
|
|
every pixel */
|
2010-08-09 06:22:56 +04:00
|
|
|
analysis->uniformColor = deltas == 0;
|
2010-08-09 06:19:17 +04:00
|
|
|
if (analysis->uniformColor) {
|
2010-08-09 06:22:56 +04:00
|
|
|
if (d_first_alpha == 0.0) {
|
2007-03-03 03:18:34 +03:00
|
|
|
/* can't unpremultiply, this is OK */
|
2010-08-09 06:19:17 +04:00
|
|
|
analysis->r = analysis->g = analysis->b = 0.0;
|
2007-03-03 03:18:34 +03:00
|
|
|
} else {
|
2010-08-09 06:19:17 +04:00
|
|
|
analysis->r = (first & 0xFF)/d_first_alpha;
|
|
|
|
analysis->g = ((first >> 8) & 0xFF)/d_first_alpha;
|
|
|
|
analysis->b = ((first >> 16) & 0xFF)/d_first_alpha;
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-09 06:19:17 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|