IntelliPhoto 0.5

IntelliShapedImage.cpp
Go to the documentation of this file.
1 #include "Image/IntelliShapedImage.h"
2 #include "IntelliHelper/IntelliTriangulation.h"
3 #include <QPainter>
4 #include <QRect>
5 #include <QDebug>
6
7 IntelliShapedImage::IntelliShapedImage(int width, int height, bool fastRendererOn)
8 : IntelliRasterImage(width, height, fastRendererOn){
9 TypeOfImage = IntelliImage::ImageType::SHAPEDIMAGE;
10 this->fastRenderer = fastRendererOn;
11 }
12
13 IntelliShapedImage::~IntelliShapedImage(){
14
15 }
16
17 QImage IntelliShapedImage::getDisplayable(int alpha){
18 return getDisplayable(imageData.size(),alpha);
19 }
20
21 IntelliImage* IntelliShapedImage::getDeepCopy(){
22 IntelliShapedImage* shaped = new IntelliShapedImage(imageData.width(), imageData.height(), false);
23 shaped->setPolygon(this->polygonData);
24 shaped->imageData.fill(Qt::transparent);
25 shaped->TypeOfImage = IntelliImage::ImageType::SHAPEDIMAGE;
26 return shaped;
27 }
28
29 void IntelliShapedImage::calculateVisiblity(){
30 if(fastRenderer) {
31 this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
32 }
33
34 if(polygonData.size()<=2) {
35 QColor clr;
36 for(int y=0; y<imageData.height(); y++) {
37 for(int x=0; x<imageData.width(); x++) {
38 clr = imageData.pixel(x,y);
39 clr.setAlpha(255);
40 imageData.setPixelColor(x,y,clr);
41 }
42 }
43 if(fastRenderer) {
44 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
45 }
46 return;
47 }
48 QColor clr;
49 for(int y=0; y<imageData.height(); y++) {
50 for(int x=0; x<imageData.width(); x++) {
51 QPoint ptr(x,y);
52 clr = imageData.pixelColor(x,y);
53 bool isInPolygon = IntelliTriangulation::isInPolygon(triangles, ptr);
54 if(isInPolygon) {
55 clr.setAlpha(std::min(255, clr.alpha()));
56 }else{
57 clr.setAlpha(0);
58 }
59 imageData.setPixelColor(x,y,clr);
60 }
61 }
62 if(fastRenderer) {
63 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
64 }
65 }
66
67 QImage IntelliShapedImage::getDisplayable(const QSize& displaySize, int alpha){
68 QImage copy = imageData;
69 if(fastRenderer) {
70 copy = copy.convertToFormat(QImage::Format_ARGB32);
71 }
72 for(int y = 0; y<copy.height(); y++) {
73 for(int x = 0; x<copy.width(); x++) {
74 QColor clr = copy.pixelColor(x,y);
75 clr.setAlpha(std::min(alpha,clr.alpha()));
76 copy.setPixelColor(x,y, clr);
77 }
78 }
79 if(fastRenderer) {
80 copy = copy.convertToFormat(QImage::Format_Indexed8);
81 }
82 return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
83 }
84
85 void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
86 if(polygonData.size()<3) {
87 this->polygonData.clear();
88 }else{
89 this->polygonData.clear();
90 for(auto element:polygonData) {
91 this->polygonData.push_back(QPoint(element.x(), element.y()));
92 }
93 triangles = IntelliTriangulation::calculateTriangles(polygonData);
94 }
95 calculateVisiblity();
96 return;
97 }
IntelliShapedImage::getDisplayable
virtual QImage getDisplayable(const QSize &displaySize, int alpha=255) override
A function returning the displayable ImageData in a requested transparence and size.
Definition: IntelliShapedImage.cpp:67
IntelliShapedImage.h
IntelliTriangulation::calculateTriangles
std::vector< Triangle > calculateTriangles(std::vector< QPoint > polyPoints)
A function to split a polygon in its spanning traingles by using Meisters Theorem of graph theory by ...
Definition: IntelliTriangulation.cpp:7
IntelliShapedImage
The IntelliShapedImage manages a Shapedimage.
Definition: IntelliShapedImage.h:10
IntelliShapedImage::getDeepCopy
virtual IntelliImage * getDeepCopy() override
A function that copys all that returns a [allocated] Image.
Definition: IntelliShapedImage.cpp:21
IntelliImage::ImageType::SHAPEDIMAGE
IntelliTriangulation::isInPolygon
bool isInPolygon(std::vector< Triangle > &triangles, QPoint &point)
A function to check if a point lies in a polygon by checking its spanning triangles.
Definition: IntelliTriangulation.cpp:116
IntelliImage::TypeOfImage
ImageType TypeOfImage
The Type, an Image is.
Definition: IntelliImage.h:42
IntelliImage::fastRenderer
bool fastRenderer
fastRenderer is the flag that represents the usage of 8bit pictures.
Definition: IntelliImage.h:47
IntelliImage::imageData
QImage imageData
The underlying image data.
Definition: IntelliImage.h:37
IntelliImage
An abstract class which manages the basic IntelliImage operations.
Definition: IntelliImage.h:19
IntelliShapedImage::polygonData
std::vector< QPoint > polygonData
The Vertices of The Polygon. Needs to be a planar Polygon.
Definition: IntelliShapedImage.h:27
IntelliShapedImage::IntelliShapedImage
IntelliShapedImage(int width, int height, bool fastRendererOn)
The Construcor of the IntelliShapedImage. Given the Image dimensions.
Definition: IntelliShapedImage.cpp:7
IntelliShapedImage::~IntelliShapedImage
virtual ~IntelliShapedImage() override
An Destructor.
Definition: IntelliShapedImage.cpp:13
IntelliRasterImage
The IntelliRasterImage manages a RASTERIMAGE.
Definition: IntelliRasterImage.h:9
IntelliTriangulation.h
IntelliShapedImage::setPolygon
virtual void setPolygon(const std::vector< QPoint > &polygonData) override
A function that sets the data of the visible Polygon.
Definition: IntelliShapedImage.cpp:85