/****************************************************************************
**
** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qpicture.h"
#include <private/qpicture_p.h>

#ifndef QT_NO_PICTURE

#include <private/qfactoryloader_p.h>
#include <private/qpaintengine_pic_p.h>

#include "qdatastream.h"
#include "qfile.h"
#include "qimage.h"
#include "qmutex.h"
#include "qpainter.h"
#include "qpainterpath.h"
#include "qpixmap.h"
#include "qregion.h"
#include "qdebug.h"

/*!
    \class QPicture qpicture.h
    \brief The QPicture class is a paint device that records and
    replays QPainter commands.

    \ingroup multimedia
    \ingroup shared
    \mainclass

    A picture serializes painter commands to an IO device in a
    platform-independent format. They are sometimes referred to as meta-files.

    Qt pictures use a proprietary binary format. Unlike native picture
    (meta-file) formats on many window systems, Qt pictures have no
    limitations regarding their contents. Everything that can be
    painted on a widget or pixmap (e.g., fonts, pixmaps, regions,
    transformed graphics, etc.)  can also be stored in a picture.

    QPicture is resolution independent, i.e. a QPicture can be
    displayed on different devices (for example svg, pdf, ps, printer
    and screen) looking the same. This is, for instance, needed for
    WYSIWYG print preview. QPicture runs in the default system dpi,
    and scales the painter to match differences in resolution
    depending on the window system.

    QPicture is an \link shclass.html implicitly shared\endlink class.

    Example of how to record a picture:
    \quotefromfile snippets/picture/picture.cpp
    \skipto RECORD
    \skipto QPicture
    \printuntil save

    Example of how to replay a picture:
    \quotefromfile snippets/picture/picture.cpp
    \skipto REPLAY
    \skipto QPicture
    \printuntil end();

    Pictures can also be drawn using play(). Some basic data about a
    picture is available, for example, size(), isNull() and
    boundingRect().

    \sa QMovie
*/

const char  *qt_mfhdr_tag = "QPIC"; // header tag
static const quint16 mfhdr_maj = 8; // major version #
static const quint16 mfhdr_min = 0; // minor version #
extern int qt_defaultDpi();

/*!
    Constructs an empty picture.

    The \a formatVersion parameter may be used to \e create a QPicture
    that can be read by applications that are compiled with earlier
    versions of Qt.

    Note that the default formatVersion is -1 which signifies the
    current release, i.e. for Qt 4.0 a formatVersion of 7 is the same
    as the default formatVersion of -1.

    Reading pictures generated by earlier versions of Qt is not
    supported in Qt 4.0.
*/

QPicture::QPicture(int formatVersion)
    : QPaintDevice(),
      d_ptr(new QPicturePrivate)
{
    Q_D(QPicture);
    d_ptr->q_ptr = this;
    d->paintEngine = 0;

    if (formatVersion == 0)
        qWarning("QPicture: invalid format version 0");

    // still accept the 0 default from before Qt 3.0.
    if (formatVersion > 0 && formatVersion != (int)mfhdr_maj) {
        d->formatMajor = formatVersion;
        d->formatMinor = 0;
        d->formatOk = false;
    }
    else {
        d->resetFormat();
    }
}

/*!
    Constructs a \link shclass.html shallow copy\endlink of \a pic.
*/

QPicture::QPicture(const QPicture &pic)
    : QPaintDevice(), d_ptr(pic.d_ptr)
{
    d_func()->ref.ref();
}

/*! \internal */
QPicture::QPicture(QPicturePrivate &dptr)
    : QPaintDevice(),
      d_ptr(&dptr)
{
    d_ptr->q_ptr = this;
}

/*!
    Destroys the picture.
*/
QPicture::~QPicture()
{
    if (!d_func()->ref.deref()) {
        delete d_func()->paintEngine;
        delete d_func();
    }
}

/*!
  \internal
*/
int QPicture::devType() const
{
    return QInternal::Picture;
}

/*!
    \fn bool QPicture::isNull() const

    Returns true if the picture contains no data; otherwise returns
    false.
*/

/*!
    \fn uint QPicture::size() const

    Returns the size of the picture data.

    \sa data()
*/

/*!
    \fn const char* QPicture::data() const

    Returns a pointer to the picture data. The pointer is only valid
    until the next non-const function is called on this picture. The
    returned pointer is 0 if the picture contains no data.

    \sa size(), isNull()
*/


bool QPicture::isNull() const
{
    return d_func()->pictb.buffer().isNull();
}

uint QPicture::size() const
{
    return d_func()->pictb.buffer().size();
}

const char* QPicture::data() const
{
    return d_func()->pictb.buffer();
}

void QPicture::detach()
{
    if (d_func()->ref != 1)
        detach_helper();
}

bool QPicture::isDetached() const
{
    return d_func()->ref == 1;
}

/*!
    Sets the picture data directly from \a data and \a size. This
    function copies the input data.

    \sa data(), size()
*/

void QPicture::setData(const char* data, uint size)
{
    detach();
    d_func()->pictb.setData(data, size);
    d_func()->resetFormat();                                // we'll have to check
}


/*!
    Loads a picture from the file specified by \a fileName and returns
    true if successful; otherwise returns false.

    By default, the file will be interpreted as being in the native
    QPicture format. Specifying the \a format string is optional and
    is only needed for importing picture data stored in a different
    format.

    \sa save()
*/

bool QPicture::load(const QString &fileName, const char *format)
{
    QFile f(fileName);
    if (!f.open(QIODevice::ReadOnly))
        return false;
    return load(&f, format);
}

/*!
    \overload

    \a dev is the device to use for loading.
*/

