Re: Converting Bitmap into 2D-Array




"Lucress Carol" <incognito.me@xxxxxx> ha scritto nel messaggio
news:b8f26056-f07b-43cc-b72d-d4569993f623@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

With the statement:
LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE) I
have to use Win32 API or MFC right?
is it then possible to use GetDIBits in a console Application?Can I
store
with GetDLBits the 8-bit-grayscale image data in an array?because I
need an array which
I will pass to my function in order to calculate the fft.

You may consider a bit useful the following code I developed (more testing
required, but it seems to work on VC++2008):

I defined a simple template class Matrix2D to store 2D matrix data (matrix
elements are stored as 1D array, row-wise); and there is a function
ExtractGrayScaleFromImage, which extracts grayscale values (expressed as
floats) from an input CImage instance; e.g.:

// CImage img;
Matrix2D<float> grayScale; // output
ExtractGrayScaleFromImage( img, grayScale );

The CImage class has easy-to-use methods to get and set a specific pixel
values, etc. (It may be not as fast as GetDIBits, but you can optimize code
later, if you need; it is however more programmer-friendly IMHO than pure
Win32 APIs like GetDIBits.)

<code>

//
// Stores 2D matrix
// (Does bounds-checking in debug builds, using ASSERT macro.)
//
template <typename T>
class Matrix2D
{
public:

//
// CONSTRUCTION
//

// Constructs an empty matrix
Matrix2D()
: _rows(0), _cols(0)
{
}

// Constructs a 2D matrix, with given dimensions
explicit Matrix2D( int rows, int cols )
: _rows(rows), _cols(cols)
{
// Check parameters
ASSERT( rows > 0 );
ASSERT( cols > 0 );

// Make room in array
_data.resize( _rows * _cols );
}


//
// ATTRIBUTES
//

// Returns number of rows
int Rows() const
{
return _rows;
}

// Returns number of columns
int Columns() const
{
return _cols;
}

bool IsEmpty() const
{
if ( _rows == 0 && _cols == 0)
{
ASSERT( _data.empty() );
return true;
}
else
{
ASSERT( _rows > 0 && _cols > 0 );
return false;
}
}


//
// OPERATIONS
//

// Gets item at specified position in matrix
const T & Get( int row, int col ) const
{
ASSERT( IsValidIndex(row, col) );
return _data[ Get1DIndex( row, col ) ];
}

// Sets item value at a specified position in matrix
void Set( int row, int col, const T & value )
{
ASSERT( IsValidIndex(row, col) );
_data[ Get1DIndex( row, col ) ] = value;
}

// Changes matrix size
void Resize( int rows, int cols )
{
ASSERT( rows > 0 && cols > 0 );
_rows = rows;
_cols = cols;
_data.clear();
_data.resize( _rows * _cols );
}

// Makes it an empty matrix
void Clear()
{
_rows = _cols = 0;
_data.clear();
}


// Saves matrix data into 1D vector (row-wise)
void CopyRowwise( OUT std::vector< T > & v ) const
{
v = _data;
}


//
// IMPLEMENTATION
//
private:

//
// DATA MEMBERS
//

// 2D data stored as 1D array, row-wise
std::vector< T > _data;

// Row count
int _rows;

// Column count
int _cols;


//
// HELPER METHODS
//

// Checks if (row, col) pair is valid
bool IsValidIndex( int row, int col ) const
{
ASSERT( !IsEmpty() );

if ( 0 <= row && row < _rows &&
0 <= col && col < _cols )
{
return true;
}
else
{
return false;
}
}

// Converts from 2D (row, col) to 1D index into array
int Get1DIndex( int row, int col ) const
{
ASSERT( IsValidIndex( row, col ) );

// Convert from 2D to 1D
int index = row * _cols + col;

ASSERT( 0 <= index && index < (int)_data.size() );
return index;
}
};


//
// Extracts gray-scale data from an image.
//
void ExtractGrayScaleFromImage(
IN const CImage & image,
OUT Matrix2D<float> & grayData )
{
// Get input image size
int width = image.GetWidth();
int height = image.GetHeight();

// Resize matrix to accommodate new data
grayData.Resize(height, width);

// For each pixel in image:
for ( int row = 0; row < height; row++ )
{
for ( int col = 0; col < width; col++ )
{
// Get color of current pixel
COLORREF cr = image.GetPixel( col, row );
ASSERT( cr != CLR_INVALID );

// Extract color components (R,G,B)
BYTE red = GetRValue(cr);
BYTE green = GetGValue(cr);
BYTE blue = GetBValue(cr);

//
// Convert from RGB to gray scale, using:
//
// Y = 0.3*R + 0.59*G + 0.11*B
//
float gray = 0.3f*red + 0.59f*green + 0.11f*blue;

// Store gray value in output matrix
grayData.Set( row, col, gray );
}
}
}

</code>

HTH,
Giovanni


.



Relevant Pages

  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • [2.6.6-BK] NTFS 2.1.11 Really final cleanups.
    ... throughout the NTFS code to ntfschar since uchar_t is already defined by POSIX ... +BOOL find_attr(const ATTR_TYPES type, const ntfschar *name, const u32 name_len, ... register int rc; ...
    (Linux-Kernel)
  • Re: The proof that I was referring to is on the website
    ... You construct a "halt analyzer"... ... int WillHaltPO(char const * machine, ... and now we see that Naughty doesn't halt. ...
    (sci.logic)