| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #include "magick/studio.h" |
|---|
| 43 | #include "magick/property.h" |
|---|
| 44 | #include "magick/blob.h" |
|---|
| 45 | #include "magick/blob-private.h" |
|---|
| 46 | #include "magick/color.h" |
|---|
| 47 | #include "magick/color-private.h" |
|---|
| 48 | #include "magick/constitute.h" |
|---|
| 49 | #include "magick/exception.h" |
|---|
| 50 | #include "magick/exception-private.h" |
|---|
| 51 | #include "magick/geometry.h" |
|---|
| 52 | #include "magick/image-private.h" |
|---|
| 53 | #include "magick/magick.h" |
|---|
| 54 | #include "magick/memory_.h" |
|---|
| 55 | #include "magick/monitor.h" |
|---|
| 56 | #include "magick/monitor-private.h" |
|---|
| 57 | #include "magick/resource_.h" |
|---|
| 58 | #include "magick/quantum-private.h" |
|---|
| 59 | #include "magick/static.h" |
|---|
| 60 | #include "magick/statistic.h" |
|---|
| 61 | #include "magick/string_.h" |
|---|
| 62 | #include "magick/module.h" |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | static MagickBooleanType |
|---|
| 68 | WriteHISTOGRAMImage(const ImageInfo *,Image *); |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | ModuleExport unsigned long RegisterHISTOGRAMImage(void) |
|---|
| 94 | { |
|---|
| 95 | MagickInfo |
|---|
| 96 | *entry; |
|---|
| 97 | |
|---|
| 98 | entry=SetMagickInfo("HISTOGRAM"); |
|---|
| 99 | entry->encoder=(EncodeImageHandler *) WriteHISTOGRAMImage; |
|---|
| 100 | entry->adjoin=MagickFalse; |
|---|
| 101 | entry->format_type=ImplicitFormatType; |
|---|
| 102 | entry->description=ConstantString("Histogram of the image"); |
|---|
| 103 | entry->module=ConstantString("HISTOGRAM"); |
|---|
| 104 | (void) RegisterMagickInfo(entry); |
|---|
| 105 | return(MagickImageCoderSignature); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | ModuleExport void UnregisterHISTOGRAMImage(void) |
|---|
| 128 | { |
|---|
| 129 | (void) UnregisterMagickInfo("HISTOGRAM"); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | static inline size_t MagickMax(const size_t x,const size_t y) |
|---|
| 167 | { |
|---|
| 168 | if (x > y) |
|---|
| 169 | return(x); |
|---|
| 170 | return(y); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | static MagickBooleanType WriteHISTOGRAMImage(const ImageInfo *image_info, |
|---|
| 174 | Image *image) |
|---|
| 175 | { |
|---|
| 176 | #define HistogramDensity "256x200" |
|---|
| 177 | |
|---|
| 178 | ChannelType |
|---|
| 179 | channel; |
|---|
| 180 | |
|---|
| 181 | char |
|---|
| 182 | filename[MaxTextExtent]; |
|---|
| 183 | |
|---|
| 184 | FILE |
|---|
| 185 | *file; |
|---|
| 186 | |
|---|
| 187 | Image |
|---|
| 188 | *histogram_image; |
|---|
| 189 | |
|---|
| 190 | ImageInfo |
|---|
| 191 | *write_info; |
|---|
| 192 | |
|---|
| 193 | int |
|---|
| 194 | unique_file; |
|---|
| 195 | |
|---|
| 196 | long |
|---|
| 197 | y; |
|---|
| 198 | |
|---|
| 199 | MagickBooleanType |
|---|
| 200 | status; |
|---|
| 201 | |
|---|
| 202 | MagickPixelPacket |
|---|
| 203 | *histogram; |
|---|
| 204 | |
|---|
| 205 | MagickRealType |
|---|
| 206 | maximum, |
|---|
| 207 | scale; |
|---|
| 208 | |
|---|
| 209 | RectangleInfo |
|---|
| 210 | geometry; |
|---|
| 211 | |
|---|
| 212 | register const PixelPacket |
|---|
| 213 | *p; |
|---|
| 214 | |
|---|
| 215 | register long |
|---|
| 216 | x; |
|---|
| 217 | |
|---|
| 218 | register PixelPacket |
|---|
| 219 | *q, |
|---|
| 220 | *r; |
|---|
| 221 | |
|---|
| 222 | size_t |
|---|
| 223 | length; |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 229 | assert(image_info->signature == MagickSignature); |
|---|
| 230 | assert(image != (Image *) NULL); |
|---|
| 231 | assert(image->signature == MagickSignature); |
|---|
| 232 | if (image->debug != MagickFalse) |
|---|
| 233 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
|---|
| 234 | image_info->filename); |
|---|
| 235 | SetGeometry(image,&geometry); |
|---|
| 236 | if (image_info->density == (char *) NULL) |
|---|
| 237 | (void) ParseAbsoluteGeometry(HistogramDensity,&geometry); |
|---|
| 238 | else |
|---|
| 239 | (void) ParseAbsoluteGeometry(image_info->density,&geometry); |
|---|
| 240 | histogram_image=CloneImage(image,geometry.width,geometry.height,MagickTrue, |
|---|
| 241 | &image->exception); |
|---|
| 242 | if (histogram_image == (Image *) NULL) |
|---|
| 243 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 244 | (void) SetImageStorageClass(histogram_image,DirectClass); |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | length=MagickMax((size_t) ScaleQuantumToChar((Quantum) QuantumRange)+1UL, |
|---|
| 249 | histogram_image->columns); |
|---|
| 250 | histogram=(MagickPixelPacket *) AcquireQuantumMemory(length, |
|---|
| 251 | sizeof(*histogram)); |
|---|
| 252 | if (histogram == (MagickPixelPacket *) NULL) |
|---|
| 253 | { |
|---|
| 254 | histogram_image=DestroyImage(histogram_image); |
|---|
| 255 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | channel=image_info->channel; |
|---|
| 261 | (void) ResetMagickMemory(histogram,0,length*sizeof(*histogram)); |
|---|
| 262 | for (y=0; y < (long) image->rows; y++) |
|---|
| 263 | { |
|---|
| 264 | p=AcquireImagePixels(image,0,y,image->columns,1,&image->exception); |
|---|
| 265 | if (p == (const PixelPacket *) NULL) |
|---|
| 266 | break; |
|---|
| 267 | for (x=0; x < (long) image->columns; x++) |
|---|
| 268 | { |
|---|
| 269 | if ((channel & RedChannel) != 0) |
|---|
| 270 | histogram[ScaleQuantumToChar(p->red)].red++; |
|---|
| 271 | if ((channel & GreenChannel) != 0) |
|---|
| 272 | histogram[ScaleQuantumToChar(p->green)].green++; |
|---|
| 273 | if ((channel & BlueChannel) != 0) |
|---|
| 274 | histogram[ScaleQuantumToChar(p->blue)].blue++; |
|---|
| 275 | p++; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | maximum=histogram[0].red; |
|---|
| 279 | for (x=0; x < (long) histogram_image->columns; x++) |
|---|
| 280 | { |
|---|
| 281 | if (((channel & RedChannel) != 0) && (maximum < histogram[x].red)) |
|---|
| 282 | maximum=histogram[x].red; |
|---|
| 283 | if (((channel & GreenChannel) != 0) && (maximum < histogram[x].green)) |
|---|
| 284 | maximum=histogram[x].green; |
|---|
| 285 | if (((channel & BlueChannel) != 0) && (maximum < histogram[x].blue)) |
|---|
| 286 | maximum=histogram[x].blue; |
|---|
| 287 | } |
|---|
| 288 | scale=(MagickRealType) histogram_image->rows/maximum; |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | (void) QueryColorDatabase("#000000",&histogram_image->background_color, |
|---|
| 293 | &image->exception); |
|---|
| 294 | (void) SetImageBackgroundColor(histogram_image); |
|---|
| 295 | for (x=0; x < (long) histogram_image->columns; x++) |
|---|
| 296 | { |
|---|
| 297 | q=GetImagePixels(histogram_image,x,0,1,histogram_image->rows); |
|---|
| 298 | if (q == (PixelPacket *) NULL) |
|---|
| 299 | break; |
|---|
| 300 | if ((channel & RedChannel) != 0) |
|---|
| 301 | { |
|---|
| 302 | y=(long) (histogram_image->rows-scale*histogram[x].red+0.5); |
|---|
| 303 | r=q+y; |
|---|
| 304 | for ( ; y < (long) histogram_image->rows; y++) |
|---|
| 305 | { |
|---|
| 306 | r->red=(Quantum) QuantumRange; |
|---|
| 307 | r++; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | if ((channel & GreenChannel) != 0) |
|---|
| 311 | { |
|---|
| 312 | y=(long) (histogram_image->rows-scale*histogram[x].green+0.5); |
|---|
| 313 | r=q+y; |
|---|
| 314 | for ( ; y < (long) histogram_image->rows; y++) |
|---|
| 315 | { |
|---|
| 316 | r->green=(Quantum) QuantumRange; |
|---|
| 317 | r++; |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | if ((channel & BlueChannel) != 0) |
|---|
| 321 | { |
|---|
| 322 | y=(long) (histogram_image->rows-scale*histogram[x].blue+0.5); |
|---|
| 323 | r=q+y; |
|---|
| 324 | for ( ; y < (long) histogram_image->rows; y++) |
|---|
| 325 | { |
|---|
| 326 | r->blue=(Quantum) QuantumRange; |
|---|
| 327 | r++; |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | if (SyncImagePixels(histogram_image) == MagickFalse) |
|---|
| 331 | break; |
|---|
| 332 | status=SetImageProgress(image,SaveImageTag,y,histogram_image->rows); |
|---|
| 333 | if (status == MagickFalse) |
|---|
| 334 | break; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | histogram=(MagickPixelPacket *) RelinquishMagickMemory(histogram); |
|---|
| 340 | file=(FILE *) NULL; |
|---|
| 341 | unique_file=AcquireUniqueFileResource(filename); |
|---|
| 342 | if (unique_file != -1) |
|---|
| 343 | file=fdopen(unique_file,"wb"); |
|---|
| 344 | if ((unique_file != -1) && (file != (FILE *) NULL)) |
|---|
| 345 | { |
|---|
| 346 | char |
|---|
| 347 | *property; |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | (void) GetNumberColors(image,file,&image->exception); |
|---|
| 353 | (void) fclose(file); |
|---|
| 354 | property=FileToString(filename,~0UL,&image->exception); |
|---|
| 355 | if (property != (char *) NULL) |
|---|
| 356 | { |
|---|
| 357 | (void) SetImageProperty(histogram_image,"comment",property); |
|---|
| 358 | property=DestroyString(property); |
|---|
| 359 | } |
|---|
| 360 | } |
|---|
| 361 | (void) RelinquishUniqueFileResource(filename); |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | (void) CopyMagickString(histogram_image->filename,image_info->filename, |
|---|
| 366 | MaxTextExtent); |
|---|
| 367 | write_info=CloneImageInfo(image_info); |
|---|
| 368 | (void) SetImageInfo(write_info,MagickTrue,&image->exception); |
|---|
| 369 | if (LocaleCompare(write_info->magick,"HISTOGRAM") == 0) |
|---|
| 370 | (void) FormatMagickString(histogram_image->filename,MaxTextExtent, |
|---|
| 371 | "miff:%s",write_info->filename); |
|---|
| 372 | status=WriteImage(write_info,histogram_image); |
|---|
| 373 | histogram_image=DestroyImage(histogram_image); |
|---|
| 374 | write_info=DestroyImageInfo(write_info); |
|---|
| 375 | return(status); |
|---|
| 376 | } |
|---|