📄 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<unsigned char> &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.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);
} int colors = (int)MagickGetImageColors(magick_wand); delete [] pixels; magick_wand = DestroyMagickWand(magick_wand);
return colors;
}
int GetImageColors(Matrix<unsigned char> &Image)
{
MatrixList<unsigned char> 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<unsigned char> AdaptiveThresholdImage(MatrixList<unsigned char> &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<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 = MagickAdaptiveThresholdImage(magick_wand, width, height, offset); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
status = MagickGetImagePixels( magick_wand, 0, 0, Image.Columns(), Image.Rows(), map, CharPixel, pixels ); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
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;
}
Matrix<unsigned char> AdaptiveThresholdImage(Matrix<unsigned char> &Image, int width, int height, int offset)
{
MatrixList<unsigned char> temp(Image);
MatrixList<unsigned char> temp2 = AdaptiveThresholdImage(temp,width,height,offset);
return temp2[0];
}
MatrixList<unsigned char> AddNoiseImage(MatrixList<unsigned char> &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<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 = MagickAddNoiseImage(magick_wand, noise); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
status = MagickGetImagePixels( magick_wand, 0, 0, Image.Columns(), Image.Rows(), map, CharPixel, pixels ); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
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;
}
Matrix<unsigned char> AddNoiseImage(Matrix<unsigned char> &Image, NoiseType noise)
{
MatrixList<unsigned char> temp(Image);
MatrixList<unsigned char> temp2 = AddNoiseImage(temp,noise);
return temp2[0];
}
MatrixList<unsigned char> BlackThresholdImage(MatrixList<unsigned char> &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<Image.Rows(); i++) { for(int j=0; j<Image.Columns(); j++) { for(int k=0; k<noPlanes; k++) { pixels[z] = Image[k].ElemNC(i,j); z++; } } }
status = MagickConstituteImage(magick_wand, Image.Columns(), Image.Rows(), map.c_str(), CharPixel, pixels);
if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
PixelWand *pw = NewPixelWand();
if(map == "RGB")
{
PixelSetRed(pw, pixel.Red/255.0);
PixelSetGreen(pw, pixel.Green/255.0);
PixelSetBlue(pw, pixel.Blue/255.0);
}
else if(map == "I")
{
PixelSetRed(pw, pixel.Intensity/255.0);
PixelSetGreen(pw, pixel.Intensity/255.0);
PixelSetBlue(pw, pixel.Intensity/255.0);
}
status = MagickBlackThresholdImage(magick_wand, pw); pw = DestroyPixelWand(pw); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
status = MagickGetImagePixels( magick_wand, 0, 0, Image.Columns(), Image.Rows(), map.c_str(), CharPixel, pixels ); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
MatrixList<unsigned char> temp(noPlanes, Image.Rows(), Image.Columns());
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;
}
Matrix<unsigned char> BlackThresholdImage(Matrix<unsigned char> &Image, CimplColor pixel)
{
MatrixList<unsigned char> temp(Image);
MatrixList<unsigned char> temp2 = BlackThresholdImage(temp,pixel);
return temp2[0];
}
MatrixList<unsigned char> BlurImage(MatrixList<unsigned char> &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<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 = MagickBlurImage(magick_wand, supportRadius, sigma); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
status = MagickGetImagePixels( magick_wand, 0, 0, Image.Columns(), Image.Rows(), map, CharPixel, pixels ); if (status == MagickFalse)
{
ThrowWandException(magick_wand);
}
MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -