A class to read bitmap and display.

Source: Internet
Author: User

Header file

# Pragma once
# Include <vector>
Using namespace STD;
Typedef unsigned char * hmybitmap;
Const cstring file_type_error = "it is not bitmap file! ";
Const cstring file_read_error = "Read File error! ";
Const cstring file_open_error = "Open File error! ";
Const int max_size = 50;
Const word palversion = 0x300;
Typedef struct tagbitmappoint
{
Int pointx;
Int pointy;
Hmybitmap pbitmapdata;
} Bitmappoint, * lpbitmappoint;

Typedef vector <bitmappoint> vbitmappoint;
Class cmybitmap
{
Public:
Cmybitmap (void );
Virtual ~ Cmybitmap (void );
// Read bitmap files
Void readbitmap (const cstring & strfilename, cstring & strerror );
PRIVATE:
// The handle pointing to the bitmap
Hmybitmap hbitmap;
// Pointer to the header of the bitmap file
Bitmapinfoheader * pbitmapinfoheader;
// File Header Structure
Bitmapfileheader m_bitmapfileheader;
// The color palette of the currently read bitmap
Cpalette * pcpalette;
// Point to the data area of the bitmap
Hmybitmap hbitmapdata;
Public:
// Create a color palette for the current bitmap
Void createpalette (void );
Void paintbitmap (CDC * PDC, int destinationx, int destinationy, DWORD width, DWORD height, int resourcex, int resourcey, uint Startline, uint scanlines, uint coloruse = dib_rgb_colors );
 
Ulonglong getbitmapwidth (void );
Ulonglong getbitmapheight (void );
Hmybitmap getbitmap (void );
// Convert a 256-color image to a 2-value image
Bool bitmaptobinarymap (void );
// Obtain the pixel coordinates of the vertices at the top after the image deflection.
Cpoint gettopanglepoint (void );
PRIVATE:
// Save the detected black points
Vbitmappoint m_vbitmappoint;
Public:
Void searchleft (cpoint point, Int & Deep );
Void searshortght (cpoint point, Int & Deep );
};

Implementation File

# Include "stdafx. H"
# Include "./bitmap. H"
# Include <math. h>
# Include "mylog. H"

Const int max_deep = 200;

Cmybitmap: cmybitmap (void)
: Pbitmapinfoheader (null)
, Pcpalette (null)
, Hbitmap (null)
, Hbitmapdata (null)
, M_vbitmappoint (0)
{
}

Cmybitmap ::~ Cmybitmap (void)
{
}

// Read bitmap files
Void cmybitmap: readbitmap (const cstring & strfilename, cstring & strerror)
{
Cfile file;
 
If (file. Open (strfilename, cfile: modereadwrite ))
{
// Read the file header to determine if it is a bitmap file
If (sizeof (bitmapfileheader) = file. Read (& m_bitmapfileheader, sizeof (bitmapfileheader )))
{
If (m_bitmapfileheader.bftype = 19778)
{
// Allocate memory controls to files
Ulonglong bitmapfilesize = file. getlength ()-sizeof (bitmapfileheader );
If (hbitmap! = NULL)
{
Delete [] hbitmap;
Hbitmap = NULL;
}
If (pcpalette! = NULL)
{
Delete pcpalette;
Pcpalette = NULL;
}
Hbitmap = (hmybitmap) New char [(size_t) bitmapfilesize];
If (file. Read (hbitmap, bitmapfilesize) = bitmapfilesize)
{
Pbitmapinfoheader = (bitmapinfoheader *) hbitmap;
// Create a color palette
Hbitmapdata = hbitmap + (m_bitmapfileheader.bfoffbits-sizeof (bitmapfileheader ));
Createpalette ();
}
Else
{
Strerror = file_read_error;
}
}
Else
{
Strerror = file_type_error;
}
}
Else
{
Strerror = file_read_error;
}
}
Else
{
Strerror = file_open_error;
}
File. Close ();
}

// Create a color palette for the current bitmap
Void cmybitmap: createpalette (void)
{
Pcpalette = new cpalette;
Double colornum = POW (2, pbitmapinfoheader-> bibitcount );
Double palettesize = sizeof (logpalette) + colornum * sizeof (paletteentry );
// Define the logical palette and allocate space for it
Plogpalette lplogpalette = (plogpalette) New char [(size_t) palettesize];
// Fill the logical palette
Lplogpalette-> palversion = palversion;
Lplogpalette-> palnumentries = (Word) colornum;
Lpbitmapinfo ppaletteentry = (lpbitmapinfo) hbitmap;
# Ifdef ndebug
Cmylog log;
Cstring palcolor;
Char Buf [10];
# Endif
For (INT I = 0; I <colornum; I ++)
{
Lplogpalette-> palpalentry [I]. pered = ppaletteentry-> bmicolors [I]. rgbred;
Lplogpalette-> palpalentry [I]. pegreen = ppaletteentry-> bmicolors [I]. rgbgreen;
Lplogpalette-> palpalentry [I]. peblue = ppaletteentry-> bmicolors [I]. rgbblue;
Lplogpalette-> palpalentry [I]. peflags = 0;

# Ifdef ndebug
Palcolor = "The redcolor: bluecolor: greencolor is :";
ITOA (INT) lplogpalette-> palpalentry [I]. pered, Buf, 10 );
Palcolor + = Buf;
Palcolor + = ":";
ITOA (INT) lplogpalette-> palpalentry [I]. peblue, Buf, 10 );
Palcolor + = Buf;
Palcolor + = ":";
ITOA (INT) lplogpalette-> palpalentry [I]. pegreen, Buf, 10 );
Palcolor + = Buf;
Log. writelog (palcolor );
# Endif

}
// Create the actual palette through the logical palette
Pcpalette-> createpalette (lplogpalette );
Delete [] (char *) lplogpalette;
}

