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.