IntelliPhoto  1
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 
18 
20 
21 }
22 
23 bool IntelliImage::loadImage(const QString &filePath){
24  // Holds the image
25  QImage loadedImage;
26 
27  // If the image wasn't loaded leave this function
28  if (!loadedImage.load(filePath))
29  return false;
30 
31  // scaled Image to size of Layer
32  loadedImage = loadedImage.scaled(imageData.size(),Qt::IgnoreAspectRatio);
33 
34  imageData = loadedImage.convertToFormat(fastRenderering ? QImage::Format_Indexed8 : QImage::Format_ARGB32);
35  return true;
36 }
37 
38 void IntelliImage::resizeImage(QImage*image, const QSize &newSize){
39  // Check if we need to redraw the image
40  if (image->size() == newSize)
41  return;
42 
43  // Create a new image to display and fill it with white
44  QImage newImage(newSize, QImage::Format_ARGB32);
45  newImage.fill(qRgb(255, 255, 255));
46 
47  // Draw the image
48  QPainter painter(&newImage);
49  painter.drawImage(QPoint(0, 0), *image);
50  if(fastRenderering) {
51  *image = newImage.convertToFormat(QImage::Format_Indexed8);
52  }
53  else{
54  *image = newImage;
55  }
56 }
57 
58 void IntelliImage::drawPixel(const QPoint &p1, const QColor& color){
59  if(fastRenderering) {
60  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
61  }
62  // Used to draw on the widget
63  QPainter* painter = new QPainter(&imageData);
64 
65  // Set the current settings for the pen
66  painter->setPen(QPen(color, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
67 
68  // Draw a line from the last registered point to the current
69  painter->drawPoint(p1);
70  delete painter;
71  painter = nullptr;
72 
73  if(fastRenderering) {
74  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
75  }
76 }
77 
78 void IntelliImage::drawPoint(const QPoint &p1, const QColor& color, const int& penWidth){
79  if(fastRenderering) {
80  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
81  }
82  // Used to draw on the widget
83  QPainter* painter = new QPainter(&imageData);
84 
85  // Set the current settings for the pen
86  painter->setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
87  // Draw a line from the last registered point to the current
88  painter->drawPoint(p1);
89  delete painter;
90 
91  painter = nullptr;
92  if(fastRenderering) {
93  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
94  }
95 }
96 
97 void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth){
98  if(fastRenderering) {
99  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
100  }
101  // Used to draw on the widget
102  QPainter* painter = new QPainter(&imageData);
103 
104  // Set the current settings for the pen
105  painter->setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
106 
107  // Draw a line from the last registered point to the current
108  painter->drawLine(p1, p2);
109  delete painter;
110  painter = nullptr;
111 
112  if(fastRenderering) {
113  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
114  }
115 }
116 
117 void IntelliImage::drawPlain(const QColor& color){
118  if(fastRenderering) {
119  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
120  }
121  imageData.fill(color);
122  if(fastRenderering) {
123  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
124  }
125 }
126 
127 QColor IntelliImage::getPixelColor(QPoint& point){
128  if(fastRenderering) {
129  QImage copy = this->imageData.convertToFormat(QImage::Format_ARGB32);
130  return copy.pixelColor(point);
131  }
132  return imageData.pixelColor(point);
133 }
134 
136  QImage copy = imageData;
137  if(fastRenderering) {
138  copy = copy.convertToFormat(QImage::Format_ARGB32);
139  }
140  return copy;
141 }
142 
143 void IntelliImage::setImageData(const QImage& newData){
144  imageData = newData;
145  if(fastRenderering) {
146  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
147  }
148  else {
149  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
150  }
151 }
152 
153 void IntelliImage::updateRendererSetting(bool fastRendererOn){
154  this->fastRenderering = fastRendererOn;
155  if(fastRenderering) {
156  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
157  }
158  else {
159  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
160  }
161 }
162 
164  return imageData.width();
165 }
166 
168  return imageData.height();
169 }
170 
172  return this->fastRenderering;
173 }
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:97
IntelliImage.h
IntelliImage::~IntelliImage
virtual ~IntelliImage()=0
An Abstract Destructor.
Definition: IntelliImage.cpp:19
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:58
IntelliImage::isFastRendering
virtual bool isFastRendering() const
isFastRendering returns if the Image is in fast rendering mode.
Definition: IntelliImage.cpp:171
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:78
IntelliImage::updateRendererSetting
virtual void updateRendererSetting(bool fastRendererOn)
updateRendererSetting updates the existing image format to the new format.
Definition: IntelliImage.cpp:153
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:135
IntelliImage::resizeImage
void resizeImage(QImage *image, const QSize &newSize)
Definition: IntelliImage.cpp:38
IntelliImage::setImageData
virtual void setImageData(const QImage &newData)
setImageData overwrites the old imageData the new imageData.
Definition: IntelliImage.cpp:143
IntelliImage::getPixelColor
virtual QColor getPixelColor(QPoint &point)
A function that returns the pixelcolor at a certain point.
Definition: IntelliImage.cpp:127
IntelliImage::getHeight
virtual int getHeight() const
getHeight returns the height of the Image.
Definition: IntelliImage.cpp:167
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:23
IntelliImage::getWidth
virtual int getWidth() const
getWidth returns the width of the Image.
Definition: IntelliImage.cpp:163
IntelliImage::drawPlain
virtual void drawPlain(const QColor &color)
A function that clears the whole image in a given Color.
Definition: IntelliImage.cpp:117