-
Notifications
You must be signed in to change notification settings - Fork 0
/
SketcherEngine.cpp
435 lines (355 loc) · 8.53 KB
/
SketcherEngine.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
#include "StdAfx.h"
#include "SketcherEngine.h"
#include <comdef.h>
#include <cmath>
#include "Display.h"
SketcherEngine::SketcherEngine(void)
: m_iDisplay(DISPLAY_CANVAS)
{
}
SketcherEngine::~SketcherEngine(void)
{
}
void SketcherEngine::UpdateBuffer(const CRect& drRect)
{
if(m_imgBuffer.IsNull())
{
m_imgBuffer.Create(drRect.Width(), drRect.Height(), m_imgSource.GetBPP());
}
else
{
if(m_imgBuffer.GetWidth() != drRect.Width() || m_imgBuffer.GetHeight() != drRect.Height())
{
m_imgBuffer.Destroy();
m_imgBuffer.Create(drRect.Width(), drRect.Height(), m_imgSource.GetBPP());
}
}
}
void SketcherEngine::PaintBufferBackground(CDC* pDC, const CRect& drRect)
{
CBrush brush;
brush.CreateSolidBrush(RGB(125, 125, 125));
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(125, 125, 125));
CBrush* pOldBrush=pDC->SelectObject(&brush);
CPen* pOldPen=pDC->SelectObject(&pen);
pDC->Rectangle(&drRect);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
pen.DeleteObject();
brush.DeleteObject();
}
bool SketcherEngine::Load(LPCTSTR filename)
{
m_imgSource.Destroy();
HRESULT hResult=m_imgSource.Load(filename);
if(FAILED(hResult))
{
CString fmt;
fmt.Format(_T("Error: Failed to load %s: \n %x - %s"), filename, hResult, _com_error(hResult));
AfxMessageBox(fmt, MB_OK);
return false;
}
m_imgCanvas.Destroy();
m_imgCanvas.Create(m_imgSource.GetWidth(), m_imgSource.GetHeight(), m_imgSource.GetBPP());
ResetCanvas();
m_imgGradient.Destroy();
m_imgLuminance.Destroy();
return true;
}
void SketcherEngine::CreateGradientMap()
{
const float PI=3.14159265f;
m_imgGradient.Destroy();
m_imgGradient.Create(m_imgSource.GetWidth(), m_imgSource.GetHeight(), m_imgSource.GetBPP());
int height=m_imgSource.GetHeight();
int width=m_imgSource.GetWidth();
for(int y=0; y<height; y++)
{
for(int x=0; x<width; x++)
{
int norm=static_cast<int>(GetGradientNorm(x, y));
m_imgGradient.SetPixel(x, y, RGB(norm, norm, norm));
}
}
}
int SketcherEngine::GetLuminance(COLORREF color)
{
int r=GetRValue(color);
int g=GetGValue(color);
int b=GetBValue(color);
return static_cast<int>(0.299f * r + 0.587f * g + 0.114f * b);
}
void SketcherEngine::CreateLuminanceMap()
{
m_imgLuminance.Destroy();
m_imgLuminance.Create(m_imgSource.GetWidth(), m_imgSource.GetHeight(), m_imgSource.GetBPP());
int height=m_imgSource.GetHeight();
int width=m_imgSource.GetWidth();
for(int y=0; y<height; y++)
{
for(int x=0; x<width; x++)
{
int luminance=GetLuminance(x, y);
m_imgLuminance.SetPixel(x, y, RGB(luminance, luminance, luminance));
}
}
}
CPoint SketcherEngine::GetGradient(int x, int y)
{
CPoint dir;
int pixel[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
pixel[i][j]=0;
}
}
int width=m_imgSource.GetWidth();
int height=m_imgSource.GetHeight();
for(int i=0; i<3; i++)
{
int yy=y + i - 1;
if(yy < 0)
{
continue;
}
else if(yy >= height)
{
break;
}
for(int j=0; j<3; j++)
{
int xx=x+j-1;
if(xx < 0)
{
continue;
}
else if(xx >= width)
{
break;
}
if(m_imgLuminance.IsNull())
{
pixel[i][j]=GetLuminance(xx, yy);
}
else
{
pixel[i][j]=GetRValue(m_imgLuminance.GetPixel(xx, yy));
}
}
}
dir.x= - pixel[0][0] + pixel[0][2]
- 2 * pixel[1][0] + 2 * pixel[1][2]
- pixel[2][0] + pixel[2][2];
dir.y= - pixel[0][0] + pixel[2][0]
- 2 * pixel[0][1] + 2 * pixel[2][1]
- pixel[0][2] + pixel[2][2];
return dir;
}
int SketcherEngine::GetLuminance(int x, int y)
{
return GetLuminance(m_imgSource.GetPixel(x, y));
}
float SketcherEngine::GetGradientNorm(int x, int y)
{
CPoint dir=GetGradient(x, y);
float xx=static_cast<float>(dir.x);
float yy=static_cast<float>(dir.y);
float l=sqrt(xx * xx + yy * yy);
return l;
}
void SketcherEngine::DisplaySource(CDC* pDC, const CRect& vrect)
{
m_imgSource.StretchBlt(pDC->m_hDC, vrect.left, vrect.top, vrect.Width(), vrect.Height(), SRCCOPY);
}
CRect SketcherEngine::GetCanvasRect(const CRect& rtClient)
{
CRect vrect;
int img_width=m_imgSource.GetWidth();
int img_height=m_imgSource.GetHeight();
float img_aspect=static_cast<float>(img_width) / static_cast<float>(img_height);
int rt_width=rtClient.Width();
int rt_height=rtClient.Height();
float rt_aspect=static_cast<float>(rt_width) / static_cast<float>(rt_height);
vrect.left=0;
vrect.top=0;
int canvas_width=rt_width;
int canvas_height=rt_height;
if(img_aspect < rt_aspect)
{
canvas_width=static_cast<int>(rt_height * img_aspect);
vrect.left=static_cast<int>((rt_width - canvas_width) / 2.0);
}
else if(img_aspect > rt_aspect)
{
canvas_height=static_cast<int>(rt_width / img_aspect);
vrect.top=static_cast<int>((rt_height-canvas_height) / 2.0);
}
vrect.right=vrect.left + canvas_width;
vrect.bottom=vrect.top+canvas_height;
return vrect;
}
void SketcherEngine::SetDisplay(int iDisplay)
{
m_iDisplay=iDisplay;
}
void SketcherEngine::Display(CDC* pDC, const CRect& rect)
{
UpdateBuffer(rect);
CDC* pBufferDC=CDC::FromHandle(m_imgBuffer.GetDC());
PaintBufferBackground(pBufferDC, rect);
CRect vrect=GetCanvasRect(rect);
switch(m_iDisplay)
{
case DISPLAY_SOURCE:
DisplaySource(pBufferDC, vrect);
break;
case DISPLAY_GRADIENT_MAP:
DisplayGradientMap(pBufferDC, vrect);
break;
case DISPLAY_LUMINANCE_MAP:
DisplayLuminanceMap(pBufferDC, vrect);
break;
case DISPLAY_CANVAS:
DisplayCanvas(pBufferDC, vrect);
break;
}
m_imgBuffer.ReleaseDC();
m_imgBuffer.BitBlt(pDC->m_hDC, 0, 0, SRCCOPY);
}
int SketcherEngine::GetDisplay(void) const
{
return m_iDisplay;
}
bool SketcherEngine::IsNull(void) const
{
return m_imgSource.IsNull();
}
void SketcherEngine::Save(LPCTSTR pFilename)
{
switch(m_iDisplay)
{
case DISPLAY_SOURCE:
m_imgSource.Save(pFilename);
break;
case DISPLAY_GRADIENT_MAP:
if(m_imgGradient.IsNull())
{
CreateGradientMap();
}
m_imgGradient.Save(pFilename);
break;
case DISPLAY_LUMINANCE_MAP:
if(m_imgLuminance.IsNull())
{
CreateLuminanceMap();
}
m_imgLuminance.Save(pFilename);
break;
case DISPLAY_CANVAS:
m_imgCanvas.Save(pFilename);
break;
}
}
void SketcherEngine::DisplayGradientMap(CDC* pDC, const CRect& rect)
{
if(m_imgGradient.IsNull())
{
gDisplay.SetStatusText(_T("Gradient Map does not exist. Create Gradient Map..."));
CreateGradientMap();
gDisplay.SetStatusText(_T("Gradient Map successfully created..."));
}
m_imgGradient.StretchBlt(pDC->m_hDC, rect.left, rect.top, rect.Width(), rect.Height(), SRCCOPY);
}
void SketcherEngine::DisplayLuminanceMap(CDC* pDC, const CRect& rect)
{
if(m_imgLuminance.IsNull())
{
gDisplay.SetStatusText(_T("Luminance Map does not exist. Create Luminance Map..."));
CreateLuminanceMap();
gDisplay.SetStatusText(_T("Luminance Map successfully created..."));
}
m_imgLuminance.StretchBlt(pDC->m_hDC, rect.left, rect.top, rect.Width(), rect.Height(), SRCCOPY);
}
int SketcherEngine::GetWidth() const
{
if(m_imgSource.IsNull())
{
return -1;
}
return m_imgSource.GetWidth();
}
int SketcherEngine::GetHeight() const
{
if(m_imgSource.IsNull())
{
return -1;
}
return m_imgSource.GetHeight();
}
Vec2f SketcherEngine::GetDirection(int x, int y)
{
CPoint dir=GetGradient(x, y);
Vec2f orientation;
float xx=-static_cast<float>(dir.y) / 255.0f;
float yy=static_cast<float>(dir.x) / 255.0f;
float l=sqrt(xx * xx + yy * yy);
if(l == 0)
{
orientation.SetX(0);
orientation.SetY(0);
}
else
{
orientation.SetX(xx / l);
orientation.SetY(yy / l);
}
return orientation;
}
void SketcherEngine::DisplayCanvas(CDC* pDC, const CRect& rect)
{
m_imgCanvas.StretchBlt(pDC->m_hDC, rect.left, rect.top, rect.Width(), rect.Height(), SRCCOPY);
}
bool SketcherEngine::IsGradientMapImgAvailable(void)
{
return !m_imgGradient.IsNull();
}
bool SketcherEngine::IsLuminanceMapImgAvailable(void)
{
return !m_imgLuminance.IsNull();
}
void SketcherEngine::Draw(SketchElement* pElement)
{
CDC* pDC=CDC::FromHandle(m_imgCanvas.GetDC());
pElement->Draw(this, pDC);
m_imgCanvas.ReleaseDC();
}
COLORREF SketcherEngine::SampleColor(int x, int y)
{
return m_imgSource.GetPixel(x, y);
}
void SketcherEngine::ResetCanvas(void)
{
if(m_imgCanvas.IsNull())
{
return;
}
else
{
CDC* pDC=CDC::FromHandle(m_imgCanvas.GetDC());
CBrush brush;
brush.CreateSolidBrush(RGB(255, 255, 255));
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
CPen* pOldPen=pDC->SelectObject(&pen);
CBrush* pOldBrush=(CBrush*)pDC->SelectObject(&brush);
pDC->Rectangle(0, 0, m_imgCanvas.GetWidth(), m_imgCanvas.GetHeight());
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
brush.DeleteObject();
pen.DeleteObject();
m_imgCanvas.ReleaseDC();
}
}