-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCImageFilterDesc.cpp
229 lines (174 loc) · 5.63 KB
/
CImageFilterDesc.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
// CImageFilterDesc.cpp
//
// CImageFilterDesc class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define BLACK_POINT_ATTRIB CONSTLIT("blackPoint")
#define BLOOM_POINT_ATTRIB CONSTLIT("bloomPoint")
#define BRIGHTNESS_ATTRIB CONSTLIT("brightness")
#define GAMMA_ATTRIB CONSTLIT("gamma")
#define HUE_ATTRIB CONSTLIT("hue")
#define OPACITY_ATTRIB CONSTLIT("opacity")
#define SATURATION_ATTRIB CONSTLIT("saturation")
#define WHITE_POINT_ATTRIB CONSTLIT("whitePoint")
constexpr int DEFAULT_BLOOM_POINT = 220;
constexpr Metric BLOOM_BLUR_RADIUS = 48.0;
static TStaticStringTable<TStaticStringEntry<CImageFilterDesc::ETypes>, 4> TYPE_TABLE = {
"Bloom", CImageFilterDesc::filterBloom,
"Colorize", CImageFilterDesc::filterColorize,
"HueSaturation", CImageFilterDesc::filterHueSaturation,
"Levels", CImageFilterDesc::filterLevels,
};
CImageFilterDesc::CImageFilterDesc (void) :
m_iType(filterNone),
m_iData1(0),
m_iData2(0),
m_iData3(0),
m_rData1(0.0),
m_byOpacity(0)
// CImageFilterDesc constructor
{
}
bool CImageFilterDesc::operator== (const CImageFilterDesc &Val) const
// CImageFilterDesc operator ==
{
return (m_iType == Val.m_iType
&& m_iData1 == Val.m_iData1
&& m_iData2 == Val.m_iData2
&& m_iData3 == Val.m_iData3
&& m_rData1 == Val.m_rData1
&& m_byOpacity == Val.m_byOpacity);
}
void CImageFilterDesc::ApplyBloom (CG32bitImage &Dest, int iClipPoint) const
// ApplyBloom
//
// Bloom effect
{
RECT rcRect;
rcRect.left = 0;
rcRect.top = 0;
rcRect.right = Dest.GetWidth();
rcRect.bottom = Dest.GetHeight();
// Start by creating an 8 bit image that will contain the bloom
CG8bitImage Bloom;
Bloom.Create(Dest.GetWidth(), Dest.GetHeight());
// Now get all values above the clip point and put them in the bloom.
CGFilter::Threshold(Dest, rcRect, (BYTE)iClipPoint, Bloom);
// Blur the bloom
CGFilter::Blur(Bloom, rcRect, BLOOM_BLUR_RADIUS, Bloom);
// Now screen on the original
BYTE *pSrcRow = Bloom.GetPixelPos(0, 0);
BYTE *pSrcRowEnd = Bloom.GetPixelPos(0, Bloom.GetHeight());
CG32bitPixel *pDestRow = Dest.GetPixelPos(0, 0);
while (pSrcRow < pSrcRowEnd)
{
BYTE *pSrcPos = pSrcRow;
BYTE *pSrcPosEnd = pSrcRow + Bloom.GetWidth();
CG32bitPixel *pDestPos = pDestRow;
while (pSrcPos < pSrcPosEnd)
{
BYTE byScreen = *pSrcPos;
if (byScreen > 0 && pDestPos->GetAlpha() > 0)
*pDestPos = CG32bitPixel::Screen(*pDestPos, byScreen);
pSrcPos++;
pDestPos++;
}
pSrcRow = Bloom.NextRow(pSrcRow);
pDestRow = Dest.NextRow(pDestRow);
}
}
void CImageFilterDesc::ApplyColorize (CG32bitImage &Dest, int iHue, int iSaturation, int iBrightness, BYTE byOpacity) const
// ApplyColorize
//
// Colorize the image.
{
Metric rHue = (Metric)iHue;
Metric rSaturation = (Metric)iSaturation / 100.0;
CG32bitPixel *pDestRow = Dest.GetPixelPos(0, 0);
CG32bitPixel *pDestRowEnd = Dest.GetPixelPos(0, Dest.GetHeight());
while (pDestRow < pDestRowEnd)
{
CG32bitPixel *pDestPos = pDestRow;
CG32bitPixel *pDestPosEnd = pDestRow + Dest.GetWidth();
while (pDestPos < pDestPosEnd)
{
*pDestPos = CG32bitPixel::Colorize(*pDestPos, rHue, rSaturation);
pDestPos++;
}
pDestRow = Dest.NextRow(pDestRow);
}
}
void CImageFilterDesc::ApplyHueSaturation (CG32bitImage &Dest, int iHue, int iSaturation, int iBrightness, BYTE byOpacity) const
// ApplyHueSaturation
//
// Adjust hue/saturation/brightness
{
}
void CImageFilterDesc::ApplyLevels (CG32bitImage &Dest, int iBlack, int iWhite, Metric rGamma) const
// ApplyLevels
//
// Adjust image levels.
{
}
void CImageFilterDesc::ApplyTo (CG32bitImage &Image) const
// ApplyTo
//
// Applies the filter to the given image.
{
switch (m_iType)
{
case filterBloom:
ApplyBloom(Image, m_iData1);
break;
case filterColorize:
ApplyColorize(Image, m_iData1, m_iData2, m_iData3, m_byOpacity);
break;
case filterHueSaturation:
ApplyHueSaturation(Image, m_iData1, m_iData2, m_iData3, m_byOpacity);
break;
case filterLevels:
ApplyLevels(Image, m_iData1, m_iData2, m_rData1);
break;
default:
// Nothing to do
break;
}
}
ALERROR CImageFilterDesc::InitFromXML (SDesignLoadCtx &Ctx, const CXMLElement &Desc)
// InitFromXML
//
// Loads from an XML element
{
const TStaticStringEntry<ETypes> *pEntry = TYPE_TABLE.GetAt(Desc.GetTag());
if (pEntry == NULL)
{
Ctx.sError = strPatternSubst(CONSTLIT("Unknown filter type: %s"), Desc.GetTag());
return ERR_FAIL;
}
m_iType = pEntry->Value;
switch (m_iType)
{
case filterBloom:
m_iData1 = Desc.GetAttributeIntegerBounded(BLOOM_POINT_ATTRIB, 1, 255, DEFAULT_BLOOM_POINT);
break;
case filterColorize:
m_iData1 = Desc.GetAttributeIntegerBounded(HUE_ATTRIB, 0, 360, 0);
m_iData2 = Desc.GetAttributeIntegerBounded(SATURATION_ATTRIB, 0, 100, 30);
m_iData3 = Desc.GetAttributeIntegerBounded(BRIGHTNESS_ATTRIB, -100, 100, 0);
break;
case filterHueSaturation:
m_iData1 = Desc.GetAttributeIntegerBounded(HUE_ATTRIB, -180, 180, 0);
m_iData2 = Desc.GetAttributeIntegerBounded(SATURATION_ATTRIB, -100, 100, 0);
m_iData3 = Desc.GetAttributeIntegerBounded(BRIGHTNESS_ATTRIB, -100, 100, 0);
break;
case filterLevels:
m_iData1 = Desc.GetAttributeIntegerBounded(BLACK_POINT_ATTRIB, 0, 253, 0);
m_iData2 = Desc.GetAttributeIntegerBounded(WHITE_POINT_ATTRIB, m_iData1 + 2, 255, 255);
m_rData1 = Desc.GetAttributeDoubleBounded(GAMMA_ATTRIB, 0.01, 9.99, 1.0);
break;
}
// Several filters use opacity, so we load it here.
DWORD dwOpacityLevel = Desc.GetAttributeIntegerBounded(OPACITY_ATTRIB, 0, 100, 100);
m_byOpacity = (BYTE)(dwOpacityLevel * 255 / 100);
return NOERROR;
}