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 if (pMydata->image.isNull() || iLinenr >= mng_uint32(pMydata->image.height()))
121 return nullptr;
122 return (mng_ptr)pMydata->image.scanLine(iLinenr);
123}
124
125static mng_bool MNG_DECL myrefresh(mng_handle /*hMNG*/,
126 mng_uint32 /*iX*/,
127 mng_uint32 /*iY*/,
128 mng_uint32 /*iWidth*/,
129 mng_uint32 /*iHeight*/)
130{
131 return MNG_TRUE;
132}
133
134static mng_uint32 MNG_DECL mygettickcount(mng_handle hMNG)
135{
136 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
137 return pMydata->elapsed++;
138}
139
140static mng_bool MNG_DECL mysettimer(mng_handle hMNG,
141 mng_uint32 iMsecs)
142{
143 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
144 pMydata->elapsed += iMsecs;
145 pMydata->nextDelay = iMsecs;
146 return MNG_TRUE;
147}
148
149static mng_bool MNG_DECL myprocessterm(mng_handle hMNG,
150 mng_uint8 iTermaction,
151 mng_uint8 /*iIteraction*/,
152 mng_uint32 /*iDelay*/,
153 mng_uint32 iItermax)
154{
155 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
156 if (iTermaction == 3)
157 pMydata->iterCount = qMin<mng_uint32>(iItermax, 0x7FFFFFFF);
158 return MNG_TRUE;
159}
160
161static mng_bool MNG_DECL mytrace(mng_handle,
162 mng_int32 iFuncnr,
163 mng_int32 iFuncseq,
164 mng_pchar zFuncname)
165{
166 qDebug("mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
167 return MNG_TRUE;
168}
169
170QMngHandlerPrivate::QMngHandlerPrivate(QMngHandler *q_ptr)
171 : haveReadNone(true), haveReadAll(false), elapsed(0), nextDelay(0), iterCount(1),
172 frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
173{
174 iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
175 // Initialize libmng
176 hMNG = mng_initialize((mng_ptr)this, myalloc, myfree, mytrace);
177 if (hMNG) {
178 // Set callback functions
179 mng_setcb_errorproc(hMNG, myerror);
180 mng_setcb_openstream(hMNG, myopenstream);
181 mng_setcb_closestream(hMNG, myclosestream);
182 mng_setcb_readdata(hMNG, myreaddata);
183 mng_setcb_writedata(hMNG, mywritedata);
184 mng_setcb_processheader(hMNG, myprocessheader);
185 mng_setcb_getcanvasline(hMNG, mygetcanvasline);
186 mng_setcb_refresh(hMNG, myrefresh);
187 mng_setcb_gettickcount(hMNG, mygettickcount);
188 mng_setcb_settimer(hMNG, mysettimer);
189 mng_setcb_processterm(hMNG, myprocessterm);
190 mng_set_doprogressive(hMNG, MNG_FALSE);
191 mng_set_suspensionmode(hMNG, MNG_TRUE);
192 }
193}
194
195QMngHandlerPrivate::~QMngHandlerPrivate()
196{
197 mng_cleanup(&hMNG);
198}
199
200mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
201{
202 Q_Q(QMngHandler);
203 const qint64 nRead = q->device()->read((char *)pBuf, iSize);
204 *pRead = (nRead > 0) ? mng_uint32(nRead) : 0;
205 return (nRead > 0) ? MNG_TRUE : MNG_FALSE;
206}
207
208mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
209{
210 Q_Q(QMngHandler);
211 const qint64 nWritten = q->device()->write((char *)pBuf, iSize);
212 if (nWritten < 0) {
213 *pWritten = 0;
214 return MNG_FALSE;
215 }
216 *pWritten = mng_uint32(nWritten);
217 return MNG_TRUE;
218}
219
220mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
221{
222 if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
223 return MNG_FALSE;
224 if (!QImageIOHandler::allocateImage(QSize(iWidth, iHeight), QImage::Format_ARGB32, &image))
225 return MNG_FALSE;
226 image.fill(0);
227 return MNG_TRUE;
228}
229
230bool QMngHandlerPrivate::getNextImage(QImage *result)
231{
232 mng_retcode ret;
233 const bool savedHaveReadAll = haveReadAll;
234 if (haveReadNone) {
235 haveReadNone = false;
236 ret = mng_readdisplay(hMNG);
237 } else {
238 ret = mng_display_resume(hMNG);
239 }
240 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
241 *result = image;
242
243 // QTBUG-28894 -- libmng produces an extra frame at the end
244 // of the animation on the first loop only.
245 if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
246 ret = mng_display_resume(hMNG);
247 }
248
249 frameIndex = nextIndex++;
250 if (haveReadAll && (frameCount == 0))
251 frameCount = nextIndex;
252 return true;
253 }
254 return false;
255}
256
257bool QMngHandlerPrivate::writeImage(const QImage &image)
258{
259 mng_reset(hMNG);
260 if (mng_create(hMNG) != MNG_NOERROR)
261 return false;
262
263 this->image = image.convertToFormat(QImage::Format_ARGB32);
264 int w = image.width();
265 int h = image.height();
266
267 if (
268 // width, height, ticks, layercount, framecount, playtime, simplicity
269 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
270 // termination_action, action_after_iterations, delay, iteration_max
271 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
272 // width, height, bitdepth, colortype, compression, filter, interlace
273 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
274 // width, height, colortype, bitdepth, compression, filter, interlace, canvasstyle, getcanvasline
275 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
276 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
277 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
278 (mng_write(hMNG) == MNG_NOERROR)
279 )
280 return true;
281 return false;
282}
283
284int QMngHandlerPrivate::currentImageNumber() const
285{
286// return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
287 return frameIndex;
288}
289
290int QMngHandlerPrivate::imageCount() const
291{
292// return mng_get_totalframes(hMNG); not implemented, apparently
293 if (haveReadAll)
294 return frameCount;
295 return 0; // Don't know
296}
297
298bool QMngHandlerPrivate::jumpToImage(int imageNumber)
299{
300 if (imageNumber == nextIndex)
301 return true;
302
303 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
304 // Loop!
305 nextIndex = 0;
306 return true;
307 }
308 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
309 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
310 nextIndex = imageNumber;
311 return true;
312 }
313 }
314 return false;
315}
316
317bool QMngHandlerPrivate::jumpToNextImage()
318{
319 const int numImages = imageCount();
320 return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
321}
322
323int QMngHandlerPrivate::nextImageDelay() const
324{
325 return nextDelay;
326}
327
328bool QMngHandlerPrivate::setBackgroundColor(const QColor &color)
329{
330 mng_uint16 iRed = (mng_uint16)(color.red() << 8);
331 mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
332 mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
333 return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
334}
335
336QColor QMngHandlerPrivate::backgroundColor() const
337{
338 mng_uint16 iRed;
339 mng_uint16 iBlue;
340 mng_uint16 iGreen;
341 if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
342 return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
343 return QColor();
344}
345
347 : d_ptr(new QMngHandlerPrivate(this))
348{
349}
350
354
355/*! \reimp */
356bool QMngHandler::canRead() const
357{
358 Q_D(const QMngHandler);
359 if ((!d->haveReadNone
360 && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
361 || canRead(device()))
362 {
363 setFormat("mng");
364 return true;
365 }
366 return false;
367}
368
369/*! \internal */
370bool QMngHandler::canRead(QIODevice *device)
371{
372 if (!device) {
373 qWarning("QMngHandler::canRead() called with no device");
374 return false;
375 }
376
377 return device->peek(8) == "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
378}
379
380/*! \reimp */
381bool QMngHandler::read(QImage *image)
382{
383 Q_D(QMngHandler);
384 return canRead() ? d->getNextImage(image) : false;
385}
386
387/*! \reimp */
388bool QMngHandler::write(const QImage &image)
389{
390 Q_D(QMngHandler);
391 return d->writeImage(image);
392}
393
394/*! \reimp */
396{
397 Q_D(const QMngHandler);
398 return d->currentImageNumber();
399}
400
401/*! \reimp */
403{
404 Q_D(const QMngHandler);
405 return d->imageCount();
406}
407
408/*! \reimp */
409bool QMngHandler::jumpToImage(int imageNumber)
410{
411 Q_D(QMngHandler);
412 return d->jumpToImage(imageNumber);
413}
414
415/*! \reimp */
417{
418 Q_D(QMngHandler);
419 return d->jumpToNextImage();
420}
421
422/*! \reimp */
424{
425 Q_D(const QMngHandler);
426 if (d->iterCount == 0x7FFFFFFF)
427 return -1; // infinite loop
428 return d->iterCount-1;
429}
430
431/*! \reimp */
433{
434 Q_D(const QMngHandler);
435 return d->nextImageDelay();
436}
437
438/*! \reimp */
439QVariant QMngHandler::option(ImageOption option) const
440{
441 Q_D(const QMngHandler);
442 if (option == QImageIOHandler::Animation)
443 return true;
444 else if (option == QImageIOHandler::BackgroundColor)
445 return d->backgroundColor();
446 return QVariant();
447}
448
449/*! \reimp */
450void QMngHandler::setOption(ImageOption option, const QVariant & value)
451{
452 Q_D(QMngHandler);
453 if (option == QImageIOHandler::BackgroundColor)
454 d->setBackgroundColor(qvariant_cast<QColor>(value));
455}
456
457/*! \reimp */
458bool QMngHandler::supportsOption(ImageOption option) const
459{
460 if (option == QImageIOHandler::Animation)
461 return true;
462 else if (option == QImageIOHandler::BackgroundColor)
463 return true;
464 return false;
465}
466
467QT_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)