Use magick ++ 6.8.5 in MFC

Source: Internet
Author: User
Tags imagemagick

Magick ++ is the c ++ encapsulation of the ImageMagick Image Library. ImageMagick is a software suite that allows you to create, edit, synthesize, and convert image formats.
Compilation steps:
1. Download the source code package from http://www.imagemagick.org/download/windows/imagemagick-windows.zip;
2. decompress the package to the folder and openConfigure.exeFile, press "Next" to enter the configuration interface, you can set the compilation type and compilation options, here I use the default compilation type, that is, dynamic multi-threaded DLL runtimes type, select the default compilation option, as shown in:

3. Click "Next" until "complete", and you can see that the system automatically generates the file in the ".. \ visualmagick" directory"Visualdynamicmt. sln"File (this name varies depending on different compilation types), open it with vs2008, right-click the" all "project in the solution, and select" clean ", then select "build ". After compilation, you can view the generated file in the "bin" folder under ".. \ visualmagick" and "lib" folder.
4. Open the "\ visualmagick \ bin" directory"Imdisplay.exe", Load any image (Chinese path not supported) to see if it can be properly displayed, as shown in:

The following describes how to use magick ++ in vs2008.
1. Create a Win32 console project, select "Empty Project", and remove "precompiled header". The project name is testmagick;
2. Add a new CPP file named test. cpp. The Code is as follows:

1
2
3
4
5
6
7
8
9
10
11
# Include <magick ++. h>
Using namespace magick;

Int main (INT/* argc */, char ** argv)
{
Initializemagick (* argv );
Image image ("100x100", "White ");
Image. pixelcolor (49, 49, "Red ");
Image. Write ("red_pixel.png ");
Return 0;
}

The code function is to create a x pixel white background image with the center pixel in red.
3. right-click Project → "properties" → "C/C ++", fill in "general" → "Additional include directories" to "X: \ ImageMagick-6.8.5"; "X: \ ImageMagick-6.8.5 \ magick ++ \ Lib "," Preprocessor "→" Preprocessor definitions "add fill"; _ VisualC _; _ crt_secure_no_warnings; _ crt_nonstdc_no_warnings ". "Linker", "general" → "Additional
Library directories "fill in" X: \ ImageMagick-6.8.5 \ visualmagick \ Lib "," input "→" additional dependencies "fill in" core_db_magick ++ _. lib ". "Debugging" → "working directory" fill in "X: \ ImageMagick-6.8.5 \ visualmagick \ bin ".
4. Compile and run the program. You can see the generated file in the ".. \ visualmagick \ bin" directory.Red_pixel.pngFile.

The following describes how to use magick ++ in MFC.
1. Create an MFC single-document application named testmagickmfc;
2. InStdafx. hFile, add the following code:

1
# Include <magick ++. h>

3. InTestmagickmfc. cppAdd the following code to the initinstance function:

1
Magick: initializemagick (null );

4. InTestmagickmfcview. hAdd the following code to the file:

1
2
3
Magick: Image m_pimage;
CDC * moffscreendc;
Void dodisplayimage (CDC * PDC );

5. InTestmagickmfcview. cppAdd the following code to file modification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# If (quantumdepth = 8)
# Define scalequantumtochar (quantum) (unsigned char) (quantum ))
# Elif (quantumdepth = 16)
# Define scalequantumtochar (quantum) (unsigned char) (quantum)/257 ))
# Elif (quantumdepth = 32)
# Define scalequantumtochar (quantum) (unsigned char) (quantum)/16843009ul ))
# Elif (quantumdepth = 64)
# Define scalequantumtochar (quantum )\
(Unsigned char) (quantum)/71777214294589695 ))
# Endif

Ctestmagickmfcview: ctestmagickmfcview ()
: Moffscreendc (null ),
M_pimage (null)
{
}

Ctestmagickmfcview ::~ Ctestmagickmfcview ()
{
If (moffscreendc)
{
Delete moffscreendc;
}
}

Void ctestmagickmfcview: ondraw (CDC * PDC)
{
Ctestmagickmfcdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (! Pdoc)
Return;

Dodisplayimage (PDC );
}

Void ctestmagickmfcview: oninitialupdate ()
{
Cview: oninitialupdate ();

M_pimage.read ("E: \ myprivate \ 1.jpg ");
}

