The following is the source program of the projection method, the second parameter is a bool variable, for true, the projection in the horizontal direction, otherwise in the vertical direction of the projection. Note that we are aiming at a two-value map, but for the convenience of processing, with a level 256 grayscale, but only 0 and 2,552 gray level.
BOOL projection (HWND hwnd,bool Hori)
{
DWORD offbits,bufsize;
Lpbitmapinfoheader Lpimgdata;
LPSTR lpptr;
Hlocal Htempimgdata;
Lpbitmapinfoheader Lptempimgdata;
LPSTR lptempptr;
HDC HDC;
hfile HF;
LONG X,y;
int num;
Use a level 256 grayscale, but only 0 and 2,552 shades of gray are used.
if (numcolors!=256) {
MessageBox (HWnd, "must be a mono bitmap with grayscale palette!",
"Error message", mb_ok| Mb_iconexclamation);
return FALSE;
}
Offset value of map data in place
Offbits=bf.bfoffbits-sizeof (Bitmapfileheader);
Buffer size
Bufsize=offbits+bi.biheight*linebytes;
allocating memory for new diagram buffers
if ((Htempimgdata=localalloc (lhnd,bufsize)) ==null)
{
MessageBox (hWnd, "Error alloc memory!", "error message",
mb_ok| Mb_iconexclamation);
return FALSE;
}
Lpimgdata= (Lpbitmapinfoheader) GlobalLock (himgdata);
Lptempimgdata= (Lpbitmapinfoheader) LocalLock (htempimgdata);
New diagram buffer initialized to 255
Memset (Lptempimgdata, (BYTE) 255,bufsize);
Copy header information
memcpy (lptempimgdata,lpimgdata,offbits);
if (Hori)
{
Horizontal projection
for (y=0;y<bi.biheight;y++) {
Lpptr= (char *) lpimgdata+ (bufsize-linebytes-y*linebytes);
num=0; Counter initialized to 0
for (x=0;x<bi.biwidth;x++)
if (* (lpptr++)!=0)//is a white dot
num++; Counter plus 1
Lptempptr= (char *) lptempimgdata+ (bufsize-linebytes-y*linebytes);
for (x=0;x<num;x++)
* (lptempptr++) = 0; In the new diagram, the bank has a num black dot
}
}
else{//Vertical projection
for (x=0;x<bi.biwidth;x++) {
num=0; Counter initialized to 0
Lpptr= (char *) lpimgdata+ (bufsize-linebytes) +x;
for (y=0;y<bi.biheight;y++) {
if (*lpptr!=0)
num++; Counter plus 1
Lpptr-=linebytes;
}
Lptempptr= (char *) lptempimgdata+offbits+x;
for (y=0;y<num;y++) {
*lptempptr=0; In the new chart, there are num black dots in the column.
Lptempptr+=linebytes;
}
}
}
if (hbitmap!=null)
DeleteObject (HBITMAP);
HDC=GETDC (HWND);
Create a new bitmap
Hbitmap=createdibitmap (HDc, (Lpbitmapinfoheader) Lptempimgdata,
(LONG) Cbm_init,
(LPSTR) lptempimgdata+
sizeof (Bitmapinfoheader) +
Numcolors*sizeof (Rgbquad),
(Lpbitmapinfo) Lptempimgdata,
Dib_rgb_colors);
A different result file name
if (Hori)
Hf=_lcreat ("C:\\hproject.bmp", 0);
Else
Hf=_lcreat ("C:\\vproject.bmp", 0);
_lwrite (HF, (LPSTR) &bf,sizeof (Bitmapfileheader));
_lwrite (HF, (LPSTR) lptempimgdata,bufsize);
_lclose (HF);
Freeing Memory and resources
ReleaseDC (HWND,HDC);
LocalUnlock (Htempimgdata);
LocalFree (Htempimgdata);
GlobalUnlock (Himgdata);
return TRUE;
}