1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  
     | 
    
      /*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                               A N A L I Z E                                 %
%                                                                             %
%               Methods to Compute a Information about an Image               %
%                                                                             %
%                                                                             %
%                             Software Design                                 %
%                               Bill Corbis                                   %
%                              December 1998                                  %
%                                                                             %
%                                                                             %
%  Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization      %
%  dedicated to making software imaging solutions freely available.           %
%                                                                             %
%  You may not use this file except in compliance with the License.  You may  %
%  obtain a copy of the License at                                            %
%                                                                             %
%    http://www.imagemagick.org/www/Copyright.html                            %
%                                                                             %
%  Unless required by applicable law or agreed to in writing, software        %
%  distributed under the License is distributed on an "AS IS" BASIS,          %
%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
%  See the License for the specific language governing permissions and        %
%  limitations under the License.                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
*/
/*
  Include declarations.
*/
#include "magick/studio.h"
#include "magick/attribute.h"
#include "magick/cache.h"
#include "magick/gem.h"
#include "magick/string_.h"
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   A n a l y z e I m a g e                                                   %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  AnalyzeImage(0 computes the brightness and saturation mean and standard
%  deviation and stores these values as attributes of the image.
%
%  The format of the AnalyzeImage method is:
%
%      MagickBooleanType AnalyzeImage(Image *image)
%
%  A description of each parameter follows:
%
%    o image: The address of a structure of type Image.
%
*/
#define PRECISION "%.0f"
ModuleExport MagickBooleanType AnalyzeImage(Image **image,const int argc,
  char **argv)
{
  double
    bsumX = 0.0,
    bsumX2 = 0.0,
    brightness_mean = 0.0,
    brightness_stdev = 0.0,
    ssumX = 0.0,
    ssumX2 = 0.0,
    saturation_mean = 0.0,
    saturation_stdev = 0.0,
    total_pixels = 0.0;
  double
    brightness,
    hue,
    saturation;
  long
    y;
  register long
    x;
  register PixelPacket
    *p;
  char
    text[MaxTextExtent];
  assert(image != (Image **) NULL);
  assert(*image != (Image *) NULL);
  for (y=0; y < (int) (*image)->rows; y++)
  {
    p=GetImagePixels((*image),0,y,(*image)->columns,1);
    if (p == (PixelPacket *) NULL)
      break;
    if (y == 0)
      {
        (void) FormatMagickString(text,MaxTextExtent,"#%02x%02x%02x",
          p->red,p->green,p->blue);
        (void) SetImageAttribute((*image),"TopLeftColor",text);
      }
    if (y == (long) ((*image)->rows-1))
      {
        (void) FormatMagickString(text,MaxTextExtent,"#%02x%02x%02x",
          p->red,p->green,p->blue);
        (void) SetImageAttribute((*image),"BottomLeftColor",text);
      }
    for (x=0; x < (long) (*image)->columns; x++)
    {
      TransformHSL(p->red,p->green,p->blue,&hue,&saturation,&brightness);
      brightness *= MaxRGB;
            bsumX += brightness;
            bsumX2 += brightness * brightness;
      saturation *= MaxRGB;
            ssumX += saturation;
            ssumX2 += saturation * saturation;
      total_pixels++;
      p++;
    }
    p--; /* backup one pixel to allow us to sample */
    if (y == 0)
      {
        (void) FormatMagickString(text,MaxTextExtent,"#%02x%02x%02x",
          p->red,p->green,p->blue);
        (void) SetImageAttribute((*image),"TopRightColor",text);
      }
    if (y == (long) ((*image)->rows-1))
      {
        (void) FormatMagickString(text,MaxTextExtent,"#%02x%02x%02x",
          p->red,p->green,p->blue);
        (void) SetImageAttribute((*image),"BottomRightColor",text);
      }
  }
  if (total_pixels > 0.0)
    {
      brightness_mean = bsumX/total_pixels;
      (void) FormatMagickString(text,MaxTextExtent,PRECISION,brightness_mean);
      (void) SetImageAttribute((*image),"BrightnessMean",text);
      /*  This formula gives a slightly biased result */
      brightness_stdev =
          sqrt(bsumX2/total_pixels - (bsumX/total_pixels*bsumX/total_pixels));
      (void) FormatMagickString(text,MaxTextExtent,PRECISION,brightness_stdev);
      (void) SetImageAttribute((*image),"BrightnessStddev",text);
      /* Now the correction for bias. */
      /*  stdev = stdev*sqrt((double)total_pixels/(double)(total_pixels-1)); */
      /* Now calculate the standard deviation of the mean */
      /*  brightness_stdevmean = bstdev/sqrt((double)total_pixels); */
      saturation_mean = ssumX/total_pixels;
      (void) FormatMagickString(text,MaxTextExtent,PRECISION,saturation_mean);
      (void) SetImageAttribute((*image),"SaturationMean",text);
      /* This formula gives a slightly biased result */
      saturation_stdev =sqrt(ssumX2/total_pixels -
        (ssumX/total_pixels*ssumX/total_pixels));
      (void) FormatMagickString(text,MaxTextExtent,PRECISION,saturation_stdev);
      (void) SetImageAttribute((*image),"SaturationStddev",text);
      /* Now the correction for bias. */
      /*  stdev = stdev*sqrt((double)total_pixels/(double)(total_pixels-1)); */
      /* Now calculate the standard deviation of the mean */
      /*  saturation_stdevmean = sstdev/sqrt((double)total_pixels); */
    }
  return(MagickTrue);
}
 
     |