-
-
Save anonymous/73e0f538bf05515adb34d93d8e9bd6ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QObject> | |
#include <QDebug> | |
#include <QColor> | |
#include <QImage> | |
#include <QQuickImageProvider> | |
#include <QElapsedTimer> | |
#include <vector> | |
#include "Filter.h" | |
Filter::Filter(QObject *parent) : QObject(parent), QQuickImageProvider(QQuickImageProvider::Image) | |
{ | |
this->_kernelRadius = 0; | |
} | |
Filter::~Filter(){ | |
} | |
// | |
void Filter::setKernelRadius(int radius) | |
{ | |
qDebug() << "setKernelRadius:" << radius; | |
this->_kernelRadius = radius; | |
} | |
// | |
QImage Filter::requestImage(const QString &id, QSize *size, const QSize &requestedSize) | |
{ | |
QElapsedTimer et; | |
QImage srcImage; | |
QImage dstImage(requestedSize, QImage::Format::Format_RGBA8888); | |
QString path = QStringLiteral(":/Parrots.png"); | |
srcImage.load(path); | |
int width = requestedSize.width(); | |
int height = requestedSize.height(); | |
// コンボリューションカーネルの作成 | |
int kernelRadius = this->_kernelRadius; | |
int kernelSize = 2*kernelRadius + 1; | |
std::vector<std::vector<int>> kernel; | |
kernel.resize(kernelSize); | |
for (int y=0; y<kernelSize; y++) { | |
kernel[y].resize(kernelSize); | |
QString s = ""; | |
for (int x=0; x<kernelSize; x++) { | |
int xx = x - kernelRadius; | |
int yy = y - kernelRadius; | |
if (sqrt(xx*xx + yy*yy) <= kernelRadius) { | |
kernel[y][x] = 1.0f; | |
s += "■" ; | |
} else { | |
kernel[y][x] = 0.0f; | |
s += "□" ; | |
} | |
} | |
qDebug() << s ; | |
} | |
et.start(); | |
// フィルター処理 | |
for (int y = 0; y < height; ++y) | |
{ | |
for (int x = 0; x < width; ++x) | |
{ | |
int r=0.0f,g=0.0f,b=0.0f; | |
int count = 0; | |
for (int ky=0; ky<kernelSize; ky++) { | |
for (int kx=0; kx<kernelSize; kx++) { | |
int xx = x + kx - kernelSize/2; | |
int yy = y + ky - kernelSize/2; | |
if (srcImage.valid(xx, yy)) { | |
QColor color = srcImage.pixelColor(xx, yy); | |
int kv = kernel[ky][kx]; | |
r += color.red() * kv; | |
g += color.green() * kv; | |
b += color.blue() * kv; | |
count += kv; | |
} | |
} | |
} | |
QColor color(r/count, g/count, b/count); | |
dstImage.setPixelColor(x, y, color); | |
} | |
} | |
// 処理時間をコンソールに出力 | |
qDebug() << "elapsed time: " << QString::number(et.elapsed()) ; | |
return dstImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment