Magick++ 6.9.13
Loading...
Searching...
No Matches
ChannelMoments.cpp
1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Dirk Lemstra 2014-2015
4//
5// Implementation of channel moments.
6//
7
8#define MAGICKCORE_IMPLEMENTATION 1
9#define MAGICK_PLUSPLUS_IMPLEMENTATION 1
10
11#include "Magick++/Include.h"
12#include "Magick++/ChannelMoments.h"
13#include "Magick++/Exception.h"
14#include "Magick++/Image.h"
15
16using namespace std;
17
18Magick::ChannelMoments::ChannelMoments(void)
19 : _huInvariants(8),
20 _channel(UndefinedChannel),
21 _centroidX(0.0),
22 _centroidY(0.0),
23 _ellipseAxisX(0.0),
24 _ellipseAxisY(0.0),
25 _ellipseAngle(0.0),
26 _ellipseEccentricity(0.0),
27 _ellipseIntensity(0.0)
28{
29}
30
31Magick::ChannelMoments::ChannelMoments(const ChannelMoments &channelMoments_)
32 : _huInvariants(channelMoments_._huInvariants),
33 _channel(channelMoments_._channel),
34 _centroidX(channelMoments_._centroidX),
35 _centroidY(channelMoments_._centroidY),
36 _ellipseAxisX(channelMoments_._ellipseAxisX),
37 _ellipseAxisY(channelMoments_._ellipseAxisY),
38 _ellipseAngle(channelMoments_._ellipseAngle),
39 _ellipseEccentricity(channelMoments_._ellipseEccentricity),
40 _ellipseIntensity(channelMoments_._ellipseIntensity)
41{
42}
43
44Magick::ChannelMoments::~ChannelMoments(void)
45{
46}
47
48Magick::ChannelMoments::ChannelMoments(const ChannelType channel_,
49 const MagickCore::ChannelMoments *channelMoments_)
50 : _huInvariants(),
51 _channel(channel_),
52 _centroidX(channelMoments_->centroid.x),
53 _centroidY(channelMoments_->centroid.y),
54 _ellipseAxisX(channelMoments_->ellipse_axis.x),
55 _ellipseAxisY(channelMoments_->ellipse_axis.y),
56 _ellipseAngle(channelMoments_->ellipse_angle),
57 _ellipseEccentricity(channelMoments_->ellipse_eccentricity),
58 _ellipseIntensity(channelMoments_->ellipse_intensity)
59{
60 size_t
61 i;
62
63 for (i=0; i<8; i++)
64 _huInvariants.push_back(channelMoments_->I[i]);
65}
66
67double Magick::ChannelMoments::centroidX(void) const
68{
69 return(_centroidX);
70}
71
72double Magick::ChannelMoments::centroidY(void) const
73{
74 return(_centroidY);
75}
76
77Magick::ChannelType Magick::ChannelMoments::channel(void) const
78{
79 return(_channel);
80}
81
82double Magick::ChannelMoments::ellipseAxisX(void) const
83{
84 return(_ellipseAxisX);
85}
86
87double Magick::ChannelMoments::ellipseAxisY(void) const
88{
89 return(_ellipseAxisY);
90}
91
92double Magick::ChannelMoments::ellipseAngle(void) const
93{
94 return(_ellipseAngle);
95}
96
97double Magick::ChannelMoments::ellipseEccentricity(void) const
98{
99 return(_ellipseEccentricity);
100}
101
102double Magick::ChannelMoments::ellipseIntensity(void) const
103{
104 return(_ellipseIntensity);
105}
106
107double Magick::ChannelMoments::huInvariants(const size_t index_) const
108{
109 if (index_ > 7)
110 throw ErrorOption("Valid range for index is 0-7");
111
112 return(_huInvariants.at(index_));
113}
114
115Magick::ImageMoments::ImageMoments(void)
116 : _channels()
117{
118}
119
120Magick::ImageMoments::ImageMoments(const ImageMoments &imageMoments_)
121 : _channels(imageMoments_._channels)
122{
123}
124
125Magick::ImageMoments::~ImageMoments(void)
126{
127}
128
129Magick::ChannelMoments Magick::ImageMoments::channel(
130 const ChannelType channel_) const
131{
132 for (std::vector<ChannelMoments>::const_iterator it = _channels.begin();
133 it != _channels.end(); ++it)
134 {
135 if (it->channel() == channel_)
136 return(*it);
137 }
138 return(ChannelMoments());
139}
140
141Magick::ImageMoments::ImageMoments(const Image &image_)
142 : _channels()
143{
144 MagickCore::ChannelMoments*
145 channel_moments;
146
147 GetPPException;
148 channel_moments=GetImageChannelMoments(image_.constImage(),exceptionInfo);
149 if (channel_moments != (MagickCore::ChannelMoments *) NULL)
150 {
151 switch(image_.constImage()->colorspace)
152 {
153 case RGBColorspace:
154 default:
155 _channels.push_back(Magick::ChannelMoments(RedChannel,
156 &channel_moments[RedChannel]));
157 _channels.push_back(Magick::ChannelMoments(GreenChannel,
158 &channel_moments[GreenChannel]));
159 _channels.push_back(Magick::ChannelMoments(BlueChannel,
160 &channel_moments[BlueChannel]));
161 break;
162 case CMYKColorspace:
163 _channels.push_back(Magick::ChannelMoments(CyanChannel,
164 &channel_moments[CyanChannel]));
165 _channels.push_back(Magick::ChannelMoments(MagentaChannel,
166 &channel_moments[MagentaChannel]));
167 _channels.push_back(Magick::ChannelMoments(YellowChannel,
168 &channel_moments[YellowChannel]));
169 _channels.push_back(Magick::ChannelMoments(BlackChannel,
170 &channel_moments[BlackChannel]));
171 break;
172 case GRAYColorspace:
173 _channels.push_back(Magick::ChannelMoments(GrayChannel,
174 &channel_moments[GrayChannel]));
175 break;
176 }
177 if (image_.constImage()->matte != MagickFalse)
178 _channels.push_back(Magick::ChannelMoments(AlphaChannel,
179 &channel_moments[AlphaChannel]));
180 if (image_.constImage()->colorspace != GRAYColorspace)
181 _channels.push_back(Magick::ChannelMoments(CompositeChannels,
182 &channel_moments[CompositeChannels]));
183 channel_moments=(MagickCore::ChannelMoments *) RelinquishMagickMemory(
184 channel_moments);
185 }
186 ThrowPPException(image_.quiet());
187}