52 GifFileType* gifFile = DGifOpen(device(),
doInput, &err);
54 qWarning() <<
"Received error code" << err;
59 *image = QImage(gifFile->SWidth, gifFile->SHeight, QImage::Format_Indexed8);
61 GifRecordType recordType;
62 ColorMapObject* ColorMap;
64 int i, row, imageNum = 0, topRow, width, height;
68 DGifGetRecordType(gifFile, &recordType);
71 case IMAGE_DESC_RECORD_TYPE:
72 if (DGifGetImageDesc(gifFile) == GIF_ERROR)
74 qWarning(
"QGIFLibHandler::read: error %d", gifFile->Error);
77 topRow = gifFile->Image.Top;
78 width = gifFile->Image.Width;
79 height = gifFile->Image.Height;
81 if (gifFile->Image.Left + width > gifFile->SWidth ||
82 gifFile->Image.Top + height > gifFile->SHeight)
84 qWarning(
"Image %d is not confined to screen dimension, aborted.", imageNum);
90 image->fill(gifFile->SBackGroundColor);
93 if (gifFile->Image.Interlace)
96 for (i = 0; i < 4; i++)
100 if (DGifGetLine(gifFile, image->scanLine(row), width) == GIF_ERROR)
102 qWarning(
"QGIFLibHandler::read: error %d", gifFile->Error);
113 for (row = 0; row < height; row++)
115 if (DGifGetLine(gifFile, image->scanLine(row), width) == GIF_ERROR)
117 qWarning(
"QGIFLibHandler::read: error %d", gifFile->Error);
127 case EXTENSION_RECORD_TYPE:
130 GifByteType* extData;
132 if (DGifGetExtension(gifFile, &extCode, &extData) == GIF_ERROR)
134 qWarning(
"QGIFLibHandler::read: error %d", gifFile->Error);
137 while (extData != NULL)
139 int len = extData[0];
142 case GRAPHICS_EXT_FUNC_CODE:
149 if (extData[1] & 0x01)
151 transColor = extData[3];
157 case COMMENT_EXT_FUNC_CODE:
159 QByteArray comment((
char*)(extData + 1), len);
161 image->setText(
"Description", comment);
164 case PLAINTEXT_EXT_FUNC_CODE:
167 if (DGifGetExtensionNext(gifFile, &extData) == GIF_ERROR)
169 qWarning(
"QGIFLibHandler::read: error %d", gifFile->Error);
175 case TERMINATE_RECORD_TYPE:
181 while (recordType != TERMINATE_RECORD_TYPE);
184 ColorMap = (gifFile->Image.ColorMap
185 ? gifFile->Image.ColorMap
186 : gifFile->SColorMap);
189 qWarning(
"QGIFLibHandler::read: Image does not have a colormap");
192 int ccount = ColorMap->ColorCount;
193 image->setColorCount(ccount);
194 for (i = 0; i < ccount; ++i)
196 GifColorType gifColor = ColorMap->Colors[i];
197 QRgb color = gifColor.Blue | (gifColor.Green << 8) | (gifColor.Red << 16);
203 image->setColor(i, color);
225 QImage toWrite(image);
227 if (toWrite.colorCount() == 0 || toWrite.colorCount() > 256)
228 toWrite = image.convertToFormat(QImage::Format_Indexed8);
233 int colorCount = 1 << GifBitSize(toWrite.colorCount());
234 cmap.ColorCount = colorCount;
235 cmap.BitsPerPixel = 8;
236 GifColorType* colorValues = (GifColorType*)malloc(cmap.ColorCount *
sizeof(GifColorType));
237 cmap.Colors = colorValues;
239 for(; c < toWrite.colorCount(); ++c)
242 colorValues[c].Red = qRed(colorTable[c]);
243 colorValues[c].Green = qGreen(colorTable[c]);
244 colorValues[c].Blue = qBlue(colorTable[c]);
248 for (; c < colorCount; ++c)
250 colorValues[c].Red = 0;
251 colorValues[c].Green = 0;
252 colorValues[c].Blue = 0;
259 GifFileType *gif = EGifOpen(device(),
doOutput, &err);
263 EGifSetGifVersion(gif,
true);
266 if (EGifPutScreenDesc(gif, toWrite.width(), toWrite.height(), colorCount, 0, &cmap) == GIF_ERROR) {
267 qWarning(
"EGifPutScreenDesc returned error %d", gif->Error);
270 QVariant descText =
option(QImageIOHandler::Description);
271 if (descText.type() == QVariant::String)
273 QString comment = descText.toString();
277 int idx = comment.indexOf(
": ");
279 comment.remove(0, idx + 2);
281 if (!comment.isEmpty())
282 EGifPutComment(gif, comment.toUtf8().constData());
288 if (EGifPutImageDesc(gif, 0, 0, toWrite.width(), toWrite.height(), 0, &cmap) == GIF_ERROR)
289 qWarning(
"EGifPutImageDesc returned error %d", gif->Error);
291 int lc = toWrite.height();
295 int llen = toWrite.width();
299 for (
int l = 0; l < lc; ++l)
301 uchar* line = toWrite.scanLine(l);
302 if (EGifPutLine(gif, (GifPixelType*)line, llen) == GIF_ERROR)
305 qWarning(
"EGifPutLine returned error %d", i);
309 EGifCloseFile(gif, &err);