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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 03:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01: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, 07:50 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM