-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDIBSectionLite.cpp
More file actions
1720 lines (1485 loc) · 52.2 KB
/
Copy pathDIBSectionLite.cpp
File metadata and controls
1720 lines (1485 loc) · 52.2 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
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
// CDIBSectionLite.cpp : implementation file
//
// General purpose DIBsection wrapper class for Win9x, NT 4.0, W2K and WinCE.
//
// Author : Chris Maunder (cmaunder@mail.com)
// Date : 17 May 1999
//
// Copyright © Dundas Software Ltd. 1999, All Rights Reserved
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage, in any form, caused
// by this code. Use it at your own risk and as with all code expect bugs!
// It's been tested but I'm not perfect.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
// History : 25 May 1999 - First release
// 4 Jun 1999 - Fixed SetBitmap bug
// 4 May 2000 - 16 or 32 bit compression bug fix (Jim Miller <jam@3dfx.com>)
// 20 Sep 2000 - Bug fix in Save() (saving 4 bytes too many - Tadeusz Dracz)
// Allowed lpBits to be NULL in SetBitmap - Don Grasberger
// Fixed NumColorEntries bug - Itay Szekely
// Fixed buffer overrun bug in Load() - Itay Szekely
// 5 Apr 2000 - Fix for 16/32 bpp bitmaps for PocketPC's (Dan Robbins - DCR)
#include "stdafx.h"
#include "DIBSectionLite.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Use C++ exception handling instead of structured on non-CE platforms.
#ifndef _WIN32_WCE
#undef TRY
#undef CATCH
#undef END_CATCH
#define TRY try
#define CATCH(ex_class, ex_object) catch(ex_class* ex_object)
#define END_CATCH
#endif // _WIN32_WCE
// Spit out some messages as a sanity check for programmers
#ifdef DIBSECTION_NO_DITHER
#pragma message(" - CDIBSectionLite: No dithering")
#endif
#ifdef DIBSECTION_NO_MEMDC_REUSE
#pragma message(" - CDIBSectionLite: No memory DC reuse")
#endif
#ifdef DIBSECTION_NO_PALETTE
#pragma message(" - CDIBSectionLite: No palette support")
#endif
// Standard colors
RGBQUAD CDIBSectionLite::ms_StdColors[] = {
{ 0x00, 0x00, 0x00, 0 }, // System palette - first 10 colors
{ 0x80, 0x00, 0x00, 0 },
{ 0x00, 0x80, 0x00, 0 },
{ 0x80, 0x80, 0x00, 0 },
{ 0x00, 0x00, 0x80, 0 },
{ 0x80, 0x00, 0x80, 0 },
{ 0x00, 0x80, 0x80, 0 },
{ 0xC0, 0xC0, 0xC0, 0 },
{ 0xC0, 0xDC, 0xC0, 0 },
{ 0xA6, 0xCA, 0xF0, 0 },
{ 0x2C, 0x00, 0x00, 0 },
{ 0x56, 0x00, 0x00, 0 },
{ 0x87, 0x00, 0x00, 0 },
{ 0xC0, 0x00, 0x00, 0 },
{ 0xFF, 0x00, 0x00, 0 },
{ 0x00, 0x2C, 0x00, 0 },
{ 0x2C, 0x2C, 0x00, 0 },
{ 0x56, 0x2C, 0x00, 0 },
{ 0x87, 0x2C, 0x00, 0 },
{ 0xC0, 0x2C, 0x00, 0 },
{ 0xFF, 0x2C, 0x00, 0 },
{ 0x00, 0x56, 0x00, 0 },
{ 0x2C, 0x56, 0x00, 0 },
{ 0x56, 0x56, 0x00, 0 },
{ 0x87, 0x56, 0x00, 0 },
{ 0xC0, 0x56, 0x00, 0 },
{ 0xFF, 0x56, 0x00, 0 },
{ 0x00, 0x87, 0x00, 0 },
{ 0x2C, 0x87, 0x00, 0 },
{ 0x56, 0x87, 0x00, 0 },
{ 0x87, 0x87, 0x00, 0 },
{ 0xC0, 0x87, 0x00, 0 },
{ 0xFF, 0x87, 0x00, 0 },
{ 0x00, 0xC0, 0x00, 0 },
{ 0x2C, 0xC0, 0x00, 0 },
{ 0x56, 0xC0, 0x00, 0 },
{ 0x87, 0xC0, 0x00, 0 },
{ 0xC0, 0xC0, 0x00, 0 },
{ 0xFF, 0xC0, 0x00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 0xFF, 0xFF, 0xFF, 0 },
};
/////////////////////////////////////////////////////////////////////////////
// CE DIBSection global functions
#ifdef _WIN32_WCE
UINT CEGetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries, RGBQUAD *pColors);
#endif
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite static functions
//
// --- In : nBitsPerPixel - bits per pixel
// nCompression - type of compression
// --- Out :
// --- Returns :The number of colors for this color depth
// --- Effect : Returns the number of color table entries given the number
// of bits per pixel of a bitmap
/*static*/ int CDIBSectionLite::NumColorEntries(int nBitsPerPixel, int nCompression, DWORD biClrUsed /*= 0*/)
{
int nColors = 0;
switch (nBitsPerPixel)
{
case 1:
nColors = 2;
break;
#ifdef _WIN32_WCE
case 2:
nColors = 4;
break; // winCE only
#endif
case 4:
nColors = 16;
break;
case 8:
nColors = 256;
break;
case 24:
nColors = 0;
case 16:
case 32:
#ifdef _WIN32_WCE
nColors = 3; // I've found that PocketPCs need this regardless of BI_RGB or BI_BITFIELDS
#else
if (nCompression == BI_BITFIELDS)
nColors = 3; // 16 or 32 bpp have 3 colors(masks) in the color table if bitfield compression
else
nColors = 0; // 16 or 32 bpp have no color table if no bitfield compression
#endif
break;
default:
ASSERT(FALSE);
}
// If biClrUsed is provided, and it is a legal value, use it
if (biClrUsed > 0 && biClrUsed <= (DWORD)nColors)
return biClrUsed;
return nColors;
}
//
// --- In : nWidth - image width in pixels
// nBitsPerPixel - bits per pixel
// --- Out :
// --- Returns : Returns the number of storage bytes needed for each scanline
// in the bitmap
// --- Effect :
/*static*/ int CDIBSectionLite::BytesPerLine(int nWidth, int nBitsPerPixel)
{
return ( (nWidth * nBitsPerPixel + 31) & (~31) ) / 8;
}
#ifndef DIBSECTION_NO_PALETTE
// --- In : palette - reference to a palette object which will be filled
// nNumColors - number of color entries to fill
// --- Out :
// --- Returns : TRUE on success, false otherwise
// --- Effect : Creates a halftone color palette independant of screen color depth.
// palette will be filled with the colors, and nNumColors is the No.
// of colors to file. If nNumColorsis 0 or > 256, then 256 colors are used.
/*static*/ BOOL CDIBSectionLite::CreateHalftonePalette(CPalette& palette, int nNumColors)
{
palette.DeleteObject();
// Sanity check on requested number of colours.
if (nNumColors <= 0 || nNumColors > 256)
nNumColors = 256;
else if (nNumColors <= 2)
nNumColors = 2;
else if (nNumColors <= 16)
nNumColors = 16;
else if (nNumColors <= 256)
nNumColors = 256;
PALETTEINFO pi;
pi.palNumEntries = (WORD) nNumColors;
if (nNumColors == 2)
{
// According to the MS article "The Palette Manager: How and Why"
// monochrome palettes not really needed (will use B&W)
pi.palPalEntry[0].peRed = ms_StdColors[0].rgbRed;
pi.palPalEntry[0].peGreen = ms_StdColors[0].rgbGreen;
pi.palPalEntry[0].peBlue = ms_StdColors[0].rgbBlue;
pi.palPalEntry[0].peFlags = 0;
pi.palPalEntry[1].peRed = ms_StdColors[255].rgbRed;
pi.palPalEntry[1].peGreen = ms_StdColors[255].rgbGreen;
pi.palPalEntry[1].peBlue = ms_StdColors[255].rgbBlue;
pi.palPalEntry[1].peFlags = 0;
}
else if (nNumColors == 16)
{
// According to the MS article "The Palette Manager: How and Why"
// 4-bit palettes not really needed (will use VGA palette)
for (int i = 0; i < 8; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
for (i = 8; i < 16; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[248+i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[248+i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[248+i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
}
else // if (nNumColors == 256)
{
// Fill palette with full halftone palette
for (int i = 0; i < 256; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
}
return palette.CreatePalette((LPLOGPALETTE) pi);
}
#endif // DIBSECTION_NO_PALETTE
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite
CDIBSectionLite::CDIBSectionLite()
{
// Just in case...
ASSERT(sizeof(ms_StdColors) / sizeof(ms_StdColors[0]) == 256);
m_hBitmap = NULL;
m_hOldBitmap = NULL;
#ifndef DIBSECTION_NO_MEMDC_REUSE
m_bReuseMemDC = FALSE;
#endif
#ifndef DIBSECTION_NO_PALETTE
m_pOldPalette = NULL;
#endif
#ifndef DIBSECTION_NO_DITHER
m_bDither = FALSE;
m_hDrawDib = NULL;
#endif
DeleteObject(); // This will initialise to a known state - ie. empty
}
CDIBSectionLite::~CDIBSectionLite()
{
DeleteObject();
}
// --- In :
// --- Out :
// --- Returns :
// --- Effect : Resets the object to an empty state, and frees all memory used.
void CDIBSectionLite::DeleteObject()
{
// Unselect the bitmap out of the memory DC before deleting bitmap
ReleaseMemoryDC(TRUE);
if (m_hBitmap)
::DeleteObject(m_hBitmap);
m_hBitmap = NULL;
m_ppvBits = NULL;
#ifndef DIBSECTION_NO_PALETTE
m_Palette.DeleteObject();
#endif
memset(&m_DIBinfo, 0, sizeof(m_DIBinfo));
#ifndef DIBSECTION_NO_DITHER
if (m_hDrawDib)
DrawDibClose(m_hDrawDib);
m_hDrawDib = NULL;
#endif
m_iColorDataType = DIB_RGB_COLORS;
m_iColorTableSize = 0;
}
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite diagnostics
#ifdef _DEBUG
void CDIBSectionLite::AssertValid() const
{
ASSERT(m_hBitmap);
DIBSECTION ds;
DWORD dwSize = GetObject( m_hBitmap, sizeof(DIBSECTION), &ds );
ASSERT(dwSize == sizeof(DIBSECTION));
ASSERT(0 <= m_iColorTableSize && m_iColorTableSize <= 256);
CObject::AssertValid();
}
void CDIBSectionLite::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite operations
#ifndef DIBSECTION_NO_DITHER
// --- In : bDither - whether or not dithering should be enabled
// --- Out :
// --- Returns : TRUE on success
// --- Effect : Turns dithering on by using the DrawDib functions instead of
// the GDI functions
BOOL CDIBSectionLite::SetDither(BOOL bDither)
{
if ( (m_bDither == bDither) &&
((m_hDrawDib && m_bDither) || (!m_hDrawDib && !m_bDither)) )
return TRUE;
if (!bDither && m_hDrawDib != NULL)
{
DrawDibClose(m_hDrawDib);
m_hDrawDib = NULL;
}
m_bDither = bDither;
return TRUE;
}
// --- In :
// --- Out :
// --- Returns : TRUE if dithering is used
// --- Effect : Returns whether or not the DrawDib functions (and hence dithering)
// is being used.
BOOL CDIBSectionLite::GetDither()
{
return (m_bDither && GetBitCount() > 8);
}
// --- In :
// --- Out :
// --- Returns : A DrawDib context if dithering is supported, else NULL
// --- Effect : Returns a drawdib context for use with DrawDib routines
HDRAWDIB CDIBSectionLite::GetDrawDibContext()
{
if (!GetDither())
return NULL;
if (m_hDrawDib)
return m_hDrawDib;
m_hDrawDib = DrawDibOpen();
return m_hDrawDib;
}
#endif
// --- In : pDC - Pointer to a device context
// ptDest - point at which the topleft corner of the image is drawn
// --- Out :
// --- Returns : TRUE on success
// --- Effect : Draws the image 1:1 on the device context
BOOL CDIBSectionLite::Draw(CDC* pDC, CPoint ptDest, CSize size, BOOL bForceBackground /*=FALSE*/)
{
if (!m_hBitmap)
return FALSE;
if (size == CSize(0,0))
{
size = GetSize();
}
CPoint ptOrigin = CPoint(0,0);
BOOL bResult = FALSE;
#ifndef DIBSECTION_NO_DITHER
// We will only be able to get a HDRAWDIB if dithering is enabled
HDRAWDIB hDrawDib = GetDrawDibContext();
if (hDrawDib)
{
#ifndef DIBSECTION_NO_PALETTE
DrawDibSetPalette( hDrawDib, (HPALETTE)m_Palette);
DrawDibRealize( hDrawDib, pDC->GetSafeHdc(), FALSE);
#endif // DIBSECTION_NO_PALETTE
bResult = DrawDibDraw(hDrawDib, pDC->GetSafeHdc(),
ptDest.x, ptDest.y, size.cx, size.cy,
GetBitmapInfoHeader(), GetDIBits(),
ptOrigin.x, ptOrigin.y, size.cx, size.cy,
0/*DDF_HALFTONE*/);
}
else
{
#endif
// Create a memory DC compatible with the destination DC
CDC* pMemDC = GetMemoryDC(pDC, FALSE);
if (!pMemDC)
return FALSE;
#ifndef DIBSECTION_NO_PALETTE
// Select and realize the palette
CPalette* pOldPalette = NULL;
if (m_Palette.m_hObject && UsesPalette(pDC))
{
pOldPalette = pDC->SelectPalette(&m_Palette, bForceBackground);
pDC->RealizePalette();
}
#endif // DIBSECTION_NO_PALETTE
bResult = pDC->BitBlt(ptDest.x, ptDest.y, size.cx, size.cy,
pMemDC, ptOrigin.x, ptOrigin.y, SRCCOPY);
#ifndef DIBSECTION_NO_PALETTE
if (pOldPalette)
pDC->SelectPalette(pOldPalette, FALSE);
#endif // DIBSECTION_NO_PALETTE
ReleaseMemoryDC();
#ifndef DIBSECTION_NO_DITHER
}
#endif // DIBSECTION_NO_DITHER
return bResult;
}
// --- In : pDC - Pointer to a device context
// ptDest - point at which the topleft corner of the image is drawn
// size - size to stretch the image
// --- Out :
// --- Returns : TRUE on success
// --- Effect : Stretch draws the image to the desired size on the device context
BOOL CDIBSectionLite::Stretch(CDC* pDC, CPoint ptDest, CSize size,
BOOL bForceBackground /*=FALSE*/)
{
if (!m_hBitmap)
return FALSE;
CPoint ptOrigin = CPoint(0,0);
CSize imagesize = GetSize();
BOOL bResult = FALSE;
#ifndef _WIN32_WCE
pDC->SetStretchBltMode(COLORONCOLOR);
#endif
#ifndef DIBSECTION_NO_DITHER
// We will only be able to get a HDRAWDIB if dithering is enabled
HDRAWDIB hDrawDib = GetDrawDibContext();
if (hDrawDib)
{
#ifndef DIBSECTION_NO_PALETTE
DrawDibSetPalette( hDrawDib, (HPALETTE)m_Palette);
DrawDibRealize( hDrawDib, pDC->GetSafeHdc(), FALSE);
#endif // DIBSECTION_NO_PALETTE
bResult = DrawDibDraw(hDrawDib, pDC->GetSafeHdc(),
ptDest.x, ptDest.y, size.cx, size.cy,
GetBitmapInfoHeader(), GetDIBits(),
0,0, GetWidth(), GetHeight(),
0/*DDF_HALFTONE*/);
}
else
{
#endif // DIBSECTION_NO_DITHER
// Create a memory DC compatible with the destination DC
CDC* pMemDC = GetMemoryDC(pDC, FALSE);
if (!pMemDC)
return FALSE;
#ifndef DIBSECTION_NO_PALETTE
// Select and realize the palette
CPalette* pOldPalette = NULL;
if (m_Palette.m_hObject && UsesPalette(pDC))
{
pOldPalette = pDC->SelectPalette(&m_Palette, bForceBackground);
pDC->RealizePalette();
}
#endif // DIBSECTION_NO_PALETTE
#ifndef _WIN32_WCE
pDC->SetStretchBltMode(COLORONCOLOR);
#endif // _WIN32_WCE
bResult = pDC->StretchBlt(ptDest.x, ptDest.y, size.cx, size.cy,
pMemDC,
ptOrigin.x, ptOrigin.y, imagesize.cx, imagesize.cy,
SRCCOPY);
#ifndef DIBSECTION_NO_PALETTE
if (pOldPalette)
pDC->SelectPalette(pOldPalette, FALSE);
#endif // DIBSECTION_NO_PALETTE
ReleaseMemoryDC();
#ifndef DIBSECTION_NO_DITHER
}
#endif // DIBSECTION_NO_DITHER
return bResult;
}
//////////////////////////////////////////////////////////////////////////////
// Setting the bitmap...
// --- In : nIDResource - resource ID
// --- Out :
// --- Returns : Returns TRUE on success, FALSE otherwise
// --- Effect : Initialises the bitmap from a resource. If failure, then object is
// initialised back to an empty bitmap.
BOOL CDIBSectionLite::SetBitmap(UINT nIDResource)
{
return SetBitmap(MAKEINTRESOURCE(nIDResource));
}
// --- In : lpszResourceName - resource name
// --- Out :
// --- Returns : Returns TRUE on success, FALSE otherwise
// --- Effect : Initialises the bitmap from a resource. If failure, then object is
// initialised back to an empty bitmap.
BOOL CDIBSectionLite::SetBitmap(LPCTSTR lpszResourceName)
{
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetResourceHandle(),
lpszResourceName,
IMAGE_BITMAP,
0,0,
#ifdef _WIN32_WCE
0
#else
LR_CREATEDIBSECTION
#endif
);
if (!hBmp)
{
TRACE0("Unable to LoadImage");
return FALSE;
}
BOOL bResult = SetBitmap(hBmp);
::DeleteObject(hBmp);
return bResult;
}
// --- In : lpBitmapInfo - pointer to a BITMAPINFO structure
// lpBits - pointer to image bits. Can be NULL to simply create empty bitmap
// --- Out :
// --- Returns : Returns TRUE on success, FALSE otherwise
// --- Effect : Initialises the bitmap using the information in lpBitmapInfo to determine
// the dimensions and colors, and the then sets the bits from the bits in
// lpBits. If failure, then object is initialised back to an empty bitmap.
BOOL CDIBSectionLite::SetBitmap(LPBITMAPINFO lpBitmapInfo, LPVOID lpBits)
{
DeleteObject();
if (!lpBitmapInfo) // || !lpBits)
return FALSE;
HDC hDC = NULL;
TRY {
BITMAPINFOHEADER& bmih = lpBitmapInfo->bmiHeader;
// Compute the number of colors in the color table
m_iColorTableSize = NumColorEntries(bmih.biBitCount, bmih.biCompression, bmih.biClrUsed);
DWORD dwBitmapInfoSize = sizeof(BITMAPINFO) + m_iColorTableSize*sizeof(RGBQUAD);
// Copy over BITMAPINFO contents
memcpy(&m_DIBinfo, lpBitmapInfo, dwBitmapInfoSize);
// Should now have all the info we need to create the sucker.
//TRACE(_T("Width %d, Height %d, Bits/pixel %d, Image Size %d\n"),
// bmih.biWidth, bmih.biHeight, bmih.biBitCount, bmih.biSizeImage);
// Create a DC which will be used to get DIB, then create DIBsection
hDC = ::GetDC(NULL);
if (!hDC)
{
TRACE0("Unable to get DC\n");
AfxThrowResourceException();
}
m_hBitmap = CreateDIBSection(hDC, (const BITMAPINFO*) m_DIBinfo,
m_iColorDataType, &m_ppvBits, NULL, 0);
::ReleaseDC(NULL, hDC);
if (!m_hBitmap)
{
TRACE0("CreateDIBSection failed\n");
AfxThrowResourceException();
}
if (m_DIBinfo.bmiHeader.biSizeImage == 0)
{
int nBytesPerLine = BytesPerLine(lpBitmapInfo->bmiHeader.biWidth,
lpBitmapInfo->bmiHeader.biBitCount);
m_DIBinfo.bmiHeader.biSizeImage = nBytesPerLine * lpBitmapInfo->bmiHeader.biHeight;
}
#ifndef _WIN32_WCE
// Flush the GDI batch queue
GdiFlush();
#endif
// Only copy bits if they were provided
if (lpBits != NULL)
memcpy(m_ppvBits, lpBits, m_DIBinfo.bmiHeader.biSizeImage);
#ifndef DIBSECTION_NO_PALETTE
if (!CreatePalette())
{
TRACE0("Unable to create palette\n");
AfxThrowResourceException();
}
#endif // DIBSECTION_NO_PALETTE
}
CATCH (CException, e)
{
e->Delete();
_ShowLastError();
if (hDC)
::ReleaseDC(NULL, hDC);
DeleteObject();
return FALSE;
}
END_CATCH
return TRUE;
}
// --- In : hBitmap - handle to image
// pPalette - optional palette to use when setting image
// --- Out :
// --- Returns : Returns TRUE on success, FALSE otherwise
// --- Effect : Initialises the bitmap from the HBITMAP supplied. If failure, then
// object is initialised back to an empty bitmap.
BOOL CDIBSectionLite::SetBitmap(HBITMAP hBitmap
#ifndef DIBSECTION_NO_PALETTE
, CPalette* pPalette /*= NULL*/
#endif
)
{
DeleteObject();
if (!hBitmap)
return FALSE;
// Get dimensions of bitmap
BITMAP bm;
if (!::GetObject(hBitmap, sizeof(bm),(LPVOID)&bm))
return FALSE;
bm.bmHeight = abs(bm.bmHeight);
CWindowDC dc(NULL);
#ifndef DIBSECTION_NO_PALETTE
CPalette* pOldPalette = NULL;
#endif // DIBSECTION_NO_PALETTE
TRY {
m_iColorTableSize = NumColorEntries(bm.bmBitsPixel, BI_RGB);
// Initialize the BITMAPINFOHEADER in m_DIBinfo
BITMAPINFOHEADER& bih = m_DIBinfo.bmiHeader;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = bm.bmWidth;
bih.biHeight = bm.bmHeight;
bih.biPlanes = 1; // Must always be 1 according to docs
bih.biBitCount = bm.bmBitsPixel;
#ifdef _WIN32_WCE
//DCR 4/02/01 I've found PocketPCs need BI_BITFIELDS for 16 bit dibs.
if (bm.bmBitsPixel == 16 || bm.bmBitsPixel == 32)
bih.biCompression = BI_BITFIELDS;
else
bih.biCompression = BI_RGB;
#else
bih.biCompression = BI_RGB;
#endif
bih.biSizeImage = BytesPerLine(bm.bmWidth, bm.bmBitsPixel) * bm.bmHeight;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
GetColorTableEntries(dc.GetSafeHdc(), hBitmap);
#ifndef DIBSECTION_NO_PALETTE
// If we have a palette supplied, then set the palette (and hance DIB color
// table) using this palette
if (pPalette)
SetPalette(pPalette);
if (m_Palette.GetSafeHandle())
{
pOldPalette = dc.SelectPalette(&m_Palette, FALSE);
dc.RealizePalette();
}
#endif // DIBSECTION_NO_PALETTE
// Create it!
m_hBitmap = CreateDIBSection(dc.m_hDC,
(const BITMAPINFO*) m_DIBinfo,
m_iColorDataType,
&m_ppvBits,
NULL, 0);
#ifndef DIBSECTION_NO_PALETTE
if (pOldPalette)
dc.SelectPalette(pOldPalette, FALSE);
pOldPalette = NULL;
#endif // DIBSECTION_NO_PALETTE
if (!m_hBitmap)
{
TRACE0("Unable to CreateDIBSection\n");
AfxThrowResourceException();
}
#ifndef DIBSECTION_NO_PALETTE
// If palette was supplied then create a palette using the entries in the DIB
// color table.
if (!pPalette)
CreatePalette();
#endif // DIBSECTION_NO_PALETTE
// Need to copy the supplied bitmap onto the newly created DIBsection
CDC memDC, CopyDC;
if (!CopyDC.CreateCompatibleDC(&dc) || !memDC.CreateCompatibleDC(&dc))
{
TRACE0("Unable to create compatible DC's\n");
AfxThrowResourceException();
}
#ifndef DIBSECTION_NO_PALETTE
if (m_Palette.GetSafeHandle())
{
memDC.SelectPalette(&m_Palette, FALSE); memDC.RealizePalette();
CopyDC.SelectPalette(&m_Palette, FALSE); CopyDC.RealizePalette();
}
#endif // DIBSECTION_NO_PALETTE
#ifndef _WIN32_WCE