2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % Write A Histogram Image. %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
69 static MagickBooleanType
70 WriteHISTOGRAMImage(const ImageInfo *,Image *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e g i s t e r H I S T O G R A M I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
90 % The format of the RegisterHISTOGRAMImage method is:
92 % unsigned long RegisterHISTOGRAMImage(void)
95 ModuleExport unsigned long RegisterHISTOGRAMImage(void)
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);
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % UnregisterHISTOGRAMImage() removes format registrations made by the
122 % HISTOGRAM module from the list of supported formats.
124 % The format of the UnregisterHISTOGRAMImage method is:
126 % UnregisterHISTOGRAMImage(void)
129 ModuleExport void UnregisterHISTOGRAMImage(void)
131 (void) UnregisterMagickInfo("HISTOGRAM");
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % W r i t e H I S T O G R A M I m a g e %
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
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.
155 % The format of the WriteHISTOGRAMImage method is:
157 % MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
160 % A description of each parameter follows.
162 % o image_info: the image info.
164 % o image: The image.
168 static inline size_t MagickMax(const size_t x,const size_t y)
175 static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
178 #define HistogramDensity "256x200"
184 filename[MaxTextExtent];
217 register const PixelPacket
231 Allocate histogram image.
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);
244 (void) ParseAbsoluteGeometry(image_info->density,&geometry);
245 histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
247 if (histogram_image == (Image *) NULL)
248 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
249 (void) SetImageStorageClass(histogram_image,DirectClass);
251 Allocate histogram count arrays.
253 length=MagickMax((size_t) ScaleQuantumToChar((Quantum) QuantumRange)+1UL,
254 histogram_image->columns);
255 histogram=(MagickPixelPacket *) AcquireQuantumMemory(length,
257 if (histogram == (MagickPixelPacket *) NULL)
259 histogram_image=DestroyImage(histogram_image);
260 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
263 Initialize histogram count arrays.
265 channel=image_info->channel;
266 (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram));
267 for (y=0; y < (long) image->rows; y++)
269 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
270 if (p == (const PixelPacket *) NULL)
272 for (x=0; x < (long) image->columns; x++)
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++;
283 maximum=histogram[0].red;
284 for (x=0; x < (long) histogram_image->columns; x++)
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;
293 scale=(MagickRealType) histogram_image->rows/maximum;
295 Initialize histogram image.
297 exception=(&image->exception);
298 (void) QueryColorDatabase("#000000",&histogram_image->background_color,
300 (void) SetImageBackgroundColor(histogram_image);
301 for (x=0; x < (long) histogram_image->columns; x++)
303 q=GetAuthenticPixels(histogram_image,x,0,1,histogram_image->rows,exception);
304 if (q == (PixelPacket *) NULL)
306 if ((channel & RedChannel) != 0)
308 y=(long) (histogram_image->rows-scale*histogram[x].red+0.5);
310 for ( ; y < (long) histogram_image->rows; y++)
312 r->red=(Quantum) QuantumRange;
316 if ((channel & GreenChannel) != 0)
318 y=(long) (histogram_image->rows-scale*histogram[x].green+0.5);
320 for ( ; y < (long) histogram_image->rows; y++)
322 r->green=(Quantum) QuantumRange;
326 if ((channel & BlueChannel) != 0)
328 y=(long) (histogram_image->rows-scale*histogram[x].blue+0.5);
330 for ( ; y < (long) histogram_image->rows; y++)
332 r->blue=(Quantum) QuantumRange;
336 if (SyncAuthenticPixels(histogram_image,exception) == MagickFalse)
338 status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows);
339 if (status == MagickFalse)
343 Relinquish resources.
345 histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram);
347 unique_file=AcquireUniqueFileResource(filename);
348 if (unique_file != -1)
349 file=fdopen(unique_file,"wb");
350 if ((unique_file != -1) && (file != (FILE *) NULL))
356 Add a histogram as an image comment.
358 (void) GetNumberColors(image,file,&image->exception);
360 property=FileToString(filename,~0UL,&image->exception);
361 if (property != (char *) NULL)
363 (void) SetImageProperty(histogram_image,"comment",property);
364 property=DestroyString(property);
367 (void) RelinquishUniqueFileResource(filename);
369 Write Histogram image.
371 (void) CopyMagickString(histogram_image->filename,image_info->filename,
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);