Mutual conversion between iplimage cbitmap hbitmap bitmap

Source: Internet
Author: User

Note: I have verified a function for converting hbitmap to iplimage, but vs prompts that the memory is null, which may not be correct.

// Karl_bmp .h

/*************************************** *************************
* Function: mutual conversion between iplimage cbitmap hbitmap bitmap
* Write-by: Shadowwalker
* Date: 2012-4-25
①. Iplimage 2 cbitmap ②. cbitmap 2 iplimage
③ Hbitmap 2 iplimage ④. iplimage 2 hbitmap
⑤ Hbitmap 2 cbitmap 6. hbitmap 2 cbitmap
7. Bitmap 2 cbitmap worker. Bitmap 2 hbitmap
**************************************** ************************/
// 1. iplimage 2 cbitmap
Cbitmap * iplimage2cbitmap (const iplimage * pimage)
{
If (pimage & pimage-> depth = ipl_depth_8u)
{
HDC = getdc ()-> getsafehdc ();
Uchar buffer [sizeof (bitmapinfoheader) + 1024];
Bitmapinfo * BMI = (bitmapinfo *) buffer;
Int BMP _w = pimage-> width, BMP _h = pimage-> height;
Fillbitmapinfo (BMI, BMP _w, BMP _h, pimage-> depth * pimage-> nchannels, pimage-> origin );

Char * pbits = NULL;
Hbitmap = createdibsection (HDC, BMI, dib_rgb_colors, (void **) & pbits, null, 0 );
Memcpy (pbits, pimage-> imagedata, pimage-> imagesize );
Cbitmap * pbitmap = new cbitmap;
Pbitmap-> attach (hbitmap );

Return pbitmap;
}
Else
Return NULL;
}

Void fillbitmapinfo (bitmapinfo * BMI, int width, int height, int bpp, int origin)
{
Assert (BMI & width> = 0 & height> = 0 & (bpp = 8 | bpp = 24 | bpp = 32 ));

Bitmapinfoheader * bmih = & (BMI-> bmiheader );

Memset (bmih, 0, sizeof (* bmih ));
Bmih-> bisize = sizeof (bitmapinfoheader );
Bmih-> biwidth = width;
Bmih-> biheight = origin? ABS (height):-ABS (height );
Bmih-> biplanes = 1;
Bmih-> bibitcount = (unsigned short) BPP;
Bmih-> bicompression = bi_rgb;

If (bpp = 8)
{
Rgbquad * palette = BMI-> bmicolors;
Int I;
For (I = 0; I <256; I ++)
{
Palette [I]. rgbblue = palette [I]. rgbgreen = palette [I]. rgbred = (byte) I;
Palette [I]. rgbreserved = 0;
}
}
}

// 2. cbitmap 2 iplimage
Iplimage * cbitmap2iplimage (const cbitmap * pbitmap)
{
Dibsection Ds;
Pbitmap-> GetObject (sizeof (DS), & Ds );
Iplimage * pimage = cvcreateimage (cvsize (Ds. dsbm. bmwidth, DS. dsbm. bmheight), 8, DS. dsbmih. bibitcount/8 );
Memcpy (pimage-> imagedata, DS. dsbm. bmbits, pimage-> imagesize );
Return pimage;
}

// 3. hbitmap 2 iplimage
Iplimage * hbitmap2ipl (hbitmap hbmp)
{
Bitmap BMP;
: GetObject (hbmp, sizeof (Bitmap), & BMP); // hbmp --> BMP
Int nchannels = BMP. bmbitspixel = 1? 1: BMP. bmbitspixel/8;
Int depth = BMP. bmbitspixel = 1? Ipl_depth_1u: ipl_depth_8u;
Iplimage * IMG = cvcreateimage (cvsize (BMP. bmwidth, BMP. bmheight), depth, nchannels); // cvcreateimageheader
// Pbuffer = (char *) malloc (BMP. bmheight * BMP. bmwidth * nchannels * sizeof (char ));
Memcpy (IMG-> imagedata, (char *) (BMP. bmbits), BMP. bmheight * BMP. bmwidth * nchannels );
Iplimage * DST = cvcreateimage (cvgetsize (IMG), IMG-> depth, 3 );
Cvcvtcolor (IMG, DST, cv_bgra2bgr );
Cvreleaseimage (& IMG );
Return DST;
}
// 4. iplimage 2 hbitmap
Hbitmap iplimage2hbitmap (iplimage * pimg)
{
Byte TMP [sizeof (bitmapinfo) + 1024];
Bitmapinfo * BMI = (bitmapinfo *) TMP;
Hbitmap hbmp;
Int I;
Memset (BMI, 0, sizeof (bitmapinfo ));
BMI-> bmiheader. bisize = sizeof (bitmapinfoheader );
BMI-> bmiheader. biwidth = pimg-> width;
BMI-> bmiheader. biheight = pimg-> height;
BMI-> bmiheader. biplanes = 1;
BMI-> bmiheader. bibitcount = pimg-> nchannels * pimg-> depth;
BMI-> bmiheader. bicompression = bi_rgb;
BMI-> bmiheader. bisizeimage = 0; // If bicompression is bi_rgb, this can be 0
BMI-> bmiheader. biclrimportant = 0;

Switch (pimg-> nchannels * pimg-> depth)
{
Case 8:
For (I = 0; I <256; I ++ ){
BMI-> bmicolors [I]. rgbblue = I;
BMI-> bmicolors [I]. rgbgreen = I;
BMI-> bmicolors [I]. rgbred = I;
}
Break;
Case 32:
Case 24:
(DWORD *) BMI-> bmicolors) [0] = 0x00ff0000;
(DWORD *) BMI-> bmicolors) [1] = 0x0000ff00;
(DWORD *) BMI-> bmicolors) [2] = 0x000000ff;
Break;
}
Hbmp =: createdibsection (null, BMI, dib_rgb_colors, null, 0, 0 );

Setdibits (null, hbmp, 0, pimg-> height, pimg-> imagedata, BMI, dib_rgb_colors );

Return hbmp;
}
// 5. hbitmap 2 cbitmap
Cbitmap hbitmap2cbitmap (hbitmap)
{
Cbitmap;
Cbitmap. Attach (hbitmap );
Return cbitmap;
}
6. cbitmap 2 hbitmap
Hbitmap cbitmap2hbitmap (cbitmap Bitmap)
{
Hbitmap BMP = hbitmap (Bitmap );
// BMP = (hbitmap) bitmap. getsafehandle ();
Return BMP;
}

// 7. Bitmap 2 cbitmap
Cbitmap bitmap2cbitmap (bitmap BMP)
{
Cbitmap bitmap;
Bitmap. getbitmap (& BMP );
Return bitmap;
}
// 8. hbitmap 2 bitmap
Bitmap hbitmap2bitmap (hbitmap hbmp)
{
Bitmap BMP;
: GetObject (hbmp, sizeof (Bitmap), & BMP );//
Return BMP;
}

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.