extrafiltersplugin.cpp Example File | Qt 4.8 (original) (raw)

tools/plugandpaintplugins/extrafilters/extrafiltersplugin.cpp

#include

#include <math.h> #include <stdlib.h>

#include "extrafiltersplugin.h"

QStringList ExtraFiltersPlugin::filters() const { return QStringList() << tr("Flip Horizontally") << tr("Flip Vertically") << tr("Smudge...") << tr("Threshold..."); }

QImage ExtraFiltersPlugin::filterImage(const QString &filter, const QImage &image, QWidget *parent) { QImage original = image.convertToFormat(QImage::Format_RGB32); QImage result = original;

if (filter == tr("Flip Horizontally")) {
    for (int y = 0; y < original.height(); ++y) {
        for (int x = 0; x < original.width(); ++x) {
            int pixel = original.pixel(original.width() - x - 1, y);
            result.setPixel(x, y, pixel);
        }
    }
} else if (filter == tr("Flip Vertically")) {
    for (int y = 0; y < original.height(); ++y) {
        for (int x = 0; x < original.width(); ++x) {
            int pixel = original.pixel(x, original.height() - y - 1);
            result.setPixel(x, y, pixel);
        }
    }
} else if (filter == tr("Smudge...")) {
    bool ok;
    int numIters = [QInputDialog](qinputdialog.html)::getInteger(parent, tr("Smudge Filter"),
                                tr("Enter number of iterations:"),
                                5, 1, 20, 1, &ok);
    if (ok) {
        for (int i = 0; i < numIters; ++i) {
            for (int y = 1; y < original.height() - 1; ++y) {
                for (int x = 1; x < original.width() - 1; ++x) {
                    int p1 = original.pixel(x, y);
                    int p2 = original.pixel(x, y + 1);
                    int p3 = original.pixel(x, y - 1);
                    int p4 = original.pixel(x + 1, y);
                    int p5 = original.pixel(x - 1, y);

                    int red = ([qRed](qcolor.html#qRed)(p1) + [qRed](qcolor.html#qRed)(p2) + [qRed](qcolor.html#qRed)(p3) + [qRed](qcolor.html#qRed)(p4)
                               + [qRed](qcolor.html#qRed)(p5)) / 5;
                    int green = ([qGreen](qcolor.html#qGreen)(p1) + [qGreen](qcolor.html#qGreen)(p2) + [qGreen](qcolor.html#qGreen)(p3)
                                 + [qGreen](qcolor.html#qGreen)(p4) + [qGreen](qcolor.html#qGreen)(p5)) / 5;
                    int blue = ([qBlue](qcolor.html#qBlue)(p1) + [qBlue](qcolor.html#qBlue)(p2) + [qBlue](qcolor.html#qBlue)(p3)
                                + [qBlue](qcolor.html#qBlue)(p4) + [qBlue](qcolor.html#qBlue)(p5)) / 5;
                    int alpha = ([qAlpha](qcolor.html#qAlpha)(p1) + [qAlpha](qcolor.html#qAlpha)(p2) + [qAlpha](qcolor.html#qAlpha)(p3)
                                 + [qAlpha](qcolor.html#qAlpha)(p4) + [qAlpha](qcolor.html#qAlpha)(p5)) / 5;

                    result.setPixel(x, y, [qRgba](qcolor.html#qRgba)(red, green, blue, alpha));
                }
            }
        }
    }
} else if (filter == tr("Threshold...")) {
    bool ok;
    int threshold = [QInputDialog](qinputdialog.html)::getInteger(parent, tr("Threshold Filter"),
                                             tr("Enter threshold:"),
                                             10, 1, 256, 1, &ok);
    if (ok) {
        int factor = 256 / threshold;
        for (int y = 0; y < original.height(); ++y) {
            for (int x = 0; x < original.width(); ++x) {
                int pixel = original.pixel(x, y);
                result.setPixel(x, y, [qRgba](qcolor.html#qRgba)([qRed](qcolor.html#qRed)(pixel) / factor * factor,
                                            [qGreen](qcolor.html#qGreen)(pixel) / factor * factor,
                                            [qBlue](qcolor.html#qBlue)(pixel) / factor * factor,
                                            [qAlpha](qcolor.html#qAlpha)(pixel)));
            }
        }
    }
}
return result;

}

Q_EXPORT_PLUGIN2(pnp_extrafilters, ExtraFiltersPlugin)

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.