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-2014 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 "MagickCore/studio.h"
43 #include "MagickCore/artifact.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/geometry.h"
53 #include "MagickCore/histogram.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/option.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/property.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/resource_.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/module.h"
68 #include "MagickCore/token.h"
69 #include "MagickCore/utility.h"
74 static MagickBooleanType
75 WriteHISTOGRAMImage(const ImageInfo *,Image *,ExceptionInfo *);
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % R e g i s t e r H I S T O G R A M I m a g e %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % RegisterHISTOGRAMImage() adds attributes for the Histogram image format
89 % to the list of supported formats. The attributes include the image format
90 % tag, a method to read and/or write the format, whether the format
91 % supports the saving of more than one frame to the same file or blob,
92 % whether the format supports native in-memory I/O, and a brief
93 % description of the format.
95 % The format of the RegisterHISTOGRAMImage method is:
97 % size_t RegisterHISTOGRAMImage(void)
100 ModuleExport size_t RegisterHISTOGRAMImage(void)
105 entry=SetMagickInfo("HISTOGRAM");
106 entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage;
107 entry->adjoin=MagickFalse;
108 entry->format_type=ImplicitFormatType;
109 entry->description=ConstantString("Histogram of the image");
110 entry->module=ConstantString("HISTOGRAM");
111 (void) RegisterMagickInfo(entry);
112 return(MagickImageCoderSignature);
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % U n r e g i s t e r H I S T O G R A M I m a g e %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % UnregisterHISTOGRAMImage() removes format registrations made by the
127 % HISTOGRAM module from the list of supported formats.
129 % The format of the UnregisterHISTOGRAMImage method is:
131 % UnregisterHISTOGRAMImage(void)
134 ModuleExport void UnregisterHISTOGRAMImage(void)
136 (void) UnregisterMagickInfo("HISTOGRAM");
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % W r i t e H I S T O G R A M I m a g e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % WriteHISTOGRAMImage() writes an image to a file in Histogram format.
151 % The image shows a histogram of the color (or gray) values in the image. The
152 % image consists of three overlaid histograms: a red one for the red channel,
153 % a green one for the green channel, and a blue one for the blue channel. The
154 % image comment contains a list of unique pixel values and the number of times
155 % each occurs in the image.
157 % This method is strongly based on a similar one written by
158 % muquit@warm.semcor.com which in turn is based on ppmhistmap of netpbm.
160 % The format of the WriteHISTOGRAMImage method is:
162 % MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
163 % Image *image,ExceptionInfo *exception)
165 % A description of each parameter follows.
167 % o image_info: the image info.
169 % o image: The image.
171 % o exception: return any errors or warnings in this structure.
175 static inline size_t MagickMax(const size_t x,const size_t y)
182 static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
183 Image *image,ExceptionInfo *exception)
185 #define HistogramDensity "256x200"
188 filename[MaxTextExtent];
212 register const Quantum
229 Allocate histogram image.
231 assert(image_info != (const ImageInfo *) NULL);
232 assert(image_info->signature == MagickSignature);
233 assert(image != (Image *) NULL);
234 assert(image->signature == MagickSignature);
235 if (image->debug != MagickFalse)
236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
237 image_info->filename);
238 SetGeometry(image,&geometry);
239 if (image_info->density == (char *) NULL)
240 (void) ParseAbsoluteGeometry(HistogramDensity,&geometry);
242 (void) ParseAbsoluteGeometry(image_info->density,&geometry);
243 histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
245 if (histogram_image == (Image *) NULL)
246 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
247 (void) SetImageStorageClass(histogram_image,DirectClass,exception);
249 Allocate histogram count arrays.
251 length=MagickMax((size_t) ScaleQuantumToChar(QuantumRange)+1UL,
252 histogram_image->columns);
253 histogram=(PixelInfo *) AcquireQuantumMemory(length,sizeof(*histogram));
254 if (histogram == (PixelInfo *) NULL)
256 histogram_image=DestroyImage(histogram_image);
257 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
260 Initialize histogram count arrays.
262 (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram));
263 for (y=0; y < (ssize_t) image->rows; y++)
265 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
266 if (p == (const Quantum *) NULL)
268 for (x=0; x < (ssize_t) image->columns; x++)
270 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
271 histogram[ScaleQuantumToChar(GetPixelRed(image,p))].red++;
272 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
273 histogram[ScaleQuantumToChar(GetPixelGreen(image,p))].green++;
274 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
275 histogram[ScaleQuantumToChar(GetPixelBlue(image,p))].blue++;
276 p+=GetPixelChannels(image);
279 maximum=histogram[0].red;
280 for (x=0; x < (ssize_t) histogram_image->columns; x++)
282 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
283 (maximum < histogram[x].red))
284 maximum=histogram[x].red;
285 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
286 (maximum < histogram[x].green))
287 maximum=histogram[x].green;
288 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
289 (maximum < histogram[x].blue))
290 maximum=histogram[x].blue;
292 scale=(double) histogram_image->rows/maximum;
294 Initialize histogram image.
296 (void) QueryColorCompliance("#000000",AllCompliance,
297 &histogram_image->background_color,exception);
298 (void) SetImageBackgroundColor(histogram_image,exception);
299 for (x=0; x < (ssize_t) histogram_image->columns; x++)
301 q=GetAuthenticPixels(histogram_image,x,0,1,histogram_image->rows,exception);
302 if (q == (Quantum *) NULL)
304 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
306 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].red-0.5);
307 r=q+y*GetPixelChannels(histogram_image);
308 for ( ; y < (ssize_t) histogram_image->rows; y++)
310 SetPixelRed(histogram_image,QuantumRange,r);
311 r+=GetPixelChannels(histogram_image);
314 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
316 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].green-0.5);
317 r=q+y*GetPixelChannels(histogram_image);
318 for ( ; y < (ssize_t) histogram_image->rows; y++)
320 SetPixelGreen(histogram_image,QuantumRange,r);
321 r+=GetPixelChannels(histogram_image);
324 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
326 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].blue-0.5);
327 r=q+y*GetPixelChannels(histogram_image);
328 for ( ; y < (ssize_t) histogram_image->rows; y++)
330 SetPixelBlue(histogram_image,QuantumRange,r);
331 r+=GetPixelChannels(histogram_image);
334 if (SyncAuthenticPixels(histogram_image,exception) == MagickFalse)
336 status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows);
337 if (status == MagickFalse)
340 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
341 option=GetImageOption(image_info,"histogram:unique-colors");
342 if ((option == (const char *) NULL) || (IsStringTrue(option) != MagickFalse))
351 Add a unique colors as an image comment.
354 unique_file=AcquireUniqueFileResource(filename);
355 if (unique_file != -1)
356 file=fdopen(unique_file,"wb");
357 if ((unique_file != -1) && (file != (FILE *) NULL))
362 (void) GetNumberColors(image,file,exception);
364 property=FileToString(filename,~0UL,exception);
365 if (property != (char *) NULL)
367 (void) SetImageProperty(histogram_image,"comment",property,
369 property=DestroyString(property);
372 (void) RelinquishUniqueFileResource(filename);
375 Write Histogram image.
377 (void) CopyMagickString(histogram_image->filename,image_info->filename,
379 write_info=CloneImageInfo(image_info);
380 (void) SetImageInfo(write_info,1,exception);
381 if (LocaleCompare(write_info->magick,"HISTOGRAM") == 0)
382 (void) FormatLocaleString(histogram_image->filename,MaxTextExtent,"miff:%s",
383 write_info->filename);
384 status=WriteImage(write_info,histogram_image,exception);
385 histogram_image=DestroyImage(histogram_image);
386 write_info=DestroyImageInfo(write_info);