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