author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 03 May 2010 13:17:34 +0300 | |
changeset 19 | fcece45ef507 |
parent 3 | 41300fa6a67c |
child 29 | b72c6db6890b |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 2006 Friedemann Kleint <fkleint@trolltech.com> |
|
3 |
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
|
4 |
* |
|
5 |
* All rights reserved. |
|
6 |
* |
|
7 |
* Redistribution and use in source and binary forms, with or without |
|
8 |
* modification, are permitted provided that the following conditions |
|
9 |
* are met: |
|
10 |
* 1. Redistributions of source code must retain the above copyright |
|
11 |
* notice, this list of conditions and the following disclaimer. |
|
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
|
13 |
* notice, this list of conditions and the following disclaimer in the |
|
14 |
* documentation and/or other materials provided with the distribution. |
|
15 |
* |
|
16 |
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
17 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
19 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
20 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
21 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
23 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
24 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
26 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 |
*/ |
|
28 |
||
29 |
#include "config.h" |
|
30 |
#include "ImageDecoderQt.h" |
|
31 |
||
32 |
#include <QtCore/QByteArray> |
|
33 |
#include <QtCore/QBuffer> |
|
34 |
||
35 |
#include <QtGui/QImageReader> |
|
36 |
#include <qdebug.h> |
|
37 |
||
38 |
namespace WebCore { |
|
39 |
||
40 |
ImageDecoder* ImageDecoder::create(const SharedBuffer& data) |
|
41 |
{ |
|
42 |
// We need at least 4 bytes to figure out what kind of image we're dealing with. |
|
43 |
if (data.size() < 4) |
|
44 |
return 0; |
|
45 |
||
46 |
return new ImageDecoderQt; |
|
47 |
} |
|
48 |
||
49 |
ImageDecoderQt::ImageDecoderQt() |
|
50 |
: m_buffer(0) |
|
51 |
, m_reader(0) |
|
52 |
, m_repetitionCount(cAnimationNone) |
|
53 |
{ |
|
54 |
} |
|
55 |
||
56 |
ImageDecoderQt::~ImageDecoderQt() |
|
57 |
{ |
|
58 |
delete m_reader; |
|
59 |
delete m_buffer; |
|
60 |
} |
|
61 |
||
62 |
void ImageDecoderQt::setData(SharedBuffer* data, bool allDataReceived) |
|
63 |
{ |
|
64 |
if (m_failed) |
|
65 |
return; |
|
66 |
||
67 |
// No progressive loading possible |
|
68 |
if (!allDataReceived) |
|
69 |
return; |
|
70 |
||
71 |
// Cache our own new data. |
|
72 |
ImageDecoder::setData(data, allDataReceived); |
|
73 |
||
74 |
// We expect to be only called once with allDataReceived |
|
75 |
ASSERT(!m_buffer); |
|
76 |
ASSERT(!m_reader); |
|
77 |
||
78 |
// Attempt to load the data |
|
79 |
QByteArray imageData = QByteArray::fromRawData(m_data->data(), m_data->size()); |
|
80 |
m_buffer = new QBuffer; |
|
81 |
m_buffer->setData(imageData); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
82 |
m_buffer->open(QBuffer::ReadOnly | QIODevice::Unbuffered); |
0 | 83 |
m_reader = new QImageReader(m_buffer, m_format); |
84 |
} |
|
85 |
||
86 |
bool ImageDecoderQt::isSizeAvailable() |
|
87 |
{ |
|
88 |
if (!ImageDecoder::isSizeAvailable() && m_reader) |
|
89 |
internalDecodeSize(); |
|
90 |
||
91 |
return ImageDecoder::isSizeAvailable(); |
|
92 |
} |
|
93 |
||
94 |
size_t ImageDecoderQt::frameCount() |
|
95 |
{ |
|
96 |
if (m_frameBufferCache.isEmpty() && m_reader) { |
|
97 |
if (m_reader->supportsAnimation()) { |
|
98 |
int imageCount = m_reader->imageCount(); |
|
99 |
||
100 |
// Fixup for Qt decoders... imageCount() is wrong |
|
101 |
// and jumpToNextImage does not work either... so |
|
102 |
// we will have to parse everything... |
|
103 |
if (imageCount == 0) |
|
104 |
forceLoadEverything(); |
|
105 |
else |
|
106 |
m_frameBufferCache.resize(imageCount); |
|
107 |
} else { |
|
108 |
m_frameBufferCache.resize(1); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
return m_frameBufferCache.size(); |
|
113 |
} |
|
114 |
||
115 |
int ImageDecoderQt::repetitionCount() const |
|
116 |
{ |
|
117 |
if (m_reader && m_reader->supportsAnimation()) |
|
118 |
m_repetitionCount = qMax(0, m_reader->loopCount()); |
|
119 |
||
120 |
return m_repetitionCount; |
|
121 |
} |
|
122 |
||
123 |
String ImageDecoderQt::filenameExtension() const |
|
124 |
{ |
|
125 |
return String(m_format.constData(), m_format.length()); |
|
126 |
}; |
|
127 |
||
128 |
RGBA32Buffer* ImageDecoderQt::frameBufferAtIndex(size_t index) |
|
129 |
{ |
|
130 |
// In case the ImageDecoderQt got recreated we don't know |
|
131 |
// yet how many images we are going to have and need to |
|
132 |
// find that out now. |
|
133 |
int count = m_frameBufferCache.size(); |
|
134 |
if (!m_failed && count == 0) { |
|
135 |
internalDecodeSize(); |
|
136 |
count = frameCount(); |
|
137 |
} |
|
138 |
||
139 |
if (index >= static_cast<size_t>(count)) |
|
140 |
return 0; |
|
141 |
||
142 |
RGBA32Buffer& frame = m_frameBufferCache[index]; |
|
143 |
if (frame.status() != RGBA32Buffer::FrameComplete && m_reader) |
|
144 |
internalReadImage(index); |
|
145 |
return &frame; |
|
146 |
} |
|
147 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
void ImageDecoderQt::clearFrameBufferCache(size_t /*index*/) |
0 | 149 |
{ |
150 |
} |
|
151 |
||
152 |
void ImageDecoderQt::internalDecodeSize() |
|
153 |
{ |
|
154 |
ASSERT(m_reader); |
|
155 |
||
156 |
// If we have a QSize() something failed |
|
157 |
QSize size = m_reader->size(); |
|
158 |
if (size.isEmpty()) |
|
159 |
return failRead(); |
|
160 |
||
161 |
m_format = m_reader->format(); |
|
162 |
setSize(size.width(), size.height()); |
|
163 |
} |
|
164 |
||
165 |
void ImageDecoderQt::internalReadImage(size_t frameIndex) |
|
166 |
{ |
|
167 |
ASSERT(m_reader); |
|
168 |
||
169 |
if (m_reader->supportsAnimation()) |
|
170 |
m_reader->jumpToImage(frameIndex); |
|
171 |
else if (frameIndex != 0) |
|
172 |
return failRead(); |
|
173 |
||
174 |
internalHandleCurrentImage(frameIndex); |
|
175 |
||
176 |
// Attempt to return some memory |
|
177 |
for (int i = 0; i < m_frameBufferCache.size(); ++i) |
|
178 |
if (m_frameBufferCache[i].status() != RGBA32Buffer::FrameComplete) |
|
179 |
return; |
|
180 |
||
181 |
delete m_reader; |
|
182 |
delete m_buffer; |
|
183 |
m_buffer = 0; |
|
184 |
m_reader = 0; |
|
185 |
} |
|
186 |
||
187 |
void ImageDecoderQt::internalHandleCurrentImage(size_t frameIndex) |
|
188 |
{ |
|
189 |
// Now get the QImage from Qt and place it in the RGBA32Buffer |
|
190 |
QImage img; |
|
191 |
if (!m_reader->read(&img)) |
|
192 |
return failRead(); |
|
193 |
||
194 |
// now into the RGBA32Buffer - even if the image is not |
|
195 |
QSize imageSize = img.size(); |
|
196 |
RGBA32Buffer* const buffer = &m_frameBufferCache[frameIndex]; |
|
197 |
buffer->setRect(m_reader->currentImageRect()); |
|
198 |
buffer->setStatus(RGBA32Buffer::FrameComplete); |
|
199 |
buffer->setDuration(m_reader->nextImageDelay()); |
|
200 |
buffer->setDecodedImage(img); |
|
201 |
} |
|
202 |
||
203 |
// The QImageIOHandler is not able to tell us how many frames |
|
204 |
// we have and we need to parse every image. We do this by |
|
205 |
// increasing the m_frameBufferCache by one and try to parse |
|
206 |
// the image. We stop when QImage::read fails and then need |
|
207 |
// to resize the m_frameBufferCache to the final size and update |
|
208 |
// the m_failed. In case we failed to decode the first image |
|
209 |
// we want to keep m_failed set to true. |
|
210 |
||
211 |
// TODO: Do not increment the m_frameBufferCache.size() by one but more than one |
|
212 |
void ImageDecoderQt::forceLoadEverything() |
|
213 |
{ |
|
214 |
int imageCount = 0; |
|
215 |
||
216 |
do { |
|
217 |
m_frameBufferCache.resize(++imageCount); |
|
218 |
internalHandleCurrentImage(imageCount - 1); |
|
219 |
} while(!m_failed); |
|
220 |
||
221 |
// If we failed decoding the first image we actually |
|
222 |
// have no images and need to keep m_failed set to |
|
223 |
// true otherwise we want to reset it and forget about |
|
224 |
// the last attempt to decode a image. |
|
225 |
m_frameBufferCache.resize(imageCount - 1); |
|
226 |
m_failed = imageCount == 1; |
|
227 |
} |
|
228 |
||
229 |
void ImageDecoderQt::failRead() |
|
230 |
{ |
|
231 |
setFailed(); |
|
232 |
delete m_reader; |
|
233 |
delete m_buffer; |
|
234 |
m_reader = 0; |
|
235 |
m_buffer = 0; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
// vim: ts=4 sw=4 et |