Exemplo n.º 1
0
void ImageToolWidget::filterImage()
{
    if (ui.PixmapLabel->pixmap() == 0) {
        QMessageBox::information(this, "QImageTool", "Sorry, you must load an image first\n");
    } else {
        setCursor(Qt::WaitCursor);
        QImage imgToFilter = ui.PixmapLabel->pixmap()->toImage();
        QtImageFilter *filter = m_imageFilters[ui.FiltersCombo->currentIndex()];
        if (filter->name() == "Punch") {
            filter->setOption(QtImageFilter::Radius, qMin(imgToFilter.width(), imgToFilter.height())/2);
            filter->setOption(QtImageFilter::Center, QPointF(imgToFilter.width()/2.0,imgToFilter.height()/2.0));
            filter->setOption(QtImageFilter::Force, 0.5);
        } else if (filter->name() == "ConvolutionFilter") {
            // A simple mean filter just to demonstrate that we can add our own kernels.
            static int kernelElements[9] =
            {   1,  1,  1,
                1,  1,  1,
                1,  1,  1
            };
            QtConvolutionKernelMatrix kernel(kernelElements, 3, 3);
            QVariant value;
            qVariantSetValue<QtConvolutionKernelMatrix>(value, kernel);
            filter->setOption(QtImageFilter::ConvolutionKernelMatrix, value);

        }
        if (filter->supportsOption(MirrorFilter::MirrorHorizontal))
            filter->setOption(MirrorFilter::MirrorHorizontal, ui.ckHorizontal->isChecked() );
        if (filter->supportsOption(MirrorFilter::MirrorVertical))
            filter->setOption(MirrorFilter::MirrorVertical, ui.ckVertical->isChecked());

        if (filter->supportsOption(QtImageFilter::FilterChannels)) {
            QString rgba = ui.ckRed->isChecked() ? "r" : "";
            rgba+= ui.ckGreen->isChecked() ? "g" : "";
            rgba+= ui.ckBlue->isChecked() ? "b" : "";
            rgba+= ui.ckAlpha->isChecked() ? "a" : "";
            filter->setOption(QtImageFilter::FilterChannels, rgba);
        }

        if (filter->supportsOption(QtImageFilter::FilterBorderPolicy)) {
            QString borderPolicy;
            if (ui.rbExtend->isChecked()) borderPolicy = "Extend";
            else if (ui.rbMirror->isChecked()) borderPolicy = "Mirror";
            else borderPolicy = "Wrap";
            filter->setOption(QtImageFilter::FilterBorderPolicy, borderPolicy);
        }

        imgToFilter = filter->apply(imgToFilter);
        setCursor(Qt::ArrowCursor);
        setPixmapAndResize(QPixmap::fromImage(imgToFilter));
    }
}
Exemplo n.º 2
0
void ImageToolWidget::filterIndexChanged(int index)
{
    if (index == 0 || index >= m_imageFilters.count()) {
        ui.FilterButton->setToolTip(QLatin1String("No image filter chosen"));
        ui.FilterButton->setEnabled(false);
        ui.gbBorderPolicy->setVisible( false );
        ui.gbChannels->setVisible( false );
        ui.gbMirror->setVisible( false );
    } else {
        QtImageFilter *filter = m_imageFilters[index];
        ui.FilterButton->setToolTip(filter->description());
        ui.FilterButton->setEnabled(true);
        ui.gbBorderPolicy->setVisible( filter->supportsOption(QtImageFilter::FilterBorderPolicy)  );
        ui.gbChannels->setVisible( filter->supportsOption(QtImageFilter::FilterChannels)  );
        ui.gbMirror->setVisible( filter->supportsOption(MirrorFilter::MirrorHorizontal) || filter->supportsOption(MirrorFilter::MirrorVertical) );
    }
}