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
|
#include <iostream>
#include <fstream>
using namespace std;
unsigned char* readBMP(char *filename, int *w, int *h)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int b33 = *(int*)&info[30];
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
cout << b33 << endl;
*w = width;
*h = height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
return data;
}
bool isBlack(unsigned char* data, int width, int height, int x, int y)
{
return data[3 * (y * width + x)] == 0;
}
int main(int argc, char **argv)
{
unsigned char *data;
int w, h;
int *bits;
if(argc == 2)
{
data = readBMP(argv[1], &w, &h);
bits = new int[w*h];
for(int i = 1; i <= h; i++)
for(int j = 0; j < w; j++)
{
if(isBlack(data, w, h, i, j))
bits[i*j] = 0;
else
bits[i*j] = 1;
}
cout << w << " " << h << " " << endl;
for(int i = 1; i <= h; i++)
{
for(int j = 0; j < w; j++)
{
cout << bits[i*j] << " ";
}
cout << endl;
}
}
else
return -1;
return 0;
}
|