Current Location: Home> Latest Articles> Implement custom tone mapping algorithm using imagecolorresolve()

Implement custom tone mapping algorithm using imagecolorresolve()

M66 2025-05-29

How to implement a custom tone mapping algorithm and optimize image color processing using the imagecolorresolve() function?

When processing images in PHP, we often need to adjust the color, brightness, or contrast of the image. Through the image tone mapping algorithm, we can control color processing more accurately. This article will introduce how to implement a custom tone mapping algorithm using the imagecolorresolve() function and provide examples of optimizing image color processing.

Introduction to imagecolorresolve() function

imagecolorresolve() is a function in PHP's GD library that parses from a given image resource and returns the color index corresponding to the specified RGB value. Its main function is to help us locate and modify the colors in the image when processing images. This function is very useful for fine-grained color control, especially when doing tone mapping.

Function prototype:

 int imagecolorresolve ( resource $image, int $r, int $g, int $b )

Parameter description:

  • $image : The image resource to be processed.

  • $r : The value of the red channel (0 to 255).

  • $g : The value of the green channel (0 to 255).

  • $b : The value of the blue channel (0 to 255).

Return value:

Returns an integer indicating the index of the found color. If the color is not found, return -1.

Tone mapping algorithm

Hue Mapping is a common method of image color adjustment. Different image effects can be achieved by modifying the hue, saturation, or brightness of each pixel. We can use the imagecolorresolve() function to find the color value of each pixel, and then adjust the color according to specific rules.

Example: Custom tone mapping

Suppose we have a picture and want to achieve a simple tone adjustment effect. For example, make the red tones in the image darker and the green tones brighter. We can write a custom tone mapping algorithm, use imagecolorresolve() to find pixel colors, and optimize it through custom rules.

 <?php
// Loading the image
$imagePath = "https://m66.net/images/sample.jpg";
$image = imagecreatefromjpeg($imagePath);

// Get the width and height of the image
$width = imagesx($image);
$height = imagesy($image);

// Iterate through each pixel and apply a tone mapping algorithm
for ($y = 0; $y < $height; $y++) {
    for ($x = 0; $x < $width; $x++) {
        // Get the color value of the current pixel
        $rgb = imagecolorat($image, $x, $y);
        
        // GetRGBComponent
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        // Apply custom tone mapping rules
        // Here we add red channel,Reduce green channel,and increase the brightness of the blue channel
        $r = min($r + 20, 255); // Red darkening
        $g = max($g - 30, 0);   // Green weakens
        $b = min($b + 15, 255); // Blue brighten

        // use imagecolorresolve() Find color index
        $colorIndex = imagecolorresolve($image, $r, $g, $b);

        // If no corresponding color is found,Create a new color
        if ($colorIndex == -1) {
            $colorIndex = imagecolorallocate($image, $r, $g, $b);
        }

        // Set the modified color back to the image
        imagesetpixel($image, $x, $y, $colorIndex);
    }
}

// Output the modified image
header('Content-Type: image/jpeg');
imagejpeg($image);

// Free memory
imagedestroy($image);
?>

Code parsing

  1. Loading image : We load a JPEG format image through the imagecreatefromjpeg() function. In practice, you can replace it with any supported image format.

  2. Get image size : Get the width and height of the image through imagesx() and imagesy() functions, and prepare for pixel traversal.

  3. Traversing each pixel of the image : Use the imagecolorat() function to get the RGB value of the current pixel and extract the value of each color channel.

  4. Applying tone mapping rules : We customized a simple rule according to our needs: increase the red channel, reduce the green channel, and increase the blue channel.

  5. Find color index : Find the adjusted color through the imagecolorresolve() function. If not found, we use imagecolorallocate() to create a new color and apply it to the image.

  6. Output image : Finally, we use the imagejpeg() function to output the processed image to the browser, which you can save as a file as well.

Optimize image color processing

Frequent call to imagecolorresolve() can cause performance issues when working with large numbers of images, because every lookup of colors requires traversing the color table. To optimize performance, the following strategies can be considered:

  1. Color Cache : Cache frequently used colors into an array to avoid repeated calculations.

  2. Reduce image size : Scaling the image to reduce the number of pixels that need to be traversed.

  3. Batch processing : For multiple images that need to be processed, parallel processing techniques are used to speed up the processing process.

Summarize

Through the imagecolorresolve() function, we can implement a custom tone mapping algorithm to flexibly adjust the color of the image. Combining cache and performance optimization strategies, you can effectively process large amounts of images and improve image processing efficiency. Hopefully the examples in this article will help you better understand and use this function.