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(&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  if(fastRenderering) {
70  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
71  }
72 }
73 
74 void IntelliImage::drawPoint(const QPoint &p1, const QColor& color, const int& penWidth){
75  if(fastRenderering) {
76  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
77  }
78  // Used to draw on the widget
79  QPainter painter(&imageData);
80 
81  // Set the current settings for the pen
82  painter.setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
83  // Draw a line from the last registered point to the current
84  painter.drawPoint(p1);
85  if(fastRenderering) {
86  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
87  }
88 }
89 
90 void IntelliImage::drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth){
91  if(fastRenderering) {
92  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
93  }
94  // Used to draw on the widget
95  QPainter painter(&imageData);
96 
97  // Set the current settings for the pen
98  painter.setPen(QPen(color, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
99 
100  // Draw a line from the last registered point to the current
101  painter.drawLine(p1, p2);
102  if(fastRenderering) {
103  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
104  }
105 }
106 
107 void IntelliImage::drawPlain(const QColor& color){
108  if(fastRenderering) {
109  this->imageData = this->imageData.convertToFormat(QImage::Format_ARGB32);
110  }
111  imageData.fill(color);
112  if(fastRenderering) {
113  this->imageData = this->imageData.convertToFormat(QImage::Format_Indexed8);
114  }
115 }
116 
117 QColor IntelliImage::getPixelColor(QPoint& point){
118  if(fastRenderering) {
119  QImage copy = this->imageData.convertToFormat(QImage::Format_ARGB32);
120  return copy.pixelColor(point);
121  }
122  return imageData.pixelColor(point);
123 }
124 
126  QImage copy = imageData;
127  if(fastRenderering) {
128  copy = copy.convertToFormat(QImage::Format_ARGB32);
129  }
130  return copy;
131 }
132 
133 void IntelliImage::setImageData(const QImage& newData){
134  imageData = newData;
135  if(fastRenderering) {
136  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
137  }
138  else {
139  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
140  }
141 }
142 
143 void IntelliImage::updateRendererSetting(bool fastRendererOn){
144  this->fastRenderering = fastRendererOn;
145  if(fastRenderering) {
146  this->imageData = imageData.convertToFormat(QImage::Format_Indexed8);
147  }
148  else {
149  this->imageData = imageData.convertToFormat(QImage::Format_ARGB32);
150  }
151 }
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:90
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.
Definition: IntelliImage.cpp:74
IntelliImage::updateRendererSetting
virtual void updateRendererSetting(bool fastRendererOn)
updateRendererSetting updates the existing image format to the new format.
Definition: IntelliImage.cpp:143
IntelliImage::fastRenderering
bool fastRenderering
fastRendering is the flag that represents the usage of 8bit pictures.
Definition: IntelliImage.h:47
IntelliImage::getImageData
virtual QImage getImageData()
getImageData returns the data of the current image (Note: It will allways return a ARGB32bit QImage!...
Definition: IntelliImage.cpp:125
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:133
IntelliImage::getPixelColor
virtual QColor getPixelColor(QPoint &point)
A function that returns the pixelcolor at a certain point.
Definition: IntelliImage.cpp:117
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:107