-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitBlender.hpp
1317 lines (1147 loc) · 51.8 KB
/
BitBlender.hpp
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Refont of Paul Watt's BitBlender helper functions in a single file
// by Noël MARTINON
// NOTA : original namespace 'article' is renamed to 'bitblender'
/* BitBlender.h ***************************************************************
Author Paul Watt
Date: 7/21/2011
Purpose: Bit Blender is a collection of helper functions that simplifies the
use of GradientFill and AlphaBlend. These functions also extend
the capabilities of the original API to allow radial gradients,
arbitrary gradients at an angle, and alpha-blend support.
Copyright 2011 Paul Watt
******************************************************************************/
#ifndef BITBLENDER_H_INCLUDED
#define BITBLENDER_H_INCLUDED
/* Includes ******************************************************************/
#include <windows.h>
#include <algorithm>
#include <vector>
// targetver.h
#pragma once
// The following macros define the minimum required platform. The minimum required platform
// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
// your application. The macros work by enabling all features available on platform versions up to and
// including the version specified.
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Specifies that the minimum required platform is Windows Vista.
#define WINVER 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Specifies that the minimum required platform is Windows 98.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Specifies that the minimum required platform is Internet Explorer 7.0.
#define _WIN32_IE 0x0700 // Change this to the appropriate value to target other versions of IE.
#endif
// END targetver.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#include <algorithm>
using std::min;
using std::max;
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
/* Constants *****************************************************************/
/* Common Color Definitions **************************************************/
const COLORREF k_white = RGB(0xFF, 0xFF, 0xFF);
const COLORREF k_lightGray = RGB(0xED, 0xED, 0xED);
const COLORREF k_gray = RGB(0xC0, 0xC0, 0xC0);
const COLORREF k_darkGray = RGB(0x80, 0x80, 0x80);
const COLORREF k_black = RGB(0x00, 0x00, 0x00);
const COLORREF k_cpDarkOrange = RGB(0xFF, 0x99, 0x00);
const COLORREF k_cpLightOrange = RGB(0xFF, 0xE2, 0xA8);
const COLORREF k_cpDarkGreen = RGB(0x48, 0x8E, 0x00);
const COLORREF k_cpLightGreen = RGB(0x76, 0xAB, 0x40);
const COLORREF k_red2 = RGB(0xFF, 0x1A, 0x00);
const COLORREF k_red = RGB(0xFF, 0x00, 0x00);
const COLORREF k_green = RGB(0x00, 0xFF, 0x00);
const COLORREF k_blue = RGB(0x00, 0x00, 0xFF);
const COLORREF k_lightPurple = RGB(0x70, 0x70, 0xA0);
const COLORREF k_hilightPurple = RGB(0x80, 0x80, 0xC0);
const COLORREF k_redBase = RGB(0xFF, 0x40, 0x60);
const COLORREF k_deepRed = RGB(0x60, 0x00, 0x00);
const COLORREF k_greenBase = RGB(0x30, 0xC0, 0x40);
const COLORREF k_deepGreen = RGB(0x00, 0x60, 0x00);
const COLORREF k_blueBase = RGB(0x40, 0x80, 0xFF);
const COLORREF k_deepBlue = RGB(0x00, 0x00, 0x60);
const COLORREF k_blueGlass = RGB(0x40,0x50,0x70);
const COLORREF k_blueGlassEdge = RGB(0x30,0x40,0x55);
// Color Palette colors.
const COLORREF k_brightRed = RGB(237,28,36);
const COLORREF k_brightYellow = RGB(255,242,0);
const COLORREF k_yellowGreen = RGB(168,230,29);
const COLORREF k_brightGreen = RGB(34,217,76);
const COLORREF k_brightCyan = RGB(100,183,239);
const COLORREF k_indigo = RGB(47,54,153);
const COLORREF k_violet = RGB(111,49,152);
const COLORREF k_softRed = RGB(255,163,177);
const COLORREF k_softOrange = RGB(245,228,156);
const COLORREF k_softGreen = RGB(211,249,188);
const COLORREF k_periwinkle = RGB(112,154,209);
const COLORREF k_lavender = RGB(0xC0, 0xC0, 0xE0);
// END stdafx.h
/* AutoGDI.h ******************************************************************
Author: Paul Watt
Date: 7/21/2011 11:10:01 PM
Purpose: A few GDI helper objects and functions for use with C++ win32 GDI
development in the absence of MFC or WTL.
Copyright 2011 Paul Watt
*******************************************************************************/
#ifndef AutoObj_H_INCLUDED
#define AutoObj_H_INCLUDED
/* Includes ******************************************************************/
#include <windows.h>
namespace bitblender
{
/* Class Definitions *********************************************************/
// These objects are defined below to allow object cleanup on the stack.
// AutoBitmap Manages HBITMAP handles
// AutoBrush Manages HBRUSH handles
// AutoFont Manages HFONT handles
// AutoPen Manages HPEN handles
// AutoRgn Manages HRGN handles
// AutoPalette Manages HPALETTE handles
// AutoSaveDC Takes a snapshot of the current DC. Restores when leaves scope.
// MemDCBuffer Creates and manages a memory DC buffer for double buffer painting.
/* Utility Functions *********************************************************/
/******************************************************************************
Date: 8/14/2011
Purpose: Calculates the Height and Width of a rectangle.
Parameters: rc[in]: A RECT struct where left <= right and top <= bottom.
Return: Returns a SIZE struct with the calculated height and width.
*******************************************************************************/
inline
SIZE GetRectSize(const RECT& rc)
{
SIZE sz = {rc.right - rc.left, rc.bottom - rc.top};
return sz;
}
/******************************************************************************
Date: 8/14/2011
Purpose: Calculates the center point of a rectangle.
Parameters: rc[in]: A RECT struct where left <= right and top <= bottom.
sz[in]: Size has already been calculated for the rectangle.
Return: Returns the center point of the rectangle.
*******************************************************************************/
inline
POINT GetRectCenter(const RECT& rc, const SIZE& sz)
{
POINT pt = {rc.left + sz.cx/2, rc.top + sz.cy/2};
return pt;
}
/******************************************************************************
Date: 8/14/2011
Purpose: Calculates the center point of a rectangle.
Parameters: rc[in]: A RECT struct where left <= right and top <= bottom.
Return: Returns the center point of the rectangle.
*******************************************************************************/
inline
POINT GetRectCenter(const RECT& rc)
{
return GetRectCenter(rc, GetRectSize(rc));
}
/* GDI HANDLE Resource Management Object **************************************
Purpose: This class provides the template to cleanup any of the GDI objects
that should be freed with a call to ::DeleteObject.
This object is meant to be created on the stack. When the object
goes out of scope, it will automatically clean up the GDI resources.
Note: This object is only intended to be declared on the stack.
Do not declare this object with "static"
******************************************************************************/
template <typename T>
class AutoObj
{
public:
T handle;
/* Default Constructor *****************************************************/
// Use Attach to assign a handle to an empty Auto object.
AutoObj ()
: handle(NULL) { }
/* explicit Handle constructor *********************************************/
// Wouldn't it be a bitch for the compiler to convert one of your
// GDI Object Handles into one of these objects (secretly),
// and destroy it without telling you. That's why this is defined explicit.
explicit
AutoObj (T in)
: handle(in) { }
/* Destructor **************************************************************/
~AutoObj() { ::DeleteObject(handle);}
/* GDI Object Handle Conversion Operator ***********************************/
operator T() const { return handle;}
/* Public ******************************************************************
Purpose: Attach an unmanaged handle to this auto-object
for resource management.
Parameters: in[in]: The input handle to be managed.
Return: If the object is not currently managing a handle, the input
handle will become managed by this object and true is returned.
If false is returned, this object is already managing a handle.
***************************************************************************/
bool Attach(T in) { if (NULL == handle)
handle = in;
return handle == in;
}
/* Public ******************************************************************
Purpose: Detach a managed handle from this auto-object.
The caller then takes responsibility for resource management
of the GDI Object.
Return: The managed handle is returned.
If there is currently no handle, then NULL will be returned.
***************************************************************************/
T Detach() { T retVal = handle;
handle = NULL;
return retVal;
}
private:
// Make this object non-copyable by hiding these functions"
AutoObj(const AutoObj&); // Copy Constructor
AutoObj& operator=(const AutoObj&); // Assignment Operator
// Prohibit some other nonsensical functions.
bool Attach(T in) const;
T Detach() const;
};
/* DC Snapshot manager ********************************************************
Purpose: Takes a snapshot of the current DC state. When the object goes
out of scope, it will restore the context of the DC.
The restoration can be forced sooner by calling Restore.
Note: This object is only intended to be declared on the stack.
Do not declare this object with "static"
******************************************************************************/
class AutoSaveDC
{
public:
/* Construction ************************************************************/
explicit
AutoSaveDC(HDC hdc)
: m_hdc(hdc) { m_ctx = ::SaveDC(hdc);}
~AutoSaveDC() { Restore();}
/* Methods ******************************************************************/
void Restore() { if (0 != m_ctx)
{
::RestoreDC(m_hdc, m_ctx);
m_ctx = 0;
m_hdc = 0;
}
}
private:
/* Members *****************************************************************/
HDC m_hdc; // HDC to restore
int m_ctx; // Saved context
/* Methods *****************************************************************/
// No default constructor.
AutoSaveDC();
// Make this object non-copyable by hiding these functions"
AutoSaveDC(const AutoSaveDC&); // Copy Constructor
AutoSaveDC& operator=(const AutoSaveDC&); // Assignment Operator
};
/* Class **********************************************************************
Purpose: Implements a double-buffer from an input DC Handle.
A size should be specified to indicate the size of the backup buffer.
Construction:
However, if the size is omitted, and a Memory DC is passed in,
the constructor will attempt to create a duplicate buffer the
size of the buffer selected into the input Memory DC.
If the size is omitted, and the input DC is not a Memory DC, all
input directed to this object will simply write directly to the
original DC.
IsBuffered: Return true if this object maintains a double buffer, false otherwise.
Flush: Writes the data from the backup buffer into the specified buffer.
*****************************************************************************/
class MemDCBuffer
{
public:
/* Construction ************************************************************/
explicit
MemDCBuffer(HDC hdc)
: m_buffer(BufferInit_(hdc)) { }
MemDCBuffer(HDC hdc, int cx, int cy)
: m_buffer(BufferInit_(hdc, cx, cy)) { }
~MemDCBuffer() { if (IsBuffered())
::DeleteDC(m_memDC);
}
/* Operators ***************************************************************/
operator HDC() { return m_memDC;}
/* Status ******************************************************************/
bool IsBuffered() const { return NULL != (HBITMAP)m_buffer
&& m_isSelected;
}
/* Methods *****************************************************************/
void Flush(HDC hdc) { Flush(hdc, 0, 0, m_bm.bmWidth, m_bm.bmHeight);}
void Flush(HDC hdc, const RECT& rc) { Flush(hdc,
rc.left, rc.top,
rc.right - rc.left, rc.bottom - rc.top);
}
void Flush( HDC hdc,
int x, int y,
int cx, int cy) { ::BitBlt( hdc, x, y, cx, cy,
m_memDC, 0, 0, SRCCOPY);
}
HBITMAP BorrowImage() { ::SelectObject(m_memDC, m_hOldBmp);
m_isSelected = false;
return m_buffer;
}
void ReturnImage() { ::SelectObject(m_memDC, m_buffer);
m_isSelected = true;
}
private:
/* Members *****************************************************************/
HDC m_memDC;
AutoObj<HBITMAP> m_buffer;
BITMAP m_bm;
HBITMAP m_hOldBmp;
bool m_isSelected;
/* Methods *****************************************************************/
HBITMAP BufferInit_(HDC hdc, int cx=0, int cy=0)
{
if ( 0 == cx
|| 0 == cy)
{
AutoObj<HBITMAP> tempBmp(::CreateCompatibleBitmap(hdc, 1, 1));
// Attempt to create a duplicate buffer assuming
// the specified hdc is a memory DC.
m_hOldBmp = (HBITMAP)::SelectObject(hdc, tempBmp);
if (NULL == m_hOldBmp)
{
// This is not a memory DC.
// Double buffering will not be performed.
m_memDC = hdc;
m_isSelected = false;
return NULL;
}
// Get the dimensions of the existing bitmap.
::GetObject(m_hOldBmp, sizeof(BITMAP), &m_bm);
cx = m_bm.bmWidth;
cy = m_bm.bmHeight;
}
m_memDC = ::CreateCompatibleDC(hdc);
HBITMAP hBitmapBuffer = ::CreateCompatibleBitmap(hdc, cx, cy);
::GetObject(hBitmapBuffer, sizeof(BITMAP), &m_bm);
m_hOldBmp = (HBITMAP)::SelectObject(m_memDC, hBitmapBuffer);
m_isSelected = true;
return hBitmapBuffer;
}
/* Prohibited Access functions *********************************************/
MemDCBuffer();
MemDCBuffer(const MemDCBuffer&);
MemDCBuffer& operator=(const MemDCBuffer&);
};
/******************************************************************************
Date: 8/27/2011
Purpose: Makes a copy of the specified bitmap which will be compatible with
the supplied DC.
Parameters: hdc[in]: DC to make the new bitmap compatible with.
hBmp[in]: BITMAP handle, must not be selected into any memDC.
Return: A new bitmap handle that contains a copy of hBmp.
The caller is responsible to call ::DeleteObject on this handle.
*******************************************************************************/
inline
HBITMAP CopyBitmap(HDC hdc, HBITMAP hBmp)
{
HDC memInDC = ::CreateCompatibleDC(hdc);
HDC memOutDC = ::CreateCompatibleDC(hdc);
BITMAP bm;
::GetObject(hBmp, sizeof(BITMAP), &bm);
HBITMAP hOutputBmp = ::CreateCompatibleBitmap(hdc, bm.bmWidth, bm.bmHeight);
::SelectObject(memInDC, hBmp);
::SelectObject(memOutDC, hOutputBmp);
::BitBlt(memOutDC, 0, 0, bm.bmWidth, bm.bmHeight,
memInDC, 0, 0, SRCCOPY);
::DeleteDC(memInDC);
::DeleteDC(memOutDC);
return hOutputBmp;
}
/* Typedefs ******************************************************************/
typedef AutoObj<HBITMAP> AutoBitmap;
typedef AutoObj<HBRUSH> AutoBrush;
typedef AutoObj<HFONT> AutoFont;
typedef AutoObj<HPEN> AutoPen;
typedef AutoObj<HRGN> AutoRgn;
typedef AutoObj<HPALETTE> AutoPalette;
} // namespace bitblender
#endif
// END AutoGDI.h
namespace bitblender
{
/* Constants *****************************************************************/
/* Constants *****************************************************************/
const double k_pi = 3.1415926535897932384626433832795;
const double k_pi_2 = 0.5 * k_pi;
const double k_pi_4 = 0.25* k_pi;
const double k_3pi_2 = 1.5 * k_pi;
const double k_2pi = 2.0 * k_pi;
const double k_degToRad = k_pi / 180.0;
const double k_RadToDeg = 180.0 / k_pi;
// These constants are common levels of translucency.
const BYTE k_opaque = 0xFF;
const BYTE k_transparent = 0x00;
const BYTE k_alpha100 = k_opaque; //100%, 0xFF == 255
const BYTE k_alpha80 = 0xCC; // 80%, 0xCC == 204
const BYTE k_alpha75 = 0xC0; // 75%, 0xC0 == 192
const BYTE k_alpha60 = 0x99; // 60%, 0x99 == 153
const BYTE k_alpha50 = 0x80; // 50%, 0x80 == 128
const BYTE k_alpha40 = 0x66; // 40%, 0x66 == 102
const BYTE k_alpha25 = 0x40; // 25%, 0x40 == 64
const BYTE k_alpha20 = 0x33; // 20%, 0x33 == 51
const BYTE k_alpha0 = k_transparent; // 0%, 0x00 == 0
/* Utilities *****************************************************************/
inline COLOR16 ToColor16(BYTE byte) { return byte << 8;}
inline COLOR16 RVal16(COLORREF color) { return ToColor16(GetRValue(color));}
inline COLOR16 GVal16(COLORREF color) { return ToColor16(GetGValue(color));}
inline COLOR16 BVal16(COLORREF color) { return ToColor16(GetBValue(color));}
/* Functions *****************************************************************/
bool RectGradient(HDC,const RECT&,COLORREF,COLORREF,BOOL,BYTE,BYTE);
bool RectGradient(HDC,const RECT&,COLORREF,COLORREF,BOOL);
bool RadialGradient(HDC,int,int,int,COLORREF,COLORREF,size_t,BYTE,BYTE);
bool RadialGradient(HDC,int,int,int,COLORREF,COLORREF,size_t);
bool AngularGradient(HDC,const RECT&,double,COLORREF,COLORREF,BYTE,BYTE);
bool AngularGradient(HDC,const RECT&,double,COLORREF,COLORREF);
bool CombineAlphaChannel(HDC, HBITMAP, HBITMAP);
void GetColorDiff(COLORREF, COLORREF, int&, int&, int&);
BLENDFUNCTION GetBlendFn(BYTE, bool);
/* Bit Manipulation Functions ************************************************/
bool PreBlendAlphaBitmap(HDC, HBITMAP);
bool ShiftColorChannelsLeft (HDC,HBITMAP);
bool ShiftColorChannelsRight(HDC,HBITMAP);
bool InvertBitmap(HDC, HBITMAP);
bool ColorToGrayscale(HDC, HBITMAP);
bool GrayscaleToColor(HDC, HBITMAP, COLORREF);
/******************************************************************************
Date: 8/26/2011
Purpose: Template that will extract the pixels from a bitmap into a DIB, and
call a specified function on each bit.
The bitmap manipulated with this function must be 32-bit color depth.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to shift the color channels for.
fnT[in]: Functor that will modify a 32-bit number however is desired.
*******************************************************************************/
template <typename fnT>
bool ManipulateDIBits(HDC hdc, HBITMAP hBmp, fnT fn)
{
// Attempt to extract the BITMAP from the current DC.
BITMAP bm;
::GetObject(hBmp, sizeof(BITMAP), &bm);
// zero the memory for the bitmap info
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
// setup bitmap info
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = bm.bmPlanes;
bmi.bmiHeader.biBitCount = bm.bmBitsPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
std::vector<UINT32> bits;
bits.resize(bm.bmWidth * bm.bmHeight, 0x0);
if (!::GetDIBits(hdc, hBmp, 0, bm.bmHeight, (void**)&bits[0], &bmi, DIB_RGB_COLORS))
return false;
// Perform the manipulation.
std::for_each(bits.begin(), bits.end(), fn);
// Transfer the data back to the input device.
return 0
!= ::SetDIBits(hdc, hBmp, 0, bm.bmHeight, (void**)&bits[0], &bmi, DIB_RGB_COLORS);
}
/* Inline Function Implementations *******************************************/
/******************************************************************************
Date: 7/21/2011
Purpose: Helper function to simplify defining and drawing a gradient fill
in a rectangular area. This function will create all of the
internal structures for each call to GradientFill.
If the same gradient definition will be used repeatedly, !!! will
provide better performance.
Parameters: hDC[in]: The device context to write to.
rect[in]: The rectangle coordinates to fill with the gradient.
c1[in]: The color to use at the start of the gradient.
c2[in]: The color to use at the end of the gradient.
isVertical[in]: Indicates if the gradient fill should transition
vertically in the rectangle. If this value is false, horizontal
will be used.
All fills will be defined from left to right, or top to bottom.
Return: true If the function succeeds
false If an error occurs and the function fails.
*******************************************************************************/
inline
bool RectGradient(HDC hDC,
const RECT &rc,
COLORREF c1,
COLORREF c2,
BOOL isVertical)
{
return RectGradient(hDC, rc, c1, c2, isVertical, k_opaque, k_opaque);
}
/******************************************************************************
Date: 7/22/2011
Purpose: Creates a radial gradient.
The gradient is approximated by breaking up the
circle into triangles, and using the Win32 GradientFill call
to fill all of the individual triangles.
Parameters: hDC[in]: The device context to write to.
x[in]: The x coordinate of the center of the gradient.
y[in]: The y coordinate of the center of the gradient.
r[in]: The radius of the gradient fill.
c1[in]: The color to use at the start of the gradient.
c2[in]: The color to use at the end of the gradient.
segments[in]: The number of segments to break the circle into.
The default number of segments is 16.
3 is the absolute minimum, which will result in a triangle.
Return: true If the function succeeds
false If an error occurs and the function fails.
*******************************************************************************/
inline
bool RadialGradient(HDC hdc,
int x,
int y,
int r,
COLORREF c1,
COLORREF c2,
size_t segments
)
{
return RadialGradient(hdc, x, y, r, c1, c2, segments, k_opaque, k_opaque);
}
/******************************************************************************
Date: 7/25/2011
Purpose: Creates a linear gradient fill directed along an arbitrary angle.
Parameters: hDC[in]: The device context to write to.
rect[in]: The rectangle coordinates to fill with the gradient.
angle[in]: The angle in radians
c1[in]: The color to use at the start of the gradient.
c2[in]: The color to use at the end of the gradient.
Return: true If the function succeeds
false If an error occurs and the function fails.
*******************************************************************************/
inline
bool AngularGradient(
HDC hdc,
const RECT &rc,
double angle,
COLORREF c1,
COLORREF c2
)
{
return AngularGradient(hdc, rc, angle, c1, c2, k_opaque, k_opaque);
}
/******************************************************************************
Date: 7/25/2011
Purpose: Calculates the difference in color between the two colors.
The difference is calculated individually for each color channel.
Parameters: c1[in]: Start color
c2[in]: End color
Return: The difference in each color is returned.
*******************************************************************************/
inline
void GetColorDiff(COLORREF c1, COLORREF c2, int &red, int &green, int &blue)
{
// Not sure what negative colors will do yet.
// Keep a higher level of accuracy until the results are known.
red = GetRValue(c2) - GetRValue(c1);
green = GetGValue(c2) - GetGValue(c1);
blue = GetBValue(c2) - GetBValue(c1);
}
/******************************************************************************
Date: 8/7/2011
Purpose: Helper function to easily initialize a blend function for alpha blends.
Parameters: globalAlpha[in]: Indicates a global alpha constant, 0-255.
hasSrcAlpha[in]: True indicates the source image has alpha info,
this will set the AC_SRC_ALPHA flag.
Otherwise false will clear this flag.
Return: An initialized BLENDFUNCTION object is returned for use in GdiAlphaBlend.
*******************************************************************************/
inline
BLENDFUNCTION GetBlendFn(BYTE globalAlpha, bool hasSrcAlpha)
{
BLENDFUNCTION bf = {AC_SRC_OVER, 0, globalAlpha, (BYTE)(hasSrcAlpha ? AC_SRC_ALPHA : 0)};
return bf;
}
/* Functors ******************************************************************/
/* Class **********************************************************************
Purpose: Functor to multiply individual channels with a specified alpha value.
*******************************************************************************/
struct MultiplyAlpha
{
void operator()(UINT32 &elt) const
{
UINT32 val = elt;
BYTE alpha = (BYTE)(val >> 24);
double factor = alpha / 255.0;
elt = ((alpha) << 24)
| (BYTE(GetRValue(elt) * factor) )
| (BYTE(GetGValue(elt) * factor) << 8)
| (BYTE(GetBValue(elt) * factor) << 16);
}
};
/* Class **********************************************************************
Purpose: Functor to shift all color channels left.
*******************************************************************************/
struct ShiftLeft
{
void operator()(UINT32 &elt) const
{
elt <<= 8;
}
};
/* Class **********************************************************************
Purpose: Functor to shift all color channels right.
*******************************************************************************/
struct ShiftRight
{
void operator()(UINT32 &elt) const
{
elt >>= 8;
}
};
/* Class **********************************************************************
Purpose: Functor to invert the bits for the current pixel.
*******************************************************************************/
struct Invert
{
void operator()(UINT32 &elt) const
{
elt = ~elt;
}
};
/* Class **********************************************************************
Purpose: Functor to convert a color image into grayscale.
This grayscale conversion is based on relative color intensity to
create a more natural looking image compared to the original.
Intensity = (Red * 0.299)
+ (Green * 0.587)
+ (Blue * 0.114)
*******************************************************************************/
struct ToGrayscale
{
void operator()(UINT32 &elt) const
{
// Leave the alpha channel unchanged.
BYTE intensity = BYTE( (GetRValue(elt) * 0.299)
+ (GetGValue(elt) * 0.587)
+ (GetBValue(elt) * 0.114) * 255);
elt = (elt & 0xFF000000)
| intensity << 16
| intensity << 8
| intensity;
}
};
/* Class **********************************************************************
Purpose: Functor to convert a grayscale image to an image with 256 levels of
a particular color.
*******************************************************************************/
struct Colorize
{
Colorize(COLORREF color)
{
rVal = GetRValue(color);
gVal = GetGValue(color);
bVal = GetBValue(color);
}
void operator()(UINT32 &elt) const
{
// Leave the alpha channel unchanged.
// Get the intensity level for only one of the channels.
// If a proper grayscale image were passed in, R = G = B.
float intensity = GetRValue(elt) / 255.0f;
elt = (elt & 0xFF000000)
| ((BYTE(intensity * rVal)) << 16)
| ((BYTE(intensity * gVal)) << 8)
| (BYTE(intensity * bVal));
//elt = (elt & 0x000000FF)
// | ((BYTE(intensity * rVal)) << 16)
// | ((BYTE(intensity * gVal)) << 8)
// | (BYTE(intensity * bVal));
}
BYTE rVal;
BYTE gVal;
BYTE bVal;
};
/******************************************************************************
Date: 8/5/2011
Purpose: Pre-multiplies the alpha channel for each pixel in the bitmap.
Parameters: hdc[in]: A DC to the device the bitmap is compatible with.
hBmp[in]: Handle to the BITMAP to have its alpha channel pre-blended.
This handle cannot be selected into a DC when it is passed into
this function.
Return: true The function was successful, the pre-blend completed.
false The function failed, the bitmap is unchanged.
*******************************************************************************/
inline
bool PreBlendAlphaBitmap(HDC hdc, HBITMAP hBmp)
{
return ManipulateDIBits(hdc, hBmp, MultiplyAlpha());
}
/******************************************************************************
Date: 8/26/2011
Purpose: Shifts all of the color channels to the left one channel.
The image passed in must be a 32-bit color bitmap.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to shift the color channels for.
Return: true The function was successful, the channels shifted left.
false The function failed, the bitmap is unchanged.
*******************************************************************************/
inline
bool ShiftColorChannelsLeft(HDC hdc,HBITMAP hBmp)
{
return ManipulateDIBits(hdc, hBmp, ShiftLeft());
}
/******************************************************************************
Date: 8/26/2011
Purpose: Shifts all of the color channels to the right one channel.
The image passed in must be a 32-bit color bitmap.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to shift the color channels for.
*******************************************************************************/
inline
bool ShiftColorChannelsRight(HDC hdc,HBITMAP hBmp)
{
return ManipulateDIBits(hdc, hBmp, ShiftRight());
}
/******************************************************************************
Date: 8/26/2011
Purpose: Inverts all of the colors of an image. Every bit is flipped.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to shift the color channels for.
*******************************************************************************/
inline
bool InvertBitmap(HDC hdc, HBITMAP hBmp)
{
// The functor exists to perform the operation this way.
// return ManipulateDIBits(hdc, hBmp, Invert());
// However, this ROP3 method in BitBlt is implemented in hardware:
BITMAP bm;
::GetObject(hBmp, sizeof(BITMAP), &bm);
HDC memDC = ::CreateCompatibleDC(hdc);
::SelectObject(memDC, hBmp);
::BitBlt(memDC, 0, 0, bm.bmWidth, bm.bmHeight,
0, 0, 0, DSTINVERT);
::DeleteDC(memDC);
return true;
}
/******************************************************************************
Date: 8/26/2011
Purpose: Converts the input color image to a grayscale image calculated
from color intensity.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to convert to grayscale.
*******************************************************************************/
inline
bool ColorToGrayscale(HDC hdc,HBITMAP hBmp)
{
return ManipulateDIBits(hdc, hBmp, ToGrayscale());
}
/******************************************************************************
Date: 8/27/2011
Purpose: Converts a grayscale image into an image with color.
This function is not symmetric with the ColorToGrayscale function.
It does not add color based on an intensity calculation, rather it
applies the specified color scaled to the current gray pixel.
The image will still appear monochromatic, except with a shade of color.
Parameters: hdc[in]: DC to be compatible with.
hBmp[in]: Bitmap to convert to grayscale.
color[in]: The color to convert the image to.
*******************************************************************************/
inline
bool GrayscaleToColor(HDC hdc, HBITMAP hBmp, COLORREF color)
{
Colorize colorize(color);
return ManipulateDIBits(hdc, hBmp, colorize);
}
} // namespace bitblender
/* BitBlender.cpp *************************************************************
Author: Paul Watt
Date: 7/21/2011 10:35:29 PM
Purpose:
Copyright 2011 Paul Watt
*******************************************************************************/
#include <math.h>
/* Forward Declarations ******************************************************/
namespace // anonymous
{
/* Forward Declarations ******************************************************/
bool AngularGradient_(HDC,
const RECT&,
double,
COLORREF,
COLORREF,
BYTE,
BYTE
);
bool SegmentedRadialGradient_(HDC hdc,
const POINT& ctr,
int radius,
double startAngle,
COLORREF c1,
COLORREF c2,
size_t segments,
BYTE alpha1,
BYTE alpha2
);
} // namespace anonymous
namespace bitblender
{
/******************************************************************************
Date: 7/21/2011
Purpose: Helper function to simplify defining and drawing a gradient fill
in a rectangular area. This function will create all of the
internal structures for each call to GradientFill.
If the same gradient definition will be used repeatedly, !!! will
provide better performance.
Parameters: hDC[in]: The device context to write to.
rect[in]: The rectangle coordinates to fill with the gradient.
c1[in]: The color to use at the start of the gradient.
c2[in]: The color to use at the end of the gradient.
isVertical[in]: Indicates if the gradient fill should transition
vertically in the rectangle. If this value is false, horizontal
will be used.
All fills will be defined from left to right, or top to bottom.
alpha1[in]: Starting alpha level to associate with the gradient.
alpha2[in]: Ending alpha level to associate with the gradient.
Return: true If the function succeeds
false If an error occurs and the function fails.
*******************************************************************************/
bool RectGradient(
HDC hDC,
const RECT &rc,
COLORREF c1,
COLORREF c2,
BOOL isVertical,
BYTE alpha1,
BYTE alpha2
)
{
TRIVERTEX v[2] =
{
{rc.left, rc.top, RVal16(c1), GVal16(c1), BVal16(c1), ToColor16(alpha1)},
{rc.right, rc.bottom, RVal16(c2), GVal16(c2), BVal16(c2), ToColor16(alpha2)}
};
GRADIENT_RECT topGradient;
topGradient.UpperLeft = 0;
topGradient.LowerRight= 1;
BOOL result = ::GdiGradientFill(hDC,
v,
2,
&topGradient,
1,
isVertical
? GRADIENT_FILL_RECT_V
: GRADIENT_FILL_RECT_H);
return FALSE != result;
}
/******************************************************************************
Date: 7/22/2011
Purpose: Creates a radial gradient.
The gradient is approximated by breaking up the
circle into triangles, and using the Win32 GradientFill call
to fill all of the individual triangles.
Parameters: hDC[in]: The device context to write to.
x[in]: The x coordinate of the center of the gradient.
y[in]: The y coordinate of the center of the gradient.
r[in]: The radius of the gradient fill.
c1[in]: The color to use at the start of the gradient.
c2[in]: The color to use at the end of the gradient.
segments[in]: The number of segments to break the circle into.
The default number of segments is 16.
3 is the absolute minimum, which will result in a triangle.
alpha1[in]: Starting alpha level to associate with the gradient.
alpha2[in]: Ending alpha level to associate with the gradient.
Return: true If the function succeeds
false If an error occurs and the function fails.
*******************************************************************************/
bool RadialGradient(
HDC hdc,
int x,
int y,
int r,
COLORREF c1,
COLORREF c2,
size_t segments,
BYTE alpha1,
BYTE alpha2
)
{
POINT pt = {x,y};
return SegmentedRadialGradient_(hdc, pt, r, 0.0, c1, c2, segments, alpha1, alpha2);