1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gtk/nsImageToPixbuf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* vim:set sw=4 sts=4 et cin: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <gdk-pixbuf/gdk-pixbuf.h> 1.10 + 1.11 +#include "nsImageToPixbuf.h" 1.12 + 1.13 +#include "imgIContainer.h" 1.14 +#include "mozilla/gfx/2D.h" 1.15 +#include "mozilla/RefPtr.h" 1.16 +#include "nsAutoPtr.h" 1.17 + 1.18 +using mozilla::gfx::DataSourceSurface; 1.19 +using mozilla::gfx::SurfaceFormat; 1.20 +using mozilla::RefPtr; 1.21 + 1.22 +NS_IMPL_ISUPPORTS(nsImageToPixbuf, nsIImageToPixbuf) 1.23 + 1.24 +inline unsigned char 1.25 +unpremultiply (unsigned char color, 1.26 + unsigned char alpha) 1.27 +{ 1.28 + if (alpha == 0) 1.29 + return 0; 1.30 + // plus alpha/2 to round instead of truncate 1.31 + return (color * 255 + alpha / 2) / alpha; 1.32 +} 1.33 + 1.34 +NS_IMETHODIMP_(GdkPixbuf*) 1.35 +nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage) 1.36 +{ 1.37 + return ImageToPixbuf(aImage); 1.38 +} 1.39 + 1.40 +GdkPixbuf* 1.41 +nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage) 1.42 +{ 1.43 + RefPtr<SourceSurface> surface = 1.44 + aImage->GetFrame(imgIContainer::FRAME_CURRENT, 1.45 + imgIContainer::FLAG_SYNC_DECODE); 1.46 + 1.47 + // If the last call failed, it was probably because our call stack originates 1.48 + // in an imgINotificationObserver event, meaning that we're not allowed request 1.49 + // a sync decode. Presumably the originating event is something sensible like 1.50 + // OnStopFrame(), so we can just retry the call without a sync decode. 1.51 + if (!surface) 1.52 + surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT, 1.53 + imgIContainer::FLAG_NONE); 1.54 + 1.55 + NS_ENSURE_TRUE(surface, nullptr); 1.56 + 1.57 + return SourceSurfaceToPixbuf(surface, 1.58 + surface->GetSize().width, 1.59 + surface->GetSize().height); 1.60 +} 1.61 + 1.62 +GdkPixbuf* 1.63 +nsImageToPixbuf::SourceSurfaceToPixbuf(SourceSurface* aSurface, 1.64 + int32_t aWidth, 1.65 + int32_t aHeight) 1.66 +{ 1.67 + MOZ_ASSERT(aWidth <= aSurface->GetSize().width && 1.68 + aHeight <= aSurface->GetSize().height, 1.69 + "Requested rect is bigger than the supplied surface"); 1.70 + 1.71 + GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1.72 + aWidth, aHeight); 1.73 + if (!pixbuf) 1.74 + return nullptr; 1.75 + 1.76 + uint32_t destStride = gdk_pixbuf_get_rowstride (pixbuf); 1.77 + guchar* destPixels = gdk_pixbuf_get_pixels (pixbuf); 1.78 + 1.79 + RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface(); 1.80 + DataSourceSurface::MappedSurface map; 1.81 + dataSurface->Map(DataSourceSurface::MapType::READ, &map); 1.82 + uint8_t* srcData = map.mData; 1.83 + int32_t srcStride = map.mStride; 1.84 + 1.85 + SurfaceFormat format = dataSurface->GetFormat(); 1.86 + 1.87 + for (int32_t row = 0; row < aHeight; ++row) { 1.88 + for (int32_t col = 0; col < aWidth; ++col) { 1.89 + guchar* destPixel = destPixels + row * destStride + 4 * col; 1.90 + 1.91 + uint32_t* srcPixel = 1.92 + reinterpret_cast<uint32_t*>((srcData + row * srcStride + 4 * col)); 1.93 + 1.94 + if (format == SurfaceFormat::B8G8R8A8) { 1.95 + const uint8_t a = (*srcPixel >> 24) & 0xFF; 1.96 + const uint8_t r = unpremultiply((*srcPixel >> 16) & 0xFF, a); 1.97 + const uint8_t g = unpremultiply((*srcPixel >> 8) & 0xFF, a); 1.98 + const uint8_t b = unpremultiply((*srcPixel >> 0) & 0xFF, a); 1.99 + 1.100 + *destPixel++ = r; 1.101 + *destPixel++ = g; 1.102 + *destPixel++ = b; 1.103 + *destPixel++ = a; 1.104 + } else { 1.105 + MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8); 1.106 + 1.107 + const uint8_t r = (*srcPixel >> 16) & 0xFF; 1.108 + const uint8_t g = (*srcPixel >> 8) & 0xFF; 1.109 + const uint8_t b = (*srcPixel >> 0) & 0xFF; 1.110 + 1.111 + *destPixel++ = r; 1.112 + *destPixel++ = g; 1.113 + *destPixel++ = b; 1.114 + *destPixel++ = 0xFF; // A 1.115 + } 1.116 + } 1.117 + } 1.118 + 1.119 + dataSurface->Unmap(); 1.120 + 1.121 + return pixbuf; 1.122 +} 1.123 +