Image structure

This commit is contained in:
Sonaion
2019-11-28 16:34:26 +01:00
parent bccca91111
commit a23e4bdd9c
13 changed files with 129 additions and 97 deletions

9
IntelliPhoto/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmp0l4rts
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmp0l4rts
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmp0l4rts
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmpcvzqoi
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmpu6gfxp
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmpqakvqm
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmpcvzqoi
IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug/usersjonasappdatalocaltemptmp0l4rts

View File

@@ -3,8 +3,8 @@
#include<QPainter>
IntelliImage::IntelliImage(int weight, int height)
:imageData(QSize(weight, height), QImage::Format_RGB32){
:imageData(QSize(weight, height), QImage::Format_ARGB32){
imageData.fill(QColor(255,255,255,255));
}
IntelliImage::~IntelliImage(){
@@ -19,9 +19,8 @@ bool IntelliImage::loadImage(const QString &fileName){
if (!loadedImage.load(fileName))
return false;
QSize newSize = loadedImage.size().expandedTo(imageData.size());
resizeImage(&loadedImage, newSize);
imageData = loadedImage;
loadedImage =loadedImage.scaled(imageData.size(),Qt::IgnoreAspectRatio);
imageData= loadedImage.convertToFormat(QImage::Format_ARGB32);
return true;
}
@@ -31,7 +30,7 @@ void IntelliImage::resizeImage(QImage *image, const QSize &newSize){
return;
// Create a new image to display and fill it with white
QImage newImage(newSize, QImage::Format_RGB32);
QImage newImage(newSize, QImage::Format_ARGB32);
newImage.fill(qRgb(255, 255, 255));
// Draw the image
@@ -68,4 +67,13 @@ void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& co
void IntelliImage::floodFill(const QColor& color){
imageData.fill(color);
}
int IntelliImage::x(){
return imageData.size().width();
}
int IntelliImage::y(){
return imageData.size().height();
}

View File

@@ -5,6 +5,7 @@
#include<QPoint>
#include<QColor>
#include<QSize>
#include<QWidget>
#include<vector>
enum class ImageType{
@@ -13,6 +14,7 @@ enum class ImageType{
};
class IntelliImage{
protected:
void resizeImage(QImage *image, const QSize &newSize);
@@ -28,11 +30,17 @@ public:
//returns the filtered output
virtual QImage getDisplayable(const QSize& displaySize)=0;
virtual QImage getDisplayable()=0;
//returns the filtered output
//sets the data for the visible image
virtual void setPolygon(const std::vector<QPoint>& polygonData)=0;
virtual bool loadImage(const QString &fileName);
int x();
int y();
};
#endif

View File

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

View File

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

View File

@@ -12,12 +12,14 @@ IntelliShapedImage::~IntelliShapedImage(){
}
QImage IntelliShapedImage::getDisplayable(){
return getDisplayable(imageData.size());
}
QImage IntelliShapedImage::getDisplayable(const QSize& displaySize){
QImage copy = imageData;
QPoint extrem(copy.width()+1, 0);
QPoint startPoint(0,0);
//traverse through y direction
QPoint startPoint;
QPoint extrem(0,copy.width()+1);
for(int y = 0; y<copy.height(); y++){
extrem.setY(y);
startPoint.setY(y);
@@ -25,24 +27,27 @@ QImage IntelliShapedImage::getDisplayable(const QSize& displaySize){
for(int x=0; x<copy.width(); x++){
startPoint.setX(x);
//traverse all edges
int cutNumber = 0;
int cutNumberX = 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);
cutNumberX+=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));
if(!(cutNumberX&1)){
QColor tmpColor(0,0,0);
tmpColor.setAlpha(0);
copy.setPixelColor(startPoint,tmpColor);
}
}
}
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
}
void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
this->polygonData=polygonData;
for(auto element:polygonData){
this->polygonData.push_back(QPoint(element.x(), element.y()));
}
return;
}

View File

@@ -4,6 +4,7 @@
#include"Image/IntelliImage.h"
class IntelliShapedImage : public IntelliImage{
protected:
std::vector<QPoint> polygonData;
public:
@@ -12,6 +13,7 @@ public:
//returns the filtered output
virtual QImage getDisplayable(const QSize& displaySize) override;
virtual QImage getDisplayable() override;
//sets the data for the visible image
virtual void setPolygon(const std::vector<QPoint>& polygonData) override;

View File

@@ -1,9 +1,9 @@
#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());
int IntelliHelper::orientation(QPoint& p, QPoint& q, QPoint& r){
int value = (q.y()-p.y())*(r.x()-q.x())-
(q.x()-p.x())*(r.y()-q.y());
if(value==0) return 0;
return (value>0)?1:2;
}

View File

@@ -35,6 +35,10 @@ HEADERS += \
FORMS += \
widget.ui
QMAKE_CXXFLAGS += -fopenmp
QMAKE_LFLAGS += -fopenmp
RC_ICONS = icon.ico
# Default rules for deployment.

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.2, 2019-11-26T13:44:45. -->
<!-- Written by QtCreator 4.10.2, 2019-11-28T16:32:32. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -72,7 +72,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.12.5 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.12.5 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5125.win64_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@@ -322,7 +322,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/jonas/OneDrive/Documents/GitHub/intelliphoto/IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Debug</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/jonas/OneDrive/Documents/GitHub/intelliphoto/IntelliPhoto/build-IntelliPhoto-Desktop_Qt_5_12_5_MinGW_64_bit-Release</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>

View File

@@ -1,57 +1,69 @@
// ---------- PaintingArea.cpp ----------
#include <QtWidgets>
#include<QRect>
#include "PaintingArea.h"
#include "Image/IntelliRasterImage.h"
#include "Image/IntelliShapedimage.h"
#include "Image/IntelliShapedImage.h"
#include<vector>
#include<QPoint>
PaintingArea::PaintingArea(QWidget *parent)
: QWidget(parent)
{
//create standart image
this->image = new IntelliRasterimage(400,200);
this->image = new IntelliRasterImage(400,400);
std::vector<QPoint> poly;
poly.push_back(QPoint(200,0));
poly.push_back(QPoint(400,300));
poly.push_back(QPoint(0,300));
poly.push_back(QPoint(200,0));
image->setPolygon(poly);
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);
// Set defaults for the monitored variables
modified = false;
scribbling = false;
myPenWidth = 1;
myPenColor = Qt::blue;
}
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;
}
this->setUp();
}
// Used to load the image and place it in the widget
bool PaintingArea::openImage(const QString &fileName)
{
return image->loadImage(fileName);
bool open = image->loadImage(fileName);
update();
return open;
}
// Save the current image
bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
{
// Created to hold the image
QImage visibleImage = image->getDisplayable(size());
resizeImage(&visibleImage, size());
QImage visibleImage = image->getDisplayable();
if (visibleImage.save(fileName, fileFormat)) {
modified = false;
return true;
} else {
return false;
@@ -74,7 +86,6 @@ void PaintingArea::setPenWidth(int newWidth)
void PaintingArea::clearImage()
{
image->floodFill(qRgb(255, 255, 255));
modified = true;
update();
}
@@ -84,25 +95,35 @@ void PaintingArea::clearImage()
void PaintingArea::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
lastPoint = event->pos();
int x = event->x()*(float)image->x()/(float)size().width();
int y = event->y()*(float)image->y()/(float)size().height();
lastPoint=QPoint(x,y);
scribbling = true;
}
}
// When the mouse moves if the left button is clicked
// we call the drawline function which draws a line
// from the last position to the current
void PaintingArea::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && scribbling)
drawLineTo(event->pos());
if ((event->buttons() & Qt::LeftButton) && scribbling){
int x = event->x()*(float)image->x()/(float)size().width();
int y = event->y()*(float)image->y()/(float)size().height();
drawLineTo(QPoint(x,y));
update();
}
}
// If the button is released we set variables to stop drawing
void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && scribbling) {
drawLineTo(event->pos());
int x = event->x()*(float)image->x()/(float)size().width();
int y = event->y()*(float)image->y()/(float)size().height();
drawLineTo(QPoint(x,y));
update();
scribbling = false;
}
}
@@ -113,62 +134,31 @@ void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
void PaintingArea::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// Returns the rectangle that needs to be updated
QRect dirtyRect = event->rect();
// Draws the rectangle where the image needs to
// be updated
//painter.drawImage(dirtyRect, image, dirtyRect);
QRect dirtyRec = event->rect();
painter.drawImage(dirtyRec, image->getDisplayable(dirtyRec.size()), dirtyRec);
update();
}
// 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)
{
//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);
QPainter painter(this);
QRect dirtyRec(QPoint(0,0), event->size());
painter.drawImage(dirtyRec, image->getDisplayable(event->size()), dirtyRec);
update();
//QWidget::resizeEvent(event);
}
void PaintingArea::drawLineTo(const QPoint &endPoint)
{
// Used to draw on the widget
image->drawLine(lastPoint, endPoint,myPenColor, myPenWidth);
// Set that the image hasn't been saved
modified = true;
int rad = (myPenWidth / 2) + 2;
// Call to update the rectangular space where we drew
update(QRect(lastPoint, endPoint).normalized()
.adjusted(-rad, -rad, +rad, +rad));
// Update the last position where we left off drawing
lastPoint = endPoint;
update();
}
// When the app is resized create a new image using
// the changes made to the image
void PaintingArea::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 PaintingArea::resizeImage(QImage *image_res, const QSize &newSize){
image_res->scaled(newSize,Qt::IgnoreAspectRatio);
}

View File

@@ -52,11 +52,11 @@ protected:
private:
void drawLineTo(const QPoint &endPoint);
void resizeImage(QImage *image, const QSize &newSize);
void resizeImage(QImage *image_res, const QSize &newSize);
// Will be marked true or false depending on if
// we have saved after a change
bool modified;
bool modified=false;
// Marked true or false depending on if the user
// is drawing

View File

@@ -1,16 +1,17 @@
#include "GUI/IntelliPhotoGui.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
// The main application
QApplication app(argc, argv);
// Create and open the main window
IntelliPhotoGui window;
window.show();
// Display the main window
return app.exec();
}