-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDibutils.cpp
810 lines (663 loc) · 20 KB
/
Dibutils.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
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
/**************************************************************************
UTILS.C - useful functions for WinG Sample Apps
**************************************************************************/
/**************************************************************************
(C) Copyright 1994 Microsoft Corp. All rights reserved.
You have a royalty-free right to use, modify, reproduce and
distribute the Sample Files (and/or any modified version) in
any way you find useful, provided that you agree that
Microsoft has no warranty obligations or liability for any
Sample Application Files which are modified.
**************************************************************************/
/*----------------------------------------------------------------------------*\
| Functions for handling Device Independent Bitmaps and clearing the |
| System Palette. |
\*----------------------------------------------------------------------------*/
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include "dibutils.h"
#if defined(WIN32) || defined(_WIN32)
#include <memory.h> // for _fmemcpy()
#define _huge
#ifndef hmemcpy
#define hmemcpy memcpy
#endif
#endif
#define BFT_ICON 0x4349 /* 'IC' */
#define BFT_BITMAP 0x4d42 /* 'BM' */
#define BFT_CURSOR 0x5450 /* 'PT' */
/* flags for _lseek */
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0
/*
* Clear the System Palette so that we can ensure an identity palette
* mapping for fast performance.
*/
void ClearSystemPalette(void)
{
//*** A dummy palette setup
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} Palette =
{
0x300,
256
};
HPALETTE ScreenPalette = 0;
HDC ScreenDC;
int Counter;
UINT nMapped = 0;
BOOL bOK = FALSE;
int nOK = 0;
//*** Reset everything in the system palette to black
for(Counter = 0; Counter < 256; Counter++)
{
Palette.aEntries[Counter].peRed = 0;
Palette.aEntries[Counter].peGreen = 0;
Palette.aEntries[Counter].peBlue = 0;
Palette.aEntries[Counter].peFlags = PC_NOCOLLAPSE;
}
//*** Create, select, realize, deselect, and delete the palette
ScreenDC = GetDC(NULL);
ScreenPalette = CreatePalette((LOGPALETTE *)&Palette);
if (ScreenPalette)
{
ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
nMapped = RealizePalette(ScreenDC);
ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
bOK = DeleteObject(ScreenPalette);
}
nOK = ReleaseDC(NULL, ScreenDC);
return;
}
/*
* Open a DIB file and return a MEMORY DIB, a memory handle containing..
*
* BITMAP INFO bi
* palette data
* bits....
*/
int DibWriteFile(LPSTR szFile, LPBITMAPINFOHEADER lpbi)
{
HFILE fh;
OFSTRUCT of;
fh = OpenFile(szFile, &of, OF_WRITE | OF_CREATE);
if (!fh) {
// printf("la regamos0");
return 0;
}
long size = DibSize(lpbi);
// write file header
BITMAPFILEHEADER bmf;
bmf.bfType = 'BM';
bmf.bfSize = sizeof(bmf) + size;
bmf.bfReserved1 = 0;
bmf.bfReserved2 = 0;
bmf.bfOffBits = sizeof(bmf) + (char far*)(DibPtr(lpbi)) - (char far*)lpbi;
#if defined( __WATCOMC__) || defined(_MSC_VER)
if (_hwrite(fh, (LPCSTR)(&bmf), sizeof(bmf))<0 ||
_hwrite(fh, (LPCSTR)lpbi, size)<0) {
_lclose(fh);
// printf("la regamos1");
return 0;
}
#else
if (_hwrite(fh, (LPBYTE)(&bmf), sizeof(bmf))<0 ||
_hwrite(fh, (LPBYTE)lpbi, size)<0) {
_lclose(fh);
// printf("la regamos1");
return 0;
}
#endif
_lclose(fh);
return 1;
}
// The following is an alternative implementation of DIB writing, since
// the above doesn't appear to work.
/****************************************************************************
* *
* FUNCTION : PaletteSize(VOID FAR * pv) *
* *
* PURPOSE : Calculates the palette size in bytes. If the info. block *
* is of the BITMAPCOREHEADER type, the number of colors is *
* multiplied by 3 to give the palette size, otherwise the *
* number of colors is multiplied by 4. *
* *
* RETURNS : Palette size in number of bytes. *
* *
****************************************************************************/
WORD PaletteSize(VOID FAR * pv)
{
LPBITMAPINFOHEADER lpbi;
WORD NumColors;
lpbi = (LPBITMAPINFOHEADER) pv;
NumColors = DibNumColors(lpbi);
if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
return NumColors * sizeof(RGBTRIPLE);
else
return NumColors * sizeof(RGBQUAD);
}
/****************************************************************************
* *
* FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
* *
* PURPOSE : Writes data in steps of 32k till all the data is written. *
* *
* RETURNS : 0 - If write did not proceed correctly. *
* number of bytes written otherwise. *
* *
****************************************************************************/
#define MAXREAD 32768 /* Number of bytes to be read during */
/* each read operation. */
DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul)
{
DWORD ulT = ul;
#if defined(WINNT) || defined(WIN32) || defined(__WIN32__)
BYTE *hp = (BYTE *) pv;
#else
BYTE huge *hp = (BYTE huge *) pv;
#endif
while (ul > MAXREAD) {
if (_lwrite(fh, (LPSTR) hp, (WORD) MAXREAD) != MAXREAD)
return 0;
ul -= MAXREAD;
hp += MAXREAD;
}
if (_lwrite(fh, (LPSTR) hp, (WORD) ul) != (WORD) ul)
return 0;
return ulT;
}
/****************************************************************************
* *
* FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
* *
* PURPOSE : Write a global handle in CF_DIB format to a file. *
* *
* RETURNS : TRUE - if successful. *
* FALSE - otherwise *
* *
****************************************************************************/
BOOL WriteDIB(const char *szFile, LPBITMAPINFOHEADER lpbi)
{
BITMAPFILEHEADER hdr;
int fh;
OFSTRUCT of;
if (!lpbi)
return FALSE;
fh = OpenFile(szFile, &of, OF_CREATE | OF_READWRITE);
if (fh == -1)
return FALSE;
/* Fill in the fields of the file header */
hdr.bfType = BFT_BITMAP;
hdr.bfSize = DibSize(lpbi) + sizeof(BITMAPFILEHEADER);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + lpbi->biSize +
PaletteSize(lpbi);
/* Write the file header */
_lwrite(fh, (LPSTR) &hdr, sizeof(BITMAPFILEHEADER));
/* Write the DIB header and the bits */
lwrite(fh, (LPSTR) lpbi, DibSize(lpbi));
_lclose(fh);
return TRUE;
}
PDIB DibOpenFile(LPSTR szFile)
{
HFILE fh;
DWORD dwLen;
DWORD dwBits;
PDIB pdib;
LPVOID p;
OFSTRUCT of;
#if defined(WIN32) || defined(_WIN32)
#define GetCurrentInstance() GetModuleHandle(NULL)
#else
#define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
#endif
fh = OpenFile(szFile, &of, OF_READ);
#if 0
if (fh == -1)
{
HRSRC h;
h = FindResource(GetCurrentInstance(), szFile, RT_BITMAP);
#if defined(WIN32) || defined(_WIN32)
//!!! can we call GlobalFree() on this? is it the right format.
//!!! can we write to this resource?
if (h)
return (PDIB)LockResource(LoadResource(GetCurrentInstance(), h));
#else
if (h)
fh = AccessResource(GetCurrentInstance(), h);
#endif
}
#endif
if (fh == -1)
return NULL;
pdib = DibReadBitmapInfo(fh);
if (!pdib)
return NULL;
/* How much memory do we need to hold the DIB */
dwBits = pdib->biSizeImage;
dwLen = pdib->biSize + DibPaletteSize(pdib) + dwBits;
/* Can we get more memory? */
p = GlobalReAllocPtr(pdib,dwLen,0);
if (!p)
{
GlobalFreePtr(pdib);
pdib = NULL;
}
else
{
pdib = (PDIB)p;
}
if (pdib)
{
/* read in the bits */
_hread(fh, (LPBYTE)pdib + (UINT)pdib->biSize + DibPaletteSize(pdib), dwBits);
}
_lclose(fh);
return pdib;
}
/*
* ReadDibBitmapInfo()
*
* Will read a file in DIB format and return a global HANDLE to its
* BITMAPINFO. This function will work with both "old" and "new"
* bitmap formats, but will always return a "new" BITMAPINFO.
*/
PDIB DibReadBitmapInfo(HFILE fh)
{
DWORD off;
HANDLE hbi = NULL;
int size;
int i;
int nNumColors;
RGBQUAD FAR *pRgb;
BITMAPINFOHEADER bi;
BITMAPCOREHEADER bc;
BITMAPFILEHEADER bf;
PDIB pdib;
if (fh == -1)
return NULL;
off = _llseek(fh,0L,SEEK_CUR);
if (sizeof(bf) != _lread(fh,(LPSTR)&bf,sizeof(bf)))
return FALSE;
/*
* do we have a RC HEADER?
*/
if (bf.bfType != BFT_BITMAP)
{
bf.bfOffBits = 0L;
_llseek(fh,off,SEEK_SET);
}
if (sizeof(bi) != _lread(fh,(LPSTR)&bi,sizeof(bi)))
return FALSE;
/*
* what type of bitmap info is this?
*/
switch (size = (int)bi.biSize)
{
default:
case sizeof(BITMAPINFOHEADER):
break;
case sizeof(BITMAPCOREHEADER):
bc = *(BITMAPCOREHEADER*)&bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = (DWORD)bc.bcWidth;
bi.biHeight = (DWORD)bc.bcHeight;
bi.biPlanes = (UINT)bc.bcPlanes;
bi.biBitCount = (UINT)bc.bcBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
_llseek(fh,(LONG)sizeof(BITMAPCOREHEADER)-sizeof(BITMAPINFOHEADER),SEEK_CUR);
break;
}
nNumColors = DibNumColors(&bi);
#if 0
if (bi.biSizeImage == 0)
bi.biSizeImage = DibSizeImage(&bi);
if (bi.biClrUsed == 0)
bi.biClrUsed = DibNumColors(&bi);
#else
FixBitmapInfo(&bi);
#endif
pdib = (PDIB)GlobalAllocPtr(GMEM_MOVEABLE,(LONG)bi.biSize + nNumColors * sizeof(RGBQUAD));
if (!pdib)
return NULL;
*pdib = bi;
pRgb = DibColors(pdib);
if (nNumColors)
{
if (size == sizeof(BITMAPCOREHEADER))
{
/*
* convert a old color table (3 byte entries) to a new
* color table (4 byte entries)
*/
_lread(fh,(LPVOID)pRgb,nNumColors * sizeof(RGBTRIPLE));
for (i=nNumColors-1; i>=0; i--)
{
RGBQUAD rgb;
rgb.rgbRed = ((RGBTRIPLE FAR *)pRgb)[i].rgbtRed;
rgb.rgbBlue = ((RGBTRIPLE FAR *)pRgb)[i].rgbtBlue;
rgb.rgbGreen = ((RGBTRIPLE FAR *)pRgb)[i].rgbtGreen;
rgb.rgbReserved = (BYTE)0;
pRgb[i] = rgb;
}
}
else
{
_lread(fh,(LPVOID)pRgb,nNumColors * sizeof(RGBQUAD));
}
}
if (bf.bfOffBits != 0L)
_llseek(fh,off + bf.bfOffBits,SEEK_SET);
return pdib;
}
/*
* DibSetUsage(hdib,hpal,wUsage)
*
* Modifies the color table of the passed DIB for use with the wUsage
* parameter specifed.
*
* if wUsage is DIB_PAL_COLORS the DIB color table is set to 0-256
* if wUsage is DIB_RGB_COLORS the DIB color table is set to the RGB values
* in the passed palette
*/
BOOL DibSetUsage(PDIB pdib, HPALETTE hpal,UINT wUsage)
{
PALETTEENTRY ape[256];
RGBQUAD FAR * pRgb;
WORD FAR * pw;
int nColors;
int n;
if (hpal == NULL)
hpal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
if (!pdib)
return FALSE;
nColors = DibNumColors(pdib);
if (nColors == 3 && DibCompression(pdib) == BI_BITFIELDS)
nColors = 0;
if (nColors > 0)
{
pRgb = DibColors(pdib);
switch (wUsage)
{
//
// Set the DIB color table to palette indexes
//
case DIB_PAL_COLORS:
{
// According to Oliver Schreck <[email protected]>,
// this produces a wrong palette.
for (pw = (WORD FAR*)pRgb,n=0; n<nColors; n++,pw++)
*pw = n;
// Notes by JACS: This is the corrected code.
// Actually this doens't work, it gives a corrupt
// palette when reading in a JPEG.
/*
for (n=0; n<nColors; n++)
{
pRgb[n].rgbRed = n;
pRgb[n].rgbGreen = n;
pRgb[n].rgbBlue = n;
}
*/
break;
}
//
// Set the DIB color table to RGBQUADS
//
default:
case DIB_RGB_COLORS:
nColors = (nColors < 256) ? nColors: 256;
GetPaletteEntries(hpal,0,nColors,ape);
for (n=0; n<nColors; n++)
{
pRgb[n].rgbRed = ape[n].peRed;
pRgb[n].rgbGreen = ape[n].peGreen;
pRgb[n].rgbBlue = ape[n].peBlue;
pRgb[n].rgbReserved = 0;
}
break;
}
}
return TRUE;
}
/*
* DibCreate(bits, dx, dy)
*
* Creates a new packed DIB with the given dimensions and the
* given number of bits per pixel
*/
PDIB DibCreate(int bits, int dx, int dy)
{
LPBITMAPINFOHEADER lpbi ;
DWORD dwSizeImage;
int i;
DWORD FAR *pdw;
dwSizeImage = dy*(DWORD)((dx*bits/8+3)&~3);
lpbi = (PDIB)GlobalAllocPtr(GHND,sizeof(BITMAPINFOHEADER)+dwSizeImage + 1024);
if (lpbi == NULL)
return NULL;
lpbi->biSize = sizeof(BITMAPINFOHEADER) ;
lpbi->biWidth = dx;
lpbi->biHeight = dy;
lpbi->biPlanes = 1;
lpbi->biBitCount = bits ;
lpbi->biCompression = BI_RGB ;
lpbi->biSizeImage = dwSizeImage;
lpbi->biXPelsPerMeter = 0 ;
lpbi->biYPelsPerMeter = 0 ;
lpbi->biClrUsed = 0 ;
lpbi->biClrImportant = 0 ;
if (bits == 4)
lpbi->biClrUsed = 16;
else if (bits == 8)
lpbi->biClrUsed = 256;
pdw = (DWORD FAR *)((LPBYTE)lpbi+(int)lpbi->biSize);
for (i=0; i<(int)lpbi->biClrUsed/16; i++)
{
*pdw++ = 0x00000000; // 0000 black
*pdw++ = 0x00800000; // 0001 dark red
*pdw++ = 0x00008000; // 0010 dark green
*pdw++ = 0x00808000; // 0011 mustard
*pdw++ = 0x00000080; // 0100 dark blue
*pdw++ = 0x00800080; // 0101 purple
*pdw++ = 0x00008080; // 0110 dark turquoise
*pdw++ = 0x00C0C0C0; // 1000 gray
*pdw++ = 0x00808080; // 0111 dark gray
*pdw++ = 0x00FF0000; // 1001 red
*pdw++ = 0x0000FF00; // 1010 green
*pdw++ = 0x00FFFF00; // 1011 yellow
*pdw++ = 0x000000FF; // 1100 blue
*pdw++ = 0x00FF00FF; // 1101 pink (magenta)
*pdw++ = 0x0000FFFF; // 1110 cyan
*pdw++ = 0x00FFFFFF; // 1111 white
}
return (PDIB)lpbi;
}
static void xlatClut8(BYTE FAR *pb, DWORD dwSize, BYTE FAR *xlat)
{
DWORD dw;
#ifdef __cplusplus
for (dw = 0; dw < dwSize; dw++, ((BYTE _huge *&)pb)++)
#else
for (dw = 0; dw < dwSize; dw++, ((BYTE _huge *)pb)++)
#endif
*pb = xlat[*pb];
}
static void xlatClut4(BYTE FAR *pb, DWORD dwSize, BYTE FAR *xlat)
{
DWORD dw;
#ifdef __cplusplus
for (dw = 0; dw < dwSize; dw++, ((BYTE _huge *&)pb)++)
#else
for (dw = 0; dw < dwSize; dw++, ((BYTE _huge *)pb)++)
#endif
*pb = (BYTE)(xlat[*pb & 0x0F] | (xlat[(*pb >> 4) & 0x0F] << 4));
}
#define RLE_ESCAPE 0
#define RLE_EOL 0
#define RLE_EOF 1
#define RLE_JMP 2
static void xlatRle8(BYTE FAR *pb, DWORD dwSize, BYTE FAR *xlat)
{
BYTE cnt;
BYTE b;
BYTE _huge *prle = pb;
for(;;)
{
cnt = *prle++;
b = *prle;
if (cnt == RLE_ESCAPE)
{
prle++;
switch (b)
{
case RLE_EOF:
return;
case RLE_EOL:
break;
case RLE_JMP:
prle++; // skip dX
prle++; // skip dY
break;
default:
cnt = b;
for (b=0; b<cnt; b++,prle++)
*prle = xlat[*prle];
if (cnt & 1)
prle++;
break;
}
}
else
{
*prle++ = xlat[b];
}
}
}
static void xlatRle4(BYTE FAR *pb, DWORD dwSize, BYTE FAR *xlat)
{
}
static void hmemmove(BYTE _huge *d, BYTE _huge *s, LONG len)
{
d += len-1;
s += len-1;
while (len--)
*d-- = *s--;
}
/*
* DibMapToPalette(pdib, hpal)
*
* Map the colors of the DIB, using GetNearestPaletteIndex, to
* the colors of the given palette.
*/
BOOL DibMapToPalette(PDIB pdib, HPALETTE hpal)
{
LPBITMAPINFOHEADER lpbi;
PALETTEENTRY pe;
int n;
int nDibColors;
int nPalColors=0;
BYTE FAR * lpBits;
RGBQUAD FAR * lpRgb;
BYTE xlat[256];
DWORD SizeImage;
if (!hpal || !pdib)
return FALSE;
lpbi = (LPBITMAPINFOHEADER)pdib;
lpRgb = DibColors(pdib);
GetObject(hpal,sizeof(int),(LPSTR)&nPalColors);
nDibColors = DibNumColors(pdib);
if ((SizeImage = lpbi->biSizeImage) == 0)
SizeImage = DibSizeImage(lpbi);
//
// build a xlat table. from the current DIB colors to the given
// palette.
//
for (n=0; n<nDibColors; n++)
xlat[n] = (BYTE)GetNearestPaletteIndex(hpal,RGB(lpRgb[n].rgbRed,lpRgb[n].rgbGreen,lpRgb[n].rgbBlue));
lpBits = (LPBYTE)DibPtr(lpbi);
lpbi->biClrUsed = nPalColors;
//
// re-size the DIB
//
if (nPalColors > nDibColors)
{
GlobalReAllocPtr(lpbi, lpbi->biSize + nPalColors*sizeof(RGBQUAD) + SizeImage, 0);
hmemmove((BYTE _huge *)DibPtr(lpbi), (BYTE _huge *)lpBits, SizeImage);
lpBits = (LPBYTE)DibPtr(lpbi);
}
else if (nPalColors < nDibColors)
{
hmemcpy(DibPtr(lpbi), lpBits, SizeImage);
GlobalReAllocPtr(lpbi, lpbi->biSize + nPalColors*sizeof(RGBQUAD) + SizeImage, 0);
lpBits = (LPBYTE)DibPtr(lpbi);
}
//
// translate the DIB bits
//
switch (lpbi->biCompression)
{
case BI_RLE8:
xlatRle8(lpBits, SizeImage, xlat);
break;
case BI_RLE4:
xlatRle4(lpBits, SizeImage, xlat);
break;
case BI_RGB:
if (lpbi->biBitCount == 8)
xlatClut8(lpBits, SizeImage, xlat);
else
xlatClut4(lpBits, SizeImage, xlat);
break;
}
//
// Now copy the RGBs in the logical palette to the dib color table
//
for (n=0; n<nPalColors; n++)
{
GetPaletteEntries(hpal,n,1,&pe);
lpRgb[n].rgbRed = pe.peRed;
lpRgb[n].rgbGreen = pe.peGreen;
lpRgb[n].rgbBlue = pe.peBlue;
lpRgb[n].rgbReserved = (BYTE)0;
}
return TRUE;
}
HPALETTE MakePalette(const BITMAPINFO FAR* Info, UINT flags)
{
HPALETTE hPalette;
const RGBQUAD far* rgb = Info->bmiColors;
// PJO - Calculate colours correctly
WORD nColors = WORD( Info->bmiHeader.biClrUsed ? Info->bmiHeader.biClrUsed :
1 << Info->bmiHeader.biBitCount );
if ( nColors ) {
LOGPALETTE* logPal = (LOGPALETTE*)
new BYTE[sizeof(LOGPALETTE) + (nColors-1)*sizeof(PALETTEENTRY)];
logPal->palVersion = 0x300; // Windows 3.0 version
logPal->palNumEntries = nColors;
for (short n = 0; n < nColors; n++) {
logPal->palPalEntry[n].peRed = rgb[n].rgbRed;
logPal->palPalEntry[n].peGreen = rgb[n].rgbGreen;
logPal->palPalEntry[n].peBlue = rgb[n].rgbBlue;
logPal->palPalEntry[n].peFlags = (BYTE)flags;
}
hPalette = ::CreatePalette(logPal);
delete logPal;
} else
hPalette = 0;
return hPalette;
}