-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp_analys.cpp
191 lines (155 loc) · 5.03 KB
/
bmp_analys.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
// bmp_analys.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
using namespace std;
typedef struct
{
unsigned int bfType;
unsigned long bfSize;
unsigned int bfReserved1;
unsigned int bfReserved2;
unsigned long bfOffBits;
} BITMAPFILEHEADER;
typedef struct
{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BITMAPINFOHEADER;
typedef struct
{
int rgbBlue;
int rgbGreen;
int rgbRed;
int rgbReserved;
} RGBQUAD;
static unsigned short read_u16(FILE* fp);
static unsigned int read_u32(FILE* fp);
static int read_s32(FILE* fp);
static unsigned short read_u16(FILE* fp)
{
unsigned char b0, b1;
b0 = getc(fp);
b1 = getc(fp);
return ((b1 << 8) | b0);
}
static unsigned int read_u32(FILE* fp)
{
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
static int read_s32(FILE* fp)
{
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
int main()
{
FILE* pFile = fopen("C:/Users/KijoL/source/repos/CompImages/apple_1.bmp", "rb"); //cat_grey mouse
// считываем заголовок файла
BITMAPFILEHEADER header;
header.bfType = read_u16(pFile);
header.bfSize = read_u32(pFile);
header.bfReserved1 = read_u16(pFile);
header.bfReserved2 = read_u16(pFile);
header.bfOffBits = read_u32(pFile);
printf("%x %d %d\n", header.bfType, header.bfSize, header.bfOffBits);
//system("pause");
// считываем заголовок изображения
BITMAPINFOHEADER bmiHeader;
bmiHeader.biSize = read_u32(pFile);
bmiHeader.biWidth = read_s32(pFile);
bmiHeader.biHeight = read_s32(pFile);
bmiHeader.biPlanes = read_u16(pFile);
bmiHeader.biBitCount = read_u16(pFile);
bmiHeader.biCompression = read_u32(pFile);
bmiHeader.biSizeImage = read_u32(pFile);
bmiHeader.biXPelsPerMeter = read_s32(pFile);
bmiHeader.biYPelsPerMeter = read_s32(pFile);
bmiHeader.biClrUsed = read_u32(pFile);
bmiHeader.biClrImportant = read_u32(pFile);
printf("width\theight\n%d\t%d\n", bmiHeader.biWidth, bmiHeader.biHeight);
printf("bits per pixel\n%d\n", bmiHeader.biBitCount);
printf("biCompression\n%d\n", bmiHeader.biCompression);
//cout << bmiHeader.biSize << ' ' << bmiHeader.biWidth << ' ' << bmiHeader.biHeight << endl;
system("pause");
RGBQUAD** rgb = new RGBQUAD * [bmiHeader.biWidth];
for (int i = 0; i < bmiHeader.biWidth; i++) {
rgb[i] = new RGBQUAD[bmiHeader.biHeight];
}
uint32_t** colors = new uint32_t * [bmiHeader.biWidth];
for (int i = 0; i < bmiHeader.biWidth; i++) {
colors[i] = new uint32_t[bmiHeader.biHeight];
}
for (int i = 0; i < bmiHeader.biWidth; i++) {
for (int j = 0; j < bmiHeader.biHeight; j++) {
rgb[i][j].rgbBlue = getc(pFile);
rgb[i][j].rgbGreen = getc(pFile);
rgb[i][j].rgbRed = getc(pFile);
colors[i][j] = rgb[i][j].rgbBlue + (rgb[i][j].rgbGreen << 8) + (rgb[i][j].rgbRed << 16);
}
// пропускаем последний байт в строке
getc(pFile);
}
fclose(pFile);
// выводим результат
/*
for (int i = 0; i < bmiHeader.biWidth; i++) {
for (int j = 0; j < bmiHeader.biHeight; j++) {
if ((rgb[i][j].rgbRed != 102) && (rgb[i][j].rgbGreen != 102) && (rgb[i][j].rgbBlue != 102))
printf("%d %d %d\n", rgb[i][j].rgbRed, rgb[i][j].rgbGreen, rgb[i][j].rgbBlue);
}
printf("\n");
}
*/
int parts = 30;
int* vect = (int*)calloc(sizeof(int), parts);
uint32_t max = UINT32_MAX & 0x00FFFFFF;
uint32_t step = (max / parts)+1;
uint32_t border;
for (int i = 0; i < bmiHeader.biWidth; i++) {
for (int j = 0; j < bmiHeader.biHeight; j++) {
border = step;
for (int k = 0; k < parts; k++)
{
if (colors[i][j] < border)
{
vect[k]++;
break;
}
border += step;
}
}
}
for (int k = 0; k < parts; k++)
{
cout << vect[k] <<endl;
}
for (int i = 0; i < bmiHeader.biWidth; i++) {
delete rgb[i];
delete colors[i];
}
delete rgb;
delete colors;
return 0;
}