📄 qpicture.cpp
字号:
\skipto USE SVG \skipto add \printuntil ... 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(); 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, 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/*! \typedef QPicture::DataPtr \internal*//*! \fn DataPtr &QPicture::data_ptr() \internal*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -