Repository navigation
Expand file tree
/
Copy pathDib.cpp
More file actions
166 lines (150 loc) · 3.49 KB
/
Copy pathDib.cpp
File metadata and controls
166 lines (150 loc) · 3.49 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
#include "pch.h"
#include "Dib.h"
CDib::CDib(void):m_pDibBits(nullptr), m_pGrayValueCount(nullptr)
{
// initialized variables
m_nBitCount = 0;
m_nWidth = 0;
m_nHeight = 0;
m_nWidthBytes = 0;
}
CDib::CDib( CDib &Dib ):m_pDibBits(nullptr),m_pGrayValueCount(nullptr) {
// initialized variables
m_nBitCount = 0;
m_nWidth = 0;
m_nHeight = 0;
m_nWidthBytes = 0;
if (Dib == nullptr) {
return;
}
if (!IsNull())
{
Destroy();
}
Create(Dib.GetWidth(),Dib.GetHeight(),Dib.GetBPP(),0);
m_nWidth = Dib.m_nWidth;
m_nHeight = Dib.m_nHeight;
// 拷贝调色板
if(IsIndexed()) {
int nColors=Dib.GetMaxColorTableEntries();
if(nColors > 0) {
RGBQUAD* pal = new RGBQUAD[nColors]();
Dib.GetColorTable(0, nColors, pal);
SetColorTable(0, nColors, pal);
delete[] pal;
}
}
m_nWidthBytes =abs(GetPitch());
m_nBitCount = GetBPP();
m_pDibBits = (unsigned char*)GetBits()+(m_nHeight-1)*GetPitch();
memcpy(m_pDibBits, Dib.m_pDibBits, m_nHeight*m_nWidthBytes);
}
CDib::~CDib(void) {
m_pDibBits = nullptr;
if (m_pGrayValueCount != nullptr) {
delete []m_pGrayValueCount;
m_pGrayValueCount = nullptr;
}
}
void CDib::LoadFile( LPCTSTR lpszPathName ) {
Load(lpszPathName);
m_nWidth = GetWidth();
m_nHeight = GetHeight();
m_nWidthBytes =abs(GetPitch()) ;
m_nBitCount = GetBPP();
m_pDibBits = (unsigned char*)GetBits()
+ (m_nHeight - 1) * GetPitch();
}
void CDib::Invert() {
for (int i = 0; i < m_nHeight; i++) {
for (int j = 0; j < m_nWidthBytes; j++) {
*(m_pDibBits + i * m_nWidthBytes + j) = 255 - *(m_pDibBits + i * m_nWidthBytes + j);
}
}
}
void CDib::fake_color(FakeColor color) {
int nColors = this->GetMaxColorTableEntries();
if (nColors > 0) {
RGBQUAD* pal = new RGBQUAD[nColors]();
this->GetColorTable(0, nColors, pal);
switch (color) {
case FAKE_RED:
for (int i = 0; i < nColors; i++) {
pal[i].rgbBlue = 0;
pal[i].rgbGreen = 0;
}
break;
case FAKE_GREEN:
for (int i = 0; i < nColors; i++) {
pal[i].rgbRed = 0;
pal[i].rgbBlue = 0;
}
break;
case FAKE_BLUE:
for (int i = 0; i < nColors; i++) {
pal[i].rgbRed = 0;
pal[i].rgbGreen = 0;
}
break;
default:
break;
}
SetColorTable(0, nColors, pal);
}
}
void CDib::linear_transforme() {
for (int i = 0; i < m_nHeight; i++) {
for (int j = 0; j < m_nWidthBytes; j++) {
uint8_t luma = *(m_pDibBits + i * m_nWidthBytes + j);
if (luma < 100) {
*(m_pDibBits + i * m_nWidthBytes + j) = 0;
} else if (luma > 200) {
*(m_pDibBits + i * m_nWidthBytes + j) = 255;
} else {
*(m_pDibBits + i * m_nWidthBytes + j)
= (*(m_pDibBits + i * m_nWidthBytes + j) - 100)
* 2 + 100;
}
}
}
}
void CDib::bit_plane(BitPlane bit_plane) {
for (int i = 0; i < m_nHeight; i++) {
for (int j = 0; j < m_nWidthBytes; j++) {
uint8_t temp = *(m_pDibBits + i * m_nWidthBytes + j) & bit_plane;
if (temp == bit_plane) {
*(m_pDibBits + i * m_nWidthBytes + j) = 255;
}
else {
*(m_pDibBits + i * m_nWidthBytes + j) = 0;
}
}
}
}
long CDib::get_image_height() {
return m_nHeight;
}
long CDib::get_image_width() {
return m_nWidth;
}
int CDib::get_image_bit_width() {
return m_nBitCount;
}
long* CDib::GrayValueCount()
{
long nColors = GetMaxColorTableEntries();
if (nColors == 0)
{
return nullptr;
}
long *pGrayValueCount = new long[nColors];
memset(pGrayValueCount, 0, nColors*sizeof(long));
for (int i=0;i<m_nHeight;i++)
{
for (int j=0;j<m_nWidth;j++)
{
pGrayValueCount[*(m_pDibBits + i*m_nWidthBytes +j)]++;
}
}
return pGrayValueCount;
}