GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Sep-2007, 00:50
nemo nemo is offline
New Member
 
Join Date: Aug 2007
Posts: 16
nemo is on a distinguished road

BMP image on matrix load


Hello.I am working with visual c++ compiler 7.0.I have write down a c++ program to load a 256 color bitmap image (load on the same folder of .cpp program) into a 800x600 integer matrix.Everything seems to go ok but I would like a check.If some one can help me.Thanks a lot in advance.This is the code:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define WIDTHBYTES(bits)	((((bits) + 31) >> 5) << 2)

void load_bitmap(char *,int,int,void *);    [color="Red"]//member function to open the image[/color]

void main(int argc, char *argv[])
{
	int i=0,j=0;
	int w=0,h=0,z=0,t=0,k;
	unsigned char *buf = (unsigned char *)malloc(sizeof(unsigned char)*800*600*3),buff[800][600[color="red"]];//this last one is the matrix on which I would like to load my image[/color]	
char *name = "polyhedron8.bmp";


    load_bitmap(name,w,h,buf); [color="red"]//call to member function[/color]

	for(i=0; i<800; i++) for(j=0; j<600; j++){      //loading of image values on matrix
                buff[i][j]=*(buf + t);
		t++;
	}


}

void load_bitmap(char* filename,int width, int height, void* buffer)
{
	BITMAPFILEHEADER	bfh;
	BITMAPINFOHEADER	bih;
	FILE*		             fp;
	int			widthbytes;
	
	
	
	if((fp = fopen(filename, "rb")) == NULL)        [color="Red"]//check open file result[/color]
	{
		printf("I cannot open the file\n");
		return;
	}
	printf("fp memory value is %d\n",fp);

	fread(&bfh,sizeof(BITMAPFILEHEADER), 1, fp);
	fread(&bih,sizeof(BITMAPINFOHEADER), 1, fp);

	printf("heigth in pixel is %d\n",bih.biHeight); [color="Red"]//height visualization[/color]
	height=bih.biHeight;

	printf("width  in pixel is %d\n",bih.biWidth);  [color="red"]//width visualization[/color]
	width=bih.biWidth;

	widthbytes = WIDTHBYTES(width * 24);
	printf("widthbytes value is %d\n",widthbytes);

	fread(buffer, sizeof(BYTE), widthbytes * height, fp);


	printf("bits  for pixel is %d\n",bih.biBitCount);
	printf("offset in bytes from beginning of BITMAFILEHEADER to       bitmap bits is %d\n",bfh.bfOffBits); 
        printf("widthbytes * height value is %d\n",widthbytes * height);

	fclose(fp);

}
Last edited by LuciWiz : 12-Sep-2007 at 10:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 12-Sep-2007, 09:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bmp image on matrix load help


Quote:
Originally Posted by nemo
program to load a 256 color bitmap image ...into a 800x600 integer matrix

1. It appears to me that you have hard-coded a number of values so that the program doesn't have a chance of working with anything other than an 800x600 24-bit color bitmap file, but you don't test any of the header parameters to make sure that's what you actually have. You also don't test to see how many bytes you actually read from the file. None of these gives me any confidence in the program as written.

For example you allocate an array size of 3*800*600=1,440,000 chars to hold the values from the file, and you try to read that many bitmap values after the two headers. That's how a 24-bit color bitmap file would be organized and that's the number of bytes that would be in the bitmap portion.

A 256 color bitmap file has a (usually) 1024-byte color palette after the two headers (before the bitmap pixel data), then one byte per pixel for the rest of the file.

2. Then you copy the first (800*600) bytes from your 1,440,000 array of bytes from the file into your 2-D array. If there was a palette in the file (that is, if it really is a 256-color bitmap), you are reading the palette into the first 1024 members of the 2-D array. If it is really a 24-bit color file, you are reading the Red, Green, Blue color values of the first 160,000 pixels into your 2-D array. I don't know what you expect, but neither gives me anything useful.

Regards,

Dave
Last edited by davekw7x : 12-Sep-2007 at 11:08.
  #3  
Old 12-Sep-2007, 20:31
nemo nemo is offline
New Member
 
Join Date: Aug 2007
Posts: 16
nemo is on a distinguished road

Re: bmp image on matrix load help


