2006-06-16 22:38:39 +04:00
|
|
|
/* vim:set sw=4 sts=4 et cin: */
|
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/. */
|
2006-06-16 22:38:39 +04:00
|
|
|
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
|
2014-04-16 04:41:40 +04:00
|
|
|
#include "nsImageToPixbuf.h"
|
2006-06-16 22:38:39 +04: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-21 05:50:15 +04:00
|
|
|
#include "imgIContainer.h"
|
2014-04-16 04:41:40 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
using mozilla::gfx::DataSourceSurface;
|
|
|
|
using mozilla::gfx::SurfaceFormat;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsImageToPixbuf, nsIImageToPixbuf)
|
2006-06-16 22:38:39 +04:00
|
|
|
|
|
|
|
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-21 05:50:15 +04:00
|
|
|
nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage)
|
2006-06-16 22:38:39 +04: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-21 05:50:15 +04:00
|
|
|
nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage)
|
2006-06-16 22:38:39 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface =
|
2013-12-13 12:34:24 +04:00
|
|
|
aImage->GetFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE);
|
2010-07-31 21:44:23 +04:00
|
|
|
|
|
|
|
// If the last call failed, it was probably because our call stack originates
|
2012-12-18 20:37:15 +04:00
|
|
|
// in an imgINotificationObserver event, meaning that we're not allowed request
|
2010-07-31 21:44:23 +04:00
|
|
|
// a sync decode. Presumably the originating event is something sensible like
|
|
|
|
// OnStopFrame(), so we can just retry the call without a sync decode.
|
2014-04-15 22:02:23 +04:00
|
|
|
if (!surface)
|
|
|
|
surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_NONE);
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
NS_ENSURE_TRUE(surface, nullptr);
|
2014-01-23 16:34:56 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
return SourceSurfaceToPixbuf(surface,
|
|
|
|
surface->GetSize().width,
|
|
|
|
surface->GetSize().height);
|
2007-03-20 15:14:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkPixbuf*
|
2014-01-23 15:33:23 +04:00
|
|
|
nsImageToPixbuf::SourceSurfaceToPixbuf(SourceSurface* aSurface,
|
|
|
|
int32_t aWidth,
|
|
|
|
int32_t aHeight)
|
2007-03-20 15:14:52 +03:00
|
|
|
{
|
2014-01-23 15:33:23 +04:00
|
|
|
MOZ_ASSERT(aWidth <= aSurface->GetSize().width &&
|
|
|
|
aHeight <= aSurface->GetSize().height,
|
|
|
|
"Requested rect is bigger than the supplied surface");
|
|
|
|
|
2011-10-03 11:56:21 +04:00
|
|
|
GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
|
2007-03-20 15:14:52 +03:00
|
|
|
aWidth, aHeight);
|
2006-06-16 22:38:39 +04:00
|
|
|
if (!pixbuf)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
uint32_t destStride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
guchar* destPixels = gdk_pixbuf_get_pixels (pixbuf);
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
|
2014-01-23 15:33:23 +04:00
|
|
|
DataSourceSurface::MappedSurface map;
|
2015-06-11 20:06:23 +03:00
|
|
|
if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map))
|
|
|
|
return nullptr;
|
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
uint8_t* srcData = map.mData;
|
|
|
|
int32_t srcStride = map.mStride;
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
SurfaceFormat format = dataSurface->GetFormat();
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t row = 0; row < aHeight; ++row) {
|
|
|
|
for (int32_t col = 0; col < aWidth; ++col) {
|
2014-01-23 15:33:23 +04:00
|
|
|
guchar* destPixel = destPixels + row * destStride + 4 * col;
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
uint32_t* srcPixel =
|
|
|
|
reinterpret_cast<uint32_t*>((srcData + row * srcStride + 4 * col));
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
if (format == SurfaceFormat::B8G8R8A8) {
|
|
|
|
const uint8_t a = (*srcPixel >> 24) & 0xFF;
|
|
|
|
const uint8_t r = unpremultiply((*srcPixel >> 16) & 0xFF, a);
|
|
|
|
const uint8_t g = unpremultiply((*srcPixel >> 8) & 0xFF, a);
|
|
|
|
const uint8_t b = unpremultiply((*srcPixel >> 0) & 0xFF, a);
|
2006-06-16 22:38:39 +04:00
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
*destPixel++ = r;
|
|
|
|
*destPixel++ = g;
|
|
|
|
*destPixel++ = b;
|
|
|
|
*destPixel++ = a;
|
2006-06-16 22:38:39 +04:00
|
|
|
} else {
|
2014-01-23 15:33:23 +04:00
|
|
|
MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8);
|
|
|
|
|
|
|
|
const uint8_t r = (*srcPixel >> 16) & 0xFF;
|
|
|
|
const uint8_t g = (*srcPixel >> 8) & 0xFF;
|
|
|
|
const uint8_t b = (*srcPixel >> 0) & 0xFF;
|
|
|
|
|
|
|
|
*destPixel++ = r;
|
|
|
|
*destPixel++ = g;
|
|
|
|
*destPixel++ = b;
|
|
|
|
*destPixel++ = 0xFF; // A
|
2006-06-16 22:38:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 15:33:23 +04:00
|
|
|
dataSurface->Unmap();
|
|
|
|
|
2006-06-16 22:38:39 +04:00
|
|
|
return pixbuf;
|
|
|
|
}
|
2008-03-06 09:51:13 +03:00
|
|
|
|