1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32
33 #include "public/platform/WebImage.h"
34
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"
45 #include <algorithm>
46
47 using namespace WebCore;
48
49 namespace blink {
50
fromData(const WebData & data,const WebSize & desiredSize)51 WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize)
52 {
53 RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
54 OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
55 if (!decoder)
56 return WebImage();
57
58 decoder->setData(buffer.get(), true);
59 if (!decoder->isSizeAvailable())
60 return WebImage();
61
62 // Frames are arranged by decreasing size, then decreasing bit depth.
63 // Pick the frame closest to |desiredSize|'s area without being smaller,
64 // which has the highest bit depth.
65 const size_t frameCount = decoder->frameCount();
66 size_t index = 0; // Default to first frame if none are large enough.
67 int frameAreaAtIndex = 0;
68 for (size_t i = 0; i < frameCount; ++i) {
69 const IntSize frameSize = decoder->frameSizeAtIndex(i);
70 if (WebSize(frameSize) == desiredSize) {
71 index = i;
72 break; // Perfect match.
73 }
74
75 const int frameArea = frameSize.width() * frameSize.height();
76 if (frameArea < (desiredSize.width * desiredSize.height))
77 break; // No more frames that are large enough.
78
79 if (!i || (frameArea < frameAreaAtIndex)) {
80 index = i; // Closer to desired area than previous best match.
81 frameAreaAtIndex = frameArea;
82 }
83 }
84
85 ImageFrame* frame = decoder->frameBufferAtIndex(index);
86 if (!frame)
87 return WebImage();
88
89 RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
90 if (!image)
91 return WebImage();
92
93 return WebImage(image->bitmap());
94 }
95
framesFromData(const WebData & data)96 WebVector<WebImage> WebImage::framesFromData(const WebData& data)
97 {
98 // This is to protect from malicious images. It should be big enough that it's never hit in pracice.
99 const size_t maxFrameCount = 8;
100
101 RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
102 OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
103 if (!decoder)
104 return WebVector<WebImage>();
105
106 decoder->setData(buffer.get(), true);
107 if (!decoder->isSizeAvailable())
108 return WebVector<WebImage>();
109
110 // Frames are arranged by decreasing size, then decreasing bit depth.
111 // Keep the first frame at every size, has the highest bit depth.
112 const size_t frameCount = decoder->frameCount();
113 IntSize lastSize;
114
115 Vector<WebImage> frames;
116 for (size_t i = 0; i < std::min(frameCount, maxFrameCount); ++i) {
117 const IntSize frameSize = decoder->frameSizeAtIndex(i);
118 if (frameSize == lastSize)
119 continue;
120 lastSize = frameSize;
121
122 ImageFrame* frame = decoder->frameBufferAtIndex(i);
123 if (!frame)
124 continue;
125
126 RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
127 if (image && image->isDataComplete())
128 frames.append(WebImage(image->bitmap()));
129 }
130
131 return frames;
132 }
133
reset()134 void WebImage::reset()
135 {
136 m_bitmap.reset();
137 }
138
assign(const WebImage & image)139 void WebImage::assign(const WebImage& image)
140 {
141 m_bitmap = image.m_bitmap;
142 }
143
isNull() const144 bool WebImage::isNull() const
145 {
146 return m_bitmap.isNull();
147 }
148
size() const149 WebSize WebImage::size() const
150 {
151 return WebSize(m_bitmap.width(), m_bitmap.height());
152 }
153
WebImage(const PassRefPtr<Image> & image)154 WebImage::WebImage(const PassRefPtr<Image>& image)
155 {
156 operator=(image);
157 }
158
operator =(const PassRefPtr<Image> & image)159 WebImage& WebImage::operator=(const PassRefPtr<Image>& image)
160 {
161 RefPtr<NativeImageSkia> p;
162 if (image && (p = image->nativeImageForCurrentFrame()))
163 assign(p->bitmap());
164 else
165 reset();
166 return *this;
167 }
168
169 } // namespace blink
170