Exemplo n.º 1
0
QuillImage LoadFilter::apply(const QuillImage &tile) const
{
    if (priv->fileFormatQt.isEmpty() && !priv->isInvalid)
        const_cast<LoadFilter *>(this)->detectFormat();

    if (priv->isInvalid)
        return QuillImage();

    bool isNew = !reader
            || (filterAddress != this)
            || !priv->bufferInitialized
            || !tile.isFragment()
            || (priv->fileFormatQt != reader->format())
            || (!tile.targetSize().isEmpty()
            && (tile.targetSize() != tile.area().size()));

    filterAddress = const_cast<LoadFilter*>(this);
    priv->bufferInitialized = true;

    if (priv->iODevice) {
        delete reader;
        reader = new QImageReader(priv->iODevice, priv->fileFormatQt.toAscii());

        if (!priv->fileFormatQt.isEmpty())
            reader->setFormat(priv->fileFormatQt.toAscii());
    }
    else {
        if (isNew) {
            delete reader;
            reader = 0;

            if (!const_cast<LoadFilter*>(this)->readFileToByteArray())
                return QuillImage();

            buffer.seek(0);
            priv->orientation = readOrientation();
            reader = new QImageReader(&buffer, priv->fileFormatQt.toAscii());

            if (!priv->fileFormatQt.isEmpty())
                reader->setFormat(priv->fileFormatQt.toAscii());
        }
        else {
            buffer.seek(0);
        }
    }

    QuillImage input = tile;

    if (input.fullImageSize().isEmpty())
        input.setFullImageSize(reader->size());

    if (input.area().isEmpty())
        input.setArea(QRect(QPoint(0, 0), input.fullImageSize()));

    QImage newImage;

    QSize targetSize = input.targetSize();
    QSize fullImageSize = input.fullImageSize();

    QRect area = rotateArea(fullImageSize, input.area(), input);

    if ((priv->orientation == Exif_Orientation_Rotated90) ||
        (priv->orientation == Exif_Orientation_Rotated270)) {
        targetSize.transpose();
        fullImageSize.transpose();
    }

    /*
       Resetting reader for ScaledSize and ClipRect. This resetting
       will make sure that the reader will have all the options set
       correctly if the same reader has been used previously.
       Does not do the calculation in all cases since it is time consuming.
    */

    if (reader->scaledSize().isValid() || reader->clipRect().isValid()) {
        QSize readerSize = reader->size();
        reader->setScaledSize(readerSize);
        reader->setClipRect(QRect(QPoint(0,0),readerSize));
    }

    if (!input.isFragment()){
        fullImage = QImage();

        if (!targetSize.isEmpty()) {
            reader->setScaledSize(targetSize);
        }

        newImage = readFromReader();
    }
    else if (!targetSize.isEmpty() &&
             (targetSize != area.size())) {
        // Both cropping and scaling have been requested
        // Current implementation: ask plugin for the scaled image, then crop
        // This can be later optimized with a plugin that effectively
        // supports the ScaledClipRect option.
        reader->setScaledSize(QSize(targetSize.width() *
                                    fullImageSize.width() /
                                    area.width(),
                                    targetSize.height() *
                                    fullImageSize.height() /
                                    area.height()));

        newImage = readFromReader();

        newImage = newImage.copy(input.area().left() *
                                 input.targetSize().width() /
                                 input.area().width(),
                                 input.area().top() *
                                 input.targetSize().height() /
                                 input.area().height(),
                                 input.targetSize().width(),
                                 input.targetSize().height());

        newImage = QuillImage(input, newImage);
    }
    else if (reader->supportsOption(QImageIOHandler::ClipRect) &&
             // Standard Qt now supports ClipRect with a too slow
             // implementation, so we use the ScaledClipRect information
             // to reject the standard Qt Jpeg plugin.
             // TODO: This needs to be removed if Qt becomes faster.
             !reader->supportsOption(QImageIOHandler::ScaledClipRect)) {

        fullImage = QImage();

        //if it is real a tile, we then reset reader, otherwise we consider it as a full image
        if(!area.isNull()){
            //set the scale size again here because preview sets the preview scale size already.
            reader->setScaledSize(area.size());
            // clip rect support in image reader
            reader->setClipRect(area);
        }

        newImage = readFromReader();
    }
    else {
        // no clip rect support: preserve whole full image as a tile cache,
        // uses a lot of extra memory
        if (isNew || fullImage.isNull())
            fullImage = readFromReader();

        newImage = fullImage.copy(input.area());
    }

    QImage convertedImage;
    if (newImage.hasAlphaChannel()){
        convertedImage = renderAlpha(newImage, priv->backgroundColor);
    }
    else{
        convertedImage = newImage.convertToFormat(QImage::Format_RGB32);
    }

    QuillImage ret_value;

    if (!input.targetSize().isEmpty() || !input.fullImageSize().isEmpty() ||
        !input.area().isEmpty()) {
        ret_value = QuillImage(input, convertedImage);
    }
    else{
        // full image
        ret_value = QuillImage(convertedImage);
    }

    if (priv->iODevice) {
        delete reader;
        reader = 0;
    }

    return ret_value;
}