Coverage Report

Created: 2026-03-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/imagemagick/coders/histogram.c
Line
Count
Source
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
%                                   Cristy                                    %
17
%                                 July 1992                                   %
18
%                                                                             %
19
%                                                                             %
20
%  Copyright @ 1999 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
%    https://imagemagick.org/license/                                         %
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
*/
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/nt-base-private.h"
60
#include "MagickCore/option.h"
61
#include "MagickCore/pixel-accessor.h"
62
#include "MagickCore/property.h"
63
#include "MagickCore/quantum-private.h"
64
#include "MagickCore/resource_.h"
65
#include "MagickCore/static.h"
66
#include "MagickCore/statistic.h"
67
#include "MagickCore/string_.h"
68
#include "MagickCore/module.h"
69
#include "MagickCore/token.h"
70
#include "MagickCore/utility.h"
71

72
/*
73
  Forward declarations.
74
*/
75
static MagickBooleanType
76
  WriteHISTOGRAMImage(const ImageInfo *,Image *,ExceptionInfo *);
77

78
/*
79
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
%                                                                             %
81
%                                                                             %
82
%                                                                             %
83
%   R e g i s t e r H I S T O G R A M I m a g e                               %
84
%                                                                             %
85
%                                                                             %
86
%                                                                             %
87
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
%
89
%  RegisterHISTOGRAMImage() adds attributes for the Histogram image format
90
%  to the list of supported formats.  The attributes include the image format
91
%  tag, a method to read and/or write the format, whether the format
92
%  supports the saving of more than one frame to the same file or blob,
93
%  whether the format supports native in-memory I/O, and a brief
94
%  description of the format.
95
%
96
%  The format of the RegisterHISTOGRAMImage method is:
97
%
98
%      size_t RegisterHISTOGRAMImage(void)
99
%
100
*/
101
ModuleExport size_t RegisterHISTOGRAMImage(void)
102
8
{
103
8
  MagickInfo
104
8
    *entry;
105
106
8
  entry=AcquireMagickInfo("HISTOGRAM","HISTOGRAM","Histogram of the image");
107
8
  entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage;
108
8
  entry->flags^=CoderAdjoinFlag;
109
8
  entry->format_type=ImplicitFormatType;
110
8
  (void) RegisterMagickInfo(entry);
111
8
  return(MagickImageCoderSignature);
112
8
}
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
*/
133
ModuleExport void UnregisterHISTOGRAMImage(void)
134
0
{
135
0
  (void) UnregisterMagickInfo("HISTOGRAM");
136
0
}
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,
162
%        Image *image,ExceptionInfo *exception)
163
%
164
%  A description of each parameter follows.
165
%
166
%    o image_info: the image info.
167
%
168
%    o image:  The image.
169
%
170
%    o exception: return any errors or warnings in this structure.
171
%
172
*/
173
static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info,
174
  Image *image,ExceptionInfo *exception)
175
0
{
176
0
#define HistogramDensity  "256x200"
177
178
0
  char
179
0
    filename[MagickPathExtent];
180
181
0
  const char
182
0
    *option;
183
184
0
  const MagickInfo
185
0
    *magick_info;
186
187
0
  Image
188
0
    *histogram_image;
189
190
0
  ImageInfo
191
0
    *write_info;
192
193
0
  MagickBooleanType
194
0
    status;
195
196
0
  PixelInfo
197
0
    *histogram;
198
199
0
  double
200
0
    maximum,
201
0
    scale;
202
203
0
  RectangleInfo
204
0
    geometry;
205
206
0
  const Quantum
207
0
    *p;
208
209
0
  Quantum
210
0
    *q,
211
0
    *r;
212
213
0
  ssize_t
214
0
    x;
215
216
0
  size_t
217
0
    length;
218
219
0
  ssize_t
220
0
    y;
221
222
  /*
223
    Allocate histogram image.
224
  */
225
0
  assert(image_info != (const ImageInfo *) NULL);
226
0
  assert(image_info->signature == MagickCoreSignature);
227
0
  assert(image != (Image *) NULL);
228
0
  assert(image->signature == MagickCoreSignature);
229
0
  if (IsEventLogging() != MagickFalse)
230
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
231
0
      image_info->filename);
