Qt 4.8
qmnghandler.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qmnghandler_p.h"
43 
44 #include "qimage.h"
45 #include "qvariant.h"
46 #include "qcolor.h"
47 
48 #define MNG_USE_SO
49 #include <libmng.h>
50 
52 
54 {
56  public:
59  mng_handle hMNG;
61  int elapsed;
62  int nextDelay;
63  int iterCount;
65  int nextIndex;
67  mng_uint32 iStyle;
68  mng_bool readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead);
69  mng_bool writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten);
70  mng_bool processHeader(mng_uint32 iWidth, mng_uint32 iHeight);
73  bool getNextImage(QImage *result);
74  bool writeImage(const QImage &image);
75  int currentImageNumber() const;
76  int imageCount() const;
77  bool jumpToImage(int imageNumber);
78  bool jumpToNextImage();
79  int nextImageDelay() const;
80  bool setBackgroundColor(const QColor &color);
81  QColor backgroundColor() const;
83 };
84 
85 static mng_bool myerror(mng_handle /*hMNG*/,
86  mng_int32 iErrorcode,
87  mng_int8 /*iSeverity*/,
88  mng_chunkid iChunkname,
89  mng_uint32 /*iChunkseq*/,
90  mng_int32 iExtra1,
91  mng_int32 iExtra2,
92  mng_pchar zErrortext)
93 {
94  qWarning("MNG error %d: %s; chunk %c%c%c%c; subcode %d:%d",
95  iErrorcode,zErrortext,
96  (iChunkname>>24)&0xff,
97  (iChunkname>>16)&0xff,
98  (iChunkname>>8)&0xff,
99  (iChunkname>>0)&0xff,
100  iExtra1,iExtra2);
101  return TRUE;
102 }
103 
104 static mng_ptr myalloc(mng_size_t iSize)
105 {
106 #if defined(Q_OS_WINCE)
107  mng_ptr ptr = malloc(iSize);
108  memset(ptr, 0, iSize);
109  return ptr;
110 #else
111  return (mng_ptr)calloc(1, iSize);
112 #endif
113 }
114 
115 static void myfree(mng_ptr pPtr, mng_size_t /*iSize*/)
116 {
117  free(pPtr);
118 }
119 
120 static mng_bool myopenstream(mng_handle)
121 {
122  return MNG_TRUE;
123 }
124 
125 static mng_bool myclosestream(mng_handle hMNG)
126 {
127  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
128  pMydata->haveReadAll = true;
129  return MNG_TRUE;
130 }
131 
132 static mng_bool myreaddata(mng_handle hMNG,
133  mng_ptr pBuf,
134  mng_uint32 iSize,
135  mng_uint32p pRead)
136 {
137  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
138  return pMydata->readData(pBuf, iSize, pRead);
139 }
140 
141 static mng_bool mywritedata(mng_handle hMNG,
142  mng_ptr pBuf,
143  mng_uint32 iSize,
144  mng_uint32p pWritten)
145 {
146  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
147  return pMydata->writeData(pBuf, iSize, pWritten);
148 }
149 
150 static mng_bool myprocessheader(mng_handle hMNG,
151  mng_uint32 iWidth,
152  mng_uint32 iHeight)
153 {
154  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
155  return pMydata->processHeader(iWidth, iHeight);
156 }
157 
158 static mng_ptr mygetcanvasline(mng_handle hMNG,
159  mng_uint32 iLinenr)
160 {
161  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
162  return (mng_ptr)pMydata->image.scanLine(iLinenr);
163 }
164 
165 static mng_bool myrefresh(mng_handle /*hMNG*/,
166  mng_uint32 /*iX*/,
167  mng_uint32 /*iY*/,
168  mng_uint32 /*iWidth*/,
169  mng_uint32 /*iHeight*/)
170 {
171  return MNG_TRUE;
172 }
173 
174 static mng_uint32 mygettickcount(mng_handle hMNG)
175 {
176  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
177  return pMydata->elapsed++;
178 }
179 
180 static mng_bool mysettimer(mng_handle hMNG,
181  mng_uint32 iMsecs)
182 {
183  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
184  pMydata->elapsed += iMsecs;
185  pMydata->nextDelay = iMsecs;
186  return MNG_TRUE;
187 }
188 
189 static mng_bool myprocessterm(mng_handle hMNG,
190  mng_uint8 iTermaction,
191  mng_uint8 /*iIteraction*/,
192  mng_uint32 /*iDelay*/,
193  mng_uint32 iItermax)
194 {
195  QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
196  if (iTermaction == 3)
197  pMydata->iterCount = iItermax;
198  return MNG_TRUE;
199 }
200 
201 static mng_bool mytrace(mng_handle,
202  mng_int32 iFuncnr,
203  mng_int32 iFuncseq,
204  mng_pchar zFuncname)
205 {
206  qDebug("mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
207  return MNG_TRUE;
208 }
209 
211  : haveReadNone(true), haveReadAll(false), elapsed(0), nextDelay(0), iterCount(1),
212  frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
213 {
214  iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
215  // Initialize libmng
216  hMNG = mng_initialize((mng_ptr)this, myalloc, myfree, mytrace);
217  if (hMNG) {
218  // Set callback functions
219  mng_setcb_errorproc(hMNG, myerror);
220  mng_setcb_openstream(hMNG, myopenstream);
221  mng_setcb_closestream(hMNG, myclosestream);
222  mng_setcb_readdata(hMNG, myreaddata);
223  mng_setcb_writedata(hMNG, mywritedata);
224  mng_setcb_processheader(hMNG, myprocessheader);
225  mng_setcb_getcanvasline(hMNG, mygetcanvasline);
226  mng_setcb_refresh(hMNG, myrefresh);
227  mng_setcb_gettickcount(hMNG, mygettickcount);
228  mng_setcb_settimer(hMNG, mysettimer);
229  mng_setcb_processterm(hMNG, myprocessterm);
230  mng_set_doprogressive(hMNG, MNG_FALSE);
231  mng_set_suspensionmode(hMNG, MNG_TRUE);
232  }
233 }
234 
236 {
237  mng_cleanup(&hMNG);
238 }
239 
240 mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
241 {
242  Q_Q(QMngHandler);
243  *pRead = q->device()->read((char *)pBuf, iSize);
244  return (*pRead > 0) ? MNG_TRUE : MNG_FALSE;
245 }
246 
247 mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
248 {
249  Q_Q(QMngHandler);
250  *pWritten = q->device()->write((char *)pBuf, iSize);
251  return MNG_TRUE;
252 }
253 
254 mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
255 {
256  if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
257  return MNG_FALSE;
258  image = QImage(iWidth, iHeight, QImage::Format_ARGB32);
259  image.fill(0);
260  return MNG_TRUE;
261 }
262 
264 {
265  mng_retcode ret;
266  const bool savedHaveReadAll = haveReadAll;
267  if (haveReadNone) {
268  haveReadNone = false;
269  ret = mng_readdisplay(hMNG);
270  } else {
271  ret = mng_display_resume(hMNG);
272  }
273  if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
274  *result = image;
275 
276  // QTBUG-28894 -- libmng produces an extra frame at the end
277  // of the animation on the first loop only.
278  if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
279  ret = mng_display_resume(hMNG);
280  }
281 
282  frameIndex = nextIndex++;
283  if (haveReadAll && (frameCount == 0))
285  return true;
286  }
287  return false;
288 }
289 
291 {
292  mng_reset(hMNG);
293  if (mng_create(hMNG) != MNG_NOERROR)
294  return false;
295 
296  this->image = image.convertToFormat(QImage::Format_ARGB32);
297  int w = image.width();
298  int h = image.height();
299 
300  if (
301  // width, height, ticks, layercount, framecount, playtime, simplicity
302  (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
303  // termination_action, action_after_iterations, delay, iteration_max
304  (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
305  // width, height, bitdepth, colortype, compression, filter, interlace
306  (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
307  // width, height, colortype, bitdepth, compression, filter, interlace, canvasstyle, getcanvasline
308  (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
309  (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
310  (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
311  (mng_write(hMNG) == MNG_NOERROR)
312  )
313  return true;
314  return false;
315 }
316 
318 {
319 // return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
320  return frameIndex;
321 }
322 
324 {
325 // return mng_get_totalframes(hMNG); not implemented, apparently
326  if (haveReadAll)
327  return frameCount;
328  return 0; // Don't know
329 }
330 
331 bool QMngHandlerPrivate::jumpToImage(int imageNumber)
332 {
333  if (imageNumber == nextIndex)
334  return true;
335 
336  if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
337  // Loop!
338  nextIndex = 0;
339  return true;
340  }
341  if (mng_display_freeze(hMNG) == MNG_NOERROR) {
342  if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
343  nextIndex = imageNumber;
344  return true;
345  }
346  }
347  return false;
348 }
349 
351 {
352  return jumpToImage((currentImageNumber()+1) % imageCount());
353 }
354 
356 {
357  return nextDelay;
358 }
359 
361 {
362  mng_uint16 iRed = (mng_uint16)(color.red() << 8);
363  mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
364  mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
365  return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
366 }
367 
369 {
370  mng_uint16 iRed;
371  mng_uint16 iBlue;
372  mng_uint16 iGreen;
373  if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
374  return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
375  return QColor();
376 }
377 
379  : d_ptr(new QMngHandlerPrivate(this))
380 {
381 }
382 
384 {
385 }
386 
389 {
390  Q_D(const QMngHandler);
391  if ((!d->haveReadNone
392  && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
393  || canRead(device()))
394  {
395  setFormat("mng");
396  return true;
397  }
398  return false;
399 }
400 
403 {
404  if (!device) {
405  qWarning("QMngHandler::canRead() called with no device");
406  return false;
407  }
408 
409  return device->peek(8) == "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
410 }
411 
414 {
415  return "mng";
416 }
417 
420 {
421  Q_D(QMngHandler);
422  return canRead() ? d->getNextImage(image) : false;
423 }
424 
426 bool QMngHandler::write(const QImage &image)
427 {
428  Q_D(QMngHandler);
429  return d->writeImage(image);
430 }
431 
434 {
435  Q_D(const QMngHandler);
436  return d->currentImageNumber();
437 }
438 
441 {
442  Q_D(const QMngHandler);
443  return d->imageCount();
444 }
445 
447 bool QMngHandler::jumpToImage(int imageNumber)
448 {
449  Q_D(QMngHandler);
450  return d->jumpToImage(imageNumber);
451 }
452 
455 {
456  Q_D(QMngHandler);
457  return d->jumpToNextImage();
458 }
459 
462 {
463  Q_D(const QMngHandler);
464  if (d->iterCount == 0x7FFFFFFF)
465  return -1; // infinite loop
466  return d->iterCount-1;
467 }
468 
471 {
472  Q_D(const QMngHandler);
473  return d->nextImageDelay();
474 }
475 
478 {
479  Q_D(const QMngHandler);
480  if (option == QImageIOHandler::Animation)
481  return true;
482  else if (option == QImageIOHandler::BackgroundColor)
483  return d->backgroundColor();
484  return QVariant();
485 }
486 
489 {
490  Q_D(QMngHandler);
491  if (option == QImageIOHandler::BackgroundColor)
492  d->setBackgroundColor(qvariant_cast<QColor>(value));
493 }
494 
497 {
498  if (option == QImageIOHandler::Animation)
499  return true;
500  else if (option == QImageIOHandler::BackgroundColor)
501  return true;
502  return false;
503 }
504 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
int nextImageDelay() const
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
mng_bool writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
static mng_ptr myalloc(mng_size_t iSize)
virtual bool write(const QImage &image)
Reimplemented Function
virtual int currentImageNumber() const
Reimplemented Function
virtual bool jumpToImage(int imageNumber)
Reimplemented Function
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool getNextImage(QImage *result)
virtual bool jumpToNextImage()
Reimplemented Function
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition: qimage.cpp:2032
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual bool read(QImage *image)
Reimplemented Function
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
virtual int loopCount() const
Reimplemented Function
static mng_bool myopenstream(mng_handle)
virtual bool supportsOption(ImageOption option) const
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
#define Q_Q(Class)
Definition: qglobal.h:2483
static mng_bool myrefresh(mng_handle, mng_uint32, mng_uint32, mng_uint32, mng_uint32)
QMngHandlerPrivate(QMngHandler *q_ptr)
Q_CORE_EXPORT void qDebug(const char *,...)
mng_bool processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static mng_bool mywritedata(mng_handle hMNG, mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
ImageOption
This enum describes the different options supported by QImageIOHandler.
static mng_uint32 mygettickcount(mng_handle hMNG)
#define calloc(a, b)
virtual int nextImageDelay() const
Reimplemented Function
static mng_ptr mygetcanvasline(mng_handle hMNG, mng_uint32 iLinenr)
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Definition: qiodevice.cpp:1563
Q_CORE_EXPORT void qWarning(const char *,...)
static mng_bool myclosestream(mng_handle hMNG)
QColor backgroundColor() const
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static mng_bool myreaddata(mng_handle hMNG, mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
int imageCount() const
const T * ptr(const T &t)
static mng_bool myerror(mng_handle, mng_int32 iErrorcode, mng_int8, mng_chunkid iChunkname, mng_uint32, mng_int32 iExtra1, mng_int32 iExtra2, mng_pchar zErrortext)
Definition: qmnghandler.cpp:85
static mng_bool mytrace(mng_handle, mng_int32 iFuncnr, mng_int32 iFuncseq, mng_pchar zFuncname)
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
static mng_bool mysettimer(mng_handle hMNG, mng_uint32 iMsecs)
bool setBackgroundColor(const QColor &color)
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
mng_bool readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
virtual int imageCount() const
Reimplemented Function
virtual QByteArray name() const
Reimplemented Function
bool jumpToImage(int imageNumber)
bool writeImage(const QImage &image)
virtual void setOption(ImageOption option, const QVariant &value)
Reimplemented Function
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
QMngHandler * q_ptr
Definition: qmnghandler.cpp:82
virtual QVariant option(ImageOption option) const
Reimplemented Function
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
static mng_bool myprocessheader(mng_handle hMNG, mng_uint32 iWidth, mng_uint32 iHeight)
static mng_bool myprocessterm(mng_handle hMNG, mng_uint8 iTermaction, mng_uint8, mng_uint32, mng_uint32 iItermax)
virtual bool canRead() const
Reimplemented Function
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
static void myfree(mng_ptr pPtr, mng_size_t)
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
int currentImageNumber() const