author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
child 8 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the plugins of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
** WARNING: |
|
41 |
** A separate license from Unisys may be required to use the gif |
|
42 |
** reader. See http://www.unisys.com/about__unisys/lzw/ |
|
43 |
** for information from Unisys |
|
44 |
** |
|
45 |
****************************************************************************/ |
|
46 |
||
47 |
#include "qgifhandler.h" |
|
48 |
||
49 |
#include <qimage.h> |
|
50 |
#include <qiodevice.h> |
|
51 |
#include <qvariant.h> |
|
52 |
||
53 |
QT_BEGIN_NAMESPACE |
|
54 |
||
55 |
#define Q_TRANSPARENT 0x00ffffff |
|
56 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
57 |
// avoid going through QImage::scanLine() which calls detach |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
58 |
#define FAST_SCAN_LINE(bits, bpl, y) (bits + (y) * bpl) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
59 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
60 |
|
0 | 61 |
/* |
62 |
Incremental image decoder for GIF image format. |
|
63 |
||
64 |
This subclass of QImageFormat decodes GIF format images, |
|
65 |
including animated GIFs. Internally in |
|
66 |
*/ |
|
67 |
||
68 |
class QGIFFormat { |
|
69 |
public: |
|
70 |
QGIFFormat(); |
|
71 |
~QGIFFormat(); |
|
72 |
||
73 |
int decode(QImage *image, const uchar* buffer, int length, |
|
74 |
int *nextFrameDelay, int *loopCount, QSize *nextSize); |
|
75 |
||
76 |
bool newFrame; |
|
77 |
bool partialNewFrame; |
|
78 |
||
79 |
private: |
|
80 |
void fillRect(QImage *image, int x, int y, int w, int h, QRgb col); |
|
81 |
inline QRgb color(uchar index) const; |
|
82 |
||
83 |
// GIF specific stuff |
|
84 |
QRgb* globalcmap; |
|
85 |
QRgb* localcmap; |
|
86 |
QImage backingstore; |
|
87 |
unsigned char hold[16]; |
|
88 |
bool gif89; |
|
89 |
int count; |
|
90 |
int ccount; |
|
91 |
int expectcount; |
|
92 |
enum State { |
|
93 |
Header, |
|
94 |
LogicalScreenDescriptor, |
|
95 |
GlobalColorMap, |
|
96 |
LocalColorMap, |
|
97 |
Introducer, |
|
98 |
ImageDescriptor, |
|
99 |
TableImageLZWSize, |
|
100 |
ImageDataBlockSize, |
|
101 |
ImageDataBlock, |
|
102 |
ExtensionLabel, |
|
103 |
GraphicControlExtension, |
|
104 |
ApplicationExtension, |
|
105 |
NetscapeExtensionBlockSize, |
|
106 |
NetscapeExtensionBlock, |
|
107 |
SkipBlockSize, |
|
108 |
SkipBlock, |
|
109 |
Done, |
|
110 |
Error |
|
111 |
} state; |
|
112 |
int gncols; |
|
113 |
int lncols; |
|
114 |
int ncols; |
|
115 |
int lzwsize; |
|
116 |
bool lcmap; |
|
117 |
int swidth, sheight; |
|
118 |
int width, height; |
|
119 |
int left, top, right, bottom; |
|
120 |
enum Disposal { NoDisposal, DoNotChange, RestoreBackground, RestoreImage }; |
|
121 |
Disposal disposal; |
|
122 |
bool disposed; |
|
123 |
int trans_index; |
|
124 |
bool gcmap; |
|
125 |
int bgcol; |
|
126 |
int interlace; |
|
127 |
int accum; |
|
128 |
int bitcount; |
|
129 |
||
130 |
enum { max_lzw_bits=12 }; // (poor-compiler's static const int) |
|
131 |
||
132 |
int code_size, clear_code, end_code, max_code_size, max_code; |
|
133 |
int firstcode, oldcode, incode; |
|
134 |
short table[2][1<< max_lzw_bits]; |
|
135 |
short stack[(1<<(max_lzw_bits))*2]; |
|
136 |
short *sp; |
|
137 |
bool needfirst; |
|
138 |
int x, y; |
|
139 |
int frame; |
|
140 |
bool out_of_bounds; |
|
141 |
bool digress; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
142 |
void nextY(unsigned char *bits, int bpl); |
0 | 143 |
void disposePrevious(QImage *image); |
144 |
}; |
|
145 |
||
146 |
/*! |
|
147 |
Constructs a QGIFFormat. |
|
148 |
*/ |
|
149 |
QGIFFormat::QGIFFormat() |
|
150 |
{ |
|
151 |
globalcmap = 0; |
|
152 |
localcmap = 0; |
|
153 |
lncols = 0; |
|
154 |
gncols = 0; |
|
155 |
disposal = NoDisposal; |
|
156 |
out_of_bounds = false; |
|
157 |
disposed = true; |
|
158 |
frame = -1; |
|
159 |
state = Header; |
|
160 |
count = 0; |
|
161 |
lcmap = false; |
|
162 |
newFrame = false; |
|
163 |
partialNewFrame = false; |
|
164 |
} |
|
165 |
||
166 |
/*! |
|
167 |
Destroys a QGIFFormat. |
|
168 |
*/ |
|
169 |
QGIFFormat::~QGIFFormat() |
|
170 |
{ |
|
171 |
if (globalcmap) delete[] globalcmap; |
|
172 |
if (localcmap) delete[] localcmap; |
|
173 |
} |
|
174 |
||
175 |
void QGIFFormat::disposePrevious(QImage *image) |
|
176 |
{ |
|
177 |
if (out_of_bounds) { |
|
178 |
// flush anything that survived |
|
179 |
// ### Changed: QRect(0, 0, swidth, sheight) |
|
180 |
} |
|
181 |
||
182 |
// Handle disposal of previous image before processing next one |
|
183 |
||
184 |
if (disposed) return; |
|
185 |
||
186 |
int l = qMin(swidth-1,left); |
|
187 |
int r = qMin(swidth-1,right); |
|
188 |
int t = qMin(sheight-1,top); |
|
189 |
int b = qMin(sheight-1,bottom); |
|
190 |
||
191 |
switch (disposal) { |
|
192 |
case NoDisposal: |
|
193 |
break; |
|
194 |
case DoNotChange: |
|
195 |
break; |
|
196 |
case RestoreBackground: |
|
197 |
if (trans_index>=0) { |
|
198 |
// Easy: we use the transparent color |
|
199 |
fillRect(image, l, t, r-l+1, b-t+1, Q_TRANSPARENT); |
|
200 |
} else if (bgcol>=0) { |
|
201 |
// Easy: we use the bgcol given |
|
202 |
fillRect(image, l, t, r-l+1, b-t+1, color(bgcol)); |
|
203 |
} else { |
|
204 |
// Impossible: We don't know of a bgcol - use pixel 0 |
|
205 |
QRgb *bits = (QRgb*)image->bits(); |
|
206 |
fillRect(image, l, t, r-l+1, b-t+1, bits[0]); |
|
207 |
} |
|
208 |
// ### Changed: QRect(l, t, r-l+1, b-t+1) |
|
209 |
break; |
|
210 |
case RestoreImage: { |
|
211 |
if (frame >= 0) { |
|
212 |
for (int ln=t; ln<=b; ln++) { |
|
213 |
memcpy(image->scanLine(ln)+l, |
|
214 |
backingstore.scanLine(ln-t), |
|
215 |
(r-l+1)*sizeof(QRgb)); |
|
216 |
} |
|
217 |
// ### Changed: QRect(l, t, r-l+1, b-t+1) |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
disposal = NoDisposal; // Until an extension says otherwise. |
|
222 |
||
223 |
disposed = true; |
|
224 |
} |
|
225 |
||
226 |
/*! |
|
227 |
This function decodes some data into image changes. |
|
228 |
||
229 |
Returns the number of bytes consumed. |
|
230 |
*/ |
|
231 |
int QGIFFormat::decode(QImage *image, const uchar *buffer, int length, |
|
232 |
int *nextFrameDelay, int *loopCount, QSize *nextSize) |
|
233 |
{ |
|
234 |
// We are required to state that |
|
235 |
// "The Graphics Interchange Format(c) is the Copyright property of |
|
236 |
// CompuServe Incorporated. GIF(sm) is a Service Mark property of |
|
237 |
// CompuServe Incorporated." |
|
238 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
239 |
image->detach(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
240 |
int bpl = image->bytesPerLine(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
241 |
unsigned char *bits = image->bits(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
242 |
|
0 | 243 |
#define LM(l, m) (((m)<<8)|l) |
244 |
digress = false; |
|
245 |
const int initial = length; |
|
246 |
while (!digress && length) { |
|
247 |
length--; |
|
248 |
unsigned char ch=*buffer++; |
|
249 |
switch (state) { |
|
250 |
case Header: |
|
251 |
hold[count++]=ch; |
|
252 |
if (count==6) { |
|
253 |
// Header |
|
254 |
gif89=(hold[3]!='8' || hold[4]!='7'); |
|
255 |
state=LogicalScreenDescriptor; |
|
256 |
count=0; |
|
257 |
} |
|
258 |
break; |
|
259 |
case LogicalScreenDescriptor: |
|
260 |
hold[count++]=ch; |
|
261 |
if (count==7) { |
|
262 |
// Logical Screen Descriptor |
|
263 |
swidth=LM(hold[0], hold[1]); |
|
264 |
sheight=LM(hold[2], hold[3]); |
|
265 |
gcmap=!!(hold[4]&0x80); |
|
266 |
//UNUSED: bpchan=(((hold[4]&0x70)>>3)+1); |
|
267 |
//UNUSED: gcmsortflag=!!(hold[4]&0x08); |
|
268 |
gncols=2<<(hold[4]&0x7); |
|
269 |
bgcol=(gcmap) ? hold[5] : -1; |
|
270 |
//aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0; |
|
271 |
||
272 |
trans_index = -1; |
|
273 |
count=0; |
|
274 |
ncols=gncols; |
|
275 |
if (gcmap) { |
|
276 |
ccount=0; |
|
277 |
state=GlobalColorMap; |
|
278 |
globalcmap = new QRgb[gncols+1]; // +1 for trans_index |
|
279 |
globalcmap[gncols] = Q_TRANSPARENT; |
|
280 |
} else { |
|
281 |
state=Introducer; |
|
282 |
} |
|
283 |
} |
|
284 |
break; |
|
285 |
case GlobalColorMap: case LocalColorMap: |
|
286 |
hold[count++]=ch; |
|
287 |
if (count==3) { |
|
288 |
QRgb rgb = qRgb(hold[0], hold[1], hold[2]); |
|
289 |
if (state == LocalColorMap) { |
|
290 |
if (ccount < lncols) |
|
291 |
localcmap[ccount] = rgb; |
|
292 |
} else { |
|
293 |
globalcmap[ccount] = rgb; |
|
294 |
} |
|
295 |
if (++ccount >= ncols) { |
|
296 |
if (state == LocalColorMap) |
|
297 |
state=TableImageLZWSize; |
|
298 |
else |
|
299 |
state=Introducer; |
|
300 |
} |
|
301 |
count=0; |
|
302 |
} |
|
303 |
break; |
|
304 |
case Introducer: |
|
305 |
hold[count++]=ch; |
|
306 |
switch (ch) { |
|
307 |
case ',': |
|
308 |
state=ImageDescriptor; |
|
309 |
break; |
|
310 |
case '!': |
|
311 |
state=ExtensionLabel; |
|
312 |
break; |
|
313 |
case ';': |
|
314 |
// ### Changed: QRect(0, 0, swidth, sheight) |
|
315 |
state=Done; |
|
316 |
break; |
|
317 |
default: |
|
318 |
digress=true; |
|
319 |
// Unexpected Introducer - ignore block |
|
320 |
state=Error; |
|
321 |
} |
|
322 |
break; |
|
323 |
case ImageDescriptor: |
|
324 |
hold[count++]=ch; |
|
325 |
if (count==10) { |
|
326 |
int newleft=LM(hold[1], hold[2]); |
|
327 |
int newtop=LM(hold[3], hold[4]); |
|
328 |
int newwidth=LM(hold[5], hold[6]); |
|
329 |
int newheight=LM(hold[7], hold[8]); |
|
330 |
||
331 |
// disbelieve ridiculous logical screen sizes, |
|
332 |
// unless the image frames are also large. |
|
333 |
if (swidth/10 > qMax(newwidth,200)) |
|
334 |
swidth = -1; |
|
335 |
if (sheight/10 > qMax(newheight,200)) |
|
336 |
sheight = -1; |
|
337 |
||
338 |
if (swidth <= 0) |
|
339 |
swidth = newleft + newwidth; |
|
340 |
if (sheight <= 0) |
|
341 |
sheight = newtop + newheight; |
|
342 |
||
343 |
QImage::Format format = trans_index >= 0 ? QImage::Format_ARGB32 : QImage::Format_RGB32; |
|
344 |
if (image->isNull()) { |
|
345 |
(*image) = QImage(swidth, sheight, format); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
346 |
bpl = image->bytesPerLine(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
347 |
bits = image->bits(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
348 |
memset(bits, 0, image->byteCount()); |
0 | 349 |
|
350 |
// ### size of the upcoming frame, should rather |
|
351 |
// be known before decoding it. |
|
352 |
*nextSize = QSize(swidth, sheight); |
|
353 |
} |
|
354 |
||
355 |
disposePrevious(image); |
|
356 |
disposed = false; |
|
357 |
||
358 |
left = newleft; |
|
359 |
top = newtop; |
|
360 |
width = newwidth; |
|
361 |
height = newheight; |
|
362 |
||
363 |
right=qMax(0, qMin(left+width, swidth)-1); |
|
364 |
bottom=qMax(0, qMin(top+height, sheight)-1); |
|
365 |
lcmap=!!(hold[9]&0x80); |
|
366 |
interlace=!!(hold[9]&0x40); |
|
367 |
//bool lcmsortflag=!!(hold[9]&0x20); |
|
368 |
lncols=lcmap ? (2<<(hold[9]&0x7)) : 0; |
|
369 |
if (lncols) { |
|
370 |
if (localcmap) |
|
371 |
delete [] localcmap; |
|
372 |
localcmap = new QRgb[lncols+1]; |
|
373 |
localcmap[lncols] = Q_TRANSPARENT; |
|
374 |
ncols = lncols; |
|
375 |
} else { |
|
376 |
ncols = gncols; |
|
377 |
} |
|
378 |
frame++; |
|
379 |
if (frame == 0) { |
|
380 |
if (left || top || width<swidth || height<sheight) { |
|
381 |
// Not full-size image - erase with bg or transparent |
|
382 |
if (trans_index >= 0) { |
|
383 |
fillRect(image, 0, 0, swidth, sheight, color(trans_index)); |
|
384 |
// ### Changed: QRect(0, 0, swidth, sheight) |
|
385 |
} else if (bgcol>=0) { |
|
386 |
fillRect(image, 0, 0, swidth, sheight, color(bgcol)); |
|
387 |
// ### Changed: QRect(0, 0, swidth, sheight) |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
if (disposal == RestoreImage) { |
|
393 |
int l = qMin(swidth-1,left); |
|
394 |
int r = qMin(swidth-1,right); |
|
395 |
int t = qMin(sheight-1,top); |
|
396 |
int b = qMin(sheight-1,bottom); |
|
397 |
int w = r-l+1; |
|
398 |
int h = b-t+1; |
|
399 |
||
400 |
if (backingstore.width() < w |
|
401 |
|| backingstore.height() < h) { |
|
402 |
// We just use the backing store as a byte array |
|
403 |
backingstore = QImage(qMax(backingstore.width(), w), |
|
404 |
qMax(backingstore.height(), h), |
|
405 |
QImage::Format_RGB32); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
406 |
memset(bits, 0, image->byteCount()); |
0 | 407 |
} |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
408 |
const int dest_bpl = backingstore.bytesPerLine(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
409 |
unsigned char *dest_data = backingstore.bits(); |
0 | 410 |
for (int ln=0; ln<h; ln++) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
411 |
memcpy(FAST_SCAN_LINE(dest_data, dest_bpl, ln), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
412 |
FAST_SCAN_LINE(bits, bpl, t+ln) + l, w*sizeof(QRgb)); |
0 | 413 |
} |
414 |
} |
|
415 |
||
416 |
count=0; |
|
417 |
if (lcmap) { |
|
418 |
ccount=0; |
|
419 |
state=LocalColorMap; |
|
420 |
} else { |
|
421 |
state=TableImageLZWSize; |
|
422 |
} |
|
423 |
x = left; |
|
424 |
y = top; |
|
425 |
accum = 0; |
|
426 |
bitcount = 0; |
|
427 |
sp = stack; |
|
428 |
firstcode = oldcode = 0; |
|
429 |
needfirst = true; |
|
430 |
out_of_bounds = left>=swidth || y>=sheight; |
|
431 |
} |
|
432 |
break; |
|
433 |
case TableImageLZWSize: { |
|
434 |
lzwsize=ch; |
|
435 |
if (lzwsize > max_lzw_bits) { |
|
436 |
state=Error; |
|
437 |
} else { |
|
438 |
code_size=lzwsize+1; |
|
439 |
clear_code=1<<lzwsize; |
|
440 |
end_code=clear_code+1; |
|
441 |
max_code_size=2*clear_code; |
|
442 |
max_code=clear_code+2; |
|
443 |
int i; |
|
444 |
for (i=0; i<clear_code; i++) { |
|
445 |
table[0][i]=0; |
|
446 |
table[1][i]=i; |
|
447 |
} |
|
448 |
state=ImageDataBlockSize; |
|
449 |
} |
|
450 |
count=0; |
|
451 |
break; |
|
452 |
} case ImageDataBlockSize: |
|
453 |
expectcount=ch; |
|
454 |
if (expectcount) { |
|
455 |
state=ImageDataBlock; |
|
456 |
} else { |
|
457 |
state=Introducer; |
|
458 |
digress = true; |
|
459 |
newFrame = true; |
|
460 |
} |
|
461 |
break; |
|
462 |
case ImageDataBlock: |
|
463 |
count++; |
|
464 |
accum|=(ch<<bitcount); |
|
465 |
bitcount+=8; |
|
466 |
while (bitcount>=code_size && state==ImageDataBlock) { |
|
467 |
int code=accum&((1<<code_size)-1); |
|
468 |
bitcount-=code_size; |
|
469 |
accum>>=code_size; |
|
470 |
||
471 |
if (code==clear_code) { |
|
472 |
if (!needfirst) { |
|
473 |
code_size=lzwsize+1; |
|
474 |
max_code_size=2*clear_code; |
|
475 |
max_code=clear_code+2; |
|
476 |
} |
|
477 |
needfirst=true; |
|
478 |
} else if (code==end_code) { |
|
479 |
bitcount = -32768; |
|
480 |
// Left the block end arrive |
|
481 |
} else { |
|
482 |
if (needfirst) { |
|
483 |
firstcode=oldcode=code; |
|
484 |
if (!out_of_bounds && image->height() > y && firstcode!=trans_index) |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
485 |
((QRgb*)FAST_SCAN_LINE(bits, bpl, y))[x] = color(firstcode); |
0 | 486 |
x++; |
487 |
if (x>=swidth) out_of_bounds = true; |
|
488 |
needfirst=false; |
|
489 |
if (x>=left+width) { |
|
490 |
x=left; |
|
491 |
out_of_bounds = left>=swidth || y>=sheight; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
492 |
nextY(bits, bpl); |
0 | 493 |
} |
494 |
} else { |
|
495 |
incode=code; |
|
496 |
if (code>=max_code) { |
|
497 |
*sp++=firstcode; |
|
498 |
code=oldcode; |
|
499 |
} |
|
500 |
while (code>=clear_code+2) { |
|
501 |
*sp++=table[1][code]; |
|
502 |
if (code==table[0][code]) { |
|
503 |
state=Error; |
|
504 |
break; |
|
505 |
} |
|
506 |
if (sp-stack>=(1<<(max_lzw_bits))*2) { |
|
507 |
state=Error; |
|
508 |
break; |
|
509 |
} |
|
510 |
code=table[0][code]; |
|
511 |
} |
|
512 |
*sp++=firstcode=table[1][code]; |
|
513 |
code=max_code; |
|
514 |
if (code<(1<<max_lzw_bits)) { |
|
515 |
table[0][code]=oldcode; |
|
516 |
table[1][code]=firstcode; |
|
517 |
max_code++; |
|
518 |
if ((max_code>=max_code_size) |
|
519 |
&& (max_code_size<(1<<max_lzw_bits))) |
|
520 |
{ |
|
521 |
max_code_size*=2; |
|
522 |
code_size++; |
|
523 |
} |
|
524 |
} |
|
525 |
oldcode=incode; |
|
526 |
const int h = image->height(); |
|
527 |
const QRgb *map = lcmap ? localcmap : globalcmap; |
|
528 |
QRgb *line = 0; |
|
529 |
if (!out_of_bounds && h > y) |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
530 |
line = (QRgb*)FAST_SCAN_LINE(bits, bpl, y); |
0 | 531 |
while (sp>stack) { |
532 |
const uchar index = *(--sp); |
|
533 |
if (!out_of_bounds && h > y && index!=trans_index) { |
|
534 |
if (index > ncols) |
|
535 |
line[x] = Q_TRANSPARENT; |
|
536 |
else |
|
537 |
line[x] = map ? map[index] : 0; |
|
538 |
} |
|
539 |
x++; |
|
540 |
if (x>=swidth) out_of_bounds = true; |
|
541 |
if (x>=left+width) { |
|
542 |
x=left; |
|
543 |
out_of_bounds = left>=swidth || y>=sheight; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
544 |
nextY(bits, bpl); |
0 | 545 |
if (!out_of_bounds && h > y) |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
546 |
line = (QRgb*)FAST_SCAN_LINE(bits, bpl, y); |
0 | 547 |
} |
548 |
} |
|
549 |
} |
|
550 |
} |
|
551 |
} |
|
552 |
partialNewFrame = true; |
|
553 |
if (count==expectcount) { |
|
554 |
count=0; |
|
555 |
state=ImageDataBlockSize; |
|
556 |
} |
|
557 |
break; |
|
558 |
case ExtensionLabel: |
|
559 |
switch (ch) { |
|
560 |
case 0xf9: |
|
561 |
state=GraphicControlExtension; |
|
562 |
break; |
|
563 |
case 0xff: |
|
564 |
state=ApplicationExtension; |
|
565 |
break; |
|
566 |
#if 0 |
|
567 |
case 0xfe: |
|
568 |
state=CommentExtension; |
|
569 |
break; |
|
570 |
case 0x01: |
|
571 |
break; |
|
572 |
#endif |
|
573 |
default: |
|
574 |
state=SkipBlockSize; |
|
575 |
} |
|
576 |
count=0; |
|
577 |
break; |
|
578 |
case ApplicationExtension: |
|
579 |
if (count<11) hold[count]=ch; |
|
580 |
count++; |
|
581 |
if (count==hold[0]+1) { |
|
582 |
if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) { |
|
583 |
// Looping extension |
|
584 |
state=NetscapeExtensionBlockSize; |
|
585 |
} else { |
|
586 |
state=SkipBlockSize; |
|
587 |
} |
|
588 |
count=0; |
|
589 |
} |
|
590 |
break; |
|
591 |
case NetscapeExtensionBlockSize: |
|
592 |
expectcount=ch; |
|
593 |
count=0; |
|
594 |
if (expectcount) state=NetscapeExtensionBlock; |
|
595 |
else state=Introducer; |
|
596 |
break; |
|
597 |
case NetscapeExtensionBlock: |
|
598 |
if (count<3) hold[count]=ch; |
|
599 |
count++; |
|
600 |
if (count==expectcount) { |
|
601 |
*loopCount = hold[1]+hold[2]*256; |
|
602 |
state=SkipBlockSize; // Ignore further blocks |
|
603 |
} |
|
604 |
break; |
|
605 |
case GraphicControlExtension: |
|
606 |
if (count<5) hold[count]=ch; |
|
607 |
count++; |
|
608 |
if (count==hold[0]+1) { |
|
609 |
disposePrevious(image); |
|
610 |
disposal=Disposal((hold[1]>>2)&0x7); |
|
611 |
//UNUSED: waitforuser=!!((hold[1]>>1)&0x1); |
|
612 |
int delay=count>3 ? LM(hold[2], hold[3]) : 1; |
|
613 |
// IE and mozilla use a minimum delay of 10. With the minimum delay of 10 |
|
614 |
// we are compatible to them and avoid huge loads on the app and xserver. |
|
615 |
*nextFrameDelay = (delay < 2 ? 10 : delay) * 10; |
|
616 |
||
617 |
bool havetrans=hold[1]&0x1; |
|
618 |
trans_index = havetrans ? hold[4] : -1; |
|
619 |
||
620 |
count=0; |
|
621 |
state=SkipBlockSize; |
|
622 |
} |
|
623 |
break; |
|
624 |
case SkipBlockSize: |
|
625 |
expectcount=ch; |
|
626 |
count=0; |
|
627 |
if (expectcount) state=SkipBlock; |
|
628 |
else state=Introducer; |
|
629 |
break; |
|
630 |
case SkipBlock: |
|
631 |
count++; |
|
632 |
if (count==expectcount) state=SkipBlockSize; |
|
633 |
break; |
|
634 |
case Done: |
|
635 |
digress=true; |
|
636 |
/* Netscape ignores the junk, so we do too. |
|
637 |
length++; // Unget |
|
638 |
state=Error; // More calls to this is an error |
|
639 |
*/ |
|
640 |
break; |
|
641 |
case Error: |
|
642 |
return -1; // Called again after done. |
|
643 |
} |
|
644 |
} |
|
645 |
return initial-length; |
|
646 |
} |
|
647 |
||
648 |
void QGIFFormat::fillRect(QImage *image, int col, int row, int w, int h, QRgb color) |
|
649 |
{ |
|
650 |
if (w>0) { |
|
651 |
for (int j=0; j<h; j++) { |
|
652 |
QRgb *line = (QRgb*)image->scanLine(j+row); |
|
653 |
for (int i=0; i<w; i++) |
|
654 |
*(line+col+i) = color; |
|
655 |
} |
|
656 |
} |
|
657 |
} |
|
658 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
659 |
void QGIFFormat::nextY(unsigned char *bits, int bpl) |
0 | 660 |
{ |
661 |
int my; |
|
662 |
switch (interlace) { |
|
663 |
case 0: // Non-interlaced |
|
664 |
// if (!out_of_bounds) { |
|
665 |
// ### Changed: QRect(left, y, right - left + 1, 1); |
|
666 |
// } |
|
667 |
y++; |
|
668 |
break; |
|
669 |
case 1: { |
|
670 |
int i; |
|
671 |
my = qMin(7, bottom-y); |
|
672 |
// Don't dup with transparency |
|
673 |
if (trans_index < 0) { |
|
674 |
for (i=1; i<=my; i++) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
675 |
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb), |
0 | 676 |
(right-left+1)*sizeof(QRgb)); |
677 |
} |
|
678 |
} |
|
679 |
||
680 |
// if (!out_of_bounds) { |
|
681 |
// ### Changed: QRect(left, y, right - left + 1, my + 1); |
|
682 |
// } |
|
683 |
// if (!out_of_bounds) |
|
684 |
// qDebug("consumer->changed(QRect(%d, %d, %d, %d))", left, y, right-left+1, my+1); |
|
685 |
y+=8; |
|
686 |
if (y>bottom) { |
|
687 |
interlace++; y=top+4; |
|
688 |
if (y > bottom) { // for really broken GIFs with bottom < 5 |
|
689 |
interlace=2; |
|
690 |
y = top + 2; |
|
691 |
if (y > bottom) { // for really broken GIF with bottom < 3 |
|
692 |
interlace = 0; |
|
693 |
y = top + 1; |
|
694 |
} |
|
695 |
} |
|
696 |
} |
|
697 |
} break; |
|
698 |
case 2: { |
|
699 |
int i; |
|
700 |
my = qMin(3, bottom-y); |
|
701 |
// Don't dup with transparency |
|
702 |
if (trans_index < 0) { |
|
703 |
for (i=1; i<=my; i++) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
704 |
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb), |
0 | 705 |
(right-left+1)*sizeof(QRgb)); |
706 |
} |
|
707 |
} |
|
708 |
||
709 |
// if (!out_of_bounds) { |
|
710 |
// ### Changed: QRect(left, y, right - left + 1, my + 1); |
|
711 |
// } |
|
712 |
y+=8; |
|
713 |
if (y>bottom) { |
|
714 |
interlace++; y=top+2; |
|
715 |
// handle broken GIF with bottom < 3 |
|
716 |
if (y > bottom) { |
|
717 |
interlace = 3; |
|
718 |
y = top + 1; |
|
719 |
} |
|
720 |
} |
|
721 |
} break; |
|
722 |
case 3: { |
|
723 |
int i; |
|
724 |
my = qMin(1, bottom-y); |
|
725 |
// Don't dup with transparency |
|
726 |
if (trans_index < 0) { |
|
727 |
for (i=1; i<=my; i++) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
728 |
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb), |
0 | 729 |
(right-left+1)*sizeof(QRgb)); |
730 |
} |
|
731 |
} |
|
732 |
// if (!out_of_bounds) { |
|
733 |
// ### Changed: QRect(left, y, right - left + 1, my + 1); |
|
734 |
// } |
|
735 |
y+=4; |
|
736 |
if (y>bottom) { interlace++; y=top+1; } |
|
737 |
} break; |
|
738 |
case 4: |
|
739 |
// if (!out_of_bounds) { |
|
740 |
// ### Changed: QRect(left, y, right - left + 1, 1); |
|
741 |
// } |
|
742 |
y+=2; |
|
743 |
} |
|
744 |
||
745 |
// Consume bogus extra lines |
|
746 |
if (y >= sheight) out_of_bounds=true; //y=bottom; |
|
747 |
} |
|
748 |
||
749 |
inline QRgb QGIFFormat::color(uchar index) const |
|
750 |
{ |
|
751 |
if (index == trans_index || index > ncols) |
|
752 |
return Q_TRANSPARENT; |
|
753 |
||
754 |
QRgb *map = lcmap ? localcmap : globalcmap; |
|
755 |
return map ? map[index] : 0; |
|
756 |
} |
|
757 |
||
758 |
//------------------------------------------------------------------------- |
|
759 |
//------------------------------------------------------------------------- |
|
760 |
//------------------------------------------------------------------------- |
|
761 |
||
762 |
QGifHandler::QGifHandler() |
|
763 |
{ |
|
764 |
gifFormat = new QGIFFormat; |
|
765 |
nextDelay = 0; |
|
766 |
loopCnt = 0; |
|
767 |
frameNumber = -1; |
|
768 |
nextSize = QSize(); |
|
769 |
} |
|
770 |
||
771 |
QGifHandler::~QGifHandler() |
|
772 |
{ |
|
773 |
delete gifFormat; |
|
774 |
} |
|
775 |
||
776 |
// Does partial decode if necessary, just to see if an image is coming |
|
777 |
||
778 |
bool QGifHandler::imageIsComing() const |
|
779 |
{ |
|
780 |
const int GifChunkSize = 4096; |
|
781 |
||
782 |
while (!gifFormat->partialNewFrame) { |
|
783 |
if (buffer.isEmpty()) { |
|
784 |
buffer += device()->read(GifChunkSize); |
|
785 |
if (buffer.isEmpty()) |
|
786 |
break; |
|
787 |
} |
|
788 |
||
789 |
int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(), |
|
790 |
&nextDelay, &loopCnt, &nextSize); |
|
791 |
if (decoded == -1) |
|
792 |
break; |
|
793 |
buffer.remove(0, decoded); |
|
794 |
} |
|
795 |
return gifFormat->partialNewFrame; |
|
796 |
} |
|
797 |
||
798 |
bool QGifHandler::canRead() const |
|
799 |
{ |
|
800 |
if (!nextDelay && canRead(device())) { |
|
801 |
setFormat("gif"); |
|
802 |
return true; |
|
803 |
} |
|
804 |
||
805 |
return imageIsComing(); |
|
806 |
} |
|
807 |
||
808 |
bool QGifHandler::canRead(QIODevice *device) |
|
809 |
{ |
|
810 |
if (!device) { |
|
811 |
qWarning("QGifHandler::canRead() called with no device"); |
|
812 |
return false; |
|
813 |
} |
|
814 |
||
815 |
char head[6]; |
|
816 |
if (device->peek(head, sizeof(head)) == sizeof(head)) |
|
817 |
return qstrncmp(head, "GIF87a", 6) == 0 |
|
818 |
|| qstrncmp(head, "GIF89a", 6) == 0; |
|
819 |
return false; |
|
820 |
} |
|
821 |
||
822 |
bool QGifHandler::read(QImage *image) |
|
823 |
{ |
|
824 |
const int GifChunkSize = 4096; |
|
825 |
||
826 |
while (!gifFormat->newFrame) { |
|
827 |
if (buffer.isEmpty()) { |
|
828 |
buffer += device()->read(GifChunkSize); |
|
829 |
if (buffer.isEmpty()) |
|
830 |
break; |
|
831 |
} |
|
832 |
||
833 |
int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(), |
|
834 |
&nextDelay, &loopCnt, &nextSize); |
|
835 |
if (decoded == -1) |
|
836 |
break; |
|
837 |
buffer.remove(0, decoded); |
|
838 |
} |
|
839 |
if (gifFormat->newFrame || (gifFormat->partialNewFrame && device()->atEnd())) { |
|
840 |
*image = lastImage; |
|
841 |
++frameNumber; |
|
842 |
gifFormat->newFrame = false; |
|
843 |
gifFormat->partialNewFrame = false; |
|
844 |
return true; |
|
845 |
} |
|
846 |
||
847 |
return false; |
|
848 |
} |
|
849 |
||
850 |
bool QGifHandler::write(const QImage &image) |
|
851 |
{ |
|
852 |
Q_UNUSED(image); |
|
853 |
return false; |
|
854 |
} |
|
855 |
||
856 |
bool QGifHandler::supportsOption(ImageOption option) const |
|
857 |
{ |
|
858 |
return option == Size |
|
859 |
|| option == Animation; |
|
860 |
} |
|
861 |
||
862 |
QVariant QGifHandler::option(ImageOption option) const |
|
863 |
{ |
|
864 |
if (option == Size) { |
|
865 |
if (imageIsComing()) |
|
866 |
return nextSize; |
|
867 |
} else if (option == Animation) { |
|
868 |
return true; |
|
869 |
} |
|
870 |
return QVariant(); |
|
871 |
} |
|
872 |
||
873 |
void QGifHandler::setOption(ImageOption option, const QVariant &value) |
|
874 |
{ |
|
875 |
Q_UNUSED(option); |
|
876 |
Q_UNUSED(value); |
|
877 |
} |
|
878 |
||
879 |
int QGifHandler::nextImageDelay() const |
|
880 |
{ |
|
881 |
return nextDelay; |
|
882 |
} |
|
883 |
||
884 |
int QGifHandler::imageCount() const |
|
885 |
{ |
|
886 |
return 0; // Don't know |
|
887 |
} |
|
888 |
||
889 |
int QGifHandler::loopCount() const |
|
890 |
{ |
|
891 |
return loopCnt-1; // In GIF, loop count is iteration count, so subtract one |
|
892 |
} |
|
893 |
||
894 |
int QGifHandler::currentImageNumber() const |
|
895 |
{ |
|
896 |
return frameNumber; |
|
897 |
} |
|
898 |
||
899 |
QByteArray QGifHandler::name() const |
|
900 |
{ |
|
901 |
return "gif"; |
|
902 |
} |
|
903 |
||
904 |
QT_END_NAMESPACE |