cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % CCCC OOO M M PPPP AAA RRRR EEEEE % |
| 7 | % C O O MM MM P P A A R R E % |
| 8 | % C O O M M M PPPP AAAAA RRRR EEE % |
| 9 | % C O O M M P A A R R E % |
| 10 | % CCCC OOO M M P A A R R EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Comparison Methods % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % December 2003 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/artifact.h" |
cristy | c97da4b | 2014-01-18 15:58:31 +0000 | [diff] [blame] | 45 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 46 | #include "MagickCore/cache-view.h" |
cristy | 6a2180c | 2013-05-27 10:28:36 +0000 | [diff] [blame] | 47 | #include "MagickCore/channel.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #include "MagickCore/client.h" |
| 49 | #include "MagickCore/color.h" |
| 50 | #include "MagickCore/color-private.h" |
| 51 | #include "MagickCore/colorspace.h" |
| 52 | #include "MagickCore/colorspace-private.h" |
| 53 | #include "MagickCore/compare.h" |
| 54 | #include "MagickCore/composite-private.h" |
| 55 | #include "MagickCore/constitute.h" |
| 56 | #include "MagickCore/exception-private.h" |
| 57 | #include "MagickCore/geometry.h" |
| 58 | #include "MagickCore/image-private.h" |
| 59 | #include "MagickCore/list.h" |
| 60 | #include "MagickCore/log.h" |
| 61 | #include "MagickCore/memory_.h" |
| 62 | #include "MagickCore/monitor.h" |
| 63 | #include "MagickCore/monitor-private.h" |
| 64 | #include "MagickCore/option.h" |
| 65 | #include "MagickCore/pixel-accessor.h" |
cristy | b92495a | 2013-08-20 00:10:59 +0000 | [diff] [blame] | 66 | #include "MagickCore/property.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 67 | #include "MagickCore/resource_.h" |
| 68 | #include "MagickCore/string_.h" |
| 69 | #include "MagickCore/statistic.h" |
cristy | 16881e6 | 2012-05-06 14:41:29 +0000 | [diff] [blame] | 70 | #include "MagickCore/thread-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 71 | #include "MagickCore/transform.h" |
| 72 | #include "MagickCore/utility.h" |
| 73 | #include "MagickCore/version.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 77 | % % |
| 78 | % % |
| 79 | % % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 80 | % C o m p a r e I m a g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | % % |
| 82 | % % |
| 83 | % % |
| 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 85 | % |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 86 | % CompareImages() compares one or more pixel channels of an image to a |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 87 | % reconstructed image and returns the difference image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 88 | % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 89 | % The format of the CompareImages method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 90 | % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 91 | % Image *CompareImages(const Image *image,const Image *reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 92 | % const MetricType metric,double *distortion,ExceptionInfo *exception) |
| 93 | % |
| 94 | % A description of each parameter follows: |
| 95 | % |
| 96 | % o image: the image. |
| 97 | % |
| 98 | % o reconstruct_image: the reconstruct image. |
| 99 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 100 | % o metric: the metric. |
| 101 | % |
| 102 | % o distortion: the computed distortion between the images. |
| 103 | % |
| 104 | % o exception: return any errors or warnings in this structure. |
| 105 | % |
| 106 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 107 | MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image, |
| 108 | const MetricType metric,double *distortion,ExceptionInfo *exception) |
| 109 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 110 | CacheView |
| 111 | *highlight_view, |
| 112 | *image_view, |
| 113 | *reconstruct_view; |
| 114 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | const char |
| 116 | *artifact; |
| 117 | |
| 118 | Image |
| 119 | *difference_image, |
| 120 | *highlight_image; |
| 121 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 122 | MagickBooleanType |
| 123 | status; |
| 124 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 125 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 126 | highlight, |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 127 | lowlight; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 128 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 129 | ssize_t |
| 130 | y; |
| 131 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 132 | assert(image != (Image *) NULL); |
| 133 | assert(image->signature == MagickSignature); |
| 134 | if (image->debug != MagickFalse) |
| 135 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 136 | assert(reconstruct_image != (const Image *) NULL); |
| 137 | assert(reconstruct_image->signature == MagickSignature); |
| 138 | assert(distortion != (double *) NULL); |
| 139 | *distortion=0.0; |
| 140 | if (image->debug != MagickFalse) |
| 141 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 8b520b4 | 2014-01-30 00:58:45 +0000 | [diff] [blame] | 142 | if (metric != PerceptualHashErrorMetric) |
| 143 | if ((reconstruct_image->columns != image->columns) || |
| 144 | (reconstruct_image->rows != image->rows)) |
| 145 | ThrowImageException(ImageError,"ImageSizeDiffers"); |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 146 | status=GetImageDistortion(image,reconstruct_image,metric,distortion, |
| 147 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 148 | if (status == MagickFalse) |
| 149 | return((Image *) NULL); |
| 150 | difference_image=CloneImage(image,0,0,MagickTrue,exception); |
| 151 | if (difference_image == (Image *) NULL) |
| 152 | return((Image *) NULL); |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 153 | (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 154 | highlight_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 155 | exception); |
| 156 | if (highlight_image == (Image *) NULL) |
| 157 | { |
| 158 | difference_image=DestroyImage(difference_image); |
| 159 | return((Image *) NULL); |
| 160 | } |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 161 | status=SetImageStorageClass(highlight_image,DirectClass,exception); |
| 162 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 163 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | difference_image=DestroyImage(difference_image); |
| 165 | highlight_image=DestroyImage(highlight_image); |
| 166 | return((Image *) NULL); |
| 167 | } |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 168 | (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception); |
cristy | ca61154 | 2013-02-19 00:54:03 +0000 | [diff] [blame] | 169 | (void) QueryColorCompliance("#f1001ecc",AllCompliance,&highlight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 170 | artifact=GetImageArtifact(image,"highlight-color"); |
| 171 | if (artifact != (const char *) NULL) |
cristy | f9d6dc0 | 2012-01-19 02:14:44 +0000 | [diff] [blame] | 172 | (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception); |
cristy | ca61154 | 2013-02-19 00:54:03 +0000 | [diff] [blame] | 173 | (void) QueryColorCompliance("#ffffffcc",AllCompliance,&lowlight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 174 | artifact=GetImageArtifact(image,"lowlight-color"); |
| 175 | if (artifact != (const char *) NULL) |
cristy | 0b1a797 | 2011-10-22 22:17:02 +0000 | [diff] [blame] | 176 | (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | /* |
| 178 | Generate difference image. |
| 179 | */ |
| 180 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 181 | image_view=AcquireVirtualCacheView(image,exception); |
| 182 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
| 183 | highlight_view=AcquireAuthenticCacheView(highlight_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 184 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 185 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 186 | magick_threads(image,highlight_image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 187 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 188 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | { |
| 190 | MagickBooleanType |
| 191 | sync; |
| 192 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 193 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 194 | *restrict p, |
| 195 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 197 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 198 | *restrict r; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 200 | register ssize_t |
| 201 | x; |
| 202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | if (status == MagickFalse) |
| 204 | continue; |
| 205 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 206 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 207 | 1,exception); |
| 208 | r=QueueCacheViewAuthenticPixels(highlight_view,0,y,highlight_image->columns, |
| 209 | 1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 210 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL) || |
| 211 | (r == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | { |
| 213 | status=MagickFalse; |
| 214 | continue; |
| 215 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 216 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 218 | double |
| 219 | Da, |
| 220 | Sa; |
| 221 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | MagickStatusType |
| 223 | difference; |
| 224 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 225 | register ssize_t |
| 226 | i; |
| 227 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 228 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 229 | { |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 230 | SetPixelInfoPixel(highlight_image,&lowlight,r); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 231 | p+=GetPixelChannels(image); |
| 232 | q+=GetPixelChannels(reconstruct_image); |
| 233 | r+=GetPixelChannels(highlight_image); |
| 234 | continue; |
| 235 | } |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 236 | difference=MagickFalse; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 237 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 238 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 239 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 240 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 241 | double |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 242 | distance; |
| 243 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 244 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 245 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 246 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 247 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 248 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 249 | (reconstruct_traits == UndefinedPixelTrait) || |
| 250 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 251 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 252 | distance=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q); |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 253 | if (fabs((double) distance) >= MagickEpsilon) |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 254 | difference=MagickTrue; |
| 255 | } |
| 256 | if (difference == MagickFalse) |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 257 | SetPixelInfoPixel(highlight_image,&lowlight,r); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 258 | else |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 259 | SetPixelInfoPixel(highlight_image,&highlight,r); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 260 | p+=GetPixelChannels(image); |
| 261 | q+=GetPixelChannels(reconstruct_image); |
| 262 | r+=GetPixelChannels(highlight_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | } |
| 264 | sync=SyncCacheViewAuthenticPixels(highlight_view,exception); |
| 265 | if (sync == MagickFalse) |
| 266 | status=MagickFalse; |
| 267 | } |
| 268 | highlight_view=DestroyCacheView(highlight_view); |
| 269 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 270 | image_view=DestroyCacheView(image_view); |
cristy | feb3e96 | 2012-03-29 17:25:55 +0000 | [diff] [blame] | 271 | (void) CompositeImage(difference_image,highlight_image,image->compose, |
cristy | 3917240 | 2012-03-30 13:04:39 +0000 | [diff] [blame] | 272 | MagickTrue,0,0,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 273 | highlight_image=DestroyImage(highlight_image); |
| 274 | if (status == MagickFalse) |
| 275 | difference_image=DestroyImage(difference_image); |
| 276 | return(difference_image); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 281 | % % |
| 282 | % % |
| 283 | % % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 284 | % G e t I m a g e D i s t o r t i o n % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 285 | % % |
| 286 | % % |
| 287 | % % |
| 288 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 289 | % |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 290 | % GetImageDistortion() compares one or more pixel channels of an image to a |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 291 | % reconstructed image and returns the specified distortion metric. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | % |
cristy | 44097f5 | 2012-12-16 19:56:20 +0000 | [diff] [blame] | 293 | % The format of the GetImageDistortion method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 294 | % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 295 | % MagickBooleanType GetImageDistortion(const Image *image, |
| 296 | % const Image *reconstruct_image,const MetricType metric, |
| 297 | % double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 298 | % |
| 299 | % A description of each parameter follows: |
| 300 | % |
| 301 | % o image: the image. |
| 302 | % |
| 303 | % o reconstruct_image: the reconstruct image. |
| 304 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | % o metric: the metric. |
| 306 | % |
| 307 | % o distortion: the computed distortion between the images. |
| 308 | % |
| 309 | % o exception: return any errors or warnings in this structure. |
| 310 | % |
| 311 | */ |
| 312 | |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 313 | static inline double MagickMax(const double x,const double y) |
| 314 | { |
| 315 | if (x > y) |
| 316 | return(x); |
| 317 | return(y); |
| 318 | } |
| 319 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 320 | static MagickBooleanType GetAbsoluteDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 321 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 322 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 323 | CacheView |
| 324 | *image_view, |
| 325 | *reconstruct_view; |
| 326 | |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 327 | double |
| 328 | fuzz; |
| 329 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 330 | MagickBooleanType |
| 331 | status; |
| 332 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 333 | ssize_t |
| 334 | y; |
| 335 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | /* |
| 337 | Compute the absolute difference in pixels between two images. |
| 338 | */ |
| 339 | status=MagickTrue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 340 | if (image->fuzz == 0.0) |
| 341 | fuzz=MagickMax(reconstruct_image->fuzz,MagickSQ1_2)* |
| 342 | MagickMax(reconstruct_image->fuzz,MagickSQ1_2); |
| 343 | else |
| 344 | if (reconstruct_image->fuzz == 0.0) |
| 345 | fuzz=MagickMax(image->fuzz,MagickSQ1_2)* |
| 346 | MagickMax(image->fuzz,MagickSQ1_2); |
| 347 | else |
| 348 | fuzz=MagickMax(image->fuzz,MagickSQ1_2)* |
| 349 | MagickMax(reconstruct_image->fuzz,MagickSQ1_2); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 350 | image_view=AcquireVirtualCacheView(image,exception); |
| 351 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 352 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 353 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 354 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 356 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | { |
| 358 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 359 | channel_distortion[MaxPixelChannels+1]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 361 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 362 | *restrict p, |
| 363 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 365 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | i, |
| 367 | x; |
| 368 | |
| 369 | if (status == MagickFalse) |
| 370 | continue; |
| 371 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 372 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 373 | 1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 374 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 375 | { |
| 376 | status=MagickFalse; |
| 377 | continue; |
| 378 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 379 | (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 380 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 382 | double |
| 383 | Da, |
| 384 | Sa; |
| 385 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 386 | MagickBooleanType |
| 387 | difference; |
| 388 | |
| 389 | register ssize_t |
| 390 | i; |
| 391 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 392 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 393 | { |
| 394 | p+=GetPixelChannels(image); |
| 395 | q+=GetPixelChannels(reconstruct_image); |
| 396 | continue; |
| 397 | } |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 398 | difference=MagickFalse; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 399 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 400 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 401 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 402 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 403 | double |
| 404 | distance; |
| 405 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 406 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 407 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 408 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 409 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 410 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 411 | (reconstruct_traits == UndefinedPixelTrait) || |
| 412 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 413 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 414 | distance=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q); |
| 415 | if ((distance*distance) > fuzz) |
| 416 | { |
| 417 | difference=MagickTrue; |
| 418 | break; |
| 419 | } |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 420 | } |
| 421 | if (difference != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 422 | { |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 423 | channel_distortion[i]++; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 424 | channel_distortion[CompositePixelChannel]++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 425 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 426 | p+=GetPixelChannels(image); |
| 427 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 428 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 429 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 430 | #pragma omp critical (MagickCore_GetAbsoluteError) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 431 | #endif |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 432 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 433 | distortion[i]+=channel_distortion[i]; |
| 434 | } |
| 435 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 436 | image_view=DestroyCacheView(image_view); |
| 437 | return(status); |
| 438 | } |
| 439 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 440 | static size_t GetImageChannels(const Image *image) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | { |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 442 | register ssize_t |
| 443 | i; |
| 444 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 445 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | channels; |
| 447 | |
| 448 | channels=0; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 449 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 450 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 451 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 452 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 453 | if ((traits & UpdatePixelTrait) != 0) |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 454 | channels++; |
| 455 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | return(channels); |
| 457 | } |
| 458 | |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 459 | static MagickBooleanType GetFuzzDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 460 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 461 | { |
| 462 | CacheView |
| 463 | *image_view, |
| 464 | *reconstruct_view; |
| 465 | |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 466 | MagickBooleanType |
| 467 | status; |
| 468 | |
| 469 | register ssize_t |
| 470 | i; |
| 471 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 472 | ssize_t |
| 473 | y; |
| 474 | |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 475 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 476 | image_view=AcquireVirtualCacheView(image,exception); |
| 477 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 478 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 479 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 480 | magick_threads(image,image,image->rows,1) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 481 | #endif |
| 482 | for (y=0; y < (ssize_t) image->rows; y++) |
| 483 | { |
| 484 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 485 | channel_distortion[MaxPixelChannels+1]; |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 486 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 487 | register const Quantum |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 488 | *restrict p, |
| 489 | *restrict q; |
| 490 | |
| 491 | register ssize_t |
| 492 | i, |
| 493 | x; |
| 494 | |
| 495 | if (status == MagickFalse) |
| 496 | continue; |
| 497 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | e6529ff | 2011-02-04 20:05:32 +0000 | [diff] [blame] | 498 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 499 | 1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 500 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 501 | { |
| 502 | status=MagickFalse; |
| 503 | continue; |
| 504 | } |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 505 | (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion)); |
| 506 | for (x=0; x < (ssize_t) image->columns; x++) |
| 507 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 508 | double |
| 509 | Da, |
| 510 | Sa; |
| 511 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 512 | register ssize_t |
| 513 | i; |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 514 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 515 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 516 | { |
| 517 | p+=GetPixelChannels(image); |
| 518 | q+=GetPixelChannels(reconstruct_image); |
| 519 | continue; |
| 520 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 521 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 522 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 523 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 524 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 525 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 526 | distance; |
| 527 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 528 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 529 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 530 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 531 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 532 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 533 | (reconstruct_traits == UndefinedPixelTrait) || |
| 534 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 535 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 536 | distance=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image, |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 537 | channel,q)); |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 538 | channel_distortion[i]+=distance*distance; |
| 539 | channel_distortion[CompositePixelChannel]+=distance*distance; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 540 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 541 | p+=GetPixelChannels(image); |
| 542 | q+=GetPixelChannels(reconstruct_image); |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 543 | } |
| 544 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ad5c2a | 2012-05-05 21:32:54 +0000 | [diff] [blame] | 545 | #pragma omp critical (MagickCore_GetFuzzDistortion) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 546 | #endif |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 547 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 548 | distortion[i]+=channel_distortion[i]; |
| 549 | } |
| 550 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 551 | image_view=DestroyCacheView(image_view); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 552 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 553 | distortion[i]/=((double) image->columns*image->rows); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 554 | distortion[CompositePixelChannel]/=(double) GetImageChannels(image); |
| 555 | distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]); |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 556 | return(status); |
| 557 | } |
| 558 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 559 | static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 560 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 562 | CacheView |
| 563 | *image_view, |
| 564 | *reconstruct_view; |
| 565 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 566 | MagickBooleanType |
| 567 | status; |
| 568 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 569 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 570 | i; |
| 571 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 572 | ssize_t |
| 573 | y; |
| 574 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 576 | image_view=AcquireVirtualCacheView(image,exception); |
| 577 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 578 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 579 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 580 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 581 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 582 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 583 | { |
| 584 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 585 | channel_distortion[MaxPixelChannels+1]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 587 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 588 | *restrict p, |
| 589 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 591 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 592 | i, |
| 593 | x; |
| 594 | |
| 595 | if (status == MagickFalse) |
| 596 | continue; |
| 597 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 598 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 599 | 1,exception); |
| 600 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 601 | { |
| 602 | status=MagickFalse; |
| 603 | continue; |
| 604 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 606 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 608 | double |
| 609 | Da, |
| 610 | Sa; |
| 611 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 612 | register ssize_t |
| 613 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 615 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 616 | { |
| 617 | p+=GetPixelChannels(image); |
| 618 | q+=GetPixelChannels(reconstruct_image); |
| 619 | continue; |
| 620 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 621 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 622 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 623 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 624 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 625 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 626 | distance; |
| 627 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 628 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 629 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 630 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 631 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 632 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 633 | (reconstruct_traits == UndefinedPixelTrait) || |
| 634 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 635 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 636 | distance=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image, |
| 637 | channel,q)); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 638 | channel_distortion[i]+=distance; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 639 | channel_distortion[CompositePixelChannel]+=distance; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 640 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 641 | p+=GetPixelChannels(image); |
| 642 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 643 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 644 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 645 | #pragma omp critical (MagickCore_GetMeanAbsoluteError) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | #endif |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 647 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | distortion[i]+=channel_distortion[i]; |
| 649 | } |
| 650 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 651 | image_view=DestroyCacheView(image_view); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 652 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 653 | distortion[i]/=((double) image->columns*image->rows); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 654 | distortion[CompositePixelChannel]/=(double) GetImageChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | return(status); |
| 656 | } |
| 657 | |
| 658 | static MagickBooleanType GetMeanErrorPerPixel(Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 659 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 660 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 661 | CacheView |
| 662 | *image_view, |
| 663 | *reconstruct_view; |
| 664 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 665 | MagickBooleanType |
| 666 | status; |
| 667 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 668 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 669 | area, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | maximum_error, |
| 671 | mean_error; |
| 672 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 673 | ssize_t |
| 674 | y; |
| 675 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 676 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 677 | area=0.0; |
| 678 | maximum_error=0.0; |
| 679 | mean_error=0.0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 680 | image_view=AcquireVirtualCacheView(image,exception); |
| 681 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 682 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 684 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 685 | *restrict p, |
| 686 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 688 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 689 | x; |
| 690 | |
| 691 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 692 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 693 | 1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 694 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 695 | { |
| 696 | status=MagickFalse; |
| 697 | break; |
| 698 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 699 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 700 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 701 | double |
| 702 | Da, |
| 703 | Sa; |
| 704 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 705 | register ssize_t |
| 706 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 707 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 708 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 709 | { |
| 710 | p+=GetPixelChannels(image); |
| 711 | q+=GetPixelChannels(reconstruct_image); |
| 712 | continue; |
| 713 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 714 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 715 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 716 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 717 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 718 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 719 | distance; |
| 720 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 721 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 722 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 723 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 724 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 725 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 726 | (reconstruct_traits == UndefinedPixelTrait) || |
| 727 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 728 | continue; |
cristy | ce31db4 | 2013-02-22 02:20:31 +0000 | [diff] [blame] | 729 | distance=fabs((double) (Sa*p[i]-Da*GetPixelChannel(reconstruct_image, |
| 730 | channel,q))); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 731 | distortion[i]+=distance; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 732 | distortion[CompositePixelChannel]+=distance; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 733 | mean_error+=distance*distance; |
| 734 | if (distance > maximum_error) |
| 735 | maximum_error=distance; |
| 736 | area++; |
| 737 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 738 | p+=GetPixelChannels(image); |
| 739 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 743 | image_view=DestroyCacheView(image_view); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 744 | image->error.mean_error_per_pixel=distortion[CompositePixelChannel]/area; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 745 | image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area; |
| 746 | image->error.normalized_maximum_error=QuantumScale*maximum_error; |
| 747 | return(status); |
| 748 | } |
| 749 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 750 | static MagickBooleanType GetMeanSquaredDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 751 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 753 | CacheView |
| 754 | *image_view, |
| 755 | *reconstruct_view; |
| 756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | MagickBooleanType |
| 758 | status; |
| 759 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 760 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 761 | i; |
| 762 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 763 | ssize_t |
| 764 | y; |
| 765 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 766 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 767 | image_view=AcquireVirtualCacheView(image,exception); |
| 768 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 769 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 770 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 771 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 772 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 773 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | { |
| 775 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 776 | channel_distortion[MaxPixelChannels+1]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 777 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 778 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 779 | *restrict p, |
| 780 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 781 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 782 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | i, |
| 784 | x; |
| 785 | |
| 786 | if (status == MagickFalse) |
| 787 | continue; |
| 788 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 789 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 790 | 1,exception); |
| 791 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 792 | { |
| 793 | status=MagickFalse; |
| 794 | continue; |
| 795 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 796 | (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 797 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 799 | double |
| 800 | Da, |
| 801 | Sa; |
| 802 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 803 | register ssize_t |
| 804 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 806 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 807 | { |
| 808 | p+=GetPixelChannels(image); |
| 809 | q+=GetPixelChannels(reconstruct_image); |
| 810 | continue; |
| 811 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 812 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 813 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 814 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 815 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 816 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 817 | distance; |
| 818 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 819 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 820 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 821 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 822 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 823 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 824 | (reconstruct_traits == UndefinedPixelTrait) || |
| 825 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 826 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 827 | distance=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image, |
| 828 | channel,q)); |
| 829 | channel_distortion[i]+=distance*distance; |
| 830 | channel_distortion[CompositePixelChannel]+=distance*distance; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 831 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 832 | p+=GetPixelChannels(image); |
| 833 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 834 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 835 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 836 | #pragma omp critical (MagickCore_GetMeanSquaredError) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | #endif |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 838 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 839 | distortion[i]+=channel_distortion[i]; |
| 840 | } |
| 841 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 842 | image_view=DestroyCacheView(image_view); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 843 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 844 | distortion[i]/=((double) image->columns*image->rows); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 845 | distortion[CompositePixelChannel]/=GetImageChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 846 | return(status); |
| 847 | } |
| 848 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 849 | static MagickBooleanType GetNormalizedCrossCorrelationDistortion( |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 850 | const Image *image,const Image *reconstruct_image,double *distortion, |
| 851 | ExceptionInfo *exception) |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 852 | { |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 853 | #define SimilarityImageTag "Similarity/Image" |
| 854 | |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 855 | CacheView |
| 856 | *image_view, |
| 857 | *reconstruct_view; |
| 858 | |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 859 | ChannelStatistics |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 860 | *image_statistics, |
| 861 | *reconstruct_statistics; |
| 862 | |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 863 | double |
| 864 | area; |
| 865 | |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 866 | MagickBooleanType |
| 867 | status; |
| 868 | |
cristy | 18a4136 | 2010-11-27 15:56:18 +0000 | [diff] [blame] | 869 | MagickOffsetType |
| 870 | progress; |
| 871 | |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 872 | register ssize_t |
| 873 | i; |
| 874 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 875 | ssize_t |
| 876 | y; |
| 877 | |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 878 | /* |
cristy | 18a4136 | 2010-11-27 15:56:18 +0000 | [diff] [blame] | 879 | Normalize to account for variation due to lighting and exposure condition. |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 880 | */ |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 881 | image_statistics=GetImageStatistics(image,exception); |
| 882 | reconstruct_statistics=GetImageStatistics(reconstruct_image,exception); |
cristy | e287bba | 2013-09-02 20:04:42 +0000 | [diff] [blame] | 883 | if ((image_statistics == (ChannelStatistics *) NULL) || |
| 884 | (reconstruct_statistics == (ChannelStatistics *) NULL)) |
| 885 | { |
| 886 | if (image_statistics != (ChannelStatistics *) NULL) |
| 887 | image_statistics=(ChannelStatistics *) RelinquishMagickMemory( |
| 888 | image_statistics); |
| 889 | if (reconstruct_statistics != (ChannelStatistics *) NULL) |
| 890 | reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory( |
| 891 | reconstruct_statistics); |
| 892 | return(MagickFalse); |
| 893 | } |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 894 | status=MagickTrue; |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 895 | progress=0; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 896 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 897 | distortion[i]=0.0; |
cristy | 2cb55d5 | 2013-07-10 22:20:51 +0000 | [diff] [blame] | 898 | area=1.0/((double) image->columns*image->rows); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 899 | image_view=AcquireVirtualCacheView(image,exception); |
| 900 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 901 | for (y=0; y < (ssize_t) image->rows; y++) |
| 902 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 903 | register const Quantum |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 904 | *restrict p, |
| 905 | *restrict q; |
| 906 | |
| 907 | register ssize_t |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 908 | x; |
| 909 | |
| 910 | if (status == MagickFalse) |
| 911 | continue; |
| 912 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 913 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 914 | 1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 915 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 916 | { |
| 917 | status=MagickFalse; |
| 918 | continue; |
| 919 | } |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 920 | for (x=0; x < (ssize_t) image->columns; x++) |
| 921 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 922 | double |
| 923 | Da, |
| 924 | Sa; |
| 925 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 926 | register ssize_t |
| 927 | i; |
| 928 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 929 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 930 | { |
| 931 | p+=GetPixelChannels(image); |
| 932 | q+=GetPixelChannels(reconstruct_image); |
| 933 | continue; |
| 934 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 935 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 936 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 937 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 938 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 939 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 940 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 941 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 942 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 943 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 944 | (reconstruct_traits == UndefinedPixelTrait) || |
| 945 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 946 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 947 | distortion[i]+=area*QuantumScale*(Sa*p[i]-image_statistics[i].mean)* |
| 948 | (Da*GetPixelChannel(reconstruct_image,channel,q)- |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 949 | reconstruct_statistics[channel].mean); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 950 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 951 | p+=GetPixelChannels(image); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 952 | q+=GetPixelChannels(reconstruct_image); |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 953 | } |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 954 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 955 | { |
| 956 | MagickBooleanType |
| 957 | proceed; |
| 958 | |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 959 | proceed=SetImageProgress(image,SimilarityImageTag,progress++, |
| 960 | image->rows); |
| 961 | if (proceed == MagickFalse) |
| 962 | status=MagickFalse; |
| 963 | } |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 964 | } |
| 965 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 966 | image_view=DestroyCacheView(image_view); |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 967 | /* |
| 968 | Divide by the standard deviation. |
| 969 | */ |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 970 | distortion[CompositePixelChannel]=0.0; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 971 | for (i=0; i < MaxPixelChannels; i++) |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 972 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 973 | double |
cristy | 18a4136 | 2010-11-27 15:56:18 +0000 | [diff] [blame] | 974 | gamma; |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 975 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 976 | PixelChannel channel=GetPixelChannelChannel(image,i); |
cristy | 18a4136 | 2010-11-27 15:56:18 +0000 | [diff] [blame] | 977 | gamma=image_statistics[i].standard_deviation* |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 978 | reconstruct_statistics[channel].standard_deviation; |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 979 | gamma=PerceptibleReciprocal(gamma); |
cristy | 18a4136 | 2010-11-27 15:56:18 +0000 | [diff] [blame] | 980 | distortion[i]=QuantumRange*gamma*distortion[i]; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 981 | distortion[CompositePixelChannel]+=distortion[i]*distortion[i]; |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 982 | } |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 983 | distortion[CompositePixelChannel]=sqrt(distortion[CompositePixelChannel]/ |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 984 | GetImageChannels(image)); |
cristy | 34d6fdc | 2010-11-26 19:06:08 +0000 | [diff] [blame] | 985 | /* |
| 986 | Free resources. |
| 987 | */ |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 988 | reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory( |
| 989 | reconstruct_statistics); |
| 990 | image_statistics=(ChannelStatistics *) RelinquishMagickMemory( |
| 991 | image_statistics); |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 992 | return(status); |
| 993 | } |
| 994 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 995 | static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 996 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 998 | CacheView |
| 999 | *image_view, |
| 1000 | *reconstruct_view; |
| 1001 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | MagickBooleanType |
| 1003 | status; |
| 1004 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1005 | ssize_t |
| 1006 | y; |
| 1007 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1008 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1009 | image_view=AcquireVirtualCacheView(image,exception); |
| 1010 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1011 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1012 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1013 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1014 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1015 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1016 | { |
| 1017 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1018 | channel_distortion[MaxPixelChannels+1]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1019 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1020 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1021 | *restrict p, |
| 1022 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1023 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1024 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1025 | i, |
| 1026 | x; |
| 1027 | |
| 1028 | if (status == MagickFalse) |
| 1029 | continue; |
| 1030 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1031 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 1032 | 1,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1033 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1034 | { |
| 1035 | status=MagickFalse; |
| 1036 | continue; |
| 1037 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1038 | (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1039 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | { |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 1041 | double |
| 1042 | Da, |
| 1043 | Sa; |
| 1044 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1045 | register ssize_t |
| 1046 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1047 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1048 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1049 | { |
| 1050 | p+=GetPixelChannels(image); |
| 1051 | q+=GetPixelChannels(reconstruct_image); |
| 1052 | continue; |
| 1053 | } |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 1054 | Sa=QuantumScale*GetPixelAlpha(image,p); |
| 1055 | Da=QuantumScale*GetPixelAlpha(reconstruct_image,q); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1056 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1057 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1058 | double |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1059 | distance; |
| 1060 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1061 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1062 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 1063 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 1064 | channel); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1065 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1066 | (reconstruct_traits == UndefinedPixelTrait) || |
| 1067 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1068 | continue; |
cristy | db5c82c | 2013-02-22 00:41:33 +0000 | [diff] [blame] | 1069 | distance=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image, |
| 1070 | channel,q)); |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 1071 | if (distance > channel_distortion[i]) |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1072 | channel_distortion[i]=distance; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1073 | if (distance > channel_distortion[CompositePixelChannel]) |
| 1074 | channel_distortion[CompositePixelChannel]=distance; |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1075 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1076 | p+=GetPixelChannels(image); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1077 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1078 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1079 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1080 | #pragma omp critical (MagickCore_GetPeakAbsoluteError) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1081 | #endif |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1082 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1083 | if (channel_distortion[i] > distortion[i]) |
| 1084 | distortion[i]=channel_distortion[i]; |
| 1085 | } |
| 1086 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 1087 | image_view=DestroyCacheView(image_view); |
| 1088 | return(status); |
| 1089 | } |
| 1090 | |
cristy | d94b0b3 | 2014-01-30 23:19:20 +0000 | [diff] [blame] | 1091 | static double Decibels(double linear) |
| 1092 | { |
| 1093 | if (linear < MagickEpsilon) |
| 1094 | linear=MagickEpsilon; |
| 1095 | return(20.0*log10(linear)); |
| 1096 | } |
| 1097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1098 | static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1099 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1100 | { |
| 1101 | MagickBooleanType |
| 1102 | status; |
| 1103 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1104 | register ssize_t |
| 1105 | i; |
| 1106 | |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1107 | status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1108 | for (i=0; i <= MaxPixelChannels; i++) |
cristy | d94b0b3 | 2014-01-30 23:19:20 +0000 | [diff] [blame] | 1109 | distortion[i]=Decibels((double) 1.0/sqrt(distortion[i])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | return(status); |
| 1111 | } |
| 1112 | |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1113 | static Image *PerceptualImageHash(const Image *image, |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1114 | const ColorspaceType colorspace,ExceptionInfo *exception) |
| 1115 | { |
| 1116 | Image |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1117 | *phash_image; |
| 1118 | |
| 1119 | MagickBooleanType |
| 1120 | status; |
| 1121 | |
| 1122 | /* |
| 1123 | Transform colorspace then blur perceptual hash image. |
| 1124 | */ |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1125 | phash_image=BlurImage(image,0.0,1.0,exception); |
| 1126 | if (phash_image == (Image *) NULL) |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1127 | return((Image *) NULL); |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1128 | phash_image->depth=8; |
cristy | 339da3a | 2014-01-30 19:15:06 +0000 | [diff] [blame] | 1129 | status=TransformImageColorspace(phash_image,colorspace,exception); |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1130 | if (status == MagickFalse) |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1131 | phash_image=DestroyImage(phash_image); |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1132 | return(phash_image); |
| 1133 | } |
| 1134 | |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1135 | static MagickBooleanType GetPerceptualHashDistortion(const Image *image, |
| 1136 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
| 1137 | { |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1138 | ChannelMoments |
| 1139 | *image_moments, |
| 1140 | *reconstruct_moments; |
| 1141 | |
cristy | fc44196 | 2014-01-30 21:57:14 +0000 | [diff] [blame] | 1142 | double |
| 1143 | alpha, |
| 1144 | beta, |
| 1145 | difference; |
| 1146 | |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1147 | Image |
cristy | 56c028a | 2014-01-30 13:00:44 +0000 | [diff] [blame] | 1148 | *phash_image; |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1149 | |
cristy | d9244cd | 2014-01-30 23:09:31 +0000 | [diff] [blame] | 1150 | MagickBooleanType |
| 1151 | grayscale; |
| 1152 | |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1153 | register ssize_t |
| 1154 | i; |
| 1155 | |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1156 | /* |
| 1157 | Compute perceptual hash in the native image colorspace. |
| 1158 | */ |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1159 | phash_image=PerceptualImageHash(image,image->colorspace,exception); |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1160 | if (phash_image == (Image *) NULL) |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1161 | return(MagickFalse); |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1162 | image_moments=GetImageMoments(phash_image,exception); |
| 1163 | phash_image=DestroyImage(phash_image); |
cristy | 2661ac1 | 2014-01-10 14:21:07 +0000 | [diff] [blame] | 1164 | if (image_moments == (ChannelMoments *) NULL) |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1165 | return(MagickFalse); |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1166 | phash_image=PerceptualImageHash(reconstruct_image, |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1167 | reconstruct_image->colorspace,exception); |
cristy | 56c028a | 2014-01-30 13:00:44 +0000 | [diff] [blame] | 1168 | if (phash_image == (Image *) NULL) |
cristy | 2661ac1 | 2014-01-10 14:21:07 +0000 | [diff] [blame] | 1169 | { |
| 1170 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
cristy | 2661ac1 | 2014-01-10 14:21:07 +0000 | [diff] [blame] | 1171 | return(MagickFalse); |
| 1172 | } |
cristy | 56c028a | 2014-01-30 13:00:44 +0000 | [diff] [blame] | 1173 | reconstruct_moments=GetImageMoments(phash_image,exception); |
| 1174 | phash_image=DestroyImage(phash_image); |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1175 | if (reconstruct_moments == (ChannelMoments *) NULL) |
| 1176 | { |
| 1177 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
| 1178 | return(MagickFalse); |
| 1179 | } |
cristy | dc9136b | 2014-01-24 01:39:00 +0000 | [diff] [blame] | 1180 | for (i=0; i < 7; i++) |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1181 | { |
cristy | fc44196 | 2014-01-30 21:57:14 +0000 | [diff] [blame] | 1182 | ssize_t |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1183 | channel; |
cristy | 6bd008f | 2014-01-30 23:24:06 +0000 | [diff] [blame^] | 1184 | |
cristy | 5401eb3 | 2014-01-09 14:38:12 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | Compute sum of moment differences squared. |
| 1187 | */ |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1188 | for (channel=0; channel < MaxPixelChannels; channel++) |
| 1189 | { |
cristy | fc44196 | 2014-01-30 21:57:14 +0000 | [diff] [blame] | 1190 | alpha=fabs(image_moments[channel].I[i]); |
| 1191 | beta=fabs(reconstruct_moments[channel].I[i]); |
cristy | 6bd008f | 2014-01-30 23:24:06 +0000 | [diff] [blame^] | 1192 | difference=(beta < MagickEpsilon ? -MagickEpsilon : log10(beta))- |
| 1193 | (alpha < MagickEpsilon ? -MagickEpsilon : log10(alpha)); |
cristy | 70be793 | 2014-01-09 13:18:24 +0000 | [diff] [blame] | 1194 | distortion[channel]+=difference*difference; |
| 1195 | distortion[CompositePixelChannel]+=difference*difference; |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
cristy | 2661ac1 | 2014-01-10 14:21:07 +0000 | [diff] [blame] | 1198 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
cristy | bc0adce | 2014-01-08 23:15:49 +0000 | [diff] [blame] | 1199 | reconstruct_moments=(ChannelMoments *) RelinquishMagickMemory( |
| 1200 | reconstruct_moments); |
cristy | d9244cd | 2014-01-30 23:09:31 +0000 | [diff] [blame] | 1201 | grayscale=(IsImageGray(image,exception) != MagickFalse) && |
| 1202 | (IsImageGray(reconstruct_image,exception) != MagickFalse) ? MagickTrue : |
| 1203 | MagickFalse; |
cristy | 7a589a3 | 2014-01-30 21:27:40 +0000 | [diff] [blame] | 1204 | if (grayscale != MagickFalse) |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1205 | return(MagickTrue); |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1206 | /* |
| 1207 | Compute perceptual hash in the HCLP colorspace. |
| 1208 | */ |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1209 | phash_image=PerceptualImageHash(image,HCLpColorspace,exception); |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1210 | if (phash_image == (Image *) NULL) |
| 1211 | return(MagickFalse); |
| 1212 | image_moments=GetImageMoments(phash_image,exception); |
| 1213 | phash_image=DestroyImage(phash_image); |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1214 | if (image_moments == (ChannelMoments *) NULL) |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1215 | return(MagickFalse); |
cristy | e80f15f | 2014-01-30 19:13:38 +0000 | [diff] [blame] | 1216 | phash_image=PerceptualImageHash(reconstruct_image,HCLpColorspace,exception); |
cristy | 56c028a | 2014-01-30 13:00:44 +0000 | [diff] [blame] | 1217 | if (phash_image == (Image *) NULL) |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1218 | { |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1219 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1220 | return(MagickFalse); |
| 1221 | } |
cristy | 56c028a | 2014-01-30 13:00:44 +0000 | [diff] [blame] | 1222 | reconstruct_moments=GetImageMoments(phash_image,exception); |
| 1223 | phash_image=DestroyImage(phash_image); |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1224 | if (reconstruct_moments == (ChannelMoments *) NULL) |
| 1225 | { |
| 1226 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
| 1227 | return(MagickFalse); |
| 1228 | } |
cristy | dc9136b | 2014-01-24 01:39:00 +0000 | [diff] [blame] | 1229 | for (i=0; i < 7; i++) |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1230 | { |
cristy | fc44196 | 2014-01-30 21:57:14 +0000 | [diff] [blame] | 1231 | ssize_t |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1232 | channel; |
cristy | 6bd008f | 2014-01-30 23:24:06 +0000 | [diff] [blame^] | 1233 | |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1234 | /* |
| 1235 | Compute sum of moment differences squared. |
| 1236 | */ |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1237 | for (channel=0; channel < MaxPixelChannels; channel++) |
| 1238 | { |
cristy | fc44196 | 2014-01-30 21:57:14 +0000 | [diff] [blame] | 1239 | alpha=fabs(image_moments[channel].I[i]); |
| 1240 | beta=fabs(reconstruct_moments[channel].I[i]); |
cristy | 6bd008f | 2014-01-30 23:24:06 +0000 | [diff] [blame^] | 1241 | difference=(beta < MagickEpsilon ? -MagickEpsilon : log10(beta))- |
| 1242 | (alpha < MagickEpsilon ? -MagickEpsilon : log10(alpha)); |
cristy | 21b687b | 2014-01-10 00:17:34 +0000 | [diff] [blame] | 1243 | distortion[channel]+=difference*difference; |
| 1244 | distortion[CompositePixelChannel]+=difference*difference; |
| 1245 | } |
| 1246 | } |
| 1247 | image_moments=(ChannelMoments *) RelinquishMagickMemory(image_moments); |
| 1248 | reconstruct_moments=(ChannelMoments *) RelinquishMagickMemory( |
| 1249 | reconstruct_moments); |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1250 | return(MagickTrue); |
| 1251 | } |
| 1252 | |
cristy | 337bf65 | 2014-01-30 12:54:20 +0000 | [diff] [blame] | 1253 | |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1254 | static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1255 | const Image *reconstruct_image,double *distortion,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1256 | { |
| 1257 | MagickBooleanType |
| 1258 | status; |
| 1259 | |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1260 | register ssize_t |
| 1261 | i; |
| 1262 | |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1263 | status=GetMeanSquaredDistortion(image,reconstruct_image,distortion,exception); |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1264 | for (i=0; i <= MaxPixelChannels; i++) |
| 1265 | distortion[i]=sqrt(distortion[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1266 | return(status); |
| 1267 | } |
| 1268 | |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1269 | MagickExport MagickBooleanType GetImageDistortion(Image *image, |
| 1270 | const Image *reconstruct_image,const MetricType metric,double *distortion, |
| 1271 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1272 | { |
| 1273 | double |
| 1274 | *channel_distortion; |
| 1275 | |
| 1276 | MagickBooleanType |
| 1277 | status; |
| 1278 | |
| 1279 | size_t |
| 1280 | length; |
| 1281 | |
| 1282 | assert(image != (Image *) NULL); |
| 1283 | assert(image->signature == MagickSignature); |
| 1284 | if (image->debug != MagickFalse) |
| 1285 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1286 | assert(reconstruct_image != (const Image *) NULL); |
| 1287 | assert(reconstruct_image->signature == MagickSignature); |
| 1288 | assert(distortion != (double *) NULL); |
| 1289 | *distortion=0.0; |
| 1290 | if (image->debug != MagickFalse) |
| 1291 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 8b520b4 | 2014-01-30 00:58:45 +0000 | [diff] [blame] | 1292 | if (metric != PerceptualHashErrorMetric) |
| 1293 | if ((reconstruct_image->columns != image->columns) || |
| 1294 | (reconstruct_image->rows != image->rows)) |
| 1295 | ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | /* |
| 1297 | Get image distortion. |
| 1298 | */ |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1299 | length=MaxPixelChannels+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1300 | channel_distortion=(double *) AcquireQuantumMemory(length, |
| 1301 | sizeof(*channel_distortion)); |
| 1302 | if (channel_distortion == (double *) NULL) |
| 1303 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
| 1304 | (void) ResetMagickMemory(channel_distortion,0,length* |
| 1305 | sizeof(*channel_distortion)); |
| 1306 | switch (metric) |
| 1307 | { |
| 1308 | case AbsoluteErrorMetric: |
| 1309 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1310 | status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion, |
| 1311 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | } |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 1314 | case FuzzErrorMetric: |
| 1315 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1316 | status=GetFuzzDistortion(image,reconstruct_image,channel_distortion, |
| 1317 | exception); |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1320 | case MeanAbsoluteErrorMetric: |
| 1321 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1322 | status=GetMeanAbsoluteDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1323 | channel_distortion,exception); |
| 1324 | break; |
| 1325 | } |
cristy | 03fa69f | 2014-01-08 18:36:49 +0000 | [diff] [blame] | 1326 | case MeanErrorPerPixelErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1327 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1328 | status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion, |
| 1329 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | case MeanSquaredErrorMetric: |
| 1333 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1334 | status=GetMeanSquaredDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1335 | channel_distortion,exception); |
| 1336 | break; |
| 1337 | } |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1338 | case NormalizedCrossCorrelationErrorMetric: |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1339 | default: |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1340 | { |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1341 | status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1342 | channel_distortion,exception); |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1343 | break; |
| 1344 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1345 | case PeakAbsoluteErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1346 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1347 | status=GetPeakAbsoluteDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1348 | channel_distortion,exception); |
| 1349 | break; |
| 1350 | } |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1351 | case PeakSignalToNoiseRatioErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1352 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1353 | status=GetPeakSignalToNoiseRatio(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1354 | channel_distortion,exception); |
| 1355 | break; |
| 1356 | } |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1357 | case PerceptualHashErrorMetric: |
| 1358 | { |
| 1359 | status=GetPerceptualHashDistortion(image,reconstruct_image, |
| 1360 | channel_distortion,exception); |
| 1361 | break; |
| 1362 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | case RootMeanSquaredErrorMetric: |
| 1364 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1365 | status=GetRootMeanSquaredDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1366 | channel_distortion,exception); |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1370 | *distortion=channel_distortion[CompositePixelChannel]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1371 | channel_distortion=(double *) RelinquishMagickMemory(channel_distortion); |
cristy | 65aa934 | 2013-09-05 12:04:27 +0000 | [diff] [blame] | 1372 | (void) FormatImageProperty(image,"distortion","%.*g",GetMagickPrecision(), |
cristy | e863c05 | 2013-09-04 11:31:30 +0000 | [diff] [blame] | 1373 | *distortion); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1374 | return(status); |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1379 | % % |
| 1380 | % % |
| 1381 | % % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1382 | % G e t I m a g e D i s t o r t i o n s % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | % % |
| 1384 | % % |
| 1385 | % % |
| 1386 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1387 | % |
cristy | 44097f5 | 2012-12-16 19:56:20 +0000 | [diff] [blame] | 1388 | % GetImageDistortions() compares the pixel channels of an image to a |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1389 | % reconstructed image and returns the specified distortion metric for each |
| 1390 | % channel. |
| 1391 | % |
cristy | 44097f5 | 2012-12-16 19:56:20 +0000 | [diff] [blame] | 1392 | % The format of the GetImageDistortions method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1393 | % |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1394 | % double *GetImageDistortions(const Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1395 | % const Image *reconstruct_image,const MetricType metric, |
| 1396 | % ExceptionInfo *exception) |
| 1397 | % |
| 1398 | % A description of each parameter follows: |
| 1399 | % |
| 1400 | % o image: the image. |
| 1401 | % |
| 1402 | % o reconstruct_image: the reconstruct image. |
| 1403 | % |
| 1404 | % o metric: the metric. |
| 1405 | % |
| 1406 | % o exception: return any errors or warnings in this structure. |
| 1407 | % |
| 1408 | */ |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1409 | MagickExport double *GetImageDistortions(Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1410 | const Image *reconstruct_image,const MetricType metric, |
| 1411 | ExceptionInfo *exception) |
| 1412 | { |
| 1413 | double |
| 1414 | *channel_distortion; |
| 1415 | |
| 1416 | MagickBooleanType |
| 1417 | status; |
| 1418 | |
| 1419 | size_t |
| 1420 | length; |
| 1421 | |
| 1422 | assert(image != (Image *) NULL); |
| 1423 | assert(image->signature == MagickSignature); |
| 1424 | if (image->debug != MagickFalse) |
| 1425 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1426 | assert(reconstruct_image != (const Image *) NULL); |
| 1427 | assert(reconstruct_image->signature == MagickSignature); |
| 1428 | if (image->debug != MagickFalse) |
| 1429 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 8b520b4 | 2014-01-30 00:58:45 +0000 | [diff] [blame] | 1430 | if (metric != PerceptualHashErrorMetric) |
| 1431 | if ((reconstruct_image->columns != image->columns) || |
| 1432 | (reconstruct_image->rows != image->rows)) |
| 1433 | { |
| 1434 | (void) ThrowMagickException(exception,GetMagickModule(),ImageError, |
| 1435 | "ImageSizeDiffers","`%s'",image->filename); |
| 1436 | return((double *) NULL); |
| 1437 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1438 | /* |
| 1439 | Get image distortion. |
| 1440 | */ |
cristy | 3fc482f | 2011-09-23 00:43:35 +0000 | [diff] [blame] | 1441 | length=MaxPixelChannels+1UL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1442 | channel_distortion=(double *) AcquireQuantumMemory(length, |
| 1443 | sizeof(*channel_distortion)); |
| 1444 | if (channel_distortion == (double *) NULL) |
| 1445 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
| 1446 | (void) ResetMagickMemory(channel_distortion,0,length* |
| 1447 | sizeof(*channel_distortion)); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1448 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1449 | switch (metric) |
| 1450 | { |
| 1451 | case AbsoluteErrorMetric: |
| 1452 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1453 | status=GetAbsoluteDistortion(image,reconstruct_image,channel_distortion, |
| 1454 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | } |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 1457 | case FuzzErrorMetric: |
| 1458 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1459 | status=GetFuzzDistortion(image,reconstruct_image,channel_distortion, |
| 1460 | exception); |
cristy | 343eee9 | 2010-12-11 02:17:57 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1463 | case MeanAbsoluteErrorMetric: |
| 1464 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1465 | status=GetMeanAbsoluteDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1466 | channel_distortion,exception); |
| 1467 | break; |
| 1468 | } |
cristy | 03fa69f | 2014-01-08 18:36:49 +0000 | [diff] [blame] | 1469 | case MeanErrorPerPixelErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1470 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1471 | status=GetMeanErrorPerPixel(image,reconstruct_image,channel_distortion, |
| 1472 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | case MeanSquaredErrorMetric: |
| 1476 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1477 | status=GetMeanSquaredDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1478 | channel_distortion,exception); |
| 1479 | break; |
| 1480 | } |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1481 | case NormalizedCrossCorrelationErrorMetric: |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1482 | default: |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1483 | { |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1484 | status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image, |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1485 | channel_distortion,exception); |
cristy | 4c929a7 | 2010-11-24 18:54:42 +0000 | [diff] [blame] | 1486 | break; |
| 1487 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1488 | case PeakAbsoluteErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1489 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1490 | status=GetPeakAbsoluteDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | channel_distortion,exception); |
| 1492 | break; |
| 1493 | } |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1494 | case PeakSignalToNoiseRatioErrorMetric: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1495 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1496 | status=GetPeakSignalToNoiseRatio(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1497 | channel_distortion,exception); |
| 1498 | break; |
| 1499 | } |
cristy | 03d6f86 | 2014-01-08 18:34:48 +0000 | [diff] [blame] | 1500 | case PerceptualHashErrorMetric: |
| 1501 | { |
| 1502 | status=GetRootMeanSquaredDistortion(image,reconstruct_image, |
| 1503 | channel_distortion,exception); |
| 1504 | break; |
| 1505 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1506 | case RootMeanSquaredErrorMetric: |
| 1507 | { |
cristy | 8a9106f | 2011-07-05 14:39:26 +0000 | [diff] [blame] | 1508 | status=GetRootMeanSquaredDistortion(image,reconstruct_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1509 | channel_distortion,exception); |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1513 | if (status == MagickFalse) |
| 1514 | { |
| 1515 | channel_distortion=(double *) RelinquishMagickMemory(channel_distortion); |
| 1516 | return((double *) NULL); |
| 1517 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1518 | return(channel_distortion); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1523 | % % |
| 1524 | % % |
| 1525 | % % |
| 1526 | % I s I m a g e s E q u a l % |
| 1527 | % % |
| 1528 | % % |
| 1529 | % % |
| 1530 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1531 | % |
| 1532 | % IsImagesEqual() measures the difference between colors at each pixel |
| 1533 | % location of two images. A value other than 0 means the colors match |
| 1534 | % exactly. Otherwise an error measure is computed by summing over all |
| 1535 | % pixels in an image the distance squared in RGB space between each image |
| 1536 | % pixel and its corresponding pixel in the reconstruct image. The error |
| 1537 | % measure is assigned to these image members: |
| 1538 | % |
| 1539 | % o mean_error_per_pixel: The mean error for any single pixel in |
| 1540 | % the image. |
| 1541 | % |
| 1542 | % o normalized_mean_error: The normalized mean quantization error for |
| 1543 | % any single pixel in the image. This distance measure is normalized to |
| 1544 | % a range between 0 and 1. It is independent of the range of red, green, |
| 1545 | % and blue values in the image. |
| 1546 | % |
| 1547 | % o normalized_maximum_error: The normalized maximum quantization |
| 1548 | % error for any single pixel in the image. This distance measure is |
| 1549 | % normalized to a range between 0 and 1. It is independent of the range |
| 1550 | % of red, green, and blue values in your image. |
| 1551 | % |
| 1552 | % A small normalized mean square error, accessed as |
| 1553 | % image->normalized_mean_error, suggests the images are very similar in |
| 1554 | % spatial layout and color. |
| 1555 | % |
| 1556 | % The format of the IsImagesEqual method is: |
| 1557 | % |
| 1558 | % MagickBooleanType IsImagesEqual(Image *image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1559 | % const Image *reconstruct_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1560 | % |
| 1561 | % A description of each parameter follows. |
| 1562 | % |
| 1563 | % o image: the image. |
| 1564 | % |
| 1565 | % o reconstruct_image: the reconstruct image. |
| 1566 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1567 | % o exception: return any errors or warnings in this structure. |
| 1568 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1569 | */ |
| 1570 | MagickExport MagickBooleanType IsImagesEqual(Image *image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1571 | const Image *reconstruct_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1572 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1573 | CacheView |
| 1574 | *image_view, |
| 1575 | *reconstruct_view; |
| 1576 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1577 | MagickBooleanType |
| 1578 | status; |
| 1579 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1580 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1581 | area, |
| 1582 | maximum_error, |
| 1583 | mean_error, |
| 1584 | mean_error_per_pixel; |
| 1585 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1586 | ssize_t |
| 1587 | y; |
| 1588 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | assert(image != (Image *) NULL); |
| 1590 | assert(image->signature == MagickSignature); |
| 1591 | assert(reconstruct_image != (const Image *) NULL); |
| 1592 | assert(reconstruct_image->signature == MagickSignature); |
| 1593 | if ((reconstruct_image->columns != image->columns) || |
| 1594 | (reconstruct_image->rows != image->rows)) |
| 1595 | ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename); |
| 1596 | area=0.0; |
| 1597 | maximum_error=0.0; |
| 1598 | mean_error_per_pixel=0.0; |
| 1599 | mean_error=0.0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1600 | image_view=AcquireVirtualCacheView(image,exception); |
| 1601 | reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1602 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1603 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1604 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1605 | *restrict p, |
| 1606 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1607 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1608 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1609 | x; |
| 1610 | |
| 1611 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 1612 | q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns, |
| 1613 | 1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1614 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1616 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1617 | { |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1618 | register ssize_t |
| 1619 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1620 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1621 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1622 | { |
| 1623 | p+=GetPixelChannels(image); |
| 1624 | q+=GetPixelChannels(reconstruct_image); |
| 1625 | continue; |
| 1626 | } |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1627 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1628 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1629 | double |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1630 | distance; |
| 1631 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1632 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1633 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 1634 | PixelTrait reconstruct_traits=GetPixelChannelTraits(reconstruct_image, |
| 1635 | channel); |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1636 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1637 | (reconstruct_traits == UndefinedPixelTrait) || |
| 1638 | ((reconstruct_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1639 | continue; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1640 | distance=fabs(p[i]-(double) GetPixelChannel(reconstruct_image, |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 1641 | channel,q)); |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1642 | mean_error_per_pixel+=distance; |
| 1643 | mean_error+=distance*distance; |
| 1644 | if (distance > maximum_error) |
| 1645 | maximum_error=distance; |
| 1646 | area++; |
| 1647 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1648 | p+=GetPixelChannels(image); |
| 1649 | q+=GetPixelChannels(reconstruct_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | reconstruct_view=DestroyCacheView(reconstruct_view); |
| 1653 | image_view=DestroyCacheView(image_view); |
| 1654 | image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area); |
| 1655 | image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale* |
| 1656 | mean_error/area); |
| 1657 | image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error); |
| 1658 | status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse; |
| 1659 | return(status); |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1664 | % % |
| 1665 | % % |
| 1666 | % % |
| 1667 | % S i m i l a r i t y I m a g e % |
| 1668 | % % |
| 1669 | % % |
| 1670 | % % |
| 1671 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1672 | % |
| 1673 | % SimilarityImage() compares the reference image of the image and returns the |
| 1674 | % best match offset. In addition, it returns a similarity image such that an |
| 1675 | % exact match location is completely white and if none of the pixels match, |
| 1676 | % black, otherwise some gray level in-between. |
| 1677 | % |
| 1678 | % The format of the SimilarityImageImage method is: |
| 1679 | % |
| 1680 | % Image *SimilarityImage(const Image *image,const Image *reference, |
cristy | 62e5218 | 2013-03-15 14:26:17 +0000 | [diff] [blame] | 1681 | % const MetricType metric,const double similarity_threshold, |
| 1682 | % RectangleInfo *offset,double *similarity,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1683 | % |
| 1684 | % A description of each parameter follows: |
| 1685 | % |
| 1686 | % o image: the image. |
| 1687 | % |
| 1688 | % o reference: find an area of the image that closely resembles this image. |
| 1689 | % |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1690 | % o metric: the metric. |
| 1691 | % |
cristy | 62e5218 | 2013-03-15 14:26:17 +0000 | [diff] [blame] | 1692 | % o similarity_threshold: minimum distortion for (sub)image match. |
| 1693 | % |
| 1694 | % o offset: the best match offset of the reference image within the image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1695 | % |
| 1696 | % o similarity: the computed similarity between the images. |
| 1697 | % |
| 1698 | % o exception: return any errors or warnings in this structure. |
| 1699 | % |
| 1700 | */ |
| 1701 | |
| 1702 | static double GetSimilarityMetric(const Image *image,const Image *reference, |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1703 | const MetricType metric,const ssize_t x_offset,const ssize_t y_offset, |
| 1704 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1705 | { |
| 1706 | double |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1707 | distortion; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1708 | |
cristy | 713ff21 | 2010-11-26 21:56:11 +0000 | [diff] [blame] | 1709 | Image |
| 1710 | *similarity_image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1711 | |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1712 | MagickBooleanType |
| 1713 | status; |
| 1714 | |
cristy | 713ff21 | 2010-11-26 21:56:11 +0000 | [diff] [blame] | 1715 | RectangleInfo |
| 1716 | geometry; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1717 | |
cristy | 713ff21 | 2010-11-26 21:56:11 +0000 | [diff] [blame] | 1718 | SetGeometry(reference,&geometry); |
| 1719 | geometry.x=x_offset; |
| 1720 | geometry.y=y_offset; |
| 1721 | similarity_image=CropImage(image,&geometry,exception); |
| 1722 | if (similarity_image == (Image *) NULL) |
| 1723 | return(0.0); |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1724 | distortion=0.0; |
| 1725 | status=GetImageDistortion(similarity_image,reference,metric,&distortion, |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1726 | exception); |
cristy | 713ff21 | 2010-11-26 21:56:11 +0000 | [diff] [blame] | 1727 | similarity_image=DestroyImage(similarity_image); |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1728 | if (status == MagickFalse) |
| 1729 | return(0.0); |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1730 | return(distortion); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | MagickExport Image *SimilarityImage(Image *image,const Image *reference, |
cristy | 62e5218 | 2013-03-15 14:26:17 +0000 | [diff] [blame] | 1734 | const MetricType metric,const double similarity_threshold, |
| 1735 | RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1736 | { |
| 1737 | #define SimilarityImageTag "Similarity/Image" |
| 1738 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1739 | CacheView |
| 1740 | *similarity_view; |
| 1741 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1742 | Image |
| 1743 | *similarity_image; |
| 1744 | |
| 1745 | MagickBooleanType |
| 1746 | status; |
| 1747 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1748 | MagickOffsetType |
| 1749 | progress; |
| 1750 | |
| 1751 | ssize_t |
| 1752 | y; |
| 1753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1754 | assert(image != (const Image *) NULL); |
| 1755 | assert(image->signature == MagickSignature); |
| 1756 | if (image->debug != MagickFalse) |
| 1757 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1758 | assert(exception != (ExceptionInfo *) NULL); |
| 1759 | assert(exception->signature == MagickSignature); |
| 1760 | assert(offset != (RectangleInfo *) NULL); |
| 1761 | SetGeometry(reference,offset); |
| 1762 | *similarity_metric=1.0; |
cristy | f9255b9 | 2010-08-14 22:59:00 +0000 | [diff] [blame] | 1763 | if ((reference->columns > image->columns) || (reference->rows > image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1764 | ThrowImageException(ImageError,"ImageSizeDiffers"); |
| 1765 | similarity_image=CloneImage(image,image->columns-reference->columns+1, |
| 1766 | image->rows-reference->rows+1,MagickTrue,exception); |
| 1767 | if (similarity_image == (Image *) NULL) |
| 1768 | return((Image *) NULL); |
cristy | d5c15f9 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 1769 | status=SetImageStorageClass(similarity_image,DirectClass,exception); |
| 1770 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1771 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1772 | similarity_image=DestroyImage(similarity_image); |
| 1773 | return((Image *) NULL); |
| 1774 | } |
cristy | b979cea | 2013-03-01 14:22:42 +0000 | [diff] [blame] | 1775 | (void) SetImageAlphaChannel(similarity_image,DeactivateAlphaChannel, |
| 1776 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1777 | /* |
| 1778 | Measure similarity of reference image against image. |
| 1779 | */ |
| 1780 | status=MagickTrue; |
| 1781 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1782 | similarity_view=AcquireAuthenticCacheView(similarity_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1783 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 70ea54f | 2013-03-15 18:49:14 +0000 | [diff] [blame] | 1784 | #pragma omp parallel for schedule(static,4) \ |
| 1785 | shared(progress,status,similarity_metric) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1786 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1787 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1788 | for (y=0; y < (ssize_t) (image->rows-reference->rows+1); y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1789 | { |
| 1790 | double |
| 1791 | similarity; |
| 1792 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1793 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1794 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1795 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1796 | register ssize_t |
| 1797 | x; |
| 1798 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1799 | if (status == MagickFalse) |
| 1800 | continue; |
cristy | 266f58b | 2013-05-15 11:47:01 +0000 | [diff] [blame] | 1801 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 1aadacc | 2013-04-27 16:31:24 +0000 | [diff] [blame] | 1802 | #pragma omp flush(similarity_metric) |
cristy | 266f58b | 2013-05-15 11:47:01 +0000 | [diff] [blame] | 1803 | #endif |
cristy | 24856ba | 2013-03-15 18:24:00 +0000 | [diff] [blame] | 1804 | if (*similarity_metric <= similarity_threshold) |
| 1805 | continue; |
cristy | 3cc758f | 2010-11-27 01:33:49 +0000 | [diff] [blame] | 1806 | q=GetCacheViewAuthenticPixels(similarity_view,0,y,similarity_image->columns, |
| 1807 | 1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1808 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1809 | { |
| 1810 | status=MagickFalse; |
| 1811 | continue; |
| 1812 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1813 | for (x=0; x < (ssize_t) (image->columns-reference->columns+1); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1814 | { |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1815 | register ssize_t |
| 1816 | i; |
| 1817 | |
cristy | 266f58b | 2013-05-15 11:47:01 +0000 | [diff] [blame] | 1818 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 1aadacc | 2013-04-27 16:31:24 +0000 | [diff] [blame] | 1819 | #pragma omp flush(similarity_metric) |
cristy | 266f58b | 2013-05-15 11:47:01 +0000 | [diff] [blame] | 1820 | #endif |
cristy | 24856ba | 2013-03-15 18:24:00 +0000 | [diff] [blame] | 1821 | if (*similarity_metric <= similarity_threshold) |
| 1822 | break; |
cristy | 0913681 | 2011-10-18 15:24:30 +0000 | [diff] [blame] | 1823 | similarity=GetSimilarityMetric(image,reference,metric,x,y,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1824 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1825 | #pragma omp critical (MagickCore_SimilarityImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1826 | #endif |
| 1827 | if (similarity < *similarity_metric) |
| 1828 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1829 | offset->x=x; |
| 1830 | offset->y=y; |
cristy | 24856ba | 2013-03-15 18:24:00 +0000 | [diff] [blame] | 1831 | *similarity_metric=similarity; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1832 | } |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1833 | if (GetPixelReadMask(similarity_image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1834 | { |
cristy | c3a5802 | 2013-10-09 23:22:42 +0000 | [diff] [blame] | 1835 | SetPixelBackgoundColor(similarity_image,q); |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 1836 | q+=GetPixelChannels(similarity_image); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1837 | continue; |
| 1838 | } |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 1839 | for (i=0; i < (ssize_t) GetPixelChannels(similarity_image); i++) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1840 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1841 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1842 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 1843 | PixelTrait similarity_traits=GetPixelChannelTraits(similarity_image, |
| 1844 | channel); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1845 | if ((traits == UndefinedPixelTrait) || |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1846 | (similarity_traits == UndefinedPixelTrait) || |
| 1847 | ((similarity_traits & UpdatePixelTrait) == 0)) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1848 | continue; |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 1849 | SetPixelChannel(similarity_image,channel,ClampToQuantum(QuantumRange- |
| 1850 | QuantumRange*similarity),q); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1851 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1852 | q+=GetPixelChannels(similarity_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1853 | } |
cristy | b979cea | 2013-03-01 14:22:42 +0000 | [diff] [blame] | 1854 | if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1855 | status=MagickFalse; |
| 1856 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1857 | { |
cristy | b979cea | 2013-03-01 14:22:42 +0000 | [diff] [blame] | 1858 | MagickBooleanType |
| 1859 | proceed; |
| 1860 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1861 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1862 | #pragma omp critical (MagickCore_SimilarityImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1863 | #endif |
cristy | b979cea | 2013-03-01 14:22:42 +0000 | [diff] [blame] | 1864 | proceed=SetImageProgress(image,SimilarityImageTag,progress++, |
| 1865 | image->rows); |
| 1866 | if (proceed == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1867 | status=MagickFalse; |
| 1868 | } |
| 1869 | } |
| 1870 | similarity_view=DestroyCacheView(similarity_view); |
cristy | 1c2f48d | 2012-12-14 01:20:55 +0000 | [diff] [blame] | 1871 | if (status == MagickFalse) |
| 1872 | similarity_image=DestroyImage(similarity_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1873 | return(similarity_image); |
| 1874 | } |