PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/ImageMagick-6.6.2-6/filters/analyze.c

https://bitbucket.org/JasonGross/alphabets-with-data
C | 236 lines | 158 code | 8 blank | 70 comment | 21 complexity | c2f6811771d0e81be7d06bdac508f010 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, Apache-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % AAA N N AAA L Y Y ZZZZZ EEEEE %
  6. % A A NN N A A L Y Y ZZ E %
  7. % AAAAA N N N AAAAA L Y ZZZ EEE %
  8. % A A N NN A A L Y ZZ E %
  9. % A A N N A A LLLLL Y ZZZZZ EEEEE %
  10. % %
  11. % Analyze An Image %
  12. % %
  13. % Software Design %
  14. % Bill Corbis %
  15. % December 1998 %
  16. % %
  17. % %
  18. % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
  19. % dedicated to making software imaging solutions freely available. %
  20. % %
  21. % You may not use this file except in compliance with the License. You may %
  22. % obtain a copy of the License at %
  23. % %
  24. % http://www.imagemagick.org/script/license.php %
  25. % %
  26. % Unless required by applicable law or agreed to in writing, software %
  27. % distributed under the License is distributed on an "AS IS" BASIS, %
  28. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  29. % See the License for the specific language governing permissions and %
  30. % limitations under the License. %
  31. % %
  32. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  33. %
  34. */
  35. /*
  36. Include declarations.
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <assert.h>
  43. #include <math.h>
  44. #include "magick/MagickCore.h"
  45. /*
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. % %
  48. % %
  49. % %
  50. % a n a l y z e I m a g e %
  51. % %
  52. % %
  53. % %
  54. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55. %
  56. % analyzeImage() computes the brightness and saturation mean, standard
  57. % deviation, kurtosis and skewness and stores these values as attributes
  58. % of the image.
  59. %
  60. % The format of the analyzeImage method is:
  61. %
  62. % size_t analyzeImage(Image *images,const int argc,
  63. % char **argv,ExceptionInfo *exception)
  64. %
  65. % A description of each parameter follows:
  66. %
  67. % o image: the address of a structure of type Image.
  68. %
  69. % o argc: Specifies a pointer to an integer describing the number of
  70. % elements in the argument vector.
  71. %
  72. % o argv: Specifies a pointer to a text array containing the command line
  73. % arguments.
  74. %
  75. % o exception: return any errors or warnings in this structure.
  76. %
  77. */
  78. ModuleExport size_t analyzeImage(Image **images,const int argc,
  79. const char **argv,ExceptionInfo *exception)
  80. {
  81. char
  82. text[MaxTextExtent];
  83. double
  84. area,
  85. brightness,
  86. brightness_mean,
  87. brightness_standard_deviation,
  88. brightness_kurtosis,
  89. brightness_skewness,
  90. brightness_sum_x,
  91. brightness_sum_x2,
  92. brightness_sum_x3,
  93. brightness_sum_x4,
  94. hue,
  95. saturation,
  96. saturation_mean,
  97. saturation_standard_deviation,
  98. saturation_kurtosis,
  99. saturation_skewness,
  100. saturation_sum_x,
  101. saturation_sum_x2,
  102. saturation_sum_x3,
  103. saturation_sum_x4;
  104. Image
  105. *image;
  106. assert(images != (Image **) NULL);
  107. assert(*images != (Image *) NULL);
  108. assert((*images)->signature == MagickSignature);
  109. (void) argc;
  110. (void) argv;
  111. image=(*images);
  112. for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
  113. {
  114. CacheView
  115. *image_view;
  116. ssize_t
  117. y;
  118. MagickBooleanType
  119. status;
  120. brightness_sum_x=0.0;
  121. brightness_sum_x2=0.0;
  122. brightness_sum_x3=0.0;
  123. brightness_sum_x4=0.0;
  124. brightness_mean=0.0;
  125. brightness_standard_deviation=0.0;
  126. brightness_kurtosis=0.0;
  127. brightness_skewness=0.0;
  128. saturation_sum_x=0.0;
  129. saturation_sum_x2=0.0;
  130. saturation_sum_x3=0.0;
  131. saturation_sum_x4=0.0;
  132. saturation_mean=0.0;
  133. saturation_standard_deviation=0.0;
  134. saturation_kurtosis=0.0;
  135. saturation_skewness=0.0;
  136. area=0.0;
  137. status=MagickTrue;
  138. image_view=AcquireCacheView(image);
  139. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  140. #pragma omp parallel for schedule(dynamic,4) shared(status)
  141. #endif
  142. for (y=0; y < (ssize_t) image->rows; y++)
  143. {
  144. register const PixelPacket
  145. *p;
  146. register ssize_t
  147. x;
  148. if (status == MagickFalse)
  149. continue;
  150. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  151. if (p == (const PixelPacket *) NULL)
  152. {
  153. status=MagickFalse;
  154. continue;
  155. }
  156. for (x=0; x < (ssize_t) image->columns; x++)
  157. {
  158. ConvertRGBToHSB(p->red,p->green,p->blue,&hue,&saturation,&brightness);
  159. brightness*=QuantumRange;
  160. brightness_sum_x+=brightness;
  161. brightness_sum_x2+=brightness*brightness;
  162. brightness_sum_x3+=brightness*brightness*brightness;
  163. brightness_sum_x4+=brightness*brightness*brightness*brightness;
  164. saturation*=QuantumRange;
  165. saturation_sum_x+=saturation;
  166. saturation_sum_x2+=saturation*saturation;
  167. saturation_sum_x3+=saturation*saturation*saturation;
  168. saturation_sum_x4+=saturation*saturation*saturation*saturation;
  169. area++;
  170. p++;
  171. }
  172. }
  173. image_view=DestroyCacheView(image_view);
  174. if (area <= 0.0)
  175. break;
  176. brightness_mean=brightness_sum_x/area;
  177. (void) FormatMagickString(text,MaxTextExtent,"%g",brightness_mean);
  178. (void) SetImageProperty(image,"filter:brightness:mean",text);
  179. brightness_standard_deviation=sqrt(brightness_sum_x2/area-(brightness_sum_x/
  180. area*brightness_sum_x/area));
  181. (void) FormatMagickString(text,MaxTextExtent,"%g",
  182. brightness_standard_deviation);
  183. (void) SetImageProperty(image,"filter:brightness:standard-deviation",text);
  184. if (brightness_standard_deviation != 0)
  185. brightness_kurtosis=(brightness_sum_x4/area-4.0*brightness_mean*
  186. brightness_sum_x3/area+6.0*brightness_mean*brightness_mean*
  187. brightness_sum_x2/area-3.0*brightness_mean*brightness_mean*
  188. brightness_mean*brightness_mean)/(brightness_standard_deviation*
  189. brightness_standard_deviation*brightness_standard_deviation*
  190. brightness_standard_deviation)-3.0;
  191. (void) FormatMagickString(text,MaxTextExtent,"%g",brightness_kurtosis);
  192. (void) SetImageProperty(image,"filter:brightness:kurtosis",text);
  193. if (brightness_standard_deviation != 0)
  194. brightness_skewness=(brightness_sum_x3/area-3.0*brightness_mean*
  195. brightness_sum_x2/area+2.0*brightness_mean*brightness_mean*
  196. brightness_mean)/(brightness_standard_deviation*
  197. brightness_standard_deviation*brightness_standard_deviation);
  198. (void) FormatMagickString(text,MaxTextExtent,"%g",brightness_skewness);
  199. (void) SetImageProperty(image,"filter:brightness:skewness",text);
  200. saturation_mean=saturation_sum_x/area;
  201. (void) FormatMagickString(text,MaxTextExtent,"%g",saturation_mean);
  202. (void) SetImageProperty(image,"filter:saturation:mean",text);
  203. saturation_standard_deviation=sqrt(saturation_sum_x2/area-(saturation_sum_x/
  204. area*saturation_sum_x/area));
  205. (void) FormatMagickString(text,MaxTextExtent,"%g",
  206. saturation_standard_deviation);
  207. (void) SetImageProperty(image,"filter:saturation:standard-deviation",text);
  208. if (saturation_standard_deviation != 0)
  209. saturation_kurtosis=(saturation_sum_x4/area-4.0*saturation_mean*
  210. saturation_sum_x3/area+6.0*saturation_mean*saturation_mean*
  211. saturation_sum_x2/area-3.0*saturation_mean*saturation_mean*
  212. saturation_mean*saturation_mean)/(saturation_standard_deviation*
  213. saturation_standard_deviation*saturation_standard_deviation*
  214. saturation_standard_deviation)-3.0;
  215. (void) FormatMagickString(text,MaxTextExtent,"%g",saturation_kurtosis);
  216. (void) SetImageProperty(image,"filter:saturation:kurtosis",text);
  217. if (saturation_standard_deviation != 0)
  218. saturation_skewness=(saturation_sum_x3/area-3.0*saturation_mean*
  219. saturation_sum_x2/area+2.0*saturation_mean*saturation_mean*
  220. saturation_mean)/(saturation_standard_deviation*
  221. saturation_standard_deviation*saturation_standard_deviation);
  222. (void) FormatMagickString(text,MaxTextExtent,"%g",saturation_skewness);
  223. (void) SetImageProperty(image,"filter:saturation:skewness",text);
  224. }
  225. return(MagickImageFilterSignature);
  226. }