bool QPicture::load(QIODevice *dev, const char *format)
{
    if(format) {
#ifndef QT_NO_PICTUREIO
        QPictureIO io(dev, format);
        bool result = io.read();
        if (result) {
            operator=(io.picture());

        } else if (format)
#else
            bool result = false;
#endif
        {
            qWarning("QPicture::load: No such picture format: %s", format);
        }
        return result;
    }

    detach();
    QByteArray a = dev->readAll();

    d_func()->pictb.setData(a);                        // set byte array in buffer
    return d_func()->checkFormat();
}

/*!
    Saves a picture to the file specified by \a fileName and returns
    true if successful; otherwise returns false.

    Specifying the file \a format string is optional. By default the
    data will be saved in the native QPicture file format.

    \sa load()
*/

bool QPicture::save(const QString &fileName, const char *format)
{
    if (paintingActive()) {
        qWarning("QPicture::save: still being painted on. "
                  "Call QPainter::end() first");
        return false;
    }


    if(format) {
#ifndef QT_NO_PICTUREIO
        QPictureIO io(fileName, format);
        bool result = io.write();
        if (result) {
            operator=(io.picture());
        } else if (format)
#else
        bool result = false;
#endif
        {
            qWarning("QPicture::save: No such picture format: %s", format);
        }
        return result;
    }

    QFile f(fileName);
    if (!f.open(QIODevice::WriteOnly))
        return false;
    return save(&f, format);
}

/*!
    \overload

    \a dev is the device to use for saving.
*/

bool QPicture::save(QIODevice *dev, const char *format)
{
    if (paintingActive()) {
        qWarning("QPicture::save: still being painted on. "
                  "Call QPainter::end() first");
        return false;
    }

    if(format) {
#ifndef QT_NO_PICTUREIO
        QPictureIO io(dev, format);
        bool result = io.write();
        if (result) {
            operator=(io.picture());
        } else if (format)
#else
        bool result = false;
#endif
        {
            qWarning("QPicture::save: No such picture format: %s", format);
        }
        return result;
    }

    dev->write(d_func()->pictb.buffer(), d_func()->pictb.buffer().size());
    return true;
}

/*!
    Returns the picture's bounding rectangle or an invalid rectangle
    if the picture contains no data.
*/

QRect QPicture::boundingRect() const
{
    Q_D(const QPicture);
    // Use override rect where possible.
    if (!d->override_rect.isEmpty())
        return d->override_rect;

    if (!d->formatOk)
        d_ptr->checkFormat();

    return d->brect;
}

/*!
    Sets the picture's bounding rectangle to \a r. The automatically
    calculated value is overridden.
*/

void QPicture::setBoundingRect(const QRect &r)
{
    d_func()->override_rect = r;
}

/*!
    Replays the picture using \a painter, and returns true if
    successful; otherwise returns false.

    This function does exactly the same as QPainter::drawPicture()
    with (x, y) = (0, 0).
*/

bool QPicture::play(QPainter *painter)
{
    Q_D(QPicture);

    if (d->pictb.size() == 0)                        // nothing recorded
        return true;

    if (!d->formatOk && !d->checkFormat())
        return false;

    d->pictb.open(QIODevice::ReadOnly);                // open buffer device
    QDataStream s;
    s.setDevice(&d->pictb);                        // attach data stream to buffer
    s.device()->seek(10);                        // go directly to the data
    s.setVersion(d->formatMajor == 4 ? 3 : d->formatMajor);

    quint8  c, clen;
    quint32 nrecords;
    s >> c >> clen;
    Q_ASSERT(c == QPicturePrivate::PdcBegin);
    // bounding rect was introduced in ver 4. Read in checkFormat().
    if (d->formatMajor >= 4) {
        qint32 dummy;
        s >> dummy >> dummy >> dummy >> dummy;
    }
    s >> nrecords;
    if (!exec(painter, s, nrecords)) {
        qWarning("QPicture::play: Format error");
        d->pictb.close();
        return false;
    }
    d->pictb.close();
    return true;                                // no end-command
}


/*!
  \internal
  Iterates over the internal picture data and draws the picture using
  \a painter.
*/

