2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "public/platform/WebImage.h"
35 #include "platform/SharedBuffer.h"
36 #include "platform/graphics/Image.h"
37 #include "platform/image-decoders/ImageDecoder.h"
38 #include "public/platform/WebData.h"
39 #include "public/platform/WebSize.h"
40 #include "wtf/OwnPtr.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/PassRefPtr.h"
43 #include "wtf/Vector.h"
48 WebImage
WebImage::fromData(const WebData
& data
, const WebSize
& desiredSize
)
50 RefPtr
<SharedBuffer
> buffer
= PassRefPtr
<SharedBuffer
>(data
);
51 OwnPtr
<ImageDecoder
> decoder(ImageDecoder::create(*buffer
.get(), ImageDecoder::AlphaPremultiplied
, ImageDecoder::GammaAndColorProfileIgnored
));
55 decoder
->setData(buffer
.get(), true);
56 if (!decoder
->isSizeAvailable())
59 // Frames are arranged by decreasing size, then decreasing bit depth.
60 // Pick the frame closest to |desiredSize|'s area without being smaller,
61 // which has the highest bit depth.
62 const size_t frameCount
= decoder
->frameCount();
63 size_t index
= 0; // Default to first frame if none are large enough.
64 int frameAreaAtIndex
= 0;
65 for (size_t i
= 0; i
< frameCount
; ++i
) {
66 const IntSize frameSize
= decoder
->frameSizeAtIndex(i
);
67 if (WebSize(frameSize
) == desiredSize
) {
69 break; // Perfect match.
72 const int frameArea
= frameSize
.width() * frameSize
.height();
73 if (frameArea
< (desiredSize
.width
* desiredSize
.height
))
74 break; // No more frames that are large enough.
76 if (!i
|| (frameArea
< frameAreaAtIndex
)) {
77 index
= i
; // Closer to desired area than previous best match.
78 frameAreaAtIndex
= frameArea
;
82 ImageFrame
* frame
= decoder
->frameBufferAtIndex(index
);
86 return WebImage(frame
->bitmap());
89 WebVector
<WebImage
> WebImage::framesFromData(const WebData
& data
)
91 // This is to protect from malicious images. It should be big enough that it's never hit in pracice.
92 const size_t maxFrameCount
= 8;
94 RefPtr
<SharedBuffer
> buffer
= PassRefPtr
<SharedBuffer
>(data
);
95 OwnPtr
<ImageDecoder
> decoder(ImageDecoder::create(*buffer
.get(), ImageDecoder::AlphaPremultiplied
, ImageDecoder::GammaAndColorProfileIgnored
));
97 return WebVector
<WebImage
>();
99 decoder
->setData(buffer
.get(), true);
100 if (!decoder
->isSizeAvailable())
101 return WebVector
<WebImage
>();
103 // Frames are arranged by decreasing size, then decreasing bit depth.
104 // Keep the first frame at every size, has the highest bit depth.
105 const size_t frameCount
= decoder
->frameCount();
108 Vector
<WebImage
> frames
;
109 for (size_t i
= 0; i
< std::min(frameCount
, maxFrameCount
); ++i
) {
110 const IntSize frameSize
= decoder
->frameSizeAtIndex(i
);
111 if (frameSize
== lastSize
)
113 lastSize
= frameSize
;
115 ImageFrame
* frame
= decoder
->frameBufferAtIndex(i
);
119 const SkBitmap
& bitmap
= frame
->bitmap();
120 if (!bitmap
.isNull() && bitmap
.isImmutable())
121 frames
.append(WebImage(bitmap
));
127 void WebImage::reset()
132 void WebImage::assign(const WebImage
& image
)
134 m_bitmap
= image
.m_bitmap
;
137 bool WebImage::isNull() const
139 return m_bitmap
.isNull();
142 WebSize
WebImage::size() const
144 return WebSize(m_bitmap
.width(), m_bitmap
.height());
147 WebImage::WebImage(const PassRefPtr
<Image
>& image
)
152 WebImage
& WebImage::operator=(const PassRefPtr
<Image
>& image
)
155 if (image
&& image
->deprecatedBitmapForCurrentFrame(&p
))