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