-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIB.CPP
More file actions
739 lines (664 loc) · 24.7 KB
/
DIB.CPP
File metadata and controls
739 lines (664 loc) · 24.7 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
/****************************************************************************\
*
* FILE: DIB.C
*
* PURPOSE: DIB functions for IconPro Project
*
* COMMENTS: Icons are stored in a format almost identical to DIBs. For
* this reason, it is easiest to deal with the individual
* icon images as DIBs (or DIBSections). This has the added
* advantage of retaining color depth even on low-end displays.
*
* FUNCTIONS:
* EXPORTS:
* FindDIBBits() - Locate the image bits in a DIB
* DIBNumColors() - Calculate number of color table entries
* PaletteSize() - Calculate number of color table bytes
* BytesPerLine() - Calculate number of bytes per scan line
* ConvertDIBFormat() - Converts DIBs between formats
* SetMonoDIBPixel() - Sets/Clears a pixel in a 1bpp DIB
* ReadBMPFile() - Reads a BMP file into CF_DIB memory
* WriteBMPFile() - Write a BMP file from CF_DIB memory
* LOCALS:
* CopyColorTable() - Copies color table from DIB to DIB
*
* Copyright 1995 Microsoft Corp.
*
*
* History:
* July '95 - Created
*
\****************************************************************************/
#include "stdafx.h"
//#include <Windows.h>
#include "Dib.H"
/****************************************************************************/
/* Local Function Prototypes */
BOOL CopyColorTable( LPBITMAPINFO lpTarget, LPBITMAPINFO lpSource );
/****************************************************************************/
/****************************************************************************
*
* FUNCTION: FindDIBits
*
* PURPOSE: Locate the image bits in a CF_DIB format DIB.
*
* PARAMS: LPSTR lpbi - pointer to the CF_DIB memory block
*
* RETURNS: LPSTR - pointer to the image bits
*
* History:
* July '95 - Copied <g>
*
\****************************************************************************/
LPSTR FindDIBBits( LPSTR lpbi )
{
return ( lpbi + *(LPDWORD)lpbi + PaletteSize( lpbi ) );
}
/* End FindDIBits() *********************************************************/
/****************************************************************************
*
* FUNCTION: DIBNumColors
*
* PURPOSE: Calculates the number of entries in the color table.
*
* PARAMS: LPSTR lpbi - pointer to the CF_DIB memory block
*
* RETURNS: WORD - Number of entries in the color table.
*
* History:
* July '95 - Copied <g>
*
\****************************************************************************/
WORD DIBNumColors( LPSTR lpbi )
{
WORD wBitCount;
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;
if (dwClrUsed)
return (WORD) dwClrUsed;
wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;
switch (wBitCount)
{
case 1: return 2;
case 4: return 16;
case 8: return 256;
default:return 0;
}
return 0;
}
/* End DIBNumColors() ******************************************************/
/****************************************************************************
*
* FUNCTION: PaletteSize
*
* PURPOSE: Calculates the number of bytes in the color table.
*
* PARAMS: LPSTR lpbi - pointer to the CF_DIB memory block
*
* RETURNS: WORD - number of bytes in the color table
*
*
* History:
* July '95 - Copied <g>
*
\****************************************************************************/
WORD PaletteSize( LPSTR lpbi )
{
return ( DIBNumColors( lpbi ) * sizeof( RGBQUAD ) );
}
/* End PaletteSize() ********************************************************/
/****************************************************************************
*
* FUNCTION: BytesPerLine
*
* PURPOSE: Calculates the number of bytes in one scan line.
*
* PARAMS: LPBITMAPINFOHEADER lpBMIH - pointer to the BITMAPINFOHEADER
* that begins the CF_DIB block
*
* RETURNS: DWORD - number of bytes in one scan line (DWORD aligned)
*
* History:
* July '95 - Created
*
\****************************************************************************/
DWORD BytesPerLine( LPBITMAPINFOHEADER lpBMIH )
{
return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes * lpBMIH->biBitCount);
}
/* End BytesPerLine() ********************************************************/
/****************************************************************************
*
* FUNCTION: ConvertDIBFormat
*
* PURPOSE: Creates a new DIB of the requested format, copies the source
* image to the new DIB.
*
* PARAMS: LPBITMAPINFO lpSrcDIB - the source CF_DIB
* UINT nWidth - width for new DIB
* UINT nHeight - height for new DIB
* UINT nbpp - bpp for new DIB
* BOOL bStretch - TRUE to stretch source to dest
* FALSE to take upper left of image
*
* RETURNS: LPBYTE - pointer to new CF_DIB memory block with new image
* NULL on failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
LPBYTE ConvertDIBFormat( LPBITMAPINFO lpSrcDIB, UINT nWidth, UINT nHeight, UINT nbpp, BOOL bStretch )
{
LPBITMAPINFO lpbmi = NULL;
LPBYTE lpSourceBits, lpTargetBits, lpResult;
HDC hDC = NULL, hSourceDC, hTargetDC;
HBITMAP hSourceBitmap, hTargetBitmap, hOldTargetBitmap, hOldSourceBitmap;
DWORD dwSourceBitsSize, dwTargetBitsSize, dwTargetHeaderSize;
// Allocate and fill out a BITMAPINFO struct for the new DIB
// Allow enough room for a 256-entry color table, just in case
dwTargetHeaderSize = sizeof( BITMAPINFO ) + ( 256 * sizeof( RGBQUAD ) );
lpbmi = ( LPBITMAPINFO )malloc( dwTargetHeaderSize );
lpbmi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
lpbmi->bmiHeader.biWidth = nWidth;
lpbmi->bmiHeader.biHeight = nHeight;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = nbpp;
lpbmi->bmiHeader.biCompression = BI_RGB;
lpbmi->bmiHeader.biSizeImage = 0;
lpbmi->bmiHeader.biXPelsPerMeter = 0;
lpbmi->bmiHeader.biYPelsPerMeter = 0;
lpbmi->bmiHeader.biClrUsed = 0;
lpbmi->bmiHeader.biClrImportant = 0;
// Fill in the color table
if( ! CopyColorTable( lpbmi, (LPBITMAPINFO)lpSrcDIB ) )
{
free( lpbmi );
return NULL;
}
// Gonna use DIBSections and BitBlt() to do the conversion, so make 'em
hDC = GetDC( NULL );
hTargetBitmap = CreateDIBSection( hDC, lpbmi, DIB_RGB_COLORS, (VOID**)&lpTargetBits, NULL, 0 );
hSourceBitmap = CreateDIBSection( hDC, lpSrcDIB, DIB_RGB_COLORS, (VOID**)&lpSourceBits, NULL, 0 );
hSourceDC = CreateCompatibleDC( hDC );
hTargetDC = CreateCompatibleDC( hDC );
// Flip the bits on the source DIBSection to match the source DIB
dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight * BytesPerLine(&(lpSrcDIB->bmiHeader));
dwTargetBitsSize = lpbmi->bmiHeader.biHeight * BytesPerLine(&(lpbmi->bmiHeader));
memcpy( lpSourceBits, FindDIBBits((LPSTR)lpSrcDIB), dwSourceBitsSize );
// Select DIBSections into DCs
hOldSourceBitmap = (HBITMAP)SelectObject( hSourceDC, hSourceBitmap );
hOldTargetBitmap = (HBITMAP)SelectObject( hTargetDC, hTargetBitmap );
// Set the color tables for the DIBSections
if( lpSrcDIB->bmiHeader.biBitCount <= 8 )
SetDIBColorTable( hSourceDC, 0, 1 << lpSrcDIB->bmiHeader.biBitCount, lpSrcDIB->bmiColors );
if( lpbmi->bmiHeader.biBitCount <= 8 )
SetDIBColorTable( hTargetDC, 0, 1 << lpbmi->bmiHeader.biBitCount, lpbmi->bmiColors );
// If we are asking for a straight copy, do it
if( (lpSrcDIB->bmiHeader.biWidth==lpbmi->bmiHeader.biWidth) && (lpSrcDIB->bmiHeader.biHeight==lpbmi->bmiHeader.biHeight) )
{
BitBlt( hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY );
}
else
{
// else, should we stretch it?
if( bStretch )
{
SetStretchBltMode( hTargetDC, COLORONCOLOR );
StretchBlt( hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, lpSrcDIB->bmiHeader.biWidth, lpSrcDIB->bmiHeader.biHeight, SRCCOPY );
}
else
{
// or just take the upper left corner of the source
BitBlt( hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY );
}
}
// Clean up and delete the DCs
SelectObject( hSourceDC, hOldSourceBitmap );
SelectObject( hSourceDC, hOldTargetBitmap );
DeleteDC( hSourceDC );
DeleteDC( hTargetDC );
ReleaseDC( NULL, hDC );
// Flush the GDI batch, so we can play with the bits
GdiFlush();
// Allocate enough memory for the new CF_DIB, and copy bits
lpResult = (LPBYTE)malloc( dwTargetHeaderSize + dwTargetBitsSize );
memcpy( lpResult, lpbmi, dwTargetHeaderSize );
memcpy( FindDIBBits( (LPSTR)lpResult ), lpTargetBits, dwTargetBitsSize );
// final cleanup
DeleteObject( hTargetBitmap );
DeleteObject( hSourceBitmap );
free( lpbmi );
return lpResult;
}
/* End ConvertDIBFormat() ***************************************************/
/****************************************************************************
*
* FUNCTION: CopyColorTable
*
* PURPOSE: Copies the color table from one CF_DIB to another.
*
* PARAMS: LPBITMAPINFO lpTarget - pointer to target DIB
* LPBITMAPINFO lpSource - pointer to source DIB
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
BOOL CopyColorTable( LPBITMAPINFO lpTarget, LPBITMAPINFO lpSource )
{
// What we do depends on the target's color depth
switch( lpTarget->bmiHeader.biBitCount )
{
// 8bpp - need 256 entry color table
case 8:
if( lpSource->bmiHeader.biBitCount == 8 )
{ // Source is 8bpp too, copy color table
memcpy( lpTarget->bmiColors, lpSource->bmiColors, 256*sizeof(RGBQUAD) );
return TRUE;
}
else
{ // Source is != 8bpp, use halftone palette
HPALETTE hPal;
HDC hDC = GetDC( NULL );
PALETTEENTRY pe[256];
UINT i;
hPal = CreateHalftonePalette( hDC );
ReleaseDC( NULL, hDC );
GetPaletteEntries( hPal, 0, 256, pe );
DeleteObject( hPal );
for(i=0;i<256;i++)
{
lpTarget->bmiColors[i].rgbRed = pe[i].peRed;
lpTarget->bmiColors[i].rgbGreen = pe[i].peGreen;
lpTarget->bmiColors[i].rgbBlue = pe[i].peBlue;
lpTarget->bmiColors[i].rgbReserved = pe[i].peFlags;
}
return TRUE;
}
break; // end 8bpp
// 4bpp - need 16 entry color table
case 4:
if( lpSource->bmiHeader.biBitCount == 4 )
{ // Source is 4bpp too, copy color table
memcpy( lpTarget->bmiColors, lpSource->bmiColors, 16*sizeof(RGBQUAD) );
return TRUE;
}
else
{ // Source is != 4bpp, use system palette
HPALETTE hPal;
PALETTEENTRY pe[256];
UINT i;
hPal = (HPALETTE)GetStockObject( DEFAULT_PALETTE );
GetPaletteEntries( hPal, 0, 16, pe );
for(i=0;i<16;i++)
{
lpTarget->bmiColors[i].rgbRed = pe[i].peRed;
lpTarget->bmiColors[i].rgbGreen = pe[i].peGreen;
lpTarget->bmiColors[i].rgbBlue = pe[i].peBlue;
lpTarget->bmiColors[i].rgbReserved = pe[i].peFlags;
}
return TRUE;
}
break; // end 4bpp
// 1bpp - need 2 entry mono color table
case 1:
lpTarget->bmiColors[0].rgbRed = 0;
lpTarget->bmiColors[0].rgbGreen = 0;
lpTarget->bmiColors[0].rgbBlue = 0;
lpTarget->bmiColors[0].rgbReserved = 0;
lpTarget->bmiColors[1].rgbRed = 255;
lpTarget->bmiColors[1].rgbGreen = 255;
lpTarget->bmiColors[1].rgbBlue = 255;
lpTarget->bmiColors[1].rgbReserved = 0;
break; // end 1bpp
// no color table for the > 8bpp modes
case 32:
case 24:
case 16:
default:
return TRUE;
break;
}
return TRUE;
}
/* End CopyColorTable() *****************************************************/
/****************************************************************************
*
* FUNCTION: SetMonoDIBPixel
*
* PURPOSE: Sets/Clears a pixel in a 1bpp DIB by directly poking the bits.
*
* PARAMS: LPBYTE pANDBits - pointer to the 1bpp image bits
* DWORD dwWidth - width of the DIB
* DWORD dwHeight - height of the DIB
* DWORD x - x location of pixel to set/clear
* DWORD y - y location of pixel to set/clear
* BOOL bWhite - TRUE to set pixel, FALSE to clear it
*
* RETURNS: void
*
* History:
* July '95 - Created
*
\****************************************************************************/
void SetMonoDIBPixel( LPBYTE pANDBits, DWORD dwWidth, DWORD dwHeight, DWORD x, DWORD y, BOOL bWhite )
{
DWORD ByteIndex;
BYTE BitNumber;
// Find the byte on which this scanline begins
ByteIndex = (dwHeight - y - 1) * WIDTHBYTES(dwWidth);
// Find the byte containing this pixel
ByteIndex += (x >> 3);
// Which bit is it?
BitNumber = (BYTE)( 7 - (x % 8) );
if( bWhite )
// Turn it on
pANDBits[ByteIndex] |= (1<<BitNumber);
else
// Turn it off
pANDBits[ByteIndex] &= ~(1<<BitNumber);
}
/* End SetMonoDIBPixel() *****************************************************/
/****************************************************************************
*
* FUNCTION: ReadBMPFile
*
* PURPOSE: Reads a BMP file into CF_DIB format
*
* PARAMS: LPCTSTR szFileName - the name of the file to read
*
* RETURNS: LPBYTE - pointer to the CF_DIB, NULL for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
LPBYTE ReadBMPFile( LPCTSTR szFileName )
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
DWORD dwBytes;
LPBYTE lpDIB = NULL, lpTemp = NULL;
WORD wPaletteSize = 0;
DWORD dwBitsSize = 0;
// Open the file
if( (hFile=CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE )
{
MessageBox( NULL, "Error opening file", szFileName, MB_OK );
return NULL;
}
// Read the header
if( ( ! ReadFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL ) ) || ( dwBytes != sizeof( BITMAPFILEHEADER ) ) )
{
CloseHandle( hFile );
MessageBox( NULL, "Error reading file", szFileName, MB_OK );
return NULL;
}
// Does it look like a BMP file?
if( ( bfh.bfType != 0x4d42 ) || (bfh.bfReserved1!=0) || (bfh.bfReserved2!=0) )
{
CloseHandle( hFile );
MessageBox( NULL, "Not a recognised BMP format file", szFileName, MB_OK );
return NULL;
}
// Allocate some memory
if( (lpDIB = (LPBYTE)malloc( sizeof( BITMAPINFO ) )) == NULL )
{
CloseHandle( hFile );
MessageBox( NULL, "Failed to allocate memory for DIB", szFileName, MB_OK );
return NULL;
}
// Read in the BITMAPINFOHEADER
if( (!ReadFile( hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwBytes, NULL )) || (dwBytes!=sizeof(BITMAPINFOHEADER)) )
{
CloseHandle( hFile );
free( lpDIB );
MessageBox( NULL, "Error reading file", szFileName, MB_OK );
return NULL;
}
if( ((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof( BITMAPINFOHEADER ) )
{
CloseHandle( hFile );
free( lpDIB );
MessageBox( NULL, "OS/2 style BMPs Not Supported", szFileName, MB_OK );
return NULL;
}
// How big are the elements?
wPaletteSize = PaletteSize((LPSTR)lpDIB);
dwBitsSize = ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB);
// realloc to account for the total size of the DIB
if( (lpTemp = (LPBYTE)realloc( lpDIB, sizeof( BITMAPINFOHEADER ) + wPaletteSize + dwBitsSize )) == NULL )
{
CloseHandle( hFile );
MessageBox( NULL, "Failed to allocate memory for DIB", szFileName, MB_OK );
free( lpDIB );
return NULL;
}
lpDIB = lpTemp;
// If there is a color table, read it
if( wPaletteSize != 0 )
{
if( (!ReadFile( hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwBytes, NULL )) || (dwBytes!=wPaletteSize) )
{
CloseHandle( hFile );
free( lpDIB );
MessageBox( NULL, "Error reading file", szFileName, MB_OK );
return NULL;
}
}
// Seek to the bits
// checking against 0 in case some bogus app didn't set this element
if( bfh.bfOffBits != 0 )
{
if( SetFilePointer( hFile, bfh.bfOffBits, NULL, FILE_BEGIN ) == 0xffffffff )
{
CloseHandle( hFile );
free( lpDIB );
MessageBox( NULL, "Error reading file", szFileName, MB_OK );
return NULL;
}
}
// Read the image bits
if( (!ReadFile( hFile, FindDIBBits((LPSTR)lpDIB), dwBitsSize, &dwBytes, NULL )) || (dwBytes!=dwBitsSize) )
{
CloseHandle( hFile );
free( lpDIB );
MessageBox( NULL, "Error reading file", szFileName, MB_OK );
return NULL;
}
// clean up
CloseHandle( hFile );
return lpDIB;
}
/* End ReadBMPFile() ********************************************************/
/****************************************************************************
*
* FUNCTION: WriteBMPFile
*
* PURPOSE: Writes a BMP file from CF_DIB format
*
* PARAMS: LPCTSTR szFileName - the name of the file to read
* LPBYTE - pointer to the CF_DIB, NULL for failure
*
* RETURNS: BOOL - TRUE for success, FALSE for Failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
BOOL WriteBMPFile( LPCTSTR szFileName, LPBYTE lpDIB )
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
DWORD dwBytes, dwBytesToWrite;
LPBITMAPINFOHEADER lpbmih;
// Open the file
if( (hFile=CreateFile( szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE )
{
MessageBox( NULL, "Error opening file", szFileName, MB_OK );
return FALSE;
}
bfh.bfType = 0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + PaletteSize( (LPSTR)lpDIB );
bfh.bfSize = (bfh.bfOffBits + ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB))/4;
// Write the header
if( ( ! WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL ) ) || ( dwBytes != sizeof( BITMAPFILEHEADER ) ) )
{
CloseHandle( hFile );
MessageBox( NULL, "Error Writing file", szFileName, MB_OK );
return FALSE;
}
lpbmih = (LPBITMAPINFOHEADER)lpDIB;
lpbmih->biHeight /= 2;
dwBytesToWrite = bfh.bfOffBits + (lpbmih->biHeight * BytesPerLine(lpbmih));
if( ( ! WriteFile( hFile, lpDIB, dwBytesToWrite, &dwBytes, NULL ) ) || ( dwBytes != dwBytesToWrite ) )
{
CloseHandle( hFile );
MessageBox( NULL, "Error Writing file", szFileName, MB_OK );
return FALSE;
}
lpbmih->biHeight *= 2;
CloseHandle( hFile );
return TRUE;
}
/* End WriteBMPFile() *******************************************************/
INT32 InvertBmpLine( LPBYTE lpDIBBits, DWORD dwBytesPerLine, DWORD dwBmpSize )
{
INT32 nLineNum;
BYTE *pTempBuf;
pTempBuf = (LPBYTE)malloc( dwBmpSize );
if( NULL == pTempBuf )
{
return -31;
}
memcpy( pTempBuf, lpDIBBits, dwBmpSize );
nLineNum = dwBmpSize / dwBytesPerLine;
for( INT32 i = 0; i < nLineNum; i ++ )
{
memcpy( lpDIBBits + ( dwBytesPerLine * ( nLineNum - i - 1 ) ), pTempBuf + i * dwBytesPerLine, dwBytesPerLine );
}
return 0;
}
HANDLE MakeDib( HBITMAP hbitmap, UINT bits, HPALETTE hPalette )
{
HANDLE hdib;
HDC hdc;
BITMAP bitmap;
UINT wLineLen;
DWORD dwSize;
DWORD wColSize;
LPBITMAPINFOHEADER lpbi;
HPALETTE hOldPal;
LPBYTE lpBits;
//ASSERT( hPalette != NULL );
GetObject( hbitmap, sizeof( BITMAP ), &bitmap );
//
// DWORD align the width of the DIB
// Figure out the size of the colour table
// Calculate the size of the DIB
//
wLineLen = (bitmap.bmWidth*bits+31)/32 * 4;
wColSize = sizeof( RGBQUAD ) * ( ( bits <= 8 ) ? 1 << bits : 0 );
dwSize = sizeof( BITMAPINFOHEADER ) + wColSize +
( DWORD )( UINT ) wLineLen * ( DWORD )( UINT )bitmap.bmHeight;
//
// Allocate room for a DIB and set the LPBI fields
//
hdib = GlobalAlloc( GHND, dwSize );
if ( NULL == hdib )
return NULL;
lpbi = ( LPBITMAPINFOHEADER )GlobalLock( hdib ) ;
lpbi->biSize = sizeof(BITMAPINFOHEADER) ;
lpbi->biWidth = bitmap.bmWidth;
lpbi->biHeight = bitmap.bmHeight;
lpbi->biPlanes = 1;
lpbi->biBitCount = ( WORD )bits;
lpbi->biCompression = BI_RGB;
lpbi->biSizeImage = dwSize - sizeof(BITMAPINFOHEADER) - wColSize;
lpbi->biXPelsPerMeter = 0;
lpbi->biYPelsPerMeter = 0;
lpbi->biClrUsed = ( bits <= 8 ) ? 1 << bits : 0;
lpbi->biClrImportant = 0;
//
// Get the bits from the bitmap and stuff them after the LPBI
//
lpBits = ( LPBYTE )( lpbi + 1 ) + wColSize;
hdc = CreateCompatibleDC( NULL );
if( NULL != hPalette )
{
hOldPal = (HPALETTE)SelectObject( hdc, hPalette );
RealizePalette( hdc );
}
GetDIBits( hdc,hbitmap, 0, bitmap.bmHeight, lpBits, ( LPBITMAPINFO )lpbi, DIB_RGB_COLORS );
// Fix this if GetDIBits messed it up....
lpbi->biClrUsed = ( bits <= 8 ) ? 1 << bits : 0;
if( hPalette != NULL )
{
SelectObject( hdc, hOldPal );
RealizePalette( hdc );
}
DeleteDC( hdc );
GlobalUnlock( hdib );
return hdib;
}
PBITMAPINFO CreateBitmapInfoStruct( HBITMAP hBmp )
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
/*Retrieve the bitmap's color format, width, and height.*/
if(!GetObject( hBmp, sizeof( BITMAP ), ( LPSTR )&bmp ) )
ATLTRACE("GetObject");
/*Convert the color format to a count of bits.*/
cClrBits = ( WORD )( bmp.bmPlanes * bmp.bmBitsPixel );
if( cClrBits == 1 )
cClrBits = 1;
else if( cClrBits <= 4 )
cClrBits = 4;
else if( cClrBits <= 8 )
cClrBits = 8;
else if( cClrBits <= 16 )
cClrBits = 16;
else if( cClrBits <= 24 )
cClrBits = 24;
else
cClrBits = 32;
/*
* Allocate memory for the BITMAPINFO structure. (This structure
* contains a BITMAPINFOHEADER structure and an array of RGBQUAD data
* structures.)
*/
if( cClrBits < 16 )
pbmi = (PBITMAPINFO)LocalAlloc( LPTR, sizeof( BITMAPINFOHEADER ) + sizeof( RGBQUAD ) * ( 2^cClrBits ) );
/* There is no RGBQUAD array for the 24-bit-per-pixel format. */
else
pbmi = (PBITMAPINFO)LocalAlloc( LPTR, sizeof( BITMAPINFOHEADER ) );
/* Initialize the fields in the BITMAPINFO structure.*/
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if( cClrBits < 24 )
pbmi->bmiHeader.biClrUsed = 2 ^ cClrBits;
/*If the bitmap is not compressed, set the BI_RGB flag.*/
pbmi->bmiHeader.biCompression = BI_RGB;
/*
* Compute the number of bytes in the array of color
* indices and store the result in biSizeImage.
*/
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7 ) / 8
* pbmi->bmiHeader.biHeight
* cClrBits;
/*
* Set biClrImportant to 0, indicating that all of the
* device colors are important.
*/
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}