thanks for explenation but how Can I find where effectively my bitmap starts?
I have tried to obytain it cheking the values of fp,BITMAPINFOHEADER and BITMAPFILEHEADER pointers but they do not correspond.Can you help me?
  #4  
Old 13-Sep-2007, 07:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bmp image on matrix load help


Quote:
Originally Posted by nemo
thanks for explenation but how Can I find where effectively my bitmap starts?
I have tried to obytain it cheking the values of fp,BITMAPINFOHEADER and BITMAPFILEHEADER pointers but they do not correspond.Can you help me?

The first header (BITMAPFILEHEADER in Microsoft's definition) is 14 bytes long
The second header (BITMAPINFOHEADER) is 40 bytes long

The field bfOffBits of the BITMAPFILEHEADER tells the start of the bitmap data. .

You indicated that your program is supposed to load a 256-color bitmap file.
I suggest that you print out all of the information that you can get from the headers before trying to do anything with the data:

CPP / C++ / C Code:
    printf("    bfType    = %x\n", bfh.bfType);
    printf("    bfSize    = %ld\n", bfh.bfSize);
    printf("    bfOffBits = %ld\n", bfh.bfOffBits);
.
.
.
    printf("    biSize            = %ld\n", bih.biSize);
    printf("    biWidth           = %ld\n", bih.biWidth);
    printf("    biHeight          = %ld\n", bih.biHeight);
    printf("    biPlanes          = %d\n", bih.biPlanes);
    printf("    biBitCount        = %d\n", bih.biBitCount);
    printf("    biCompression     = %ld\n", bih.biCompression);
    printf("    biSizeImage       = %ld\n", bih.biSizeImage);

For a 256 color file (8-bit color) you should see something like:

Code:
bfType = 4d42 bfSize = 481080 bfOffBits = 1078 . . . biSize = 40 biWidth = 800 biHeight = 600 biPlanes = 1 biBitCount = 8 biCompression = 0 biSizeImage = 480002

Key elements: biBitCount = 8 for a 256 color file, The image is represented by 800*600 pixel values. (This file, for some reason has an extra two bytes at the end. That's not a problem.)

The bitmap data starts 1078 bytes from the beginning of the file, as indicated by the bfOffBits value. (54 bytes for the headers plus 1024 bytes for the palette.)

For a 24-bit color file you might see something like
Code:
bfType = 4d42 bfSize = 1440056 bfOffBits = 54 . . . biSize = 40 biWidth = 800 biHeight = 600 biPlanes = 1 biBitCount = 24 biCompression = 0 biSizeImage = 1440002

The bitmap data starts 54 bytes from the beginning of the file (54 bytes for the two headers).

The bitmap data occupies 800*600*3 bytes, since each pixel is represented by three bytes. (Again, a couple of extra bytes at the end.)

So my questions are:

1. Where did the bitmap file come from?

2. Where did the program come from? Who said that it is going to read a 256 color file?

3. What are you going to do with whatever data you get it into the 2-Dimension array of chars?

If it is a 8-bit color file (256-colors), then just storing the bitmap data bytes is useless, since you can't possibly know what colors they represent without referring to the palette. If it is a 24-bit color file (millions of colors), then each pixel is represented by three bytes. Storing one byte per pixel in your array of chars gives nothing useful. (At least that's the way that I see it.)

Regards,

Dave

Footnote: Bitmap files are not usually compressed, I think, but provision is made in the file format definition for indicating a compression scheme in the info header. You should always make sure that the biCompression value is zero, otherwise you have some more work to do.
  #5  
Old 13-Sep-2007, 08:32
nemo nemo is offline
New Member
 
Join Date: Aug 2007
Posts: 16
nemo is on a distinguished road

Re: bmp image on matrix load help


Oh thanks really.You are very prepared.So I would like to ask you how to do.
Image come from a paint where I have saved a image captured by a camera.
I have saved the image on paint like 256 color bitmap.Now I would like to convert it in a grey level image because I have to use Sobel gradient operator and I need a matrix 800x600 where each grey level pixel value is stored.Do you think is it possible?is better using 24 bitmap image and make the average for every 3 byte (RGB)?I hope you'll reply trying to help me thanks for your attention and in advance for this post.
Bye.
  #6  
Old 13-Sep-2007, 10:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bmp image on matrix load help


Quote:
Originally Posted by nemo
Image come from a paint where I have saved a image captured by a camera.
I have saved the image on paint like 256 color bitmap.Now I would like to convert it in a grey level image because I have to use Sobel gradient operator and I need a matrix 800x600 where each grey level pixel value is stored.Do you think is it possible?
Yes.
Quote:
Originally Posted by nemo
is better using 24 bitmap image and make the average for every 3 byte (RGB)?

Using an 8-bit (256-color) file would require you to use the color table (palette) to convert each 8-bit pixel value to three color values and average them. That's not too difficult, I'm thinking.

Some 8-bit gray scale bitmap files have a color table that is a "gray-scale ramp" with the final result that a 0x00 pixel value gives black, a 0xff pixel value gives white. But some have different values in the color table with the effect of adjusting brightness and contrast without actually changing bitmap pixel values in the file. Some rendering programs ignore the "color table" with the result that you don't always see what was written. Some application programs don't bother to put in the color table for eight-bit grayscale files. That's why programs that handle all kinds of bitmap files can get pretty complicated.

It seems to me that using a 24-bit color file is more straightforward, since there is no palette, and simply taking the rgb average might be a good starting point for converting to gray scale.

Generally speaking, if visual consistency is desired, experimenting with weighted averages might give more pleasing subjective (aesthetic) results, but for edge detection, a straight average will probably be sufficient.

I think that it will be easiest to make a program that always works for 24-bit color files. I believe that your example already reads that kind of file, and you need only to take the rgb average from each 3 bytes of each pixel to write into your 2-D array.

If you want to write the results to a bitmap file after the edge detection, you can just create rgb values equal to the pixel value in the 800x600 array and write a 24-bit color file (actually a grayscale). Or you can write an 8-bit grayscale bitmap if you would rather have the output in that format.


Regards,

Dave
  #7  
Old 13-Sep-2007, 21:40
nemo nemo is offline
New Member
 
Join Date: Aug 2007
Posts: 16
nemo is on a distinguished road

Re: bmp image on matrix load help


Really thanks you have been extremely clear and precise.I hope You will forgive my ignorance but making the average of the RGB bytes is not enough to obtain the grey scale level image?And really last question (sorry to disturb you) how can I visualize the obtained matrix with grey scale level values like an image?
Really thanks for your help.
  #8  
Old 13-Sep-2007, 23:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bmp image on matrix load help


Quote:
Originally Posted by nemo
but making the average of the RGB bytes is not enough to obtain the grey scale level image?
Yes, I think that iswhat you want to do.
Quote:
Originally Posted by nemo
how can I visualize the obtained matrix with grey scale level values like an image?
Really thanks for your help.
Create an output file of the same type as the input file. All of the header bytes will be the same as the input file. The bitmap data values will be that of the gray-scale.

For example. Suppose you had the following 24-bit color bitmap file
Code:
54 bytes of headers r1g1b1r2g2b2r3g3b3... (total of 3*rows*cols rgb values of the pixels)

You create an internal matrix of gray scale values:

[code]
G1G2G3... (total of rows*cols gray values for the pixels)
[code]
Where
G1 = (r1+g1+b1)/3
G2 = (r2+g2+b2)/3
.
.
.
You store the G values in your 2-Dimension matrix, so now you have a 2-D matrix of gray values.

Then perform the Sobel (or whatever) transformation) on the 2-D matrix.

