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/. */
6 #include <gdk-pixbuf/gdk-pixbuf.h>
8 #include "nsImageToPixbuf.h"
10 #include "imgIContainer.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/RefPtr.h"
16 using mozilla::gfx::DataSourceSurface
;
17 using mozilla::gfx::SurfaceFormat
;
19 inline unsigned char unpremultiply(unsigned char color
, unsigned char alpha
) {
20 if (alpha
== 0) return 0;
21 // plus alpha/2 to round instead of truncate
22 return (color
* 255 + alpha
/ 2) / alpha
;
25 already_AddRefed
<GdkPixbuf
> nsImageToPixbuf::ImageToPixbuf(
26 imgIContainer
* aImage
, const mozilla::Maybe
<nsIntSize
>& aOverrideSize
) {
27 RefPtr
<SourceSurface
> surface
;
29 const uint32_t flags
=
30 imgIContainer::FLAG_SYNC_DECODE
| imgIContainer::FLAG_ASYNC_NOTIFY
;
32 surface
= aImage
->GetFrameAtSize(*aOverrideSize
,
33 imgIContainer::FRAME_CURRENT
, flags
);
35 surface
= aImage
->GetFrame(imgIContainer::FRAME_CURRENT
, flags
);
38 // If the last call failed, it was probably because our call stack originates
39 // in an imgINotificationObserver event, meaning that we're not allowed
40 // request a sync decode. Presumably the originating event is something
41 // sensible like OnStopFrame(), so we can just retry the call without a sync
46 aImage
->GetFrameAtSize(*aOverrideSize
, imgIContainer::FRAME_CURRENT
,
47 imgIContainer::FLAG_NONE
);
49 surface
= aImage
->GetFrame(imgIContainer::FRAME_CURRENT
,
50 imgIContainer::FLAG_NONE
);
54 NS_ENSURE_TRUE(surface
, nullptr);
56 return SourceSurfaceToPixbuf(surface
, surface
->GetSize().width
,
57 surface
->GetSize().height
);
60 already_AddRefed
<GdkPixbuf
> nsImageToPixbuf::SourceSurfaceToPixbuf(
61 SourceSurface
* aSurface
, int32_t aWidth
, int32_t aHeight
) {
62 MOZ_ASSERT(aWidth
<= aSurface
->GetSize().width
&&
63 aHeight
<= aSurface
->GetSize().height
,
64 "Requested rect is bigger than the supplied surface");
66 RefPtr
<GdkPixbuf
> pixbuf
=
67 dont_AddRef(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, TRUE
, 8, aWidth
, aHeight
));
72 uint32_t destStride
= gdk_pixbuf_get_rowstride(pixbuf
);
73 guchar
* destPixels
= gdk_pixbuf_get_pixels(pixbuf
);
75 RefPtr
<DataSourceSurface
> dataSurface
= aSurface
->GetDataSurface();
76 DataSourceSurface::MappedSurface map
;
77 if (!dataSurface
->Map(DataSourceSurface::MapType::READ
, &map
)) {
81 uint8_t* srcData
= map
.mData
;
82 int32_t srcStride
= map
.mStride
;
84 SurfaceFormat format
= dataSurface
->GetFormat();
86 for (int32_t row
= 0; row
< aHeight
; ++row
) {
87 for (int32_t col
= 0; col
< aWidth
; ++col
) {
88 guchar
* destPixel
= destPixels
+ row
* destStride
+ 4 * col
;
91 reinterpret_cast<uint32_t*>((srcData
+ row
* srcStride
+ 4 * col
));
93 if (format
== SurfaceFormat::B8G8R8A8
) {
94 const uint8_t a
= (*srcPixel
>> 24) & 0xFF;
95 const uint8_t r
= unpremultiply((*srcPixel
>> 16) & 0xFF, a
);
96 const uint8_t g
= unpremultiply((*srcPixel
>> 8) & 0xFF, a
);
97 const uint8_t b
= unpremultiply((*srcPixel
>> 0) & 0xFF, a
);
104 MOZ_ASSERT(format
== SurfaceFormat::B8G8R8X8
);
106 const uint8_t r
= (*srcPixel
>> 16) & 0xFF;
107 const uint8_t g
= (*srcPixel
>> 8) & 0xFF;
108 const uint8_t b
= (*srcPixel
>> 0) & 0xFF;
113 *destPixel
++ = 0xFF; // A
118 dataSurface
->Unmap();
120 return pixbuf
.forget();