-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDib.cpp
More file actions
204 lines (163 loc) · 4.33 KB
/
Copy pathDib.cpp
File metadata and controls
204 lines (163 loc) · 4.33 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
// Dib.cpp: implementation of the CDib class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Dib.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDib::CDib()
{
m_hDrawDib=NULL;
m_pDib=NULL;
}
CDib::~CDib()
{
Close();
}
void CDib::Draw(CDC *pDC,int nWidth, int nHeight)
{
if(m_pDib!=NULL)
{
ASSERT(IsValid());
DrawDibRealize(m_hDrawDib,pDC->GetSafeHdc(),TRUE);
DrawDibDraw(m_hDrawDib,pDC->GetSafeHdc(),
0, //desktop left
0, //desktop top
nWidth,
nHeight,
(BITMAPINFOHEADER *)m_pDib,
(LPVOID) GetBits(),
0, //source left
0, //source top
((BITMAPINFOHEADER *)m_pDib)->biWidth,
((BITMAPINFOHEADER *)m_pDib)->biHeight,
DDF_BACKGROUNDPAL);
}
}
CSize CDib::GetSize()
{
return CSize(((BITMAPINFOHEADER *)m_pDib)->biWidth,
((BITMAPINFOHEADER *)m_pDib)->biHeight);
}
LONG CDib::GetWidth()
{
return ((BITMAPINFOHEADER *)m_pDib)->biWidth;
}
LONG CDib::GetHeight()
{
return ((BITMAPINFOHEADER *)m_pDib)->biHeight;
}
void CDib::Close()
{
if(m_hDrawDib!=NULL)
{
DrawDibClose(m_hDrawDib);
m_hDrawDib=NULL;
}
if(m_pDib!=NULL)
{
delete m_pDib;
m_pDib=NULL;
}
}
BOOL CDib::Open(const char * pzFileName)
{
// BITMAPFILEHEADER bmpFileHeader;
CFile file;
int nBmpFileHeaderSize;
Close();
//drawdibopen initialize the diradib library and
//returns a handle for all drawdib operations
if(!(m_hDrawDib=DrawDibOpen()))
goto exit;
//open and read the DIB file header
nBmpFileHeaderSize=sizeof(BITMAPFILEHEADER);
if(!file.Open(pzFileName,CFile::modeRead | CFile::typeBinary))
goto exit;
if(file.Read((void *)&bmpFileHeader,nBmpFileHeaderSize)!=(UINT)nBmpFileHeaderSize)
goto failure;
//validate the DIB file header by checking the first
//two characters for the signature "BM"
if(bmpFileHeader.bfType!=*((WORD *)"BM"))
goto failure;
//allocate a big chuck of global memory to store the DIB
m_pDib=(BYTE *)new char [bmpFileHeader.bfSize-nBmpFileHeaderSize];
//allocate memory fail
if(!m_pDib)
goto failure;
//read the dib into the buffer at a time using ReadHuge
file.ReadHuge(m_pDib,bmpFileHeader.bfSize-nBmpFileHeaderSize);
if(((BITMAPINFOHEADER *)m_pDib)->biSizeImage==0)
{
//the application that create this bitmap didn't fill
//in the biSizeImage field. Let's fill it
//in even though the DrawDib * functions don't need it.
BITMAPINFOHEADER *pDib=(BITMAPINFOHEADER *)m_pDib;
//scan lines must be DWord aligned, hence the strange bit stuff
pDib->biSizeImage=((((pDib->biWidth*pDib->biBitCount)+31)&~31)>>3)*pDib->biHeight;
}
m_pDibBits=GetBits();
file.Close();
return TRUE;
failure:
file.Close();
exit:
Close();
return FALSE;
}
BOOL CDib::Save(const char * pzFileName)
{
// BITMAPFILEHEADER bmpFileHeader;
CFile file;
int nBmpFileHeaderSize;
//open and read the DIB file header
nBmpFileHeaderSize=sizeof(BITMAPFILEHEADER);
if(!file.Open(pzFileName,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
goto exit;
file.Write(&bmpFileHeader,nBmpFileHeaderSize);
//allocate memory fail
if(!m_pDib)
goto failure;
//read the dib into the buffer at a time using ReadHuge
file.WriteHuge(m_pDib,bmpFileHeader.bfSize-nBmpFileHeaderSize);
file.Close();
return TRUE;
failure:
file.Close();
exit:
return FALSE;
}
BYTE * CDib::GetBits()
{
//the size of the color map is determined by the number
//of RGBQUAD structures presend.
//it also depends on the bit_depth of the Dib
DWORD dwNumColors,dwColorTableSize;
BITMAPINFOHEADER *lpDib=(BITMAPINFOHEADER *)m_pDib;
WORD wBitCount=lpDib->biBitCount;
if(lpDib->biSize>=36)
dwNumColors=lpDib->biClrUsed;
else
dwNumColors=0;
if(dwNumColors==0)
{
if(wBitCount!=24)
dwNumColors=1L<<wBitCount;
else
dwNumColors=0;
}
dwColorTableSize=dwNumColors*sizeof(RGBQUAD);
return m_pDib+lpDib->biSize+dwColorTableSize;
}
int CDib::GetBiBitCount()
{
if(m_pDib!=NULL)
return ((BITMAPINFOHEADER *)m_pDib)->biBitCount;
return 0;
}