Now you want to see the results, right?


You could create a brand new 256-color grayscale bit map, (with or without a palette), using the G values from your transformed matrix

Or you could just use the G values to create a 24-bit color (gray scale) bitmap file.

The output file will be like this:
Code:
The exact same 54 bytes of headers as the input file G1G1G1G2G2G2G3G3G3...

In other words, the output file will be exactly the same size and type as the input file. Each set of "rgb" pixel values will be the same three bytes repeated (equal parts of red, greeen, blue give a gray value.

Once you have something like this that works and for which you can see the edge-detection at work, it may occur to you to handle some other type if input and output for more efficiency or whatever.

My point is that you almost have the first part in place: namely you can (almost) read a 24-bit color file with width = 800 and height = 600 into the 3x800x600 1-Dimension array, and I think that's a reasonable start.

Regards,

Dave
  #9  
Old 18-Sep-2007, 22:07
nemo nemo is offline
New Member
 
Join Date: Aug 2007
Posts: 16
nemo is on a distinguished road

Re: bmp image on matrix load help


Hello to everybody!Sorry but I have still a problem.I have loaded a 24 bit image.Transformed into a greyscale image and loaded on a 800x600 matrix.Now I have to apply sobel gradient tecnique.It simply consists of applying two rotated mask 3x3 at each point of the matrix.This leads to two matrix one for the x gradient and another one for y gradient copmponent.I need to work separately with both components then calculate module of x and y gradient components.P{roblem is that I can only treat one for cycle in my program.I mean I cannot create two for cycle one to calculate x component and another one for y component and the another for cycle for their module.IBecause comnputer give me an error and ask me to send an e mail to microsoft.It is obviously an error in my code may be inside matrix range or definition but I still cannot find it I hope someone will help me.Thanks in advance I post the code'

//program to load a 24 color bitmap on a matrix and visualize its content like grey level image and make
//sobel gradient analysis



#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>


void load_bitmap(char *,int,int,void *);

void main(int argc, char *argv[])
{
int i=0,j=0,w=0,h=0,t=0;
unsigned char *buf = (unsigned char *)malloc(sizeof(unsigned char)*800*600*3);
unsigned char buff[800][600];
// unsigned char sobelx[800][600]; //this is the matrix in which I would like to load the x component of gradient unsigned char sobel[800][600]; //this id the matrix where I would like to load the module of x and y gradient components // unsigned char sobely[800][600]; //this is the matrix in which I woulod like to load the y component of gradient double teta[800][600]; //here I load arctang values of gradient x and y components
char *name = "polyhedron10.bmp"; //this is the name of the image I want to load (it is present on .cpp file folder) int n=0;

load_bitmap(name,w,h,buf); //member function to open the bitmap image for(i=0; i<800; i++)
{
for(j=0; j<600; j++){ //double for cycle to load the image
buff[i][j]=(int)((*(buf + t) + *(buf + t + 1) + *(buf + t + 2))/3);
t=t+3;
}
}
for(i=1; i<799; i++)
{
for(j=1; j<599; j++){
// sobelx[i][j]=(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]);
// printf(" %d ",sobelx[i][j]);
sobel[i][j]=sqrt((pow((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),2)+pow((buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]),2)));

// in the upper line I load module of x and y gradient into the sobel matrix the problem is that I cannot treat them separately I mean I cannot make two for cycles for
// x and y components load them into sobelx and sobely matrices and then load them into sobel matrix I do not know why
// but I can write down only one for cycle It is obviously impossible but I cannot find the error.I nead to treat x and y component of gradient separately

printf(" %d ",sobel[i][j]);
// teta[i][j]=atan2((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]));
}
}
for(i=1; i<799; i++)
{
for(j=1; j<599; j++){
teta[i][j]=atan2((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]));
}
}
}
void load_bitmap(char* filename,int width, int height, void* buffer)
{
FILE* fp;

if((fp = fopen(filename, "rb")) == NULL) //check it is a bitmap image {
printf("it is not a bitmap”sB\n");
return;
}

fread(buffer, sizeof(BYTE), 800*600*3, fp); // save the image from buffer pointer fclose(fp);

}
  #10  
Old 18-Sep-2007, 22:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bmp image on matrix load help


Quote:
Originally Posted by nemo
Hello to everybody!Sorry but I have still a problem

Please paste the exact program that you are using into your post. There is no way (no way) that I am going to try to extract the usable part from the mess that you showed us. All of the extra color and comment parts that you put in to the source make it unreadable and uncompileable.

Here's the drill:

1. Ask your question or state your problem

2. Paste the program into yout post.

3. Put [c] before the first line of code.

4. Put [/c] after the last line of code.

Regards,

Dave
 

Recent GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i need help in C++ PLZ its_me CPP / C++ Forum 3 04-Dec-2006 21:51
Load image problem, need help!!! Skynet159 Java Forum 6 14-Aug-2006 09:21
Combining Vectors and References Frankg CPP / C++ Forum 7 14-Jan-2006 06:17
GIM gidedit - a fltk fluid resize project cable_guy_67 FLTK Forum 2 01-Jun-2005 15:00
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.