forked from ixlang/SecretWatermark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecretWatermark.xcs
480 lines (380 loc) · 14.9 KB
/
SecretWatermark.xcs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// xlang
class MainDialog : QDialog{
QPushButton btnOk = new QPushButton(),
btnAdd = new QPushButton(),
btnref = new QPushButton(),
btnSave = new QPushButton(),
btnAbout = new QPushButton(),
genmark = new QPushButton(),
selfont = new QPushButton();
QWidget widgetori = new QWidget(), widgetdon = new QWidget(), extwid = new QWidget();
QImage oriimg, doneimg, extimg;
QLineEdit precent = new QLineEdit(), xcoord = new QLineEdit(), ycoord = new QLineEdit(), marktext = new QLineEdit();
QImage wmpic;
Switche ivw = new Switche();
String fontString = nilptr;
void generateMark(bool warning){
String marktxt = marktext.getText();
if (marktxt.length() == 0){
if (warning){
QMessageBox.Critical("注意","文字大小必须大于0, 水印文字不能为空",QMessageBox.Ok,QMessageBox.Ok);
}
return ;
}
QImage wmimg = new QImage(100, 100, QImage.Format_RGB32);
QPainter qp = new QPainter(wmimg);
qp.Paint pt = new qp.Paint();
pt.setStyle(QPainter.Paint.FILL_AND_STROKE);
pt.setColor(0xff000000);
//pt.setTextSize(fs);
QFont font = nilptr;
if (fontString != nilptr){
font = QFont.loadFromString(fontString);
}
if (font != nilptr){
qp.setFont(font);
}
qp.setPaint(pt);
QRect rc = qp.measureText(0,0,marktxt);
qp = nilptr;
wmimg = new QImage(rc.width(), rc.height(), QImage.Format_RGB32);
qp = new QPainter(wmimg);
qp.fillRect(0,0,rc.width(), rc.height(),0xffffffff,QBrush.Style.SolidPattern);
if (font != nilptr){
qp.setFont(font);
}
qp.setPaint(pt);
qp.drawText(marktxt,0, qp.ascent());
wmpic = wmimg;
doneimg = wmpic;
//doneimg.saveToFile("D:\\Cadaqs\\Desktop\\333.jpg","jpg");
widgetdon.update();
}
public bool create()override{
QBuffer qb = new QBuffer();
byte [] buffer = __xPackageResource("ui/dialog.ui");
qb.setBuffer(buffer, 0, buffer.length);
if (load(qb)){
setWindowFlags(Constant.WindowMinMaxButtonsHint | Constant.WindowCloseButtonHint | Constant.WindowTitleHint);
attachByName(btnOk, "btnOk");
attachByName(btnAdd, "btnAdd");
attachByName(btnref, "btnref");
attachByName(btnSave, "btnSave");
attachByName(btnAbout, "btnAbout");
attachByName(xcoord, "xcoord");
attachByName(ycoord, "ycoord");
attachByName(widgetori, "widgetori");
attachByName(widgetdon, "widgetdon");
attachByName(extwid, "extwid");
attachByName(ivw, "ivw");
attachByName(marktext, "marktext");
attachByName(selfont, "selfont");
attachByName(genmark, "genmark");
attachByName(precent, "precent");
ivw.setChecked(false, true);
setWindowIcon("ic256.png");
widgetori.setOnPaintListener( new onPaintListener(){
void onPaint(QObject obj, int l,int t, int r,int b, long hpaint){
QPainter painter = new QPainter(hpaint);
if (oriimg != nilptr){
painter.drawImage(oriimg,0, 0);
}
}
});
widgetdon.setOnPaintListener( new onPaintListener(){
void onPaint(QObject obj, int l,int t, int r,int b, long hpaint){
QPainter painter = new QPainter(hpaint);
if (doneimg != nilptr){
painter.drawImage(doneimg,0, 0);
}
}
});
extwid.setOnPaintListener( new onPaintListener(){
void onPaint(QObject obj, int l,int t, int r,int b, long hpaint){
QPainter painter = new QPainter(hpaint);
if (extimg != nilptr){
painter.drawImage(extimg,0, 0);
}
}
});
genmark.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
generateMark(true);
}
});
btnOk.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
String file = QFileDialog.getOpenFileName("选择图片","","*.*",MainDialog.this);
if (file == nilptr || file.length() == 0){
return;
}
oriimg = resave(file);
if (oriimg == nilptr){
QMessageBox.Critical("注意","无效的图像文件:" + file,QMessageBox.Ok,QMessageBox.Ok);
return ;
}
int w = oriimg.width();
int h = oriimg.height();
analyze(w, h, oriimg.getdata(), nilptr, 0, 0, 0, false);
}
});
btnSave.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
String file = QFileDialog.getSaveFileName("保存图片","","*.bmp;;*.png;;*.jpg",MainDialog.this);
if (file == nilptr || file.length() == 0){
return;
}
String ext = file.findExtension();
if (ext.length() > 0){
ext = ext.substring(1, ext.length());
}
doneimg.saveToFile(file,ext);
}
});
selfont.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
fontString = QFontDialog.getFontDialog("选择字体",fontString,MainDialog.this);
generateMark(false);
}
});
btnAbout.setOnClickListener(new onClickListener(){
static String getAppVersion(){
long vv = _system_.getAppVersion();
String vs = "v";
for (int i = 0; i < 4; i ++){
vs = vs + ((vv >> ((4 - (i + 1)) << 4)) & 0xffff);
if (i != 3){
vs = vs + ".";
}
}
return vs;
}
void onClick(QObject obj, bool checked)override{
String buildtime = String.formatDate("%Y年%m月%d日", _system_.getAppTimestamp());
MainDialog.this.About("关于",
"<Strong>隐形数字水印制作工具 " + getAppVersion() + " </Strong><br /><br />" +
"开发语言: Xlang v3.3<br /><br />" +
"编译时间: " + buildtime + "<br /><br />" +
"<b>该软件使用xlang 1.7开发, 软件全部源代码可在xlang的example中找到," +
"该软件的使用不受任何限制,可用于任意用途的修改或者发布.<br /><br />" +
"<em>需要注意该软件底层使用了第三方图形库Qt (Copyright The Qt Company Ltd), 二次发布和使用须遵守第三方授权协议.</em><br /><br />" +
"<strong>该项目中使用了以下第三方项目:</strong><br />" +
"<em> libfftw</em><br />" +
"github代码仓库:<a href=\"https://github.com/ixlang/examples\">https://github.com/ixlang/examples</a><br />" +
"<p align=\"right\">建议可通过以下方式反馈</p>" +
"<p align=\"right\">QQ: 3.vc@163.com</p>" +
"<p align=\"right\">Cadaquz 2019-04-07</p>");
}
});
btnAdd.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
if (oriimg == nilptr){
QMessageBox.Critical("注意","请先选择图片",QMessageBox.Ok, QMessageBox.Ok);
return ;
}
String markfile = QFileDialog.getOpenFileName("选择图片","","*.*",MainDialog.this);
if (markfile == nilptr || markfile.length() == 0){
return;
}
try{
wmpic = new QImage(markfile, nilptr);
}catch(Exception e){
QMessageBox.Critical("注意","无效的图像文件:" + markfile,QMessageBox.Ok, QMessageBox.Ok);
wmpic = nilptr;
return ;
}
int w = oriimg.width();
int h = oriimg.height();
if ((wmpic.width() > w) || (wmpic.height() * 2 > h)){
QMessageBox.Critical("注意","水印图片过大",QMessageBox.Ok, QMessageBox.Ok);
markfile = nilptr;
return ;
}
String lp = precent.getText();
double pwr = lp.parseDouble();
int mx = xcoord.getText().parseInt();
int my = ycoord.getText().parseInt();
bool biv = ivw.getChecked();
if (mx <0 || my < 0 || mx > w - wmpic.width() || my > h - wmpic.height()){
QMessageBox.Critical("注意","位置超出范围",QMessageBox.Ok, QMessageBox.Ok);
return ;
}
byte[] img_data = oriimg.getdata();
analyze(w, h, img_data, wmpic, pwr, mx, my, biv);
}
});
btnref.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
if (oriimg == nilptr){
return ;
}
if (wmpic == nilptr ){
return;
}
String lp = precent.getText();
double pwr = lp.parseDouble();
int mx = xcoord.getText().parseInt();
int my = ycoord.getText().parseInt();
bool biv = ivw.getChecked();
int w = oriimg.width();
int h = oriimg.height();
if (mx <0 || my < 0 || mx > w - wmpic.width() || my > h - wmpic.height()){
QMessageBox.Critical("注意","位置超出范围",QMessageBox.Ok, QMessageBox.Ok);
return ;
}
byte[] img_data = oriimg.getdata();
analyze(w, h, img_data, wmpic, pwr, mx, my, biv);
}
});
return true;
}
return false;
}
QImage resave(String file){
QImage tmp = nilptr;
try{
tmp = new QImage(file, nilptr);
}catch(Exception e){
return tmp;
}
byte [] dat = tmp.getdata();
int w = tmp.width();
int h = tmp.height();
QImage rsv = new QImage(dat, w, h, QImage.Format_RGB32);
String ext = file.findExtension();
if (ext.length() > 0){
ext = ext.substring(1, ext.length());
}
rsv.saveToFile("tmp.tmp",ext,-1);
return new QImage("tmp.tmp",ext);
}
void analyze(int w,int h, byte [] data, QImage wmark, double power,int mx,int my,bool biv){
ProgressView pvw = ProgressView.showWait(this, -1);
//pv.EnableProgress(true,0);
pvw.setTitleText("处理中...");
new Thread(){
void run() override{
int l = data.length / 4;
//读取数据并归一化
double []r = new double[l];
double []g = new double[l];
double []b = new double[l];
for (int i = 0; i < l; i ++){
r[i] = data[(i * 4) + 2] / 255.f;
g[i] = data[(i * 4) + 1] / 255.f;
b[i] = data[(i * 4) + 0] / 255.f;
}
// 傅里叶正变换
double[][] rf = fftw.fft2(h, w, r, nilptr);
double[][] gf = fftw.fft2(h, w, g, nilptr);
double[][] bf = fftw.fft2(h, w, b, nilptr);
if (wmark == nilptr){
displayMark(rf, gf, bf, w, h);
}else{
//打水印
setWaterMark(rf, gf, bf, w, h, wmark, power ,mx, my, biv);
double [][]rt = fftw.ifft2(h,w,rf[0],rf[1]);
double [][]gt = fftw.ifft2(h,w,gf[0],gf[1]);
double [][]bt = fftw.ifft2(h,w,bf[0],bf[1]);
double pv = 255.f / l;
byte []rgb = new byte [w * h * 4];
for (int i = 0; i < l; i ++){
rgb[2+ (i * 4)] = Math.max(0, Math.min(255,(int)(rt[0][i] * pv)));
rgb[1+ (i * 4)] = Math.max(0, Math.min(255,(int)(gt[0][i] * pv)));
rgb[0+ (i * 4)] = Math.max(0, Math.min(255,(int)(bt[0][i] * pv)));
rgb[3+ (i * 4)] = 0xff;
}
runOnUi(new Runnable(){
void run()override{
doneimg = new QImage(rgb, w, h, QImage.Format_RGB32);
//doneimg.saveToFile("D:\\Cadaqs\\Desktop\\333.jpg","jpg");
widgetdon.update();
}
});
}
pvw.dismissAfter(0);
}
}.start();
}
void setWaterMark(double[][] rf, double[][] gf, double[][] bf, int w ,int h, QImage qi, double power, int mx, int my,bool biv){
byte[] wm = qi.getdata();
int l = w * h;
double pf = power / 255.f;
int mw = qi.width(), mh = qi.height();
//int st = (w - mw) / 2, sh = (h /2 - mh) / 2;
int st = mx, sh = my;
if (biv){
for (int x = 0; x < mw; x++){
for (int y = 0; y < mh; y++){
int ry = h - (y + sh + 1);
int rx = w - (x + st + 1);
int mi = y * mw + x;
int oi = ((y + sh)* w) + x + st;
int ni = (ry * w) + rx;
rf[0][oi] += (255 - wm[mi * 4 + 0]) * pf;
gf[0][oi] += (255 - wm[mi * 4 + 1]) * pf;
bf[0][oi] += (255 - wm[mi * 4 + 2]) * pf;
rf[0][ni] += (255 - wm[mi * 4 + 0]) * pf;
gf[0][ni] += (255 - wm[mi * 4 + 1]) * pf;
bf[0][ni] += (255 - wm[mi * 4 + 2]) * pf;
}
}
}else{
for (int x = 0; x < mw; x++){
for (int y = 0; y < mh; y++){
int ry = h - (y + sh + 1);
int rx = w - (x + st + 1);
int mi = y * mw + x;
int oi = ((y + sh)* w) + x + st;
int ni = (ry * w) + rx;
rf[0][oi] += ( wm[mi * 4 + 0]) * pf;
gf[0][oi] += ( wm[mi * 4 + 1]) * pf;
bf[0][oi] += ( wm[mi * 4 + 2]) * pf;
rf[0][ni] += ( wm[mi * 4 + 0]) * pf;
gf[0][ni] += ( wm[mi * 4 + 1]) * pf;
bf[0][ni] += ( wm[mi * 4 + 2]) * pf;
}
}
}
displayMark(rf, gf, bf, w, h);
}
void displayMark(double[][] rf, double[][] gf, double[][] bf, int w, int h){
int l = w * h;
byte []rgb = new byte [w * h * 4];
for (int i = 0; i < l; i ++){
rgb[2+ (i * 4)] = bf[0][i];
rgb[1+ (i * 4)] = gf[0][i];
rgb[0+ (i * 4)] = rf[0][i];
rgb[3+ (i * 4)] = 0xff;
}
runOnUi(new Runnable(){
void run()override{
extimg = new QImage(rgb, w, h, QImage.Format_RGB32);
extwid.update();
}
});
}
};
class XMyApp : QApplication {
MainDialog dialog;
public bool create(){
if (super.createQApplication()){
dialog = new MainDialog();
if (dialog.create()){
dialog.show();
return true;
}
}
return false;
}
};
using{
Qt;
};
int main(String [] args){
XMyApp app = new XMyApp();
if (app.create()){
app.run();
}
return 0;
}