?? 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<ComplexFloat>& m, string variable, string filename)
{
Matrix<float> re = Real(m);
Matrix<float> 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.Rows(); i++)
{
int c = 0;
for(int j=0; j<m.Columns(); j++)
{
mfile << re.ElemNC(i,j) << " + " << im.ElemNC(i,j) << "i" << " ";
c++;
if(c==500)
{
mfile << " ..." << endl;
}
}
mfile << endl;
}
mfile << "];" << endl;
}
void WriteMFile(Vector<ComplexFloat>& m, string variable, string filename)
{
WriteMFile((Matrix<ComplexFloat>)m, variable, filename);
}
void WriteMFile(Matrix<ComplexDouble>& m, string variable, string filename)
{
Matrix<double> re = Real(m);
Matrix<double> 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.Rows(); i++)
{
int c = 0;
for(int j=0; j<m.Columns(); j++)
{
mfile << re.ElemNC(i,j) << " + " << im.ElemNC(i,j) << "i" << " ";
c++;
if(c==500)
{
mfile << " ..." << endl;
}
}
mfile << endl;
}
mfile << "];" << endl;
}
void WriteMFile(Vector<ComplexDouble>& m, string variable, string filename)
{
WriteMFile((Matrix<ComplexDouble>)m, variable, filename);
}
MatrixList<unsigned char> 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<unsigned char> temp;
if(isRGB == 2) { temp = MatrixList<unsigned char>(3,hei,wid);
}
else if(isRGB == 1) { temp = MatrixList<unsigned char>(1,hei,wid);
}
else if(isRGB == 3) { temp = MatrixList<unsigned char>(4,hei,wid);
}
int z=0; for(int i=0; i<temp.Rows(); i++) { for(int j=0; j<temp.Columns(); j++) { for(int k=0; k<temp.Planes(); k++) { temp[k].ElemNC(i,j) = pixels[z]; z++; } } }
delete [] pixels;
magick_wand = DestroyMagickWand(magick_wand);
return temp;
}
void ImWrite(Matrix<unsigned char>& 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.Rows(); i++) { for(int j=0; j<Image.Columns(); j++) { pixels[z] = Image.ElemNC(i,j); z++; } }
status = MagickConstituteImage(magick_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
if (status == MagickFalse)
{
ThrowWandException(magick_wand);
} status = MagickWriteImage(magick_wand, filename); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
} delete [] pixels; magick_wand = DestroyMagickWand(magick_wand);
}
void ImWrite(MatrixList<unsigned char>& 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.Planes();p++)
// {
// MagickWand *temp_wand;
// temp_wand = NewMagickWand();
// int arrayLength = Image.Rows()*Image.Columns();
// unsigned char *pixels = new unsigned char[arrayLength]; // int z=0; // for(int i=0; i<Image.Rows(); i++) // { // for(int j=0; j<Image.Columns(); j++) // { // pixels[z] = Image[p].ElemNC(i,j); // z++; // } // }
// status = MagickConstituteImage(temp_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
// if (status == MagickFalse)
// {
// ThrowWandException(temp_wand);
// } // status = MagickAddImage(magick_wand, temp_wand); // if (status == MagickFalse)
// {
// ThrowWandException(magick_wand);
// } // temp_wand = DestroyMagickWand(temp_wand);
// }
// status = MagickWriteImages(magick_wand, filename, MagickTrue); // if (status == MagickFalse)
// {
// ThrowWandException(magick_wand);
// } //}
//else
//{
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.Rows(); i++) { for(int j=0; j<Image.Columns(); j++) { for(int k=0; k<Image.Planes(); k++) { pixels[z] = Image[k].ElemNC(i,j); z++; } } }
status = MagickConstituteImage(magick_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
if (status == MagickFalse)
{
ThrowWandException(magick_wand);
} status = MagickWriteImage(magick_wand, filename); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
} //} delete [] pixels; magick_wand = DestroyMagickWand(magick_wand);
}
void ImWrite(Matrix<int>& Image, char *filename)
{
Matrix<unsigned char> 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.Length(); i++)
{
temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
ImWrite(temp, filename);
}
void ImWrite(MatrixList<int>& Image, char *filename)
{
MatrixList<unsigned char> 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.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
max = max >= 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.Planes(); z++)
{
temp[z].Init(0);
}
}
else
{
for(int z=0; z<Image.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
}
ImWrite(temp, filename);
}
void ImWrite(Matrix<float>& Image, char *filename)
{
Matrix<unsigned char> 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.Length(); i++)
{
temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
ImWrite(temp, filename);
}
void ImWrite(MatrixList<float>& Image, char *filename)
{
MatrixList<unsigned char> 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.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
max = max >= 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.Planes(); z++)
{
temp[z].Init(0);
}
}
else
{
for(int z=0; z<Image.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
}
ImWrite(temp, filename);
}
void ImWrite(Matrix<double>& Image, char *filename)
{
Matrix<unsigned char> 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.Length(); i++)
{
temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
ImWrite(temp, filename);
}
void ImWrite(MatrixList<double>& Image, char *filename)
{
MatrixList<unsigned char> 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.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
max = max >= 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<Image.Planes(); z++)
{
temp[z].Init(0);
}
}
else
{
for(int z=0; z<Image.Planes(); z++)
{
for(int i=0; i<Image[z].Length(); i++)
{
temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
}
}
}
ImWrite(temp, filename);
}
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -