954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help in C program (Image reading)

Hi, can anyone out help me with my assignment which is to read an image file of a maze and find an exit of it.

As I'm still kinda of new @ C programming, I'm having problems even reading the image onto C.

Cheers
Ming

Iphicles
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Well what format is this image file in?
BMP format perhaps?

Though most newbies start with mazes defined in ascii art, like this

#S########
#        #
######   #
#        #
#E########

Find a route through the maze from S to E

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Salem the image will be of .gif or .bmp format. I can't seems to even find the correct library or codes to even load it.

Cheers
Ming

Iphicles
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

http://www.wotsit.org/
BMP is dead easy, it's little more than a screen dump of pixels.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Salem, after reading all of the web page contents, I'm still having problems even writing out the initial program.

Cos i not so sure which library to use, for normal standard program is #include Studio.h or math.h for calculation etc.

But for this instance, I am getting more confuse. Please advice me about how to go about writing the program. If its possible, can you show me examples instead.

Cheers
ming

Iphicles
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Perhaps start with an ASCII-art map like I posted above, and then develop the maze solving code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Do you know about C File I/O?

This is the .bmp format.
http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
Open a bitmap in a hex editor as you follow it along.

Then extract the data you want. That's the easy part done.

Colin Mac
Posting Whiz
327 posts since Sep 2006
Reputation Points: 78
Solved Threads: 22
 

Colin,

I tried reading that webpage many time and I still don't have the slightest idea about how to go about it.

Is it possible that U can write a little portion about how to read an .bmp file script for me to take a look and its easier for me to understand.

Cheers
Ming

Iphicles
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

FILE *fp = fopen( "file.bmp", "rb" );
This opens the file unsigned char buff[10];
size_t n = fread( buff, sizeof(buff[0]), sizeof(buff), fp );

This reads 10 bytes from the file.

10 is obviously just an example, but you can read the documentation to find out how big a BMPHEADER structure is, and read that number of bytes instead. Having got that, you get vital information such as
- the width and height of the image
- the number of bits in each pixel
- whether there is a colour table in the file.

You then skip the optional colour table (say by using fseek), then you can start looping reading image data, based on your knowledge of the width, height and bpp of the image format.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Salem, Thanks for getting back to me on this. But when U say

unsigned char buff[10];
size_t n = fread( buff, sizeof(buff[0]), sizeof(buff), fp );
This reads 10 bytes from the file.

Do U mean that this command only allow me to read only 10bytes of data from the file only?

And the .bmp file i'm reading is simply in black and white.

Pls look at http://hereandabove.com/maze/mazeorig.form.html .

And u will have a clearer idea what sort of image i'm trying to read.

Cheers & Thanks
Ming

Iphicles
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

> Do U mean that this command only allow me to read only 10bytes of data from the file only?
That was an example - read the documentation to decide how many to read.

Short of actually posting the actual code en-masse (which isn't going to happen), I don't know what else to tell you. You can read from 1 byte to the whole file, the choice is yours.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

google "let us C yashwant kanetkar filetype:pdf" and in the book goto "graphics programming". I guarantee that u'll understand it perfectly well. And read drawing shapes b4 moving to viewing bitmap

timetraveller92
Light Poster
Banned
38 posts since Jul 2011
Reputation Points: 2
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You