232
0
  SetGeometry(image,&geometry);
233
0
  if (image_info->density == (char *) NULL)
234
0
    (void) ParseAbsoluteGeometry(HistogramDensity,&geometry);
235
0
  else
236
0
    (void) ParseAbsoluteGeometry(image_info->density,&geometry);
237
0
  histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
238
0
    exception);
239
0
  if (histogram_image == (Image *) NULL)
240
0
    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
241
0
  (void) SetImageStorageClass(histogram_image,DirectClass,exception);
242
  /*
243
    Allocate histogram count arrays.
244
  */
245
0
  length=MagickMax((size_t) ScaleQuantumToChar(QuantumRange)+1UL,
246
0
    histogram_image->columns);
247
0
  histogram=(PixelInfo *) AcquireQuantumMemory(length,sizeof(*histogram));
248
0
  if (histogram == (PixelInfo *) NULL)
249
0
    {
250
0
      histogram_image=DestroyImage(histogram_image);
251
0
      ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
252
0
    }
253
  /*
254
    Initialize histogram count arrays.
255
  */
256
0
  (void) memset(histogram,0,length*sizeof(*histogram));
257
0
  for (y=0; y < (ssize_t) image->rows; y++)
258
0
  {
259
0
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
260
0
    if (p == (const Quantum *) NULL)
261
0
      break;
262
0
    for (x=0; x < (ssize_t) image->columns; x++)
263
0
    {
264
0
      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
265
0
        histogram[ScaleQuantumToChar(GetPixelRed(image,p))].red++;
266
0
      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
267
0
        histogram[ScaleQuantumToChar(GetPixelGreen(image,p))].green++;
268
0
      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
269
0
        histogram[ScaleQuantumToChar(GetPixelBlue(image,p))].blue++;
270
0
      p+=(ptrdiff_t) GetPixelChannels(image);
271
0
    }
272
0
  }
273
0
  maximum=histogram[0].red;
274
0
  for (x=0; x < (ssize_t) histogram_image->columns; x++)
275
0
  {
276
0
    if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
277
0
        (maximum < histogram[x].red))
278
0
      maximum=histogram[x].red;
279
0
    if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
280
0
        (maximum < histogram[x].green))
281
0
      maximum=histogram[x].green;
282
0
    if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
283
0
        (maximum < histogram[x].blue))
284
0
      maximum=histogram[x].blue;
285
0
  }
286
0
  scale=0.0;
287
0
  if (fabs((double) maximum) >= MagickEpsilon)
288
0
    scale=(double) histogram_image->rows/maximum;
289
  /*
290
    Initialize histogram image.
291
  */
292
0
  (void) QueryColorCompliance("#000000",AllCompliance,
293
0
    &histogram_image->background_color,exception);
294
0
  (void) SetImageBackgroundColor(histogram_image,exception);
295
0
  for (x=0; x < (ssize_t) histogram_image->columns; x++)
