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 if (pMydata->image.isNull() || iLinenr >= mng_uint32(pMydata->image.height()))
122 return (mng_ptr)pMydata->image.scanLine(iLinenr);
136 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
137 return pMydata->elapsed++;
143 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
144 pMydata->elapsed += iMsecs;
145 pMydata->nextDelay = iMsecs;
150 mng_uint8 iTermaction,
155 QMngHandlerPrivate *pMydata =
reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
156 if (iTermaction == 3)
157 pMydata->iterCount = qMin<mng_uint32>(iItermax, 0x7FFFFFFF);
166 qDebug(
"mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
171 : haveReadNone(
true), haveReadAll(
false), elapsed(0), nextDelay(0), iterCount(1),
172 frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
174 iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
176 hMNG = mng_initialize((mng_ptr)
this,
myalloc,
myfree, mytrace);
179 mng_setcb_errorproc(hMNG, myerror);
182 mng_setcb_readdata(hMNG, myreaddata);
183 mng_setcb_writedata(hMNG, mywritedata);
184 mng_setcb_processheader(hMNG, myprocessheader);
185 mng_setcb_getcanvasline(hMNG, mygetcanvasline);
186 mng_setcb_refresh(hMNG, myrefresh);
187 mng_setcb_gettickcount(hMNG, mygettickcount);
188 mng_setcb_settimer(hMNG, mysettimer);
189 mng_setcb_processterm(hMNG, myprocessterm);
190 mng_set_doprogressive(hMNG, MNG_FALSE);
191 mng_set_suspensionmode(hMNG, MNG_TRUE);
195QMngHandlerPrivate::~QMngHandlerPrivate()
200mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
203 const qint64 nRead = q->device()->read((
char *)pBuf, iSize);
204 *pRead = (nRead > 0) ? mng_uint32(nRead) : 0;
205 return (nRead > 0) ? MNG_TRUE : MNG_FALSE;
208mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
211 const qint64 nWritten = q->device()->write((
char *)pBuf, iSize);
216 *pWritten = mng_uint32(nWritten);
220mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
222 if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
224 if (!QImageIOHandler::allocateImage(QSize(iWidth, iHeight), QImage::Format_ARGB32, &image))
230bool QMngHandlerPrivate::getNextImage(QImage *result)
233 const bool savedHaveReadAll = haveReadAll;
235 haveReadNone =
false;
236 ret = mng_readdisplay(hMNG);
238 ret = mng_display_resume(hMNG);
240 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
245 if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
246 ret = mng_display_resume(hMNG);
249 frameIndex = nextIndex++;
250 if (haveReadAll && (frameCount == 0))
251 frameCount = nextIndex;
257bool QMngHandlerPrivate::writeImage(
const QImage &image)
260 if (mng_create(hMNG) != MNG_NOERROR)
263 this->image = image.convertToFormat(QImage::Format_ARGB32);
264 int w = image.width();
265 int h = image.height();
269 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
271 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
273 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
275 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
276 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
277 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
278 (mng_write(hMNG) == MNG_NOERROR)
284int QMngHandlerPrivate::currentImageNumber()
const
290int QMngHandlerPrivate::imageCount()
const
298bool QMngHandlerPrivate::jumpToImage(
int imageNumber)
300 if (imageNumber == nextIndex)
303 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
308 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
309 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
310 nextIndex = imageNumber;
317bool QMngHandlerPrivate::jumpToNextImage()
319 const int numImages = imageCount();
320 return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
323int QMngHandlerPrivate::nextImageDelay()
const
328bool QMngHandlerPrivate::setBackgroundColor(
const QColor &color)
330 mng_uint16 iRed = (mng_uint16)(color.red() << 8);
331 mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
332 mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
333 return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
336QColor QMngHandlerPrivate::backgroundColor()
const
341 if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
342 return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
358 Q_D(
const QMngHandler);
359 if ((!d->haveReadNone
360 && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
361 || canRead(device()))
373 qWarning(
"QMngHandler::canRead() called with no device");
377 return device->peek(8) ==
"\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
384 return canRead() ? d->getNextImage(image) :
false;
391 return d->writeImage(image);
397 Q_D(
const QMngHandler);
398 return d->currentImageNumber();
404 Q_D(
const QMngHandler);
405 return d->imageCount();
412 return d->jumpToImage(imageNumber);
419 return d->jumpToNextImage();
425 Q_D(
const QMngHandler);
426 if (d->iterCount == 0x7FFFFFFF)
428 return d->iterCount-1;
434 Q_D(
const QMngHandler);
435 return d->nextImageDelay();
441 Q_D(
const QMngHandler);
442 if (option == QImageIOHandler::Animation)
444 else if (option == QImageIOHandler::BackgroundColor)
445 return d->backgroundColor();
450void QMngHandler::setOption(ImageOption option,
const QVariant & value)
453 if (option == QImageIOHandler::BackgroundColor)
454 d->setBackgroundColor(qvariant_cast<QColor>(value));
460 if (option == QImageIOHandler::Animation)
462 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)