1 /* vim:set sw=4 sts=4 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"
13 #include "nsAutoPtr.h"
15 using mozilla::gfx::DataSourceSurface
;
16 using mozilla::gfx::SurfaceFormat
;
17 using mozilla::RefPtr
;
19 NS_IMPL_ISUPPORTS(nsImageToPixbuf
, nsIImageToPixbuf
)
22 unpremultiply (unsigned char color
,
27 // plus alpha/2 to round instead of truncate
28 return (color
* 255 + alpha
/ 2) / alpha
;
31 NS_IMETHODIMP_(GdkPixbuf
*)
32 nsImageToPixbuf::ConvertImageToPixbuf(imgIContainer
* aImage
)
34 return ImageToPixbuf(aImage
);
38 nsImageToPixbuf::ImageToPixbuf(imgIContainer
* aImage
)
40 RefPtr
<SourceSurface
> surface
=
41 aImage
->GetFrame(imgIContainer::FRAME_CURRENT
,
42 imgIContainer::FLAG_SYNC_DECODE
);
44 // If the last call failed, it was probably because our call stack originates
45 // in an imgINotificationObserver event, meaning that we're not allowed request
46 // a sync decode. Presumably the originating event is something sensible like
47 // OnStopFrame(), so we can just retry the call without a sync decode.
49 surface
= aImage
->GetFrame(imgIContainer::FRAME_CURRENT
,
50 imgIContainer::FLAG_NONE
);
52 NS_ENSURE_TRUE(surface
, nullptr);
54 return SourceSurfaceToPixbuf(surface
,
55 surface
->GetSize().width
,
56 surface
->GetSize().height
);
60 nsImageToPixbuf::SourceSurfaceToPixbuf(SourceSurface
* aSurface
,
64 MOZ_ASSERT(aWidth
<= aSurface
->GetSize().width
&&
65 aHeight
<= aSurface
->GetSize().height
,
66 "Requested rect is bigger than the supplied surface");
68 GdkPixbuf
* pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, TRUE
, 8,
73 uint32_t destStride
= gdk_pixbuf_get_rowstride (pixbuf
);
74 guchar
* destPixels
= gdk_pixbuf_get_pixels (pixbuf
);
76 RefPtr
<DataSourceSurface
> dataSurface
= aSurface
->GetDataSurface();
77 DataSourceSurface::MappedSurface map
;
78 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();