Void cmybitmap: paintbitmap (CDC * PDC, int destinationx, int destinationy, DWORD width, DWORD height, int resourcex, int resourcey, uint Startline, uint scanlines, uint coloruse)
{
: Setstretchbltmode (PDC-> m_hdc, coloroncolor );
// Hpalette ppal = (hpalette) pcpalette-> m_hobject;
// Hpalette poldpalette =: selectpalette (PDC-> m_hdc, ppal, true );
Cpalette * poldpalette = PDC-> selectpalette (pcpalette, true );
: Setdibitstodevice (PDC-> m_hdc, destinationx, destinationy, width, height, resourcex, resourcey, Startline, scanlines, hbitmapdata, (bitmapinfo *) hbitmap, coloruse );
PDC-> selectpalette (poldpalette, true );
}

Ulonglong cmybitmap: getbitmapwidth (void)
{
Ulonglong width = pbitmapinfoheader-> biwidth;
Return (width + 2)/4) * 4;
}

Ulonglong cmybitmap: getbitmapheight (void)
{
Return pbitmapinfoheader-> biheight;
}

Hmybitmap cmybitmap: getbitmap (void)
{
Return hbitmap;
}

// Convert a 256-color image to a 2-value image
Bool cmybitmap: bitmaptobinarymap (void)
{
Int bitmapdatasize = m_bitmapfileheader.bfsize-m_bitmapfileheader.bfoffbits;
Unsigned char * Buf = hbitmapdata;
For (INT I = 0; I <bitmapdatasize; I ++)
{
If (* Buf! = 0)
{
// If (I/pbitmapinfoheader-> biwidth <pbitmapinfoheader-> biheight/2)
{
* Buf = 255;
}
}
Buf ++;
}
Gettopanglepoint ();
Return true;
}

// Obtain the pixel coordinates of the vertices at the top after the image deflection.
Cpoint cmybitmap: gettopanglepoint (void)
{
// Scan the image in a straight line. After obtaining the first Black Point, determine whether the point is in the left half of the graph or the right half of the graph. Then add the step size, tentatively set to 10 rows. If the first point is the corner of the table, you can scan at least two points after adding step 10, the angle between the two vertices and the straight line composed of the first vertex should be about 90 degrees. If this condition is met, a certain point is the corner point of the table.
// After scanning the first point, scan the point around to see whether it is an isolated point or a straight line point.
If (hbitmap = NULL)
{
Return cpoint (0, 0 );
}
Unsigned char * pdata = hbitmapdata;
Int width = pbitmapinfoheader-> biwidth;
Int Height = pbitmapinfoheader-> biheight;
Int deep = 0;
Cpoint point;
For (INT I = height-1; I> = 0; I --)
{
Pdata = hbitmapdata + I * width;
For (Int J = 0; j <width; j ++)
{
If (* pdata = 0)
{
Deep = 0;
Point. x = J;
Point. Y = I;
If (j> width/2)
{
Searchleft (point, deep );
}
Else
{
Searshortght (point, deep );
}
If (deep> = max_deep)
{
Return cpoint (I, j );
}
}
Pdata ++;
}
}
Return cpoint ();
}

Void cmybitmap: searchleft (cpoint point, Int & Deep)
{
If (deep> = max_deep)
{
Return;
}
If (point. x> pbitmapinfoheader-> biwidth | point. x <0 | point. Y <0 | point. Y> pbitmapinfoheader-> biheight)
{
Return;
}
If (* (hbitmapdata + pbitmapinfoheader-> biwidth * point. Y + point. X) = 0)
{
// * (Hbitmapdata + pbitmapinfoheader-> biwidth * point. Y + point. X) = 255;
Deep ++;
Searchleft (cpoint (point. X-1, point. Y), deep );
Searchleft (cpoint (point. X-1, point. Y-1), deep );
Searchleft (cpoint (point. X, point. Y-1), deep );
}
Else
{
Return;
}
}

Void cmybitmap: searshortght (cpoint point, Int & Deep)
{
If (deep> = max_deep)
{
Return;
}
If (point. x> pbitmapinfoheader-> biwidth | point. x <0 | point. Y <0 | point. Y> pbitmapinfoheader-> biheight)
{
Return;
}
If (* (hbitmapdata + pbitmapinfoheader-> biwidth * point. Y + point. X) = 0)
{
// * (Hbitmapdata + pbitmapinfoheader-> biwidth * point. Y + point. X) = 255;
Deep ++;
Searshortght (cpoint (point. x + 1, point. Y), deep );
Searshortght (cpoint (point. x + 1, point. Y-1), deep );
Searshortght (cpoint (point. X, point. Y-1), deep );
}
Else
{
Return;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.