Advertisement

03.05.2008 at 12:39AM PST, ID: 23215272
[x]
Attachment Details

Converting Bitmap in 2D Array

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6
Tags:

C++, image processing,bitmap,Win32 API

Hi everyone ,
I'm doing image processing for the first time and I'm working with VC++ 6.0 under WinXP.
I would like to create a function which loads an 8 bit grayscale bitmap image and convert it
into a 2D array where each element represents one of the image's
pixel intensities for further purposes e.g. to calculate the fft.I saw quite similar
examples on this homepage but none of them returns an 2D array.
My code is attached in Code Snippet
The problem is that I would like to have the values of the array returned and I don't know how it should look like inside LRESULT CALLBACK WndProc function(I'm using Win32 API).And I'm also getting error message because of following declarations:
BYTE  tempScanLine[WIDTH * 1]  // error C2057:it should be a constant expression
                                                    //error C2466 : can't reserved an array of size 0
                                                 //  error C2133: 'tempScanLine' : unknown parameter
const double grayscale[WIDTH*HEIGHT]; //  error C2133: 'grayscale' : unknown parameter
I thought of writing grayscale=new double [WIDTH*HEIGHT] but then I have to give the memory free
and I can't return the array anymore right?
Thank you
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
static HBITMAP hbitmap;
int WIDTH, HEIGHT;
switch (message)
     {
   case WM_PAINT:
				
HANDLE hObj = LoadImage(NULL,"image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hdc = CreateCompatibleDC(NULL);
SelectObject(hdc, hObj);
BITMAPINFO bInfo;
bInfo.bmiHeader.biSize = sizeof(bInfo.bmiHeader);
bInfo.bmiHeader.biWidth = WIDTH;
bInfo.bmiHeader.biHeight = HEIGHT;
bInfo.bmiHeader.biPlanes = 1;
bInfo.bmiHeader.biBitCount = 8; 
bInfo.bmiHeader.biCompression = BI_RGB;
BYTE tempScanLine[WIDTH ];
BYTE info[HEIGHT][WIDTH][1];
BITMAPINFO bi;
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
int count = HEIGHT - 1;
const double grayscale[WIDTH*HEIGHT];
for(int j = 0; j < HEIGHT; ++j)
{
      // find the width of the object
      GetDIBits(hdc, (HBITMAP) hObj, j, 1, NULL, &bi, DIB_RGB_COLORS);
 
      // store the color values in a 3D array of bytes
      GetDIBits(hdc, (HBITMAP) hObj, j, 1, tempScanLine, &bInfo, DIB_RGB_COLORS);
      for(int z = 0; z < bi.bmiHeader.biWidth; ++z)
      {
            info[count][z][0] = tempScanLine[(z * 1) + 2];    
            info[count][z][1] = tempScanLine[(z * 1) + 1];     
            info[count][z][2] = tempScanLine[(z * 1) + 0];                   
          //Transform to gray values
            grayscale[z]=info[count][z][0]*0.299 + info[count][z][1]*0.587+info[count][z][2]*0.114;
      }
     --count;
}
Answered By: DanRollins
Expert Since: 04/18/2000
Accepted Solutions: 4287
Computer Expertise: Guru
DanRollins has been an Expert for 8 years 8 months, during which he has posted 68077 comments and answered 4287 questions. DanRollins is just one of 346 experts in the Microsoft Visual C++ Zone. 2 experts collaborated on this answer, which was graded an "A" by the asker.
 
 
 
 
20081119-EE-VQP-47 / EE_QW_2_20070628