bool QPicture::exec(QPainter *painter, QDataStream &s, int nrecords)
{
    Q_D(QPicture);
#if defined(QT_DEBUG)
    int                strm_pos;
#endif
    quint8     c;                      // command id
    quint8     tiny_len;               // 8-bit length descriptor
    qint32     len;                    // 32-bit length descriptor
    qint16     i_16, i1_16, i2_16;     // parameters...
    qint8      i_8;
    quint32    ul;
    QByteArray  str1;
    QString     str;
    QPointF     p, p1, p2;
    QRectF      r;
    QPolygonF   a;
    QPolygon    pa;
    QColor      color;
    QFont       font;
    QPen        pen;
    QBrush      brush;
    QRegion     rgn;
    QMatrix     matrix;

    QMatrix worldMatrix = painter->matrix();
    QMatrix oldWorldMatrix = worldMatrix;
    worldMatrix.scale(qreal(painter->device()->logicalDpiX()) / qreal(qt_defaultDpi()),
                      qreal(painter->device()->logicalDpiY()) / qreal(qt_defaultDpi()));
    painter->setMatrix(worldMatrix);

    while (nrecords-- && !s.atEnd()) {
        s >> c;                 // read cmd
        s >> tiny_len;          // read param length
        if (tiny_len == 255)    // longer than 254 bytes
            s >> len;
        else
            len = tiny_len;
#if defined(QT_DEBUG)
        strm_pos = s.device()->pos();
#endif
        switch (c) {            // exec cmd
        case QPicturePrivate::PdcNOP:
            break;
        case QPicturePrivate::PdcDrawPoint:
            s >> p;
            painter->drawPoint(p);
            break;
        case QPicturePrivate::PdcDrawPoints:
// ## implement me in the picture paint engine
//                 s >> a >> i1_32 >> i2_32;
//                 painter->drawPoints(a.mid(i1_32, i2_32));
            break;
        case QPicturePrivate::PdcDrawPath: {
            QPainterPath path;
            s >> path;
            painter->drawPath(path);
            break;
        }
        case QPicturePrivate::PdcDrawLine:
            s >> p1 >> p2;
            painter->drawLine(p1, p2);
            break;
        case QPicturePrivate::PdcDrawRect:
            s >> r;
            painter->drawRect(r);
            break;
        case QPicturePrivate::PdcDrawRoundRect:
            s >> r >> i1_16 >> i2_16;
            painter->drawRoundRect(r, i1_16, i2_16);
            break;
        case QPicturePrivate::PdcDrawEllipse:
            s >> r;
            painter->drawEllipse(r);
            break;
        case QPicturePrivate::PdcDrawArc:
            s >> r >> i1_16 >> i2_16;
            painter->drawArc(r, i1_16, i2_16);
            break;
        case QPicturePrivate::PdcDrawPie:
            s >> r >> i1_16 >> i2_16;
            painter->drawPie(r, i1_16, i2_16);
            break;
        case QPicturePrivate::PdcDrawChord:
            s >> r >> i1_16 >> i2_16;
            painter->drawChord(r, i1_16, i2_16);
            break;
        case QPicturePrivate::PdcDrawLineSegments:
            s >> pa;
            painter->drawLines(pa);
            pa.clear();
            break;
        case QPicturePrivate::PdcDrawPolyline:
            s >> a;
            painter->drawPolyline(a);
            a.clear();
            break;
        case QPicturePrivate::PdcDrawPolygon:
            s >> a >> i_8;
            painter->drawPolygon(a, i_8 ? Qt::WindingFill : Qt::OddEvenFill);
            a.clear();
            break;
        case QPicturePrivate::PdcDrawCubicBezier: {
            s >> a;
            QPainterPath path;
            Q_ASSERT(a.size() == 4);
            path.moveTo(a.at(0));
            path.cubicTo(a.at(1), a.at(2), a.at(3));
            painter->strokePath(path, painter->pen());
            a.clear();
        }
            break;
        case QPicturePrivate::PdcDrawText:
            s >> p >> str1;
            painter->drawText(p, str1);
            break;
        case QPicturePrivate::PdcDrawTextFormatted:
            s >> r >> i_16 >> str1;
            painter->drawText(r, i_16, str1);
            break;
        case QPicturePrivate::PdcDrawText2:
            s >> p >> str;
            painter->drawText(p, str);
            break;
        case QPicturePrivate::PdcDrawText2Formatted:
            s >> r >> i_16 >> str;
            painter->drawText(r, i_16, str);
            break;
        case QPicturePrivate::PdcDrawTextItem: {
            Qt::LayoutDirection oldDir = painter->layoutDirection();

            s >> p >> str >> font >> ul;

            if (ul & QTextItem::RightToLeft)
                painter->setLayoutDirection(Qt::RightToLeft);
            else
                painter->setLayoutDirection(Qt::LeftToRight);

            font = QFont(font, painter->device());

            QMatrix matrix = painter->matrix();
            painter->setMatrix(oldWorldMatrix);

            p.rx() *= painter->device()->logicalDpiX() / double(qt_defaultDpi());
            p.ry() *= painter->device()->logicalDpiY() / double(qt_defaultDpi());

            qt_format_text(font, QRectF(p, QSizeF(1, 1)), Qt::TextSingleLine | Qt::TextDontClip, str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);

            painter->setLayoutDirection(oldDir);
            painter->setMatrix(matrix);
            break;
        }
        case QPicturePrivate::PdcDrawPixmap: {
            QPixmap pixmap;
            if (d->formatMajor < 4) {
                s >> p >> pixmap;
                painter->drawPixmap(p, pixmap);
            } else {
                QRectF sr;
                s >> r >> pixmap >> sr;
                painter->drawPixmap(r, pixmap, sr);
            }
        }
            break;
        case QPicturePrivate::PdcDrawTiledPixmap: {
            QPixmap pixmap;
            s >> r >> pixmap >> p;
            painter->drawTiledPixmap(r, pixmap, p);
        }
            break;
        case QPicturePrivate::PdcDrawImage: {
            QImage image;
            if (d->formatMajor < 4) {
                s >> p >> image;
                painter->drawPixmap(p, QPixmap::fromImage(image));
            } else {
                s >> r >> image;
                painter->drawPixmap(r, QPixmap::fromImage(image), QRectF(0, 0, r.width(), r.height()));
            }
        }
            break;
        case QPicturePrivate::PdcBegin:
            s >> ul;                        // number of records
            if (!exec(painter, s, ul))
                return false;
            break;
        case QPicturePrivate::PdcEnd:
            if (nrecords == 0)
                return true;
            break;
        case QPicturePrivate::PdcSave:
            painter->save();
            break;
        case QPicturePrivate::PdcRestore:
            painter->restore();
            break;
        case QPicturePrivate::PdcSetBkColor:
            s >> color;
            painter->setBackground(color);
            break;
        case QPicturePrivate::PdcSetBkMode:
            s >> i_8;
            painter->setBackgroundMode((Qt::BGMode)i_8);
            break;
        case QPicturePrivate::PdcSetROP: // NOP
            break;
        case QPicturePrivate::PdcSetBrushOrigin:
            s >> p;
            painter->setBrushOrigin(p);
            break;
        case QPicturePrivate::PdcSetFont:
            s >> font;
            painter->setFont(font);
            break;
        case QPicturePrivate::PdcSetPen:
            s >> pen;
            painter->setPen(pen);
            break;
        case QPicturePrivate::PdcSetBrush:
            s >> brush;
            painter->setBrush(brush);
            break;
// #ifdef Q_Q3PAINTER
//         case QPicturePrivate::PdcSetTabStops:
//                 s >> i_16;
//                 painter->setTabStops(i_16);
//                 break;
//             case QPicturePrivate::PdcSetTabArray:
//                 s >> i_16;
//                 if (i_16 == 0) {
//                     painter->setTabArray(0);
//                 } else {
//                     int *ta = new int[i_16];
//                     for (int i=0; i<i_16; i++) {
//                         s >> i1_16;
//                         ta[i] = i1_16;
//                     }
//                     painter->setTabArray(ta);
//                     delete [] ta;
//                 }
//                 break;
// #endif
        case QPicturePrivate::PdcSetVXform:
            s >> i_8;
            painter->setViewTransformEnabled(i_8);
            break;
        case QPicturePrivate::PdcSetWindow:
            s >> r;
            painter->setWindow(r.toRect());
            break;
        case QPicturePrivate::PdcSetViewport:
            s >> r;
            painter->setViewport(r.toRect());
            break;
        case QPicturePrivate::PdcSetWXform:
            s >> i_8;
            painter->setMatrixEnabled(i_8);
            break;
        case QPicturePrivate::PdcSetWMatrix:
            s >> matrix >> i_8;
            matrix.setMatrix(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(),
                             matrix.dx() * painter->device()->logicalDpiX() / double(qt_defaultDpi()),
                             matrix.dy() * painter->device()->logicalDpiY() / double(qt_defaultDpi()));
            // i_8 is always false due to updateXForm() in qpaintengine_pic.cpp
            painter->setMatrix(worldMatrix * matrix, false);
            oldWorldMatrix *= matrix;
            break;
// #ifdef Q_Q3PAINTER
//             case QPicturePrivate::PdcSaveWMatrix:
//                 painter->saveWorldMatrix();
//                 break;
//             case QPicturePrivate::PdcRestoreWMatrix:
//                 painter->restoreWorldMatrix();
//                 break;
// #endif
        case QPicturePrivate::PdcSetClip:
            s >> i_8;
            painter->setClipping(i_8);
            break;
        case QPicturePrivate::PdcSetClipRegion:
            s >> rgn >> i_8;
            painter->setClipRegion(rgn);
            break;
        case QPicturePrivate::PdcSetClipPath:
            {
                QPainterPath path;
                s >> path >> i_8;
                painter->setClipPath(path, Qt::ClipOperation(i_8));
                break;
            }
        case QPicturePrivate::PdcSetRenderHint:
            s >> ul;
            painter->setRenderHint(QPainter::Antialiasing,
                                   bool(ul & QPainter::Antialiasing));
            painter->setRenderHint(QPainter::SmoothPixmapTransform,
                                   bool(ul & QPainter::SmoothPixmapTransform));
            break;
        default:
            qWarning("QPicture::play: Invalid command %d", c);
            if (len)                        // skip unknown command
                s.device()->seek(s.device()->pos()+len);
        }
#if defined(QT_DEBUG)
        //qDebug("device->at(): %i, strm_pos: %i len: %i", s.device()->at(), strm_pos, len);
        Q_ASSERT(qint32(s.device()->pos() - strm_pos) == len);
#endif
    }
    return false;
}

