Method: Magick::Image#color_histogram
- Defined in:
- ext/RMagick/rmimage.cpp
#color_histogram ⇒ Hash<Magick::Pixel, Integer>
Computes the number of times each unique color appears in the image.
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 |
# File 'ext/RMagick/rmimage.cpp', line 2830
VALUE
Image_color_histogram(VALUE self)
{
Image *image, *dc_copy = NULL;
VALUE hash, pixel;
size_t x, colors;
ExceptionInfo *exception;
#if defined(IMAGEMAGICK_7)
PixelInfo *histogram;
#else
ColorPacket *histogram;
#endif
image = rm_check_destroyed(self);
exception = AcquireExceptionInfo();
// If image not DirectClass make a DirectClass copy.
if (image->storage_class != DirectClass)
{
dc_copy = rm_clone_image(image);
#if defined(IMAGEMAGICK_7)
GVL_STRUCT_TYPE(SetImageStorageClass) args = { dc_copy, DirectClass, exception };
#else
GVL_STRUCT_TYPE(SetImageStorageClass) args = { dc_copy, DirectClass };
#endif
CALL_FUNC_WITHOUT_GVL(GVL_FUNC(SetImageStorageClass), &args);
image = dc_copy;
}
GVL_STRUCT_TYPE(GetImageHistogram) args = { image, &colors, exception };
void *ret = CALL_FUNC_WITHOUT_GVL(GVL_FUNC(GetImageHistogram), &args);
histogram = reinterpret_cast<decltype(histogram)>(ret);
if (histogram == NULL)
{
if (dc_copy)
{
DestroyImage(dc_copy);
}
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
if (rm_should_raise_exception(exception, DestroyExceptionRetention))
{
RelinquishMagickMemory(histogram);
if (dc_copy)
{
DestroyImage(dc_copy);
}
rm_raise_exception(exception);
}
hash = rb_hash_new();
for (x = 0; x < colors; x++)
{
#if defined(IMAGEMAGICK_7)
pixel = Pixel_from_PixelColor(&histogram[x]);
#else
pixel = Pixel_from_PixelColor(&histogram[x].pixel);
#endif
rb_hash_aset(hash, pixel, ULONG2NUM((unsigned long)histogram[x].count));
}
/*
Christy evidently didn't agree with Bob's memory management.
*/
RelinquishMagickMemory(histogram);
if (dc_copy)
{
// Do not trace destruction
DestroyImage(dc_copy);
}
RB_GC_GUARD(hash);
RB_GC_GUARD(pixel);
return hash;
}
|