Method: Magick::Image#quantize

Defined in:
ext/RMagick/rmimage.cpp

#quantize(number_colors = 256, colorspace = Magick::RGBColorspace, dither = true, tree_depth = 0, measure_error = false) ⇒ Magick::Image

Analyzes the colors within a reference image and chooses a fixed number of colors to represent the image. The goal of the algorithm is to minimize the difference between the input and output image while minimizing the processing time.

Returns a new image.

Parameters:

  • number_colors (Numeric) (defaults to: 256)

    The maximum number of colors in the result image.

  • colorspace (Magick::ColorspaceType) (defaults to: Magick::RGBColorspace)

    The colorspace to quantize in.

  • dither (Boolean) (defaults to: true)

    If true, Magick::RiemersmaDitherMethod will be used as DitherMethod. otherwise NoDitherMethod.

  • tree_depth (Numeric) (defaults to: 0)

    The tree depth to use while quantizing. The values 0 and 1 support automatic tree depth determination. The tree depth may be forced via values ranging from 2 to

    1. The ideal tree depth depends on the characteristics of the input image, and may be

    determined through experimentation.

  • measure_error (Boolean) (defaults to: false)

    Set to true to calculate quantization errors when quantizing the image.

Returns:



11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
# File 'ext/RMagick/rmimage.cpp', line 11231

VALUE
Image_quantize(int argc, VALUE *argv, VALUE self)
{
    Image *image, *new_image;
    QuantizeInfo quantize_info;
#if defined(IMAGEMAGICK_7)
    ExceptionInfo *exception;
#endif

    image = rm_check_destroyed(self);
    GetQuantizeInfo(&quantize_info);

    switch (argc)
    {
        case 5:
            quantize_info.measure_error = (MagickBooleanType) RTEST(argv[4]);
        case 4:
            quantize_info.tree_depth = NUM2UINT(argv[3]);
        case 3:
            if (rb_obj_is_kind_of(argv[2], Class_DitherMethod))
            {
                VALUE_TO_ENUM(argv[2], quantize_info.dither_method, DitherMethod);
#if defined(IMAGEMAGICK_6)
                quantize_info.dither = (MagickBooleanType)(quantize_info.dither_method != NoDitherMethod);
#endif
            }
            else
            {
#if defined(IMAGEMAGICK_7)
                quantize_info.dither_method = RTEST(argv[2]) ? RiemersmaDitherMethod : NoDitherMethod;
#else
                quantize_info.dither = (MagickBooleanType) RTEST(argv[2]);
#endif
            }
        case 2:
            VALUE_TO_ENUM(argv[1], quantize_info.colorspace, ColorspaceType);
        case 1:
            quantize_info.number_colors = NUM2UINT(argv[0]);
        case 0:
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 5)", argc);
            break;
    }

    new_image = rm_clone_image(image);

#if defined(IMAGEMAGICK_7)
    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(QuantizeImage) args = { &quantize_info, new_image, exception };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(QuantizeImage), &args);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);
#else
    GVL_STRUCT_TYPE(QuantizeImage) args = { &quantize_info, new_image };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(QuantizeImage), &args);
    rm_check_image_exception(new_image, DestroyOnError);
#endif

    return rm_image_new(new_image);
}