Re: Converting Bitmap into 2D-Array
Lucress Carol wrote:
I'm doing image processing for the first time with VC++ 6.0 .
First problem here: VC6 is really old, unsupported by the vendor and far
from standard compliant.
I would like to create a function which takes a grayscale bitmap image
and convert it to a 2D array where each element represents one of the
image's pixel intensities for further purposes e.g. to calculate the fft.
Depending on what you actually want to do, I would rather not use a 2D
array. Rather, create a class like this:
struct bitmap {
struct column {
pixel const& operator[](size_t row) const;
pixel& operator[](size_t row);
...
};
column const& operator[](size_t col) const;
column& operator[](size_t col);
...
};
IOW, hide that there is an array behind it and also the organisation of the
array.
typedef struct
{
unsigned width, height;
char *data;
} BMP_Data;
Okay, two things here:
1. Don't use 'typedef struct { ... } somename;' in C++. In C, it was used in
order to create a typename which you could use without referring to the
type as "struct foo;".
2. Don't use raw pointers. In this case, you should rather use a std::vector
to store the data.
bitmap *bitmap;
char sign[3];
int gray_value;
Take the habit of always initialising variables when you declare them. Note
that in C++ as opposed to C89, you can declare variables everywhere and not
just at the beginning of a block. Preferably keep the scope of variables as
small as possible, this helps understanding the code.
int** StoreBmp=new int[bitmap.width][bitmap.height];
Use a std::vector here. Note that it isn't that easy to get a
two-dimensional one though. Rather, I'd suggest using a 1D vector of size
width*height.
file.open(filename,ios::binary|ios::in);
Make sure that you understand what the 'binary' flag means, I guess that
it's not what you intend it to be.
if (!file)
{
printf("Can not open the file.\n",filename);
return NULL;
}
I'd suggest that you simply
throw std::runtime_error("failed to open file '"+filename+"'");
and then catch&log the error somewhere.
if (bitmap->bitmap.width <= 0 || bitmap->bitmap.height <= 0)
{
printf("\nThe file %s contains false imagesize.",filename);
goto error_exit;
}
dito.
while(!file.eof()
This is almost always wrong. You should rather read a value and then see if
reading was successful. eof() can't promise you that your next read
operation will be successful.
for (int i=0;i<bitmap->height;i++)
{
for (int j=0;j<bitmap->width;j++,c--)
{
/* I don't know how I can introduce StoreBmp hier */
file.get(sign),
}
}
It's hard to tell what this means and how it should work. It would help if
you could explain in words what you are doing here. Also, what is the
format of the bitmap file which you are reading?
file.close();
The destructor of 'file' will close it when you return from the function, no
need for this.
/* I'm not sure if I can write something like this because i can have
to delete the memory reserved by the keyword new */
return StoreBmp;
This is correct (though not very elegant and safe) C++, you can well return
something from a function which you allocated with 'new'.
Just one last thing: It seems to me that you are trying to solve too many
problems at once. Try solving one at a time.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Michael W??hrmann, Amtsgericht Hamburg HR B62 932