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