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-2017 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 % https://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=AcquireMagickInfo("HISTOGRAM","HISTOGRAM","Histogram of the image");
106 entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage;
107 entry->flags^=CoderAdjoinFlag;
108 entry->format_type=ImplicitFormatType;
109 (void) RegisterMagickInfo(entry);
110 return(MagickImageCoderSignature);
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % U n r e g i s t e r H I S T O G R A M I m a g e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % UnregisterHISTOGRAMImage() removes format registrations made by the
125 % HISTOGRAM module from the list of supported formats.
127 % The format of the UnregisterHISTOGRAMImage method is:
129 % UnregisterHISTOGRAMImage(void)
132 ModuleExport void UnregisterHISTOGRAMImage(void)
134 (void) UnregisterMagickInfo("HISTOGRAM");
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % W r i t e H I S T O G R A M I m a g e %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % WriteHISTOGRAMImage() writes an image to a file in Histogram format.
149 % The image shows a histogram of the color (or gray) values in the image. The
150 % image consists of three overlaid histograms: a red one for the red channel,
151 % a green one for the green channel, and a blue one for the blue channel. The
152 % image comment contains a list of unique pixel values and the number of times
153 % each occurs in the image.
155 % This method is strongly based on a similar one written by
156 % muquit@warm.semcor.com which in turn is based on ppmhistmap of netpbm.
158 % The format of the WriteHISTOGRAMImage method is:
160 % MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
161 % Image *image,ExceptionInfo *exception)
163 % A description of each parameter follows.
165 % o image_info: the image info.
167 % o image: The image.
169 % o exception: return any errors or warnings in this structure.
172 static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
173 Image *image,ExceptionInfo *exception)
175 #define HistogramDensity "256x200"
178 filename[MagickPathExtent];
202 register const Quantum
219 Allocate histogram image.
221 assert(image_info != (const ImageInfo *) NULL);
222 assert(image_info->signature == MagickCoreSignature);
223 assert(image != (Image *) NULL);
224 assert(image->signature == MagickCoreSignature);
225 if (image->debug != MagickFalse)
226 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
227 image_info->filename);
228 SetGeometry(image,&geometry);
229 if (image_info->density == (char *) NULL)
230 (void) ParseAbsoluteGeometry(HistogramDensity,&geometry);
232 (void) ParseAbsoluteGeometry(image_info->density,&geometry);
233 histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
235 if (histogram_image == (Image *) NULL)
236 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
237 (void) SetImageStorageClass(histogram_image,DirectClass,exception);
239 Allocate histogram count arrays.
241 length=MagickMax((size_t) ScaleQuantumToChar(QuantumRange)+1UL,
242 histogram_image->columns);
243 histogram=(PixelInfo *) AcquireQuantumMemory(length,sizeof(*histogram));
244 if (histogram == (PixelInfo *) NULL)
246 histogram_image=DestroyImage(histogram_image);
247 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
250 Initialize histogram count arrays.
252 (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram));
253 for (y=0; y < (ssize_t) image->rows; y++)
255 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
256 if (p == (const Quantum *) NULL)
258 for (x=0; x < (ssize_t) image->columns; x++)
260 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
261 histogram[ScaleQuantumToChar(GetPixelRed(image,p))].red++;
262 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
263 histogram[ScaleQuantumToChar(GetPixelGreen(image,p))].green++;
264 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
265 histogram[ScaleQuantumToChar(GetPixelBlue(image,p))].blue++;
266 p+=GetPixelChannels(image);
269 maximum=histogram[0].red;
270 for (x=0; x < (ssize_t) histogram_image->columns; x++)
272 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
273 (maximum < histogram[x].red))
274 maximum=histogram[x].red;
275 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
276 (maximum < histogram[x].green))
277 maximum=histogram[x].green;
278 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
279 (maximum < histogram[x].blue))
280 maximum=histogram[x].blue;
283 if (fabs(maximum) >= MagickEpsilon)
284 scale=(double) histogram_image->rows/maximum;
286 Initialize histogram image.
288 (void) QueryColorCompliance("#000000",AllCompliance,
289 &histogram_image->background_color,exception);
290 (void) SetImageBackgroundColor(histogram_image,exception);
291 for (x=0; x < (ssize_t) histogram_image->columns; x++)
293 q=GetAuthenticPixels(histogram_image,x,0,1,histogram_image->rows,exception);
294 if (q == (Quantum *) NULL)
296 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
298 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].red-0.5);
299 r=q+y*GetPixelChannels(histogram_image);
300 for ( ; y < (ssize_t) histogram_image->rows; y++)
302 SetPixelRed(histogram_image,QuantumRange,r);
303 r+=GetPixelChannels(histogram_image);
306 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
308 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].green-0.5);
309 r=q+y*GetPixelChannels(histogram_image);
310 for ( ; y < (ssize_t) histogram_image->rows; y++)
312 SetPixelGreen(histogram_image,QuantumRange,r);
313 r+=GetPixelChannels(histogram_image);
316 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
318 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].blue-0.5);
319 r=q+y*GetPixelChannels(histogram_image);
320 for ( ; y < (ssize_t) histogram_image->rows; y++)
322 SetPixelBlue(histogram_image,QuantumRange,r);
323 r+=GetPixelChannels(histogram_image);
326 if (SyncAuthenticPixels(histogram_image,exception) == MagickFalse)
328 status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows);
329 if (status == MagickFalse)
332 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
333 option=GetImageOption(image_info,"histogram:unique-colors");
334 if ((IsHistogramImage(image,exception) != MagickFalse) ||
335 (IsStringTrue(option) != MagickFalse) ||
336 (GetImageOption(image_info,"format") != (const char *) NULL))
345 Add a unique colors as an image comment.
348 unique_file=AcquireUniqueFileResource(filename);
349 if (unique_file != -1)
350 file=fdopen(unique_file,"wb");
351 if ((unique_file != -1) && (file != (FILE *) NULL))
356 (void) GetNumberColors(image,file,exception);
358 property=FileToString(filename,~0UL,exception);
359 if (property != (char *) NULL)
361 (void) SetImageProperty(histogram_image,"comment",property,
363 property=DestroyString(property);
366 (void) RelinquishUniqueFileResource(filename);
369 Write Histogram image.
371 (void) CopyMagickString(histogram_image->filename,image_info->filename,
373 (void) ResetImagePage(histogram_image,"0x0+0+0");
374 write_info=CloneImageInfo(image_info);
375 *write_info->magick='\0';
376 (void) SetImageInfo(write_info,1,exception);
377 if ((*write_info->magick == '\0') ||
378 (LocaleCompare(write_info->magick,"HISTOGRAM") == 0))
379 (void) FormatLocaleString(histogram_image->filename,MagickPathExtent,
380 "miff:%s",write_info->filename);
381 status=WriteImage(write_info,histogram_image,exception);
382 histogram_image=DestroyImage(histogram_image);
383 write_info=DestroyImageInfo(write_info);