-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageprocessor.cpp
More file actions
119 lines (106 loc) · 2.75 KB
/
imageprocessor.cpp
File metadata and controls
119 lines (106 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "imageprocessor.h"
ImageProcessor::ImageProcessor(QObject *parent)
: QObject(parent)
, currentImage(0)
{
}
void ImageProcessor::addImage(QImage newImage)
{
if (currentImage < images.length())
images.erase(image + 1, images.end());
images.append(newImage);
if (!currentImage)
image = images.begin();
else
image = images.end() - 1;
++currentImage;
}
bool ImageProcessor::canRedo()
{
return (currentImage < images.length() ? true : false);
}
bool ImageProcessor::canUndo()
{
return (currentImage > 1 ? true : false);
}
QImage* ImageProcessor::flip(eFlip direction)
{
addImage((kFlipHorizontal == direction) ? image->mirrored(true, false) : image->mirrored(false, true));
return image;
}
QImage* ImageProcessor::redo()
{
if (canRedo())
{
++image;
++currentImage;
}
return image;
}
void ImageProcessor::reset()
{
currentImage = 0;
images.clear();
}
QImage* ImageProcessor::rotate(int angle)
{
QPixmap pixmap = QPixmap::fromImage(*image);
QMatrix matrix;
matrix.rotate(angle);
addImage(pixmap.transformed(matrix).toImage());
return image;
}
void ImageProcessor::setImage(const QImage &image)
{
// if (currentImage)
// {
// currentImage = 0;
// images.erase(images.begin(), images.end());
// }
addImage(image);
}
QImage* ImageProcessor::toGrayscale()
{
QImage tempImage = image->convertToFormat(QImage::Format_RGB32);
for (int i = 0; i < tempImage.height(); i++)
{
QRgb *pixel = reinterpret_cast<QRgb *>(tempImage.scanLine(i));
QRgb *end = pixel + tempImage.width();
while (pixel++ != end)
{
int color = qGray(*pixel);
*pixel = QColor(color, color, color).rgb();
}
}
addImage(tempImage);
return image;
}
QImage* ImageProcessor::toSepia()
{
QImage tempImage = image->convertToFormat(QImage::Format_RGB32);
for (int i = 0; i < tempImage.height(); i++)
{
QRgb *pixel = reinterpret_cast<QRgb *>(tempImage.scanLine(i));
QRgb *end = pixel + tempImage.width();
while (pixel++ != end)
{
int red = qRed(*pixel) * 0.393 + qGreen(*pixel) * 0.769 + qBlue(*pixel) * 0.189;
int green = qRed(*pixel) * 0.349 + qGreen(*pixel) * 0.686 + qBlue(*pixel) * 0.168;
int blue = qRed(*pixel) * 0.272 + qGreen(*pixel) * 0.534 + qBlue(*pixel) * 0.131;
*pixel = QColor(red < 255 ? red : 255,
green < 255 ? green : 255,
blue < 255 ? blue : 255).rgb();
}
}
addImage(tempImage);
return image;
}
QImage* ImageProcessor::undo()
{
if (canUndo())
{
--image;
--currentImage;
}
return image;
}