www.pudn.com > Image_segment.rar > IO.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 "./IO.h" 
//#include "./ImageMagick.h" 
#include "wand/magick_wand.h"
 
#ifndef ThrowWandException 
 
#define ThrowWandException(wand) \ 
{ \ 
char \ 
*description; \ 
\ 
ExceptionType \ 
severity; \ 
\ 
description=MagickGetException(wand,&severity); \ 
(void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \ 
description=(char *) MagickRelinquishMemory(description); \ 
exit(-1); \ 
} 
 
#endif 
 
 
namespace IO 
{ 
 
	void WriteMFile(Matrix& m, string variable, string filename) 
	{ 
		Matrix re = Real(m); 
		Matrix im = Imag(m); 
		 
		ofstream mfile(filename.c_str(), ios::out); 
		if(!RandomGen::Initialized()) 
		{ 
			RandomGen::Initialize(); 
		} 
		//int variable = (int)(rand()/(double)RAND_MAX*1000); 
		 
		mfile <<  variable << " = [ " << endl; 
		 
		for(int i=0; i& m, string variable, string filename) 
	{ 
		WriteMFile((Matrix)m, variable, filename); 
	} 
 
	void WriteMFile(Matrix& m, string variable, string filename) 
	{ 
		Matrix re = Real(m); 
		Matrix im = Imag(m); 
		 
		ofstream mfile(filename.c_str(), ios::out); 
		if(!RandomGen::Initialized()) 
		{ 
			RandomGen::Initialize(); 
		} 
		//int variable = (int)(rand()/(double)RAND_MAX*1000); 
		 
		mfile << variable << " = [ " << endl; 
		 
		for(int i=0; i& m, string variable, string filename) 
	{ 
		WriteMFile((Matrix)m, variable, filename); 
	} 
 
 
 
 
 
	MatrixList ImRead(char *filename) 
	{ 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		status = MagickReadImage(magick_wand, filename); 
		if (status == MagickFalse) 
		{ 
			ThrowWandException(magick_wand); 
		}
		
		ImageType imType = MagickGetImageType(magick_wand);
		char *map;
		int isRGB;
		//|| imType == BilevelType
		if(imType == GrayscaleType || imType == BilevelType)
		{
			map = "I";
			isRGB = 1;
		}
		else if(imType == TrueColorMatteType || imType == PaletteMatteType)
		{
			map = "RGBA";
			isRGB = 3;
		}
		else
		{
			map = "RGB";
			isRGB = 2;
		}
		
		int hei = (int)MagickGetImageHeight(magick_wand);
		int wid = (int)MagickGetImageWidth(magick_wand);
		
		int l;
		if(isRGB == 2)
		{
			l = 3*wid*hei;
		}
		else if(isRGB == 1)
		{
			l = wid*hei;
		}
		else if(isRGB == 3)
		{
			l = 4*wid*hei;
		}
		unsigned char *pixels = new (std::nothrow) unsigned char[l];
		Utility::CheckPointer(pixels);
		status = MagickGetImagePixels( magick_wand, 0, 0, wid, hei, map, CharPixel, pixels );
		if (status == MagickFalse) 
		{ 
			ThrowWandException(magick_wand); 
		}
 
		MatrixList temp; 
		if(isRGB == 2)
		{
			temp = MatrixList(3,hei,wid); 
		} 
		else if(isRGB == 1)
		{
			temp = MatrixList(1,hei,wid); 
		} 
		else if(isRGB == 3)
		{
			temp = MatrixList(4,hei,wid); 
		} 
 
		int z=0;
		for(int i=0; i& Image, char *filename) 
	{ 
		char *map = "I"; 
		MagickBooleanType status; 
		MagickWand *magick_wand; 
		magick_wand = NewMagickWand();   
 
		unsigned char *pixels = new (std::nothrow) unsigned char[Image.Length()];
		Utility::CheckPointer(pixels);
		int z = 0;
		for(int i=0; i& Image, char *filename) 
	{ 
		//bool imageList = false; 
		char *map; 
		if(Image.Planes() == 1) 
		{ 
			map = "I"; 
		} 
		else if(Image.Planes() == 3) 
		{ 
			map = "RGB"; 
		} 
		else if(Image.Planes() == 4) 
		{ 
			map = "RGBA"; 
		} 
		//else if(Image.Planes() > 0) 
		//{ 
		//	map = "I"; 
		//	imageList = 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();   
 
		//if(imageList) 
		//{ 
		//	for(int p=0;p& Image, char *filename) 
	{ 
		Matrix temp(Image.Rows(), Image.Columns()); 
		int max = Maximum(Image(":")); 
		int min = Minimum(Image(":")); 
 
		if((max-min) == 0) 
		{ 
			Utility::Warning("Image is constant. Setting its values to zero (black)!"); 
			temp.Init(0); 
		} 
		else 
		{ 
			for(int i=0; i& Image, char *filename) 
	{ 
		MatrixList temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		int max = Image[0].ElemNC(0); 
		int min = Image[0].ElemNC(0); 
		for(int z=0; z= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i); 
				min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i); 
			} 
		} 
		if((max-min) == 0) 
		{ 
			Utility::Warning("Image is constant. Setting its values to zero (black)!"); 
			for(int z=0; z& Image, char *filename) 
	{ 
		Matrix temp(Image.Rows(), Image.Columns()); 
		float max = Maximum(Image(":")); 
		float min = Minimum(Image(":")); 
 
		if((max-min) < 0.000000001) 
		{ 
			Utility::Warning("Image is (almost) constant. Setting its values to zero (black)!"); 
			temp.Init(0); 
		} 
		else 
		{ 
			for(int i=0; i& Image, char *filename) 
	{ 
		MatrixList temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		float max = Image[0].ElemNC(0); 
		float min = Image[0].ElemNC(0); 
		for(int z=0; z= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i); 
				min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i); 
			} 
		} 
		if((max-min) < 0.000000001) 
		{ 
			Utility::Warning("Image is constant. Setting its values to zero (black)!"); 
			for(int z=0; z& Image, char *filename) 
	{ 
		Matrix temp(Image.Rows(), Image.Columns()); 
		double max = Maximum(Image(":")); 
		double min = Minimum(Image(":")); 
 
		if((max-min) < 0.00000000000001) 
		{ 
			Utility::Warning("Image is (almost) constant. Setting its values to zero (black)!"); 
			temp.Init(0); 
		} 
		else 
		{ 
			for(int i=0; i& Image, char *filename) 
	{ 
		MatrixList temp(Image.Planes(), Image.Rows(), Image.Columns()); 
		double max = Image[0].ElemNC(0); 
		double min = Image[0].ElemNC(0); 
		for(int z=0; z= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i); 
				min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i); 
			} 
		} 
		if((max-min) < 0.00000000000001) 
		{ 
			Utility::Warning("Image is constant. Setting its values to zero (black)!"); 
			for(int z=0; z