-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogramView.cpp
More file actions
215 lines (215 loc) · 11.1 KB
/
Copy pathHistogramView.cpp
File metadata and controls
215 lines (215 loc) · 11.1 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
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// HistogramView.cpp
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2019-2024 Adrian Maurer. All rights reserved.
// Distributed under the GPL-3.0 software license (https://opensource.org/licenses/GPL-3.0).
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "pch.h"
#include "framework.h"
#include "HistogramView.h"
#include "HspGradatorApp.h"
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Histogram
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Histogram - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
float Histogram::calcValue(const float v) const
{
return (*this)[static_cast<int>(v * (static_cast<float>(size()) - 1.0F))];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// HistogramArray
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// HistogramArray - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void HistogramArray::init(const CTrueColorImage& mi)
{
for (Histogram& h : *this)
{
h.fill(0.0F);
h.setAverage(0.0F);
}
if (const CSize is(mi.getSize()); is.cx > 0 && is.cy > 0)
{
ama::Color<long long> sc(0, 0, 0);
long long sp = 0;
for (int j = 0; j < is.cy; ++j)
{
for (int i = 0; i < is.cx; ++i)
{
const ama::Color<BYTE> c(mi.getPixel<ama::Color<BYTE>>(i, j));
const BYTE p = ama::round<BYTE>(calcPerceivedBrightness(ama::Color<float>(c.r, c.g, c.b)));
// Count values
++(*this)[0][c.r];
++(*this)[1][c.g];
++(*this)[2][c.b];
++(*this)[3][p];
// Sum up values
sc.r += c.r;
sc.g += c.g;
sc.b += c.b;
sp += p;
}
}
// Normalize values
float m = 0.0F;
for (int i = 0; i < 3; ++i)
{
for (const float& v : (*this)[i]) if (m < v) m = v;
}
if (m > 0.0F)
{
for (int i = 0; i < 3; ++i)
{
for (float& v : (*this)[i]) v /= m;
}
}
m = 0.0F;
for (const float& v : (*this)[3]) if (m < v) m = v;
if (m > 0.0F)
{
for (float& v : (*this)[3]) v /= m;
}
// Take the average
const float s = static_cast<float>(is.cx * is.cy);
(*this)[0].setAverage(sc.r / s);
(*this)[1].setAverage(sc.g / s);
(*this)[2].setAverage(sc.b / s);
(*this)[3].setAverage(sp / s / 2.55F);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHistogramView
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHistogramView - CONSTRUCTION / DESTRUCTION / ASSIGNMENT
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CHistogramView::CHistogramView() :
p_histogramArray(nullptr),
memoryDc(),
memoryBitmap(),
borderSize(0),
textHeight(0),
wndSize(0, 0)
{
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IMPLEMENT_DYNCREATE(CHistogramView, CView)
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHistogramView - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CHistogramView::update(const HistogramArray* const p_ha)
{
p_histogramArray = p_ha;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CHistogramView::draw()
{
if (p_histogramArray)
{
const CSize ds(borderSize, borderSize); // Distance size
memoryDc.FillSolidRect(0, 0, wndSize.cx, wndSize.cy, ::GetSysColor(COLOR_3DLIGHT));
const int hw = (wndSize.cx - ds.cx) / 4;
CRect hr(ds.cx, ds.cy, hw, wndSize.cy - ds.cy - textHeight); // Histogram rect
CRect tr(ds.cx, hr.bottom, hw, hr.bottom + textHeight); // Text rect
const std::array<COLORREF, 4> ca{RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255), ::GetSysColor(COLOR_WINDOWTEXT)};
const std::wstring sa(L"RGBP");
for (int i = 0; i < 4; ++i)
{
const COLORREF bc = RGB(128, 128, 128);
memoryDc.FillSolidRect(hr.left, hr.top - 1, hr.Width(), 1, bc);
memoryDc.FillSolidRect(hr.left, hr.bottom, hr.Width(), 1, bc);
memoryDc.FillSolidRect(hr.left - 1, hr.top - 1, 1, hr.Height() + 2, bc);
memoryDc.FillSolidRect(hr.right, hr.top - 1, 1, hr.Height() + 2, bc);
const double wf = 256.0 / hr.Width(); // Width factor
for (int j = 0; j < hr.Width(); ++j)
{
if (hr.Height() > 0)
{
const int k1 = ama::round((j + 0.0) * wf);
const int k2 = ama::round((j + 1.0) * wf);
float mh = (*p_histogramArray)[i][k1];
for (int k = k1 + 1; k < k2; ++k) mh = (std::max)((*p_histogramArray)[i][k], mh);
memoryDc.FillSolidRect(hr.left + j, hr.bottom, 1, static_cast<int>(-mh * hr.Height()), ca[i]);
}
}
memoryDc.SetTextAlign(TA_LEFT | TA_TOP);
memoryDc.TextOutW(tr.left, tr.top + textHeight / 3, &sa[i], 1);
memoryDc.SetTextAlign(TA_RIGHT | TA_TOP);
const std::wstring as(std::format(L"A={:.1f}", (*p_histogramArray)[i].getAverage()));
memoryDc.TextOutW(tr.right, tr.top + textHeight / 3, as.data());
hr.OffsetRect(hw, 0);
tr.OffsetRect(hw, 0);
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CHistogramView::refresh()
{
Invalidate(FALSE);
UpdateWindow();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHistogramView - OVERRIDES
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BOOL CHistogramView::PreCreateWindow(CREATESTRUCT& cs)
{
if (CView::PreCreateWindow(cs))
{
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, ::LoadCursor(nullptr, IDC_ARROW));
return TRUE;
}
return FALSE;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CHistogramView::OnDraw(CDC* pDC)
{
draw();
pDC->BitBlt(0, 0, wndSize.cx, wndSize.cy, &memoryDc, 0, 0, SRCCOPY);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CHistogramView - MESSAGE MAP FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CHistogramView, CView)
ON_WM_SIZE()
ON_WM_CREATE()
END_MESSAGE_MAP()
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int CHistogramView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == 0)
{
if (memoryDc.CreateCompatibleDC(nullptr))
{
CClientDC dc(this);
const CSize mws( // Maximum window size
::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN));
if (memoryBitmap.CreateCompatibleBitmap(&dc, mws.cx, mws.cy))
{
if (memoryDc.SelectObject(&memoryBitmap))
{
memoryDc.SetBkMode(TRANSPARENT);
memoryDc.SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
memoryDc.SetTextAlign(TA_LEFT | TA_TOP);
borderSize = mws.cy / 120;
textHeight = memoryDc.GetTextExtent(L"A").cy;
return 0;
}
}
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CHistogramView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
wndSize = {cx, cy};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------