Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / exported / WebImageSkia.cpp
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 namespace blink {
48
49 WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize)
50 {
51     RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
52     OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
53     if (!decoder)
54         return WebImage();
55
56     decoder->setData(buffer.get(), true);
57     if (!decoder->isSizeAvailable())
58         return WebImage();
59
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) {
69             index = i;
70             break; // Perfect match.
71         }
72
73         const int frameArea = frameSize.width() * frameSize.height();
74         if (frameArea < (desiredSize.width * desiredSize.height))
75             break; // No more frames that are large enough.
76
77         if (!i || (frameArea < frameAreaAtIndex)) {
78             index = i; // Closer to desired area than previous best match.
79             frameAreaAtIndex = frameArea;
80         }
81     }
82
83     ImageFrame* frame = decoder->frameBufferAtIndex(index);
84     if (!frame)
85         return WebImage();
86
87     RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
88     if (!image)
89         return WebImage();
90
91     return WebImage(image->bitmap());
92 }
93
94 WebVector<WebImage> WebImage::framesFromData(const WebData& data)
95 {
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;
98
99     RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
100     OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
101     if (!decoder)
102         return WebVector<WebImage>();
103
104     decoder->setData(buffer.get(), true);
105     if (!decoder->isSizeAvailable())
106         return WebVector<WebImage>();
107
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();
111     IntSize lastSize;
112
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)
117             continue;
118         lastSize = frameSize;
119
120         ImageFrame* frame = decoder->frameBufferAtIndex(i);
121         if (!frame)
122             continue;
123
124         RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
125         if (image && image->isDataComplete())
126             frames.append(WebImage(image->bitmap()));
127     }
128
129     return frames;
130 }
131
132 void WebImage::reset()
133 {
134     m_bitmap.reset();
135 }
136
137 void WebImage::assign(const WebImage& image)
138 {
139     m_bitmap = image.m_bitmap;
140 }
141
142 bool WebImage::isNull() const
143 {
144     return m_bitmap.isNull();
145 }
146
147 WebSize WebImage::size() const
148 {
149     return WebSize(m_bitmap.width(), m_bitmap.height());
150 }
151
152 WebImage::WebImage(const PassRefPtr<Image>& image)
153 {
154     operator=(image);
155 }
156
157 WebImage& WebImage::operator=(const PassRefPtr<Image>& image)
158 {
159     RefPtr<NativeImageSkia> p;
160     if (image && (p = image->nativeImageForCurrentFrame()))
161         assign(p->bitmap());
162     else
163         reset();
164     return *this;
165 }
166
167 } // namespace blink