Hello I have problem with this... I don't know how to start with it, first thing that came up to my mind was "loop through an array of bits" in bitmap (monochromatic), but I can't get the bits.
So I've searched the internet and found lots of examples but they didn't helped me much.
Here's code I have:
#include <iostream>
#include <fstream>
usingnamespace std;
unsignedchar* readBMP(char* filename)
{
int i;
FILE* f = fopen(filename, "rb");
unsignedchar info[54];
fread(info, sizeof(unsignedchar), 54, f); // read the 54-byte header
// extract image height and width from header
int s = *(int*)&info[2];
int bisize = *(int*)&info[14];
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsignedchar* data = newunsignedchar[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsignedchar), size, f); // read the rest of the data at once
fclose(f);
for(i = 0; i < size; i += 3)
{
unsignedchar tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
return data;
}
int main(int argc, char **argv)
{
unsignedchar *data;
if(argc == 2)
{
data = readBMP(argv[1]);
cout << data;
}
elsereturn -1;
return 0;
}
No erros, it's working fine but I just can't get the bits of picture. I've tried to "cout" values form header (width, height) and they're ok, but I wonder how to get representation of picture itself. I thought that this code will give me something like array of 1 and 0.
I have to convert black and white (monochromatic) bmp image to min. finite automata.
I'll be very thankful for any advice
PS: sorry for my english it's while since i spoke or wrote something in english
Well, if you have an RGB bmp image, and you know that it's only black and white pixels, then you can check if the pixel is black or white simply like that:
1 2 3 4
bool isBlack(unsignedchar* data, int width, int height, int x, int y)
{
return data[3 * (y * width + x)] == 0;
}
Basically, every unsignedchar triplet in a bmp represent red, green, and blue for every pixel. Black pixels will have (0, 0, 0) and white ones (255, 255, 255).
However,I have no idea what you mean by converting an image to "min. finite automata".
thanks, the image will be monochomatic always so it will contain only back or white pixels
your post is very helpfull for me but can you tell me how is the char triplet stored in that array I have. It's just sequence of those numbers (something like 000255255255000....) or is it stored different way...
Don't waste your time with that automata thing I thing I fugured that out..
Basically, yes, it's just a sequence of numbers as you've written.
Logically, though, this is not a simple sequence of unsigned chars - it is a 2D array of unsigned char triplets (although physically it's 1D) - that's why I wrote the formula data[3 * (y * width + x)] - it means that logically, we have a 2D array each line of which is width triplets long. So if you want to access line number y you should skip y * width * 3 elements. Similarly, you need to skip x * 3 elements to access pixel number x in line y. And this sums up to the formula above.