-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdimage.cpp
194 lines (168 loc) · 3.83 KB
/
lcdimage.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
#include "lcdimage.h"
#include <QtGui>
LcdImage::LcdImage(QObject *parent) :
QObject(parent)
{
// initialize default Values
invert = false;
redLimit = 128;
greenLimit = 128;
blueLimit = 128;
height = 68;
width = 96;
fullImage = QImage();
}
int LcdImage::getHeight()
{
return height;
}
int LcdImage::getWidth()
{
return width;
}
bool LcdImage::getInvert()
{
return invert;
}
int LcdImage::getRedLimit()
{
return redLimit;
}
int LcdImage::getGreenLimit()
{
return greenLimit;
}
int LcdImage::getBlueLimit()
{
return blueLimit;
}
QImage LcdImage::getImgPreview()
{
return fullImage.scaled(width,height);
}
QImage LcdImage::getLcdPreview()
{
QImage colorImage(fullImage.scaled(width, height));
QImage monoImage(width, height, QImage::Format_Mono);
monoImage.fill(0);
if (invert) //invert the color in monoImage.setPixel() with "!"
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
QColor myPoint(colorImage.pixel(x,y));
monoImage.setPixel(x,y,!(myPoint.red() > redLimit ||
myPoint.green() > greenLimit ||
myPoint.blue() > blueLimit));
}
}
}
else
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
QColor myPoint(colorImage.pixel(x,y));
monoImage.setPixel(x,y,(myPoint.red() > redLimit ||
myPoint.green() > greenLimit ||
myPoint.blue() > blueLimit));
}
}
}
return monoImage;
}
void LcdImage::LoadFile(QString filename)
{
fullImage = QImage(filename);
}
void LcdImage::setWidth(int w)
{
width = w;
}
void LcdImage::setHeight(int h)
{
height = h;
}
void LcdImage::setInvert(bool inv)
{
invert = inv;
}
void LcdImage::setColorLimit(int r, int g, int b)
{
if (r < 0)
{
redLimit = 0;
}
else if (r > 255)
{
redLimit = 255;
}
else
{
redLimit = r;
}
if (g < 0)
{
greenLimit = 0;
}
else if (g > 255)
{
greenLimit = 255;
}
else
{
greenLimit = g;
}
if (b < 0)
{
blueLimit = 0;
}
else if (b > 255)
{
blueLimit = 255;
}
else
{
blueLimit = b;
}
}
void LcdImage::saveAsLcd(QString filename)
{
QImage picture = getLcdPreview();
char pixmap[(height+7)/8][width];
memset(pixmap, 0, (height+7)/8 * width);
//Read pixels from image in the array
if (invert) //invert the color with bool pixel = !(...)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bool pixel = !(picture.pixel(width-1-x,height-1-y) == qRgb(0,0,0));
char byte = pixmap[y/8][x]; //get Byte from array
byte |= pixel<<(y%8); //set bit in byte
pixmap[y/8][x] = byte; //write byte back to array
}
}
}
else
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bool pixel = (picture.pixel(width-1-x,height-1-y) == qRgb(0,0,0));
char byte = pixmap[y/8][x]; //get Byte from array
byte |= pixel<<(y%8); //set bit in byte
pixmap[y/8][x] = byte; //write byte back to array
}
}
}
//Write bits in file
QFile file(filename);
file.open(QIODevice::WriteOnly);
file.write(&pixmap[0][0], ((height+7)/8) * width);
file.close();
}