/*!
    Internal implementation of the virtual QPaintDevice::metric()
    function.

    A picture has the following hard-coded values: numcolors=16777216
    and depth=24.

    \a m is the metric to get.
*/

int QPicture::metric(PaintDeviceMetric m) const
{
    int val;
    QRect brect = boundingRect();
    switch (m) {
        case PdmWidth:
            val = brect.width();
            break;
        case PdmHeight:
            val = brect.height();
            break;
        case PdmWidthMM:
            val = int(25.4/qt_defaultDpi()*brect.width());
            break;
        case PdmHeightMM:
            val = int(25.4/qt_defaultDpi()*brect.height());
            break;
        case PdmDpiX:
        case PdmPhysicalDpiX:
            val = qt_defaultDpi();
            break;
        case PdmDpiY:
        case PdmPhysicalDpiY:
            val = qt_defaultDpi();
            break;
        case PdmNumColors:
            val = 16777216;
            break;
        case PdmDepth:
            val = 24;
            break;
        default:
            val = 0;
            qWarning("QPicture::metric: Invalid metric command");
    }
    return val;
}

/*!
    \fn void QPicture::detach()
    \internal
    Detaches from shared picture data and makes sure that this picture
    is the only one referring to the data.

    If multiple pictures share common data, this picture makes a copy
    of the data and detaches itself from the sharing mechanism.
    Nothing is done if there is just a single reference.
*/

/*! \fn bool QPicture::isDetached() const
\internal
*/
void QPicture::detach_helper()
{
    Q_D(QPicture);
    QPicturePrivate *x = new QPicturePrivate;
    int pictsize = size();
    x->pictb.setData(data(), pictsize);
    if (d->pictb.isOpen()) {
        x->pictb.open(d->pictb.openMode());
        x->pictb.seek(d->pictb.pos());
    }
    x->trecs = d->trecs;
    x->formatOk = d->formatOk;
    x->formatMinor = d->formatMinor;
    x->brect = d->brect;
    x->override_rect = d->override_rect;
    x = qAtomicSetPtr(&d_ptr, x);
    if (!x->ref.deref())
        delete x;
}

/*!
    Assigns picture \a p to this picture and returns a reference to
    this picture.
*/
QPicture& QPicture::operator=(const QPicture &p)
{
    qAtomicAssign<QPicturePrivate>(d_ptr, p.d_ptr);
    return *this;
}

/*!
  \internal

  Sets formatOk to false and resets the format version numbers to default
*/

void QPicturePrivate::resetFormat()
{
    formatOk = false;
    formatMajor = mfhdr_maj;
    formatMinor = mfhdr_min;
}


