root / ImageMagick / trunk / coders / histogram.c

Revision 12033, 13.2 kB (checked in by cristy, 7 weeks ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%       H   H  IIIII  SSSSS  TTTTT   OOO    GGGG  RRRR    AAA   M   M         %
7%       H   H    I    SS       T    O   O  G      R   R  A   A  MM MM         %
8%       HHHHH    I     SSS     T    O   O  G  GG  RRRR   AAAAA  M M M         %
9%       H   H    I       SS    T    O   O  G   G  R R    A   A  M   M         %
10%       H   H  IIIII  SSSSS    T     OOO    GGG   R  R   A   A  M   M         %
11%                                                                             %
12%                                                                             %
13%                          Write A Histogram Image.                           %
14%                                                                             %
15%                              Software Design                                %
16%                                John Cristy                                  %
17%                                 July 1992                                   %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization      %
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  Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/property.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/color.h"
47#include "magick/color-private.h"
48#include "magick/constitute.h"
49#include "magick/exception.h"
50#include "magick/exception-private.h"
51#include "magick/geometry.h"
52#include "magick/image-private.h"
53#include "magick/magick.h"
54#include "magick/memory_.h"
55#include "magick/monitor.h"
56#include "magick/monitor-private.h"
57#include "magick/resource_.h"
58#include "magick/quantum-private.h"
59#include "magick/static.h"
60#include "magick/statistic.h"
61#include "magick/string_.h"
62#include "magick/module.h"
63
64/*
65  Forward declarations.
66*/
67static MagickBooleanType
68  WriteHISTOGRAMImage(const ImageInfo *,Image *);
69
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72%                                                                             %
73%                                                                             %
74%                                                                             %
75%   R e g i s t e r H I S T O G R A M I m a g e                               %
76%                                                                             %
77%                                                                             %
78%                                                                             %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81%  RegisterHISTOGRAMImage() adds attributes for the Histogram image format
82%  to the list of supported formats.  The attributes include the image format
83%  tag, a method to read and/or write the format, whether the format
84%  supports the saving of more than one frame to the same file or blob,
85%  whether the format supports native in-memory I/O, and a brief
86%  description of the format.
87%
88%  The format of the RegisterHISTOGRAMImage method is:
89%
90%      unsigned long RegisterHISTOGRAMImage(void)
91%
92*/
93ModuleExport unsigned long RegisterHISTOGRAMImage(void)
94{
95  MagickInfo
96    *entry;
97
98  entry=SetMagickInfo("HISTOGRAM");
99  entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage;
100  entry->adjoin=MagickFalse;
101  entry->format_type=ImplicitFormatType;
102  entry->description=ConstantString("Histogram of the image");
103  entry->module=ConstantString("HISTOGRAM");
104  (void) RegisterMagickInfo(entry);
105  return(MagickImageCoderSignature);
106}
107
108/*
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110%                                                                             %
111%                                                                             %
112%                                                                             %
113%   U n r e g i s t e r H I S T O G R A M I m a g e                           %
114%                                                                             %
115%                                                                             %
116%                                                                             %
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118%
119%  UnregisterHISTOGRAMImage() removes format registrations made by the
120%  HISTOGRAM module from the list of supported formats.
121%
122%  The format of the UnregisterHISTOGRAMImage method is:
123%
124%      UnregisterHISTOGRAMImage(void)
125%
126*/
127ModuleExport void UnregisterHISTOGRAMImage(void)
128{
129  (void) UnregisterMagickInfo("HISTOGRAM");
130}
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134%                                                                             %
135%                                                                             %
136%                                                                             %
137%   W r i t e H I S T O G R A M I m a g e                                     %
138%                                                                             %
139%                                                                             %
140%                                                                             %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143%  WriteHISTOGRAMImage() writes an image to a file in Histogram format.
144%  The image shows a histogram of the color (or gray) values in the image.  The
145%  image consists of three overlaid histograms:  a red one for the red channel,
146%  a green one for the green channel, and a blue one for the blue channel.  The
147%  image comment contains a list of unique pixel values and the number of times
148%  each occurs in the image.
149%
150%  This method is strongly based on a similar one written by
151%  muquit@warm.semcor.com which in turn is based on ppmhistmap of netpbm.
152%
153%  The format of the WriteHISTOGRAMImage method is:
154%
155%      MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
156%        Image *image)
157%
158%  A description of each parameter follows.
159%
160%    o image_info: the image info.
161%
162%    o image:  The image.
163%
164*/
165
166static inline size_t MagickMax(const size_t x,const size_t y)
167{
168  if (x > y)
169    return(x);
170  return(y);
171}
172
173static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
174  Image *image)
175{
176#define HistogramDensity  "256x200"
177
178  ChannelType
179    channel;
180
181  char
182    filename[MaxTextExtent];
183
184  FILE
185    *file;
186
187  Image
188    *histogram_image;
189
190  ImageInfo
191    *write_info;
192
193  int
194    unique_file;
195
196  long
197    y;
198
199  MagickBooleanType
200    status;
201
202  MagickPixelPacket
203    *histogram;
204
205  MagickRealType
206    maximum,
207    scale;
208
209  RectangleInfo
210    geometry;
211
212  register const PixelPacket
213    *p;
214
215  register long
216    x;
217
218  register PixelPacket
219    *q,
220    *r;
221
222  size_t
223    length;
224
225  /*
226    Allocate histogram image.
227  */
228  assert(image_info != (const ImageInfo *) NULL);
229  assert(image_info->signature == MagickSignature);
230  assert(image != (Image *) NULL);
231  assert(image->signature == MagickSignature);
232  if (image->debug != MagickFalse)
233    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
234      image_info->filename);
235  SetGeometry(image,&geometry);
236  if (image_info->density == (char *) NULL)
237    (void) ParseAbsoluteGeometry(HistogramDensity,&geometry);
238  else
239    (void) ParseAbsoluteGeometry(image_info->density,&geometry);
240  histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
241    &image->exception);
242  if (histogram_image == (Image *) NULL)
243    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
244  (void) SetImageStorageClass(histogram_image,DirectClass);
245  /*
246    Allocate histogram count arrays.
247  */
248  length=MagickMax((size_t) ScaleQuantumToChar((Quantum) QuantumRange)+1UL,
249    histogram_image->columns);
250  histogram=(MagickPixelPacket *) AcquireQuantumMemory(length,
251    sizeof(*histogram));
252  if (histogram == (MagickPixelPacket *) NULL)
253    {
254      histogram_image=DestroyImage(histogram_image);
255      ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
256    }
257  /*
258    Initialize histogram count arrays.
259  */
260  channel=image_info->channel;
261  (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram));
262  for (y=0; y < (long) image->rows; y++)
263  {
264    p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception);
265    if (p == (const PixelPacket *) NULL)
266      break;
267    for (x=0; x < (long) image->columns; x++)
268    {
269      if ((channel & RedChannel) != 0)
270        histogram[ScaleQuantumToChar(p->red)].red++;
271      if ((channel & GreenChannel) != 0)
272        histogram[ScaleQuantumToChar(p->green)].green++;
273      if ((channel & BlueChannel) != 0)
274        histogram[ScaleQuantumToChar(p->blue)].blue++;
275      p++;
276    }
277  }
278  maximum=histogram[0].red;
279  for (x=0; x < (long) histogram_image->columns; x++)
280  {
281    if (((channel & RedChannel) != 0) && (maximum < histogram[x].red))
282      maximum=histogram[x].red;
283    if (((channel & GreenChannel) != 0) && (maximum < histogram[x].green))
284      maximum=histogram[x].green;
285    if (((channel & BlueChannel) != 0) && (maximum < histogram[x].blue))
286      maximum=histogram[x].blue;
287  }
288  scale=(MagickRealType) histogram_image->rows/maximum;
289  /*
290    Initialize histogram image.
291  */
292  (void) QueryColorDatabase("#000000",&histogram_image->background_color,
293    &image->exception);
294  (void) SetImageBackgroundColor(histogram_image);
295  for (x=0; x < (long) histogram_image->columns; x++)
296  {
297    q=GetImagePixels(histogram_image,x,0,1,histogram_image->rows);
298    if (q == (PixelPacket *) NULL)
299      break;
300    if ((channel & RedChannel) != 0)
301      {
302        y=(long) (histogram_image->rows-scale*histogram[x].red+0.5);
303        r=q+y;
304        for ( ; y < (long) histogram_image->rows; y++)
305        {
306          r->red=(Quantum) QuantumRange;
307          r++;
308        }
309      }
310    if ((channel & GreenChannel) != 0)
311      {
312        y=(long) (histogram_image->rows-scale*histogram[x].green+0.5);
313        r=q+y;
314        for ( ; y < (long) histogram_image->rows; y++)
315        {
316          r->green=(Quantum) QuantumRange;
317          r++;
318        }
319      }
320    if ((channel & BlueChannel) != 0)
321      {
322        y=(long) (histogram_image->rows-scale*histogram[x].blue+0.5);
323        r=q+y;
324        for ( ; y < (long) histogram_image->rows; y++)
325        {
326          r->blue=(Quantum) QuantumRange;
327          r++;
328        }
329      }
330    if (SyncImagePixels(histogram_image) == MagickFalse)
331      break;
332    status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows);
333    if (status == MagickFalse)
334      break;
335  }
336  /*
337    Relinquish resources.
338  */
339  histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram);
340  file=(FILE *) NULL;
341  unique_file=AcquireUniqueFileResource(filename);
342  if (unique_file != -1)
343    file=fdopen(unique_file,"wb");
344  if ((unique_file != -1) && (file != (FILE *) NULL))
345    {
346      char
347        *property;
348
349      /*
350        Add a histogram as an image comment.
351      */
352      (void) GetNumberColors(image,file,&image->exception);
353      (void) fclose(file);
354      property=FileToString(filename,~0UL,&image->exception);
355      if (property != (char *) NULL)
356        {
357          (void) SetImageProperty(histogram_image,"comment",property);
358          property=DestroyString(property);
359        }
360    }
361  (void) RelinquishUniqueFileResource(filename);
362  /*
363    Write Histogram image.
364  */
365  (void) CopyMagickString(histogram_image->filename,image_info->filename,
366    MaxTextExtent);
367  write_info=CloneImageInfo(image_info);
368  (void) SetImageInfo(write_info,MagickTrue,&image->exception);
369  if (LocaleCompare(write_info->magick,"HISTOGRAM") == 0)
370    (void) FormatMagickString(histogram_image->filename,MaxTextExtent,
371      "miff:%s",write_info->filename);
372  status=WriteImage(write_info,histogram_image);
373  histogram_image=DestroyImage(histogram_image);
374  write_info=DestroyImageInfo(write_info);
375  return(status);
376}
Note: See TracBrowser for help on using the browser.