IntelliPhoto 0.6

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 = ImageType::SHAPEDIMAGE;
10 this->fastRenderering = fastRendererOn;
11 }
12
13 IntelliShapedImage* IntelliShapedImage::copy(const IntelliShapedImage& image){
14 this->TypeOfImage = ImageType::SHAPEDIMAGE;
15 IntelliShapedImage* shaped = new IntelliShapedImage(imageData.width(), imageData.height(), this->fastRenderering);
16 shaped->imageData.copy(0,0,image.getWidth(),image.getWidth());
17 return shaped;
18 }
19
20 IntelliShapedImage::~IntelliShapedImage(){
21
22 }
23
24 QImage IntelliShapedImage::getDisplayable(int alpha){
25 return getDisplayable(imageData.size(),alpha);
26 }
27
28 IntelliImage* IntelliShapedImage::getDeepCopy(){
29 IntelliShapedImage* shaped = new IntelliShapedImage(imageData.width(), imageData.height(), this->fastRenderering);
30 shaped->setPolygon(this->polygonData);
31 shaped->imageData.fill(Qt::transparent);
32 shaped->TypeOfImage = ImageType::SHAPEDIMAGE;
33 return shaped;
34 }
35
36 void IntelliShapedImage::calculateVisiblity(){
37 if(polygonData.size()<2) {
38 return;
39 }
40 if(fastRenderering) {
41 this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
42 }
43
44 if(polygonData.size()<=2) {
45 QColor clr;
46 for(int y = 0; y<imageData.height(); y++) {
47 for(int x = 0; x<imageData.width(); x++) {
48 clr = imageData.pixel(x,y);
49 clr.setAlpha(255);
50 imageData.setPixelColor(x,y,clr);
51 }
52 }
53 if(fastRenderering) {
54 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
55 }
56 return;
57 }
58 QColor clr;
59 for(int y = 0; y<imageData.height(); y++) {
60 for(int x = 0; x<imageData.width(); x++) {
61 QPoint ptr(x,y);
62 clr = imageData.pixelColor(x,y);
63 bool isInPolygon = IntelliTriangulation::isInPolygon(triangles, ptr);
64 if(isInPolygon) {
65 clr.setAlpha(std::min(255, clr.alpha()));
66 }else{
67 clr.setAlpha(0);
68 }
69 imageData.setPixelColor(x,y,clr);
70 }
71 }
72 if(fastRenderering) {
73 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
74 }
75 }
76
77 QImage IntelliShapedImage::getDisplayable(const QSize& displaySize, int alpha){
78 QImage copy = imageData;
79 if(fastRenderering) {
80 copy = copy.convertToFormat(QImage::Format_ARGB32);
81 }
82 for(int y = 0; y<copy.height(); y++) {
83 for(int x = 0; x<copy.width(); x++) {
84 QColor clr = copy.pixelColor(x,y);
85 clr.setAlpha(std::min(alpha,clr.alpha()));
86 copy.setPixelColor(x,y, clr);
87 }
88 }
89 if(fastRenderering) {
90 copy = copy.convertToFormat(QImage::Format_Indexed8);
91 }
92 return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
93 }
94
95 void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
96 if(polygonData.size()<3) {
97 this->polygonData.clear();
98 }else{
99 this->polygonData.clear();
100 for(auto element:polygonData) {
101 this->polygonData.push_back(QPoint(element.x(), element.y()));
102 }
103 triangles = IntelliTriangulation::calculateTriangles(polygonData);
104 if(fastRenderering) {
105 imageData = imageData.convertToFormat(QImage::Format_ARGB32);
106 }
107 for(int y = 0; y<imageData.height(); y++) {
108 for(int x = 0; x<imageData.width(); x++) {
109 QColor clr = imageData.pixelColor(x,y);
110 clr.setAlpha(255);
111 imageData.setPixelColor(x,y,clr);
112 }
113 }
114 if(fastRenderering) {
115 imageData = imageData.convertToFormat(QImage::Format_Indexed8);
116 }
117 }
118 calculateVisiblity();
119 return;
120 }
121
122 std::vector<QPoint> IntelliShapedImage::getPolygon(){
123 return polygonData;
124 }
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:77
IntelliShapedImage::copy
IntelliShapedImage * copy(const IntelliShapedImage &image)
Definition: IntelliShapedImage.cpp:13
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::getPolygon
virtual std::vector< QPoint > getPolygon() override
getPolygon
Definition: IntelliShapedImage.cpp:122
IntelliShapedImage
The IntelliShapedImage manages a Shapedimage.
Definition: IntelliShapedImage.h:13
IntelliShapedImage::getDeepCopy
virtual IntelliImage * getDeepCopy() override
A function that copys all that returns a [allocated] Image.
Definition: IntelliShapedImage.cpp:28
IntelliImage::fastRenderering
bool fastRenderering
fastRendering is the flag that represents the usage of 8bit pictures.
Definition: IntelliImage.h:51
ImageType::SHAPEDIMAGE
@ SHAPEDIMAGE
IntelliImage::TypeOfImage
ImageType TypeOfImage
The Type, an Image is.
Definition: IntelliImage.h:46
IntelliImage::imageData
QImage imageData
The underlying image data.
Definition: IntelliImage.h:41
IntelliImage
An abstract class which manages the basic IntelliImage operations.
Definition: IntelliImage.h:30
IntelliShapedImage::polygonData
std::vector< QPoint > polygonData
The Vertices of The Polygon. Needs to be a planar Polygon.
Definition: IntelliShapedImage.h:31
IntelliShapedImage::IntelliShapedImage
IntelliShapedImage(int width, int height, bool fastRendererOn)
The Construcor of the IntelliShapedImage. Given the Image dimensions.
Definition: IntelliShapedImage.cpp:7
IntelliTriangulation::isInPolygon
bool isInPolygon(const 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
IntelliShapedImage::~IntelliShapedImage
virtual ~IntelliShapedImage() override
An Destructor.
Definition: IntelliShapedImage.cpp:20
IntelliRasterImage
The IntelliRasterImage manages a RASTERIMAGE.
Definition: IntelliRasterImage.h:12
IntelliTriangulation.h
IntelliImage::getWidth
virtual int getWidth() const
Definition: IntelliImage.cpp:163
IntelliShapedImage::setPolygon
virtual void setPolygon(const std::vector< QPoint > &polygonData) override
A function that sets the data of the visible Polygon.
Definition: IntelliShapedImage.cpp:95