HTYPaint/imagewidget.cpp

1144 lines
34 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "math.h"
#include "imagewidget.h"
#include "mainwindow.h"
ImageWidget::ImageWidget(QWidget *parent)
: QWidget(parent)
{
qApp->installEventFilter(this);
cundo = 0;
font = qApp->font();
newfile();
brush = QBrush(Qt::transparent, Qt::SolidPattern);
connect(new QShortcut(QKeySequence(Qt::ALT + Qt::Key_Up), this), SIGNAL(activated()), this, SLOT(moveUp()));
connect(new QShortcut(QKeySequence(Qt::ALT + Qt::Key_Down), this), SIGNAL(activated()), this, SLOT(moveDown()));
connect(new QShortcut(QKeySequence(Qt::ALT + Qt::Key_Left), this), SIGNAL(activated()), this, SLOT(moveLeft()));
connect(new QShortcut(QKeySequence(Qt::ALT + Qt::Key_Right), this), SIGNAL(activated()), this, SLOT(moveRight()));
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up), this), SIGNAL(activated()), this, SLOT(moveTopUp()));
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down), this), SIGNAL(activated()), this, SLOT(moveTopDown()));
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left), this), SIGNAL(activated()), this, SLOT(moveLeftLeft()));
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right), this), SIGNAL(activated()), this, SLOT(moveLeftRight()));
connect(new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Left), this), SIGNAL(activated()), this, SLOT(moveRightLeft()));
connect(new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Right), this), SIGNAL(activated()), this, SLOT(moveRightRight()));
connect(new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Up), this), SIGNAL(activated()), this, SLOT(moveBottomUp()));
connect(new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Down), this), SIGNAL(activated()), this, SLOT(moveBottomDown()));
}
ImageWidget::~ImageWidget()
{
}
void ImageWidget::paintEvent(QPaintEvent *)
{
resize(imgtemp.size());
setMinimumSize(imgtemp.size());
QPainter painter(this);
//生成棋盘背景
int dx = 50;
int dy = 50;
QBrush brush1(QColor(200,200,200));
QBrush brush2(QColor(255,255,255));
for (int y=0; y<imgtemp.height(); y+=dy) {
for (int x=0; x<imgtemp.width(); x+=dx) {
painter.fillRect(x, y, dx/2, dy/2, brush1);
painter.fillRect(x + dx/2, y, dx/2, dy/2, brush2);
painter.fillRect(x, y + dy/2, dx/2, dy/2, brush2);
painter.fillRect(x + dx/2, y + dy/2, dx/2, dy/2, brush1);
}
}
painter.drawImage(0, 0, imgtemp);
}
void ImageWidget::draw(QImage &img)
{
QPainter painter(&img);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing, true);
if (!boolBorder) {
pen.setColor(Qt::transparent);
}
switch (drawType){
case POINT_DRAW:
painter.drawPath(painterPath);
break;
case LINE_DRAW:{
int length = static_cast<int>(sqrt(pow(endPnt.x()-startPnt.x(),2) + pow(endPnt.y()-startPnt.y(),2)));
painter.drawLine(startPnt, endPnt);
emit statusBar2Message("(" + QString::number(startPnt.x()) + "," + QString::number(startPnt.y()) + ") - (" + QString::number(endPnt.x()) + "," + QString::number(endPnt.y()) + ") = " + QString::number(length));
break;}
case POLYLINE_DRAW:
polygon.append(startPnt);
painter.drawPolyline(polygon);
break;
case ARROW_DRAW:{
if (boolFill) {
painter.setBrush(brush);
} else {
painter.setBrush(QBrush(Qt::transparent, Qt::SolidPattern));
}
QPen pena = pen;
pena.setWidth(1);
painter.setPen(pena);
double pi = 3.14;
double a = pi/9;
double l = sqrt(pow(endPnt.y() - startPnt.y(),2) + pow(endPnt.x() - startPnt.x(),2));
double b = asin((endPnt.y() - startPnt.y())/l);
int LW = pen.width();
double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,l56;
l56 = LW/2;
double d = (l56 + LW/2)/sin(a);
if (startPnt.x() > endPnt.x()) {
x1 = startPnt.x() - LW/2*sin(b);
x2 = startPnt.x() + LW/2*sin(b);
x4 = endPnt.x() + d*cos(b-a);
x3 = x4 - l56*sin(b);
x5 = endPnt.x() + d*cos(a + b);
x6 = x5 + l56*sin(b);
} else {
x1 = startPnt.x() + LW/2*sin(b);
x2 = startPnt.x() - LW/2*sin(b);
x4 = endPnt.x() - d*cos(b-a);
x3 = x4 + l56*sin(b);
x5 = endPnt.x() - d*cos(a+b);
x6 = x5 - l56*sin(b);
}
y1 = startPnt.y() - LW/2*cos(b);
y2 = startPnt.y() + LW/2*cos(b);
y5 = endPnt.y() - d*sin(a+b);
y6 = y5 + l56*cos(b);
y4 = endPnt.y() - d*sin(b-a);
y3 = y4 - l56*cos(b);
QPointF points[7] = {
QPointF(x1,y1),
QPointF(x2,y2),
QPointF(x3,y3),
QPointF(x4,y4),
QPointF(endPnt),
QPointF(x5,y5),
QPointF(x6,y6)
};
painter.drawPolygon(points,7);
int length = static_cast<int>(sqrt(pow(endPnt.x()-startPnt.x(),2) + pow(endPnt.y()-startPnt.y(),2)));
emit statusBar2Message("(" + QString::number(startPnt.x()) + "," + QString::number(startPnt.y()) + ") - (" + QString::number(endPnt.x()) + "," + QString::number(endPnt.y()) + ")" + QString::number(length));
break;}
case RECT_DRAW:{
pen.setJoinStyle(Qt::MiterJoin);
if (boolFill) {
painter.setBrush(brush);
} else {
painter.setBrush(QBrush(Qt::transparent,Qt::SolidPattern));
}
QRect rect(startPnt, endPnt);
painter.drawRect(rect);
emit statusBar2Message("(" + QString::number(startPnt.x()) + "," + QString::number(startPnt.y()) + ") - (" + QString::number(endPnt.x()) + "," + QString::number(endPnt.y()) + ") = (" + QString::number(rect.width()) + "," + QString::number(rect.height()) + ")");
break;}
case SELECT_DRAW:{
painter.setPen(QPen(Qt::black, 1, Qt::DashLine));
painter.setBrush(QBrush(Qt::transparent, Qt::SolidPattern));
QPoint bottomRight;
int dx = endPnt.x() - startPnt.x();
if (selectRatio == SELECT_FREE) {
bottomRight = endPnt;
} else if (selectRatio == SELECT_1_1) {
bottomRight = QPoint(startPnt.x() + dx, startPnt.y() + dx);
} else if (selectRatio == SELECT_4_3) {
int dy = dx * 3 / 4;
bottomRight = QPoint(startPnt.x() + dx, startPnt.y() + dy);
} else if (selectRatio == SELECT_3_4) {
int dy = dx * 4 / 3;
bottomRight = QPoint(startPnt.x() + dx, startPnt.y() + dy);
} else if (selectRatio == SELECT_16_9) {
int dy = dx * 9 / 16;
bottomRight = QPoint(startPnt.x() + dx, startPnt.y() + dy);
} else if (selectRatio == SELECT_9_16) {
int dy = dx * 16 / 9;
bottomRight = QPoint(startPnt.x() + dx, startPnt.y() + dy);
}
rect_select = QRect(startPnt, bottomRight);
painter.drawRect(rect_select.adjusted(1,1,-1,-1));
emit statusBar2Message("(" + QString::number(startPnt.x()) + "," + QString::number(startPnt.y()) + ") - (" + QString::number(bottomRight.x()) + "," + QString::number(bottomRight.y()) + ") = (" + QString::number(rect_select.width()) + "," + QString::number(rect_select.height()) + ")");
break;}
case ELLIPSE_DRAW:{
QRect rect(startPnt, endPnt);
if (boolFill) {
painter.setBrush(brush);
} else {
painter.setBrush(QBrush(Qt::transparent, Qt::SolidPattern));
}
painter.drawEllipse(rect);
emit statusBar2Message("(" + QString::number(startPnt.x()) + "," + QString::number(startPnt.y()) + ") = (" + QString::number(rect.width()) + "," + QString::number(rect.height()) + ")");
break;}
case TEXT_DRAW:{
//描边文字
QPainterPath path;
path.addText(endPnt, font, text);
if (boolBorder)
painter.setPen(pen);//描边
else
painter.setPen(Qt::transparent);
if (boolFill)
painter.setBrush(brush);
else
painter.setBrush(Qt::transparent);
painter.drawPath(path);
break;}
case FILL_DRAW:
qDebug() << painterPath;
if (!painterPath.isEmpty() && painterPath.contains(startPnt)) {
painter.setBrush(brush);
painter.drawPath(painterPath);
}
break;
case ERASE_DRAW:
//painter.setPen(QPen(Qt::white,1));
//painter.setBrush(QBrush(Qt::white,Qt::SolidPattern));
painter.setBrush(QBrush(brush));
painter.drawEllipse(endPnt.x(), endPnt.y(), 20, 20);
break;
case DEL_DRAW:{
QRect rect(startPnt, endPnt);
for (int x = rect.x(); x<rect.x() + rect.width(); x++) {
for (int y = rect.y(); y < rect.y() + rect.height(); y++) {
image.setPixelColor(x, y, brush.color());
}
}
drawType = SELECT_DRAW;
break;}
case MOVE_DRAW:{
QRect target(endPnt, imgmove.size());
QPoint p(0,0);
QRect source(p, imgmove.size());
painter.drawImage(target, imgmove, source);
break;}
case COLORPICKER_DRAW:{
MainWindow *mainWindow = (MainWindow*)parentWidget()->parentWidget()->parentWidget();
qDebug() << mainWindow->checkBox_color_border->isChecked() << mainWindow->checkBox_color_fill->isChecked();
QRgb RGB = imgtemp.pixel(startPnt.x(), startPnt.y());
if (mainWindow->checkBox_color_border->isChecked()) {
pen.setColor(RGB);
painter.setPen(pen);
QPalette plt = mainWindow->toolButton_color_border->palette();
plt.setColor(QPalette::ButtonText, pen.color());
mainWindow->toolButton_color_border->setPalette(plt);
}
if (mainWindow->checkBox_color_fill->isChecked()) {
brush.setColor(RGB);
painter.setBrush(brush);
QPalette plt = mainWindow->toolButton_color_fill->palette();
plt.setColor(QPalette::ButtonText, brush.color());
mainWindow->toolButton_color_fill->setPalette(plt);
}
statusBar1Message("RGB(" + QString::number(qRed(RGB)) + "," + QString::number(qGreen(RGB)) + "," + QString::number(qBlue(RGB)) + ")");
break;}
default:
break;
}
update();
}
void ImageWidget::mousePressEvent(QMouseEvent *e)
{
startPnt = e->pos();
endPnt = e->pos();
switch (drawType) {
case POINT_DRAW:
painterPath.clear();
painterPath.moveTo(e->pos());
break;
case POLYLINE_DRAW:
draw(imgtemp);
image = imgtemp;
moveImgbuf();
break;
case ERASE_DRAW:
draw(imgtemp);
image = imgtemp;
moveImgbuf();
cundo = 0;
break;
case COLORPICKER_DRAW:
draw(imgtemp);
break;
case FILL_DRAW:
draw(image);
imgtemp = image;
break;
default:
break;
}
if (e->buttons() & Qt::RightButton) {
imgtemp = image;
draw(imgtemp);
}
}
void ImageWidget::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::LeftButton) {
endPnt = e->pos();
switch (drawType) {
case POINT_DRAW:
painterPath.lineTo(e->pos());
draw(imgtemp);
break;
case ERASE_DRAW:
draw(imgtemp);
cundo = 0;
moveImgbuf();
break;
case FILL_DRAW:
break;
default:
imgtemp = image;
draw(imgtemp);
}
}
}
void ImageWidget::mouseReleaseEvent(QMouseEvent *e)
{
Q_UNUSED(e);
switch (drawType) {
case MOVE_DRAW:
imgtemp = image;
draw(imgtemp);
break;
case SELECT_DRAW:
painterPath.addRect(rect_select);
break;
case TEXT_DRAW:
imgtemp = image;
draw(imgtemp);
image = imgtemp;
moveImgbuf();
cundo = 0;
break;
default:
image = imgtemp;
moveImgbuf();
cundo = 0;
}
}
void ImageWidget::zoomin()
{
QImage imgzoom = imgtemp.scaled(static_cast<int>(imgtemp.width() * 1.2), static_cast<int>(imgtemp.height() * 1.2), Qt::KeepAspectRatio, Qt::SmoothTransformation);
imgtemp = imgzoom;
update();
}
void ImageWidget::zoomout()
{
QImage imgzoom = imgtemp.scaled(static_cast<int>(imgtemp.width() * 0.8), static_cast<int>(imgtemp.height() * 0.8), Qt::KeepAspectRatio, Qt::SmoothTransformation);
imgtemp = imgzoom;
update();
}
void ImageWidget::zoom1()
{
imgtemp = imgload;
update();
}
void ImageWidget::scale(int width, int height)
{
QImage imgscale = imgtemp.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
imgtemp = imgscale;
image = imgtemp;
update();
}
void ImageWidget::rotate(qreal degrees)
{
QMatrix matrix;
matrix.rotate(degrees);
QImage imgrotate = imgtemp.transformed(matrix, Qt::SmoothTransformation);
imgtemp = imgrotate;
image = imgtemp;
//resize(imgrotate.size());
update();
}
void ImageWidget::mirror(bool bh,bool bv)
{
QImage imgmirror = imgtemp.mirrored(bh,bv);
imgtemp = imgmirror;
image = imgtemp;
//resize(imgmirror.size());
update();
}
void ImageWidget::drawPoint()
{
image = imgtemp;
drawType = POINT_DRAW;
setCursor(QCursor(QPixmap(":/icon/pencil.png")));
}
void ImageWidget::drawLine()
{
image = imgtemp;
drawType = LINE_DRAW;
setCursor(QCursor(QPixmap(":/icon/line.png")));
}
void ImageWidget::drawPolyline()
{
image = imgtemp;
drawType = POLYLINE_DRAW;
setCursor(QCursor(QPixmap(":/icon/polyline.png")));
polygon.clear();
}
void ImageWidget::drawArrow()
{
image = imgtemp;
drawType = ARROW_DRAW;
setCursor(QCursor(QPixmap(":/icon/arrow.png")));
}
void ImageWidget::drawRect()
{
image = imgtemp;
drawType = RECT_DRAW;
setCursor(QCursor(QPixmap(":/icon/rect.png")));
}
void ImageWidget::drawEllipse()
{
image = imgtemp;
drawType = ELLIPSE_DRAW;
setCursor(QCursor(QPixmap(":/icon/ellipse.png")));
}
void ImageWidget::drawText()
{
image = imgtemp;
drawType = TEXT_DRAW;
setCursor(QCursor(QPixmap(":/icon/text.png")));
}
void ImageWidget::drawFill()
{
image = imgtemp;
drawType = FILL_DRAW;
setCursor(QCursor(QPixmap(":/icon/fill.png")));
}
void ImageWidget::drawErase()
{
image = imgtemp;
drawType = ERASE_DRAW;
setCursor(QCursor(QPixmap(":/icon/eraser.png")));
//spinbox->setValue(20);
}
void ImageWidget::drawMove()
{
if (drawType == SELECT_DRAW) {
imgmove = imgtemp.copy(startPnt.x() +1 , startPnt.y() + 1, endPnt.x() - startPnt.x() - 1, endPnt.y() - startPnt.y() - 1);
} else {
imgmove = imgpaste;
}
drawType = MOVE_DRAW;
setCursor(QCursor(QPixmap(":/icon/move.png")));
}
void ImageWidget::drawRectSelect()
{
if (drawType != SELECT_DRAW)
image = imgtemp;
drawType = SELECT_DRAW;
setCursor(QCursor(QPixmap(":/icon/rectselect.png")));
}
void ImageWidget::colorPicker()
{
drawType = COLORPICKER_DRAW;
setCursor(QCursor(QPixmap(":/icon/color_picker.png")));
}
void ImageWidget::selectAll()
{
if (drawType == SELECT_DRAW) {
imgtemp = image;
update();
} else {
drawType = SELECT_DRAW;
}
startPnt = QPoint(0,0);
endPnt = QPoint(imgtemp.width()-1,imgtemp.height()-1);
painterPath.addRect(QRect(startPnt, endPnt));
qDebug() << painterPath;
draw(imgtemp);
}
void ImageWidget::delSelect()
{
drawType = DEL_DRAW;
draw(image);
imgtemp = image;
update();
}
void ImageWidget::newfile()
{
QImage imgnew = QImage(800, 600, QImage::Format_ARGB32);
imgnew.fill(Qt::transparent);
imgtemp = imgnew;
image = imgnew;
painterPath.clear();
update();
}
void ImageWidget::load(QString fileName)
{
imgload.load(fileName);
imgtemp = imgload;
image = imgload;
resize(imgload.size());
update();
drawType = NONE_DRAW;
setCursor(Qt::ArrowCursor);
cundo = 0;
for(int i=0; i<10; i++){
imgbuf[i] = image;
}
}
void ImageWidget::save(QString path)
{
image.save(path, nullptr, 100);
}
void ImageWidget::cutSelect()
{
//imgtemp = imgtemp.copy(startPnt.x()+1,startPnt.y()+1,endPnt.x()-startPnt.x()-1,endPnt.y()-startPnt.y()-1);
//imgtemp = image.copy(select_topLeft.x(), select_topLeft.y(), select_bottomRight.x() - startPnt.x() + 1, endPnt.y() - startPnt.y() + 1);
imgtemp = image.copy(rect_select);
image = imgtemp;
update();
}
void ImageWidget::newSize(int width, int height)
{
imgtemp = QImage(width, height, QImage::Format_ARGB32);
imgtemp.fill(Qt::transparent);
QPainter painter(&imgtemp);
painter.drawImage(0, 0, image);
update();
image = imgtemp;
}
void ImageWidget::copy()
{
if (drawType == SELECT_DRAW) {
QImage imgcopy = imgtemp.copy(startPnt.x()+1,startPnt.y()+1,endPnt.x()-startPnt.x()-1,endPnt.y()-startPnt.y()-1);
QApplication::clipboard()->setImage(imgcopy, QClipboard::Clipboard);
statusBar1Message("选区已复制到剪贴板");
}
}
void ImageWidget::paste()
{
//image=imgtemp;
imgpaste = QApplication::clipboard()->image();
if (imgpaste.width() > width() || imgpaste.height()> height()){
QMessageBox:: StandardButton SB = QMessageBox::question(nullptr, "修改图片尺寸", "粘贴图片比当前图片大,是否扩张图片尺寸?", QMessageBox::Yes | QMessageBox::No);
switch (SB) {
case QMessageBox::Yes:
newSize(qMax(imgpaste.width(), width()), qMax(imgpaste.height(), height()));
break;
default:
break;
}
}
QPainter painter(&imgtemp);
painter.drawImage(0, 0, imgpaste);
update();
statusBar1Message("剪贴板粘贴完成");
}
void ImageWidget::moveUp()
{
if(startPnt.y()>0 && endPnt.y()>0){
imgtemp = image;
startPnt.setY(startPnt.y()-1);
endPnt.setY(endPnt.y()-1);
draw(imgtemp);
}
}
void ImageWidget::moveDown()
{
if(endPnt.y() < imgtemp.height()){
imgtemp = image;
startPnt.setY(startPnt.y()+1);
endPnt.setY(endPnt.y()+1);
draw(imgtemp);
}
}
void ImageWidget::moveLeft()
{
if(startPnt.x()>0 && endPnt.x()>0){
imgtemp = image;
startPnt.setX(startPnt.x()-1);
endPnt.setX(endPnt.x()-1);
draw(imgtemp);
}
}
void ImageWidget::moveRight()
{
if(endPnt.x() < imgtemp.width()-2 && startPnt.x() < imgtemp.width()-2){
imgtemp = image;
startPnt.setX(startPnt.x()+1);
endPnt.setX(endPnt.x()+1);
draw(imgtemp);
}
}
void ImageWidget::moveTopUp()
{
if (startPnt.y()>0) {
imgtemp = image;
startPnt.setY(startPnt.y()-1);
draw(imgtemp);
}
}
void ImageWidget::moveTopDown()
{
if (startPnt.y() < imgtemp.height()-1) {
imgtemp = image;
startPnt.setY(startPnt.y()+1);
draw(imgtemp);
}
}
void ImageWidget::moveLeftLeft()
{
if (startPnt.x() > 0) {
imgtemp = image;
startPnt.setX(startPnt.x() - 1);
draw(imgtemp);
}
}
void ImageWidget::moveLeftRight()
{
if (startPnt.x() < imgtemp.width()-1) {
imgtemp = image;
startPnt.setX(startPnt.x() + 1);
draw(imgtemp);
}
}
void ImageWidget::moveRightLeft()
{
if (endPnt.x() > 0) {
imgtemp = image;
endPnt.setX(endPnt.x() - 1);
draw(imgtemp);
}
}
void ImageWidget::moveRightRight()
{
if (endPnt.x() < imgtemp.width() - 1) {
imgtemp = image;
endPnt.setX(endPnt.x() + 1);
draw(imgtemp);
}
}
void ImageWidget::moveBottomUp()
{
if (endPnt.y() > 0) {
imgtemp = image;
endPnt.setY(endPnt.y() - 1);
draw(imgtemp);
}
}
void ImageWidget::moveBottomDown()
{
if (endPnt.y() < imgtemp.height() - 2) {
imgtemp = image;
endPnt.setY(endPnt.y()+1);
draw(imgtemp);
}
}
void ImageWidget::moveImgbuf()
{
for (int i=9; i>0; i--) {
imgbuf[i] = imgbuf[i-1];
//qDebug() << "imgbuf" << i << "=" << i-1;
}
imgbuf[0] = imgtemp;
}
void ImageWidget::undo()
{
if (cundo < 10) {
imgtemp = imgbuf[cundo];
image = imgtemp;
//draw(imgtemp);
update();
cundo++;
}
}
void ImageWidget::redo()
{
if (cundo==10) cundo--;
if (cundo >= 0) {
imgtemp = imgbuf[cundo];
image = imgtemp;
//draw(imgtemp);
update();
cundo--;
}
}
void ImageWidget::gray()
{
int w,h;
w = imgtemp.width();
h = imgtemp.height();
QImage imgGray(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb rgba = image.pixel(x,y);
int gray = qGray(rgba);
QRgb grayRGBA = qRgba(gray, gray, gray, qAlpha(rgba));
imgGray.setPixel(x, y, grayRGBA);
}
}
imgtemp = imgGray;
image = imgGray;
update();
moveImgbuf();
}
void ImageWidget::invert()
{
int w, h;
w = imgtemp.width();
h = imgtemp.height();
QImage imgInvert(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x, y);
QRgb RGBi = qRgba(255-qRed(RGB), 255-qGreen(RGB), 255-qBlue(RGB), qAlpha(RGB));
imgInvert.setPixel(x, y, RGBi);
}
}
imgtemp = imgInvert;
image = imgInvert;
update();
moveImgbuf();
}
void ImageWidget::transparent()
{
int w,h;
w = imgtemp.width();
h = imgtemp.height();
QImage imgTansparent(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x,y);
//if (RGB == pen.color().rgb()) {//颜色相等
if (qAbs(qRed(RGB) - pen.color().red()) <= pen.width() && qAbs(qGreen(RGB) - pen.color().green()) <= pen.width() && qAbs(qBlue(RGB) - pen.color().blue()) < pen.width()) {//颜色近似
QRgb RGBT = qRgba(qRed(RGB), qGreen(RGB), qBlue(RGB), 0);
imgTansparent.setPixel(x,y,RGBT);
} else {
imgTansparent.setPixel(x,y,RGB);
}
}
}
imgtemp = imgTansparent;
image = imgTansparent;
update();
moveImgbuf();
}
void ImageWidget::blur(int p)
{
int xs,xe,ys,ye;
if(startPnt.x()<endPnt.x() && startPnt.y()<endPnt.y()){xs=startPnt.x(); ys=startPnt.y(); xe=endPnt.x(); ye=endPnt.y();}
if(startPnt.x()>endPnt.x() && startPnt.y()<endPnt.y()){xs=endPnt.x(); ys=startPnt.y(); xe=startPnt.x(); ye=endPnt.y();}
if(startPnt.x()>endPnt.x() && startPnt.y()>endPnt.y()){xs=endPnt.x(); ys=endPnt.y(); xe=startPnt.x(); ye=startPnt.y();}
if(startPnt.x()<endPnt.x() && startPnt.y()>endPnt.y()){xs=startPnt.x(); ys=endPnt.y(); xe=endPnt.x(); ye=startPnt.y();}
QImage imgBlur(qAbs(endPnt.x()-startPnt.x())+2, qAbs(endPnt.y()-startPnt.y())+2, QImage::Format_RGB32);
for (int x=xs; x<xe+2; x++) {
for (int y=ys; y<ye+2; y++) {
int red=0, green=0, blue=0, pc=0;
for (int a=-p; a<=p; a++) {
for (int b=-p; b<=p; b++) {
int xa = x+a;
int yb = y+b;
if (xa>0 && yb>0 && xa<image.width() && yb<image.height()) {
red += qRed(image.pixel(xa, yb));
green += qGreen(image.pixel(xa, yb));
blue += qBlue(image.pixel(xa, yb));
pc += 1;
}
}
}
QRgb RGBblur = qRgb(red/pc, green/pc, blue/pc);
imgBlur.setPixel(x-xs, y-ys, RGBblur);
}
}
QPainter painter(&imgtemp);
painter.drawImage(xs, ys, imgBlur);
update();
}
bool ImageWidget::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj);
if (event->type() == QEvent::MouseMove) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
//emit statusBar2Message(QString::number(mouseEvent->pos().x()) + "," + QString::number(mouseEvent->pos().y()));
MainWindow *mainWindow = (MainWindow*)parentWidget()->parentWidget()->parentWidget();
mainWindow->LSB2->setText(QString::number(mouseEvent->pos().x()) + "," + QString::number(mouseEvent->pos().y()));
} else if(event->type() == QEvent::Leave) {
//emit statusBar2Message("");
MainWindow *mainWindow = (MainWindow*)parentWidget()->parentWidget()->parentWidget();
mainWindow->LSB2->setText("");
}
return false;
}
void ImageWidget::mosaic(int p)
{
imgtemp = image;
update();
int xs, ys, xe, ye;
if (startPnt.x() < endPnt.x() && startPnt.y() < endPnt.y()) { xs=startPnt.x(); ys=startPnt.y(); xe=endPnt.x(); ye=endPnt.y(); }
if (startPnt.x() > endPnt.x() && startPnt.y() < endPnt.y()) { xs=endPnt.x(); ys=startPnt.y(); xe=startPnt.x(); ye=endPnt.y(); }
if (startPnt.x() > endPnt.x() && startPnt.y() > endPnt.y()) { xs=endPnt.x(); ys=endPnt.y(); xe=startPnt.x(); ye=startPnt.y(); }
if (startPnt.x() < endPnt.x() && startPnt.y() > endPnt.y()) { xs=startPnt.x(); ys=endPnt.y(); xe=endPnt.x(); ye=startPnt.y(); }
QImage imgMosaic(qAbs(endPnt.x() - startPnt.x()), qAbs(endPnt.y() - startPnt.y()), QImage::Format_RGB32);
QPainter painterm(&imgMosaic);
for (int x=xs; x<xe; x+=p) {
for (int y=ys; y<ye; y+=p) {
QRgb rgb = image.pixel(x, y);
QColor colorm(qRed(rgb), qGreen(rgb), qBlue(rgb));
painterm.setPen(QPen(colorm));
painterm.setBrush(QBrush(colorm));
painterm.drawRect(QRect(x-xs, y-ys, p, p));
}
}
QPainter painter(&imgtemp);
painter.drawImage(xs, ys, imgMosaic);
update();
}
void ImageWidget::matting()
{
//抠图,不可能完成的任务
int w,h;
w = imgtemp.width();
h = imgtemp.height();
QImage imgTansparent(w, h, QImage::Format_ARGB32);
int gray = qGray(pen.color().rgb());
for (int x=0; x<w; x++) {
for(int y=0; y<h; y++){
QRgb RGB = image.pixel(x,y);
if (qAbs(qGray(RGB) - gray) < 5){//颜色近似
QRgb RGBT = qRgba(qRed(RGB), qGreen(RGB), qBlue(RGB), 0);
imgTansparent.setPixel(x, y, RGBT);
} else {
imgTansparent.setPixel(x, y, RGB);
}
}
}
imgtemp = imgTansparent;
image = imgTansparent;
update();
moveImgbuf();
}
void ImageWidget::adjustRGB(int vr, int vg, int vb, bool b)
{
int w,h;
w = image.width();
h = image.height();
QImage imga(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x, y);
int R = qRed(RGB);
int G = qGreen(RGB);
int B = qBlue(RGB);
R += vr;
G += vg;
B += vb;
if (R < 0) R = 0;
if (G < 0) G = 0;
if (B < 0) B = 0;
if (R > 255) R = 255;
if (G > 255) G = 255;
if (B > 255) B = 255;
QRgb RGBa = qRgba(R, G, B, qAlpha(RGB));
imga.setPixel(x, y, RGBa);
}
}
if (b) {
image = imga;
moveImgbuf();
} else {
imgtemp = imga;
}
update();
}
void ImageWidget::adjustBrightness(int v, bool b)
{
//调亮度https://blog.csdn.net/u013015629/article/details/54669541
int w,h;
w = image.width();
h = image.height();
QImage imga(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x, y);
int R = qRed(RGB);
int G = qGreen(RGB);
int B = qBlue(RGB);
R += v;
G += v;
B += v;
if (R < 0) R = 0;
if (G < 0) G = 0;
if (B < 0) B = 0;
if (R > 255) R = 255;
if (G > 255) G = 255;
if (B > 255) B = 255;
QRgb RGBa = qRgba(R, G, B, qAlpha(RGB));
imga.setPixel(x, y, RGBa);
}
}
if (b) {
image = imga;
moveImgbuf();
} else {
imgtemp = imga;
}
update();
}
int ImageWidget::avarageGray()
{
int sum = 0;
int w = image.width();
int h = image.height();
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x, y);
int gray = qGray(RGB);
sum += gray;
}
}
int avg = sum / (w * h);
return avg;
}
void ImageWidget::adjustContrast(int v, bool b)
{
//https://www.cnblogs.com/justany/archive/2013/01/02/2842234.html
int avg = avarageGray();
qDebug() << "灰度平均值:" << avg;
int w = image.width();
int h = image.height();
QImage image1(w, h, QImage::Format_ARGB32);
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
QRgb RGB = image.pixel(x, y);
int R = qRed(RGB);
int G = qGreen(RGB);
int B = qBlue(RGB);
//线性
/*
R *= v;
G *= v;
B *= v;
*/
//非线性
if (v >= 0) {
R = R + (R - avg) * (1 / (1 - v / 255.0) - 1);
G = G + (G - avg) * (1 / (1 - v / 255.0) - 1);
B = B + (B - avg) * (1 / (1 - v / 255.0) - 1);
} else {
R = R + (R - avg) * v / 255;
G = G + (G - avg) * v / 255;
B = B + (B - avg) * v / 255;
}
if (R < 0) R = 0;
if (G < 0) G = 0;
if (B < 0) B = 0;
if (R > 255) R = 255;
if (G > 255) G = 255;
if (B > 255) B = 255;
image1.setPixel(x, y, qRgb(R, G, B));
}
}
if (b) {
image = image1;
moveImgbuf();
} else {
imgtemp = image1;
}
update();
}
void ImageWidget::clip(int x1, int y1, int x2, int y2, bool isConfirm)
{
imgtemp = image.copy(x1, y1, x2 - x1, y2 - y1);
emit statusBar2Message("(" + QString::number(x1) + "," + QString::number(y1) + ") - (" + QString::number(x2-x1) + "," + QString::number(y2-y1) + ")");
if (isConfirm) {
image = imgtemp;
moveImgbuf();
}
update();
}
void ImageWidget::clipPath(int x, int y, int width, int height, bool isConfirm)
{
QPixmap pixmap0 = QPixmap::fromImage(image);
QPixmap pixmap(pixmap0.width(), pixmap0.height());
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPainterPath pPath;
pPath.addEllipse(x, y, width, height);
painter.setClipPath(pPath);
painter.drawPixmap(0, 0, pixmap0.width(), pixmap0.height(), pixmap0);
if (isConfirm) {
image = pixmap.toImage();
moveImgbuf();
} else {
imgtemp = pixmap.toImage();
}
update();
}
void ImageWidget::deleteBlackBorder(BorderType border_type, int d)
{
switch (border_type) {
case BORDER_TOP:{
bool isEnd = false;
int y1 = 0;
for (int y=0; y<image.height(); y++) {
for (int x=0; x<image.width(); x++) {
QRgb rgb = image.pixel(x,y);
int r = qRed(rgb);
int g = qGreen(rgb);
int b = qBlue(rgb);
if (r>d && g>d && b>d) {
emit statusBar2Message("(" + QString::number(x) + "," + QString::number(y) + ") (" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + ")");
isEnd = true;
break;
}
}
if (isEnd) {
y1 = y;
break;
}
}
image = image.copy(0, y1, image.width()-1, image.height() - y1);
imgtemp = image;
update();
break;}
case BORDER_BOTTOM:{
bool isEnd = false;
int y1 = image.height()-1;
for (int y=image.height()-1; y>0; y--) {
for (int x=0; x<image.width(); x++) {
QRgb rgb = image.pixel(x,y);
int r = qRed(rgb);
int g = qGreen(rgb);
int b = qBlue(rgb);
if (r>d && g>d && b>d) {
emit statusBar2Message("(" + QString::number(x) + "," + QString::number(y) + ") (" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + ")");
isEnd = true;
break;
}
}
if (isEnd) {
y1 = y;
break;
}
}
image = image.copy(0, 0, image.width() -1, y1);
imgtemp = image;
update();}
break;
case BORDER_LEFT:{
bool isEnd = false;
int x1 = 0;
for (int x=0; x<image.width(); x++) {
for (int y=0; y<image.height(); y++) {
QRgb rgb = image.pixel(x,y);
int r = qRed(rgb);
int g = qGreen(rgb);
int b = qBlue(rgb);
if (r>d && g>d && b>d) {
emit statusBar2Message("(" + QString::number(x) + "," + QString::number(y) + ") (" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + ")");
isEnd = true;
break;
}
}
if (isEnd) {
x1 = x;
break;
}
}
image = image.copy(x1, 0, image.width() - x1, image.height());
imgtemp = image;
update();
break;}
case BORDER_RIGHT:{
bool isEnd = false;
int x1 = image.width() - 1;
for (int x=image.width()-1; x>0; x--) {
for (int y=0; y<image.height(); y++) {
QRgb rgb = image.pixel(x,y);
int r = qRed(rgb);
int g = qGreen(rgb);
int b = qBlue(rgb);
if (r>d && g>d && b>d) {
emit statusBar2Message("(" + QString::number(x) + "," + QString::number(y) + ") (" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + ")");
isEnd = true;
break;
}
}
if (isEnd) {
x1 = x;
break;
}
}
image = image.copy(0, 0, x1, image.height());
imgtemp = image;
update();}
break;
}
}
void ImageWidget::cutBorder(int top, int right, int bottom, int left)
{
image = image.copy(left, top, image.width() - left - right, image.height() - top - bottom);
imgtemp = image;
emit statusBar1Message("图片尺寸:" + QString::number(image.width()) + " X " + QString::number(image.height()));
update();
}