12#define MNG_NO_INCLUDE_JNG
17class QMngHandlerPrivate
19 Q_DECLARE_PUBLIC(QMngHandler)
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;
49static mng_bool MNG_DECL
myerror(mng_handle ,
52 mng_chunkid iChunkname,
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,
68static mng_ptr MNG_DECL
myalloc(mng_size_t iSize)
70 return (mng_ptr)calloc(1, iSize);
73static void MNG_DECL
myfree(mng_ptr pPtr, mng_size_t )
85 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
86 pMydata->haveReadAll =
true;
95 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
96 return pMydata->readData(pBuf, iSize, pRead);
102 mng_uint32p pWritten)
104 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
105 return pMydata->writeData(pBuf, iSize, pWritten);
112 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
113 return pMydata->processHeader(iWidth, iHeight);
119 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
120 return (mng_ptr)pMydata->image.scanLine(iLinenr);
134 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
135 return pMydata->elapsed++;
141 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
142 pMydata->elapsed += iMsecs;
143 pMydata->nextDelay = iMsecs;
148 mng_uint8 iTermaction,
153 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
154 if (iTermaction == 3)
155 pMydata->iterCount = iItermax;
164 qDebug(
"mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
169 : haveReadNone(
true), haveReadAll(
false), elapsed(0), nextDelay(0), iterCount(1),
170 frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
172 iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
174 hMNG = mng_initialize((mng_ptr)
this,
myalloc,
myfree, mytrace);
177 mng_setcb_errorproc(hMNG, myerror);
180 mng_setcb_readdata(hMNG, myreaddata);
181 mng_setcb_writedata(hMNG, mywritedata);
182 mng_setcb_processheader(hMNG, myprocessheader);
183 mng_setcb_getcanvasline(hMNG, mygetcanvasline);
184 mng_setcb_refresh(hMNG, myrefresh);
185 mng_setcb_gettickcount(hMNG, mygettickcount);
186 mng_setcb_settimer(hMNG, mysettimer);
187 mng_setcb_processterm(hMNG, myprocessterm);
188 mng_set_doprogressive(hMNG, MNG_FALSE);
189 mng_set_suspensionmode(hMNG, MNG_TRUE);
193QMngHandlerPrivate::~QMngHandlerPrivate()
198mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
201 *pRead = q->device()->read((
char *)pBuf, iSize);
202 return (*pRead > 0) ? MNG_TRUE : MNG_FALSE;
205mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
208 *pWritten = q->device()->write((
char *)pBuf, iSize);
212mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
214 if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
216 if (!QImageIOHandler::allocateImage(QSize(iWidth, iHeight), QImage::Format_ARGB32, &image))
222bool QMngHandlerPrivate::getNextImage(QImage *result)
225 const bool savedHaveReadAll = haveReadAll;
227 haveReadNone =
false;
228 ret = mng_readdisplay(hMNG);
230 ret = mng_display_resume(hMNG);
232 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
237 if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
238 ret = mng_display_resume(hMNG);
241 frameIndex = nextIndex++;
242 if (haveReadAll && (frameCount == 0))
243 frameCount = nextIndex;
249bool QMngHandlerPrivate::writeImage(
const QImage &image)
252 if (mng_create(hMNG) != MNG_NOERROR)
255 this->image = image.convertToFormat(QImage::Format_ARGB32);
256 int w = image.width();
257 int h = image.height();
261 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
263 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
265 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
267 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
268 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
269 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
270 (mng_write(hMNG) == MNG_NOERROR)
276int QMngHandlerPrivate::currentImageNumber()
const
282int QMngHandlerPrivate::imageCount()
const
290bool QMngHandlerPrivate::jumpToImage(
int imageNumber)
292 if (imageNumber == nextIndex)
295 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
300 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
301 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
302 nextIndex = imageNumber;
309bool QMngHandlerPrivate::jumpToNextImage()
311 const int numImages = imageCount();
312 return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
315int QMngHandlerPrivate::nextImageDelay()
const
320bool QMngHandlerPrivate::setBackgroundColor(
const QColor &color)
322 mng_uint16 iRed = (mng_uint16)(color.red() << 8);
323 mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
324 mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
325 return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
328QColor QMngHandlerPrivate::backgroundColor()
const
333 if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
334 return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
350 Q_D(
const QMngHandler);
351 if ((!d->haveReadNone
352 && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
353 || canRead(device()))
365 qWarning(
"QMngHandler::canRead() called with no device");
369 return device->peek(8) ==
"\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
376 return canRead() ? d->getNextImage(image) :
false;
383 return d->writeImage(image);
389 Q_D(
const QMngHandler);
390 return d->currentImageNumber();
396 Q_D(
const QMngHandler);
397 return d->imageCount();
404 return d->jumpToImage(imageNumber);
411 return d->jumpToNextImage();
417 Q_D(
const QMngHandler);
418 if (d->iterCount == 0x7FFFFFFF)
420 return d->iterCount-1;
426 Q_D(
const QMngHandler);
427 return d->nextImageDelay();
433 Q_D(
const QMngHandler);
434 if (option == QImageIOHandler::Animation)
436 else if (option == QImageIOHandler::BackgroundColor)
437 return d->backgroundColor();
442void QMngHandler::setOption(ImageOption option,
const QVariant & value)
445 if (option == QImageIOHandler::BackgroundColor)
446 d->setBackgroundColor(qvariant_cast<QColor>(value));
452 if (option == QImageIOHandler::Animation)
454 else if (option == QImageIOHandler::BackgroundColor)
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)