2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set sw=4 sts=4 et cin: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org widget code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Christian Biesinger <cbiesinger@web.de>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
#include "imgIContainer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
#include "nsImageToPixbuf.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsImageToPixbuf, nsIImageToPixbuf)
|
|
|
|
|
|
|
|
inline unsigned char
|
|
|
|
unpremultiply (unsigned char color,
|
|
|
|
unsigned char alpha)
|
|
|
|
{
|
|
|
|
if (alpha == 0)
|
|
|
|
return 0;
|
|
|
|
// plus alpha/2 to round instead of truncate
|
|
|
|
return (color * 255 + alpha / 2) / alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(GdkPixbuf*)
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return ImageToPixbuf(aImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
GdkPixbuf*
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
nsRefPtr<gfxImageSurface> frame;
|
2010-07-31 10:44:23 -07:00
|
|
|
nsresult rv = aImage->CopyFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE,
|
|
|
|
getter_AddRefs(frame));
|
|
|
|
|
|
|
|
// If the last call failed, it was probably because our call stack originates
|
|
|
|
// in an imgIDecoderObserver event, meaning that we're not allowed request
|
|
|
|
// a sync decode. Presumably the originating event is something sensible like
|
|
|
|
// OnStopFrame(), so we can just retry the call without a sync decode.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
aImage->CopyFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_NONE,
|
|
|
|
getter_AddRefs(frame));
|
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
return ImgSurfaceToPixbuf(frame, frame->Width(), frame->Height());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkPixbuf*
|
2008-03-05 22:51:13 -08:00
|
|
|
nsImageToPixbuf::ImgSurfaceToPixbuf(gfxImageSurface* aImgSurface, PRInt32 aWidth, PRInt32 aHeight)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-03 00:56:21 -07:00
|
|
|
GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
|
2007-03-22 10:30:00 -07:00
|
|
|
aWidth, aHeight);
|
|
|
|
if (!pixbuf)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
PRUint32 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
guchar* pixels = gdk_pixbuf_get_pixels (pixbuf);
|
|
|
|
|
2008-03-05 22:51:13 -08:00
|
|
|
long cairoStride = aImgSurface->Stride();
|
|
|
|
unsigned char* cairoData = aImgSurface->Data();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-05 22:51:13 -08:00
|
|
|
gfxASurface::gfxImageFormat format = aImgSurface->Format();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (PRInt32 row = 0; row < aHeight; ++row) {
|
|
|
|
for (PRInt32 col = 0; col < aWidth; ++col) {
|
|
|
|
guchar* pixel = pixels + row * rowstride + 4 * col;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
PRUint32* cairoPixel = reinterpret_cast<PRUint32*>
|
|
|
|
((cairoData + row * cairoStride + 4 * col));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (format == gfxASurface::ImageFormatARGB32) {
|
|
|
|
const PRUint8 a = (*cairoPixel >> 24) & 0xFF;
|
|
|
|
const PRUint8 r = unpremultiply((*cairoPixel >> 16) & 0xFF, a);
|
|
|
|
const PRUint8 g = unpremultiply((*cairoPixel >> 8) & 0xFF, a);
|
|
|
|
const PRUint8 b = unpremultiply((*cairoPixel >> 0) & 0xFF, a);
|
|
|
|
|
|
|
|
*pixel++ = r;
|
|
|
|
*pixel++ = g;
|
|
|
|
*pixel++ = b;
|
|
|
|
*pixel++ = a;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(format == gfxASurface::ImageFormatRGB24,
|
|
|
|
"unexpected format");
|
|
|
|
const PRUint8 r = (*cairoPixel >> 16) & 0xFF;
|
|
|
|
const PRUint8 g = (*cairoPixel >> 8) & 0xFF;
|
|
|
|
const PRUint8 b = (*cairoPixel >> 0) & 0xFF;
|
|
|
|
|
|
|
|
*pixel++ = r;
|
|
|
|
*pixel++ = g;
|
|
|
|
*pixel++ = b;
|
|
|
|
*pixel++ = 0xFF; // A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
}
|
2008-03-05 22:51:13 -08:00
|
|
|
|
|
|
|
GdkPixbuf*
|
|
|
|
nsImageToPixbuf::SurfaceToPixbuf(gfxASurface* aSurface, PRInt32 aWidth, PRInt32 aHeight)
|
|
|
|
{
|
|
|
|
if (aSurface->CairoStatus()) {
|
|
|
|
NS_ERROR("invalid surface");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> imgSurface;
|
|
|
|
if (aSurface->GetType() == gfxASurface::SurfaceTypeImage) {
|
|
|
|
imgSurface = static_cast<gfxImageSurface*>
|
|
|
|
(static_cast<gfxASurface*>(aSurface));
|
|
|
|
} else {
|
|
|
|
imgSurface = new gfxImageSurface(gfxIntSize(aWidth, aHeight),
|
|
|
|
gfxImageSurface::ImageFormatARGB32);
|
|
|
|
|
|
|
|
if (!imgSurface)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(imgSurface);
|
|
|
|
if (!context)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
context->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
context->SetSource(aSurface);
|
|
|
|
context->Paint();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ImgSurfaceToPixbuf(imgSurface, aWidth, aHeight);
|
|
|
|
}
|