296
0
  {
297
0
    q=GetAuthenticPixels(histogram_image,x,0,1,histogram_image->rows,exception);
298
0
    if (q == (Quantum *) NULL)
299
0
      break;
300
0
    if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
301
0
      {
302
0
        y=CastDoubleToSsizeT(ceil((double) histogram_image->rows-scale*
303
0
          histogram[x].red-0.5));
304
0
        r=q+y*(ssize_t) GetPixelChannels(histogram_image);
305
0
        for ( ; y < (ssize_t) histogram_image->rows; y++)
306
0
        {
307
0
          SetPixelRed(histogram_image,QuantumRange,r);
308
0
          r+=(ptrdiff_t) GetPixelChannels(histogram_image);
309
0
        }
310
0
      }
311
0
    if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
312
0
      {
313
0
        y=CastDoubleToSsizeT(ceil((double) histogram_image->rows-scale*
314
0
          histogram[x].green-0.5));
315
0
        r=q+y*(ssize_t) GetPixelChannels(histogram_image);
316
0
        for ( ; y < (ssize_t) histogram_image->rows; y++)
317
0
        {
318
0
          SetPixelGreen(histogram_image,QuantumRange,r);
319
0
          r+=(ptrdiff_t) GetPixelChannels(histogram_image);
320
0
        }
321
0
      }
322
0
    if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
323
0
      {
324
0
        y=CastDoubleToSsizeT(ceil((double) histogram_image->rows-scale*
325
0
          histogram[x].blue-0.5));
326
0
        r=q+y*(ssize_t) GetPixelChannels(histogram_image);
327
0
        for ( ; y < (ssize_t) histogram_image->rows; y++)
328
0
        {
329
0
          SetPixelBlue(histogram_image,QuantumRange,r);
330
0
          r+=(ptrdiff_t) GetPixelChannels(histogram_image);
331
0
        }
332
0
      }
333
0
    if (SyncAuthenticPixels(histogram_image,exception) == MagickFalse)
334
0
      break;
335
0
    status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
336
0
      histogram_image->rows);
337
0
    if (status == MagickFalse)
338
0
      break;
339
0
  }
340
0
  histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
341
0
  option=GetImageOption(image_info,"histogram:unique-colors");
342
0
  if ((IsHistogramImage(image,exception) != MagickFalse) || 
343
0
      (IsStringTrue(option) != MagickFalse) ||
344
0
      (GetImageOption(image_info,"format") != (const char *) NULL))
345
0
    {
346
0
      FILE
347
0
        *file;
348
349
0
      int
350
0
        unique_file;
351
352
      /*
353
        Add a unique colors as an image comment.
354
      */
355
0
      file=(FILE *) NULL;
356
0
      unique_file=AcquireUniqueFileResource(filename);
357
0
      if (unique_file != -1)
358
0
        file=fdopen(unique_file,"wb");
359
0
      if ((unique_file != -1) && (file != (FILE *) NULL))
360
0
        {
361
0
          char
362
0
            *property;
363
364
0
          (void) GetNumberColors(image,file,exception);
365
0
          (void) fclose(file);
366
0
          property=FileToString(filename,~0UL,exception);
367
0
          if (property != (char *) NULL)
368
0
            {
369
0
              (void) SetImageProperty(histogram_image,"comment",property,
370
0
                exception);
371
0
              property=DestroyString(property);
372
0
            }
373
0
        }
374
0
      (void) RelinquishUniqueFileResource(filename);
375
0
    }
376
  /*
377
    Write Histogram image.
378
  */
379
0
  (void) CopyMagickString(histogram_image->filename,image_info->filename,
380
0
    MagickPathExtent);
381
0
  (void) ResetImagePage(histogram_image,"0x0+0+0");
382
0
  write_info=CloneImageInfo(image_info);
383
0
  *write_info->magick='\0';
384
0
  (void) SetImageInfo(write_info,1,exception);
385
0
  magick_info=GetMagickInfo(write_info->magick,exception);
386
0
  if ((magick_info == (const MagickInfo*) NULL) ||
387
0
      (LocaleCompare(magick_info->magick_module,"HISTOGRAM") == 0))
388
0
    (void) FormatLocaleString(histogram_image->filename,MagickPathExtent,
389
0
      "miff:%s",write_info->filename);
390
0
  status=WriteImage(write_info,histogram_image,exception);
391
0
  histogram_image=DestroyImage(histogram_image);
392
0
  write_info=DestroyImageInfo(write_info);
393
0
  return(status);
394
0
}