Example #1
0
void ImageDocument::setContents(const ImageContents &contents)
{
    Q_D(ImageDocument);

    if (contents.isNull()) {
        clear();
        return;
    }

    const auto header = contents.header();
    d->type = header.type();
    d->size = header.size();
    d->imageFormat = header.imageFormat();
    d->imageCount = header.imageCount();
    d->mipmapCount = header.mipmapCount();
    d->frameDelay = header.frameDelay();
    d->loopCount = header.loopCount();
    for (int index = 0; index < d->imageCount; ++index) {
        for (int level = 0; level < d->mipmapCount; ++level) {
            auto image = contents.image(index, level);
            std::unique_ptr<ImageDocumentItem> item(new ImageDocumentItem(this));
            item->setSize(image.size());
            item->setImage(image);
            d->items[ImageDocumentPrivate::ImageIndex(index, level)] = std::move(item);
        }
    }

    emit contentsChanged();
}
Example #2
0
int main(int argc, char *argv[])
{
    ImageIO reader("image.png");
    auto result = reader.read();
    ImageIOResult ok = result.first;
    if (!ok) {
        qWarning() << "Error reading image:" << ok.toString();
        return 1;
    }
    ImageContents contents = result.second;
    handleImage(contents.image());
    return 0;
}
bool DefaultHandler::write(const ImageContents &contents, const WriteOptions &options)
{
    Q_UNUSED(options);
    QImageWriter writer(device(), mimeTypeToFormat(mimeType()));
    const bool ok = writer.write(contents.image());
    if (!ok)
        return false;

    return true;
}
bool DefaultHandler::read(ImageContents &contents, const ReadOptions &options)
{
    Q_UNUSED(options);
    QImageReader reader(device(), mimeTypeToFormat(mimeType()));

    int count = reader.imageCount();
    if (reader.supportsOption(QImageIOHandler::Animation)) {
        contents.setImageCount(count);
        for (int i = 0; i < count; i++) {
            QImage image;
            const bool ok = reader.read(&image);
            if (!ok)
                return false;

            contents.setImage(image, i);
        }
    } else if (count > 0) {
        contents.setMipmapCount(count);
        for (int i = 0; i < count; i++) {
            QImage image;
            reader.jumpToImage(i);
            const bool ok = reader.read(&image);
            if (!ok)
                return false;

            contents.setImage(image, 0, i);
        }
    } else {
        QImage image;
        const bool ok = reader.read(&image);
        if (!ok)
            return false;

        contents.setImage(image);
    }

    return true;
}