-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_GlobalCommon.cpp
More file actions
403 lines (382 loc) · 11.8 KB
/
_GlobalCommon.cpp
File metadata and controls
403 lines (382 loc) · 11.8 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
//GlobalCommon.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "_GlobalCommon.h"
/**
功能: 从图像文件中建造DIB类
参数: strBmpFile --- 需要打开的BMP文件名
返回:文件缓冲区指针 (NULL表示失败)
**/
char *OpenBMPfile(CString strBmpFile)
{
CFile hFile;
if( !hFile.Open(strBmpFile,CFile::modeRead|CFile::typeBinary) )
{
AfxMessageBox("Failed to open the BMP file");
return( NULL );
}
/**/
// if( hFile.Seek(0L,CFile::begin) != 0L )
// {
// return( NULL );
// }
/**/
long lFileSize = (long)hFile.Seek(0L, CFile::end);
char *pFileBuf = new char [lFileSize+1];
hFile.Seek(0L, CFile::begin);
hFile.Read(pFileBuf, lFileSize);
hFile.Close();
/**/
BITMAPFILEHEADER *pBmpHead = (BITMAPFILEHEADER *)pFileBuf;
BITMAPINFOHEADER *pBmpInfo = (BITMAPINFOHEADER *)(pFileBuf + sizeof(BITMAPFILEHEADER));
/**/
if( pBmpHead->bfType != 0x4D42 || //"BM"=0x424D
pBmpInfo->biSize != 0x28 || // 位图信息子结构长度(等于40,即0x28)
pBmpInfo->biPlanes != 0x01 ) // 此域必须等于1
{
AfxMessageBox("It isn't a valid BMP file");
return( NULL );
}
/**/
if( pBmpInfo->biCompression != BI_RGB )
{
AfxMessageBox("It is a compressed BMP file");
return( NULL );
}
/**/
if( pBmpInfo->biBitCount != 8 &&
pBmpInfo->biBitCount != 24 )
{
AfxMessageBox("Only 8-bit and 24-bit BMP files are supported");
return( NULL );
}
/**/
return( pFileBuf );
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
BITMAPFILEHEADER *GetDIBHEADER(char *pFileBuffer)
{
char *p = pFileBuffer + 0;
return( (BITMAPFILEHEADER *)p );
}
BITMAPINFOHEADER *GetDIBINFO(char *pFileBuffer)
{
char *p = pFileBuffer + sizeof(BITMAPFILEHEADER);
return( (BITMAPINFOHEADER *)p );
}
char *GetDIBImageData(char *pFileBuffer)
{
const BITMAPFILEHEADER *pBmpHead = GetDIBHEADER(pFileBuffer);
char *p = pFileBuffer + pBmpHead->bfOffBits;
return( p );
}
//return NULL denoting no palette
RGBQUAD *GetDIBPaletteData(char *pFileBuffer,int nEntryNumber[1])
{
char *pPaletteData = NULL;
if( GetColorBits(pFileBuffer) <= 8 )
{
nEntryNumber[0] = 0;
char *pDIBImageData = GetDIBImageData(pFileBuffer);
pPaletteData = pFileBuffer + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
int nNum = (DWORD)(pDIBImageData - pPaletteData) / sizeof(RGBQUAD);
int iUsedColors = (int)GetDIBINFO(pFileBuffer)->biClrUsed;
if( nNum > 0 && (int)iUsedColors > 0 )
nEntryNumber[0] = min(nNum,(int)iUsedColors);
else
pPaletteData = NULL;
}
return( (RGBQUAD *)pPaletteData );
}
int GetImageWidth(char *pFileBuffer)
{
BITMAPINFOHEADER *pInfo = GetDIBINFO(pFileBuffer);
return( pInfo->biWidth );
}
int GetImageHeight(char *pFileBuffer)
{
BITMAPINFOHEADER *pInfo = GetDIBINFO(pFileBuffer);
return( pInfo->biHeight );
}
int GetColorBits(char *pFileBuffer)
{
BITMAPINFOHEADER *pInfo = GetDIBINFO(pFileBuffer);
return( pInfo->biBitCount );
}
long GetUsedColors(char *pFileBuffer)
{
BITMAPINFOHEADER *pInfo = GetDIBINFO(pFileBuffer);
return( (long)pInfo->biClrUsed );
}
long GetWidthBytes(char *pFileBuffer)
{
BITMAPINFOHEADER *pInfo = GetDIBINFO(pFileBuffer);
long nBytesPerRow = 4 * ((pInfo->biWidth * pInfo->biBitCount + 31) / 32);
return( nBytesPerRow );
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
void DisplayHeaderMessage(char *pBmpFileBuf)
{
BITMAPFILEHEADER *pBmpHead = GetDIBHEADER(pBmpFileBuf);
BITMAPINFOHEADER *pBmpInfo = GetDIBINFO(pBmpFileBuf);
/**/
char msg[4096];
sprintf(msg,
"bfType (file type) = %4.4X \n"
"bfSize (file length) = %ld \n"
"bfOffBits (offset of bitmap data in bytes) = %ld \n"
"biSize (header structure length should be 40 or 0x28) = %ld \n"
"biWidth (image width) = %ld \n"
"biHeight (image height) = %ld \n"
"biPlanes (must be eaual to 1) = %u \n"
"biBitCount (color/pixel bits) = %u \n"
"biCompression (compressed?) = %ld \n"
"biSizeImage (length of bitmap data in bytes must be the times of 4) = %ld \n"
"biXPelsPerMeter (horizontal resolution of target device in pixels/metre) = %ld \n"
"biYPelsPerMeter (vertical resolution of target device in pixels/metre) = %ld \n"
"biColorUsed (number of colors used in bitmap,0=2**biBitCount) = %ld \n"
"biColorImportant (number of important colors,0=all colors are important) = %ld \n\n"
"The following is additional information: \n"
"Bytes per row in bitmap (nBytesPerRow) = %ld \n"
"Total bytes of bitmap (nImageSizeInByte) = %ld \n"
"Actual pixels per row in bitmap (nPixelsPerRow) = %ld \n"
"Total rows of bitmap (nTotalRows) = %ld \n"
"Total colors (2**biBitCount)(nTotalColors) = %ld \n"
"Used colors (biColorUsed)(nUsedColors) = %ld ",
pBmpHead->bfType,
pBmpHead->bfSize,
pBmpHead->bfOffBits,
pBmpInfo->biSize,
pBmpInfo->biWidth,
pBmpInfo->biHeight,
pBmpInfo->biPlanes,
pBmpInfo->biBitCount,
pBmpInfo->biCompression,
pBmpInfo->biSizeImage,
pBmpInfo->biXPelsPerMeter,
pBmpInfo->biYPelsPerMeter,
pBmpInfo->biClrUsed,
pBmpInfo->biClrImportant,
GetWidthBytes(pBmpFileBuf),
GetWidthBytes(pBmpFileBuf) * GetImageHeight(pBmpFileBuf),
GetImageWidth(pBmpFileBuf),
GetImageHeight(pBmpFileBuf),
1 << GetColorBits(pBmpFileBuf),
GetUsedColors(pBmpFileBuf) );
AfxMessageBox(msg);
}
//Mode = 0, normal display
// 1,2,3, display grayscale image in red, green, blue colors
void DisplayImage(CDC *pDC,char *pBmpFileBuf,int disp_xL,int disp_yL,int disp_Width,int disp_Height,int mode)
{
ASSERT( pDC != NULL );
HDC hDC = pDC->GetSafeHdc();
ASSERT( hDC != 0 );
/**/
int imageWidth = GetImageWidth(pBmpFileBuf);
int imageHeight = GetImageHeight(pBmpFileBuf);
if( disp_Width <= 0 || disp_Height <= 0 )
{
disp_Width = imageWidth;
disp_Height = imageHeight;
}
CRect rect;
CWnd *pWnd = pDC->GetWindow();
pWnd->GetClientRect(&rect);
disp_Width = min(disp_Width, rect.right - disp_xL);
disp_Height = min(disp_Height, rect.bottom - disp_yL);
/**/
BITMAPINFOHEADER *pBitmapInfo = GetDIBINFO(pBmpFileBuf);
char *pDIBImageData = GetDIBImageData(pBmpFileBuf);
/**/
char buf[40+256*4];
BITMAPINFO *pBitsInfo = (BITMAPINFO *)buf;
memcpy(&pBitsInfo->bmiHeader,pBitmapInfo,sizeof(BITMAPINFOHEADER));
/**/
int palleteNum = 0;
RGBQUAD *pallete = GetDIBPaletteData(pBmpFileBuf,&palleteNum);
for(int c = 0; c < 256; c++)
{
if( mode == 0 )
{
(pBitsInfo->bmiColors[c]).rgbRed = (pallete!=NULL && c<palleteNum? pallete[c].rgbRed : c);
(pBitsInfo->bmiColors[c]).rgbGreen = (pallete!=NULL && c<palleteNum? pallete[c].rgbGreen : c);
(pBitsInfo->bmiColors[c]).rgbBlue = (pallete!=NULL && c<palleteNum? pallete[c].rgbBlue : c);
}
else
{
(pBitsInfo->bmiColors[c]).rgbRed = (mode==1? c : 0);
(pBitsInfo->bmiColors[c]).rgbGreen = (mode==2? c : 0);
(pBitsInfo->bmiColors[c]).rgbBlue = (mode==3? c : 0);
}
}
/**/
SetStretchBltMode(hDC,COLORONCOLOR);
StretchDIBits(hDC,disp_xL,disp_yL,disp_Width,disp_Height,
0,0,imageWidth,imageHeight,pDIBImageData,pBitsInfo,DIB_RGB_COLORS,SRCCOPY );
/**/
return;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 象素操作
// 读象素颜色值
// 返回: >=0 表示象素在位图数据中的偏移值
// <0 失败或参数无效
long GetPixel(char *pFileBuffer,int x,int y,RGBQUAD rgb[1],bool bGray[1])
{
int nColorBits = GetColorBits(pFileBuffer);
int nImageHeight = GetImageHeight(pFileBuffer);
int nBytesPerRow = GetWidthBytes(pFileBuffer);
/**/
long nOffInImage = (nImageHeight-1-y) * nBytesPerRow;
char *p = GetDIBImageData(pFileBuffer) + nOffInImage;
/**/
if( bGray != NULL ) *bGray = true;
if( nColorBits == 8 )
{
nOffInImage += x;
rgb[0].rgbReserved = p[x];
rgb[0].rgbRed = p[x];
rgb[0].rgbGreen = p[x];
rgb[0].rgbBlue = p[x];
}
else if( nColorBits == 24 )
{
if( bGray != NULL ) *bGray = false;
nOffInImage += 3 * x;
p += (3 * x);
rgb[0].rgbReserved = 0;
rgb[0].rgbRed = p[2];
rgb[0].rgbGreen = p[1];
rgb[0].rgbBlue = p[0];
}
else
{
AfxMessageBox("It is not an 8-bit or 24-bit image");
return( -1L );
}
/**/
return( nOffInImage );
}
// 设置像素(x,y)的颜色值
void SetPixel(char *pFileBuffer,int x,int y,RGBQUAD rgb)
{
int nColorBits = GetColorBits(pFileBuffer);
int nImageHeight = GetImageHeight(pFileBuffer);
int nBytesPerRow = GetWidthBytes(pFileBuffer);
/**/
long nOffInImage = (nImageHeight-1-y) * nBytesPerRow;
char *p = GetDIBImageData(pFileBuffer) + nOffInImage;
/**/
if( nColorBits == 8 )
{
p[ x ] = rgb.rgbReserved;
}
else if( nColorBits == 24 )
{
p += (3 * x);
p[0] = rgb.rgbBlue;
p[1] = rgb.rgbGreen;
p[2] = rgb.rgbRed;
}
else
{
AfxMessageBox("It is not an 8-bit or 24-bit image");
}
/**/
return;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 保存为BMP文件
BOOL SaveDIB(char *pFileBuffer,CString strBmpFile)
{
CFile hFile;
if( !hFile.Open(strBmpFile,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary) )
{
AfxMessageBox("Failed to create the BMP file");
return( FALSE );
}
/**/
BITMAPFILEHEADER *pBmpHead = (BITMAPFILEHEADER *)pFileBuffer;
long lFileSize = pBmpHead->bfSize;
hFile.Write(pFileBuffer,lFileSize);
hFile.Close();
return( TRUE );
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 图像插值
/**
功能: 图像插值
nMethod 插值算法
0 = 最临近插值法
1 = (双)线性插值法
返回: 新图像的BMP文件缓冲区首地址
NULL 表示失败(内存不足)
**/
char *ImageInterpolation(char *pBmpFileBuf,int newWidth,int newHeight,int nMethod)
{
BITMAPFILEHEADER *pFileHeader = (BITMAPFILEHEADER *)pBmpFileBuf;
BITMAPINFOHEADER *pDIBInfo = (BITMAPINFOHEADER *)(pBmpFileBuf + sizeof(BITMAPFILEHEADER));
// char *pDIBData = pBmpFileBuf + pFileHeader->bfOffBits;
int orgWidth = pDIBInfo->biWidth;
int orgHeight = pDIBInfo->biHeight;
int colorBits = pDIBInfo->biBitCount;
/**/
long bytesPerRow = 4 * ((newWidth * colorBits + 31) / 32);
long newBmpFileSize = pFileHeader->bfOffBits + bytesPerRow * newHeight;
char *pNewBmpFileBuf = new char [newBmpFileSize];
memcpy(pNewBmpFileBuf, pBmpFileBuf, pFileHeader->bfOffBits);
BITMAPFILEHEADER *pNewFileHeader = (BITMAPFILEHEADER *)pNewBmpFileBuf;
BITMAPINFOHEADER *pNewDIBInfo = (BITMAPINFOHEADER *)(pNewBmpFileBuf + sizeof(BITMAPFILEHEADER));
pNewFileHeader->bfSize = newBmpFileSize;
pNewDIBInfo->biWidth = newWidth;
pNewDIBInfo->biHeight = newHeight;
pNewDIBInfo->biSizeImage = bytesPerRow * newHeight;
// char *pNewDIBData = pNewBmpFileBuf + pFileHeader->bfOffBits;
/**/
/**/
float xScale = (float)orgWidth / (float)newWidth;
float yScale = (float)orgHeight / (float)newHeight;
for(int y = 0; y < newHeight; y++)
{
float fy = y * yScale;
for(int x = 0; x < newWidth; x++)
{
RGBQUAD rgb;
float fx = x * xScale;
if( nMethod == 0 ) //最临近插值法
{
int xx = min( (int)(fx+0.5), orgWidth - 1 );
int yy = min( (int)(fy+0.5), orgHeight - 1 );
GetPixel(pBmpFileBuf, xx, yy, &rgb);
}
else
{ //(双)线性插值法
RGBQUAD rgbLT,rgbRT,rgbLB,rgbRB;
int x1 = (int)fx;
int x2 = min(x1+1, orgWidth-1);
float dx = fx - (float)x1;
int y1 = (int)fy;
int y2 = min(y1+1, orgHeight-1);
float dy = fy - (float)y1;
GetPixel(pBmpFileBuf, x1, y1, &rgbLT);
GetPixel(pBmpFileBuf, x2, y1, &rgbRT);
GetPixel(pBmpFileBuf, x1, y2, &rgbLB);
GetPixel(pBmpFileBuf, x2, y2, &rgbRB);
for(int N = 0; N < 4; N++)
{
float v1 = ((BYTE *)&rgbLT)[N] + dy * (((BYTE *)&rgbLB)[N] - ((BYTE *)&rgbLT)[N]);
float v2 = ((BYTE *)&rgbRT)[N] + dy * (((BYTE *)&rgbRB)[N] - ((BYTE *)&rgbRT)[N]);
((BYTE *)&rgb)[N] = (int)(v1 + dx * (v2 - v1) + 0.5);
}
}
SetPixel(pNewBmpFileBuf, x, y, rgb);
}
}
/**/
return( pNewBmpFileBuf );
}