Turning BMP image to min. finite automata

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 743 Views - Last Post: 17 May 2013 - 02:35 PM Rate Topic: -----

#1 ReVolt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 13

Turning BMP image to min. finite automata

Posted 14 May 2013 - 10:34 AM

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>
#include "bmp.h"

using namespace std;

unsigned char* readBMP(char* filename)
{
    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 s = *(int*)&info[2];
	int bisize = *(int*)&info[14];
	int width = *(int*)&info[18];
    int height = *(int*)&info[22];
    int size = 3 * width * 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;
}

int main(int argc, char **argv)
{

	unsigned char *data;
	if(argc == 2)
	{
	data = readBMP(argv[1]);
	cout << data;
	}
	else
		return -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

Is This A Good Question/Topic? 0
  • +

Replies To: Turning BMP image to min. finite automata

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8376
  • View blog
  • Posts: 31,134
  • Joined: 12-June 08

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:22 AM

Are you trying to create an image or read the coloring off an image?
Was This Post Helpful? 0
  • +
  • -

#3 ReVolt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 13

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:33 AM

I want to read the bits not to create image.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8376
  • View blog
  • Posts: 31,134
  • Joined: 12-June 08

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:38 AM

Curious question - does it have to be a bitmap? Why not have a CSV file or some sort of array based file?
Was This Post Helpful? 0
  • +
  • -

#5 ReVolt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 13

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:43 AM

It has to be image... for some reason
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:46 AM

Hi,

You need to reread the file format as the pixel rows are padded as multiple of 4 bytes

http://www.fileforma.../bmp/corion.htm

read the above link it might help you to read the file properly.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8376
  • View blog
  • Posts: 31,134
  • Joined: 12-June 08

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:47 AM

Is this per some homework assignment?

What about using a nice library?

http://easybmp.sourceforge.net/
Was This Post Helpful? 0
  • +
  • -

#8 ReVolt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 13

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 11:50 AM

View Postsnoopy11, on 14 May 2013 - 11:46 AM, said:

Hi,

You need to reread the file format as the pixel rows are padded as multiple of 4 bytes

http://www.fileforma.../bmp/corion.htm

read the above link it might help you to read the file properly.

Regards

Snoopy.


Thank you very much

View Postmodi123_1, on 14 May 2013 - 11:47 AM, said:

Is this per some homework assignment?

What about using a nice library?

http://easybmp.sourceforge.net/


Our professor told us that it would be cool to implement it :)

I want to learn it withou any lib.
Was This Post Helpful? 0
  • +
  • -

#9 ReVolt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 13

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 12:38 PM

thanks snoopy11, it helped me to understand the header structures, but it's still problem form me to get the rest
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 01:45 PM

Hi,

In what way are you having difficulty please show what you have so far as in terms of actually reading a bmp file format ?

By that I mean I expect you to post code.

And by that I mean not your last code.
Snoopy.
Was This Post Helpful? 1
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 03:44 PM

I am willing to give you a full and proper answer but I do ask you put in some real effort into understanding the format,....

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 05:00 PM

If you are a visual learner, the chart on the Wikipedia page is very useful. If you crack open your .BMP file and follow along the chart things should start making sense.
Was This Post Helpful? 2
  • +
  • -

#13 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 08:34 PM

Yes it is.

Once you are in position to read the image data

Then the wiki is really good on how to interpret the data.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 09:06 PM

I can sort of see how to use a .BMP for a finite state automata: the rows correspond to states, the columns to possible inputs, and the values at the intersection of the row and column define the state transition. I'm at a loss though to see why a .BMP would be a better representation versus a plain old array.

On the other hand, a see a better application of a .BMP with a finite state automata: use the .BMP data as the initial state for the field used in Conway's Game of Life. Zero vs. Non-zero pixel would indicate dead vs. live cells, and different colors could represent different life forms.

This post has been edited by Skydiver: 14 May 2013 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#15 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Turning BMP image to min. finite automata

Posted 14 May 2013 - 10:03 PM

Yes,

Its that time of year again when Professors say to their students
wouldnt it be fun to get the pixel data from a bitmap ?

They seem to have a hard on for it. I have no idea why.

The .bmp format comes in so many flavours 1bpp, 2bpp, 4bpp, 8bpp. 16bpp, 24bpp or 32bpp

Students always make the mistake that a 1bpp image will be easier to read.

This is not actually true as a 1bpp image pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte. Each bit is an index into a table of 2 colors. This is confusing even for hardened programmers.

Its actually easier to read a 24bpp image as the RGB info is stored in 3 separate bytes in BGR format with the image of course inverted but that is no biggy.



Anyway I would love to give this guy some code....

But it seems he is not playing.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2