1 /*
2 * Copyright (C) 2006, 2007 Apple Computer, Kevin Ollivier. 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "ImageSource.h"
28
29 #include "BMPImageDecoder.h"
30 #include "GIFImageDecoder.h"
31 #include "ICOImageDecoder.h"
32 #include "JPEGImageDecoder.h"
33 #include "NotImplemented.h"
34 #include "PNGImageDecoder.h"
35 #include "SharedBuffer.h"
36 #include "XBMImageDecoder.h"
37
38 #include <wx/defs.h>
39 #include <wx/bitmap.h>
40 #if USE(WXGC)
41 #include <wx/graphics.h>
42 #endif
43 #include <wx/image.h>
44 #include <wx/rawbmp.h>
45
46 namespace WebCore {
47
createDecoder(const SharedBuffer & data)48 ImageDecoder* createDecoder(const SharedBuffer& data)
49 {
50 // We need at least 4 bytes to figure out what kind of image we're dealing with.
51 int length = data.size();
52 if (length < 4)
53 return 0;
54
55 const unsigned char* uContents = (const unsigned char*)data.data();
56 const char* contents = data.data();
57
58 // GIFs begin with GIF8(7 or 9).
59 if (strncmp(contents, "GIF8", 4) == 0)
60 return new GIFImageDecoder();
61
62 // Test for PNG.
63 if (uContents[0]==0x89 &&
64 uContents[1]==0x50 &&
65 uContents[2]==0x4E &&
66 uContents[3]==0x47)
67 return new PNGImageDecoder();
68
69 // JPEG
70 if (uContents[0]==0xFF &&
71 uContents[1]==0xD8 &&
72 uContents[2]==0xFF)
73 return new JPEGImageDecoder();
74
75 // BMP
76 if (strncmp(contents, "BM", 2) == 0)
77 return new BMPImageDecoder();
78
79 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
80 // CURs begin with 2-byte 0 followed by 2-byte 2.
81 if (!memcmp(contents, "\000\000\001\000", 4) ||
82 !memcmp(contents, "\000\000\002\000", 4))
83 return new ICOImageDecoder();
84
85 // XBMs require 8 bytes of info.
86 if (length >= 8 && strncmp(contents, "#define ", 8) == 0)
87 return new XBMImageDecoder();
88
89 // Give up. We don't know what the heck this is.
90 return 0;
91 }
92
ImageSource()93 ImageSource::ImageSource()
94 : m_decoder(0)
95 {}
96
~ImageSource()97 ImageSource::~ImageSource()
98 {
99 clear(true);
100 }
101
initialized() const102 bool ImageSource::initialized() const
103 {
104 return m_decoder;
105 }
106
setData(SharedBuffer * data,bool allDataReceived)107 void ImageSource::setData(SharedBuffer* data, bool allDataReceived)
108 {
109 // Make the decoder by sniffing the bytes.
110 // This method will examine the data and instantiate an instance of the appropriate decoder plugin.
111 // If insufficient bytes are available to determine the image type, no decoder plugin will be
112 // made.
113 if (m_decoder)
114 delete m_decoder;
115 m_decoder = createDecoder(*data);
116 if (!m_decoder)
117 return;
118 m_decoder->setData(data, allDataReceived);
119 }
120
isSizeAvailable()121 bool ImageSource::isSizeAvailable()
122 {
123 if (!m_decoder)
124 return false;
125
126 return m_decoder->isSizeAvailable();
127 }
128
size() const129 IntSize ImageSource::size() const
130 {
131 if (!m_decoder)
132 return IntSize();
133
134 return m_decoder->size();
135 }
136
frameSizeAtIndex(size_t index) const137 IntSize ImageSource::frameSizeAtIndex(size_t index) const
138 {
139 if (!m_decoder)
140 return IntSize();
141
142 return m_decoder->frameSizeAtIndex(index);
143 }
144
repetitionCount()145 int ImageSource::repetitionCount()
146 {
147 if (!m_decoder)
148 return cAnimationNone;
149
150 return m_decoder->repetitionCount();
151 }
152
filenameExtension() const153 String ImageSource::filenameExtension() const
154 {
155 notImplemented();
156 return String();
157 }
158
frameCount() const159 size_t ImageSource::frameCount() const
160 {
161 return m_decoder ? m_decoder->frameCount() : 0;
162 }
163
frameIsCompleteAtIndex(size_t index)164 bool ImageSource::frameIsCompleteAtIndex(size_t index)
165 {
166 // FIXME: should we be testing the RGBA32Buffer's status as well?
167 return (m_decoder && m_decoder->frameBufferAtIndex(index) != 0);
168 }
169
clear(bool destroyAll,size_t clearBeforeFrame,SharedBuffer * data,bool allDataReceived)170 void ImageSource::clear(bool destroyAll, size_t clearBeforeFrame, SharedBuffer* data, bool allDataReceived)
171 {
172 if (!destroyAll) {
173 if (m_decoder)
174 m_decoder->clearFrameBufferCache(clearBeforeFrame);
175 return;
176 }
177
178 delete m_decoder;
179 m_decoder = 0;
180 if (data)
181 setData(data, allDataReceived);
182 }
183
createFrameAtIndex(size_t index)184 NativeImagePtr ImageSource::createFrameAtIndex(size_t index)
185 {
186 if (!m_decoder)
187 return 0;
188
189 RGBA32Buffer* buffer = m_decoder->frameBufferAtIndex(index);
190 if (!buffer || buffer->status() == RGBA32Buffer::FrameEmpty)
191 return 0;
192
193 return buffer->asNewNativeImage();
194 }
195
frameDurationAtIndex(size_t index)196 float ImageSource::frameDurationAtIndex(size_t index)
197 {
198 if (!m_decoder)
199 return 0;
200
201 RGBA32Buffer* buffer = m_decoder->frameBufferAtIndex(index);
202 if (!buffer || buffer->status() == RGBA32Buffer::FrameEmpty)
203 return 0;
204
205 float duration = buffer->duration() / 1000.0f;
206
207 // Follow other ports (and WinIE's) behavior to slow annoying ads that
208 // specify a 0 duration.
209 if (duration < 0.051f)
210 return 0.100f;
211 return duration;
212 }
213
frameHasAlphaAtIndex(size_t index)214 bool ImageSource::frameHasAlphaAtIndex(size_t index)
215 {
216 if (!m_decoder || !m_decoder->supportsAlpha())
217 return false;
218
219 RGBA32Buffer* buffer = m_decoder->frameBufferAtIndex(index);
220 if (!buffer || buffer->status() == RGBA32Buffer::FrameEmpty)
221 return false;
222
223 return buffer->hasAlpha();
224 }
225
226 }
227