Void ctestmagickmfcview: dodisplayimage (CDC * PDC)
{
If (! M_pimage.isvalid ())
{
Return;
}
If (! PDC)
{
Return;
}

// If we don't already have a ready offscreen, then prepare one
If (! Moffscreendc)
{
// Set up the Windows Bitmap header
Bitmapinfoheader BMI;
BMI. bisize = sizeof (bitmapinfoheader); // size of Structure
BMI. biwidth = (long) m_pimage.columns (); // bitmaps width in pixels
BMI. biheight = (-1) * (long) m_pimage.rows (); // bitmaps height n pixels
BMI. biplanes = 1; // number of planes in the image
BMI. bibitcount = 32; // The number of bits per pixel
BMI. bicompression = bi_rgb; // The type of compression used
BMI. bisizeimage = 0; // the size of the image in bytes
BMI. bixpelspermeter = 0; // horizontal resolution
BMI. biypelspermeter = 0; // veritical resolution
BMI. biclrused = 0; // number of colors actually used
BMI. biclrimportant = 0; // colors most important

Rgbquad * prgbadib = 0;
Hbitmap;
Hbitmap = createdibsection
(
PDC-> m_hdc, // handle to device context
(Bitmapinfo *) & BMI, // pointer to structure containing bitmap size, format, and color data
Dib_rgb_colors, // color data type indicator: RGB values or palette indices
(Void **) & prgbadib, // pointer to variable to receive a pointer to the bitmap's bit values
Null, // optional handle to a file mapping object
0 // offset to the bitmap bit values within the file mapping object
);

If (! Hbitmap)
{
Return;
}

//
// If image is non-opaque, create overlay the image on top
// A pattern background so non-opaque regions become evident.
//

Magick: image = m_pimage;
If (m_pimage.matte ())
{
Magick: Image matteimage;
Matteimage. Size (magick: geometry (m_pimage.columns (), m_pimage.rows ()));
Matteimage. Read ("pattern: checkerboard ");
Matteimage. Composite (m_pimage, 0, 0, magick: atopcompositeop );
Image = matteimage;
}

//
// Extract the pixels from magick ++ image object and convert to a Dib Section
//

Const unsigned int columns = (unsigned INT) image. Columns ();
Const unsigned int rows = (unsigned INT) image. Rows ();

Rgbquad * pdestpixel = prgbadib;

For (unsigned int ROW = 0; row <rows; row ++)
{
Const magick: pixelpacket * ppixels = image. getconstpixels (0, row, columns, 1 );
# If quantumdepth = 8
// Form of pixelpacket is identical to rgbquad when quantumdepth = 8
Memcpy (void *) pdestpixel, (const void *) ppixels, sizeof (pixelpacket) * columns );
Pdestpixel + = columns;

# Else // 16 or 32 bit quantum
// Transfer pixels, scaling to quantum
For (unsigned long npixelcount = columns; npixelcount --)
{
Pdestpixel-> rgbred = scalequantumtochar (ppixels-> Red );
Pdestpixel-> rgbgreen = scalequantumtochar (ppixels-> green );
Pdestpixel-> rgbblue = scalequantumtochar (ppixels-> blue );
Pdestpixel-> rgbreserved = 0;
++ Pdestpixel;
++ Ppixels;
}
# Endif
}

// Create a display surface
Moffscreendc = new CDC ();
Moffscreendc-> createcompatibledc (PDC );

// Now copy the bitmap to Device
Moffscreendc-> SelectObject (hbitmap );
Deleteobject (hbitmap );
}
PDC-> bitblt (0, 0, (unsigned INT) m_pimage.columns (), (unsigned INT) m_pimage.rows (), moffscreendc, 0, 0, srccopy );
}

6. The setting of project properties is the same as that of a test project. Compile and run, as shown in:

References:
1. ImageMagick advanced Windows Installation

Http://www.imagemagick.org/script/advanced-windows-installation.php

2. compilation and use of magick ++ in vs2012

Http://www.stardrad.com/blog/magick%E5%9C%A8vs2012%E7%9A%84%E7%BC%96%E8%AF%91%E4%B8%8E%E4%BD%BF%E7%94%A8/

3. Create an ImageMagick development environment under Visual Studio

Http://hi.baidu.com/soulmachine/item/2bed97dd3dda813f48e1dde0

4. ImageMagick curse

Http://www.ibm.com/developerworks/cn/opensource/os-imagemagick/

5. ImageMagick function using http://www.netingcn.com/category/imagemagick

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.