-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPage.h
728 lines (528 loc) · 14.9 KB
/
Page.h
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#ifndef _Page_H
#define _Page_H
#include <QLabel>
#include <QImage>
#include <QPainter>
#include <QMessageBox>
#include <QDialog>
#include <QMouseEvent>
#include <PCX.h>
#include <thread>
#include "ui_Page.h"
#include "HeaderFrame_PCX.h"
#include "PaletteWindow.h"
#include "BitPlanesWindow.h"
#include "HistogramWindow.h"
#include "ColorPlanesWindow.h"
#include "OverLayWindow.h"
class Page : public QLabel
{
Q_OBJECT
public:
Page(const std::string &fpath, const std::string &name, QWidget *parent = Q_NULLPTR) : QLabel{ parent }, path{ fpath }, fname{ name }
{
ui.setupUi(this);
this->setMouseTracking(true);
// get format
img_format = path.substr(path.find_last_of('.') + 1);
// open image
openImageGetHeaderAndPixelsAndPalette();
// backup original data
auto td = backupData();
// transfer image data to QImage
transferDataToQImage();
// calculate coordinate
calculateQImageCoordinate();
// generate oringinal histogram
generateHistogramWindow(operation_data);
if (td.joinable())
td.join();
}
virtual ~Page()
{
/*if (is_changed)
{
QMessageBox::warning(this, QStringLiteral("消息框"), QStringLiteral("這是警告消息框"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
}*/
}
public:
void paintEvent(QPaintEvent *ev)
{
//updata coordinate
calculateQImageCoordinate();
QPainter p{ this };
if (!qimg.isNull())
p.drawImage(img_start.x(), img_start.y(), qimg);
}
QPoint transformPageXYToImgXY(QPoint coor) const
{
int x = round(1.0 * (coor.x() - center.x()) / (multiple / 100.0)) + org_data.getCol() / 2;
int y = round(1.0 * (coor.y() - center.y()) / (multiple / 100.0)) + org_data.getRow() / 2;
if (x < 0 || y < 0 || x >= org_data.getCol() || y >= org_data.getRow())
return QPoint{ -100,-100 };
else
return QPoint{ x,y };
}
public: /*-------- get inner data functions --------*/
img::Approximation getApproximation(void) const
{
return global_approx;
}
img::RotateType getRotateType(void) const
{
return global_rotate_type;
}
int getMultiple(void) const
{
return multiple;
}
int getAngleDegree(void) const
{
return now_angle_dgree;
}
QColor getColor(QPoint point) const
{
if (org_data.getColor() == 3)
return QColor{ org_data[point.y()][point.x()][0] ,org_data[point.y()][point.x()][1] ,org_data[point.y()][point.x()][2] };
else
return QColor{ org_data[point.y()][point.x()][0] ,0,0 };
}
HeaderFrame_PCX* getHeaderWindow(void)
{
return header_window;
}
PaletteWindow* getPaletteWindow(void)
{
return palette_window;
}
const std::string& getName(void) const
{
return fname;
}
bool hasPaletteWindow(void) const
{
return has_palette_window;
}
HistogramWindow* getHistogramWindow(void)
{
return histogram_window;
}
QString getFilePath(void) const
{
return path.c_str();
}
public: /*-------- set inner data functions --------*/
void setApproximation(img::Approximation approx)
{
global_approx = approx;
}
void setRotateType(img::RotateType type)
{
global_rotate_type = type;
}
void updateHistogramWindow(void)
{
generateHistogramWindow(operation_data);
}
public: /*-------- public method --------*/
// will be called by imgview , it acts likes a slot function
void setThresholds(const std::vector<int> &values)
{
operation_data = std::move(img::ImgFunction::setThreshold(org_data, values));
transferDataToQImage();
this->update();
}
void storeThresholdDataToOrgData(const std::vector<int> &values)
{
org_data = std::move(img::ImgFunction::setThreshold(org_data, values));
}
void undoTresholdData(void)
{
operation_data = org_data;
transferDataToQImage();
this->update();
}
void store_img(int store_type, const QString &npath = QString{}) const
{
img::PCX nimage{ operation_data };
switch (store_type)
{
case 1: // store to old
nimage.store(path);
break;
case 2: // store to new
nimage.store(npath.toStdString());
break;
}
nimage.close();
}
public slots: /*-------- image operation slot functions --------*/
/* -------------------------------------------------------------------------
if the function will change the org_data and set is_changed flag true,
then the function name will have a "change" flag .
------------------------------------------------------------------------- */
void setComplement_change_slot(void)
{
// complement both operation_data and org_data
img::ImgFunction::setComplementaryColor(operation_data);
std::thread td{ [&]()
{
img::ImgFunction::setComplementaryColor(org_data);
} };
td.detach();
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void setGrayLevel_change_slot(void)
{
// transform gray both operation_data and org_data
img::ImgFunction::transformToGrayLevel(operation_data);
std::thread td{ [&]()
{
img::ImgFunction::transformToGrayLevel(org_data);
} };
td.detach();
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void getBitPlanes_slot(void)
{
BitPlanesWindow bit_plane_window { operation_data , 1 };
bit_plane_window.exec();
}
void getGrayCodeBitPlanes_slot(void)
{
BitPlanesWindow bit_plane_window{ operation_data , 2 };
bit_plane_window.exec();
}
void getColorPlanes_slot(void)
{
ColorPlanesWindow color_plane_window{ operation_data };
color_plane_window.exec();
}
void histogramEqualization_change_slot(void)
{
img::ImgFunction::histogramEqualization(operation_data);
std::thread td{ [&]()
{
img::ImgFunction::histogramEqualization(org_data);
} };
td.detach();
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void otsu_change_slot(void)
{
img::ImgFunction::otsuThresholdMethod(operation_data);
std::thread td{ [&]()
{
img::ImgFunction::otsuThresholdMethod(org_data);
} };
td.detach();
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void addWatermark_change_slot(void)
{
img::ImgFunction::addWatermark(operation_data);
std::thread td{ [&]()
{
img::ImgFunction::histogramEqualization(org_data);
} };
td.detach();
// changed
is_changed = true;
transferDataToQImage();
this->update();
// show the watermark bit plane
QDialog *wd = new QDialog{ this };
QLabel *label = new QLabel{ wd };
auto data = img::ImgFunction::getBitPlane(operation_data, 1);
int h = data.getRow();
int w = data.getCol();
auto format = (data.getColor() == 1 ? QImage::Format_Grayscale8 : QImage::Format_RGB888);
QImage qimg{ w,h,format };
uchar *ptr;
for (unsigned r = 0; r < h; ++r)
{
ptr = qimg.scanLine(r);
for (unsigned c = 0; c < w; ++c)
for (unsigned color = 0; color < data.getColor(); ++color)
*(ptr++) = data[r][c][color];
}
label->setPixmap(QPixmap::fromImage(qimg));
wd->resize(w+40, h+40);
label->setGeometry(20, 20, w, h);
wd->exec();
}
void showImageOverlayWindow_slot(void)
{
OverLayWindow w{ operation_data };
w.exec();
}
void large_slot(void)
{
resizeImg(Zoom::LARGE);
}
void small_slot(void)
{
resizeImg(Zoom::SMALL);
}
void org_size_slot(void)
{
resizeImg(Zoom::ORIGINAL);
}
void rotate_right_90_slot(void)
{
rotateImg(Rotate::RIGHT, -10 );
}
void rotate_left_90_slot(void)
{
rotateImg(Rotate::LEFT, 10 );
}
void org_angle_slot(void)
{
rotateImg(Rotate::ORIGINAL, 0);
}
void horizontalFlip_change_slot(void)
{
// flip both operation_data and org_data
img::ImgFunction::horizontalFlip(operation_data);
img::ImgFunction::horizontalFlip(org_data);
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void verticalFlip_change_slot(void)
{
// flip both operation_data and org_data
img::ImgFunction::verticalFlip(operation_data);
img::ImgFunction::verticalFlip(org_data);
// changed
is_changed = true;
transferDataToQImage();
this->update();
}
void setApproximationBilinear_slot(void)
{
global_approx = img::Approximation::BilinearInterpolation;
}
void setApproximationNearest_slot(void)
{
global_approx = img::Approximation::NearestNeighbor;
}
void setRotateTypeInverse_slot(void)
{
global_rotate_type = img::RotateType::INVERSE;
}
void setRotateTypeDirect_slot(void)
{
global_rotate_type = img::RotateType::DIRECT;
}
private: /*-------- inner data --------*/
Ui::Page ui;
// header imformation window
HeaderFrame_PCX *header_window;
// palette window
PaletteWindow *palette_window{ nullptr };
bool has_palette_window{ false };
// histogram window
HistogramWindow *histogram_window{ nullptr };
// file information
std::string path;
std::string fname;
std::string img_format;
// matrix data
img::Matrix org_data;
img::Matrix operation_data;
// output image
QImage qimg;
// coordinate data
QPoint center;
QPoint img_start;
// calculation flags
img::Approximation global_approx{ img::Approximation::BilinearInterpolation };
img::RotateType global_rotate_type{ img::RotateType::INVERSE };
// status data
const std::vector<int> multiple_times{ 10,17,25,30,50,70,100,150,200,250,300,400 };
int multiple{ 100 };
int now_angle_dgree{ 0 };
bool is_changed{ false }; // some operation will need to check if want to save
private: /*-------- inner enumeration and constant data type --------*/
enum class Zoom { LARGE, SMALL, ORIGINAL };
enum class Rotate { RIGHT, LEFT, ORIGINAL };
double const PI = 3.14159265359L;
private: /*-------- every image types for open image and get inner data --------*/
void openImageGetHeaderAndPixelsAndPalette(void)
{
if (img_format == "pcx" || img_format == "PCX")
{
img::PCX image{ path };
_PCX_getHeaderImformation(image.getHeader());
_PCX_openImgAndGetPixelData(image);
_PCX_getPalette(image);
}
else
throw "format not match";
}
void _PCX_openImgAndGetPixelData(img::PCX &image)
{
operation_data = std::move(image.getImgPixelData_r());
}
void _PCX_getHeaderImformation(const img::PCXHeader &header)
{
header_window = new HeaderFrame_PCX{ header,this };
header_window->hide();
}
void _PCX_getPalette(img::PCX &image)
{
auto &p = image.getExtendPlette();
switch (image.getPaletteType())
{
case img::PCX::PaletteType::EXTEND_PALETTE :
palette_window = new PaletteWindow{ image.getExtendPlette(),this };
palette_window->hide();
has_palette_window = true;
break;
case img::PCX::PaletteType::ORG_PALETTE :
palette_window = new PaletteWindow{ _getHeaderPalette(image.getHeader()),this };
palette_window->hide();
has_palette_window = true;
break;
default :
palette_window = nullptr;
has_palette_window = false;
break;
}
}
img::PCX::Palette _getHeaderPalette(const img::PCXHeader &header) const
{
img::PCX::Palette palette;
for (unsigned i = 0; i < 16; ++i)
palette.push_back(std::vector<img::BYTE>{header.Palette[i], header.Palette[i + 1], header.Palette[i + 2]});
return palette;
}
private: /*-------- help function --------*/
void transferDataToQImage(void)
{
QImage::Format format{ ((operation_data.getColor() == 1) ? QImage::Format_Grayscale8 : QImage::Format_RGB888) };
qimg = QImage(operation_data.getCol(), operation_data.getRow(), format);
unsigned r, c, color;
for (r = 0; r < operation_data.getRow(); ++r)
{
// use ScanLine , because QImage inner data will be set alignment by 4 and prone to error
uchar *ptr = qimg.scanLine(r);
for (c = 0; c < operation_data.getCol(); ++c)
for (color = 0; color < operation_data.getColor(); ++color)
*(ptr++) = operation_data[r][c][color];
}
}
void calculateQImageCoordinate(void)
{
// 置中設定
center.setX(this->width() / 2);
center.setY(this->height() / 2);
img_start.setX(center.x() - operation_data.getCol() / 2);
img_start.setY(center.y() - operation_data.getRow() / 2);
}
std::thread backupData(void)
{
std::thread td{ [&]()
{
org_data = operation_data;
} };
return td;
}
double degreeToRad(int deg) const
{
return deg / 180.0 * PI;
}
void generateHistogramWindow(const img::Matrix &data)
{
if (histogram_window)
delete histogram_window;
histogram_window = new HistogramWindow{ img::ImgFunction::getHistogram(data),this };
histogram_window->hide();
}
private: /*-------- image operation functions --------*/
void resizeImg(Page::Zoom type)
{
// first : resize from org_data, and store in to operation_data
// second : rotate the operation_data if needed
int mul_idx = std::find(multiple_times.cbegin(), multiple_times.cend(), multiple) - multiple_times.cbegin();
if (mul_idx == multiple_times.size())
throw "multiple times error";
bool is_zooming = true;
switch (type)
{
case Zoom::LARGE :
if (mul_idx != multiple_times.size() - 1)
{
multiple = multiple_times[++mul_idx];
if (multiple == 100)
operation_data = org_data;
else
operation_data = img::ImgFunction::resize(org_data, multiple / 100.0, global_approx);
}
else
is_zooming = false;
break;
case Zoom::SMALL :
if (mul_idx != 0)
{
multiple = multiple_times[--mul_idx];
if (multiple == 100)
operation_data = org_data;
else
operation_data = img::ImgFunction::resize(org_data, multiple / 100.0, global_approx);
}
else
is_zooming = false;
break;
case Zoom::ORIGINAL :
if (multiple != 100)
{
multiple = 100;
operation_data = org_data;
}
else
is_zooming = false;
break;
}
if(is_zooming)
operation_data = img::ImgFunction::rotate(operation_data, degreeToRad(now_angle_dgree), global_rotate_type, global_approx);
transferDataToQImage();
this->update();
}
void rotateImg(Page::Rotate type, double angle)
{
// first : rotate the angle from org_data, and store in operation_data
// second : resize the operation_data if needed
switch (type)
{
case Page::Rotate::RIGHT :
case Page::Rotate::LEFT :
now_angle_dgree += angle;
operation_data = img::ImgFunction::rotate(org_data, degreeToRad(now_angle_dgree), global_rotate_type, global_approx);
break;
case Page::Rotate::ORIGINAL :
operation_data = org_data;
now_angle_dgree = 0;
break;
}
if(multiple != 100)
operation_data = img::ImgFunction::resize(operation_data, multiple / 100.0, global_approx);
transferDataToQImage();
this->update();
}
};
#endif // end _Page_H