/*!
  \internal

  Checks data integrity and format version number. Set formatOk to
  true on success, to false otherwise. Returns the resulting formatOk
  value.
*/
bool QPicturePrivate::checkFormat()
{
    resetFormat();

    // can't check anything in an empty buffer
    if (pictb.size() == 0 || pictb.isOpen())
        return false;

    pictb.open(QIODevice::ReadOnly);                        // open buffer device
    QDataStream s;
    s.setDevice(&pictb);                        // attach data stream to buffer

    char mf_id[4];                                // picture header tag
    s.readRawData(mf_id, 4);                        // read actual tag
    if (memcmp(mf_id, qt_mfhdr_tag, 4) != 0) {         // wrong header id
        qWarning("QPicturePaintEngine::checkFormat: Incorrect header");
        pictb.close();
        return false;
    }

    int cs_start = sizeof(quint32);                // pos of checksum word
    int data_start = cs_start + sizeof(quint16);
    quint16 cs,ccs;
    QByteArray buf = pictb.buffer();        // pointer to data

    s >> cs;                                // read checksum
    ccs = (quint16) qChecksum(buf.constData() + data_start, buf.size() - data_start);
    if (ccs != cs) {
        qWarning("QPicturePaintEngine::checkFormat: Invalid checksum %x, %x expected",
                  ccs, cs);
        pictb.close();
        return false;
    }

    quint16 major, minor;
    s >> major >> minor;                        // read version number
    if (major > mfhdr_maj) {                // new, incompatible version
        qWarning("QPicturePaintEngine::checkFormat: Incompatible version %d.%d",
                  major, minor);
        pictb.close();
        return false;
    }
    s.setVersion(major != 4 ? major : 3);

    quint8  c, clen;
    s >> c >> clen;
    if (c == QPicturePrivate::PdcBegin) {
        if (!(major >= 1 && major <= 3)) {
            qint32 l, t, w, h;
            s >> l >> t >> w >> h;
            brect = QRect(l, t, w, h);
        }
    } else {
        qWarning("QPicturePaintEngine::checkFormat: Format error");
        pictb.close();
        return false;
    }
    pictb.close();

    formatOk = true;                        // picture seems to be ok
    formatMajor = major;
    formatMinor = minor;
    return true;
}

/*! \internal */
QPaintEngine *QPicture::paintEngine() const
{
    if (!d_func()->paintEngine)
        const_cast<QPicture*>(this)->d_func()->paintEngine = new QPicturePaintEngine;
    return d_func()->paintEngine;
}

/*****************************************************************************
  QPicture stream functions
 *****************************************************************************/

/*!
    \relates QPicture

    Writes picture \a r to the stream \a s and returns a reference to
    the stream.
*/

QDataStream &operator<<(QDataStream &s, const QPicture &r)
{
    quint32 size = r.d_func()->pictb.buffer().size();
    s << size;
    // null picture ?
    if (size == 0)
        return s;
    // just write the whole buffer to the stream
    s.writeRawData (r.d_func()->pictb.buffer(), r.d_func()->pictb.buffer().size());
    return s;
}

/*!
    \relates QPicture

    Reads a picture from the stream \a s into picture \a r and returns
    a reference to the stream.
*/

QDataStream &operator>>(QDataStream &s, QPicture &r)
{
    QDataStream sr;

    // "init"; this code is similar to the beginning of QPicture::cmd()
    sr.setDevice(&r.d_func()->pictb);
    sr.setVersion(r.d_func()->formatMajor);
    quint32 len;
    s >> len;
    QByteArray data;
    if (len > 0) {
        data.resize(len);
        s.readRawData(data.data(), len);
    }

    r.d_func()->pictb.setData(data);
    r.d_func()->resetFormat();
    return s;
}


#ifndef QT_NO_PICTUREIO
#include "qregexp.h"
#include "qapplication.h"
#include "qpictureformatplugin.h"

/*!
    Returns a string that specifies the picture format of the file \a
    fileName, or 0 if the file cannot be read or if the format is not
    recognized.

    The QPictureIO documentation lists the guaranteed supported picture
    formats, or use QPicture::inputFormats() and QPicture::outputFormats()
    to get lists that include the installed formats.

    \sa load() save()
*/

const char* QPicture::pictureFormat(const QString &fileName)
{
    return QPictureIO::pictureFormat(fileName);
}

/*!
    Returns a list of picture formats that are supported for picture
    input.

    \sa outputFormats() inputFormatList() QPictureIO
*/
QList<QByteArray> QPicture::inputFormats()
{
    return QPictureIO::inputFormats();
}

static QStringList qToStringList(const QList<QByteArray> arr)
{
    QStringList list;
    for (int i = 0; i < arr.count(); ++i)
        list.append(QString(arr.at(i)));
    return list;
}

/*!
    Returns a list of picture formats that are supported for picture
    input.

    Note that if you want to iterate over the list, you should iterate
    over a copy, e.g.
    \quotefromfile snippets/picture/picture.cpp
    \skipto FORMATS
    \skipto QStringList
    \printuntil myProcessing

    \sa outputFormatList() inputFormats() QPictureIO
*/
QStringList QPicture::inputFormatList()
{
    return qToStringList(QPictureIO::inputFormats());
}


/*!
    Returns a list of picture formats that are supported for picture
    output.

    Note that if you want to iterate over the list, you should iterate
    over a copy, e.g.
    \quotefromfile snippets/picture/picture.cpp
    \skipto OUTPUT
    \skipto QStringList
    \printuntil myProcessing

    \sa inputFormatList() outputFormats() QPictureIO
*/
QStringList QPicture::outputFormatList()
{
    return qToStringList(QPictureIO::outputFormats());
}

