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 
49 QT_BEGIN_NAMESPACE
50 
51 class 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 
myerror(mng_handle,mng_int32 iErrorcode,mng_int8,mng_chunkid iChunkname,mng_uint32,mng_int32 iExtra1,mng_int32 iExtra2,mng_pchar zErrortext)83 static 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 
myalloc(mng_size_t iSize)102 static mng_ptr myalloc(mng_size_t iSize)
103 {
104     return (mng_ptr)calloc(1, iSize);
105 }
106 
myfree(mng_ptr pPtr,mng_size_t)107 static void myfree(mng_ptr pPtr, mng_size_t /*iSize*/)
108 {
109     free(pPtr);
110 }
111 
myopenstream(mng_handle)112 static mng_bool myopenstream(mng_handle)
113 {
114     return MNG_TRUE;
115 }
116 
myclosestream(mng_handle hMNG)117 static 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 
myreaddata(mng_handle hMNG,mng_ptr pBuf,mng_uint32 iSize,mng_uint32p pRead)124 static 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 
mywritedata(mng_handle hMNG,mng_ptr pBuf,mng_uint32 iSize,mng_uint32p pWritten)133 static 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 
myprocessheader(mng_handle hMNG,mng_uint32 iWidth,mng_uint32 iHeight)142 static 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 
mygetcanvasline(mng_handle hMNG,mng_uint32 iLinenr)150 static 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 
myrefresh(mng_handle,mng_uint32,mng_uint32,mng_uint32,mng_uint32)157 static 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 
mygettickcount(mng_handle hMNG)166 static mng_uint32 mygettickcount(mng_handle hMNG)
167 {
168     QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
169     return pMydata->elapsed++;
170 }
171 
mysettimer(mng_handle hMNG,mng_uint32 iMsecs)172 static 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 
myprocessterm(mng_handle hMNG,mng_uint8 iTermaction,mng_uint8,mng_uint32,mng_uint32 iItermax)181 static 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 
mytrace(mng_handle,mng_int32 iFuncnr,mng_int32 iFuncseq,mng_pchar zFuncname)193 static 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 
QMngHandlerPrivate(QMngHandler * q_ptr)202 QMngHandlerPrivate::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 
~QMngHandlerPrivate()227 QMngHandlerPrivate::~QMngHandlerPrivate()
228 {
229     mng_cleanup(&hMNG);
230 }
231 
readData(mng_ptr pBuf,mng_uint32 iSize,mng_uint32p pRead)232 mng_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 
writeData(mng_ptr pBuf,mng_uint32 iSize,mng_uint32p pWritten)239 mng_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 
processHeader(mng_uint32 iWidth,mng_uint32 iHeight)246 mng_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 
getNextImage(QImage * result)255 bool 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 
writeImage(const QImage & image)282 bool 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 
currentImageNumber() const309 int QMngHandlerPrivate::currentImageNumber() const
310 {
311 //    return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
312     return frameIndex;
313 }
314 
imageCount() const315 int 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 
jumpToImage(int imageNumber)323 bool 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 
jumpToNextImage()342 bool QMngHandlerPrivate::jumpToNextImage()
343 {
344     const int numImages = imageCount();
345     return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
346 }
347 
nextImageDelay() const348 int QMngHandlerPrivate::nextImageDelay() const
349 {
350     return nextDelay;
351 }
352 
setBackgroundColor(const QColor & color)353 bool 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 
backgroundColor() const361 QColor 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 
QMngHandler()371 QMngHandler::QMngHandler()
372     : d_ptr(new QMngHandlerPrivate(this))
373 {
374 }
375 
~QMngHandler()376 QMngHandler::~QMngHandler()
377 {
378 }
379 
380 /*! \reimp */
canRead() const381 bool 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 */
canRead(QIODevice * device)395 bool 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 */
read(QImage * image)406 bool QMngHandler::read(QImage *image)
407 {
408     Q_D(QMngHandler);
409     return canRead() ? d->getNextImage(image) : false;
410 }
411 
412 /*! \reimp */
write(const QImage & image)413 bool QMngHandler::write(const QImage &image)
414 {
415     Q_D(QMngHandler);
416     return d->writeImage(image);
417 }
418 
419 /*! \reimp */
currentImageNumber() const420 int QMngHandler::currentImageNumber() const
421 {
422     Q_D(const QMngHandler);
423     return d->currentImageNumber();
424 }
425 
426 /*! \reimp */
imageCount() const427 int QMngHandler::imageCount() const
428 {
429     Q_D(const QMngHandler);
430     return d->imageCount();
431 }
432 
433 /*! \reimp */
jumpToImage(int imageNumber)434 bool QMngHandler::jumpToImage(int imageNumber)
435 {
436     Q_D(QMngHandler);
437     return d->jumpToImage(imageNumber);
438 }
439 
440 /*! \reimp */
jumpToNextImage()441 bool QMngHandler::jumpToNextImage()
442 {
443     Q_D(QMngHandler);
444     return d->jumpToNextImage();
445 }
446 
447 /*! \reimp */
loopCount() const448 int QMngHandler::loopCount() const
449 {
450     Q_D(const QMngHandler);
451     if (d->iterCount == 0x7FFFFFFF)
452         return -1; // infinite loop
453     return d->iterCount-1;
454 }
455 
456 /*! \reimp */
nextImageDelay() const457 int QMngHandler::nextImageDelay() const
458 {
459     Q_D(const QMngHandler);
460     return d->nextImageDelay();
461 }
462 
463 /*! \reimp */
option(ImageOption option) const464 QVariant QMngHandler::option(ImageOption option) const
465 {
466     Q_D(const QMngHandler);
467     if (option == QImageIOHandler::Animation)
468         return true;
469     else if (option == QImageIOHandler::BackgroundColor)
470         return d->backgroundColor();
471     return QVariant();
472 }
473 
474 /*! \reimp */
setOption(ImageOption option,const QVariant & value)475 void QMngHandler::setOption(ImageOption option, const QVariant & value)
476 {
477     Q_D(QMngHandler);
478     if (option == QImageIOHandler::BackgroundColor)
479         d->setBackgroundColor(qvariant_cast<QColor>(value));
480 }
481 
482 /*! \reimp */
supportsOption(ImageOption option) const483 bool QMngHandler::supportsOption(ImageOption option) const
484 {
485     if (option == QImageIOHandler::Animation)
486         return true;
487     else if (option == QImageIOHandler::BackgroundColor)
488         return true;
489     return false;
490 }
491 
492 QT_END_NAMESPACE
493