widget/gtk/nsImageToPixbuf.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim:set sw=4 sts=4 et cin: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <gdk-pixbuf/gdk-pixbuf.h>
michael@0 7
michael@0 8 #include "nsImageToPixbuf.h"
michael@0 9
michael@0 10 #include "imgIContainer.h"
michael@0 11 #include "mozilla/gfx/2D.h"
michael@0 12 #include "mozilla/RefPtr.h"
michael@0 13 #include "nsAutoPtr.h"
michael@0 14
michael@0 15 using mozilla::gfx::DataSourceSurface;
michael@0 16 using mozilla::gfx::SurfaceFormat;
michael@0 17 using mozilla::RefPtr;
michael@0 18
michael@0 19 NS_IMPL_ISUPPORTS(nsImageToPixbuf, nsIImageToPixbuf)
michael@0 20
michael@0 21 inline unsigned char
michael@0 22 unpremultiply (unsigned char color,
michael@0 23 unsigned char alpha)
michael@0 24 {
michael@0 25 if (alpha == 0)
michael@0 26 return 0;
michael@0 27 // plus alpha/2 to round instead of truncate
michael@0 28 return (color * 255 + alpha / 2) / alpha;
michael@0 29 }
michael@0 30
michael@0 31 NS_IMETHODIMP_(GdkPixbuf*)
michael@0 32 nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer* aImage)
michael@0 33 {
michael@0 34 return ImageToPixbuf(aImage);
michael@0 35 }
michael@0 36
michael@0 37 GdkPixbuf*
michael@0 38 nsImageToPixbuf::ImageToPixbuf(imgIContainer* aImage)
michael@0 39 {
michael@0 40 RefPtr<SourceSurface> surface =
michael@0 41 aImage->GetFrame(imgIContainer::FRAME_CURRENT,
michael@0 42 imgIContainer::FLAG_SYNC_DECODE);
michael@0 43
michael@0 44 // If the last call failed, it was probably because our call stack originates
michael@0 45 // in an imgINotificationObserver event, meaning that we're not allowed request
michael@0 46 // a sync decode. Presumably the originating event is something sensible like
michael@0 47 // OnStopFrame(), so we can just retry the call without a sync decode.
michael@0 48 if (!surface)
michael@0 49 surface = aImage->GetFrame(imgIContainer::FRAME_CURRENT,
michael@0 50 imgIContainer::FLAG_NONE);
michael@0 51
michael@0 52 NS_ENSURE_TRUE(surface, nullptr);
michael@0 53
michael@0 54 return SourceSurfaceToPixbuf(surface,
michael@0 55 surface->GetSize().width,
michael@0 56 surface->GetSize().height);
michael@0 57 }
michael@0 58
michael@0 59 GdkPixbuf*
michael@0 60 nsImageToPixbuf::SourceSurfaceToPixbuf(SourceSurface* aSurface,
michael@0 61 int32_t aWidth,
michael@0 62 int32_t aHeight)
michael@0 63 {
michael@0 64 MOZ_ASSERT(aWidth <= aSurface->GetSize().width &&
michael@0 65 aHeight <= aSurface->GetSize().height,
michael@0 66 "Requested rect is bigger than the supplied surface");
michael@0 67
michael@0 68 GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8,
michael@0 69 aWidth, aHeight);
michael@0 70 if (!pixbuf)
michael@0 71 return nullptr;
michael@0 72
michael@0 73 uint32_t destStride = gdk_pixbuf_get_rowstride (pixbuf);
michael@0 74 guchar* destPixels = gdk_pixbuf_get_pixels (pixbuf);
michael@0 75
michael@0 76 RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
michael@0 77 DataSourceSurface::MappedSurface map;
michael@0 78 dataSurface->Map(DataSourceSurface::MapType::READ, &map);
michael@0 79 uint8_t* srcData = map.mData;
michael@0 80 int32_t srcStride = map.mStride;
michael@0 81
michael@0 82 SurfaceFormat format = dataSurface->GetFormat();
michael@0 83
michael@0 84 for (int32_t row = 0; row < aHeight; ++row) {
michael@0 85 for (int32_t col = 0; col < aWidth; ++col) {
michael@0 86 guchar* destPixel = destPixels + row * destStride + 4 * col;
michael@0 87
michael@0 88 uint32_t* srcPixel =
michael@0 89 reinterpret_cast<uint32_t*>((srcData + row * srcStride + 4 * col));
michael@0 90
michael@0 91 if (format == SurfaceFormat::B8G8R8A8) {
michael@0 92 const uint8_t a = (*srcPixel >> 24) & 0xFF;
michael@0 93 const uint8_t r = unpremultiply((*srcPixel >> 16) & 0xFF, a);
michael@0 94 const uint8_t g = unpremultiply((*srcPixel >> 8) & 0xFF, a);
michael@0 95 const uint8_t b = unpremultiply((*srcPixel >> 0) & 0xFF, a);
michael@0 96
michael@0 97 *destPixel++ = r;
michael@0 98 *destPixel++ = g;
michael@0 99 *destPixel++ = b;
michael@0 100 *destPixel++ = a;
michael@0 101 } else {
michael@0 102 MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8);
michael@0 103
michael@0 104 const uint8_t r = (*srcPixel >> 16) & 0xFF;
michael@0 105 const uint8_t g = (*srcPixel >> 8) & 0xFF;
michael@0 106 const uint8_t b = (*srcPixel >> 0) & 0xFF;
michael@0 107
michael@0 108 *destPixel++ = r;
michael@0 109 *destPixel++ = g;
michael@0 110 *destPixel++ = b;
michael@0 111 *destPixel++ = 0xFF; // A
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 dataSurface->Unmap();
michael@0 117
michael@0 118 return pixbuf;
michael@0 119 }
michael@0 120

mercurial