Altering Brightness, Hue, and Saturation
The next three filters modify the brightness, saturation, and hue of an image. To do that, you need to be able to convert between the Red-Green-Blue and Hue-Saturation-Brightness (HSB) forms of color representation, for which you can use the conversion functions
RGBtoHSB and
HSBtoRGB. Both conversions are based on very common algorithms so I won't describe them here, but
Listing 1 shows the
RGBtoHSB function code while
Listing 2 shows the code for the
HSBtoRGB function:
Brightness Filter
 | |
Figure 14. Bright Filter: To increase brightness, raise the HSB brightness component of the image's pixels. |
As you might expect, the brightness filter can make a dark image lighter or a light image darker.
Figure 14 shows an example. First, you change the RGB color value to an HSB value, and then change the brightness portion of the HSB value.
// Listing brightness.php
<?php
$im = imagecreatefromjpeg("tree.jpg");
$size = getimagesize("tree.jpg");
$L=$size[0];
$H=$size[1];
for($j=0;$j<$H;$j++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($im, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$getArrayHSB = RGBtoHSB($r,$g,$b);
$brightness = 1.5; //[0.0, 1.0] - darker,
[1.0, 10.0] - brightner
$getArrayHSB[2] = $brightness*$getArrayHSB[2];
$getArrayHSB[2] = max(0.0,min($getArrayHSB[2],255.0));
$getArrayRGB = HSBtoRGB(
$getArrayHSB[0], $getArrayHSB[1],
$getArrayHSB[2]);
$hsbrgb = imagecolorallocate(
$im, $getArrayRGB[0], $getArrayRGB[1],
$getArrayRGB[2]);
$result = (($rgb & 0xff000000)|($hsbrgb));
imagesetpixel($im, $i, $j, $result);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Saturation Filter
 | |
Figure 15. Saturation Filter: Increasing saturation increases the color depth across the entire image. |
Modifying the saturation of an image changes the amount of color in its pixels.
Figure 15 shows the tree image with increased saturation.
// Listing saturation.php
<?php
$im = imagecreatefromjpeg("tree.jpg");
$size = getimagesize("tree.jpg");
$L=$size[0];
$H=$size[1];
for($j=0;$j<$H;$j++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($im, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$getArrayHSB = RGBtoHSB($r,$g,$b);
$saturation = 9; //[0,10]
$getArrayHSB[1] = $saturation*$getArrayHSB[1];
$getArrayHSB[1] = max(0.0,min($getArrayHSB[1],1.0));
$getArrayRGB = HSBtoRGB($getArrayHSB[0],
$getArrayHSB[1],$getArrayHSB[2]);
$hsbrgb = imagecolorallocate($im,
$getArrayRGB[0], $getArrayRGB[1],
$getArrayRGB[2]);
$result = (($rgb & 0xff000000)|($hsbrgb));
imagesetpixel($im, $i, $j, $result);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Hue Filter
 | |
Figure 16. Hue Filter: Changing hue moves the overall image colors toward a specific color band. |
Changing the hue is a similar operation, programmatically, but changes the overall color or "hue" of the image (see
Figure 16):
// Listing hue.php
<?php
$im = imagecreatefromjpeg("tree.jpg");
$size = getimagesize("tree.jpg");
$L=$size[0];
$H=$size[1];
for($j=0;$j<$H;$j++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($im, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$getArrayHSB = RGBtoHSB($r,$g,$b);
$hue = 3; //[0,10]
$getArrayHSB[0] = $hue*$getArrayHSB[0];
$getArrayHSB[0] = max(0.0,min($getArrayHSB[0],360.0));
$getArrayRGB = HSBtoRGB($getArrayHSB[0],
$getArrayHSB[1],$getArrayHSB[2]);
$hsbrgb = imagecolorallocate($im,
$getArrayRGB[0], $getArrayRGB[1],
$getArrayRGB[2]);
$result = (($rgb & 0xff000000)|($hsbrgb));
imagesetpixel($im, $i, $j, $result);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>