gecko/widget/gtk/nsImageToPixbuf.cpp
Martin Stransky 03394f2857 Bug 917270 - Rename widget/gtk2 to widget/gtk. r=karlt
--HG--
rename : widget/gtk2/Makefile.in => widget/gtk/Makefile.in
rename : widget/gtk2/WidgetTraceEvent.cpp => widget/gtk/WidgetTraceEvent.cpp
rename : widget/gtk2/compat/gdk/gdkdnd.h => widget/gtk/compat/gdk/gdkdnd.h
rename : widget/gtk2/compat/gdk/gdkkeysyms.h => widget/gtk/compat/gdk/gdkkeysyms.h
rename : widget/gtk2/compat/gdk/gdkvisual.h => widget/gtk/compat/gdk/gdkvisual.h
rename : widget/gtk2/compat/gdk/gdkwindow.h => widget/gtk/compat/gdk/gdkwindow.h
rename : widget/gtk2/compat/gdk/gdkx.h => widget/gtk/compat/gdk/gdkx.h
rename : widget/gtk2/compat/glib/gmem.h => widget/gtk/compat/glib/gmem.h
rename : widget/gtk2/compat/gtk/gtkcolorseldialog.h => widget/gtk/compat/gtk/gtkcolorseldialog.h
rename : widget/gtk2/compat/gtk/gtkdialog.h => widget/gtk/compat/gtk/gtkdialog.h
rename : widget/gtk2/compat/gtk/gtkplug.h => widget/gtk/compat/gtk/gtkplug.h
rename : widget/gtk2/compat/gtk/gtkselection.h => widget/gtk/compat/gtk/gtkselection.h
rename : widget/gtk2/compat/gtk/gtkwidget.h => widget/gtk/compat/gtk/gtkwidget.h
rename : widget/gtk2/compat/gtk/gtkwindow.h => widget/gtk/compat/gtk/gtkwindow.h
rename : widget/gtk2/crashtests/673390-1.html => widget/gtk/crashtests/673390-1.html
rename : widget/gtk2/crashtests/crashtests.list => widget/gtk/crashtests/crashtests.list
rename : widget/gtk2/gtk2drawing.c => widget/gtk/gtk2drawing.c
rename : widget/gtk2/gtk3drawing.c => widget/gtk/gtk3drawing.c
rename : widget/gtk2/gtkdrawing.h => widget/gtk/gtkdrawing.h
rename : widget/gtk2/maiRedundantObjectFactory.c => widget/gtk/maiRedundantObjectFactory.c
rename : widget/gtk2/maiRedundantObjectFactory.h => widget/gtk/maiRedundantObjectFactory.h
rename : widget/gtk2/moz.build => widget/gtk/moz.build
rename : widget/gtk2/mozcontainer.c => widget/gtk/mozcontainer.c
rename : widget/gtk2/mozcontainer.h => widget/gtk/mozcontainer.h
rename : widget/gtk2/nsAppShell.cpp => widget/gtk/nsAppShell.cpp
rename : widget/gtk2/nsAppShell.h => widget/gtk/nsAppShell.h
rename : widget/gtk2/nsBidiKeyboard.cpp => widget/gtk/nsBidiKeyboard.cpp
rename : widget/gtk2/nsBidiKeyboard.h => widget/gtk/nsBidiKeyboard.h
rename : widget/gtk2/nsCUPSShim.cpp => widget/gtk/nsCUPSShim.cpp
rename : widget/gtk2/nsCUPSShim.h => widget/gtk/nsCUPSShim.h
rename : widget/gtk2/nsClipboard.cpp => widget/gtk/nsClipboard.cpp
rename : widget/gtk2/nsClipboard.h => widget/gtk/nsClipboard.h
rename : widget/gtk2/nsColorPicker.cpp => widget/gtk/nsColorPicker.cpp
rename : widget/gtk2/nsColorPicker.h => widget/gtk/nsColorPicker.h
rename : widget/gtk2/nsDeviceContextSpecG.cpp => widget/gtk/nsDeviceContextSpecG.cpp
rename : widget/gtk2/nsDeviceContextSpecG.h => widget/gtk/nsDeviceContextSpecG.h
rename : widget/gtk2/nsDragService.cpp => widget/gtk/nsDragService.cpp
rename : widget/gtk2/nsDragService.h => widget/gtk/nsDragService.h
rename : widget/gtk2/nsFilePicker.cpp => widget/gtk/nsFilePicker.cpp
rename : widget/gtk2/nsFilePicker.h => widget/gtk/nsFilePicker.h
rename : widget/gtk2/nsGTKToolkit.h => widget/gtk/nsGTKToolkit.h
rename : widget/gtk2/nsGtkCursors.h => widget/gtk/nsGtkCursors.h
rename : widget/gtk2/nsGtkIMModule.cpp => widget/gtk/nsGtkIMModule.cpp
rename : widget/gtk2/nsGtkIMModule.h => widget/gtk/nsGtkIMModule.h
rename : widget/gtk2/nsGtkKeyUtils.cpp => widget/gtk/nsGtkKeyUtils.cpp
rename : widget/gtk2/nsGtkKeyUtils.h => widget/gtk/nsGtkKeyUtils.h
rename : widget/gtk2/nsGtkUtils.h => widget/gtk/nsGtkUtils.h
rename : widget/gtk2/nsIImageToPixbuf.h => widget/gtk/nsIImageToPixbuf.h
rename : widget/gtk2/nsIdleServiceGTK.cpp => widget/gtk/nsIdleServiceGTK.cpp
rename : widget/gtk2/nsIdleServiceGTK.h => widget/gtk/nsIdleServiceGTK.h
rename : widget/gtk2/nsImageToPixbuf.cpp => widget/gtk/nsImageToPixbuf.cpp
rename : widget/gtk2/nsImageToPixbuf.h => widget/gtk/nsImageToPixbuf.h
rename : widget/gtk2/nsLookAndFeel.cpp => widget/gtk/nsLookAndFeel.cpp
rename : widget/gtk2/nsLookAndFeel.h => widget/gtk/nsLookAndFeel.h
rename : widget/gtk2/nsNativeKeyBindings.cpp => widget/gtk/nsNativeKeyBindings.cpp
rename : widget/gtk2/nsNativeKeyBindings.h => widget/gtk/nsNativeKeyBindings.h
rename : widget/gtk2/nsNativeThemeGTK.cpp => widget/gtk/nsNativeThemeGTK.cpp
rename : widget/gtk2/nsNativeThemeGTK.h => widget/gtk/nsNativeThemeGTK.h
rename : widget/gtk2/nsPSPrinters.cpp => widget/gtk/nsPSPrinters.cpp
rename : widget/gtk2/nsPSPrinters.h => widget/gtk/nsPSPrinters.h
rename : widget/gtk2/nsPaperPS.cpp => widget/gtk/nsPaperPS.cpp
rename : widget/gtk2/nsPaperPS.h => widget/gtk/nsPaperPS.h
rename : widget/gtk2/nsPrintDialogGTK.cpp => widget/gtk/nsPrintDialogGTK.cpp
rename : widget/gtk2/nsPrintDialogGTK.h => widget/gtk/nsPrintDialogGTK.h
rename : widget/gtk2/nsPrintOptionsGTK.cpp => widget/gtk/nsPrintOptionsGTK.cpp
rename : widget/gtk2/nsPrintOptionsGTK.h => widget/gtk/nsPrintOptionsGTK.h
rename : widget/gtk2/nsPrintSettingsGTK.cpp => widget/gtk/nsPrintSettingsGTK.cpp
rename : widget/gtk2/nsPrintSettingsGTK.h => widget/gtk/nsPrintSettingsGTK.h
rename : widget/gtk2/nsScreenGtk.cpp => widget/gtk/nsScreenGtk.cpp
rename : widget/gtk2/nsScreenGtk.h => widget/gtk/nsScreenGtk.h
rename : widget/gtk2/nsScreenManagerGtk.cpp => widget/gtk/nsScreenManagerGtk.cpp
rename : widget/gtk2/nsScreenManagerGtk.h => widget/gtk/nsScreenManagerGtk.h
rename : widget/gtk2/nsSound.cpp => widget/gtk/nsSound.cpp
rename : widget/gtk2/nsSound.h => widget/gtk/nsSound.h
rename : widget/gtk2/nsToolkit.cpp => widget/gtk/nsToolkit.cpp
rename : widget/gtk2/nsWidgetFactory.cpp => widget/gtk/nsWidgetFactory.cpp
rename : widget/gtk2/nsWindow.cpp => widget/gtk/nsWindow.cpp
rename : widget/gtk2/nsWindow.h => widget/gtk/nsWindow.h
2013-09-23 09:21:57 -04:00