/*!
    Returns a list of picture formats that are supported for picture
    output.

    \sa inputFormats() outputFormatList() QPictureIO
*/
QList<QByteArray> QPicture::outputFormats()
{
    return QPictureIO::outputFormats();
}

/*****************************************************************************
  QPictureIO member functions
 *****************************************************************************/

/*!
    \class QPictureIO qpicture.h

    \brief The QPictureIO class contains parameters for loading and
    saving pictures.

    \ingroup multimedia
    \ingroup io

    QPictureIO contains a QIODevice object that is used for picture data
    I/O. The programmer can install new picture file formats in addition
    to those that Qt provides.

    You don't normally need to use this class; QPicture::load(),
    QPicture::save().

    \sa QPicture QPixmap QFile
*/

struct QPictureIOData
{
    QPicture        pi;                                // picture
    int                iostat;                                // IO status
    QByteArray        frmt;                                // picture format
    QIODevice  *iodev;                                // IO device
    QString        fname;                                // file name
    QString     descr;                                // picture description
    const char *parameters;
    int quality;
    float gamma;
};

/*!
    Constructs a QPictureIO object with all parameters set to zero.
*/

QPictureIO::QPictureIO()
{
    init();
}

/*!
    Constructs a QPictureIO object with the I/O device \a ioDevice and a
    \a format tag.
*/

QPictureIO::QPictureIO(QIODevice *ioDevice, const char *format)
{
    init();
    d->iodev = ioDevice;
    d->frmt = format;
}

/*!
    Constructs a QPictureIO object with the file name \a fileName and a
    \a format tag.
*/

QPictureIO::QPictureIO(const QString &fileName, const char* format)
{
    init();
    d->frmt = format;
    d->fname = fileName;
}

/*!
    Contains initialization common to all QPictureIO constructors.
*/

void QPictureIO::init()
{
    d = new QPictureIOData();
    d->parameters = 0;
    d->quality = -1; // default quality of the current format
    d->gamma=0.0f;
    d->iostat = 0;
    d->iodev  = 0;
}

/*!
    Destroys the object and all related data.
*/

QPictureIO::~QPictureIO()
{
    if (d->parameters)
        delete [] (char*)d->parameters;
    delete d;
}


/*****************************************************************************
  QPictureIO picture handler functions
 *****************************************************************************/

class QPictureHandler
{
public:
    QPictureHandler(const char *f, const char *h, const QByteArray& fl,
                     picture_io_handler r, picture_io_handler w);
    QByteArray              format;                        // picture format
    QRegExp              header;                        // picture header pattern
    enum TMode { Untranslated=0, TranslateIn, TranslateInOut } text_mode;
    picture_io_handler  read_picture;                // picture read function
    picture_io_handler  write_picture;                // picture write function
    bool              obsolete;                        // support not "published"
};

QPictureHandler::QPictureHandler(const char *f, const char *h, const QByteArray& fl,
                              picture_io_handler r, picture_io_handler w)
    : format(f), header(QString::fromLatin1(h))
{
    text_mode = Untranslated;
    if (fl.contains('t'))
        text_mode = TranslateIn;
    else if (fl.contains('T'))
        text_mode = TranslateInOut;
    obsolete = fl.contains('O');
    read_picture  = r;
    write_picture = w;
}

typedef QList<QPictureHandler *> QPHList;
static QPHList pictureHandlers;

#ifndef QT_NO_LIBRARY
Q_GLOBAL_STATIC(QMutex, mutex)
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
                          (QPictureFormatInterface_iid,
                           QCoreApplication::libraryPaths(),
                           "/pictureformats"))
#endif
void qt_init_picture_plugins()
{
#ifndef QT_NO_LIBRARY
    QMutexLocker locker(mutex());
    QFactoryLoader *loader = ::loader();
    QStringList keys = loader->keys();
    for (int i = 0; i < keys.count(); ++i)
        if (QPictureFormatInterface *format = qobject_cast<QPictureFormatInterface*>(loader->instance(keys.at(i))))
            format->installIOHandler(keys.at(i));
#endif
}

static void cleanup()
{
    // make sure that picture handlers are delete before plugin manager
    while (!pictureHandlers.isEmpty())
        delete pictureHandlers.takeFirst();
}

void qt_init_picture_handlers()                // initialize picture handlers
{
    static bool done = false;
    if (done) return;
    done = true;

    qAddPostRoutine(cleanup);
}

static QPictureHandler *get_picture_handler(const char *format)
{                                                // get pointer to handler
    qt_init_picture_handlers();
    qt_init_picture_plugins();
    for (int i = 0; i < pictureHandlers.size(); ++i) {
        if (pictureHandlers.at(i)->format == format)
            return pictureHandlers.at(i);
    }
    return 0;                                        // no such handler
}


/*!
    Defines a picture I/O handler for the picture format called \a
    format, which is recognized using the regular
    expression defined in \a header, read using \a readPicture and
    written using \a writePicture.

    \a flags is a string of single-character flags for this format.
    The only flag defined currently is T (upper case), so the only
    legal value for \a flags are "T" and the empty string. The "T"
    flag means that the picture file is a text file, and Qt should treat
    all newline conventions as equivalent. (XPM files and some PPM
    files are text files for example.)

    \a format is used to select a handler to write a QPicture; \a header
    is used to select a handler to read an picture file.

    If \a readPicture is a null pointer, the QPictureIO will not be able
    to read pictures in \a format. If \a writePicture is a null pointer,
    the QPictureIO will not be able to write pictures in \a format. If
    both are null, the QPictureIO object is valid but useless.

    Example:
    \quotefromfile snippets/picture/picture.cpp
    \skipto SVG READ
    \skipto readSVG
    \printuntil }
    \skipto SVG WRITE
    \skipto writeSVG
    \printuntil }
    \skipto USE SVG
    \skipto add
    \printuntil ...

    Before the regulare expression test, all the 0 bytes in the file header are
    converted to 1 bytes. This is done because when Qt was ASCII-based, QRegExp
    could not handle 0 bytes in strings.

    The regexp is only applied on the first 14 bytes of the file.

    (Note that if one handlerIO supports writing a format and another
    supports reading it, Qt supports both reading and writing. If two
    handlers support the same operation, Qt chooses one arbitrarily.)
*/

