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