Dark Filter
 | |
| Figure 9. Dark Filter: This filter reduces all RGB values by a predetermined amount. |
You can darken an image by using a dark filter, which reduces all the RGB values by some specified fractional amount (see
Figure 9):
// Listing dark_filter.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&0x00FF0000;
$r = $r >> 16;
$g = $rgb&0x0000FF00;
$g = $g >> 8;
$b = $rgb&0x0000FF;
$frac = 0.5; // 0.0 < frac < 1.0
$rr = (int)($r*$frac);
$gg = (int)($g*$frac);
$bb = (int)($b*$frac);
$result = (0x000000FF<<24)|
($rr <<16)|($gg << 8)|$bb;
$new_r = ($result >> 16) & 0xFF;
$new_g = ($result >> 8) & 0xFF;
$new_b = $result & 0xFF;
$new_color = imagecolorallocate(
$im, $new_r, $new_g, $new_b);
imagesetpixel($im, $i, $j, $new_color);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Black Filter
 | |
| Figure 10. Black Filter: Increasing the amount of black in an image doesn't simply darken it, it alters the effect. |
This application uses a filter that increases the black color in an image by choosing a random threshold value between 0 and 255, and then setting all the pixels where the R, G, and B values exceed that threshold to black (
0,
0,
0). Note that this operates primarily on the darker-colored pixels in the image (see
Figure 10):
// Listing strong_filter.php
<?php
$im = imagecreatefromjpeg("tree.jpg");
$size = getimagesize("tree.jpg");
$L=$size[0];
$H=$size[1];
$k=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;
$profile = rand(0,255);
if(($r<$profile)&&($g<$profile)&&
($b<$profile))
{
$new_color = imagecolorallocate(
$im, 0, 0, 0);
imagesetpixel($im, $i, $j, $new_color);
}
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Black-Removal Filter
 | |
| Figure 11. Snow Filter: Increasing the amount of white in an image functions as a "snow" filter. |
The reverse of a black filter takes dark pixels and changes them to another color—white in this example—creating a "snow" filter (see
Figure 11):
// Listing black_removal_filter.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);
$red = 255;
$green = 255;
$blue = 255;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if(($r<50)&&($g<50)&&($b<50)) {
$color = imagecolorallocate(
$im, $red, $green, $blue);
imagesetpixel($im, $i, $j, $color);
}
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Shading Filter
 | |
| Figure 12. Shading Filter: AND'ing a specific color with the existing colors in an image produces a color-shading effect. |
You can display an image in shades of a specific color using this filter by AND'ing the existing color with the filter-specific color. The example in
Figure 12 uses a hard-coded orange hue, but you can use any color you like.
// Listing mixed_filter.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);
$red = 255;
$green = 124;
$blue = 28;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 16) & 0xFF;
$b = $rgb & 0xFF;
$color = imagecolorallocate(
$im, $red, $green, $blue);
$result = $rgb & $color;
imagesetpixel($im, $i, $j, $result);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>
Paint Filter
 | |
| Figure 13. Paint Filter: The Paint Filter provides an unusual "hand-painted" effect. |
Here's a slightly more complex filter that "paints" a picture (see
Figure 13):
// Listing paint_filter.php
<?php
$im = imagecreatefromjpeg("tree.jpg");
$size = getimagesize("tree.jpg");
$L=$size[0];
$H=$size[1];
for($m=0;$m<$H;$m++){
for($i=0;$i<$L;$i++){
$rgb = imagecolorat($im, $i, $m);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 16) & 0xFF;
$blue = $rgb & 0xFF;
$tR = $red;
$tG = $green;
$tB = $blue;
$R = array(255,255,0,0,0,
255,255,0,64,128,192,255,255,-1);
$G = array(0,255,0,255,0,255,
0,255,64,128,192,200,175,-1);
$B = array(0,255,0,0,255,0,
255,255,64,128,192,0,175,-1);
$R[13]=$red;
$G[13]=$green;
$B[13]=$blue;
for($j=0;$j<13;$j++){
for($k=$j+1;$k<14;$k++)
{
if($R[$j]<$R[$k]){
$aux=$R[$j];$R[$j]=$R[$k];$R[$k]=$aux;}
if($G[$j]<$G[$k]){
$aux=$G[$j];$G[$j]=$G[$k];$G[$k]=$aux;}
if($B[$j]<$B[$k]){
$aux=$B[$j];$B[$j]=$B[$k];$B[$k]=$aux;}
}
}
for($j=0;$j<14;$j++){
if(($R[$j]==$tR)&&($j==0)){$xR=$R[$j+1];break;}
if(($R[$j]==$tR)&&($j==8)){$xR=$R[$j-1];break;}
if(($R[$j]==$tR)&&($j!=0)&&($j!=8)){
$xR=min(($tR-$R[$j-1]),($R[$j+1]-$tR));
break;
}
}
for($j=0;$j<14;$j++){
if(($G[$j]==$tG)&&($j==0)){$xG=$G[$j+1];break;}
if(($G[$j]==$tG)&&($j==8)){$xG=$G[$j-1];break;}
if(($G[$j]==$tG)&&($j!=0)&&($j!=8)){
$xG=min(($tG-$G[$j-1]),($G[$j+1]-$tG));
break;
}
}
for($j=0;$j<14;$j++){
if(($B[$j]==$tB)&&($j==0)){$xB=$B[$j+1];break;}
if(($B[$j]==$tB)&&($j==8)){$xB=$B[$j-1];break;}
if(($B[$j]==$tB)&&($j!=0)&&($j!=8)){
$xB=min(($tB-$B[$j-1]),($B[$j+1]-$tB));
break;
}
}
$color = imagecolorallocate(
$im, abs($xR), abs($xG), abs($xB));
imagesetpixel($im, $i, $m, $color);
}
}
header("Content-type: image/jpeg");
imagejpeg($im);
?>