Tuesday, May 8, 2012

Understanding Image Colorspace within the ImageMagick Class of PHP

The getColorSpace function of the ImageMagick class returns an integer that is equal to one of the colorspace constants of the ImageMagick class.  That's going to look like "imagick::COLORSPACE_UNDEFINED", but the "_VALUE" could be anything among: undefined, rgb, gray, transparent, ohta, lab, xyz, ycbcr, ycc, yiq, ypbpr, yuv, cmyk, srgb, hsb, hsl, hwb, rec601luma, rec709luma, log, and cmy.  Of course, it actually returns an integer based on the order in which the value appears in this list.  Undefined returns '0', RGB returns '1', Gray returns '2', and so on.

While things like Undefined, Gray, and Transparent are obvious, the only two here I have heard of were RGB ("Red/Green/Blue") and CMYK ("Cyan/Magenta/Yellow/blacK").  The others may be of use, though.  The rec601luma and rec709luma are colorspaces used to define luminance within an image, so that could help you get a grasp of the brightness of an image.  According to Wikipedia, the rec601luma is nothing more than a specialized spectrum of the R/G/B values: "0.299 R' + 0.587 G' + 0.114 B'".  The rec709luma is: "0.2126 R' + 0.7152 G' + 0.0722 B'".

For every single image I have worked with, I have always received back a '0' from this function, indicating an Undefined colorspace.  So far, this function has simply returned whatever value was set using the setColorspace function of the ImageMagick class.

So, what's the difference between the functions getColorspace and getImageColorspace?  Not much, except the getColorspace default value is set to "_UNDEFINED", whereas the getImageColorspace functions default value is set to "_SRGB" ('13').

The total code looks like ...

<?php
   
        // Create Imagick Object
        // ---------------------------------------------
   
    $imagick_type = new Imagick();
   
        // Filename to Open
        // ---------------------------------------------

    $file_to_grab_with_location = "image_workshop_directory/test.bmp";
   
        // Open File
        // ---------------------------------------------
           
    $file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');

        // Read File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);

        // Get Colorspace
        // ---------------------------------------------

    $imagick_type_color_space = $imagick_type->getColorspace();

        // Print Colorspace
        // ---------------------------------------------

    print($imagick_type_color_space);

?>

Expected results?  So far, always: '0', representing the ImageMagick constant 'imagick::COLORSPACE_UNDEFINED'.

Official Function Page: http://www.php.net/manual/en/imagick.getcolorspace.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

No comments:

Post a Comment