IntelliPhoto 0.5

IntelliImage.cpp
Go to the documentation of this file.
1 #include "Image/IntelliImage.h"
2 #include <QSize>
3 #include <QPainter>
4
5 IntelliImage::IntelliImage(int width, int height, bool fastRendererOn)
6 : imageData(QSize(width, height), fastRendererOn ? QImage::Format_Indexed8 : QImage::Format_ARGB32){
7 if(fastRendererOn) {
8 imageData = imageData.convertToFormat(QImage::Format_ARGB32);
9 }
10 imageData.fill(QColor(255,255,255,255));
11 if(fastRendererOn) {
12 imageData = imageData.convertToFormat(QImage::Format_Indexed8);
13 }
14 this->fastRenderer = fastRendererOn;
15
16 }
17
18 IntelliImage::~IntelliImage(){
19
20 }
21
22 bool IntelliImage::loadImage(const QString &filePath){
23 // Holds the image
24 QImage loadedImage;
25
26 // If the image wasn't loaded leave this function
27 if (!loadedImage.load(filePath))
28 return false;
29
30 // scaled Image to size of Layer
31 loadedImage = loadedImage.scaled(imageData.size(),Qt::IgnoreAspectRatio);
32
33 imageData = loadedImage.convertToFormat(fastRenderer ? QImage::Format_Indexed8 : QImage::Format_ARGB32);
34 return true;
35 }
36
37 void IntelliImage::resizeImage(QImage*image, const QSize &newSize){
38 // Check if we need to redraw the image
39 if (image->size() == newSize)
40 return;
41
42 // Create a new image to display and fill it with white
43 QImage newImage(newSize, QImage::Format_ARGB32);
44 newImage.fill(qRgb(255, 255, 255));
45
46 // Draw the image
47 QPainter painter(&newImage);
48 painter.drawImage(QPoint(0, 0), *image);
49 *image = newImage;
50 if(fastRenderer) {
51 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
52 }
53 }
54
55 void IntelliImage::drawPixel(const QPoint &p1, const QColor& color){
56 if(fastRenderer) {
57 this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
58 }
59 // Used to draw on the widget
60 QPainter painter(&imageData);
61
62 // Set the current settings for the pen
63 painter.setPen(QPen(color, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
64
65 // Draw a line from the last registered point to the current
66 painter.drawPoint(p1);
67 if(fastRenderer) {
68 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
69 }
70 }
71
72 void IntelliImage::drawPoint(const QPoint &p1, const QColor& color, const int& penWidth){
73 if(fastRenderer) {
74 this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
75 }
76 // Used to draw on the widget
77 QPainter painter(&imageData);
78
79 // Set the current settings for the pen
80 painter.setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
81 // Draw a line from the last registered point to the current
82 painter.drawPoint(p1);
83 if(fastRenderer) {
84 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
85 }
86 }
87
88 void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth){
89 if(fastRenderer) {
90 this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
91 }
92 // Used to draw on the widget
93 QPainter painter(&imageData);
94
95 // Set the current settings for the pen
96 painter.setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
97
98 // Draw a line from the last registered point to the current
99 painter.drawLine(p1, p2);
100 if(fastRenderer) {
101 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
102 }
103 }
104
105 void IntelliImage::drawPlain(const QColor& color){
106 if(fastRenderer) {
107 this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
108 }
109 imageData.fill(color);
110 if(fastRenderer) {
111 this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
112 }
113 }
114
115 QColor IntelliImage::getPixelColor(QPoint& point){
116 if(fastRenderer) {
117 QImage copy = this->imageData.convertToFormat(QImage::Format_ARGB32);
118 return copy.pixelColor(point);
119 }
120 return imageData.pixelColor(point);
121 }
122
123 QImage IntelliImage::getImageData(){
124 return this->imageData;
125 }
126
127 void IntelliImage::updateRendererSetting(bool fastRendererOn){
128 this->fastRenderer = fastRendererOn;
129 this->imageData = this->imageData.convertToFormat(fastRenderer ? QImage::Format_Indexed8 : QImage::Format_ARGB32);
130 }
IntelliImage::drawLine
virtual void drawLine(const QPoint &p1, const QPoint &p2, const QColor &color, const int &penWidth)
A function that draws A Line between two given Points in a given color.
Definition: IntelliImage.cpp:88
IntelliImage.h
IntelliImage::~IntelliImage
virtual ~IntelliImage()=0
An Abstract Destructor.
Definition: IntelliImage.cpp:18
IntelliImage::drawPixel
virtual void drawPixel(const QPoint &p1, const QColor &color)
A funtcion used to draw a pixel on the Image with the given Color.
Definition: IntelliImage.cpp:55
IntelliImage::IntelliImage
IntelliImage(int width, int height, bool fastRendererOn)
The Construcor of the IntelliImage. Given the Image dimensions.
Definition: IntelliImage.cpp:5
IntelliImage::drawPoint
virtual void drawPoint(const QPoint &p1, const QColor &color, const int &penWidth)
A.
Definition: IntelliImage.cpp:72
IntelliImage::updateRendererSetting
virtual void updateRendererSetting(bool fastRendererOn)
updateRendererSetting updates the existing image format to the new format.
Definition: IntelliImage.cpp:127
IntelliImage::getImageData
virtual QImage getImageData()
getImageData returns the data of the current image.
Definition: IntelliImage.cpp:123
IntelliImage::resizeImage
void resizeImage(QImage *image, const QSize &newSize)
Definition: IntelliImage.cpp:37
IntelliImage::getPixelColor
virtual QColor getPixelColor(QPoint &point)
A function that returns the pixelcolor at a certain point.
Definition: IntelliImage.cpp:115
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::loadImage
virtual bool loadImage(const QString &filePath)
A function that loads and sclaes an image to the fitting dimensions.
Definition: IntelliImage.cpp:22
IntelliImage::drawPlain
virtual void drawPlain(const QColor &color)
A function that clears the whole image in a given Color.
Definition: IntelliImage.cpp:105