need help in how to convert bit map image in txo dimensionnal array

This is a discussion on need help in how to convert bit map image in txo dimensionnal array within the C++ Programming forums, part of the General Programming Boards category; Iím studying computer science at the university and i have never use Visual C++. I had to realize a simple ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    need help in how to convert bit map image in txo dimensionnal array

    Iím studying computer science at the university and i have never use Visual C++.

    I had to realize a simple image processing software with visual C++. My image analysis algorithms work on an image represented by a two dimensional pixel array . I know how to work on my pixel matrix, but I really donít know how to extract this matrix from the Bitmap image I had to analyse.

    Thus I am searching a visaul C++ fonction convertImage, that can convert a monochromatic (black and white) bitmap image , with an A4 format, into a pixel matrix , that I will use in my program.

    Here is the specification of the function that I need :

    // definition of the matrix type that will be used for
    // represnting the image
    Typedef int Matrix [][] ;

    // fonction that convert the bitmap image i had to analyse // into a pixel matrix
    Matrice convertImage ( const bmp image ) ;

    thank you for your help

    there is an image which illustrate the problme in this link.....
    http://asso.bandrele.anpcba.free.fr/convert.jpg
    Last edited by beide; 03-02-2005 at 03:46 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Hi, welcome to the forums!

    Unfortunately, we don't have enough information to help you with your query. To give you help with this problem we will need the following:
    • Details on the Matrix type. Typedef int Matrix [][] ; is not a valid statement in C++. Therefore, it may be just a guide. Do you have any more information on it?
    • Details of the bmp type. bmp is not a C++ or Windows type and most of us aren't familiar with it. Could you post a definition or documentation page?
    • Code that you've tried or a specific query. We are not able to write code from scratch, so you should provide code that you have tried and we can make suggestions on making it work. If you're getting any errors when you compile your code you should post the error text. Alternatively, you can ask a specific question that we can try to answer.

    Thanks.

    P.S You may be able to get some general help by trying the following board searches:
    Search: Multi-dimension arrays
    Search: Matrix
    Last edited by anonytmouse; 03-02-2005 at 03:10 PM.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    http://www.wotsit.org/search.asp?s=graphics
    Look at the bitmap specs here

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    hi anonytmouse,

    I am not a profesional programer but jus a student, i have to develop a software able to analyse an bit map image and describe images(geometrical shapes) for example a squard or circle..........so the responsible of project advise me to develop the software in windev thanks to w-language but i think that the source code which should analyse images must be xritten in c or c++.this is the reason why i choose to develop on visual c++ but i've never used visual c++.............if you you have an idea to resolve problem.........thank you....try to see this link:http://asso.bandrele.anpcba.free.fr/convert.jpg.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    hi sand_man,

    thank you, but i've ever had this link............this is only the bitmap specs but it doest help me work on images........

  6. #6
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Are you doing grayscale or color? Grayscale BMPs are the easiest.

    Once you read in the header (Google it for exact specs) and find the dimensions, allocate that much space on the heap in your 2-dimensional array. Make sure it's grayscale (also in the header) or 256 colors. Then, each byte will correspond to that pixel's color (or color index).

    so it's like this:

    <-----header----><------------------------data------------------------>

    Just loop, assigning the next byte to the element in the array you have.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A bitmap is a pixel array. Each pixel corresponds to one element in the array be it a BYTE, WORD, or DWORD.

    Here is code to read from a pixel array.

    Code:
    ...
    ...
    
    struct RAWHeader
    {
      DWORD dwWidth;
      DWORD dwHeight;
      DWORD dwChunkSize;
    };
      
    void Copy32(DWORD *dwpSource,DWORD *dwpDest,DWORD dwSize);
    void Copy32ASM(DWORD *dwpSource,DWORD *dwpDest,DWORD dwSize);
    
    
    
    void Copy32(DWORD *dwpSource,DWORD *dwpDest,DWORD dwSize)
    {
       //byte copy
       memcpy((DWORD *)dwpDest,(DWORD *)dwpSource,dwsize*sizeof(DWORD));
    }
    
    void Copy32ASM(DWORD *dwpSource,DWORD *dwpDest,DWORD dwSize)
    {
       //dword copy
       asm {
         mov    esi,[dwpSource]
         mov    edi,[dwpDest]
         mov    ecx,dwSize
         rep     movsd
         and    ecx,03h
         rep     movsb
       }
    }
    
    int main(void)
    {
       //Setup pointers
       DWORD *dwpImage=Image.GetBufferPointer();
       DWORD dwSize=Image.m_dwWidth*Image.m_dwHeight;
    
       //Allocate space for image in pixel array
       DWORD *dwpPixelArray=new DWORD[size];
    
       //If valid copy from image to pixel array
       if (dwpPixelArray) Copy32ASM(dwpImage,dwpPixelArray,dwSize);
    
       //Open a file
       int handle=_open("pixelarray.raw",_O_BINARY,_S_IREAD);
       
       //Check for valid handle
       if (handle!=-1)
       {
         //Write out header to disk
         RAWHeader temp;
         temp.dwHeight=Image.dwHeight;
         temp.dwWidth=Image.dwWidth;
    
         //Size in DWORDS, not bytes
         temp.dwChunksize=dwSize;
         _write(handle,(RAWHeader *)&temp,sizeof(RAWHeader));
    
         //Write out data - size in total bytes
         _write(handle,(DWORD *)dwpPixelArray,dwSize*sizeof(DWORD));
    
         //Close the file
         _close(handle);
       return (0);
    }
    This will copy data from the image or screen to an array and write the array to disk. It also writes a small header to disk. Using this setup you can write as many chunks to the disk as you want. So you could store multiple images in the file this way.

    All the screen is, or any bitmap is, is an array of pixels. So it's just a memory to memory copy or bit block transfer - bitblt, or just a simple blit.

    Sorry for using assembly, but this kind of programming task can really make use of the power of assembly to move large chunks of data from one memory location to another.

    A linear array is the same as a 2D array conceptually and technically. The 2D portion is only a convention written into the compiler to make 2D arrays possible and flexible. But these are the same thing.

    Code:
    int width=20;
    int height=30;
    
    DWORD *map=new DWORD[width*height];
    
    DWORD map[width][height];
    So:

    Code:
    DWORD value=map[x][y];
    is the same as:

    Code:
    DWORD value=map[(y*width)+x];
    In fact the latter is exactly what the compiler does when you access a 2D array in the form map[x][y]. It auto-computes the correct offset in memory. I think linear arrays are much easier to work with personally.
    Last edited by VirtualAce; 03-03-2005 at 02:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Hi, how to convert a grayscale image into 8 bit image?
    By Alexanderbinich in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2005, 06:50 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21