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