Magick++ 6.8.5在MFC中的使用

來源:互聯網
上載者:User

      Magick++是ImageMagick映像庫的C++封裝。ImageMagick是一個集建立、編輯、合成和轉換映像格式的軟體套件。
編譯步驟:
1.從http://www.imagemagick.org/download/windows/ImageMagick-windows.zip下載原始碼包;
2.解壓到檔案夾,開啟"..\VisualMagick\configure"目錄下的configure.exe檔案,按"下一步"進入配置介面,可以進行設定編譯類型和編譯選項,在這裡我使用預設的編譯類型,即Dynamic Multi-threaded DLL runtimes類型,編譯選項選擇預設選項,如所示:

3.繼續點擊"下一步",直到"完成",就可以看到在"..\VisualMagick"目錄自動產生了"VisualDynamicMT.sln"檔案(此名稱會根據不同的編譯類型而不同),用VS2008進行開啟,右鍵解決方案裡的"All"工程,選擇"Clean",然後再選擇"Build"。編譯完成之後,就可以在"..\VisualMagick"下的"bin"檔案夾和"lib"檔案夾看到所產生的檔案。
4.開啟"\VisualMagick\bin"目錄下的"IMDisplay.exe",載入任意一張圖片(不支援中文路徑),看是否可以正常顯示,如所示:

下面介紹在VS2008中使用Magick++
1.建立win32控制台項目,選擇"Empty project",去除"Precompiled header",工程名為TestMagick;
2.添加建立一個cpp檔案,名稱為test.cpp,代碼如下:

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;
}

代碼的功能是建立一張100x100像素的白色背景圖片,中心像素為紅色。
3.右鍵工程→"Properties"→"C/C++"下,"General"→"Additional Include Directories"填入"X:\ImageMagick-6.8.5";"X:\ImageMagick-6.8.5\Magick++\lib","Preprocessor"→"Preprocessor Definitions"增加填入";_VISUALC_;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS"。"Linker"下,"General"→"Additional
Library Directories"填入"X:\ImageMagick-6.8.5\VisualMagick\lib","Input"→"Additional Dependencies"填入"CORE_DB_Magick++_.lib"。"Debugging"→"Working Directory"填入"X:\ImageMagick-6.8.5\VisualMagick\bin"。
4.編譯運行,即可在"..\VisualMagick\bin"目錄下看到產生的red_pixel.png檔案。

下面介紹在MFC中使用Magick++
1.建立MFC單文檔應用程式,工程名為TestMagickMFC;
2.在stdafx.h檔案中,加入以下代碼:

1
#include <Magick++.h>

3.在TestMagickMFC.cpp函數InitInstance裡,加入以下代碼:

1
Magick::InitializeMagick(NULL);

4.在TestMagickMFCView.h檔案加入以下代碼:

1
2
3
Magick::Image m_pImage;
CDC* mOffscreenDC;
void DoDisplayImage(CDC* pDC);

5.在TestMagickMFCView.cpp檔案修改加入以下代碼:

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;
        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 of
        // a pattern background so non-opaque regions become evident.
        //

        Magick::Image 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 ; 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.工程屬性的設定跟上一個測試工程一樣。編譯運行,如所示:

參考資料:
1.ImageMagick Advanced Windows Installation

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

2.Magick++在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.在Visual Studio下建立ImageMagick開發環境

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

4.ImageMagick 魔咒

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

5.ImageMagicK功能使用 http://www.netingcn.com/category/imagemagick

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.