blob: 9278efc1531ea8a866e14f782e2e61c181912eeb [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
cristy787c09c2010-04-16 18:18:34 +00006% 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 %
cristy3ed852e2009-09-05 21:47:34 +000011% %
12% %
cristy787c09c2010-04-16 18:18:34 +000013% Write A Histogram Image. %
cristy3ed852e2009-09-05 21:47:34 +000014% %
15% Software Design %
cristy787c09c2010-04-16 18:18:34 +000016% John Cristy %
17% July 1992 %
cristy3ed852e2009-09-05 21:47:34 +000018% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/property.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/quantum-private.h"
62#include "MagickCore/resource_.h"
63#include "MagickCore/static.h"
64#include "MagickCore/statistic.h"
65#include "MagickCore/string_.h"
66#include "MagickCore/module.h"
cristye40005d2012-03-23 12:18:45 +000067#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000068#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71 Forward declarations.
72*/
cristy787c09c2010-04-16 18:18:34 +000073static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000074 WriteHISTOGRAMImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000075
76/*
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78% %
79% %
80% %
cristy787c09c2010-04-16 18:18:34 +000081% R e g i s t e r H I S T O G R A M I m a g e %
cristy3ed852e2009-09-05 21:47:34 +000082% %
83% %
84% %
85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86%
cristy787c09c2010-04-16 18:18:34 +000087% RegisterHISTOGRAMImage() adds attributes for the Histogram image format
88% to the list of supported formats. The attributes include the image format
89% tag, a method to read and/or write the format, whether the format
90% supports the saving of more than one frame to the same file or blob,
91% whether the format supports native in-memory I/O, and a brief
92% description of the format.
cristy3ed852e2009-09-05 21:47:34 +000093%
cristy787c09c2010-04-16 18:18:34 +000094% The format of the RegisterHISTOGRAMImage method is:
cristy3ed852e2009-09-05 21:47:34 +000095%
cristybb503372010-05-27 20:51:26 +000096% size_t RegisterHISTOGRAMImage(void)
cristy787c09c2010-04-16 18:18:34 +000097%
98*/
cristybb503372010-05-27 20:51:26 +000099ModuleExport size_t RegisterHISTOGRAMImage(void)
cristy787c09c2010-04-16 18:18:34 +0000100{
101 MagickInfo
102 *entry;
103
104 entry=SetMagickInfo("HISTOGRAM");
105 entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage;
106 entry->adjoin=MagickFalse;
cristy009d7392010-07-25 22:08:41 +0000107 entry->format_type=ImplicitFormatType;
cristy787c09c2010-04-16 18:18:34 +0000108 entry->description=ConstantString("Histogram of the image");
109 entry->module=ConstantString("HISTOGRAM");
110 (void) RegisterMagickInfo(entry);
111 return(MagickImageCoderSignature);
112}
113
114/*
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116% %
117% %
118% %
119% U n r e g i s t e r H I S T O G R A M I m a g e %
120% %
121% %
122% %
123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124%
125% UnregisterHISTOGRAMImage() removes format registrations made by the
126% HISTOGRAM module from the list of supported formats.
127%
128% The format of the UnregisterHISTOGRAMImage method is:
129%
130% UnregisterHISTOGRAMImage(void)
131%
132*/
133ModuleExport void UnregisterHISTOGRAMImage(void)
134{
135 (void) UnregisterMagickInfo("HISTOGRAM");
136}
137
138/*
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140% %
141% %
142% %
143% W r i t e H I S T O G R A M I m a g e %
144% %
145% %
146% %
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148%
149% WriteHISTOGRAMImage() writes an image to a file in Histogram format.
150% The image shows a histogram of the color (or gray) values in the image. The
151% image consists of three overlaid histograms: a red one for the red channel,
152% a green one for the green channel, and a blue one for the blue channel. The
153% image comment contains a list of unique pixel values and the number of times
154% each occurs in the image.
155%
156% This method is strongly based on a similar one written by
157% muquit@warm.semcor.com which in turn is based on ppmhistmap of netpbm.
158%
159% The format of the WriteHISTOGRAMImage method is:
160%
161% MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000162% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000163%
164% A description of each parameter follows.
165%
cristy787c09c2010-04-16 18:18:34 +0000166% o image_info: the image info.
cristy3ed852e2009-09-05 21:47:34 +0000167%
cristy787c09c2010-04-16 18:18:34 +0000168% o image: The image.
cristy3ed852e2009-09-05 21:47:34 +0000169%
cristy1e178e72011-08-28 19:44:34 +0000170% o exception: return any errors or warnings in this structure.
171%
cristy3ed852e2009-09-05 21:47:34 +0000172*/
173
cristy787c09c2010-04-16 18:18:34 +0000174static inline size_t MagickMax(const size_t x,const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000175{
cristy787c09c2010-04-16 18:18:34 +0000176 if (x > y)
177 return(x);
178 return(y);
cristy3ed852e2009-09-05 21:47:34 +0000179}
180
cristy787c09c2010-04-16 18:18:34 +0000181static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000182 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000183{
cristy31ac0f02011-03-12 02:04:47 +0000184#define HistogramDensity "256x200"
cristy3ed852e2009-09-05 21:47:34 +0000185
cristy1bd4a3a2010-04-16 12:38:47 +0000186 char
cristy787c09c2010-04-16 18:18:34 +0000187 filename[MaxTextExtent];
cristy1bd4a3a2010-04-16 12:38:47 +0000188
cristy787c09c2010-04-16 18:18:34 +0000189 Image
190 *histogram_image;
191
192 ImageInfo
193 *write_info;
194
cristy1bd4a3a2010-04-16 12:38:47 +0000195 MagickBooleanType
196 status;
197
cristy4c08aed2011-07-01 19:47:50 +0000198 PixelInfo
cristy787c09c2010-04-16 18:18:34 +0000199 *histogram;
cristy1bd4a3a2010-04-16 12:38:47 +0000200
cristya19f1d72012-08-07 18:24:38 +0000201 double
cristy787c09c2010-04-16 18:18:34 +0000202 maximum,
203 scale;
204
205 RectangleInfo
206 geometry;
207
cristy4c08aed2011-07-01 19:47:50 +0000208 register const Quantum
cristy1bd4a3a2010-04-16 12:38:47 +0000209 *p;
210
cristy4c08aed2011-07-01 19:47:50 +0000211 register Quantum
cristy787c09c2010-04-16 18:18:34 +0000212 *q,
213 *r;
cristy1bd4a3a2010-04-16 12:38:47 +0000214
cristy31ac0f02011-03-12 02:04:47 +0000215 register ssize_t
216 x;
217
cristy787c09c2010-04-16 18:18:34 +0000218 size_t
219 length;
cristy1bd4a3a2010-04-16 12:38:47 +0000220
cristy31ac0f02011-03-12 02:04:47 +0000221 ssize_t
222 y;
223
cristy1bd4a3a2010-04-16 12:38:47 +0000224 /*
cristy787c09c2010-04-16 18:18:34 +0000225 Allocate histogram image.
cristy1bd4a3a2010-04-16 12:38:47 +0000226 */
cristy787c09c2010-04-16 18:18:34 +0000227 assert(image_info != (const ImageInfo *) NULL);
228 assert(image_info->signature == MagickSignature);
229 assert(image != (Image *) NULL);
230 assert(image->signature == MagickSignature);
231 if (image->debug != MagickFalse)
232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
233 image_info->filename);
234 SetGeometry(image,&geometry);
235 if (image_info->density == (char *) NULL)
236 (void) ParseAbsoluteGeometry(HistogramDensity,&geometry);
237 else
238 (void) ParseAbsoluteGeometry(image_info->density,&geometry);
239 histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
cristy1e178e72011-08-28 19:44:34 +0000240 exception);
cristy787c09c2010-04-16 18:18:34 +0000241 if (histogram_image == (Image *) NULL)
242 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy1e178e72011-08-28 19:44:34 +0000243 (void) SetImageStorageClass(histogram_image,DirectClass,exception);
cristy787c09c2010-04-16 18:18:34 +0000244 /*
245 Allocate histogram count arrays.
246 */
cristy6e963d82012-06-19 15:23:24 +0000247 length=MagickMax((size_t) ScaleQuantumToChar(QuantumRange)+1UL,
cristy787c09c2010-04-16 18:18:34 +0000248 histogram_image->columns);
cristy4c08aed2011-07-01 19:47:50 +0000249 histogram=(PixelInfo *) AcquireQuantumMemory(length,
cristy787c09c2010-04-16 18:18:34 +0000250 sizeof(*histogram));
cristy4c08aed2011-07-01 19:47:50 +0000251 if (histogram == (PixelInfo *) NULL)
cristy1bd4a3a2010-04-16 12:38:47 +0000252 {
cristy787c09c2010-04-16 18:18:34 +0000253 histogram_image=DestroyImage(histogram_image);
254 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
255 }
256 /*
257 Initialize histogram count arrays.
258 */
cristy787c09c2010-04-16 18:18:34 +0000259 (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000260 for (y=0; y < (ssize_t) image->rows; y++)
cristy787c09c2010-04-16 18:18:34 +0000261 {
cristy1e178e72011-08-28 19:44:34 +0000262 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000263 if (p == (const Quantum *) NULL)
cristy787c09c2010-04-16 18:18:34 +0000264 break;
cristybb503372010-05-27 20:51:26 +0000265 for (x=0; x < (ssize_t) image->columns; x++)
cristy787c09c2010-04-16 18:18:34 +0000266 {
cristyed231572011-07-14 02:18:59 +0000267 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000268 histogram[ScaleQuantumToChar(GetPixelRed(image,p))].red++;
cristyed231572011-07-14 02:18:59 +0000269 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000270 histogram[ScaleQuantumToChar(GetPixelGreen(image,p))].green++;
cristyed231572011-07-14 02:18:59 +0000271 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000272 histogram[ScaleQuantumToChar(GetPixelBlue(image,p))].blue++;
cristyed231572011-07-14 02:18:59 +0000273 p+=GetPixelChannels(image);
cristy787c09c2010-04-16 18:18:34 +0000274 }
275 }
276 maximum=histogram[0].red;
cristybb503372010-05-27 20:51:26 +0000277 for (x=0; x < (ssize_t) histogram_image->columns; x++)
cristy787c09c2010-04-16 18:18:34 +0000278 {
cristyed231572011-07-14 02:18:59 +0000279 if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
cristy2b9582a2011-07-04 17:38:56 +0000280 (maximum < histogram[x].red))
cristy787c09c2010-04-16 18:18:34 +0000281 maximum=histogram[x].red;
cristyed231572011-07-14 02:18:59 +0000282 if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
cristy2b9582a2011-07-04 17:38:56 +0000283 (maximum < histogram[x].green))
cristy787c09c2010-04-16 18:18:34 +0000284 maximum=histogram[x].green;
cristyed231572011-07-14 02:18:59 +0000285 if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
cristy2b9582a2011-07-04 17:38:56 +0000286 (maximum < histogram[x].blue))
cristy787c09c2010-04-16 18:18:34 +0000287 maximum=histogram[x].blue;
288 }
cristya19f1d72012-08-07 18:24:38 +0000289 scale=(double) histogram_image->rows/maximum;
cristy787c09c2010-04-16 18:18:34 +0000290 /*
291 Initialize histogram image.
292 */
cristyfad60c92012-01-19 18:32:39 +0000293 (void) QueryColorCompliance("#000000",AllCompliance,
cristy9950d572011-10-01 18:22:35 +0000294 &histogram_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +0000295 (void) SetImageBackgroundColor(histogram_image,exception);
cristybb503372010-05-27 20:51:26 +0000296 for (x=0; x < (ssize_t) histogram_image->columns; x++)
cristy787c09c2010-04-16 18:18:34 +0000297 {
cristyfad60c92012-01-19 18:32:39 +0000298 q=GetAuthenticPixels(histogram_image,x,0,1,histogram_image->rows,exception);
cristyacd2ed22011-08-30 01:44:23 +0000299 if (q == (Quantum *) NULL)
cristy787c09c2010-04-16 18:18:34 +0000300 break;
cristyed231572011-07-14 02:18:59 +0000301 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy1bd4a3a2010-04-16 12:38:47 +0000302 {
cristybb503372010-05-27 20:51:26 +0000303 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].red-0.5);
cristy787c09c2010-04-16 18:18:34 +0000304 r=q+y;
cristybb503372010-05-27 20:51:26 +0000305 for ( ; y < (ssize_t) histogram_image->rows; y++)
cristy1bd4a3a2010-04-16 12:38:47 +0000306 {
cristy4c08aed2011-07-01 19:47:50 +0000307 SetPixelRed(histogram_image,QuantumRange,r);
cristy787c09c2010-04-16 18:18:34 +0000308 r++;
cristy1bd4a3a2010-04-16 12:38:47 +0000309 }
cristy787c09c2010-04-16 18:18:34 +0000310 }
cristyed231572011-07-14 02:18:59 +0000311 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy787c09c2010-04-16 18:18:34 +0000312 {
cristybb503372010-05-27 20:51:26 +0000313 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].green-0.5);
cristy787c09c2010-04-16 18:18:34 +0000314 r=q+y;
cristybb503372010-05-27 20:51:26 +0000315 for ( ; y < (ssize_t) histogram_image->rows; y++)
cristy787c09c2010-04-16 18:18:34 +0000316 {
cristy4c08aed2011-07-01 19:47:50 +0000317 SetPixelGreen(histogram_image,QuantumRange,r);
cristy787c09c2010-04-16 18:18:34 +0000318 r++;
319 }
320 }
cristyed231572011-07-14 02:18:59 +0000321 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy787c09c2010-04-16 18:18:34 +0000322 {
cristybb503372010-05-27 20:51:26 +0000323 y=(ssize_t) ceil(histogram_image->rows-scale*histogram[x].blue-0.5);
cristy787c09c2010-04-16 18:18:34 +0000324 r=q+y;
cristybb503372010-05-27 20:51:26 +0000325 for ( ; y < (ssize_t) histogram_image->rows; y++)
cristy787c09c2010-04-16 18:18:34 +0000326 {
cristy4c08aed2011-07-01 19:47:50 +0000327 SetPixelBlue(histogram_image,QuantumRange,r);
cristy787c09c2010-04-16 18:18:34 +0000328 r++;
329 }
330 }
331 if (SyncAuthenticPixels(histogram_image,exception) == MagickFalse)
332 break;
333 status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows);
334 if (status == MagickFalse)
335 break;
336 }
cristy4c08aed2011-07-01 19:47:50 +0000337 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
anthony6f201312012-03-30 04:08:15 +0000338
anthony2f7f9ca2012-05-14 06:33:53 +0000339 /* output unique colors? True by default (when not defined) */
340 if (IfStringNotFalse(GetImageOption(image_info, "histogram:unique-colors")))
cristy1bd4a3a2010-04-16 12:38:47 +0000341 {
cristy787c09c2010-04-16 18:18:34 +0000342 FILE
343 *file;
cristy1bd4a3a2010-04-16 12:38:47 +0000344
cristy787c09c2010-04-16 18:18:34 +0000345 int
346 unique_file;
347
348 /*
349 Add a unique colors as an image comment.
350 */
351 file=(FILE *) NULL;
352 unique_file=AcquireUniqueFileResource(filename);
353 if (unique_file != -1)
354 file=fdopen(unique_file,"wb");
355 if ((unique_file != -1) && (file != (FILE *) NULL))
356 {
357 char
358 *property;
359
cristy1e178e72011-08-28 19:44:34 +0000360 (void) GetNumberColors(image,file,exception);
cristy787c09c2010-04-16 18:18:34 +0000361 (void) fclose(file);
cristy1e178e72011-08-28 19:44:34 +0000362 property=FileToString(filename,~0UL,exception);
cristy787c09c2010-04-16 18:18:34 +0000363 if (property != (char *) NULL)
364 {
cristyd15e6592011-10-15 00:13:06 +0000365 (void) SetImageProperty(histogram_image,"comment",property,
366 exception);
cristy787c09c2010-04-16 18:18:34 +0000367 property=DestroyString(property);
368 }
369 }
370 (void) RelinquishUniqueFileResource(filename);
cristy1bd4a3a2010-04-16 12:38:47 +0000371 }
cristy787c09c2010-04-16 18:18:34 +0000372 /*
373 Write Histogram image.
374 */
375 (void) CopyMagickString(histogram_image->filename,image_info->filename,
376 MaxTextExtent);
377 write_info=CloneImageInfo(image_info);
cristy1e178e72011-08-28 19:44:34 +0000378 (void) SetImageInfo(write_info,1,exception);
cristy787c09c2010-04-16 18:18:34 +0000379 if (LocaleCompare(write_info->magick,"HISTOGRAM") == 0)
cristyb51dff52011-05-19 16:55:47 +0000380 (void) FormatLocaleString(histogram_image->filename,MaxTextExtent,
cristy787c09c2010-04-16 18:18:34 +0000381 "miff:%s",write_info->filename);
cristy1e178e72011-08-28 19:44:34 +0000382 status=WriteImage(write_info,histogram_image,exception);
cristy787c09c2010-04-16 18:18:34 +0000383 histogram_image=DestroyImage(histogram_image);
384 write_info=DestroyImageInfo(write_info);
385 return(status);
cristy1bd4a3a2010-04-16 12:38:47 +0000386}