forked from chrisguo/beijing_fushengji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dib256.cpp
executable file
·525 lines (432 loc) · 14.3 KB
/
dib256.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
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Jörg König
// All rights reserved
//
// This file is part of the completely free tetris clone "CGTetris".
//
// This is free software.
// You may redistribute it by any means providing it is not sold for profit
// without the authors written consent.
//
// No warrantee of any kind, expressed or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date. I can be reached as follows:
// [email protected] (company site)
// [email protected] (private site)
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "dib256.h"
#include "dibpal.h"
#define PADWIDTH(x) (((x)*8 + 31) & (~31))/8
CDIBitmap :: CDIBitmap()
: m_pInfo(0)
, m_pPixels(0)
, m_pPal(0)
, m_bIsPadded(FALSE)
{
}
CDIBitmap :: ~CDIBitmap() {
delete [] (BYTE*)m_pInfo;
delete [] m_pPixels;
delete m_pPal;
}
void CDIBitmap :: DestroyBitmap() {
delete [] (BYTE*)m_pInfo;
delete [] m_pPixels;
delete m_pPal;
m_pInfo = 0;
m_pPixels = 0;
m_pPal = 0;
}
BOOL CDIBitmap :: CreateFromBitmap( CDC * pDC, CBitmap * pSrcBitmap ) {
ASSERT_VALID(pSrcBitmap);
ASSERT_VALID(pDC);
try {
BITMAP bmHdr;
// Get the pSrcBitmap info
pSrcBitmap->GetObject(sizeof(BITMAP), &bmHdr);
// Reallocate space for the image data
if( m_pPixels ) {
delete [] m_pPixels;
m_pPixels = 0;
}
DWORD dwWidth;
if (bmHdr.bmBitsPixel > 8)
dwWidth = PADWIDTH(bmHdr.bmWidth * 3);
else
dwWidth = PADWIDTH(bmHdr.bmWidth);
m_pPixels = new BYTE[dwWidth*bmHdr.bmHeight];
if( !m_pPixels )
throw TEXT("could not allocate data storage\n");
// Set the appropriate number of colors base on BITMAP structure info
WORD wColors;
switch( bmHdr.bmBitsPixel ) {
case 1 :
wColors = 2;
break;
case 4 :
wColors = 16;
break;
case 8 :
wColors = 256;
break;
default :
wColors = 0;
break;
}
// Re-allocate and populate BITMAPINFO structure
if( m_pInfo ) {
delete [] (BYTE*)m_pInfo;
m_pInfo = 0;
}
m_pInfo = (BITMAPINFO*)new BYTE[sizeof(BITMAPINFOHEADER) + wColors*sizeof(RGBQUAD)];
if( !m_pInfo )
throw TEXT("could not allocate BITMAPINFO struct\n");
// Populate BITMAPINFO header info
m_pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pInfo->bmiHeader.biWidth = bmHdr.bmWidth;
m_pInfo->bmiHeader.biHeight = bmHdr.bmHeight;
m_pInfo->bmiHeader.biPlanes = bmHdr.bmPlanes;
if( bmHdr.bmBitsPixel > 8 )
m_pInfo->bmiHeader.biBitCount = 24;
else
m_pInfo->bmiHeader.biBitCount = bmHdr.bmBitsPixel;
m_pInfo->bmiHeader.biCompression = BI_RGB;
m_pInfo->bmiHeader.biSizeImage = ((((bmHdr.bmWidth * bmHdr.bmBitsPixel) + 31) & ~31) >> 3) * bmHdr.bmHeight;
m_pInfo->bmiHeader.biXPelsPerMeter = 0;
m_pInfo->bmiHeader.biYPelsPerMeter = 0;
m_pInfo->bmiHeader.biClrUsed = 0;
m_pInfo->bmiHeader.biClrImportant = 0;
// Now actually get the bits
int test = ::GetDIBits(pDC->GetSafeHdc(), (HBITMAP)pSrcBitmap->GetSafeHandle(),
0, (WORD)bmHdr.bmHeight, m_pPixels, m_pInfo, DIB_RGB_COLORS);
// check that we scanned in the correct number of bitmap lines
if( test != (int)bmHdr.bmHeight )
throw TEXT("call to GetDIBits did not return full number of requested scan lines\n");
CreatePalette();
m_bIsPadded = FALSE;
#ifdef _DEBUG
} catch( TCHAR * psz ) {
TRACE1("CDIBitmap::CreateFromBitmap(): %s\n", psz);
#else
} catch( TCHAR * ) {
#endif
if( m_pPixels ) {
delete [] m_pPixels;
m_pPixels = 0;
}
if( m_pInfo ) {
delete [] (BYTE*) m_pInfo;
m_pInfo = 0;
}
return FALSE;
}
return TRUE;
}
BOOL CDIBitmap :: LoadResource(LPCTSTR pszID) {
HBITMAP hBmp = (HBITMAP)::LoadImage(
AfxGetInstanceHandle(),
pszID, IMAGE_BITMAP,
0,0, LR_CREATEDIBSECTION
);
if( hBmp == 0 )
return FALSE;
CBitmap bmp;
bmp.Attach(hBmp);
CClientDC cdc( CWnd::GetDesktopWindow() );
BOOL bRet = CreateFromBitmap( &cdc, &bmp );
bmp.DeleteObject();
return bRet;
}
BOOL CDIBitmap :: Load( CFile* pFile ) {
ASSERT( pFile );
BOOL fReturn = TRUE;
try {
delete [] (BYTE*)m_pInfo;
delete [] m_pPixels;
m_pInfo = 0;
m_pPixels = 0;
DWORD dwStart = pFile->GetPosition();
//
// Check to make sure we have a bitmap. The first two bytes must
// be 'B' and 'M'.
BITMAPFILEHEADER fileHeader;
pFile->Read(&fileHeader, sizeof(fileHeader));
if( fileHeader.bfType != 0x4D42 )
throw TEXT("Error:Unexpected file type, not a DIB\n");
BITMAPINFOHEADER infoHeader;
pFile->Read( &infoHeader, sizeof(infoHeader) );
if( infoHeader.biSize != sizeof(infoHeader) )
throw TEXT("Error:OS2 PM BMP Format not supported\n");
// Store the sizes of the DIB structures
int cPaletteEntries = GetPalEntries( infoHeader );
int cColorTable = 256 * sizeof(RGBQUAD);
int cInfo = sizeof(BITMAPINFOHEADER) + cColorTable;
int cPixels = fileHeader.bfSize - fileHeader.bfOffBits;
//
// Allocate space for a new bitmap info header, and copy
// the info header that was loaded from the file. Read the
// the file and store the results in the color table.
m_pInfo = (BITMAPINFO*)new BYTE[cInfo];
memcpy( m_pInfo, &infoHeader, sizeof(BITMAPINFOHEADER) );
pFile->Read( ((BYTE*)m_pInfo) + sizeof(BITMAPINFOHEADER),
cColorTable );
//
// Allocate space for the pixel area, and load the pixel
// info from the file.
m_pPixels = new BYTE[cPixels];
pFile->Seek(dwStart + fileHeader.bfOffBits, CFile::begin);
pFile->Read( m_pPixels, cPixels );
CreatePalette();
m_bIsPadded = TRUE;
#ifdef _DEBUG
} catch( TCHAR * psz ) {
TRACE( psz );
#else
} catch( TCHAR * ) {
#endif
fReturn = FALSE;
}
return fReturn;
}
BOOL CDIBitmap :: Load( const CString & strFilename ) {
CFile file;
if( file.Open( strFilename, CFile::modeRead ) )
return Load( &file );
return FALSE;
}
BOOL CDIBitmap :: Save( const CString & strFileName ) {
ASSERT(! strFileName.IsEmpty());
CFile File;
if( !File.Open(strFileName, CFile::modeCreate|CFile::modeWrite) ) {
TRACE1("CDIBitmap::Save(): Failed to open file %s for writing.\n", LPCSTR(strFileName));
return FALSE;
}
return Save( &File );
}
// Does not open or close pFile. Assumes
// caller will do it.
BOOL CDIBitmap :: Save( CFile * pFile ) {
ASSERT_VALID( pFile );
ASSERT( m_pInfo );
ASSERT( m_pPixels );
BITMAPFILEHEADER bmfHdr;
DWORD dwPadWidth = PADWIDTH(GetWidth());
// Make sure bitmap data is in padded format
PadBits();
bmfHdr.bfType = 0x4D42;
// initialize to BitmapInfo size
DWORD dwImageSize= m_pInfo->bmiHeader.biSize;
// Add in palette size
WORD wColors = GetColorCount();
WORD wPaletteSize = (WORD)(wColors*sizeof(RGBQUAD));
dwImageSize += wPaletteSize;
// Add in size of actual bit array
dwImageSize += PADWIDTH((GetWidth()) * DWORD(m_pInfo->bmiHeader.biBitCount)/8) * GetHeight();
m_pInfo->bmiHeader.biSizeImage = 0;
bmfHdr.bfSize = dwImageSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + m_pInfo->bmiHeader.biSize + wPaletteSize;
pFile->Write(&bmfHdr, sizeof(BITMAPFILEHEADER));
pFile->Write(m_pInfo, sizeof(BITMAPINFO) + (wColors-1)*sizeof(RGBQUAD));
pFile->WriteHuge(m_pPixels,
DWORD((dwPadWidth*(DWORD)m_pInfo->bmiHeader.biBitCount*GetHeight())/8) );
return TRUE;
}
BOOL CDIBitmap :: CreatePalette() {
if( m_pPal )
delete m_pPal;
m_pPal = 0;
ASSERT( m_pInfo );
// We only need a palette, if there are <= 256 colors.
// otherwise we would bomb the memory.
if( m_pInfo->bmiHeader.biBitCount <= 8 )
m_pPal = new CBmpPalette(this);
return m_pPal ? TRUE : FALSE;
}
void CDIBitmap :: ClearPalette() {
if( m_pPal )
delete m_pPal;
m_pPal = 0;
}
void CDIBitmap :: DrawDIB( CDC* pDC, int x, int y ) {
DrawDIB( pDC, x, y, GetWidth(), GetHeight() );
}
//
// DrawDib uses StretchDIBits to display the bitmap.
void CDIBitmap :: DrawDIB( CDC* pDC, int x, int y, int width, int height ) {
ASSERT( pDC );
HDC hdc = pDC->GetSafeHdc();
CPalette * pOldPal = 0;
if( m_pPal ) {
pOldPal = pDC->SelectPalette( m_pPal, FALSE );
pDC->RealizePalette();
// Make sure to use the stretching mode best for color pictures
pDC->SetStretchBltMode(COLORONCOLOR);
}
if( m_pInfo )
StretchDIBits( hdc,
x,
y,
width,
height,
0,
0,
GetWidth(),
GetHeight(),
GetPixelPtr(),
GetHeaderPtr(),
DIB_RGB_COLORS,
SRCCOPY );
if( m_pPal )
pDC->SelectPalette( pOldPal, FALSE );
}
int CDIBitmap :: DrawDIB( CDC * pDC, CRect & rectDC, CRect & rectDIB ) {
ASSERT( pDC );
HDC hdc = pDC->GetSafeHdc();
CPalette * pOldPal = 0;
if( m_pPal ) {
pOldPal = pDC->SelectPalette( m_pPal, FALSE );
pDC->RealizePalette();
// Make sure to use the stretching mode best for color pictures
pDC->SetStretchBltMode(COLORONCOLOR);
}
int nRet = 0;
if( m_pInfo )
nRet = SetDIBitsToDevice(
hdc, // device
rectDC.left, // DestX
rectDC.top, // DestY
rectDC.Width(), // DestWidth
rectDC.Height(), // DestHeight
rectDIB.left, // SrcX
GetHeight() -
rectDIB.top -
rectDIB.Height(), // SrcY
0, // StartScan
GetHeight(), // NumScans
GetPixelPtr(), // color data
GetHeaderPtr(), // header data
DIB_RGB_COLORS // color usage
);
if( m_pPal )
pDC->SelectPalette( pOldPal, FALSE );
return nRet;
}
BITMAPINFO * CDIBitmap :: GetHeaderPtr() const {
ASSERT( m_pInfo );
ASSERT( m_pPixels );
return m_pInfo;
}
RGBQUAD * CDIBitmap :: GetColorTablePtr() const {
ASSERT( m_pInfo );
ASSERT( m_pPixels );
RGBQUAD* pColorTable = 0;
if( m_pInfo != 0 ) {
int cOffset = sizeof(BITMAPINFOHEADER);
pColorTable = (RGBQUAD*)(((BYTE*)(m_pInfo)) + cOffset);
}
return pColorTable;
}
BYTE * CDIBitmap :: GetPixelPtr() const {
ASSERT( m_pInfo );
ASSERT( m_pPixels );
return m_pPixels;
}
int CDIBitmap :: GetWidth() const {
ASSERT( m_pInfo );
return m_pInfo->bmiHeader.biWidth;
}
int CDIBitmap :: GetHeight() const {
ASSERT( m_pInfo );
return m_pInfo->bmiHeader.biHeight;
}
WORD CDIBitmap :: GetColorCount() const {
ASSERT( m_pInfo );
switch( m_pInfo->bmiHeader.biBitCount ) {
case 1: return 2;
case 4: return 16;
case 8: return 256;
default: return 0;
}
}
int CDIBitmap :: GetPalEntries() const {
ASSERT( m_pInfo );
return GetPalEntries( *(BITMAPINFOHEADER*)m_pInfo );
}
int CDIBitmap :: GetPalEntries( BITMAPINFOHEADER& infoHeader ) const {
int nReturn;
if( infoHeader.biClrUsed == 0 )
nReturn = ( 1 << infoHeader.biBitCount );
else
nReturn = infoHeader.biClrUsed;
return nReturn;
}
DWORD CDIBitmap :: GetBitsPerPixel() const {
ASSERT( m_pInfo );
return m_pInfo->bmiHeader.biBitCount;
}
DWORD CDIBitmap :: LastByte( DWORD dwBitsPerPixel, DWORD dwPixels ) const {
register DWORD dwBits = dwBitsPerPixel * dwPixels;
register DWORD numBytes = dwBits / 8;
register DWORD extraBits = dwBits - numBytes * 8;
return (extraBits % 8) ? numBytes+1 : numBytes;
}
DWORD CDIBitmap :: GetBytesPerLine( DWORD dwBitsPerPixel, DWORD dwWidth ) const {
DWORD dwBits = dwBitsPerPixel * dwWidth;
if( (dwBits % 32) == 0 )
return (dwBits/8); // already DWORD aligned, no padding needed
DWORD dwPadBits = 32 - (dwBits % 32);
return (dwBits/8 + dwPadBits/8 + (((dwPadBits % 8) > 0) ? 1 : 0));
}
BOOL CDIBitmap :: PadBits() {
if( m_bIsPadded )
return TRUE;
// dwAdjust used when bits per pixel spreads over more than 1 byte
DWORD dwAdjust = 1, dwOffset = 0, dwPadOffset=0;
BOOL bIsOdd = FALSE;
dwPadOffset = GetBytesPerLine(GetBitsPerPixel(), GetWidth());
dwOffset = LastByte(GetBitsPerPixel(), GetWidth());
if( dwPadOffset == dwOffset )
return TRUE;
BYTE * pTemp = new BYTE [GetWidth()*dwAdjust];
if( !pTemp ) {
TRACE1("CDIBitmap::PadBits(): could not allocate row of width %d.\n", GetWidth());
return FALSE;
}
// enough space has already been allocated for the bit array to
// include the padding, so we just need to shift rows around.
// This will pad each "row" on a DWORD alignment.
for( DWORD row = GetHeight()-1 ; row>0 ; --row ) {
CopyMemory((void *)pTemp, (const void *)(m_pPixels + (row*dwOffset)), dwOffset );
CopyMemory((void *)(m_pPixels + (row*dwPadOffset)), (const void *)pTemp, dwOffset);
}
delete [] pTemp;
return TRUE;
}
BOOL CDIBitmap::UnPadBits() {
if( ! m_bIsPadded )
return TRUE;
DWORD dwAdjust = 1;
BOOL bIsOdd = FALSE;
DWORD dwPadOffset = GetBytesPerLine(GetBitsPerPixel(), GetWidth());
DWORD dwOffset = LastByte(GetBitsPerPixel(), GetWidth());
BYTE * pTemp = new BYTE [dwOffset];
if( !pTemp ) {
TRACE1("CDIBitmap::UnPadBits() could not allocate row of width %d.\n", GetWidth());
return FALSE;
}
// enough space has already been allocated for the bit array to
// include the padding, so we just need to shift rows around.
for( DWORD row=1 ; row < DWORD(GetHeight()); ++row ) {
CopyMemory((void *)pTemp, (const void *)(m_pPixels + row*(dwPadOffset)), dwOffset);
CopyMemory((void *)(m_pPixels + (row*dwOffset)), (const void *)pTemp, dwOffset);
}
delete [] pTemp;
return TRUE;
}