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