1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the plugins of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
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
45
****************************************************************************/
47
#include "qgifhandler_p.h"
50
#include <qiodevice.h>
55
#define Q_TRANSPARENT 0x00ffffff
57
// avoid going through QImage::scanLine() which calls detach
58
#define FAST_SCAN_LINE(bits, bpl, y) (bits + (y) * bpl)
62
Incremental image decoder for GIF image format.
64
This subclass of QImageFormat decodes GIF format images,
65
including animated GIFs. Internally in
73
int decode(QImage *image, const uchar* buffer, int length,
74
int *nextFrameDelay, int *loopCount);
75
static void scan(QIODevice *device, QVector<QSize> *imageSizes, int *loopCount);
81
void fillRect(QImage *image, int x, int y, int w, int h, QRgb col);
82
inline QRgb color(uchar index) const;
88
unsigned char hold[16];
95
LogicalScreenDescriptor,
104
GraphicControlExtension,
105
ApplicationExtension,
106
NetscapeExtensionBlockSize,
107
NetscapeExtensionBlock,
120
int left, top, right, bottom;
121
enum Disposal { NoDisposal, DoNotChange, RestoreBackground, RestoreImage };
131
enum { max_lzw_bits=12 }; // (poor-compiler's static const int)
133
int code_size, clear_code, end_code, max_code_size, max_code;
134
int firstcode, oldcode, incode;
143
void nextY(unsigned char *bits, int bpl);
144
void disposePrevious(QImage *image);
148
Constructs a QGIFFormat.
150
QGIFFormat::QGIFFormat()
156
disposal = NoDisposal;
157
out_of_bounds = false;
164
partialNewFrame = false;
171
Destroys a QGIFFormat.
173
QGIFFormat::~QGIFFormat()
175
if (globalcmap) delete[] globalcmap;
176
if (localcmap) delete[] localcmap;
180
void QGIFFormat::disposePrevious(QImage *image)
183
// flush anything that survived
184
// ### Changed: QRect(0, 0, swidth, sheight)
187
// Handle disposal of previous image before processing next one
189
if (disposed) return;
191
int l = qMin(swidth-1,left);
192
int r = qMin(swidth-1,right);
193
int t = qMin(sheight-1,top);
194
int b = qMin(sheight-1,bottom);
201
case RestoreBackground:
202
if (trans_index>=0) {
203
// Easy: we use the transparent color
204
fillRect(image, l, t, r-l+1, b-t+1, Q_TRANSPARENT);
205
} else if (bgcol>=0) {
206
// Easy: we use the bgcol given
207
fillRect(image, l, t, r-l+1, b-t+1, color(bgcol));
209
// Impossible: We don't know of a bgcol - use pixel 0
210
QRgb *bits = (QRgb*)image->bits();
211
fillRect(image, l, t, r-l+1, b-t+1, bits[0]);
213
// ### Changed: QRect(l, t, r-l+1, b-t+1)
217
for (int ln=t; ln<=b; ln++) {
218
memcpy(image->scanLine(ln)+l,
219
backingstore.scanLine(ln-t),
220
(r-l+1)*sizeof(QRgb));
222
// ### Changed: QRect(l, t, r-l+1, b-t+1)
226
disposal = NoDisposal; // Until an extension says otherwise.
232
This function decodes some data into image changes.
234
Returns the number of bytes consumed.
236
int QGIFFormat::decode(QImage *image, const uchar *buffer, int length,
237
int *nextFrameDelay, int *loopCount)
239
// We are required to state that
240
// "The Graphics Interchange Format(c) is the Copyright property of
241
// CompuServe Incorporated. GIF(sm) is a Service Mark property of
242
// CompuServe Incorporated."
245
stack = new short[(1 << max_lzw_bits) * 4];
246
table[0] = &stack[(1 << max_lzw_bits) * 2];
247
table[1] = &stack[(1 << max_lzw_bits) * 3];
251
int bpl = image->bytesPerLine();
252
unsigned char *bits = image->bits();
254
#define LM(l, m) (((m)<<8)|l)
256
const int initial = length;
257
while (!digress && length) {
259
unsigned char ch=*buffer++;
265
gif89=(hold[3]!='8' || hold[4]!='7');
266
state=LogicalScreenDescriptor;
270
case LogicalScreenDescriptor:
273
// Logical Screen Descriptor
274
swidth=LM(hold[0], hold[1]);
275
sheight=LM(hold[2], hold[3]);
276
gcmap=!!(hold[4]&0x80);
277
//UNUSED: bpchan=(((hold[4]&0x70)>>3)+1);
278
//UNUSED: gcmsortflag=!!(hold[4]&0x08);
279
gncols=2<<(hold[4]&0x7);
280
bgcol=(gcmap) ? hold[5] : -1;
281
//aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0;
288
state=GlobalColorMap;
289
globalcmap = new QRgb[gncols+1]; // +1 for trans_index
290
globalcmap[gncols] = Q_TRANSPARENT;
296
case GlobalColorMap: case LocalColorMap:
299
QRgb rgb = qRgb(hold[0], hold[1], hold[2]);
300
if (state == LocalColorMap) {
302
localcmap[ccount] = rgb;
304
globalcmap[ccount] = rgb;
306
if (++ccount >= ncols) {
307
if (state == LocalColorMap)
308
state=TableImageLZWSize;
319
state=ImageDescriptor;
322
state=ExtensionLabel;
325
// ### Changed: QRect(0, 0, swidth, sheight)
330
// Unexpected Introducer - ignore block
334
case ImageDescriptor:
337
int newleft=LM(hold[1], hold[2]);
338
int newtop=LM(hold[3], hold[4]);
339
int newwidth=LM(hold[5], hold[6]);
340
int newheight=LM(hold[7], hold[8]);
342
// disbelieve ridiculous logical screen sizes,
343
// unless the image frames are also large.
344
if (swidth/10 > qMax(newwidth,200))
346
if (sheight/10 > qMax(newheight,200))
350
swidth = newleft + newwidth;
352
sheight = newtop + newheight;
354
QImage::Format format = trans_index >= 0 ? QImage::Format_ARGB32 : QImage::Format_RGB32;
355
if (image->isNull()) {
356
(*image) = QImage(swidth, sheight, format);
357
bpl = image->bytesPerLine();
358
bits = image->bits();
359
memset(bits, 0, image->byteCount());
362
disposePrevious(image);
370
right=qMax(0, qMin(left+width, swidth)-1);
371
bottom=qMax(0, qMin(top+height, sheight)-1);
372
lcmap=!!(hold[9]&0x80);
373
interlace=!!(hold[9]&0x40);
374
//bool lcmsortflag=!!(hold[9]&0x20);
375
lncols=lcmap ? (2<<(hold[9]&0x7)) : 0;
379
localcmap = new QRgb[lncols+1];
380
localcmap[lncols] = Q_TRANSPARENT;
387
if (left || top || width<swidth || height<sheight) {
388
// Not full-size image - erase with bg or transparent
389
if (trans_index >= 0) {
390
fillRect(image, 0, 0, swidth, sheight, color(trans_index));
391
// ### Changed: QRect(0, 0, swidth, sheight)
392
} else if (bgcol>=0) {
393
fillRect(image, 0, 0, swidth, sheight, color(bgcol));
394
// ### Changed: QRect(0, 0, swidth, sheight)
399
if (disposal == RestoreImage) {
400
int l = qMin(swidth-1,left);
401
int r = qMin(swidth-1,right);
402
int t = qMin(sheight-1,top);
403
int b = qMin(sheight-1,bottom);
407
if (backingstore.width() < w
408
|| backingstore.height() < h) {
409
// We just use the backing store as a byte array
410
backingstore = QImage(qMax(backingstore.width(), w),
411
qMax(backingstore.height(), h),
412
QImage::Format_RGB32);
413
memset(bits, 0, image->byteCount());
415
const int dest_bpl = backingstore.bytesPerLine();
416
unsigned char *dest_data = backingstore.bits();
417
for (int ln=0; ln<h; ln++) {
418
memcpy(FAST_SCAN_LINE(dest_data, dest_bpl, ln),
419
FAST_SCAN_LINE(bits, bpl, t+ln) + l, w*sizeof(QRgb));
428
state=TableImageLZWSize;
435
firstcode = oldcode = 0;
437
out_of_bounds = left>=swidth || y>=sheight;
440
case TableImageLZWSize: {
442
if (lzwsize > max_lzw_bits) {
446
clear_code=1<<lzwsize;
447
end_code=clear_code+1;
448
max_code_size=2*clear_code;
449
max_code=clear_code+2;
451
for (i=0; i<clear_code; i++) {
455
state=ImageDataBlockSize;
459
} case ImageDataBlockSize:
462
state=ImageDataBlock;
471
accum|=(ch<<bitcount);
473
while (bitcount>=code_size && state==ImageDataBlock) {
474
int code=accum&((1<<code_size)-1);
478
if (code==clear_code) {
481
max_code_size=2*clear_code;
482
max_code=clear_code+2;
485
} else if (code==end_code) {
487
// Left the block end arrive
490
firstcode=oldcode=code;
491
if (!out_of_bounds && image->height() > y && ((frame == 0) || (firstcode != trans_index)))
492
((QRgb*)FAST_SCAN_LINE(bits, bpl, y))[x] = color(firstcode);
494
if (x>=swidth) out_of_bounds = true;
498
out_of_bounds = left>=swidth || y>=sheight;
503
if (code>=max_code) {
507
while (code>=clear_code+2) {
508
if (code >= max_code) {
512
*sp++=table[1][code];
513
if (code==table[0][code]) {
517
if (sp-stack>=(1<<(max_lzw_bits))*2) {
528
*sp++=firstcode=table[1][code];
530
if (code<(1<<max_lzw_bits)) {
531
table[0][code]=oldcode;
532
table[1][code]=firstcode;
534
if ((max_code>=max_code_size)
535
&& (max_code_size<(1<<max_lzw_bits)))
542
const int h = image->height();
544
if (!out_of_bounds && h > y)
545
line = (QRgb*)FAST_SCAN_LINE(bits, bpl, y);
547
const uchar index = *(--sp);
548
if (!out_of_bounds && h > y && ((frame == 0) || (index != trans_index))) {
549
line[x] = color(index);
552
if (x>=swidth) out_of_bounds = true;
555
out_of_bounds = left>=swidth || y>=sheight;
557
if (!out_of_bounds && h > y)
558
line = (QRgb*)FAST_SCAN_LINE(bits, bpl, y);
564
partialNewFrame = true;
565
if (count==expectcount) {
567
state=ImageDataBlockSize;
573
state=GraphicControlExtension;
576
state=ApplicationExtension;
580
state=CommentExtension;
590
case ApplicationExtension:
591
if (count<11) hold[count]=ch;
593
if (count==hold[0]+1) {
594
if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) {
596
state=NetscapeExtensionBlockSize;
603
case NetscapeExtensionBlockSize:
606
if (expectcount) state=NetscapeExtensionBlock;
607
else state=Introducer;
609
case NetscapeExtensionBlock:
610
if (count<3) hold[count]=ch;
612
if (count==expectcount) {
613
*loopCount = hold[1]+hold[2]*256;
614
state=SkipBlockSize; // Ignore further blocks
617
case GraphicControlExtension:
618
if (count<5) hold[count]=ch;
620
if (count==hold[0]+1) {
621
disposePrevious(image);
622
disposal=Disposal((hold[1]>>2)&0x7);
623
//UNUSED: waitforuser=!!((hold[1]>>1)&0x1);
624
int delay=count>3 ? LM(hold[2], hold[3]) : 1;
625
// IE and mozilla use a minimum delay of 10. With the minimum delay of 10
626
// we are compatible to them and avoid huge loads on the app and xserver.
627
*nextFrameDelay = (delay < 2 ? 10 : delay) * 10;
629
bool havetrans=hold[1]&0x1;
630
trans_index = havetrans ? hold[4] : -1;
639
if (expectcount) state=SkipBlock;
640
else state=Introducer;
644
if (count==expectcount) state=SkipBlockSize;
648
/* Netscape ignores the junk, so we do too.
650
state=Error; // More calls to this is an error
654
return -1; // Called again after done.
657
return initial-length;
661
Scans through the data stream defined by \a device and returns the image
662
sizes found in the stream in the \a imageSizes vector.
664
void QGIFFormat::scan(QIODevice *device, QVector<QSize> *imageSizes, int *loopCount)
669
qint64 oldPos = device->pos();
670
if (!device->seek(0))
674
int localColorCount = 0;
675
int globalColorCount = 0;
676
int colorReadCount = 0;
677
bool localColormap = false;
678
bool globalColormap = false;
685
State state = Header;
687
const int readBufferSize = 40960; // 40k read buffer
688
QByteArray readBuffer(device->read(readBufferSize));
690
if (readBuffer.isEmpty()) {
691
device->seek(oldPos);
695
// This is a specialized version of the state machine from decode(),
696
// which doesn't do any image decoding or mallocing, and has an
697
// optimized way of skipping SkipBlocks, ImageDataBlocks and
698
// Global/LocalColorMaps.
700
while (!readBuffer.isEmpty()) {
701
int length = readBuffer.size();
702
const uchar *buffer = (const uchar *) readBuffer.constData();
703
while (!done && length) {
705
uchar ch = *buffer++;
710
state = LogicalScreenDescriptor;
714
case LogicalScreenDescriptor:
717
imageWidth = LM(hold[0], hold[1]);
718
imageHeight = LM(hold[2], hold[3]);
719
globalColormap = !!(hold[4] & 0x80);
720
globalColorCount = 2 << (hold[4] & 0x7);
722
colorCount = globalColorCount;
723
if (globalColormap) {
724
int colorTableSize = 3 * globalColorCount;
725
if (length >= colorTableSize) {
726
// skip the global color table in one go
727
length -= colorTableSize;
728
buffer += colorTableSize;
732
state = GlobalColorMap;
743
if (++colorReadCount >= colorCount) {
744
if (state == LocalColorMap)
745
state = TableImageLZWSize;
756
state = ImageDescriptor;
759
state = ExtensionLabel;
769
case ImageDescriptor:
772
int newLeft = LM(hold[1], hold[2]);
773
int newTop = LM(hold[3], hold[4]);
774
int newWidth = LM(hold[5], hold[6]);
775
int newHeight = LM(hold[7], hold[8]);
777
if (imageWidth/10 > qMax(newWidth,200))
779
if (imageHeight/10 > qMax(newHeight,200))
783
imageWidth = newLeft + newWidth;
784
if (imageHeight <= 0)
785
imageHeight = newTop + newHeight;
787
*imageSizes << QSize(imageWidth, imageHeight);
789
localColormap = !!(hold[9] & 0x80);
790
localColorCount = localColormap ? (2 << (hold[9] & 0x7)) : 0;
792
colorCount = localColorCount;
794
colorCount = globalColorCount;
798
int colorTableSize = 3 * localColorCount;
799
if (length >= colorTableSize) {
800
// skip the local color table in one go
801
length -= colorTableSize;
802
buffer += colorTableSize;
803
state = TableImageLZWSize;
806
state = LocalColorMap;
809
state = TableImageLZWSize;
813
case TableImageLZWSize:
814
if (ch > max_lzw_bits)
817
state = ImageDataBlockSize;
820
case ImageDataBlockSize:
823
if (length >= blockSize) {
824
// we can skip the block in one go
829
state = ImageDataBlock;
837
if (count == blockSize) {
839
state = ImageDataBlockSize;
845
state = GraphicControlExtension;
848
state = ApplicationExtension;
851
state = SkipBlockSize;
855
case ApplicationExtension:
859
if (count == hold[0] + 1) {
860
if (qstrncmp((char*)(hold+1), "NETSCAPE", 8) == 0)
861
state=NetscapeExtensionBlockSize;
867
case GraphicControlExtension:
871
if (count == hold[0] + 1) {
873
state = SkipBlockSize;
876
case NetscapeExtensionBlockSize:
880
state = NetscapeExtensionBlock;
884
case NetscapeExtensionBlock:
888
if (count == blockSize) {
889
*loopCount = LM(hold[1], hold[2]);
890
state = SkipBlockSize;
897
if (length >= blockSize) {
898
// we can skip the block in one go
910
if (count == blockSize)
911
state = SkipBlockSize;
917
device->seek(oldPos);
921
readBuffer = device->read(readBufferSize);
923
device->seek(oldPos);
927
void QGIFFormat::fillRect(QImage *image, int col, int row, int w, int h, QRgb color)
930
for (int j=0; j<h; j++) {
931
QRgb *line = (QRgb*)image->scanLine(j+row);
932
for (int i=0; i<w; i++)
933
*(line+col+i) = color;
938
void QGIFFormat::nextY(unsigned char *bits, int bpl)
942
case 0: // Non-interlaced
943
// if (!out_of_bounds) {
944
// ### Changed: QRect(left, y, right - left + 1, 1);
950
my = qMin(7, bottom-y);
951
// Don't dup with transparency
952
if (trans_index < 0) {
953
for (i=1; i<=my; i++) {
954
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb),
955
(right-left+1)*sizeof(QRgb));
959
// if (!out_of_bounds) {
960
// ### Changed: QRect(left, y, right - left + 1, my + 1);
962
// if (!out_of_bounds)
963
// qDebug("consumer->changed(QRect(%d, %d, %d, %d))", left, y, right-left+1, my+1);
966
interlace++; y=top+4;
967
if (y > bottom) { // for really broken GIFs with bottom < 5
970
if (y > bottom) { // for really broken GIF with bottom < 3
979
my = qMin(3, bottom-y);
980
// Don't dup with transparency
981
if (trans_index < 0) {
982
for (i=1; i<=my; i++) {
983
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb),
984
(right-left+1)*sizeof(QRgb));
988
// if (!out_of_bounds) {
989
// ### Changed: QRect(left, y, right - left + 1, my + 1);
993
interlace++; y=top+2;
994
// handle broken GIF with bottom < 3
1003
my = qMin(1, bottom-y);
1004
// Don't dup with transparency
1005
if (trans_index < 0) {
1006
for (i=1; i<=my; i++) {
1007
memcpy(FAST_SCAN_LINE(bits, bpl, y+i)+left*sizeof(QRgb), FAST_SCAN_LINE(bits, bpl, y)+left*sizeof(QRgb),
1008
(right-left+1)*sizeof(QRgb));
1011
// if (!out_of_bounds) {
1012
// ### Changed: QRect(left, y, right - left + 1, my + 1);
1015
if (y>bottom) { interlace++; y=top+1; }
1018
// if (!out_of_bounds) {
1019
// ### Changed: QRect(left, y, right - left + 1, 1);
1024
// Consume bogus extra lines
1025
if (y >= sheight) out_of_bounds=true; //y=bottom;
1028
inline QRgb QGIFFormat::color(uchar index) const
1031
return Q_TRANSPARENT;
1033
QRgb *map = lcmap ? localcmap : globalcmap;
1034
QRgb col = map ? map[index] : 0;
1035
return index == trans_index ? col & Q_TRANSPARENT : col;
1038
//-------------------------------------------------------------------------
1039
//-------------------------------------------------------------------------
1040
//-------------------------------------------------------------------------
1042
QGifHandler::QGifHandler()
1044
gifFormat = new QGIFFormat;
1048
scanIsCached = false;
1051
QGifHandler::~QGifHandler()
1056
// Does partial decode if necessary, just to see if an image is coming
1058
bool QGifHandler::imageIsComing() const
1060
const int GifChunkSize = 4096;
1062
while (!gifFormat->partialNewFrame) {
1063
if (buffer.isEmpty()) {
1064
buffer += device()->read(GifChunkSize);
1065
if (buffer.isEmpty())
1069
int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(),
1070
&nextDelay, &loopCnt);
1073
buffer.remove(0, decoded);
1075
return gifFormat->partialNewFrame;
1078
bool QGifHandler::canRead() const
1080
if (canRead(device()) || imageIsComing()) {
1088
bool QGifHandler::canRead(QIODevice *device)
1091
qWarning("QGifHandler::canRead() called with no device");
1096
if (device->peek(head, sizeof(head)) == sizeof(head))
1097
return qstrncmp(head, "GIF87a", 6) == 0
1098
|| qstrncmp(head, "GIF89a", 6) == 0;
1102
bool QGifHandler::read(QImage *image)
1104
const int GifChunkSize = 4096;
1106
while (!gifFormat->newFrame) {
1107
if (buffer.isEmpty()) {
1108
buffer += device()->read(GifChunkSize);
1109
if (buffer.isEmpty())
1113
int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(),
1114
&nextDelay, &loopCnt);
1117
buffer.remove(0, decoded);
1119
if (gifFormat->newFrame || (gifFormat->partialNewFrame && device()->atEnd())) {
1122
gifFormat->newFrame = false;
1123
gifFormat->partialNewFrame = false;
1130
bool QGifHandler::write(const QImage &image)
1136
bool QGifHandler::supportsOption(ImageOption option) const
1138
if (!device() || device()->isSequential())
1139
return option == Animation;
1141
return option == Size
1142
|| option == Animation;
1145
QVariant QGifHandler::option(ImageOption option) const
1147
if (option == Size) {
1148
if (!scanIsCached) {
1149
QGIFFormat::scan(device(), &imageSizes, &loopCnt);
1150
scanIsCached = true;
1152
// before the first frame is read, or we have an empty data stream
1153
if (frameNumber == -1)
1154
return (imageSizes.count() > 0) ? QVariant(imageSizes.at(0)) : QVariant();
1155
// after the last frame has been read, the next size is undefined
1156
if (frameNumber >= imageSizes.count() - 1)
1158
// and the last case: the size of the next frame
1159
return imageSizes.at(frameNumber + 1);
1160
} else if (option == Animation) {
1166
void QGifHandler::setOption(ImageOption option, const QVariant &value)
1172
int QGifHandler::nextImageDelay() const
1177
int QGifHandler::imageCount() const
1179
if (!scanIsCached) {
1180
QGIFFormat::scan(device(), &imageSizes, &loopCnt);
1181
scanIsCached = true;
1183
return imageSizes.count();
1186
int QGifHandler::loopCount() const
1188
if (!scanIsCached) {
1189
QGIFFormat::scan(device(), &imageSizes, &loopCnt);
1190
scanIsCached = true;
1195
else if (loopCnt == -1)
1201
int QGifHandler::currentImageNumber() const
1206
QByteArray QGifHandler::name() const