/
picedit.cpp
310 lines (279 loc) · 8.27 KB
/
picedit.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "picedit.h"
#include "ui_picedit.h"
#include <QRgb>
#include <QDebug>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
const int Bs = 255;
const int Ns = 0;
const int Step1[5][5]={
{Bs,Bs,Bs,Bs,Bs},
{Bs,Bs,Bs,Bs,Bs},
{Bs,Bs,Ns,Bs,Bs},
{Bs,Bs,Bs,Bs,Bs},
{Bs,Bs,Bs,Bs,Bs},
};
const int Step2[5][5]={
{Bs,Bs,Bs,Bs,Bs},
{Bs,Bs,Ns,Bs,Bs},
{Bs,Ns,Ns,Ns,Bs},
{Bs,Bs,Ns,Bs,Bs},
{Bs,Bs,Bs,Bs,Bs},
};
const int Step3[5][5]={
{Bs,Bs,Ns,Bs,Bs},
{Bs,Ns,Ns,Ns,Bs},
{Ns,Ns,Ns,Ns,Ns},
{Bs,Ns,Ns,Ns,Bs},
{Bs,Bs,Ns,Bs,Bs},
};
const int Step4[5][5]={
{Bs,Ns,Ns,Ns,Bs},
{Ns,Ns,Ns,Ns,Ns},
{Ns,Ns,Ns,Ns,Ns},
{Ns,Ns,Ns,Ns,Ns},
{Bs,Ns,Ns,Ns,Bs},
};
PicEdit::PicEdit(QWidget *parent) :
QWidget(parent),
ui(new Ui::PicEdit)
{
ui->setupUi(this);
preOn = false;
this->setWindowTitle(tr("GrayScale"));
potrace = new PotraceWidget();
}
PicEdit::~PicEdit()
{
delete ui;
}
void PicEdit::GrayScalePicture(int grayscale)
{
QImage tempImg; //用于缩放
tempImg = QImage("temp.png");
QImage pos_1 = tempImg; //拷贝
QImage show;
if(tempImg.format()>3)
{
qDebug()<<tr("format = %1").arg(tempImg.format());
QRgb *pixels = (QRgb *)tempImg.scanLine(0);
QRgb *grays = (QRgb *)pos_1.scanLine(0);
QRgb thread = qRgb(grayscale,grayscale,grayscale);
QRgb threadw = qRgb(255,255,255);
QRgb threadb = qRgb(0,0,0);
for(int y =0;y<tempImg.height();y++)
{
for(int x=0;x<tempImg.width();x++)
{
if(pixels[x+y*(tempImg.width())]>thread)
grays[x+y*(tempImg.width())] = threadw;
else
grays[x+y*(tempImg.width())] = threadb;
}
}
show = pos_1;
}
else
{
show = tempImg;
}
// ui->pixValue->setText(tr("图片大小:%1x%2").arg(pos.width()).arg(pos.height()));
// show.save("temp.png","PNG",100); //变换了灰度值的图
gray_image = show;
ui->label->setPixmap(QPixmap::fromImage(show));
}
//begin print
void PicEdit::on_startPrint_clicked()
{
gray_image.save("print.png","PNG",100); //变换了灰度值的图
gray_image.save("gi.png","PNG",100);
emit Sig_Print();
}
//slider value change
void PicEdit::on_horizontalSlider_valueChanged(int value)
{
GrayScalePicture(value);
ui->label_3->setText(QString::number(value));
}
//load a picture
void PicEdit::LoadPicture(QString file)
{
QImage image(file);
image.save("temp.png");
ui->label->setPixmap(QPixmap::fromImage(image));
}
QImage PicEdit::Image2GrayScale(QImage src)
{
QImage image = src;
int depth =4;
int media = 0;
int h = image.height();
int w = image.width();
QRgb* rgbpixel = (QRgb *)image.scanLine(0);
for (int ii = 0; ii < image.height()/5; ii++)
{
for (int jj = 0; jj < image.width()/5; jj++)
{
media = 0;
for(int n=0;n<5;n++)
{
for(int m=0;m<5;m++)
{
QRgb a = rgbpixel[(ii*5+n)*w+(jj*5+m)];
int gray = qGray(a);
media+=gray;
}
}
media = media/25;
for(int y=0;y<5;y++)
{
for(int x=0;x<5;x++)
{
if((media>250)&&(media<255))
{
QRgb a = QColor(255, 255, 255).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
else if((media>=190)&&(media<250))
{
QRgb a = QColor(Step1[y][x], Step1[y][x], Step1[y][x]).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
else if((media>=130)&&(media<190))
{
QRgb a = QColor(Step2[y][x], Step2[y][x], Step2[y][x]).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
else if((media>=70)&&(media<130))
{
QRgb a = QColor(Step3[y][x], Step3[y][x], Step3[y][x]).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
else if((media>=10)&&(media<70))
{
QRgb a = QColor(Step4[y][x], Step4[y][x], Step4[y][x]).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
else if((media>=0)&&(media<10))
{
QRgb a = QColor(0, 0, 0).rgba();
rgbpixel[(ii*5+y)*w+(jj*5+x)] = a;
}
}
}
}
}
image.save("hi.png","PNG",100);
return image;
}
void PicEdit::ExportGrayPix(QImage src)
{
QFile file("gray.gcode");
if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
QMessageBox::critical(NULL, tr("提示"), tr("无法创建文件"));
return ;
}
QTextStream out(&file);
QImage image = src;
double scale=0.1;
out<<"G0 X1 Y1 F6000 \n";
out<<"M4 P0 \n";
for (int ii = 0; ii < image.height(); ii++)
{
uchar* scan = image.scanLine(ii);
QString p1;
QString p2;
if(ii%2==0)
{
int depth =4;
for (int jj = 0; jj < image.width(); jj++)
{
QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + jj*depth);
int gray = qGray(*rgbpixel);
//power off
if(gray>240)
{
if(preOn)
{
p1 = "M4 P0 \n";
p2 = tr("G0 X%1 Y%2 F6000 \n").arg(jj*scale).arg(ii*scale);
out<<p1<<p2;
preOn = false; //关闭
}
else
{
p2 = tr("G0 X%1 Y%2 \n").arg(jj*scale).arg(ii*scale);
out<<p2;
}
}
else
{
if(!preOn)
{
p1 = tr("M4 P%1 \n").arg(255-gray);
p2 = tr("G1 X%1 Y%2 F4000 \n").arg(jj*scale).arg(ii*scale);
preOn = true;
out<<p1<<p2;
}
else
{
p2 = tr("G1 X%1 Y%2 \n").arg(jj*scale).arg(ii*scale);
out<<p2;
}
}
}
}
else
{
int depth =4;
for (int jj = image.width(); jj >0; jj--)
{
QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + jj*depth);
int gray = qGray(*rgbpixel);
//power off
if(gray>=240)
{
if(preOn)
{
p1 = "M4 P0 \n";
p2 = tr("G0 X%1 Y%2 F6000 \n").arg(jj*scale).arg(ii*scale);
out<<p1<<p2;
preOn = false; //关闭
}
else
{
p2 = tr("G0 X%1 Y%2 \n").arg(jj*scale).arg(ii*scale);
out<<p2;
}
}
else
{
if(!preOn)
{
p1 = tr("M4 P%1 \n").arg(255-gray);
p2 = tr("G1 X%1 Y%2 F4000 \n").arg(jj*scale).arg(ii*scale);
preOn = true;
out<<p1<<p2;
}
else
{
p2 = tr("G1 X%1 Y%2 \n").arg(jj*scale).arg(ii*scale);
out<<p2;
}
}
}
}
}
qDebug()<<"export finish!!";
file.close();
}
void PicEdit::on_btngray_clicked()
{
// QImage image("temp.png");
// ExportGrayPix(Image2GrayScale(image));
gray_image.save("gi.png","PNG",100);
potrace->loadImage("temp.png");
potrace->outSvg();
potrace->outGcode();
}