www.pudn.com > Image_segment.rar > ImageMagick.cpp


//Copyright (c) 2004-2005, Baris Sumengen 
//All rights reserved. 
// 
// CIMPL Matrix Performance Library 
// 
//Redistribution and use in source and binary 
//forms, with or without modification, are 
//permitted provided that the following 
//conditions are met: 
// 
//    * No commercial use is allowed.  
//    This software can only be used 
//    for non-commercial purposes. This  
//    distribution is mainly intended for 
//    academic research and teaching. 
//    * Redistributions of source code must 
//    retain the above copyright notice, this 
//    list of conditions and the following 
//    disclaimer. 
//    * Redistributions of binary form must 
//    mention the above copyright notice, this 
//    list of conditions and the following 
//    disclaimer in a clearly visible part  
//    in associated product manual,  
//    readme, and web site of the redistributed  
//    software. 
//    * Redistributions in binary form must 
//    reproduce the above copyright notice, 
//    this list of conditions and the 
//    following disclaimer in the 
//    documentation and/or other materials 
//    provided with the distribution. 
//    * The name of Baris Sumengen may not be 
//    used to endorse or promote products 
//    derived from this software without 
//    specific prior written permission. 
// 
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
//HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
//NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
//PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
//CONTRIBUTORS BE LIABLE FOR ANY 
//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
//DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF 
//LIABILITY, WHETHER IN CONTRACT, STRICT 
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
//OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
//POSSIBILITY OF SUCH DAMAGE. 
 
 
 
#include "./ImageMagick.h" 
 
 
namespace ImageMagick 
{ 
 
 
	int GetImageColors(MatrixList &Image) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i &Image) 
	{ 
		MatrixList temp(Image); 
		return GetImageColors(temp); 
	} 
 
 
 
 
	CimplImageInfo PingImage(string filename) 
	{ 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
		status = MagickPingImage(magick_wand, filename.c_str()); 
		if (status == MagickFalse) 
		{ 
			ThrowWandException(magick_wand); 
		}
		
		CimplImageInfo temp;
		temp.Width = (int)MagickGetImageWidth(magick_wand);
		temp.Height = (int)MagickGetImageHeight(magick_wand);
		temp.Size = (long long)MagickGetImageSize(magick_wand);
		temp.Format = (string)MagickGetImageFormat(magick_wand);
		
		return temp;
	} 
	 
	MatrixList AdaptiveThresholdImage(MatrixList &Image, int width, int height, int offset) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		z=0;
		for(int i=0; i AdaptiveThresholdImage(Matrix &Image, int width, int height, int offset) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = AdaptiveThresholdImage(temp,width,height,offset); 
		return temp2[0]; 
	} 
	 
	MatrixList AddNoiseImage(MatrixList &Image, NoiseType noise) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		z=0;
		for(int i=0; i AddNoiseImage(Matrix &Image, NoiseType noise) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = AddNoiseImage(temp,noise); 
		return temp2[0]; 
	} 
	 
	MatrixList BlackThresholdImage(MatrixList &Image, CimplColor pixel) 
	{ 
		string map; 
		bool alphaDiscarded = false; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			Utility::Warning("Alpha channel is discarded for this operation!"); 
			map = "RGB"; 
			alphaDiscarded = true; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int noPlanes; 
		if(alphaDiscarded) 
		{ 
			noPlanes = Image.Planes()-1; 
		} 
		else 
		{ 
			noPlanes = Image.Planes(); 
		} 
		int arrayLength = Image.Rows()*Image.Columns()*noPlanes; 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(noPlanes, Image.Rows(), Image.Columns()); 
		z=0;
		for(int i=0; i BlackThresholdImage(Matrix &Image, CimplColor pixel) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = BlackThresholdImage(temp,pixel); 
		return temp2[0]; 
	} 
 
	 
	 
	 
	MatrixList BlurImage(MatrixList &Image, double supportRadius, double sigma) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		z=0;
		for(int i=0; i BlurImage(Matrix &Image, double supportRadius, double sigma) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = BlurImage(temp,supportRadius,sigma); 
		return temp2[0]; 
	} 
 
 
	MatrixList BorderImage(MatrixList &Image, CimplColor borderColor, int borderWidth, int borderHeight) 
	{ 
		char *map; 
		PixelWand *pw = NewPixelWand(); 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
			PixelSetRed(pw, borderColor.Intensity/255.0); 
			PixelSetGreen(pw, borderColor.Intensity/255.0); 
			PixelSetBlue(pw, borderColor.Intensity/255.0); 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
			PixelSetRed(pw, borderColor.Red/255.0); 
			PixelSetGreen(pw, borderColor.Green/255.0); 
			PixelSetBlue(pw, borderColor.Blue/255.0); 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
			PixelSetRed(pw, borderColor.Red/255.0); 
			PixelSetGreen(pw, borderColor.Green/255.0); 
			PixelSetBlue(pw, borderColor.Blue/255.0); 
			PixelSetAlpha(pw, borderColor.Alpha/255.0); 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), hei, wid); 
		z=0;
		for(int i=0; i BorderImage(Matrix &Image, CimplColor borderColor, int borderWidth, int borderHeight) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = BorderImage(temp,borderColor,borderWidth,borderHeight); 
		return temp2[0]; 
	} 
	 
	 
	MatrixList CharcoalImage(MatrixList &Image, double supportRadius, double sigma) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		z=0;
		for(int i=0; i CharcoalImage(Matrix &Image, double supportRadius, double sigma) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = CharcoalImage(temp,supportRadius,sigma); 
		return temp2[0]; 
	} 
 
	 
	MatrixList ChopImage(MatrixList &Image, int offsetX, int offsetY, int chopWidth, int chopHeight) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), hei, wid); 
		z=0;
		for(int i=0; i ChopImage(Matrix &Image, int offsetX, int offsetY, int chopWidth, int chopHeight) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = ChopImage(temp,offsetX,offsetY,chopWidth,chopHeight); 
		return temp2[0]; 
	} 
 
 
	MatrixList CropImage(MatrixList &Image, int offsetX, int offsetY, int cropWidth, int cropHeight) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), hei, wid); 
		z=0;
		for(int i=0; i CropImage(Matrix &Image, int offsetX, int offsetY, int cropWidth, int cropHeight) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = CropImage(temp,offsetX,offsetY,cropWidth,cropHeight); 
		return temp2[0]; 
	} 
 
 
 
 
	MatrixList MagnifyImage(MatrixList &Image) 
	{ 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		else 
		{ 
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; 
			Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!"); 
		} 
 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		int arrayLength = Image.Rows()*Image.Columns()*Image.Planes(); 
		unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
		Utility::CheckPointer(pixels);
		int z=0;
		for(int i=0; i temp(Image.Planes(), hei, wid); 
		z=0;
		for(int i=0; i MagnifyImage(Matrix &Image) 
	{ 
		MatrixList temp(Image); 
		MatrixList temp2 = MagnifyImage(temp); 
		return temp2[0]; 
	} 
 
 
	 
};