void QPictureIO::defineIOHandler(const char *format,
                                const char *header,
                                const char *flags,
                                picture_io_handler readPicture,
                                picture_io_handler writePicture)
{
    qt_init_picture_handlers();
    QPictureHandler *p;
    p = new QPictureHandler(format, header, QByteArray(flags), readPicture, writePicture);
    pictureHandlers.prepend(p);
}


/*****************************************************************************
  QPictureIO normal member functions
 *****************************************************************************/

/*!
    Returns the picture currently set.

    \sa setPicture()
*/
const QPicture &QPictureIO::picture() const { return d->pi; }

/*!
    Returns the picture's IO status. A non-zero value indicates an
    error, whereas 0 means that the IO operation was successful.

    \sa setStatus()
*/
int QPictureIO::status() const { return d->iostat; }

/*!
    Returns the picture format string or 0 if no format has been
    explicitly set.
*/
const char *QPictureIO::format() const { return d->frmt; }

/*!
    Returns the IO device currently set.

    \sa setIODevice()
*/
QIODevice *QPictureIO::ioDevice() const { return d->iodev; }

/*!
    Returns the file name currently set.

    \sa setFileName()
*/
QString QPictureIO::fileName() const { return d->fname; }


/*!
    Returns the picture description string.

    \sa setDescription()
*/
QString QPictureIO::description() const { return d->descr; }

/*!
    Sets the picture to \a picture.

    \sa picture()
*/
void QPictureIO::setPicture(const QPicture &picture)
{
    d->pi = picture;
}

/*!
    Sets the picture IO status to \a status. A non-zero value indicates
    an error, whereas 0 means that the IO operation was successful.

    \sa status()
*/
void QPictureIO::setStatus(int status)
{
    d->iostat = status;
}

/*!
    Sets the picture format to \a format for the picture to be read or
    written.

    It is necessary to specify a format before writing an picture, but
    it is not necessary to specify a format before reading an picture.

    If no format has been set, Qt guesses the picture format before
    reading it. If a format is set the picture will only be read if it
    has that format.

    \sa read() write() format()
*/
void QPictureIO::setFormat(const char *format)
{
    d->frmt = format;
}

/*!
    Sets the IO device to be used for reading or writing an picture.

    Setting the IO device allows pictures to be read/written to any
    block-oriented QIODevice.

    If \a ioDevice is not null, this IO device will override file name
    settings.

    \sa setFileName()
*/
void QPictureIO::setIODevice(QIODevice *ioDevice)
{
    d->iodev = ioDevice;
}

/*!
    Sets the name of the file to read or write an picture from to \a
    fileName.

    \sa setIODevice()
*/
void QPictureIO::setFileName(const QString &fileName)
{
    d->fname = fileName;
}

/*!
    Returns the quality of the written picture, related to the
    compression ratio.

    \sa setQuality() QPicture::save()
*/
int QPictureIO::quality() const
{
    return d->quality;
}

/*!
    Sets the quality of the written picture to \a q, related to the
    compression ratio.

    \a q must be in the range -1..100. Specify 0 to obtain small
    compressed files, 100 for large uncompressed files. (-1 signifies
    the default compression.)

    \sa quality() QPicture::save()
*/

void QPictureIO::setQuality(int q)
{
    d->quality = q;
}

/*!
    Returns the picture's parameters string.

    \sa setParameters()
*/

const char *QPictureIO::parameters() const
{
    return d->parameters;
}

/*!
    Sets the picture's parameter string to \a parameters. This is for
    picture handlers that require special parameters.

    Although the current picture formats supported by Qt ignore the
    parameters string, it may be used in future extensions or by
    contributions (for example, JPEG).

    \sa parameters()
*/

void QPictureIO::setParameters(const char *parameters)
{
    if (d->parameters)
        delete [] (char*)d->parameters;
    d->parameters = qstrdup(parameters);
}

/*!
    Sets the gamma value at which the picture will be viewed to \a
    gamma. If the picture format stores a gamma value for which the
    picture is intended to be used, then this setting will be used to
    modify the picture. Setting to 0.0 will disable gamma correction
    (i.e. any specification in the file will be ignored).

    The default value is 0.0.

    \sa gamma()
*/
void QPictureIO::setGamma(float gamma)
{
    d->gamma=gamma;
}

/*!
    Returns the gamma value at which the picture will be viewed.

    \sa setGamma()
*/
float QPictureIO::gamma() const
{
    return d->gamma;
}

/*!
    Sets the picture description string for picture handlers that support
    picture descriptions to \a description.

    Currently, no picture format supported by Qt uses the description
    string.
*/

void QPictureIO::setDescription(const QString &description)
{
    d->descr = description;
}


/*!
    Returns a string that specifies the picture format of the file \a
    fileName, or null if the file cannot be read or if the format is
    not recognized.
*/

QByteArray QPictureIO::pictureFormat(const QString &fileName)
{
    QFile file(fileName);
    QByteArray format;
    if (!file.open(QIODevice::ReadOnly))
        return format;
    format = pictureFormat(&file);
    file.close();
    return format;
}

/*!
    \overload

    Returns a string that specifies the picture format of the picture read
    from IO device \a d, or 0 if the device cannot be read or if the
    format is not recognized.

    Make sure that \a d is at the right position in the device (for
    example, at the beginning of the file).

    \sa QIODevice::at()
*/

