-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePro2Doc.cpp
More file actions
312 lines (247 loc) · 7.21 KB
/
ImagePro2Doc.cpp
File metadata and controls
312 lines (247 loc) · 7.21 KB
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
// ImagePro2Doc.cpp: CImagePro2Doc 클래스의 구현
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS는 미리 보기, 축소판 그림 및 검색 필터 처리기를 구현하는 ATL 프로젝트에서 정의할 수 있으며
// 해당 프로젝트와 문서 코드를 공유하도록 해 줍니다.
#ifndef SHARED_HANDLERS
#include "ImagePro2.h"
#endif
#include "ImagePro2Doc.h"
#include <propkey.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CImagePro2Doc
IMPLEMENT_DYNCREATE(CImagePro2Doc, CDocument)
BEGIN_MESSAGE_MAP(CImagePro2Doc, CDocument)
END_MESSAGE_MAP()
// CImagePro2Doc 생성/소멸
CImagePro2Doc::CImagePro2Doc() noexcept
{
inputimg = NULL;
inputimg2 = NULL;
resultimg = NULL;
gresultimg = NULL;
// TODO: 여기에 일회성 생성 코드를 추가합니다.
}
CImagePro2Doc::~CImagePro2Doc()
{
int i;
if (inputimg != NULL)
{
for (i = 0; i < ImageHeight; i++)
free(inputimg[i]);
free(inputimg);
}
if (resultimg != NULL)
{
for (i = 0; i < ImageHeight; i++)
free(resultimg[i]);
free(resultimg);
}
if (inputimg2 != NULL)
{
for (i = 0; i < ImageHeight; i++)
free(inputimg2[i]);
free(inputimg2);
}
if (gresultimg != NULL)
{
for (i = 0; i < gImageHeight; i++)
free(gresultimg[i]);
free(gresultimg);
}
}
BOOL CImagePro2Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: 여기에 재초기화 코드를 추가합니다.
// SDI 문서는 이 문서를 다시 사용합니다.
return TRUE;
}
// CImagePro2Doc serialization
void CImagePro2Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: 여기에 저장 코드를 추가합니다.
}
else
{
LoadImageFile(ar);
// TODO: 여기에 로딩 코드를 추가합니다.
}
}
#ifdef SHARED_HANDLERS
// 축소판 그림을 지원합니다.
void CImagePro2Doc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
// 문서의 데이터를 그리려면 이 코드를 수정하십시오.
dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));
CString strText = _T("TODO: implement thumbnail drawing here");
LOGFONT lf;
CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
pDefaultGUIFont->GetLogFont(&lf);
lf.lfHeight = 36;
CFont fontDraw;
fontDraw.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&fontDraw);
dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
dc.SelectObject(pOldFont);
}
// 검색 처리기를 지원합니다.
void CImagePro2Doc::InitializeSearchContent()
{
CString strSearchContent;
// 문서의 데이터에서 검색 콘텐츠를 설정합니다.
// 콘텐츠 부분은 ";"로 구분되어야 합니다.
// 예: strSearchContent = _T("point;rectangle;circle;ole object;");
SetSearchContent(strSearchContent);
}
void CImagePro2Doc::SetSearchContent(const CString& value)
{
if (value.IsEmpty())
{
RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
}
else
{
CMFCFilterChunkValueImpl *pChunk = nullptr;
ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
if (pChunk != nullptr)
{
pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
SetChunkValue(pChunk);
}
}
}
#endif // SHARED_HANDLERS
// CImagePro2Doc 진단
#ifdef _DEBUG
void CImagePro2Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CImagePro2Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CImagePro2Doc 명령
int CImagePro2Doc::LoadImageFile(CArchive& ar)
{
{
int maxValue, i;
char type[16], buf[256];
CFile* fp = ar.GetFile();
CString fname = fp->GetFilePath();
if (!strcmp(strchr(fname, '.'), ".bmp") || !strcmp(strchr(fname, '.'), ".BMP")) {
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
fp->Read(&bf, sizeof(BITMAPFILEHEADER));
fp->Read(&bi, sizeof(BITMAPINFOHEADER));
ImageWidth = bi.biWidth;
ImageHeight = bi.biHeight;
depth = bi.biBitCount / 8;
if (depth != 1 && depth != 3 && depth != 4) {
AfxMessageBox("지원하지 않는 BMP 파일 형식입니다.");
return 0;
}
if (depth == 4) {
depth = 3; // 4-byte BMP images are typically RGBA, convert to RGB
}
// BMP 이미지 데이터는 아래에서 위로 저장되므로 뒤집어서 읽어야 함
inputimg = (unsigned char**)malloc(ImageHeight * sizeof(unsigned char*));
resultimg = (unsigned char**)malloc(ImageHeight * sizeof(unsigned char*));
int rowSize = ((ImageWidth * depth + 3) / 4) * 4; // Adjust for padding
for (int i = 0; i < ImageHeight; i++) {
inputimg[i] = (unsigned char*)malloc(rowSize);
resultimg[i] = (unsigned char*)malloc(rowSize);
fp->Seek(bf.bfOffBits + (ImageHeight - i - 1) * rowSize, CFile::begin);
fp->Read(inputimg[i], rowSize);
// BGR to RGB
for (int j = 0; j < ImageWidth * depth; j += depth) {
unsigned char temp = inputimg[i][j];
inputimg[i][j] = inputimg[i][j + 2];
inputimg[i][j + 2] = temp;
}
}
}
//strcmp(strchr(fname, '.'), ".ppm"); // == 0 => 확장자가 ppm
else {
if (!strcmp(strchr(fname, '.'), ".ppm") || !strcmp(strchr(fname, '.'), ".PPM") ||
!strcmp(strchr(fname, '.'), ".pgm") || !strcmp(strchr(fname, '.'), ".PGM")) {
ar.ReadString(type, 15);
do {
ar.ReadString(buf, 255);
} while (buf[0] == '#');
sscanf(buf, "%d %d", &ImageWidth, &ImageHeight);
do {
ar.ReadString(buf, 255);
} while (buf[0] == '#');
sscanf(buf, "%d", &maxValue);
if (!strcmp(type, "P5")) depth = 1;
else depth = 3;
}
else if (!strcmp(strchr(fname, '.'), ".raw") || !strcmp(strchr(fname, '.'), ".RAW")) {
ImageWidth = 256;
ImageHeight = 256;
depth = 1;
}
//메모리 할당
inputimg = (unsigned char**)malloc(ImageHeight * sizeof(unsigned char*));
resultimg = (unsigned char**)malloc(ImageHeight * sizeof(unsigned char*));
for (i = 0; i < ImageHeight; i++) {
inputimg[i] = (unsigned char*)malloc(ImageWidth * depth);
resultimg[i] = (unsigned char*)malloc(ImageWidth * depth);
}
//파일에서 읽어서 저장
for (i = 0; i < ImageHeight; i++)
ar.Read(inputimg[i], ImageWidth * depth);
}
return 0;
}
}
void CImagePro2Doc::LoadSecondImageFile(CArchive& ar)
{
int w, h, d;
int maxValue, i;
char type[16], buf[256];
CFile* fp = ar.GetFile();
CString fname = fp->GetFilePath();
if (!strcmp(strchr(fname, '.'), ".ppm") || !strcmp(strchr(fname, '.'), ".PPM") ||
!strcmp(strchr(fname, '.'), ".pgm") || !strcmp(strchr(fname, '.'), ".PGM")) {
ar.ReadString(type, 15);
do {
ar.ReadString(buf, 255);
} while (buf[0] == '#');
sscanf(buf, "%d %d", &w, &h);
do {
ar.ReadString(buf, 255);
} while (buf[0] == '#');
sscanf(buf, "%d", &maxValue);
if (!strcmp(type, "P5")) d = 1;
else d = 3;
}
else if (!strcmp(strchr(fname, '.'), ".raw") || !strcmp(strchr(fname, '.'), ".RAW")) {
w = 256;
h = 256;
d = 1;
}
if (ImageWidth != w || ImageHeight != h || depth != d) {
AfxMessageBox("두번째 파일의 width,height,depth가 다르면 읽을 수 없습니다.");
return;
}
//메모리 할당
inputimg2 = (unsigned char**)malloc(ImageHeight * sizeof(unsigned char*));
for (i = 0; i < ImageHeight; i++) {
inputimg2[i] = (unsigned char*)malloc(ImageWidth * depth);
}
//파일에서 읽어서 저장
for (i = 0; i < ImageHeight; i++)
ar.Read(inputimg2[i], ImageWidth * depth);
return;
}