forked from ranjiewwen/DIPDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImg.h
275 lines (222 loc) · 5.42 KB
/
Img.h
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
// Img.h: interface for the CImg class.
//
//////////////////////////////////////////////////////////////////////
#ifndef __GRAY_H__
#define __GRAY_H__
#include "math.h"
// 处理图像的类
// 在计算图像大小时,采用公式:biSizeImage = biWidth' × biHeight。
// 是biWidth',而不是biWidth,这里的biWidth'必须是4的整倍数,表示
// 大于或等于biWidth的,离4最近的整倍数。WIDTHBYTES就是用来计算
// biWidth'
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
////// 0 -> 255 黑->白
class CImg
{
public:
// 构造函数
CImg();
// Copy构造函数
CImg(CImg& gray);
// 重载“=”操作符来创建新的对象
void operator = (CImg& gray); //图像赋值
BOOL operator == (CImg& gray); //判断2幅图像是否相同
CImg operator & (CImg& gray); //图像按位与
CImg operator | (CImg& gray); //图像按位或
CImg operator + (CImg gray); //图像相加
CImg operator - (CImg& gray); //图像减法
CImg operator ! (); //图像反色
// 析构函数
virtual ~CImg();
public:
// 判断位图是否有效
BOOL IsValidate() { return m_pBMIH != NULL; }
// 将所有像素的值初始化为color
void InitPixels(BYTE color);
// 从文件加载位图
BOOL AttachFromFile(LPCTSTR lpcPathName);
BOOL AttachFromFile(CFile &file);
// 将位图保存到文件
BOOL SaveToFile(LPCTSTR lpcPathName);
BOOL SaveToFile(CFile &file);
// 在DC上绘制位图
BOOL Draw(CDC* pDC);
BOOL Draw(CDC* pDC, CRect rt);
// 设置像素的值
void SetPixel(int x, int y, COLORREF color);
// 获取像素的值
COLORREF GetPixel(int x, int y);
// 获取灰度值
BYTE GetGray(int x, int y);
// 获取一行的字节数
int GetWidthByte();
// 获取一行的像素数
int GetWidthPixel();
// 获取高度
int GetHeight();
//改变位图的尺寸
void ImResize(int nHeight, int nWidth);
public:
// 绘画函数
// 绘制直线
void Line(POINT ptStart, POINT ptEnd);
void Line(POINT ptStart, int nLen, int nWide, BOOL bHor);
// 绘制矩形
void Rectangle(int x, int y, int r = 5);
void Rectangle(POINT ptLT, int r = 5);
void Rectangle(POINT ptLT, POINT ptRB);
void Circle(int x, int y, int r = 5){};
public:
// 判断是否是二值图像
BOOL IsBinaryImg();
// 判断是否是索引图像
BOOL IsIndexedImg();
// 256色索引图像转灰度图像
bool Index2Gray();
LPVOID GetColorTable(){return m_lpvColorTable;}
int GetColorTableEntriesNum(){return m_nColorTableEntries;}
private:
void CleanUp();
public:
// 文件数据
BITMAPINFOHEADER *m_pBMIH;
LPBYTE *m_lpData;
protected:
int m_nColorTableEntries;
LPVOID m_lpvColorTable;
};
/////////////////////////////////// inline functions ///////////////////////////////
/**************************************************
inline int CImg::GetWidthByte()
功能:
返回CImg实例中的图像每行占用的字节数
限制:
无
参数:
无
返回值:
int类型,返回图像每行占用的字节数
***************************************************/
inline int CImg::GetWidthByte()
{
return WIDTHBYTES((m_pBMIH->biWidth)*m_pBMIH->biBitCount);
}
/**************************************************
inline int CImg::GetWidthPixel()
功能:
返回CImg实例中的图像每行的像素数目,即横向分辨率或宽度
限制:
无
参数:
无
返回值:
int类型,返回图像每行的像素数目
***************************************************/
inline int CImg::GetWidthPixel()
{
return m_pBMIH->biWidth;
}
/**************************************************
inline int CImg::GetHeight()
功能:
返回CImg实例中的图像每列的像素数目,即纵向分辨率或高度
限制:
无
参数:
无
返回值:
int类型,返回图像每列的像素数目
***************************************************/
inline int CImg::GetHeight()
{
return m_pBMIH->biHeight;
}
/**************************************************
inline BYTE CImg::GetGray(int x, int y)
功能:
返回指定坐标位置像素的灰度值
参数:
int x, int y
指定的像素横、纵坐标值
返回值:
给定像素位置的灰度值
***************************************************/
inline BYTE CImg::GetGray(int x, int y)
{
COLORREF ref = GetPixel(x, y);
BYTE r, g, b, byte;
r = GetRValue(ref);
g = GetGValue(ref);
b = GetBValue(ref);
if(r == g && r == b)
return r;
double dGray = (0.30*r + 0.59*g + 0.11*b);
// 灰度化
byte = (int)dGray;
return byte;
}
/**************************************************
inline COLORREF CImg::GetPixel(int x, int y)
功能:
返回指定坐标位置像素的颜色值
参数:
int x, int y
指定像素位置的坐标
返回值:
COLERREF类型,返回用RGB形式表示的指定位置的颜色值
***************************************************/
inline COLORREF CImg::GetPixel(int x, int y)
{
if(m_pBMIH->biBitCount == 8) // 256色图
{
BYTE byte = m_lpData[m_pBMIH->biHeight - y - 1][x];
return RGB(byte, byte, byte);
}
else if(m_pBMIH->biBitCount == 1) // 单色图
{
BYTE ret = (1<<(7-x%8) & m_lpData[m_pBMIH->biHeight - y - 1][x/8]);
// 令0代表黑
RGBQUAD *p = (RGBQUAD*)m_lpvColorTable;
if(p[0].rgbBlue != 0)
ret = !ret;
if(ret)
return RGB(255, 255, 255); // 白色
else
return RGB(0, 0, 0); // 黑色
}
else if(m_pBMIH->biBitCount == 24) // 真彩图
{
COLORREF color = RGB(m_lpData[m_pBMIH->biHeight - y - 1][x*3 + 2],
m_lpData[m_pBMIH->biHeight - y - 1][x*3 + 1],
m_lpData[m_pBMIH->biHeight - y - 1][x*3]);
return color;
}
else
{
throw "not support now";
return 0;
}
}
inline BOOL CImg::IsBinaryImg()
{
int i,j;
for(i = 0; i < m_pBMIH->biHeight; i++)
{
for(j = 0; j < m_pBMIH->biWidth; j++)
{
if( (GetGray(j, i) != 0) && (GetGray(j, i) != 255) )//存在0和255之外的灰度值
return FALSE;
}//for j
}//for i
return TRUE;
}
inline BOOL CImg::IsIndexedImg()
{
if ((m_lpvColorTable != NULL)&&(m_nColorTableEntries!=0)) {
return true;
}
else {
return false;
}
}
#endif // __GRAY_H__