Some image change

This commit is contained in:
Sonaion
2019-11-26 17:26:43 +01:00
parent b4d8738e71
commit bccca91111
29 changed files with 340 additions and 4099 deletions

View File

@@ -3,7 +3,7 @@
#include <QtWidgets>
#include "IntelliPhotoGui.h"
#include "PaintingArea.h"
#include "Layer/PaintingArea.h"
// IntelliPhotoGui constructor
IntelliPhotoGui::IntelliPhotoGui()

View File

@@ -0,0 +1,71 @@
#include"Image/IntelliImage.h"
#include<QSize>
#include<QPainter>
IntelliImage::IntelliImage(int weight, int height)
:imageData(QSize(weight, height), QImage::Format_RGB32){
}
IntelliImage::~IntelliImage(){
}
bool IntelliImage::loadImage(const QString &fileName){
// Holds the image
QImage loadedImage;
// If the image wasn't loaded leave this function
if (!loadedImage.load(fileName))
return false;
QSize newSize = loadedImage.size().expandedTo(imageData.size());
resizeImage(&loadedImage, newSize);
imageData = loadedImage;
return true;
}
void IntelliImage::resizeImage(QImage *image, const QSize &newSize){
// Check if we need to redraw the image
if (image->size() == newSize)
return;
// Create a new image to display and fill it with white
QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(qRgb(255, 255, 255));
// Draw the image
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), *image);
*image = newImage;
}
void IntelliImage::drawPixel(const QPoint &p1, const QColor& color){
// Used to draw on the widget
QPainter painter(&imageData);
// Set the current settings for the pen
painter.setPen(QPen(color, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
// Draw a line from the last registered point to the current
painter.drawPoint(p1);
// Call to update the rectangular space where we drew
//update(QRect(p1, p2));
}
void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth){
// Used to draw on the widget
QPainter painter(&imageData);
// Set the current settings for the pen
painter.setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
// Draw a line from the last registered point to the current
painter.drawLine(p1, p2);
}
void IntelliImage::floodFill(const QColor& color){
imageData.fill(color);
}

View File

@@ -0,0 +1,38 @@
#ifndef INTELLIIMAGE_H
#define INTELLIIMAGE_H
#include<QImage>
#include<QPoint>
#include<QColor>
#include<QSize>
#include<vector>
enum class ImageType{
Raster_Image,
Shaped_Image
};
class IntelliImage{
protected:
void resizeImage(QImage *image, const QSize &newSize);
QImage imageData;
public:
IntelliImage(int weight, int height);
virtual ~IntelliImage() = 0;
//start on top left
virtual void drawPixel(const QPoint &p1, const QColor& color);
virtual void drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth);
virtual void floodFill(const QColor& color);
//returns the filtered output
virtual QImage getDisplayable(const QSize& displaySize)=0;
//sets the data for the visible image
virtual void setPolygon(const std::vector<QPoint>& polygonData)=0;
virtual bool loadImage(const QString &fileName);
};
#endif

View File

@@ -0,0 +1,25 @@
#include"Image/IntelliRasterImage.h"
#include<QPainter>
#include<QRect>
#include<QDebug>
IntelliRasterimage::IntelliRasterimage(int weight, int height)
:IntelliImage(weight, height){
}
IntelliRasterimage::~IntelliRasterimage(){
}
QImage IntelliRasterimage::getDisplayable(const QSize& displaySize){
QImage copy = imageData;
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
}
void IntelliRasterimage::setPolygon(const std::vector<QPoint>& polygonData){
qDebug() << "Raster Image has no polygon data " << polygonData.size() <<"\n";
return;
}

View File

@@ -0,0 +1,18 @@
#ifndef INTELLIRASTER_H
#define INTELLIRASTER_H
#include"Image/IntelliImage.h"
class IntelliRasterimage : public IntelliImage{
public:
IntelliRasterimage(int weight, int height);
virtual ~IntelliRasterimage() override;
//returns the filtered output
virtual QImage getDisplayable(const QSize& displaySize) override;
//sets the data for the visible image
virtual void setPolygon(const std::vector<QPoint>& polygonData) override;
};
#endif

View File

