-
Notifications
You must be signed in to change notification settings - Fork 2
/
MFC_ImageProcessingDlg_BACKUP_442.cpp
2672 lines (2184 loc) · 73.7 KB
/
MFC_ImageProcessingDlg_BACKUP_442.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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<<<<<<< HEAD
// MFC_ImageProcessingDlg.cpp: 实现文件
//
#include "pch.h"
#include "afxdialogex.h"
#include "CScaleDlg.h"
#include "CTextInputDialog.h"
#include "ParaSet.h"
#include "framework.h"
#include "MFC_ImageProcessing.h"
#include "MFC_ImageProcessingDlg.h"
#include"toning_func.h"
#include <gdiplus.h>
#include <windows.h>
#pragma comment(lib, "gdiplus.lib")
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define _MinDefTmp_
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define _MaxDefTmp_
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMFCImageProcessingDlg* Imagepass;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCImageProcessingDlg 对话框
CMFCImageProcessingDlg::CMFCImageProcessingDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MFC_IMAGEPROCESSING_DIALOG, pParent)
, nummo(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCImageProcessingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_FUNC, m_func_select);
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
//DDX_Text(pDX, IDC_HEIGHT_EDIT, m_Scale_Height);
//DDX_Text(pDX, IDC_WIDTH_EDIT, m_Scale_Width);
//DDX_Control(pDX, IDC_SLIDER_AD, m_slider_ad);
DDX_Slider(pDX, IDC_SLIDER_AD, nummo);
}
BEGIN_MESSAGE_MAP(CMFCImageProcessingDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN_BUTTON, &CMFCImageProcessingDlg::OnClickedOpenButton)
ON_BN_CLICKED(IDC_SAVE_BUTTON, &CMFCImageProcessingDlg::OnClickedSaveButton)
ON_BN_CLICKED(IDC_Rotation, &CMFCImageProcessingDlg::OnClickedRotation)
ON_BN_CLICKED(IDC_BLUR_BUTTON, &CMFCImageProcessingDlg::OnClickedBlurButton)
ON_BN_CLICKED(IDC_SHARP_BUTTON, &CMFCImageProcessingDlg::OnClickedSharpButton)
ON_BN_CLICKED(IDC_SCALE_BUTTON, &CMFCImageProcessingDlg::OnClickedScaleButton)
ON_WM_LBUTTONDBLCLK()
ON_STN_CLICKED(IDC_STATIC_PIC, &CMFCImageProcessingDlg::OnStnClickedStaticPic)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCImageProcessingDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CMFCImageProcessingDlg::OnBnClickedButton2)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_BUTTON_TEXT, &CMFCImageProcessingDlg::OnBnClickedTextButton)
ON_WM_LBUTTONDOWN()
ON_LBN_SELCHANGE(IDC_LIST_FUNC, &CMFCImageProcessingDlg::OnSelchangeListFunc)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_MOSAIC_BUTTON, &CMFCImageProcessingDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CMFCImageProcessingDlg 消息处理程序
BOOL CMFCImageProcessingDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_func_select.AddString(_T("顺时针旋转 90 度"));
m_func_select.AddString(_T("缩放"));
m_func_select.AddString(_T("添加文本框"));
m_func_select.AddString(_T("模糊"));
m_func_select.AddString(_T("锐化"));
m_func_select.AddString(_T("调节饱和度"));
m_func_select.AddString(_T("调节曝光"));
m_func_select.AddString(_T("调节色温"));
m_func_select.AddString(_T("图像分割"));
m_func_select.AddString(_T("添加马赛克"));
//初始化进度条
m_progress.SetRange(0, 100);
m_progress.SetStep(1);
m_progress.SetPos(0);
CStatic* pImageControl = (CStatic*)GetDlgItem(IDC_STATIC_PIC);
//m_slider_ad.SetRange(15, 50);//设置滑动范围为15-50
//m_slider_ad.SetTicFreq(5);//每5个单位画一刻度
//m_slider_ad.SetPos(20);//设置滑块初始位置为20
nummo = 20;
SetDlgItemInt(IDC_EDIT_MO, 20);//设置编辑框的初始值SetDlgItemInt(IDC_EDIT1, Start);//设置编辑框的初始值
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMFCImageProcessingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMFCImageProcessingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCImageProcessingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// 内部函数
// 显示位图函数
void CMFCImageProcessingDlg::Show_Bmp(double hfactor = 1,double wfactor = 1)
{
// 清除图片控件
GetDlgItem(IDC_STATIC_PIC)->ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_PIC)->ShowWindow(TRUE);
// 获取图片控件基本变量
CWnd* pWnd = GetDlgItem(IDC_STATIC_PIC); //获得pictrue控件窗口的句柄
CDC* pDC = pWnd->GetDC(); //获得pictrue控件的DC
CRect rect;
pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域
// 计算宽高比,使用浮点数避免精度问题
int image_width = bmpInfo.biWidth;
int image_height = bmpInfo.biHeight;
double wid_hei_ratio = (double)image_width / (double)image_height;
// 调整缩略图大小
if (rect.Width() < wid_hei_ratio * rect.Height())
{
if (wfactor <= 1 && hfactor <= 1)
{
image_width = static_cast<int>(rect.Width() * wfactor);
image_height = static_cast<int>(image_width / wid_hei_ratio * hfactor);
}
else
{
AfxMessageBox(_T("倍数大于一可能无法在缩略图中呈现缩放效果"));
image_width = rect.Width();
image_height = static_cast<int>(image_width / wid_hei_ratio);
}
}
else
{
if (wfactor <= 1 && hfactor <= 1)
{
image_height = static_cast<int>(rect.Height() * hfactor);
image_width = static_cast<int>(wid_hei_ratio * image_height * wfactor);
}
else
{
AfxMessageBox(_T("倍数大于一可能无法在缩略图中呈现缩放效果"));
image_height = rect.Height();
image_width = static_cast<int>(wid_hei_ratio * image_height);
}
}
// 确保图像宽高不为负数
if (image_width <= 0 || image_height <= 0)
{
AfxMessageBox(_T("宽高为负数!"));
return;
}
// 显示位图
pDC->SetStretchBltMode(COLORONCOLOR);
StretchDIBits(pDC->GetSafeHdc(),
(rect.Width() - image_width) / 2,
(rect.Height() - image_height) / 2,
image_width,
image_height,
0,
0,
bmpInfo.biWidth,
bmpInfo.biHeight,
pBmpData,
pBmpInfo,
DIB_RGB_COLORS,
SRCCOPY);
pWnd->ReleaseDC(pDC);
}
// 保存并打开临时位图
void CMFCImageProcessingDlg::Save_Open_Temp_Bmp()
{
// 保存临时位图
CString TempPath = _T(".\\Temp");
if (!PathIsDirectory(TempPath)) CreateDirectory(TempPath, 0); //不存在临时文件夹则创建
CString TempFilePath = _T(".\\Temp\\Temp.bmp");
if (!bmpFile.Open(TempFilePath, CFile::modeCreate |
CFile::modeWrite | CFile::typeBinary))
{
AfxMessageBox(_T("无法打开临时文件进行写入"));
return;
}
bmpFile.Write(&bmpHeader, sizeof(BITMAPFILEHEADER));
bmpFile.Write(&bmpInfo, sizeof(BITMAPINFOHEADER));
DWORD bmpDataSize = bmpInfo.biSizeImage;
if (bmpDataSize == 0)
{
int bpp = bmpInfo.biBitCount / 8;
bmpDataSize = (bmpInfo.biWidth * bpp + RowComplete) * bmpInfo.biHeight;
}
bmpFile.Write(pBmpData, bmpDataSize);
bmpFile.Close();
// 打开临时位图
if (!bmpFile.Open(TempFilePath, CFile::modeRead | CFile::typeBinary))
return;
if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return;
if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
return;
pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
Bytes = bmpHeader.bfSize - bmpHeader.bfOffBits;
pBmpData = new BYTE[Bytes];
bmpFile.Read(pBmpData, Bytes);
FilePath = TempFilePath;
bmpFile.Close();
}
// 将 BGR 编码的位图转换成 OpenCV Mat 格式
void CMFCImageProcessingDlg::Bmp2Mat(cv::Mat img, int height, int width)
{
int index = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
img.at<cv::Vec3b>(y, x)[0] = pBmpData[index + 2]; // B
img.at<cv::Vec3b>(y, x)[1] = pBmpData[index + 1]; // G
img.at<cv::Vec3b>(y, x)[2] = pBmpData[index]; // R
index += 3; // 每个像素 3 byte
if ((index + RowComplete) % (width * 3 + RowComplete) == 0)
index += RowComplete; // 跳过填充字节
}
}
}
// 将 OpenCV Mat 格式还原成 BGR 编码的位图文件
void CMFCImageProcessingDlg::Mat2Bmp(cv::Mat img, int height, int width)
{
int index = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
pBmpData[index + 2] = img.at<cv::Vec3b>(y, x)[0]; // B
pBmpData[index + 1] = img.at<cv::Vec3b>(y, x)[1]; // G
pBmpData[index] = img.at<cv::Vec3b>(y, x)[2]; // R
index += 3; // 每个像素 3 byte
if ((index + RowComplete) % (width * 3 + RowComplete) == 0)
index += RowComplete; // 跳过填充字节
}
}
}
// 获取图像编码器的 CLSID
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0) return -1;
Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == nullptr) return -1;
Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
// 将 bmp 文件转换成 jpg, jpeg, png 文件
void ConvertToBMP(const CString& inputPath, const CString& outputPath)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
Gdiplus::Image image(inputPath);
CLSID bmpClsid;
GetEncoderClsid(L"image/bmp", &bmpClsid);
image.Save(outputPath, &bmpClsid, nullptr);
//Gdiplus::GdiplusShutdown(gdiplusToken);
}
// 控件函数
void CMFCImageProcessingDlg::OnClickedOpenButton()
{
// 设置过滤器
LPCTSTR szFilter = _T("Image Files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png|");
// 构造打开文件对话框
CFileDialog fileDlg(TRUE, _T(".bmp"), NULL,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, szFilter, this);
// 显示打开文件对话框
if (IDOK == fileDlg.DoModal())
{
FilePath = fileDlg.GetPathName(); // 获取文件路径
SetDlgItemText(IDC_OPEN_EDIT, FilePath); // 在编辑框显示文件路径
CString BmpName = fileDlg.GetPathName();
CString EntName = fileDlg.GetFileExt();
EntName.MakeLower();
if (EntName.Compare(_T("bmp")) == 0)
{
// 读取并显示 BMP 文件
if (!bmpFile.Open(BmpName, CFile::modeRead | CFile::typeBinary))
return;
if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return;
if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
return;
pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
Bytes = bmpHeader.bfSize - bmpHeader.bfOffBits; // 计算位图数据大小
pBmpData = new BYTE[Bytes]; // 分配位图数据数组
int bpp = bmpInfo.biBitCount / 8;
RowComplete = (4 - ((bmpInfo.biWidth * bpp) % 4)) % 4; // 计算每行的比特填充字节数
bmpFile.Read(pBmpData, Bytes);
bmpFile.Close();
Show_Bmp();
m_is_open = true;
}
else if (EntName.Compare(_T("jpg")) == 0 || EntName.Compare(_T("jpeg")) == 0 || EntName.Compare(_T("png")) == 0)
{
// 将 JPG/PNG 文件转换为 BMP 并读取
CString tempBmpPath = _T("transfer_temp.bmp");
ConvertToBMP(FilePath, tempBmpPath);
if (!bmpFile.Open(tempBmpPath, CFile::modeRead | CFile::typeBinary))
return;
if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return;
if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
return;
pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
Bytes = bmpHeader.bfSize - bmpHeader.bfOffBits; // 计算位图数据大小
pBmpData = new BYTE[Bytes]; // 分配位图数据数组
int bpp = bmpInfo.biBitCount / 8;
RowComplete = (4 - ((bmpInfo.biWidth * bpp) % 4)) % 4; // 计算每行的比特填充字节数
bmpFile.Read(pBmpData, Bytes);
bmpFile.Close();
Show_Bmp();
m_is_open = true;
}
}
}
void CMFCImageProcessingDlg::OnClickedSaveButton()
{
// TODO: 在此添加控件通知处理程序代码
// 设置过滤器
LPCTSTR szFilter = _T("bmp (*.bmp)|*.bmp|");
// 构造保存文件对话框
CFileDialog fileDlg(FALSE, _T(".bmp"), _T("default.bmp"),
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
fileDlg.m_ofn.lpstrTitle = _T("保存"); // 保存对话窗口标题名
CString FilePath;
if (m_is_open == false) {
AfxMessageBox(_T("没有打开文件,无法保存"));
return;
}
// 显示保存文件对话框
if (IDOK == fileDlg.DoModal()) //判断是否点击了确定
{
FilePath = fileDlg.GetPathName(); // 获取文件路径
SetDlgItemText(IDC_SAVE_EDIT, FilePath);
if (!bmpFile.Open(FilePath, CFile::modeCreate |
CFile::modeWrite | CFile::typeBinary))
{
AfxMessageBox(_T("无法打开文件进行写入"));
return;
}
// 写入文件头
bmpFile.Write(&bmpHeader, sizeof(BITMAPFILEHEADER));
// 写入信息头
bmpFile.Write(&bmpInfo, sizeof(BITMAPINFOHEADER));
// 计算位图数据大小
int bpp = bmpInfo.biBitCount / 8;
DWORD bmpDataSize = bmpHeader.bfSize - bmpHeader.bfOffBits;
if (bmpDataSize == 0)
{
bmpDataSize = ((bmpInfo.biWidth * bpp + RowComplete) * bmpInfo.biHeight);
}
// 写入位图数据
bmpFile.Write(pBmpData, bmpDataSize);
// 关闭文件
bmpFile.Close();
}
}
void CMFCImageProcessingDlg::OnClickedRotation()
{
// TODO: 在此添加控件通知处理程序代码
// 获取原始位图的宽度和高度
int width = bmpInfo.biWidth;
int height = bmpInfo.biHeight;
// 计算新的宽度和高度
int newWidth = height;
int newHeight = width;
// 分配新的位图数据数组
int bpp = bmpInfo.biBitCount / 8; // 每个像素的字节数
DWORD newRowComplete = (4 - ((newWidth * bpp) % 4)) % 4; // 计算新的每行的比特填充字节数
Bytes = (newWidth * bpp + newRowComplete) * newHeight; // 计算新的图像数据大小
BYTE* pNewBmpData = new BYTE[Bytes];
memset(pNewBmpData, 0, Bytes); // 初始化为 0
// 进行像素的旋转变换,注意跳过填充字节
int oldIndex = 0;
int newIndex = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// 计算新坐标
int newX = y;
int newY = width - x - 1;
// 计算原始和新的数据偏移
newIndex = newY * (newWidth * bpp + newRowComplete) + newX * bpp;
memcpy(&pNewBmpData[newIndex], &pBmpData[oldIndex], bpp); // 复制像素
oldIndex += bpp;
if ((oldIndex + RowComplete) % (width * bpp + RowComplete) == 0)
oldIndex += RowComplete; // 跳过填充字节
}
}
//补全填充字节
int newRowSize = newWidth * bpp + newRowComplete;
for (int idx = newWidth * bpp; idx < Bytes; idx += newRowSize)
{
for (int i = 0; i < newRowComplete; i++)
pNewBmpData[idx + i] = 0;
}
// 更新位图信息
// 更新信息头
bmpInfo.biWidth = newWidth;
bmpInfo.biHeight = newHeight;
bmpInfo.biSizeImage = newWidth * newHeight * (bmpInfo.biBitCount / 8);
RowComplete = newRowComplete;
//更新图像数据
delete[] pBmpData;
pBmpData = pNewBmpData;
// 更新文件头
bmpHeader.bfSize = Bytes + bmpHeader.bfOffBits;
// 保持并打开新的位图
Save_Open_Temp_Bmp();
// 显示位图
Show_Bmp();
}
void CMFCImageProcessingDlg::OnClickedBlurButton()
{
// TODO: 在此添加控件通知处理程序代码
// 1. 将位图数据转换为 OpenCV Mat
int width = bmpInfo.biWidth;
int height = bmpInfo.biHeight;
cv::Mat img(height, width, CV_8UC3);
m_progress.SetPos(0);
// 假设位图数据是 BGR 格式
Bmp2Mat(img, height, width);
m_progress.SetPos(40);
// 2. 应用高斯模糊
cv::Mat blurredImg;
cv::GaussianBlur(img, blurredImg, cv::Size(3, 3), 0,0);
m_progress.SetPos(80);
// 3. 将处理后的图像转换回位图格式
Mat2Bmp(blurredImg, height, width);
m_progress.SetPos(100);
// 4. 显示图像
Show_Bmp();
}
void CMFCImageProcessingDlg::OnClickedSharpButton()
{
// TODO: 在此添加控件通知处理程序代码
// 1. 将位图数据转换为 OpenCV Mat
int width = bmpInfo.biWidth;
int height = bmpInfo.biHeight;
cv::Mat img(height, width, CV_8UC3);
m_progress.SetPos(0);
// 假设位图数据是 BGR 格式
Bmp2Mat(img, height, width);
m_progress.SetPos(20);
// 2. 应用高斯滤波锐化模糊图片
cv::Mat SharpenedImg;
cv::GaussianBlur(img, SharpenedImg, cv::Size(3, 3), 0,0);
m_progress.SetPos(40);
cv::addWeighted(img, 2, SharpenedImg, -1, 0, SharpenedImg);
m_progress.SetPos(60);
// 3. 将处理后的图像转换回位图格式
Mat2Bmp(SharpenedImg, height, width);
m_progress.SetPos(100);
// 4. 显示图像
Show_Bmp();
}
void CMFCImageProcessingDlg::OnClickedScaleButton()
{
// TODO: 在此添加控件通知处理程序代码
// 1. 将位图数据转换为 OpenCV Mat
int width = bmpInfo.biWidth;
int height = bmpInfo.biHeight;
cv::Mat img(height, width, CV_8UC3);
// 假设位图数据是 BGR 格式
Bmp2Mat(img, height, width);
CScaleDlg Dlg;
if (Dlg.DoModal() == IDOK2) {
// 2. 用 opencv resize 函数放缩图片
UpdateData(TRUE);
if (m_Scale_Height == 0 || m_Scale_Width == 0)
{
AfxMessageBox(_T("倍数不能为零!"));
return;
}
cv::Mat ResizedImg;
cv::resize(img, ResizedImg, cv::Size(0, 0), m_Scale_Width, m_Scale_Height);
UpdateData(FALSE);
// 更新位图信息
int NewWidth = static_cast<int>(width * m_Scale_Width);
int NewHeight = static_cast<int>(height * m_Scale_Height);
int bpp = bmpInfo.biBitCount / 8;
RowComplete = (4 - ((NewWidth * bpp) % 4)) % 4;
bmpInfo.biWidth = NewWidth;
bmpInfo.biHeight = NewHeight;
bmpInfo.biSizeImage = NewWidth * NewHeight * (bmpInfo.biBitCount / 8);
Bytes = (NewWidth * bpp + RowComplete) * NewHeight;
pBmpData = new BYTE[Bytes];
bmpHeader.bfSize = Bytes + bmpHeader.bfOffBits;
// 3. 将处理后的图像转换回位图格式
Mat2Bmp(ResizedImg, NewHeight, NewWidth);
Save_Open_Temp_Bmp();
// 4. 显示图像
Show_Bmp(m_Scale_Height, m_Scale_Width);
}
}
void CMFCImageProcessingDlg::CreateTextImage(CString& text, CRect& textRect, const CString& textImagePath,const int font_size,const COLORREF& font_col)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
Gdiplus::Bitmap bitmap(textRect.Width(), textRect.Height(), PixelFormat32bppARGB);
Gdiplus::Graphics graphics(&bitmap);
graphics.Clear(Gdiplus::Color(255, 255, 255, 255));
Gdiplus::FontFamily fontFamily(L"Arial");
Gdiplus::Font font(&fontFamily, font_size, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
Gdiplus::PointF pointF(0.0f, 0.0f);
BYTE red = GetRValue(font_col);
BYTE green = GetGValue(font_col);
BYTE blue = GetBValue(font_col);
Gdiplus::SolidBrush solidBrush(Gdiplus::Color(255, red, green, blue));
graphics.DrawString(text, -1, &font, pointF, &solidBrush);
CLSID clsid;
GetEncoderClsid(L"image/bmp", &clsid);
bitmap.Save(textImagePath, &clsid, nullptr);
//Gdiplus::GdiplusShutdown(gdiplusToken);
}
void CMFCImageProcessingDlg::AddTextToImage(CString& text, CRect& textRect,
const int font_size, const COLORREF& font_col)
{
// 创建文本图像
CString textImagePath = _T("text_image.bmp");
CreateTextImage(text, textRect, textImagePath,font_size,font_col);
// 打开文本图像
CImage textImage;
textImage.Load(textImagePath);
// 将文本图像叠加到原始图像上
CImage originalImage;
originalImage.Attach((HBITMAP)::CreateDIBitmap(::GetDC(NULL), &bmpInfo,
CBM_INIT, pBmpData, pBmpInfo, DIB_RGB_COLORS));
CDC* pDC = CDC::FromHandle(originalImage.GetDC());
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.Attach(textImage.Detach());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
pDC->TransparentBlt(textRect.left, textRect.top, textRect.Width(),
textRect.Height(), &memDC, 0, 0, textRect.Width(), textRect.Height(), RGB(255, 255, 255));
memDC.SelectObject(pOldBitmap);
originalImage.ReleaseDC();
// 保存修改后的图像
originalImage.Save(_T("output.bmp"));
}
void CMFCImageProcessingDlg::AdjustInit(int choice,int factor)
{
//读取图像
// 获取 CString 的缓冲区
std::string img_path;
CT2CA pszConvertedAnsiString(FilePath);
// 将缓冲区转换为 std::string
std::string str(pszConvertedAnsiString);
img_path = str;
cv::Mat src = cv::imread(img_path, 1);
if (src.empty())
{
std::cout << "can not load image" << std::endl;
return;
}
cv::Mat dst = src.clone();
int saturation_value = 0;
double brightness_value = 0;
int warm_value = 0;
m_progress.SetPos(0);
switch(choice){
case 1:
saturation_factor = factor;
saturation_value = (factor - 50) * 2;
dst = Saturation(dst, saturation_value,m_progress);
break;
case 2:
brightness_factor = factor;
if (factor < 50)
brightness_value = factor / 50.0;
else
brightness_value = factor / 50.0; //手动缩小了亮度的调整范围
dst = Brightness(dst, brightness_value, 0,m_progress);
break;
case 3:
warmth_factor = factor;
warm_value = (factor - 50) * 2;
dst = ColorTemperature(dst, warm_value,m_progress);
break;
}
// 保存图像
std::string output_path = "output_ad.bmp";
cv::imwrite(output_path, dst);
CString BmpName = _T(".\\output_ad.bmp");
// 读取并显示 BMP 文件
if (!bmpFile.Open(BmpName, CFile::modeRead | CFile::typeBinary))
return;
if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return;
if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
return;
pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
DWORD Bytes = bmpInfo.biWidth * bmpInfo.biHeight * (bmpInfo.biBitCount / 8);
pBmpData = new BYTE[Bytes];
bmpFile.Read(pBmpData, Bytes);
FilePath = BmpName;
Show_Bmp();
m_is_open = true;
bmpFile.Close();
}
void CMFCImageProcessingDlg::OnBnClickedTextButton()
{
// TODO: 在此添加控件通知处理程序代码
if (!m_is_open)
{
AfxMessageBox(_T("请先打开一张图片"));
return;
}
m_is_text = true;
m_texted = true;
AfxMessageBox(_T("请在图片上点击鼠标左键,选择文本框位置"));
}
void CMFCImageProcessingDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_is_text == true) {
CTextInputDialog dlg;
if (dlg.DoModal() == IDOK) // 显示对话框并等待用户输入
{
m_text = dlg.GetInputText(); // 获取用户输入的文本
m_font_size = dlg.GetFontSize(); //获取字体大小
m_font_col = dlg.GetFontCol(); //获取字体颜色
// 获取图片控件的指针
CWnd* pWnd = GetDlgItem(IDC_STATIC_PIC);
// 获取图片控件在屏幕上的矩形区域
CRect picRect;
pWnd->GetWindowRect(&picRect);
// 将鼠标点击点从主窗口坐标转换为屏幕坐标
ClientToScreen(&point);
// 检查点击点是否在图片控件内部
if (picRect.PtInRect(point)) {
// 用户定义文本框的位置,可以改为动态捕获鼠标位置
//
// 将屏幕坐标转换为图片控件的客户区坐标
pWnd->ScreenToClient(&point);
// 获取图片控件的矩形区域
CRect rect;
pWnd->GetClientRect(&rect); //556 453
// 计算宽高比
double wid_hei_ratio = (double)bmpInfo.biWidth / (double)bmpInfo.biHeight;
// 计算缩放比例
double scaleX = (double)rect.Width() / bmpInfo.biWidth;
double scaleY = (double)rect.Height() / bmpInfo.biHeight;
double scale = min(scaleX, scaleY);
// 计算缩略图在控件区的偏移量
int offsetX = (rect.Width() - (int)(bmpInfo.biWidth * scale)) / 2;
int offsetY = (rect.Height() - (int)(bmpInfo.biHeight * scale)) / 2;
// 将控件区的坐标映射回原始图片的像素坐标
int originalX = (int)((point.x - offsetX) / scale);
int originalY = (int)((point.y - offsetY) / scale);
// 确保坐标在原始图片的范围内
if (originalX < 0 || originalX >= bmpInfo.biWidth || originalY < 0 || originalY >= bmpInfo.biHeight)
{
AfxMessageBox(_T("坐标超出图片范围!"));
return;
}
// 创建字体
m_font.DeleteObject();
m_font.CreatePointFont(m_font_size, _T("Arial"));
CDC* pDC = GetDC(); // 获取设备上下文
// 获取要输入的字符的宽度和高度
CSize size = pDC->GetTextExtent(m_text);
// 输出宽度和高度
int charWidth = (size.cx * m_font_size / 10) / scale; //font_size取十分之一为字体大小
int charHeight = (size.cy * m_font_size / 10) / scale;
// 绘制一个矩形
int left = originalX - charWidth / 2;
int top = originalY - charHeight / 2;
int right = originalX + charWidth / 2;
int bottom = originalY + charHeight / 2;
m_textRect = CRect(left, top, right, bottom);
//m_textRect = CRect(50,50,200,100);
// 添加文本到图像
AddTextToImage(m_text, m_textRect,m_font_size,m_font_col);
CString BmpName = _T(".\\output.bmp");
// 读取并显示 BMP 文件
if (!bmpFile.Open(BmpName, CFile::modeRead | CFile::typeBinary))
return;
if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return;
if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
return;
pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
Bytes = bmpHeader.bfSize - bmpHeader.bfOffBits;
pBmpData = new BYTE[Bytes];
bmpFile.Read(pBmpData, Bytes);
Show_Bmp();
m_is_open = true;
bmpFile.Close();
FilePath = BmpName;
}
else {
AfxMessageBox(_T("不能点击图片外的区域!"));
}
}
m_is_text = false;
}
if (ismosaic) {
// 获取图片控件的指针
CWnd* pWnd = GetDlgItem(IDC_STATIC_PIC);
// 获取图片控件的客户区矩形
CRect picRect;
// 获取控件相对于屏幕的位置
GetDlgItem(IDC_STATIC_PIC)->GetWindowRect(&picRect);
//将控件转化为对话框上的相对位置
ScreenToClient(picRect);