-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDrawImpl.h
386 lines (304 loc) · 9.67 KB
/
DrawImpl.h
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
// DrawImpl.h
//
// Drawing implementation classes
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#pragma once
#include "NoiseImpl.h"
// Glow Ring Painter ----------------------------------------------------------
class CGlowingRingPainter
{
public:
CGlowingRingPainter (CG32bitImage &Dest, int iRadius, int iWidth, CG32bitPixel rgbColor);
CGlowingRingPainter (CG32bitImage &Dest, int iRadius, int iWidth, const TArray<CG32bitPixel> &ColorRamp, BYTE byOpacity = 0xff);
void Draw (int xCenter, int yCenter);
private:
void DrawLine (int x, int y);
CG32bitImage &m_Dest;
const RECT &m_rcClip;
int m_xDest;
int m_yDest;
int m_iRadius;
int m_iWidth;
const TArray<CG32bitPixel> *m_pColorRamp;
TArray<CG32bitPixel> m_ColorRamp;
};
// Blt Painters ---------------------------------------------------------------
class CFilterNormal : public TBlt<CFilterNormal>
{
public:
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const { return rgbSrc; }
};
class CFilterBackColor : public TBlt<CFilterBackColor>
{
public:
CFilterBackColor (CG32bitPixel rgbBackColor) : m_rgbBackColor(rgbBackColor)
{ }
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
if (rgbSrc.GetRed() <= 0x01 && rgbSrc.GetGreen() <= 0x01 && rgbSrc.GetBlue() <= 0x01)
return CG32bitPixel::Null();
else
return rgbSrc;
}
private:
CG32bitPixel m_rgbBackColor;
};
class CFilterTrans : public TBlt<CFilterTrans>
{
public:
CFilterTrans (BYTE byOpacity) : m_byOpacity(byOpacity)
{ }
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
BYTE byAlpha = rgbSrc.GetAlpha();
if (byAlpha == 0x00)
return rgbSrc;
else if (byAlpha == 0xff)
return CG32bitPixel(CG32bitPixel::Blend(0, rgbSrc, m_byOpacity), m_byOpacity);
else
return CG32bitPixel(CG32bitPixel::Blend(0, rgbSrc, m_byOpacity), CG32bitPixel::BlendAlpha(byAlpha, m_byOpacity));
}
private:
BYTE m_byOpacity;
};
class CFilterDesaturate : public TBlt<CFilterDesaturate>
{
public:
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
if (rgbSrc.GetAlpha() == 0x00)
return rgbSrc;
else
return CG32bitPixel::Desaturate(rgbSrc);
}
};
class CFilterDesaturateTrans : public TBlt<CFilterDesaturateTrans>
{
public:
CFilterDesaturateTrans (BYTE byOpacity) : m_byOpacity(byOpacity)
{ }
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
BYTE byAlpha = rgbSrc.GetAlpha();
if (byAlpha == 0x00)
return rgbSrc;
else if (byAlpha == 0xff)
return CG32bitPixel(CG32bitPixel::Blend(0, CG32bitPixel::Desaturate(rgbSrc), m_byOpacity), m_byOpacity);
else
{
CG32bitPixel rgbResult = CG32bitPixel::Blend(0, CG32bitPixel::Desaturate(rgbSrc), m_byOpacity);
return CG32bitPixel(rgbResult, CG32bitPixel::BlendAlpha(byAlpha, m_byOpacity));
}
}
private:
BYTE m_byOpacity;
};
class CFilterColorize : public TBlt<CFilterColorize>
{
public:
CFilterColorize (Metric rHue, Metric rSaturation) :
m_rHue(rHue),
m_rSaturation(rSaturation),
m_rgbBlack(CG32bitPixel(0, 0, 0)),
m_rgbWhite(CG32bitPixel(0xff, 0xff, 0xff)),
m_rgbHue(CGRealRGB::FromHSB(CGRealHSB(rHue, 1.0, 1.0))),
m_rgbColor(CG32bitPixel::Blend(CG32bitPixel(0x80, 0x80, 0x80), m_rgbHue, rSaturation))
{
}
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
// See: Computer Graphics, Foley & van Dam, p.593.
BYTE byBrightness = CG32bitPixel::Desaturate(rgbSrc).GetRed();
Metric rB = byBrightness / 255.0;
return CG32bitPixel::Blend3(m_rgbBlack, m_rgbColor, m_rgbWhite, 2.0 * (rB - 1.0) + 1.0);
}
private:
Metric m_rHue;
Metric m_rSaturation;
CG32bitPixel m_rgbBlack;
CG32bitPixel m_rgbWhite;
CG32bitPixel m_rgbHue;
CG32bitPixel m_rgbColor;
};
class CFilterLighten : public TBlt<CFilterLighten>
{
public:
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
BYTE bySrcAlpha = rgbSrc.GetAlpha();
BYTE bySrcRed, bySrcGreen, bySrcBlue;
if (bySrcAlpha == 0xff)
{
bySrcRed = rgbSrc.GetRed();
bySrcGreen = rgbSrc.GetGreen();
bySrcBlue = rgbSrc.GetBlue();
}
else
{
BYTE *pAlphaInv = CG32bitPixel::AlphaTable(bySrcAlpha ^ 0xff); // Equivalent to 255 - (*pAlphaPos)
bySrcRed = pAlphaInv[pDest->GetRed()] + rgbSrc.GetRed();
bySrcGreen = pAlphaInv[pDest->GetGreen()] + rgbSrc.GetGreen();
bySrcBlue = pAlphaInv[pDest->GetBlue()] + rgbSrc.GetBlue();
}
DWORD dwTotalSrc = bySrcRed + bySrcGreen + bySrcBlue;
DWORD dwTotalDest = pDest->GetRed() + pDest->GetGreen() + pDest->GetBlue();
if (dwTotalSrc > dwTotalDest)
return CG32bitPixel(bySrcRed, bySrcGreen, bySrcBlue);
else
return CG32bitPixel::Null();
}
};
class CFilterMask0 : public TBlt<CFilterMask0>
{
public:
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
if (Max(Max(rgbSrc.GetRed(), rgbSrc.GetGreen()), rgbSrc.GetBlue()) <= 7)
return CG32bitPixel::Null();
else
return rgbSrc;
}
};
class CFilterShimmer : public TBlt<CFilterShimmer>
{
public:
CFilterShimmer (BYTE byOpacity, DWORD dwSeed) :
m_byOpacity(byOpacity),
m_dwRnd(LARGE_PRIME2 * (1 + dwSeed))
{
NoiseInit();
}
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const
{
if (PERM(((DWORD)pDest >> 2) * LARGE_PRIME2 + m_dwRnd) < m_byOpacity)
return rgbSrc;
else
return CG32bitPixel::Null();
}
inline void StartRow (CG32bitPixel *pSrc, CG32bitPixel *pDest)
{
m_dwRnd *= LARGE_PRIME1;
}
private:
enum EConstants
{
LARGE_PRIME1 = 433494437,
LARGE_PRIME2 = 3010349,
};
BYTE m_byOpacity;
DWORD m_dwRnd;
};
template <class BLENDER> class TBltImageNormal : public TImagePainter<TBltImageNormal<BLENDER>, BLENDER>
{
private:
inline CG32bitPixel Filter (CG32bitPixel rgbSrc, CG32bitPixel *pDest) const { return rgbSrc; }
friend TImagePainter;
};
template <class BLENDER> class TFillImageSolid : public TImagePainter<TFillImageSolid<BLENDER>, BLENDER>
{
public:
TFillImageSolid (CG32bitPixel rgbColor) :
m_rgbColor(rgbColor)
{ }
private:
CG32bitPixel GetPixelAt (int x, int y) const { return m_rgbColor; }
CG32bitPixel m_rgbColor;
friend TImagePainter;
};
// Fill Painters --------------------------------------------------------------
class CSolidFill
{
public:
CSolidFill (CG32bitPixel rgbColor) :
m_rgbColor(rgbColor)
{ }
inline CG32bitPixel GetColor (void) const { return m_rgbColor; }
private:
CG32bitPixel m_rgbColor;
};
// Circle Painters ------------------------------------------------------------
template <class BLENDER> class CImageCirclePainter : public TCirclePainter32<CImageCirclePainter<BLENDER>, BLENDER>
{
public:
CImageCirclePainter (int iRadius, BYTE byOpacity, const CG32bitImage &Src, int xSrc = 0, int ySrc = 0, int cxSrc = -1, int cySrc = -1) : TCirclePainter32((cxSrc >= 0 ? cxSrc : Src.GetWidth()), iRadius),
m_byOpacity(byOpacity),
m_Src(Src),
m_xSrc(xSrc),
m_ySrc(ySrc),
m_cxSrc(cxSrc >= 0 ? cxSrc : Src.GetWidth()),
m_cySrc(cySrc >= 0 ? cySrc : Src.GetHeight())
{ }
inline CG32bitPixel GetColorAt (int iAngle, int iRadius) const
{
ASSERT(iAngle >= 0 && iAngle < m_iAngleRange);
ASSERT(iRadius >= 0 && iRadius <= m_iRadius);
int yOffset = m_iRadius - iRadius;
if (yOffset >= m_cySrc || m_byOpacity == 0x00)
return CG32bitPixel::Null();
CG32bitPixel rgbImage = *m_Src.GetPixelPos(m_xSrc + iAngle, m_ySrc + yOffset);
if (m_byOpacity == 0xff)
return rgbImage;
else
{
BYTE byImageAlpha = rgbImage.GetAlpha();
if (byImageAlpha == 0x00)
return CG32bitPixel::Null();
else if (byImageAlpha == 0xff)
return CG32bitPixel(CG32bitPixel::Blend(0, rgbImage, m_byOpacity), m_byOpacity);
else
return CG32bitPixel(CG32bitPixel::Blend(0, rgbImage, m_byOpacity), CG32bitPixel::BlendAlpha(byImageAlpha, m_byOpacity));
}
}
private:
BYTE m_byOpacity;
const CG32bitImage &m_Src;
int m_xSrc;
int m_ySrc;
int m_cxSrc;
int m_cySrc;
};
class CRadialCirclePainter8 : public TRadialPainter8<CRadialCirclePainter8>
{
public:
CRadialCirclePainter8 (int iRadius, BYTE CenterValue, BYTE EdgeValue);
inline BYTE GetColorAt (int iRadius) const { return m_Ramp[iRadius]; }
private:
TArray<BYTE> m_Ramp;
};
template <class BLENDER> class TFillCircleSolid : public TRadialPainter32<TFillCircleSolid<BLENDER>, BLENDER>
{
public:
TFillCircleSolid (int iRadius, CG32bitPixel rgbColor) : TRadialPainter32(iRadius),
m_rgbColor(CG32bitPixel::PreMult(rgbColor))
{ }
private:
CG32bitPixel GetColorAt (int iRadius) const { return m_rgbColor; }
CG32bitPixel m_rgbColor;
friend TRadialPainter32;
};
template <class BLENDER> class CRadialCirclePainter : public TRadialPainter32<CRadialCirclePainter<BLENDER>, BLENDER>
{
public:
CRadialCirclePainter (int iRadius, const TArray<CG32bitPixel> &ColorRamp, bool bPreMult = false) : TRadialPainter32(iRadius)
{
int i;
// If the ramp is not pre-multiplied, then we need to do that now
if (!bPreMult)
{
// Pre-multiply the color ramp
m_ColorRamp.InsertEmpty(ColorRamp.GetCount());
m_pColorRamp = &m_ColorRamp;
for (i = 0; i < m_ColorRamp.GetCount(); i++)
m_ColorRamp[i] = CG32bitPixel::PreMult(ColorRamp[i]);
}
// Otherwise we just take the ramp we've been given
else
m_pColorRamp = &ColorRamp;
}
private:
inline CG32bitPixel GetColorAt (int iRadius) const { return m_pColorRamp->GetAt(iRadius); }
const TArray<CG32bitPixel> *m_pColorRamp;
TArray<CG32bitPixel> m_ColorRamp;
friend TRadialPainter32;
};
extern const BYTE g_wSmallRoundMask[9];