@@ -0,0 +1,48 @@
#include"Image/IntelliShapedImage.h"
#include"IntelliHelper/IntelliHelper.h"
#include<QPainter>
#include<QRect>
#include<QDebug>
IntelliShapedImage::IntelliShapedImage(int weight, int height)
:IntelliImage(weight, height){
}
IntelliShapedImage::~IntelliShapedImage(){
}
QImage IntelliShapedImage::getDisplayable(const QSize& displaySize){
QImage copy = imageData;
QPoint extrem(copy.width()+1, 0);
QPoint startPoint(0,0);
//traverse through y direction
for(int y = 0; y<copy.height(); y++){
extrem.setY(y);
startPoint.setY(y);
//traverse through x dircetion
for(int x=0; x<copy.width(); x++){
startPoint.setX(x);
//traverse all edges
int cutNumber = 0;
for(size_t i=0; i<polygonData.size()-1; i++){
QPoint& start = polygonData[i];
QPoint& end = polygonData[i+1];
cutNumber += IntelliHelper::hasIntersection(startPoint, extrem, start, end);
}
//check if zhe cutNumber is Even -> not in Polygon
if(!(cutNumber&1)){
QColor tmpColor(copy.color(y*copy.width()+x));
tmpColor.setAlpha(0);
copy.setPixelColor(startPoint,tmpColor);
}
}
}
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
}
void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
this->polygonData=polygonData;
return;
}

View File

@@ -0,0 +1,20 @@
#ifndef INTELLISHAPE_H
#define INTELLISHAPE_H
#include"Image/IntelliImage.h"
class IntelliShapedImage : public IntelliImage{
protected:
std::vector<QPoint> polygonData;
public:
IntelliShapedImage(int weight, int height);
virtual ~IntelliShapedImage() override;
//returns the filtered output
virtual QImage getDisplayable(const QSize& displaySize) override;
//sets the data for the visible image
virtual void setPolygon(const std::vector<QPoint>& polygonData) override;
};
#endif

View File

@@ -0,0 +1,39 @@
#include"IntelliHelper.h"
#include<algorithm>
int IntelliHelper::orientation(QPoint& p1, QPoint& p2, QPoint& p3){
int value = (p2.x()-p1.x())*(p3.x()-p2.x())-
(p2.y()-p1.y())*(p3.y()-p2.y());
if(value==0) return 0;
return (value>0)?1:2;
}
bool IntelliHelper::onSegment(QPoint& p1, QPoint& q, QPoint& p2){
return (q.x() >= std::min(p1.x(),p2.x()) && q.x() <= std::max(p1.x(), p2.x()) &&
q.y() >= std::min(p1.y(),p2.y()) && q.y() <= std::max(p1.y(), p2.y()));
}
bool IntelliHelper::hasIntersection(QPoint& p1, QPoint& q1, QPoint& p2, QPoint& q2){
int o1 = IntelliHelper::orientation(p1,q1,p2);
int o2 = IntelliHelper::orientation(p1,q1,q2);
int o3 = IntelliHelper::orientation(p2,q2,p1);
int o4 = IntelliHelper::orientation(p2,q2,q1);
// General case
if (o1 != o2 && o3 != o4)
return true;
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false; // Doesn't fall in any of the above cases
}

View File

@@ -0,0 +1,22 @@
#ifndef INTELLIHELPER_H
#define INTELLIHELPER_H
#include<QPoint>
class IntelliHelper{
public:
//checks for orientation:
// 0 - colinear
// 1 - clockwise
// 2 - counter clockwise
static int orientation(QPoint& p1, QPoint& p2, QPoint& p3);
//checks if q is on segment p1-p2
static bool onSegment(QPoint& p1, QPoint& q, QPoint& p2);
//cheks if p1-q1 intersects with p2-q2
static bool hasIntersection(QPoint& p1, QPoint& q1, QPoint& p2, QPoint& q2);
};
#endif

View File

@@ -16,13 +16,21 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
IntelliPhotoGui.cpp \
PaintingArea.cpp \
GUI/IntelliPhotoGui.cpp \
Image/IntelliImage.cpp \
Image/IntelliRasterImage.cpp \
Image/IntelliShapedImage.cpp \
IntelliHelper/IntelliHelper.cpp \
Layer/PaintingArea.cpp \
main.cpp
HEADERS += \
IntelliPhotoGui.h \
PaintingArea.h
GUI/IntelliPhotoGui.h \
Image/IntelliImage.h \
Image/IntelliRasterImage.h \
Image/IntelliShapedImage.h \
IntelliHelper/IntelliHelper.h \
Layer/PaintingArea.h
FORMS += \
widget.ui

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.2, 2019-11-22T08:10:33. -->
<!-- Written by QtCreator 4.10.2, 2019-11-26T13:44:45. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -59,6 +59,11 @@
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</data>
<data>

