diff options
Diffstat (limited to 'src/gui/image/qpicture.cpp')
-rw-r--r-- | src/gui/image/qpicture.cpp | 832 |
1 files changed, 6 insertions, 826 deletions
diff --git a/src/gui/image/qpicture.cpp b/src/gui/image/qpicture.cpp index 8548f1857e..84de91353a 100644 --- a/src/gui/image/qpicture.cpp +++ b/src/gui/image/qpicture.cpp @@ -263,14 +263,14 @@ void QPicture::setData(const char* data, uint size) \sa save() */ -bool QPicture::load(const QString &fileName, const char *format) +bool QPicture::load(const QString &fileName) { QFile f(fileName); if (!f.open(QIODevice::ReadOnly)) { operator=(QPicture()); return false; } - return load(&f, format); + return load(&f); } /*! @@ -279,21 +279,8 @@ bool QPicture::load(const QString &fileName, const char *format) \a dev is the device to use for loading. */ -bool QPicture::load(QIODevice *dev, const char *format) +bool QPicture::load(QIODevice *dev) { - if(format) { -#ifndef QT_NO_PICTUREIO - QPictureIO io(dev, format); - if (io.read()) { - operator=(io.picture()); - return true; - } -#endif - qWarning("QPicture::load: No such picture format: %s", format); - operator=(QPicture()); - return false; - } - detach(); QByteArray a = dev->readAll(); @@ -311,7 +298,7 @@ bool QPicture::load(QIODevice *dev, const char *format) \sa load() */ -bool QPicture::save(const QString &fileName, const char *format) +bool QPicture::save(const QString &fileName) { if (paintingActive()) { qWarning("QPicture::save: still being painted on. " @@ -319,27 +306,10 @@ bool QPicture::save(const QString &fileName, const char *format) 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); + return save(&f); } /*! @@ -348,7 +318,7 @@ bool QPicture::save(const QString &fileName, const char *format) \a dev is the device to use for saving. */ -bool QPicture::save(QIODevice *dev, const char *format) +bool QPicture::save(QIODevice *dev) { if (paintingActive()) { qWarning("QPicture::save: still being painted on. " @@ -356,22 +326,6 @@ bool QPicture::save(QIODevice *dev, const char *format) 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; } @@ -1188,780 +1142,6 @@ QDataStream &operator>>(QDataStream &s, QPicture &r) } #endif // QT_NO_DATASTREAM - -#ifndef QT_NO_PICTUREIO - -QT_BEGIN_INCLUDE_NAMESPACE -#include "qregexp.h" -#include "qpictureformatplugin.h" -QT_END_INCLUDE_NAMESPACE - -#if QT_DEPRECATED_SINCE(5, 10) -/*! - \obsolete - - Returns a string that specifies the picture format of the file \a - fileName, or \nullptr if the file cannot be read or if the format - is not recognized. - - \sa load(), save() -*/ - -const char* QPicture::pictureFormat(const QString &fileName) -{ - const QByteArray format = QPictureIO::pictureFormat(fileName); - // This function returns a const char * from a QByteArray. - // Double check that the QByteArray is not detached, otherwise - // we would return a dangling pointer. - Q_ASSERT(!format.isDetached()); - return format; -} - -/*! - \obsolete - - 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; - const int count = arr.count(); - list.reserve(count); - for (int i = 0; i < count; ++i) - list.append(QString::fromLatin1(arr.at(i))); - return list; -} - -/*! - \obsolete - - 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. - \snippet picture/picture.cpp 2 - - \sa outputFormatList(), inputFormats(), QPictureIO -*/ -QStringList QPicture::inputFormatList() -{ - return qToStringList(QPictureIO::inputFormats()); -} - - -/*! - \obsolete - - 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. - \snippet picture/picture.cpp 3 - - \sa inputFormatList(), outputFormats(), QPictureIO -*/ -QStringList QPicture::outputFormatList() -{ - return qToStringList(QPictureIO::outputFormats()); -} - -/*! - \obsolete - - Returns a list of picture formats that are supported for picture - output. - - \sa inputFormats(), outputFormatList(), QPictureIO -*/ -QList<QByteArray> QPicture::outputFormats() -{ - return QPictureIO::outputFormats(); -} -#endif // QT_DEPRECATED_SINCE(5, 10) - -/***************************************************************************** - QPictureIO member functions - *****************************************************************************/ - -/*! - \obsolete - - \class QPictureIO - - \brief The QPictureIO class contains parameters for loading and - saving pictures. - - \ingroup painting - \ingroup io - \inmodule QtGui - - 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 [] 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; -Q_GLOBAL_STATIC(QPHList, pictureHandlers) - -void qt_init_picture_plugins() -{ - typedef QMultiMap<int, QString> PluginKeyMap; - typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; - - static QBasicMutex mutex; - QMutexLocker locker(&mutex); - static QFactoryLoader loader(QPictureFormatInterface_iid, - QStringLiteral("/pictureformats")); - - const PluginKeyMap keyMap = loader.keyMap(); - const PluginKeyMapConstIterator cend = keyMap.constEnd(); - for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { - if (QPictureFormatPlugin *format = qobject_cast<QPictureFormatPlugin*>(loader.instance(it.key()))) - format->installIOHandler(it.value()); - } -} - -static void cleanup() -{ - // make sure that picture handlers are delete before plugin manager - if (QPHList *list = pictureHandlers()) { - qDeleteAll(*list); - list->clear(); - } -} - -void qt_init_picture_handlers() // initialize picture handlers -{ - static QBasicAtomicInt done = Q_BASIC_ATOMIC_INITIALIZER(0); - if (done.testAndSetRelaxed(0, 1)) { - qAddPostRoutine(cleanup); - } -} - -static QPictureHandler *get_picture_handler(const char *format) -{ // get pointer to handler - qt_init_picture_handlers(); - qt_init_picture_plugins(); - if (QPHList *list = pictureHandlers()) { - for (int i = 0; i < list->size(); ++i) { - if (list->at(i)->format == format) - return list->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 \nullptr, the QPictureIO will not be able - to read pictures in \a format. If \a writePicture is \nullptr, - the QPictureIO will not be able to write pictures in \a format. If - both are null, the QPictureIO object is valid but useless. - - Example: - \snippet picture/picture.cpp 6 - \codeline - \snippet picture/picture.cpp 7 - \codeline - \snippet picture/picture.cpp 8 - - Before the regular 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(); - if (QPHList *list = pictureHandlers()) { - QPictureHandler *p; - p = new QPictureHandler(format, header, QByteArray(flags), readPicture, writePicture); - list->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 \nullptr 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 [] 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::pos() -*/ - -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, 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); - if (QPHList *list = pictureHandlers()) { - for (int i = 0; i < list->size(); ++i) { - if (list->at(i)->header.indexIn(bufStr) != -1) { // try match with headers - format = list->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(); - - if (QPHList *list = pictureHandlers()) { - for (int i = 0; i < list->size(); ++i) { - QPictureHandler *p = list->at(i); - if (p->read_picture && !p->obsolete && !result.contains(p->format)) - result.append(p->format); - } - } - std::sort(result.begin(), result.end()); - - 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; - if (QPHList *list = pictureHandlers()) { - for (int i = 0; i < list->size(); ++i) { - QPictureHandler *p = list->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 \c true if the picture was - successfully read; otherwise returns \c 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 \l{format()}{format} used to read the picture. If you need that - information, use the pictureFormat() static functions. - - Example: - - \snippet picture/picture.cpp 4 - - \sa setIODevice(), setFileName(), setFormat(), write(), QPixmap::load() -*/ -bool QPictureIO::read() -{ - QFile file; - QByteArray 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.isEmpty()) { - 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 \c true if the picture was - successfully written; otherwise returns \c 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: - \snippet picture/picture.cpp 5 - - \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 - QT_END_NAMESPACE #endif // QT_NO_PICTURE |