Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmnghandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
6
7#include "qimage.h"
8#include "qvariant.h"
9#include "qcolor.h"
10
11#define MNG_USE_SO
12#define MNG_NO_INCLUDE_JNG
13#include <libmng.h>
14
15QT_BEGIN_NAMESPACE
16
17class QMngHandlerPrivate
18{
19 Q_DECLARE_PUBLIC(QMngHandler)
20 public:
21 bool haveReadNone;
22 bool haveReadAll;
23 mng_handle hMNG;
24 QImage image;
25 int elapsed;
26 int nextDelay;
27 int iterCount;
28 int frameIndex;
29 int nextIndex;
30 int frameCount;
31 mng_uint32 iStyle;
32 mng_bool readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead);
33 mng_bool writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten);
34 mng_bool processHeader(mng_uint32 iWidth, mng_uint32 iHeight);
35 QMngHandlerPrivate(QMngHandler *q_ptr);
36 ~QMngHandlerPrivate();
37 bool getNextImage(QImage *result);
38 bool writeImage(const QImage &image);
39 int currentImageNumber() const;
40 int imageCount() const;
41 bool jumpToImage(int imageNumber);
42 bool jumpToNextImage();
43 int nextImageDelay() const;
44 bool setBackgroundColor(const QColor &color);
45 QColor backgroundColor() const;
46 QMngHandler *q_ptr;
47};
48
49static mng_bool MNG_DECL myerror(mng_handle /*hMNG*/,
50 mng_int32 iErrorcode,
51 mng_int8 /*iSeverity*/,
52 mng_chunkid iChunkname,
53 mng_uint32 /*iChunkseq*/,
54 mng_int32 iExtra1,
55 mng_int32 iExtra2,
56 mng_pchar zErrortext)
57{
58 qWarning("MNG error %d: %s; chunk %c%c%c%c; subcode %d:%d",
59 iErrorcode,zErrortext,
60 (iChunkname>>24)&0xff,
61 (iChunkname>>16)&0xff,
62 (iChunkname>>8)&0xff,
63 (iChunkname>>0)&0xff,
64 iExtra1,iExtra2);
65 return MNG_TRUE;
66}
67
68static mng_ptr MNG_DECL myalloc(mng_size_t iSize)
69{
70 return (mng_ptr)calloc(1, iSize);
71}
72
73static void MNG_DECL myfree(mng_ptr pPtr, mng_size_t /*iSize*/)
74{
75 free(pPtr);
76}
77
78static mng_bool MNG_DECL myopenstream(mng_handle)
79{
80 return MNG_TRUE;
81}
82
83static mng_bool MNG_DECL myclosestream(mng_handle hMNG)
84{
85 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
86 pMydata->haveReadAll = true;
87 return MNG_TRUE;
88}
89
90static mng_bool MNG_DECL myreaddata(mng_handle hMNG,
91 mng_ptr pBuf,
92 mng_uint32 iSize,
93 mng_uint32p pRead)
94{
95 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
96 return pMydata->readData(pBuf, iSize, pRead);
97}
98
99static mng_bool MNG_DECL mywritedata(mng_handle hMNG,
100 mng_ptr pBuf,
101 mng_uint32 iSize,
102 mng_uint32p pWritten)
103{
104 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
105 return pMydata->writeData(pBuf, iSize, pWritten);
106}
107
108static mng_bool MNG_DECL myprocessheader(mng_handle hMNG,
109 mng_uint32 iWidth,
110 mng_uint32 iHeight)
111{
112 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
113 return pMydata->processHeader(iWidth, iHeight);
114}
115
116static mng_ptr MNG_DECL mygetcanvasline(mng_handle hMNG,
117 mng_uint32 iLinenr)
118{
119 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
120 return (mng_ptr)pMydata->image.scanLine(iLinenr);
121}
122
123static mng_bool MNG_DECL myrefresh(mng_handle /*hMNG*/,
124 mng_uint32 /*iX*/,
125 mng_uint32 /*iY*/,
126 mng_uint32 /*iWidth*/,
127 mng_uint32 /*iHeight*/)
128{
129 return MNG_TRUE;
130}
131
132static mng_uint32 MNG_DECL mygettickcount(mng_handle hMNG)
133{
134 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
135 return pMydata->elapsed++;
136}
137
138static mng_bool MNG_DECL mysettimer(mng_handle hMNG,
139 mng_uint32 iMsecs)
140{
141 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
142 pMydata->elapsed += iMsecs;
143 pMydata->nextDelay = iMsecs;
144 return MNG_TRUE;
145}
146
147static mng_bool MNG_DECL myprocessterm(mng_handle hMNG,
148 mng_uint8 iTermaction,
149 mng_uint8 /*iIteraction*/,
150 mng_uint32 /*iDelay*/,
151 mng_uint32 iItermax)
152{
153 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
154 if (iTermaction == 3)
155 pMydata->iterCount = iItermax;
156 return MNG_TRUE;
157}
158
159static mng_bool MNG_DECL mytrace(mng_handle,
160 mng_int32 iFuncnr,
161 mng_int32 iFuncseq,
162 mng_pchar zFuncname)
163{
164 qDebug("mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
165 return MNG_TRUE;
166}
167
168QMngHandlerPrivate::QMngHandlerPrivate(QMngHandler *q_ptr)
169 : haveReadNone(true), haveReadAll(false), elapsed(0), nextDelay(0), iterCount(1),
170 frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
171{
172 iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
173 // Initialize libmng
174 hMNG = mng_initialize((mng_ptr)this, myalloc, myfree, mytrace);
175 if (hMNG) {
176 // Set callback functions
177 mng_setcb_errorproc(hMNG, myerror);
178 mng_setcb_openstream(hMNG, myopenstream);
179 mng_setcb_closestream(hMNG, myclosestream);
180 mng_setcb_readdata(hMNG, myreaddata);
181 mng_setcb_writedata(hMNG, mywritedata);
182 mng_setcb_processheader(hMNG, myprocessheader);
183 mng_setcb_getcanvasline(hMNG, mygetcanvasline);
184 mng_setcb_refresh(hMNG, myrefresh);
185 mng_setcb_gettickcount(hMNG, mygettickcount);
186 mng_setcb_settimer(hMNG, mysettimer);
187 mng_setcb_processterm(hMNG, myprocessterm);
188 mng_set_doprogressive(hMNG, MNG_FALSE);
189 mng_set_suspensionmode(hMNG, MNG_TRUE);
190 }
191}
192
193QMngHandlerPrivate::~QMngHandlerPrivate()
194{
195 mng_cleanup(&hMNG);
196}
197
198mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
199{
200 Q_Q(QMngHandler);
201 *pRead = q->device()->read((char *)pBuf, iSize);
202 return (*pRead > 0) ? MNG_TRUE : MNG_FALSE;
203}
204
205mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
206{
207 Q_Q(QMngHandler);
208 *pWritten = q->device()->write((char *)pBuf, iSize);
209 return MNG_TRUE;
210}
211
212mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
213{
214 if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
215 return MNG_FALSE;
216 if (!QImageIOHandler::allocateImage(QSize(iWidth, iHeight), QImage::Format_ARGB32, &image))
217 return MNG_FALSE;
218 image.fill(0);
219 return MNG_TRUE;
220}
221
222bool QMngHandlerPrivate::getNextImage(QImage *result)
223{
224 mng_retcode ret;
225 const bool savedHaveReadAll = haveReadAll;
226 if (haveReadNone) {
227 haveReadNone = false;
228 ret = mng_readdisplay(hMNG);
229 } else {
230 ret = mng_display_resume(hMNG);
231 }
232 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
233 *result = image;
234
235 // QTBUG-28894 -- libmng produces an extra frame at the end
236 // of the animation on the first loop only.
237 if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
238 ret = mng_display_resume(hMNG);
239 }
240
241 frameIndex = nextIndex++;
242 if (haveReadAll && (frameCount == 0))
243 frameCount = nextIndex;
244 return true;
245 }
246 return false;
247}
248
249bool QMngHandlerPrivate::writeImage(const QImage &image)
250{
251 mng_reset(hMNG);
252 if (mng_create(hMNG) != MNG_NOERROR)
253 return false;
254
255 this->image = image.convertToFormat(QImage::Format_ARGB32);
256 int w = image.width();
257 int h = image.height();
258
259 if (
260 // width, height, ticks, layercount, framecount, playtime, simplicity
261 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
262 // termination_action, action_after_iterations, delay, iteration_max
263 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
264 // width, height, bitdepth, colortype, compression, filter, interlace
265 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
266 // width, height, colortype, bitdepth, compression, filter, interlace, canvasstyle, getcanvasline
267 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
268 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
269 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
270 (mng_write(hMNG) == MNG_NOERROR)
271 )
272 return true;
273 return false;
274}
275
276int QMngHandlerPrivate::currentImageNumber() const
277{
278// return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
279 return frameIndex;
280}
281
282int QMngHandlerPrivate::imageCount() const
283{
284// return mng_get_totalframes(hMNG); not implemented, apparently
285 if (haveReadAll)
286 return frameCount;
287 return 0; // Don't know
288}
289
290bool QMngHandlerPrivate::jumpToImage(int imageNumber)
291{
292 if (imageNumber == nextIndex)
293 return true;
294
295 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
296 // Loop!
297 nextIndex = 0;
298 return true;
299 }
300 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
301 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
302 nextIndex = imageNumber;
303 return true;
304 }
305 }
306 return false;
307}
308
309bool QMngHandlerPrivate::jumpToNextImage()
310{
311 const int numImages = imageCount();
312 return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
313}
314
315int QMngHandlerPrivate::nextImageDelay() const
316{
317 return nextDelay;
318}
319
320bool QMngHandlerPrivate::setBackgroundColor(const QColor &color)
321{
322 mng_uint16 iRed = (mng_uint16)(color.red() << 8);
323 mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
324 mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
325 return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
326}
327
328QColor QMngHandlerPrivate::backgroundColor() const
329{
330 mng_uint16 iRed;
331 mng_uint16 iBlue;
332 mng_uint16 iGreen;
333 if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
334 return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
335 return QColor();
336}
337
339 : d_ptr(new QMngHandlerPrivate(this))
340{
341}
342
346
347/*! \reimp */
348bool QMngHandler::canRead() const
349{
350 Q_D(const QMngHandler);
351 if ((!d->haveReadNone
352 && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
353 || canRead(device()))
354 {
355 setFormat("mng");
356 return true;
357 }
358 return false;
359}
360
361/*! \internal */
362bool QMngHandler::canRead(QIODevice *device)
363{
364 if (!device) {
365 qWarning("QMngHandler::canRead() called with no device");
366 return false;
367 }
368
369 return device->peek(8) == "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
370}
371
372/*! \reimp */
373bool QMngHandler::read(QImage *image)
374{
375 Q_D(QMngHandler);
376 return canRead() ? d->getNextImage(image) : false;
377}
378
379/*! \reimp */
380bool QMngHandler::write(const QImage &image)
381{
382 Q_D(QMngHandler);
383 return d->writeImage(image);
384}
385
386/*! \reimp */
388{
389 Q_D(const QMngHandler);
390 return d->currentImageNumber();
391}
392
393/*! \reimp */
395{
396 Q_D(const QMngHandler);
397 return d->imageCount();
398}
399
400/*! \reimp */
401bool QMngHandler::jumpToImage(int imageNumber)
402{
403 Q_D(QMngHandler);
404 return d->jumpToImage(imageNumber);
405}
406
407/*! \reimp */
409{
410 Q_D(QMngHandler);
411 return d->jumpToNextImage();
412}
413
414/*! \reimp */
416{
417 Q_D(const QMngHandler);
418 if (d->iterCount == 0x7FFFFFFF)
419 return -1; // infinite loop
420 return d->iterCount-1;
421}
422
423/*! \reimp */
425{
426 Q_D(const QMngHandler);
427 return d->nextImageDelay();
428}
429
430/*! \reimp */
431QVariant QMngHandler::option(ImageOption option) const
432{
433 Q_D(const QMngHandler);
434 if (option == QImageIOHandler::Animation)
435 return true;
436 else if (option == QImageIOHandler::BackgroundColor)
437 return d->backgroundColor();
438 return QVariant();
439}
440
441/*! \reimp */
442void QMngHandler::setOption(ImageOption option, const QVariant & value)
443{
444 Q_D(QMngHandler);
445 if (option == QImageIOHandler::BackgroundColor)
446 d->setBackgroundColor(qvariant_cast<QColor>(value));
447}
448
449/*! \reimp */
450bool QMngHandler::supportsOption(ImageOption option) const
451{
452 if (option == QImageIOHandler::Animation)
453 return true;
454 else if (option == QImageIOHandler::BackgroundColor)
455 return true;
456 return false;
457}
458
459QT_END_NAMESPACE
int imageCount() const override
\reimp
static bool canRead(QIODevice *device)
bool read(QImage *image) override
\reimp
int loopCount() const override
\reimp
bool jumpToNextImage() override
\reimp
int currentImageNumber() const override
\reimp
int nextImageDelay() const override
\reimp
bool write(const QImage &image) override
\reimp
bool canRead() const override
\reimp
bool jumpToImage(int imageNumber) override
\reimp
static mng_bool MNG_DECL myprocessheader(mng_handle hMNG, mng_uint32 iWidth, mng_uint32 iHeight)
static mng_ptr MNG_DECL mygetcanvasline(mng_handle hMNG, mng_uint32 iLinenr)
static mng_uint32 MNG_DECL mygettickcount(mng_handle hMNG)
static mng_bool MNG_DECL myprocessterm(mng_handle hMNG, mng_uint8 iTermaction, mng_uint8, mng_uint32, mng_uint32 iItermax)
static mng_bool MNG_DECL mysettimer(mng_handle hMNG, mng_uint32 iMsecs)
static mng_bool MNG_DECL myreaddata(mng_handle hMNG, mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
static mng_ptr MNG_DECL myalloc(mng_size_t iSize)
static mng_bool MNG_DECL mywritedata(mng_handle hMNG, mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
static mng_bool MNG_DECL myclosestream(mng_handle hMNG)
static mng_bool MNG_DECL myopenstream(mng_handle)
static mng_bool MNG_DECL myrefresh(mng_handle, mng_uint32, mng_uint32, mng_uint32, mng_uint32)
static mng_bool MNG_DECL mytrace(mng_handle, mng_int32 iFuncnr, mng_int32 iFuncseq, mng_pchar zFuncname)
static mng_bool MNG_DECL myerror(mng_handle, mng_int32 iErrorcode, mng_int8, mng_chunkid iChunkname, mng_uint32, mng_int32 iExtra1, mng_int32 iExtra2, mng_pchar zErrortext)
static void MNG_DECL myfree(mng_ptr pPtr, mng_size_t)