📄 image.cpp
字号:
// Basic ATL/COM wrapper for the Image object using the ImageMagick++ classes.// There isn't much in the way of comments in here, since the code is pretty// cookie-cutter. Lots of "TODO" comments that should be removed or replaced// with something a little more descriptive.//// The Composite() method shows one way to handle multiple image objects.// As the comments point out, this is a COM hack. It's no reallu a valid way// to use COM, even though it works. I haven't been able to come up with// a "legal" way to do this. //// TODO: Methods that require a Color object haven't been implemented yet.// The basic idea was for the user to instanciate a MagickCOM.Color// object, set it's properties, then pass this object to the Image // method(s). Those methods would then reconstruct a C++ version of// the class which finally gets used.//// A lot of the overloaded methods haven't been implemented either. // I picked the most common ones to implement first. The overloaded// versions will require a new method name for them to work in COM. //// Written by: Paul Mrozowski// Senior Applications Developer// // Kirtland Associates, Inc.// 1220 Morse// Suite 200// Royal Oak, MI 48067// Phone: 248.542.2675//// Email: pcm@kirtlandsys.com//// Copyright(C) 2002 - Kirtland Associates// Image.cpp : Implementation of CImage#include "stdafx.h"#include "MagickCOM.h"#include "Image.h"#include "Magick++.h"#include "ShellAPI.h"#include "string.h"#include "ImageControl.h"using namespace std;using namespace Magick;//CComBSTR ErrorMsg;//unsigned int ImageWidth = 70;//unsigned int ImageHeight = 70;//unsigned int Columns;//unsigned int Rows;//Magick::Image oImage;/////////////////////////////////////////////////////////////////////////////// CImageSTDMETHODIMP CImage::InterfaceSupportsErrorInfo(REFIID riid){ static const IID* arr[] = { &IID_IImage, }; for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++) { if (InlineIsEqualGUID(*arr[i],riid)) return S_OK; } return S_FALSE;}STDMETHODIMP CImage::OnStartPage (IUnknown* pUnk) { if(!pUnk) return E_POINTER; CComPtr<IScriptingContext> spContext; HRESULT hr; // Get the IScriptingContext Interface hr = pUnk->QueryInterface(IID_IScriptingContext, (void **)&spContext); if(FAILED(hr)) return hr; // Get Request Object Pointer hr = spContext->get_Request(&m_piRequest); if(FAILED(hr)) { spContext.Release(); return hr; } // Get Response Object Pointer hr = spContext->get_Response(&m_piResponse); if(FAILED(hr)) { m_piRequest.Release(); return hr; } // Get Server Object Pointer hr = spContext->get_Server(&m_piServer); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); return hr; } // Get Session Object Pointer hr = spContext->get_Session(&m_piSession); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); return hr; } // Get Application Object Pointer hr = spContext->get_Application(&m_piApplication); if(FAILED(hr)) { m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); m_piSession.Release(); return hr; } m_bOnStartPageCalled = TRUE; return S_OK;}STDMETHODIMP CImage::OnEndPage () { m_bOnStartPageCalled = FALSE; // Release all interfaces m_piRequest.Release(); m_piResponse.Release(); m_piServer.Release(); m_piSession.Release(); m_piApplication.Release(); return S_OK;}/*STDMETHODIMP CImage::SetErrorMessage(string ErrorG);{ //string ErrorG = error_.what(); // Can't use A2OLE in an Exception....// ErrorMsg = A2OLE(ErrorG.c_str()) ; int nStrSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, NULL, 0); WCHAR *wzStringBuf = new WCHAR[nStrSize + 1]; int nRetVal = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, wzStringBuf, nStrSize); ErrorMsg = wzStringBuf; }*/STDMETHODIMP CImage::Read(BSTR cFilename, VARIANT_BOOL *pVal){ // Image oImage; USES_CONVERSION; try { oImage->read(cFilename ? OLE2A(cFilename) : ""); *pVal = true; ErrorMsg.Empty(); } catch( Exception &error_ ) { *pVal = false; //error_.what() // Can't use A2OLE in an Exception.... string ErrorG = error_.what(); // ErrorMsg = A2OLE(ErrorG.c_str()) ; CImage::SetErrorMessage(ErrorG.c_str()); /*-------------------------- int nStrSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, NULL, 0); WCHAR *wzStringBuf = new WCHAR[nStrSize + 1]; int nRetVal = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, wzStringBuf, nStrSize); ErrorMsg = wzStringBuf; ----------------------------*/ return S_FALSE; } return S_OK;}STDMETHODIMP CImage::Resize(BSTR cFilename, BSTR cOutput, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here //Image oImage; try { // Read a file USES_CONVERSION; oImage->read(cFilename ? OLE2A(cFilename) : ""); oImage->sample(Magick::Geometry(ImageWidth, ImageHeight)); //Write a file oImage->write(cOutput ? OLE2A(cOutput) : ""); ErrorMsg.Empty(); *pVal = true; } catch( Exception &error_ ) {// cout << error_.what() << endl; // iReturnCode = 1; // CImage::SetErrorMessage(error_.what());/* USES_CONVERSION; string strError; string strErrorWhat = error_.what(); strError = "An error occurred during resize: " + strErrorWhat; MessageBox(NULL, strError.c_str(), "MagickCOM Error", MB_OK); string ErrorG = error_.what(); ErrorMsg = A2OLE(ErrorG.c_str()) ;*/ string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); /*------------------------------ int nStrSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, NULL, 0); WCHAR *wzStringBuf = new WCHAR[nStrSize + 1]; int nRetVal = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ErrorG.c_str(), -1, wzStringBuf, nStrSize); ErrorMsg = wzStringBuf; --------------------------------*/ *pVal = false; return S_FALSE; } return S_OK;}STDMETHODIMP CImage::get_ImageWidth(unsigned int *pVal){ // TODO: Add your implementation code here // *pVal = oImage.columns() ; // ImageWidth; *pVal = ImageWidth ; return S_OK;}STDMETHODIMP CImage::put_ImageWidth(unsigned int newVal){ // TODO: Add your implementation code here ImageWidth = newVal; return S_OK;}STDMETHODIMP CImage::get_ImageHeight(unsigned int *pVal){ // TODO: Add your implementation code here //*pVal = oImage.rows(); //ImageHeight; *pVal = ImageHeight; return S_OK;}STDMETHODIMP CImage::put_ImageHeight(unsigned int newVal){ // TODO: Add your implementation code here ImageHeight = newVal; return S_OK;}STDMETHODIMP CImage::get_ErrorMsg(BSTR *pVal){ // TODO: Add your implementation code here *pVal = ErrorMsg; return S_OK;}STDMETHODIMP CImage::put_ErrorMsg(BSTR newVal){ // TODO: Add your implementation code here ErrorMsg = newVal; return S_OK;}STDMETHODIMP CImage::InitMagick(BSTR cPath){ // TODO: Add your implementation code here USES_CONVERSION; InitializeMagick(cPath ? OLE2A(cPath) : ""); return S_OK;}STDMETHODIMP CImage::get_Columns(unsigned int *pVal){ // TODO: Add your implementation code here *pVal = oImage->columns(); return S_OK;}STDMETHODIMP CImage::get_Rows(unsigned int *pVal){ // TODO: Add your implementation code here *pVal = oImage->rows(); return S_OK;}void CImage::SetErrorMessage(const char *strErrorG){// Can't use A2OLE in an Exception....// ErrorMsg = A2OLE(ErrorG.c_str()) ; int nStrSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, strErrorG, -1, NULL, 0); WCHAR *wzStringBuf = new WCHAR[nStrSize + 1]; int nRetVal = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, strErrorG, -1, wzStringBuf, nStrSize); ErrorMsg = wzStringBuf; }STDMETHODIMP CImage::Scale(unsigned int x, unsigned int y, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->scale(Magick::Geometry(x, y)); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Sample(unsigned int x, unsigned int y, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->sample(Magick::Geometry(x, y)); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Write(BSTR cFilename, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { // Read a file USES_CONVERSION; oImage->write(cFilename ? OLE2A(cFilename) : ""); ErrorMsg.Empty(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Rotate(double Degrees, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->rotate(Degrees); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Flip(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->flip(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Enhance(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->enhance(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Equalize(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->equalize(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Flop(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->flop(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Normalize(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->normalize(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Despeckle(VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->despeckle(); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Contrast(unsigned int sharpen, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->contrast(sharpen); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Edge(unsigned int radius, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->edge(radius); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Emboss(double radius, double sigma, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->emboss(radius, sigma); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::Gamma(double Red, double Green, double Blue, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty(); try { oImage->gamma(Red, Green, Blue); *pVal = true; } catch( Exception &error_ ) { string ErrorG = error_.what(); CImage::SetErrorMessage(ErrorG.c_str()); *pVal = false; } return S_OK;}STDMETHODIMP CImage::GaussianBlur(double width, double sigma, VARIANT_BOOL *pVal){ // TODO: Add your implementation code here ErrorMsg.Empty();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -