source: trunk/src/plugins/imageformats/mng/qmnghandler.cpp@ 187

Last change on this file since 187 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qmnghandler.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
51QT_BEGIN_NAMESPACE
52
53class QMngHandlerPrivate
54{
55 Q_DECLARE_PUBLIC(QMngHandler)
56 public:
57 bool haveReadNone;
58 bool haveReadAll;
59 mng_handle hMNG;
60 QImage image;
61 int elapsed;
62 int nextDelay;
63 int iterCount;
64 int frameIndex;
65 int nextIndex;
66 int frameCount;
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);
71 QMngHandlerPrivate(QMngHandler *q_ptr);
72 ~QMngHandlerPrivate();
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;
82 QMngHandler *q_ptr;
83};
84
85static 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
104static 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
115static void myfree(mng_ptr pPtr, mng_size_t /*iSize*/)
116{
117 free(pPtr);
118}
119
120static mng_bool myopenstream(mng_handle)
121{
122 return MNG_TRUE;
123}
124
125static 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
132static 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
141static 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
150static 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
158static 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
165static 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
174static mng_uint32 mygettickcount(mng_handle hMNG)
175{
176 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
177 return pMydata->elapsed++;
178}
179
180static 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
189static 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
201static 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
210QMngHandlerPrivate::QMngHandlerPrivate(QMngHandler *q_ptr)
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
235QMngHandlerPrivate::~QMngHandlerPrivate()
236{
237 mng_cleanup(&hMNG);
238}
239
240mng_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
247mng_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
254mng_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
263bool QMngHandlerPrivate::getNextImage(QImage *result)
264{
265 mng_retcode ret;
266 if (haveReadNone) {
267 haveReadNone = false;
268 ret = mng_readdisplay(hMNG);
269 } else {
270 ret = mng_display_resume(hMNG);
271 }
272 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
273 *result = image;
274 image.fill(0);
275 frameIndex = nextIndex++;
276 if (haveReadAll && (frameCount == 0))
277 frameCount = nextIndex;
278 return true;
279 }
280 return false;
281}
282
283bool QMngHandlerPrivate::writeImage(const QImage &image)
284{
285 mng_reset(hMNG);
286 if (mng_create(hMNG) != MNG_NOERROR)
287 return false;
288
289 this->image = image.convertToFormat(QImage::Format_ARGB32);
290 int w = image.width();
291 int h = image.height();
292
293 if (
294 // width, height, ticks, layercount, framecount, playtime, simplicity
295 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
296 // termination_action, action_after_iterations, delay, iteration_max
297 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
298 // width, height, bitdepth, colortype, compression, filter, interlace
299 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
300 // width, height, colortype, bitdepth, compression, filter, interlace, canvasstyle, getcanvasline
301 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
302 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
303 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
304 (mng_write(hMNG) == MNG_NOERROR)
305 )
306 return true;
307 return false;
308}
309
310int QMngHandlerPrivate::currentImageNumber() const
311{
312// return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
313 return frameIndex;
314}
315
316int QMngHandlerPrivate::imageCount() const
317{
318// return mng_get_totalframes(hMNG); not implemented, apparently
319 if (haveReadAll)
320 return frameCount;
321 return 0; // Don't know
322}
323
324bool QMngHandlerPrivate::jumpToImage(int imageNumber)
325{
326 if (imageNumber == nextIndex)
327 return true;
328
329 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
330 // Loop!
331 nextIndex = 0;
332 return true;
333 }
334 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
335 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
336 nextIndex = imageNumber;
337 return true;
338 }
339 }
340 return false;
341}
342
343bool QMngHandlerPrivate::jumpToNextImage()
344{
345 return jumpToImage((currentImageNumber()+1) % imageCount());
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 Q_D(QMngHandler);
379 delete d;
380}
381
382/*! \reimp */
383bool QMngHandler::canRead() const
384{
385 Q_D(const QMngHandler);
386 if (!d->haveReadNone)
387 return (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount)));
388
389 if (canRead(device())) {
390 setFormat("mng");
391 return true;
392 }
393 return false;
394}
395
396/*! \internal */
397bool QMngHandler::canRead(QIODevice *device)
398{
399 if (!device) {
400 qWarning("QMngHandler::canRead() called with no device");
401 return false;
402 }
403
404 return device->peek(8) == "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
405}
406
407/*! \reimp */
408QByteArray QMngHandler::name() const
409{
410 return "mng";
411}
412
413/*! \reimp */
414bool QMngHandler::read(QImage *image)
415{
416 Q_D(QMngHandler);
417 return canRead() ? d->getNextImage(image) : false;
418}
419
420/*! \reimp */
421bool QMngHandler::write(const QImage &image)
422{
423 Q_D(QMngHandler);
424 return d->writeImage(image);
425}
426
427/*! \reimp */
428int QMngHandler::currentImageNumber() const
429{
430 Q_D(const QMngHandler);
431 return d->currentImageNumber();
432}
433
434/*! \reimp */
435int QMngHandler::imageCount() const
436{
437 Q_D(const QMngHandler);
438 return d->imageCount();
439}
440
441/*! \reimp */
442bool QMngHandler::jumpToImage(int imageNumber)
443{
444 Q_D(QMngHandler);
445 return d->jumpToImage(imageNumber);
446}
447
448/*! \reimp */
449bool QMngHandler::jumpToNextImage()
450{
451 Q_D(QMngHandler);
452 return d->jumpToNextImage();
453}
454
455/*! \reimp */
456int QMngHandler::loopCount() const
457{
458 Q_D(const QMngHandler);
459 if (d->iterCount == 0x7FFFFFFF)
460 return -1; // infinite loop
461 return d->iterCount-1;
462}
463
464/*! \reimp */
465int QMngHandler::nextImageDelay() const
466{
467 Q_D(const QMngHandler);
468 return d->nextImageDelay();
469}
470
471/*! \reimp */
472QVariant QMngHandler::option(ImageOption option) const
473{
474 Q_D(const QMngHandler);
475 if (option == QImageIOHandler::Animation)
476 return true;
477 else if (option == QImageIOHandler::BackgroundColor)
478 return d->backgroundColor();
479 return QVariant();
480}
481
482/*! \reimp */
483void QMngHandler::setOption(ImageOption option, const QVariant & value)
484{
485 Q_D(QMngHandler);
486 if (option == QImageIOHandler::BackgroundColor)
487 d->setBackgroundColor(qVariantValue<QColor>(value));
488}
489
490/*! \reimp */
491bool QMngHandler::supportsOption(ImageOption option) const
492{
493 if (option == QImageIOHandler::Animation)
494 return true;
495 else if (option == QImageIOHandler::BackgroundColor)
496 return true;
497 return false;
498}
499
500QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.