Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Malloc allocation fails!

Status
Not open for further replies.
You can do it in VB.
 

hi,
i wanna read a bmp image and place it in a 1200*1200 array using malloc.
is my code correct? i've got the concepts but i think it's not right..
pls advise.. :cry:

Code:
#include<iostream.h>
#include<iostream.h>
#include<fstream.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
int **CreateImage(int , int);

int main(void)
{
	char buffer[1200];    
	ifstream inFile("22354.bmp"); 
	if (!inFile);    
	{        
		cout << "Unable to open the file\n";        
		return 1;    
	}    
	while (inFile >> buffer)        
	                cout << buffer << endl;    
	inFile.close();    
	return 0;
	int **myImage = NULL;
	myImage = CreateImage(1200,1200);
} 

int **CreateImage(int m, int n)
{
	 int **pt = (int ** ) malloc (n * sizeof (int));
 	 if (pt == NULL)
	 {
		 cout << "not enough memory";
		 getch();
		 exit(0);
	 }
	 return pt;
}

Thanks!!!
 

No your code isn't correct. First of all you haven't dimensioned your 2-D array properly. See what I have done here...
Code:
int **CreateImage(int m, int n) 
{ 
    
	int **pt = (int ** ) malloc (n * sizeof (int*)); 
	if (pt == NULL) 
	{ 
		cout << "not enough memory"; 
		getch(); 
		exit(0); 
	} 


	for(int i = 0; i<n; i++)
	{
		pt[i] = (int * ) malloc (m * sizeof (int*));
		if (pt[i] == NULL) 
		{ 
			cout << "not enough memory"; 
			getch(); 
			exit(0); 
		} 
	}

	return pt; 
}

Second thing is it is not that easy to read BMP files. You need to read the header information from the file first, and check whether bitmap is compressed or not, how many colors are used, what are its dimensions etc...Then read the actual picture information using this information.

Instead use PGM (for grayscale) or PPM (for color) formats for image processing. These formats are extremely simple to read and create.
 

cant i use this to read a BMP file?
Code:
       char buffer[1200];    
	ifstream inFile("image.bmp"); 
	if (!inFile);    
	{        
		cout << "Unable to open the file\n";        
		return 1;    
	}    
	while (inFile >> buffer)        
		cout << buffer << endl;    
	inFile.close();    
	
	return 0;

my goal is to change a greyscale bitmap to a monochrome bitmap.remember the code u showed me?

Code:
void Threshold (int **in_image, int x_max, int y_max, int threshold) 
{ 
   for(int x=0; x<x_max; x++) 
   { 
      for(int y=0; y<y_max; y++) 
      { 
         if (in_image[x][y] >= threshold) 
            in_image[x][y] = 255; 
         else 
            in_image[x][y] = 0; 
      } 
   } 
}

I really have problems combining them into one... :? from reading the file to converting it to monocrome at the mean time placing it in the array... :cry:

Thanks CMOS!!
 

You need to read the header information from the file first
what is the header information?
im taking lots of pictures and not just one (im doing fingerprint recognition)..do i need to set fixed dimensions for every input file before any further process?
err..can u tell me roughly about PGM?
thanks for helping this newbie... :cry:
 

Remember, not everything in a BMP file is picture information. And this is the case with all picture formats. Some bytes are related to size and color of picture so you need not put that information in your image array.

To understand the structure of BMP file read this **broken link removed**

And for PGM/PPM File read this
**broken link removed**
**broken link removed**
 

i thought for each array element, it's equivalent to 1 byte which is 8 bits.. if it's 11111111, then equals to white, 00000000 equals to black rite?
that's y threshold can be applied here. isnt that right?
 

i've gone thru the article..that means if i wanna use bmp format, i'll have to define the header issit? something like this? :

Code:
typedef struct tagBITMAPFILEHEADER {    /* bmfh */
    UINT    bfType;
    DWORD   bfSize;
    UINT    bfReserved1;
    UINT    bfReserved2;
    DWORD   bfOffBits;
} BITMAPFILEHEADER;

how many colors are there for greyscale bitmap?

sorry im not really good in C++ though...
 

Yes, you have to do it that way . For more help on opening BMP files in C++ try searching for some sample code on Google.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top