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