9#define MAGICKCORE_IMPLEMENTATION 1
10#define MAGICK_PLUSPLUS_IMPLEMENTATION 1
12#include "Magick++/Include.h"
13#include "Magick++/Exception.h"
14#include "Magick++/Statistic.h"
15#include "Magick++/Image.h"
19Magick::ChannelMoments::ChannelMoments(
void)
20 : _channel(SyncPixelChannel),
27 _ellipseEccentricity(0.0),
28 _ellipseIntensity(0.0)
32Magick::ChannelMoments::ChannelMoments(
const ChannelMoments &channelMoments_)
33 : _channel(channelMoments_._channel),
34 _huInvariants(channelMoments_._huInvariants),
35 _centroidX(channelMoments_._centroidX),
36 _centroidY(channelMoments_._centroidY),
37 _ellipseAxisX(channelMoments_._ellipseAxisX),
38 _ellipseAxisY(channelMoments_._ellipseAxisY),
39 _ellipseAngle(channelMoments_._ellipseAngle),
40 _ellipseEccentricity(channelMoments_._ellipseEccentricity),
41 _ellipseIntensity(channelMoments_._ellipseIntensity)
45Magick::ChannelMoments::~ChannelMoments(
void)
49double Magick::ChannelMoments::centroidX(
void)
const
54double Magick::ChannelMoments::centroidY(
void)
const
59Magick::PixelChannel Magick::ChannelMoments::channel(
void)
const
64double Magick::ChannelMoments::ellipseAxisX(
void)
const
66 return(_ellipseAxisX);
69double Magick::ChannelMoments::ellipseAxisY(
void)
const
71 return(_ellipseAxisY);
74double Magick::ChannelMoments::ellipseAngle(
void)
const
76 return(_ellipseAngle);
79double Magick::ChannelMoments::ellipseEccentricity(
void)
const
81 return(_ellipseEccentricity);
84double Magick::ChannelMoments::ellipseIntensity(
void)
const
86 return(_ellipseIntensity);
89double Magick::ChannelMoments::huInvariants(
const size_t index_)
const
94 return(_huInvariants.at(index_));
97bool Magick::ChannelMoments::isValid()
const
99 return(_channel != SyncPixelChannel);
102Magick::ChannelMoments::ChannelMoments(
const PixelChannel channel_,
103 const MagickCore::ChannelMoments *channelMoments_)
104 : _channel(channel_),
106 _centroidX(channelMoments_->centroid.x),
107 _centroidY(channelMoments_->centroid.y),
108 _ellipseAxisX(channelMoments_->ellipse_axis.x),
109 _ellipseAxisY(channelMoments_->ellipse_axis.y),
110 _ellipseAngle(channelMoments_->ellipse_angle),
111 _ellipseEccentricity(channelMoments_->ellipse_eccentricity),
112 _ellipseIntensity(channelMoments_->ellipse_intensity)
118 _huInvariants.push_back(channelMoments_->invariant[i]);
121Magick::ChannelPerceptualHash::ChannelPerceptualHash(
void)
122 : _channel(SyncPixelChannel),
128Magick::ChannelPerceptualHash::ChannelPerceptualHash(
129 const ChannelPerceptualHash &channelPerceptualHash_)
130 : _channel(channelPerceptualHash_._channel),
131 _srgbHuPhash(channelPerceptualHash_._srgbHuPhash),
132 _hclpHuPhash(channelPerceptualHash_._hclpHuPhash)
136Magick::ChannelPerceptualHash::ChannelPerceptualHash(
137 const PixelChannel channel_,
const std::string &hash_)
138 : _channel(channel_),
145 if (hash_.length() != 70)
146 throw ErrorOption(
"Invalid hash length");
156#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
157 if (sscanf_s(hash_.substr(i*5,5).c_str(),
"%05x",&hex) != 1)
159 if (sscanf(hash_.substr(i*5,5).c_str(),
"%05x",&hex) != 1)
161 throw ErrorOption(
"Invalid hash value");
163 value=((
unsigned short)hex) / pow(10.0, (
double)(hex >> 17));
167 _srgbHuPhash[i]=value;
169 _hclpHuPhash[i-7]=value;
173Magick::ChannelPerceptualHash::~ChannelPerceptualHash(
void)
177Magick::ChannelPerceptualHash::operator std::string()
const
186 return(std::string());
200 value=_srgbHuPhash[i];
202 value=_hclpHuPhash[i-7];
205 while(hex < 7 && fabs(value*10) < 65536)
214 hex=(hex<<16)+(
unsigned int)(value < 0.0 ? -(value - 0.5) : value + 0.5);
215 (void) FormatLocaleString(buffer,6,
"%05x",hex);
216 hash+=std::string(buffer);
221Magick::PixelChannel Magick::ChannelPerceptualHash::channel()
const
226bool Magick::ChannelPerceptualHash::isValid()
const
228 return(_channel != SyncPixelChannel);
231double Magick::ChannelPerceptualHash::sumSquaredDifferences(
243 ssd+=((_srgbHuPhash[i]-channelPerceptualHash_._srgbHuPhash[i])*
244 (_srgbHuPhash[i]-channelPerceptualHash_._srgbHuPhash[i]));
245 ssd+=((_hclpHuPhash[i]-channelPerceptualHash_._hclpHuPhash[i])*
246 (_hclpHuPhash[i]-channelPerceptualHash_._hclpHuPhash[i]));
251double Magick::ChannelPerceptualHash::srgbHuPhash(
const size_t index_)
const
256 return(_srgbHuPhash.at(index_));
259double Magick::ChannelPerceptualHash::hclpHuPhash(
const size_t index_)
const
264 return(_hclpHuPhash.at(index_));
267Magick::ChannelPerceptualHash::ChannelPerceptualHash(
268 const PixelChannel channel_,
269 const MagickCore::ChannelPerceptualHash *channelPerceptualHash_)
270 : _channel(channel_),
279 _srgbHuPhash[i]=channelPerceptualHash_->phash[0][i];
280 _hclpHuPhash[i]=channelPerceptualHash_->phash[1][i];
284Magick::ChannelStatistics::ChannelStatistics(
void)
285 : _channel(SyncPixelChannel),
294 _standardDeviation(0.0),
297 _sumFourthPower(0.0),
303Magick::ChannelStatistics::ChannelStatistics(
304 const ChannelStatistics &channelStatistics_)
305 : _channel(channelStatistics_._channel),
306 _area(channelStatistics_._area),
307 _depth(channelStatistics_._depth),
308 _entropy(channelStatistics_._entropy),
309 _kurtosis(channelStatistics_._kurtosis),
310 _maxima(channelStatistics_._maxima),
311 _mean(channelStatistics_._mean),
312 _minima(channelStatistics_._minima),
313 _skewness(channelStatistics_._skewness),
314 _standardDeviation(channelStatistics_._standardDeviation),
315 _sum(channelStatistics_._sum),
316 _sumCubed(channelStatistics_._sumCubed),
317 _sumFourthPower(channelStatistics_._sumFourthPower),
318 _sumSquared(channelStatistics_._sumSquared),
319 _variance(channelStatistics_._variance)
323Magick::ChannelStatistics::~ChannelStatistics(
void)
327double Magick::ChannelStatistics::area()
const
332Magick::PixelChannel Magick::ChannelStatistics::channel()
const
337size_t Magick::ChannelStatistics::depth()
const
342double Magick::ChannelStatistics::entropy()
const
347bool Magick::ChannelStatistics::isValid()
const
349 return(_channel != SyncPixelChannel);
352double Magick::ChannelStatistics::kurtosis()
const
357double Magick::ChannelStatistics::maxima()
const
362double Magick::ChannelStatistics::mean()
const
367double Magick::ChannelStatistics::minima()
const
372double Magick::ChannelStatistics::skewness()
const
377double Magick::ChannelStatistics::standardDeviation()
const
379 return(_standardDeviation);
382double Magick::ChannelStatistics::sum()
const
387double Magick::ChannelStatistics::sumCubed()
const
392double Magick::ChannelStatistics::sumFourthPower()
const
394 return(_sumFourthPower);
397double Magick::ChannelStatistics::sumSquared()
const
402double Magick::ChannelStatistics::variance()
const
407Magick::ChannelStatistics::ChannelStatistics(
const PixelChannel channel_,
408 const MagickCore::ChannelStatistics *channelStatistics_)
409 : _channel(channel_),
410 _area(channelStatistics_->area),
411 _depth(channelStatistics_->depth),
412 _entropy(channelStatistics_->entropy),
413 _kurtosis(channelStatistics_->kurtosis),
414 _maxima(channelStatistics_->maxima),
415 _mean(channelStatistics_->mean),
416 _minima(channelStatistics_->minima),
417 _skewness(channelStatistics_->skewness),
418 _standardDeviation(channelStatistics_->standard_deviation),
419 _sum(channelStatistics_->sum),
420 _sumCubed(channelStatistics_->sum_cubed),
421 _sumFourthPower(channelStatistics_->sum_fourth_power),
422 _sumSquared(channelStatistics_->sum_squared),
423 _variance(channelStatistics_->variance)
427Magick::ImageMoments::ImageMoments(
void)
432Magick::ImageMoments::ImageMoments(
const ImageMoments &imageMoments_)
433 : _channels(imageMoments_._channels)
437Magick::ImageMoments::~ImageMoments(
void)
441Magick::ChannelMoments Magick::ImageMoments::channel(
442 const PixelChannel channel_)
const
444 for (std::vector<ChannelMoments>::const_iterator it = _channels.begin();
445 it != _channels.end(); ++it)
447 if (it->channel() == channel_)
453Magick::ImageMoments::ImageMoments(
const Image &image_)
456 MagickCore::ChannelMoments*
460 channel_moments=GetImageMoments(image_.constImage(),exceptionInfo);
461 if (channel_moments != (MagickCore::ChannelMoments *) NULL)
466 for (i=0; i < (ssize_t) GetPixelChannels(image_.constImage()); i++)
468 PixelChannel channel=GetPixelChannelChannel(image_.constImage(),i);
469 PixelTrait traits=GetPixelChannelTraits(image_.constImage(),channel);
470 if (traits == UndefinedPixelTrait)
472 if ((traits & UpdatePixelTrait) == 0)
475 &channel_moments[channel]));
478 &channel_moments[CompositePixelChannel]));
479 channel_moments=(MagickCore::ChannelMoments *) RelinquishMagickMemory(
482 ThrowPPException(image_.quiet());
485Magick::ImagePerceptualHash::ImagePerceptualHash(
void)
490Magick::ImagePerceptualHash::ImagePerceptualHash(
491 const ImagePerceptualHash &imagePerceptualHash_)
492 : _channels(imagePerceptualHash_._channels)
496Magick::ImagePerceptualHash::ImagePerceptualHash(
const std::string &hash_)
499 if (hash_.length() != 210)
500 throw ErrorOption(
"Invalid hash length");
503 hash_.substr(0, 70)));
505 hash_.substr(70, 70)));
507 hash_.substr(140, 70)));
510Magick::ImagePerceptualHash::~ImagePerceptualHash(
void)
514Magick::ImagePerceptualHash::operator std::string()
const
517 return(std::string());
519 return static_cast<std::string
>(_channels[0]) +
520 static_cast<std::string
>(_channels[1]) +
521 static_cast<std::string
>(_channels[2]);
524Magick::ChannelPerceptualHash Magick::ImagePerceptualHash::channel(
525 const PixelChannel channel_)
const
527 for (std::vector<ChannelPerceptualHash>::const_iterator it =
528 _channels.begin(); it != _channels.end(); ++it)
530 if (it->channel() == channel_)
536bool Magick::ImagePerceptualHash::isValid()
const
538 if (_channels.size() != 3)
541 if (_channels[0].channel() != RedPixelChannel)
544 if (_channels[1].channel() != GreenPixelChannel)
547 if (_channels[2].channel() != BluePixelChannel)
553double Magick::ImagePerceptualHash::sumSquaredDifferences(
564 if (!channelPerceptualHash_.isValid())
565 throw ErrorOption(
"channelPerceptualHash_ is not valid");
570 ssd+=_channels[i].sumSquaredDifferences(_channels[i]);
575Magick::ImagePerceptualHash::ImagePerceptualHash(
579 MagickCore::ChannelPerceptualHash*
580 channel_perceptual_hash;
586 channel_perceptual_hash=GetImagePerceptualHash(image_.constImage(),
588 if (channel_perceptual_hash != (MagickCore::ChannelPerceptualHash *) NULL)
590 traits=GetPixelChannelTraits(image_.constImage(),RedPixelChannel);
591 if ((traits & UpdatePixelTrait) != 0)
593 &channel_perceptual_hash[RedPixelChannel]));
594 traits=GetPixelChannelTraits(image_.constImage(),GreenPixelChannel);
595 if ((traits & UpdatePixelTrait) != 0)
597 &channel_perceptual_hash[GreenPixelChannel]));
598 traits=GetPixelChannelTraits(image_.constImage(),BluePixelChannel);
599 if ((traits & UpdatePixelTrait) != 0)
601 &channel_perceptual_hash[BluePixelChannel]));
602 channel_perceptual_hash=(MagickCore::ChannelPerceptualHash *)
603 RelinquishMagickMemory(channel_perceptual_hash);
605 ThrowPPException(image_.quiet());
608Magick::ImageStatistics::ImageStatistics(
void)
613Magick::ImageStatistics::ImageStatistics(
614 const ImageStatistics &imageStatistics_)
615 : _channels(imageStatistics_._channels)
619Magick::ImageStatistics::~ImageStatistics(
void)
623Magick::ChannelStatistics Magick::ImageStatistics::channel(
624 const PixelChannel channel_)
const
626 for (std::vector<ChannelStatistics>::const_iterator it = _channels.begin();
627 it != _channels.end(); ++it)
629 if (it->channel() == channel_)
635Magick::ImageStatistics::ImageStatistics(
const Image &image_)
638 MagickCore::ChannelStatistics*
642 channel_statistics=GetImageStatistics(image_.constImage(),exceptionInfo);
643 if (channel_statistics != (MagickCore::ChannelStatistics *) NULL)
648 for (i=0; i < (ssize_t) GetPixelChannels(image_.constImage()); i++)
650 PixelChannel channel=GetPixelChannelChannel(image_.constImage(),i);
651 PixelTrait traits=GetPixelChannelTraits(image_.constImage(),channel);
652 if (traits == UndefinedPixelTrait)
654 if ((traits & UpdatePixelTrait) == 0)
657 &channel_statistics[channel]));
660 &channel_statistics[CompositePixelChannel]));
661 channel_statistics=(MagickCore::ChannelStatistics *) RelinquishMagickMemory(
664 ThrowPPException(image_.quiet());