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/graphics/skia/NativeImageSkia.h"
38 #include "platform/image-decoders/ImageDecoder.h"
39 #include "public/platform/WebData.h"
40 #include "public/platform/WebSize.h"
41 #include "wtf/OwnPtr.h"
42 #include "wtf/PassOwnPtr.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/Vector.h"
49 WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize)
51 RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
52 OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
56 decoder->setData(buffer.get(), true);
57 if (!decoder->isSizeAvailable())
60 // Frames are arranged by decreasing size, then decreasing bit depth.
61 // Pick the frame closest to |desiredSize|'s area without being smaller,
62 // which has the highest bit depth.
63 const size_t frameCount = decoder->frameCount();
64 size_t index = 0; // Default to first frame if none are large enough.
65 int frameAreaAtIndex = 0;
66 for (size_t i = 0; i < frameCount; ++i) {
67 const IntSize frameSize = decoder->frameSizeAtIndex(i);
68 if (WebSize(frameSize) == desiredSize) {
70 break; // Perfect match.
73 const int frameArea = frameSize.width() * frameSize.height();
74 if (frameArea < (desiredSize.width * desiredSize.height))
75 break; // No more frames that are large enough.
77 if (!i || (frameArea < frameAreaAtIndex)) {
78 index = i; // Closer to desired area than previous best match.
79 frameAreaAtIndex = frameArea;
83 ImageFrame* frame = decoder->frameBufferAtIndex(index);
87 RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
91 return WebImage(image->bitmap());
94 WebVector<WebImage> WebImage::framesFromData(const WebData& data)
96 // This is to protect from malicious images. It should be big enough that it's never hit in pracice.
97 const size_t maxFrameCount = 8;
99 RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
100 OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
102 return WebVector<WebImage>();
104 decoder->setData(buffer.get(), true);
105 if (!decoder->isSizeAvailable())
106 return WebVector<WebImage>();
108 // Frames are arranged by decreasing size, then decreasing bit depth.
109 // Keep the first frame at every size, has the highest bit depth.
110 const size_t frameCount = decoder->frameCount();
113 Vector<WebImage> frames;
114 for (size_t i = 0; i < std::min(frameCount, maxFrameCount); ++i) {
115 const IntSize frameSize = decoder->frameSizeAtIndex(i);
116 if (frameSize == lastSize)
118 lastSize = frameSize;
120 ImageFrame* frame = decoder->frameBufferAtIndex(i);
124 RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
125 if (image && image->isDataComplete())
126 frames.append(WebImage(image->bitmap()));
132 void WebImage::reset()
137 void WebImage::assign(const WebImage& image)
139 m_bitmap = image.m_bitmap;
142 bool WebImage::isNull() const
144 return m_bitmap.isNull();
147 WebSize WebImage::size() const
149 return WebSize(m_bitmap.width(), m_bitmap.height());
152 WebImage::WebImage(const PassRefPtr<Image>& image)
157 WebImage& WebImage::operator=(const PassRefPtr<Image>& image)
159 RefPtr<NativeImageSkia> p;
160 if (image && (p = image->nativeImageForCurrentFrame()))