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->fastRenderering = fastRendererOn;
15 
16 }
17 
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(fastRenderering ? 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  if(fastRenderering) {
50  *image = newImage.convertToFormat(QImage::Format_Indexed8);
51  }
52  else{
53  *image = newImage;
54  }
55 }
56 
57 void IntelliImage::drawPixel(const QPoint &p1, const QColor& color){
58  if(fastRenderering) {
59  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
60  }
61  // Used to draw on the widget
62  QPainter* painter = new QPainter(&imageData);
63 
64  // Set the current settings for the pen
65  painter->setPen(QPen(color, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
66 
67  // Draw a line from the last registered point to the current
68  painter->drawPoint(p1);
69  delete painter;
70  painter = nullptr;
71 
72  if(fastRenderering) {
73  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
74  }
75 }
76 
77 void IntelliImage::drawPoint(const QPoint &p1, const QColor& color, const int& penWidth){
78  if(fastRenderering) {
79  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
80  }
81  // Used to draw on the widget
82  QPainter* painter = new QPainter(&imageData);
83 
84  // Set the current settings for the pen
85  painter->setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
86  // Draw a line from the last registered point to the current
87  painter->drawPoint(p1);
88  delete painter;
89 
90  painter = nullptr;
91  if(fastRenderering) {
92  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
93  }
94 }
95 
96 void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth){
97  if(fastRenderering) {
98  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
99  }
100  // Used to draw on the widget
101  QPainter* painter = new QPainter(&imageData);
102 
103  // Set the current settings for the pen
104  painter->setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
105 
106  // Draw a line from the last registered point to the current
107  painter->drawLine(p1, p2);
108  delete painter;
109  painter = nullptr;
110 
111  if(fastRenderering) {
112  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
113  }
114 }
115 
116 void IntelliImage::drawPlain(const QColor& color){
117  if(fastRenderering) {
118  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
119  }
120  imageData.fill(color);
121  if(fastRenderering) {
122  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
123  }
124 }
125 
126 QColor IntelliImage::getPixelColor(QPoint& point){
127  if(fastRenderering) {
128  QImage copy = this->imageData.convertToFormat(QImage::Format_ARGB32);
129  return copy.pixelColor(point);
130  }
131  return imageData.pixelColor(point);
132 }
133 
135  QImage copy = imageData;
136  if(fastRenderering) {
137  copy = copy.convertToFormat(QImage::Format_ARGB32);
138  }
139  return copy;
140 }
141 
142 void IntelliImage::setImageData(const QImage& newData){
143  imageData = newData;
144  if(fastRenderering) {
145  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
146  }
147  else {
148  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
149  }
150 }
151 
152 void IntelliImage::updateRendererSetting(bool fastRendererOn){
153  this->fastRenderering = fastRendererOn;
154  if(fastRenderering) {
155  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
156  }
157  else {
158  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
159  }
160 }
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:96
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:57
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 function that draws a point between on a given point in a given color.
Definition: IntelliImage.cpp:77
IntelliImage::updateRendererSetting
virtual void updateRendererSetting(bool fastRendererOn)
updateRendererSetting updates the existing image format to the new format.
Definition: IntelliImage.cpp:152
IntelliImage::fastRenderering
bool fastRenderering
fastRendering is the flag that represents the usage of 8bit pictures.
Definition: IntelliImage.h:51
IntelliImage::getImageData
virtual QImage getImageData()
getImageData returns the data of the current image (Note: It will allways return a ARGB32bit QImage!...
Definition: IntelliImage.cpp:134
IntelliImage::resizeImage
void resizeImage(QImage *image, const QSize &newSize)
Definition: IntelliImage.cpp:37
IntelliImage::setImageData
virtual void setImageData(const QImage &newData)
setImageData overwrites the old imageData the new imageData.
Definition: IntelliImage.cpp:142
IntelliImage::getPixelColor
virtual QColor getPixelColor(QPoint &point)
A function that returns the pixelcolor at a certain point.
Definition: IntelliImage.cpp:126
IntelliImage::imageData
QImage imageData
The underlying image data.
Definition: IntelliImage.h:41
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:116