This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLayeredPixmap.cpp
216 lines (173 loc) · 5.11 KB
/
LayeredPixmap.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/****************************************/
// QT Paint
// File: LayeredPixmap.cpp
// Author: Hong Joon Choi
// Date: 01.04.2015
// Description: the header file
// -- LayeredPixmap class implementation.
//
/****************************************/
#include "LayeredPixmap.h"
using namespace std;
// constructor
LayeredPixmap::LayeredPixmap(const QWidget* parent, int count)
{
parentWidget = parent;
paint = new QPainter(parentWidget);
layerCount = count;
layers = new QPixmap*[count];
for (int i=0; i<count; i++)
{
layers[i] = new QPixmap();
}
displayLayer = new bool[count];
for (int i=0; i<count; i++)
displayLayer[i]=true;
currentIndexOnEdit=0;
}
// destructor
LayeredPixmap::LayeredPixmap(const LayeredPixmap& other)
{
parentWidget = other.parentWidget;
paint = new QPainter(parentWidget);
layerCount = other.layerCount;
layers = new QPixmap*[layerCount];
for (int i=0; i<layerCount; i++)
{
layers[i] = new QPixmap(*other.layers[i]);
}
displayLayer = new bool[layerCount];
for (int i=0; i<layerCount; i++)
displayLayer[i]=other.displayLayer[i];
currentIndexOnEdit=other.currentIndexOnEdit;
//cout << "copy constructor called " << endl;
}
LayeredPixmap::~LayeredPixmap()
{
clearAllLayers();
delete paint;
delete[] displayLayer;
}
void LayeredPixmap::clearAllLayers()
{
for (int i=0; i<layerCount; i++)
{
if (layers[i]!=NULL){ delete layers[i];layers[i]=NULL;}
}
}
////////////////////////////////////////////////
// Getter and setter functions
////////////////////////////////////////////////
int LayeredPixmap::getLayerCount() const
{
return layerCount;
}
void LayeredPixmap::setCurrentLayerOnEdit(int index)
{
currentIndexOnEdit = index;
}
int LayeredPixmap::getCurrentIndexOnEdit() const
{
return currentIndexOnEdit;
}
// Both read and write are enabled.
QPixmap* LayeredPixmap::currentLayerOnEdit() const
{
return layers[currentIndexOnEdit];
}
////////////////////////////////////////////////
// Feature implementations
////////////////////////////////////////////////
void LayeredPixmap::newImageLayersWithSize(int width, int height, const QColor& bgcolor)
{
clearAllLayers();
for (int i=0; i<layerCount-1; i++)
{
layers[i] = new QPixmap(width, height);
layers[i]->fill(Qt::white);
}
// fill background color only for the last layer
layers[layerCount-1] = new QPixmap(width, height);
layers[layerCount-1]->fill(bgcolor);
}
void LayeredPixmap::resizeImageLayers(int width, int height, const QColor& bgcolor)
{
for (int i=0; i<layerCount-1; i++)
{
// Create imageTemp, resize image filled with WHITE color.
QPixmap* imageTemp;
imageTemp = new QPixmap(*layers[i]);
imageTemp->resize(width, height);
imageTemp->fill(Qt::white);
// then, draw current image onto imageTemp.
paint->begin(imageTemp);
paint->drawPixmap(0, 0, *layers[i]);
paint->end();
delete layers[i];
layers[i]=imageTemp;
}
// Create imageTemp, resize the LAST image filled with BACKGROUND color.
QPixmap* imageTemp;
imageTemp = new QPixmap(*layers[layerCount-1]);
imageTemp->resize(width, height);
imageTemp->fill(bgcolor);
// then, draw current image onto imageTemp.
paint->begin(imageTemp);
paint->drawPixmap(0, 0, *layers[layerCount-1]);
paint->end();
delete layers[layerCount-1];
layers[layerCount-1]=imageTemp;
}
void LayeredPixmap::combineLayersToImage(QPixmap *image) const
{
if (layers[0]==NULL || layers[0]->isNull()) return;
if (image!=NULL){ delete image; image = NULL;}
if( paint->isActive() )
paint->end();
// temporary pen for setting color for each pixel.
QPen pen;
// creates new image and initialize to white color.
image = new QPixmap(*(layers[layerCount-1]));
image->fill(Qt::white);
// Begins painting all layers to image.
paint->begin(image);
for (int index=layerCount-1; index>=0; index--)
{
if (displayLayer[index])
{
QImage currentLayer = layers[index]->convertToImage();
// for each layers.. copy pixel by pixel.
for (int i=0; i<layers[index]->width(); i++)
{
for (int j=0; j<layers[index]->height(); j++)
{
if (Qt::white.rgb() != currentLayer.pixel(i,j))
{
pen.setColor(QColor(currentLayer.pixel(i,j)));
paint->setPen(pen);
paint->drawPoint(i,j);
}
}
}
}
}
paint->end();
}
// returns rescaled image in specified index.
QPixmap* LayeredPixmap::getIconSizedImage(int index) const
{
QPen pen;
QPixmap* pix = new QPixmap(22,22);
QImage imageTemp = layers[index]->convertToImage();
QImage resizedImage = imageTemp.smoothScale(22,22);
paint->begin(pix);
for (int i=0; i<22; i++)
for (int j=0; j<22; j++)
{
pen.setColor(QColor(resizedImage.pixel(i,j)));
paint->setPen(pen);
paint->drawPoint(i,j);
}
paint->end();
return pix;
}