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