QByteArray QPictureIO::pictureFormat(QIODevice *d)
{
    // if you change this change the documentation for defineIOHandler()
    const int buflen = 14;

    char buf[buflen];
    char buf2[buflen];
    qt_init_picture_handlers();
    qt_init_picture_plugins();
    int pos = d->pos();                      // save position
    int rdlen = d->read(buf, buflen);        // read a few bytes

    QByteArray format;
    if (rdlen != buflen)
        return format;

    memcpy(buf2, buf, sizeof(char)*buflen);

    for (int n = 0; n < rdlen; n++)
        if (buf[n] == '\0')
            buf[n] = '\001';
    if (rdlen > 0) {
        buf[rdlen - 1] = '\0';
        QString bufStr = QString::fromLatin1(buf);
        for (int i = 0; i < pictureHandlers.size(); ++i) {
            if (pictureHandlers.at(i)->header.indexIn(bufStr) != -1) { // try match with headers
                format = pictureHandlers.at(i)->format;
                break;
            }
        }
    }
    d->seek(pos);                                // restore position
    return format;
}

/*!
    Returns a sorted list of picture formats that are supported for
    picture input.
*/
QList<QByteArray> QPictureIO::inputFormats()
{
    QList<QByteArray> result;

    qt_init_picture_handlers();
    qt_init_picture_plugins();

    for (int i = 0; i < pictureHandlers.size(); ++i) {
        QPictureHandler *p = pictureHandlers.at(i);
        if (p->read_picture && !p->obsolete  && !result.contains(p->format))
            result.append(p->format);
    }
    qSort(result);

    return result;
}

/*!
    Returns a sorted list of picture formats that are supported for
    picture output.
*/
QList<QByteArray> QPictureIO::outputFormats()
{
    qt_init_picture_handlers();
    qt_init_picture_plugins();

    QList<QByteArray> result;
    for (int i = 0; i < pictureHandlers.size(); ++i) {
        QPictureHandler *p = pictureHandlers.at(i);
        if (p->write_picture && !p->obsolete && !result.contains(p->format))
            result.append(p->format);
    }
    return result;
}



/*!
    Reads an picture into memory and returns true if the picture was
    successfully read; otherwise returns false.

    Before reading an picture you must set an IO device or a file name.
    If both an IO device and a file name have been set, the IO device
    will be used.

    Setting the picture file format string is optional.

    Note that this function does \e not set the \link format()
    format\endlink used to read the picture. If you need that
    information, use the pictureFormat() static functions.

    Example:

    \quotefromfile snippets/picture/picture.cpp
    \skipto PIC
    \skipto QPictureIO
    \printuntil }

    \sa setIODevice() setFileName() setFormat() write() QPixmap::load()
*/
bool QPictureIO::read()
{
    QFile           file;
    const char          *picture_format;
    QPictureHandler *h;

    if (d->iodev) {                                // read from io device
        // ok, already open
    } else if (!d->fname.isEmpty()) {                // read from file
        file.setFileName(d->fname);
        if (!file.open(QIODevice::ReadOnly))
            return false;                        // cannot open file
        d->iodev = &file;
    } else {                                        // no file name or io device
        return false;
    }
    if (d->frmt.isEmpty()) {
        // Try to guess format
        picture_format = pictureFormat(d->iodev);        // get picture format
        if (!picture_format) {
            if (file.isOpen()) {                        // unknown format
                file.close();
                d->iodev = 0;
            }
            return false;
        }
    } else {
        picture_format = d->frmt;
    }

    h = get_picture_handler(picture_format);
    if (file.isOpen()) {
#if !defined(Q_OS_UNIX)
        if (h && h->text_mode) {                // reopen in translated mode
            file.close();
            file.open(QIODevice::ReadOnly | QIODevice::Text);
        }
        else
#endif
            file.seek(0);                        // position to start
    }
    d->iostat = 1;                                        // assume error

    if (h && h->read_picture)
        (*h->read_picture)(this);

    if (file.isOpen()) {                        // picture was read using file
        file.close();
        d->iodev = 0;
    }
    return d->iostat == 0;                                // picture successfully read?
}


/*!
    Writes an picture to an IO device and returns true if the picture was
    successfully written; otherwise returns false.

    Before writing an picture you must set an IO device or a file name.
    If both an IO device and a file name have been set, the IO device
    will be used.

    The picture will be written using the specified picture format.

    Example:
    \quotefromfile snippets/picture/picture.cpp
    \skipto PIC WRITE
    \skipto QPictureIO
    \printuntil returned

    \sa setIODevice() setFileName() setFormat() read() QPixmap::save()
*/
bool QPictureIO::write()
{
    if (d->frmt.isEmpty())
        return false;
    QPictureHandler *h = get_picture_handler(d->frmt);
    if (!h || !h->write_picture) {
        qWarning("QPictureIO::write: No such picture format handler: %s",
                 format());
        return false;
    }
    QFile file;
    if (!d->iodev && !d->fname.isEmpty()) {
        file.setFileName(d->fname);
        bool translate = h->text_mode==QPictureHandler::TranslateInOut;
        QIODevice::OpenMode fmode = translate ? QIODevice::WriteOnly | QIODevice::Text : QIODevice::OpenMode(QIODevice::WriteOnly);
        if (!file.open(fmode))                // couldn't create file
            return false;
        d->iodev = &file;
    }
    d->iostat = 1;
    (*h->write_picture)(this);
    if (file.isOpen()) {                        // picture was written using file
        file.close();
        d->iodev = 0;
    }
    return d->iostat == 0;                                // picture successfully written?
}
#endif //QT_NO_PICTUREIO

/*!
    \fn QPicture QPicture::copy() const

    Use simple assignment instead.
*/

#endif // QT_NO_PICTURE