View File

@@ -2,10 +2,31 @@
#include <QtWidgets>
#include "PaintingArea.h"
#include "Image/IntelliRasterImage.h"
#include "Image/IntelliShapedimage.h"
PaintingArea::PaintingArea(QWidget *parent)
: QWidget(parent)
{
//create standart image
this->image = new IntelliRasterimage(400,200);
this->setUp();
}
PaintingArea::PaintingArea(int width, int height, ImageType type, QWidget *parent)
: QWidget(parent){
if(type==ImageType::Raster_Image){
this->image = new IntelliRasterimage(width, height);
}else if(type==ImageType::Shaped_Image){
this->image = new IntelliShapedImage(width, height);
}else{
qDebug() << "No valid Image type error";
return;
}
}
void PaintingArea::setUp(){
// Roots the widget to the top left even if resized
setAttribute(Qt::WA_StaticContents);
@@ -19,26 +40,14 @@ PaintingArea::PaintingArea(QWidget *parent)
// Used to load the image and place it in the widget
bool PaintingArea::openImage(const QString &fileName)
{
// Holds the image
QImage loadedImage;
// If the image wasn't loaded leave this function
if (!loadedImage.load(fileName))
return false;
QSize newSize = loadedImage.size().expandedTo(size());
resizeImage(&loadedImage, newSize);
image = loadedImage;
modified = false;
update();
return true;
return image->loadImage(fileName);
}
// Save the current image
bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
{
// Created to hold the image
QImage visibleImage = image;
QImage visibleImage = image->getDisplayable(size());
resizeImage(&visibleImage, size());
if (visibleImage.save(fileName, fileFormat)) {
@@ -64,7 +73,7 @@ void PaintingArea::setPenWidth(int newWidth)
// Color the image area with white
void PaintingArea::clearImage()
{
image.fill(qRgb(255, 255, 255));
image->floodFill(qRgb(255, 255, 255));
modified = true;
update();
}
@@ -110,33 +119,27 @@ void PaintingArea::paintEvent(QPaintEvent *event)
// Draws the rectangle where the image needs to
// be updated
painter.drawImage(dirtyRect, image, dirtyRect);
//painter.drawImage(dirtyRect, image, dirtyRect);
}
// Resize the image to slightly larger then the main window
// to cut down on the need to resize the image
void PaintingArea::resizeEvent(QResizeEvent *event)
{
if (width() > image.width() || height() > image.height()) {
int newWidth = qMax(width() + 128, image.width());
int newHeight = qMax(height() + 128, image.height());
resizeImage(&image, QSize(newWidth, newHeight));
update();
}
//resizing done here
//if (width() > image.width() || height() > image.height()) {
// int newWidth = qMax(width() + 128, image.width());
// int newHeight = qMax(height() + 128, image.height());
// resizeImage(&image, QSize(newWidth, newHeight));
// update();
//}
QWidget::resizeEvent(event);
}
void PaintingArea::drawLineTo(const QPoint &endPoint)
{
// Used to draw on the widget
QPainter painter(&image);
// Set the current settings for the pen
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
// Draw a line from the last registered point to the current
painter.drawLine(lastPoint, endPoint);
image->drawLine(lastPoint, endPoint,myPenColor, myPenWidth);
// Set that the image hasn't been saved
modified = true;

View File

@@ -4,6 +4,7 @@
#include <QColor>
#include <QImage>
#include"Image/IntelliImage.h"
#include <QPoint>
#include <QWidget>
@@ -15,7 +16,9 @@ class PaintingArea : public QWidget
Q_OBJECT
public:
//create raster image 400*200
PaintingArea(QWidget *parent = nullptr);
PaintingArea(int width, int height, ImageType type, QWidget *parent = nullptr);
// Handles all events
bool openImage(const QString &fileName);
@@ -33,6 +36,8 @@ public slots:
// Events to handle
void clearImage();
//void setUp helper for konstruktor
void setUp();
protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
@@ -62,7 +67,7 @@ private:
QColor myPenColor;
// Stores the image being drawn
QImage image;
IntelliImage* image;
// Stores the location at the current mouse event
QPoint lastPoint;

View File

@@ -1,4 +1,4 @@
#include "IntelliPhotoGui.h"
#include "GUI/IntelliPhotoGui.h"
#include <QApplication>
int main(int argc, char *argv[])