Re: Converting Bitmap into 2D-Array
On 21 Feb., 13:40, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
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 th=
e
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::vect=
or
to store the data.
bitmap *bitmap;
char sign[3];
int gray_value;
Take the habit of always initialising variables when you declare them. Not=
e
that in C++ as opposed to C89, you can declare variables everywhere and no=
t
just at the beginning of a block. Preferably keep the scope of variables a=
s
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 i=
f
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 retur=
n
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=E4ftsf=FChrer: Michael W=F6hrmann, Amtsgericht Hamburg HR B62 932
Thank you for the advices.For my purposes i have to store grayscale
bitmap
images (256x256 0r 512x512) in arrays (vector) in order to calcute the
fft.
With the following two for loops :
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);
}
}
I wanted to store each pixel gray value of each line in the array but
I didn't
find out how i could managed to do it...
now my idea ist (but i'm not really sure):
-create a class for the header
-create a vector for the data
-open the file
-read the file and store each pixel gray value in the vector
But my problem is that the vector of gray values should be given to
the
function which has to calculate the fft and I have never worked before
with vector.I'm not an c++-expert but i guess i have to write the
following statement
#include<vector> right?
Another question.what does the following statement means?I didn't know
one can implement two strutures once at the time.......
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);
...
};
Do you mind explaining me this?
Thank you
Carol