-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDIB.CPP
648 lines (542 loc) · 16.4 KB
/
DIB.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
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 1998 Jorge Lodos ([email protected])
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// dib.cpp
//
#include "stdafx.h"
#include "dib.h"
#include <windowsx.h>
#include <afxadv.h>
#include <io.h>
#include <errno.h>
/////////////////////////////////////////////////////////////////////////////
// CDib
IMPLEMENT_DYNAMIC(CDib, CObject)
CDib::CDib()
{
m_pBMI = NULL;
m_pBits = NULL;
m_pPalette = NULL;
}
CDib::~CDib()
{
Free();
}
void CDib::Free()
{
// Make sure all member data that might have been allocated is freed.
if (m_pBits)
{
GlobalFreePtr(m_pBits);
m_pBits = NULL;
}
if (m_pBMI)
{
GlobalFreePtr(m_pBMI);
m_pBMI = NULL;
}
if (m_pPalette)
{
m_pPalette->DeleteObject();
delete m_pPalette;
m_pPalette = NULL;
}
}
/*************************************************************************
*
* Paint()
*
* Parameters:
*
* HDC hDC - DC to do output to
*
* LPRECT lpDCRect - rectangle on DC to do output to
*
* LPRECT lpDIBRect - rectangle of DIB to output into lpDCRect
*
* CPalette* pPal - pointer to CPalette containing DIB's palette
*
* Return Value:
*
* BOOL - TRUE if DIB was drawn, FALSE otherwise
*
* Description:
* Painting routine for a DIB. Calls StretchDIBits() or
* SetDIBitsToDevice() to paint the DIB. The DIB is
* output to the specified DC, at the coordinates given
* in lpDCRect. The area of the DIB to be output is
* given by lpDIBRect.
*
************************************************************************/
BOOL CDib::Paint(HDC hDC, LPRECT lpDCRect, LPRECT lpDIBRect) const
{
if (!m_pBMI)
return FALSE;
HPALETTE hPal = NULL; // Our DIB's palette
HPALETTE hOldPal = NULL; // Previous palette
// Get the DIB's palette, then select it into DC
if (m_pPalette != NULL)
{
hPal = (HPALETTE) m_pPalette->m_hObject;
// Select as background since we have
// already realized in forground if needed
hOldPal = ::SelectPalette(hDC, hPal, TRUE);
}
/* Make sure to use the stretching mode best for color pictures */
::SetStretchBltMode(hDC, COLORONCOLOR);
/* Determine whether to call StretchDIBits() or SetDIBitsToDevice() */
BOOL bSuccess;
if ((RECTWIDTH(lpDCRect) == RECTWIDTH(lpDIBRect)) &&
(RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDIBRect)))
bSuccess = ::SetDIBitsToDevice(hDC, // hDC
lpDCRect->left, // DestX
lpDCRect->top, // DestY
RECTWIDTH(lpDCRect), // nDestWidth
RECTHEIGHT(lpDCRect), // nDestHeight
lpDIBRect->left, // SrcX
(int)Height() -
lpDIBRect->top -
RECTHEIGHT(lpDIBRect), // SrcY
0, // nStartScan
(WORD)Height(), // nNumScans
m_pBits, // lpBits
m_pBMI, // lpBitsInfo
DIB_RGB_COLORS); // wUsage
else
bSuccess = ::StretchDIBits(hDC, // hDC
lpDCRect->left, // DestX
lpDCRect->top, // DestY
RECTWIDTH(lpDCRect), // nDestWidth
RECTHEIGHT(lpDCRect), // nDestHeight
lpDIBRect->left, // SrcX
lpDIBRect->top, // SrcY
RECTWIDTH(lpDIBRect), // wSrcWidth
RECTHEIGHT(lpDIBRect), // wSrcHeight
m_pBits, // lpBits
m_pBMI, // lpBitsInfo
DIB_RGB_COLORS, // wUsage
SRCCOPY); // dwROP
/* Reselect old palette */
if (hOldPal != NULL)
{
::SelectPalette(hDC, hOldPal, TRUE);
}
return bSuccess;
}
/*************************************************************************
*
* CreatePalette()
*
* Return Value:
*
* TRUE if succesfull, FALSE otherwise
*
* Description:
*
* This function creates a palette from a DIB by allocating memory for the
* logical palette, reading and storing the colors from the DIB's color table
* into the logical palette, creating a palette from this logical palette,
* and then returning the palette's handle. This allows the DIB to be
* displayed using the best possible colors (important for DIBs with 256 or
* more colors).
*
************************************************************************/
BOOL CDib::CreatePalette()
{
if (!m_pBMI)
return FALSE;
//get the number of colors in the DIB
WORD wNumColors = NumColors();
if (wNumColors != 0)
{
// allocate memory block for logical palette
HANDLE hLogPal = ::GlobalAlloc(GHND, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY)*wNumColors);
// if not enough memory, clean up and return NULL
if (hLogPal == 0)
return FALSE;
LPLOGPALETTE lpPal = (LPLOGPALETTE)::GlobalLock((HGLOBAL)hLogPal);
// set version and number of palette entries
lpPal->palVersion = PALVERSION;
lpPal->palNumEntries = (WORD)wNumColors;
for (int i = 0; i < (int)wNumColors; i++)
{
lpPal->palPalEntry[i].peRed = m_pBMI->bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen = m_pBMI->bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = m_pBMI->bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
/* create the palette and get handle to it */
if (m_pPalette)
{
m_pPalette->DeleteObject();
delete m_pPalette;
}
m_pPalette = new CPalette;
BOOL bResult = m_pPalette->CreatePalette(lpPal);
::GlobalUnlock((HGLOBAL) hLogPal);
::GlobalFree((HGLOBAL) hLogPal);
return bResult;
}
return TRUE;
}
/*************************************************************************
*
* Width()
*
* Return Value:
*
* DWORD - width of the DIB
*
* Description:
*
* This function gets the width of the DIB from the BITMAPINFOHEADER
* width field
*
************************************************************************/
DWORD CDib::Width() const
{
if (!m_pBMI)
return 0;
/* return the DIB width */
return m_pBMI->bmiHeader.biWidth;
}
/*************************************************************************
*
* Height()
*
* Return Value:
*
* DWORD - height of the DIB
*
* Description:
*
* This function gets the height of the DIB from the BITMAPINFOHEADER
* height field
*
************************************************************************/
DWORD CDib::Height() const
{
if (!m_pBMI)
return 0;
/* return the DIB height */
return m_pBMI->bmiHeader.biHeight;
}
/*************************************************************************
*
* PaletteSize()
*
* Return Value:
*
* WORD - size of the color palette of the DIB
*
* Description:
*
* This function gets the size required to store the DIB's palette by
* multiplying the number of colors by the size of an RGBQUAD
*
************************************************************************/
WORD CDib::PaletteSize() const
{
if (!m_pBMI)
return 0;
return NumColors() * sizeof(RGBQUAD);
}
/*************************************************************************
*
* NumColors()
*
* Return Value:
*
* WORD - number of colors in the color table
*
* Description:
*
* This function calculates the number of colors in the DIB's color table
* by finding the bits per pixel for the DIB (whether Win3.0 or other-style
* DIB). If bits per pixel is 1: colors=2, if 4: colors=16, if 8: colors=256,
* if 24, no colors in color table.
*
************************************************************************/
WORD CDib::NumColors() const
{
if (!m_pBMI)
return 0;
WORD wBitCount; // DIB bit count
/* The number of colors in the color table can be less than
* the number of bits per pixel allows for (i.e. lpbi->biClrUsed
* can be set to some value).
* If this is the case, return the appropriate value.
*/
DWORD dwClrUsed;
dwClrUsed = m_pBMI->bmiHeader.biClrUsed;
if (dwClrUsed != 0)
return (WORD)dwClrUsed;
/* Calculate the number of colors in the color table based on
* the number of bits per pixel for the DIB.
*/
wBitCount = m_pBMI->bmiHeader.biBitCount;
/* return number of colors based on bits per pixel */
switch (wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
/*************************************************************************
*
* Save()
*
* Saves the specified DIB into the specified CFile. The CFile
* is opened and closed by the caller.
*
* Parameters:
*
* CFile& file - open CFile used to save DIB
*
* Return value: Number of saved bytes or CFileException
*
*************************************************************************/
DWORD CDib::Save(CFile& file) const
{
BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
DWORD dwDIBSize;
if (m_pBMI == NULL)
return 0;
// Fill in the fields of the file header
// Fill in file type (first 2 bytes must be "BM" for a bitmap)
bmfHdr.bfType = DIB_HEADER_MARKER; // "BM"
// Calculating the size of the DIB is a bit tricky (if we want to
// do it right). The easiest way to do this is to call GlobalSize()
// on our global handle, but since the size of our global memory may have
// been padded a few bytes, we may end up writing out a few too
// many bytes to the file (which may cause problems with some apps).
//
// So, instead let's calculate the size manually (if we can)
//
// First, find size of header plus size of color table. Since the
// first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER conains
// the size of the structure, let's use this.
dwDIBSize = *(LPDWORD)&m_pBMI->bmiHeader + PaletteSize(); // Partial Calculation
// Now calculate the size of the image
if ((m_pBMI->bmiHeader.biCompression == BI_RLE8) || (m_pBMI->bmiHeader.biCompression == BI_RLE4))
{
// It's an RLE bitmap, we can't calculate size, so trust the
// biSizeImage field
dwDIBSize += m_pBMI->bmiHeader.biSizeImage;
}
else
{
DWORD dwBmBitsSize; // Size of Bitmap Bits only
// It's not RLE, so size is Width (DWORD aligned) * Height
dwBmBitsSize = WIDTHBYTES((m_pBMI->bmiHeader.biWidth)*((DWORD)m_pBMI->bmiHeader.biBitCount)) * m_pBMI->bmiHeader.biHeight;
dwDIBSize += dwBmBitsSize;
// Now, since we have calculated the correct size, why don't we
// fill in the biSizeImage field (this will fix any .BMP files which
// have this field incorrect).
m_pBMI->bmiHeader.biSizeImage = dwBmBitsSize;
}
// Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
/*
* Now, calculate the offset the actual bitmap bits will be in
* the file -- It's the Bitmap file header plus the DIB header,
* plus the size of the color table.
*/
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + m_pBMI->bmiHeader.biSize + PaletteSize();
// Write the file header
file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
DWORD dwBytesSaved = sizeof(BITMAPFILEHEADER);
// Write the DIB header
UINT nCount = sizeof(BITMAPINFO) + (NumColors()-1)*sizeof(RGBQUAD);
dwBytesSaved += nCount;
file.Write(m_pBMI, nCount);
// Write the DIB bits
DWORD dwBytes = m_pBMI->bmiHeader.biBitCount * Width();
// Calculate the number of bytes per line
if (dwBytes%32 == 0)
dwBytes /= 8;
else
dwBytes = dwBytes/8 + (32-dwBytes%32)/8 + (((32-dwBytes%32)%8 > 0) ? 1 : 0);
nCount = dwBytes * Height();
dwBytesSaved += nCount;
file.Write(m_pBits, nCount);
return dwBytesSaved;
}
/*************************************************************************
Function: Read (CFile&)
Purpose: Reads in the specified DIB file into a global chunk of
memory.
Returns: Number of read bytes.
*************************************************************************/
DWORD CDib::Read(CFile& file)
{
// Ensures no memory leaks will occur
Free();
BITMAPFILEHEADER bmfHeader;
// Go read the DIB file header and check if it's valid.
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return 0;
if (bmfHeader.bfType != DIB_HEADER_MARKER)
return 0;
DWORD dwReadBytes = sizeof(bmfHeader);
DWORD dwHeadSize=bmfHeader.bfOffBits-sizeof(BITMAPFILEHEADER) + 256*sizeof(RGBQUAD);
// Allocate memory for DIB
m_pBMI = (LPBITMAPINFO)GlobalAllocPtr(GHND, bmfHeader.bfOffBits-sizeof(BITMAPFILEHEADER) + 256*sizeof(RGBQUAD));
if (m_pBMI == 0)
return 0;
// Read header.
if (file.Read(m_pBMI, bmfHeader.bfOffBits-sizeof(BITMAPFILEHEADER)) != (UINT)(bmfHeader.bfOffBits-sizeof(BITMAPFILEHEADER)))
{
GlobalFreePtr(m_pBMI);
m_pBMI = NULL;
return 0;
}
dwReadBytes += bmfHeader.bfOffBits-sizeof(BITMAPFILEHEADER);
// AF TODO
DWORD dwLength = (DWORD)file.GetLength();
// Go read the bits.
m_pBits = (LPBYTE)GlobalAllocPtr(GHND, dwLength - bmfHeader.bfOffBits);
if (m_pBits == 0)
{
GlobalFreePtr(m_pBMI);
m_pBMI = NULL;
return 0;
}
if (file.Read(m_pBits, dwLength-bmfHeader.bfOffBits) != (dwLength - bmfHeader.bfOffBits))
{
GlobalFreePtr(m_pBMI);
m_pBMI = NULL;
GlobalFreePtr(m_pBits);
m_pBits = NULL;
return 0;
}
dwReadBytes += dwLength - bmfHeader.bfOffBits;
CreatePalette();
return dwReadBytes;
}
#ifdef _DEBUG
void CDib::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif
//////////////////////////////////////////////////////////////////////////
//// Clipboard support
//---------------------------------------------------------------------
//
// Function: CopyToHandle
//
// Purpose: Makes a copy of the DIB to a global memory block. Returns
// a handle to the new memory block (NULL on error).
//
// Returns: Handle to new global memory block.
//
//---------------------------------------------------------------------
HGLOBAL CDib::CopyToHandle() const
{
CSharedFile file;
try
{
if (Save(file)==0)
return 0;
}
catch (CFileException* e)
{
e->Delete();
return 0;
}
return file.Detach();
}
//---------------------------------------------------------------------
//
// Function: ReadFromHandle
//
// Purpose: Initializes from the given global memory block.
//
// Returns: Number of read bytes.
//
//---------------------------------------------------------------------
DWORD CDib::ReadFromHandle(HGLOBAL hGlobal)
{
CSharedFile file;
file.SetHandle(hGlobal, FALSE);
DWORD dwResult = Read(file);
file.Detach();
return dwResult;
}
//////////////////////////////////////////////////////////////////////////
//// Serialization support
void CDib::Serialize(CArchive& ar)
{
CFile* pFile = ar.GetFile();
ASSERT(pFile != NULL);
if (ar.IsStoring())
{ // storing code
Save(*pFile);
}
else
{ // loading code
Read(*pFile);
}
}
DWORD CDib::LoadFromResource(LPCTSTR lpszResourceName)
{
DWORD dwReadBytes=0;
// Ensures no memory leaks will occur
Free();
ASSERT_VALID(this);
HINSTANCE hInst=AfxGetResourceHandle();
if(hInst==NULL )
return 0;
HRSRC hRes = FindResource(hInst,lpszResourceName,RT_BITMAP); // Search the Bitmap HGLOBAL hGlobal=NULL, hTemp = LoadResource(m_hInst, hRes); // Load it
if( NULL == hRes )
return 0;
HGLOBAL hTemp = LoadResource(hInst, hRes);
if( NULL == hTemp )
return FALSE;
BITMAPINFOHEADER* lpRes = (BITMAPINFOHEADER*)LockResource(hTemp); // Get the resource handle to the Bitmap
if( NULL == lpRes)
return 0;
DWORD dwHeaderSize=sizeof(BITMAPINFOHEADER);
DWORD dwImageSize=lpRes->biSizeImage;
if (lpRes->biBitCount <= 8)
{
int nColors=1<<lpRes->biBitCount;
dwHeaderSize+=nColors*sizeof(RGBQUAD);
}
// Allocate memory for DIB
m_pBMI = (LPBITMAPINFO)GlobalAllocPtr(GHND,dwHeaderSize);
m_pBits = (BYTE*)GlobalAllocPtr(GHND,dwImageSize);
memcpy(m_pBMI,lpRes,dwHeaderSize);
// Copy the image data
memcpy(m_pBits,((BYTE*)lpRes)+dwHeaderSize,dwImageSize);
CreatePalette();
return dwImageSize+dwHeaderSize;
}