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