142 lines
4.6 KiB
C++

/* vim:set sw=4 sts=4 et cin: */
/* 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/. */
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "gfxASurface.h"
#include "gfxImageSurface.h"
#include "gfxContext.h"
#include "imgIContainer.h"
#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*)
nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage)
{
return ImageToPixbuf(aImage);
}
GdkPixbuf*
nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage)
{
nsRefPtr<gfxASurface> surface;
aImage->GetFrame(imgIContainer::FRAME_CURRENT,
imgIContainer::FLAG_SYNC_DECODE,
getter_AddRefs(surface));
// If the last call failed, it was probably because our call stack originates
// in an imgINotificationObserver 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 (!surface)
aImage->GetFrame(imgIContainer::FRAME_CURRENT,
imgIContainer::FLAG_NONE,
getter_AddRefs(surface));
NS_ENSURE_TRUE(surface, nullptr);
nsRefPtr<gfxImageSurface> frame(surface->GetAsReadableARGB32ImageSurface());
NS_ENSURE_TRUE(frame, nullptr);
return ImgSurfaceToPixbuf(frame, frame->Width(), frame->Height());
}
GdkPixbuf*
nsImageToPixbuf::ImgSurfaceToPixbuf(gfxImageSurface* aImgSurface, int32_t aWidth, int32_t aHeight)
{
GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
aWidth, aHeight);
if (!pixbuf)
return nullptr;
uint32_t rowstride = gdk_pixbuf_get_rowstride (pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels (pixbuf);
long cairoStride = aImgSurface->Stride();
unsigned char* cairoData = aImgSurface->Data();
gfxASurface::gfxImageFormat format = aImgSurface->Format();
for (int32_t row = 0; row < aHeight; ++row) {
for (int32_t col = 0; col < aWidth; ++col) {
guchar* pixel = pixels + row * rowstride + 4 * col;
uint32_t* cairoPixel = reinterpret_cast<uint32_t*>
((cairoData + row * cairoStride + 4 * col));
if (format == gfxASurface::ImageFormatARGB32) {
const uint8_t a = (*cairoPixel >> 24) & 0xFF;
const uint8_t r = unpremultiply((*cairoPixel >> 16) & 0xFF, a);
const uint8_t g = unpremultiply((*cairoPixel >> 8) & 0xFF, a);
const uint8_t b = unpremultiply((*cairoPixel >> 0) & 0xFF, a);
*pixel++ = r;
*pixel++ = g;
*pixel++ = b;
*pixel++ = a;
} else {
NS_ASSERTION(format == gfxASurface::ImageFormatRGB24,
"unexpected format");
const uint8_t r = (*cairoPixel >> 16) & 0xFF;
const uint8_t g = (*cairoPixel >> 8) & 0xFF;
const uint8_t b = (*cairoPixel >> 0) & 0xFF;
*pixel++ = r;
*pixel++ = g;
*pixel++ = b;
*pixel++ = 0xFF; // A
}
}
}
return pixbuf;
}
GdkPixbuf*
nsImageToPixbuf::SurfaceToPixbuf(gfxASurface* aSurface, int32_t aWidth, int32_t aHeight)
{
if (aSurface->CairoStatus()) {
NS_ERROR("invalid surface");
return nullptr;
}
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 nullptr;
nsRefPtr<gfxContext> context = new gfxContext(imgSurface);
if (!context)
return nullptr;
context->SetOperator(gfxContext::OPERATOR_SOURCE);
context->SetSource(aSurface);
context->Paint();
}
return ImgSurfaceToPixbuf(imgSurface, aWidth, aHeight);
}