<?php
|
/*---------------------------------------------------------------*/
|
/*
|
Titre : Egalisation des couleurs d'une image
|
|
URL : https://phpsources.net/code_s.php?id=971
|
Date édition : 15 Fév 2019
|
*/
|
/*---------------------------------------------------------------*/
|
|
function image_equalize($img){
|
$NUM_PIXELS = imagesx($img) * imagesy($img);
|
|
/**
|
|
* Get a repartition table of R, V, B channel components. Get also the min and max value of each components.
|
|
* @return array['r'=>[0..255 => nb of occurence], 'v'=>[0..255 => nb of occurence], 'b'=>[0..255 => nb of occurence],
|
|
* 'r_min'=>minimum R value, 'r_max'=>maximum R value, 'v_min'=>minimum V value, 'v_max'=>maximum V value, 'b_min'=>minimum B value,'b_max'=>maximum B value ]
|
* @warning time consumming function.
|
*/
|
|
function image_getcolorstable($img)
|
{
|
$width = imagesx($img);
|
$height = imagesy($img);
|
|
$lut_r = array_fill(0, 255, 0);
|
$lut_v = array_fill(0, 255, 0);
|
$lut_b = array_fill(0, 255, 0);
|
$r_min = $v_min = $b_min = 9999;
|
$r_max = $v_max = $b_max = 0;
|
for($x = 0; $x < $width; $x++)
|
{
|
for($y = 0; $y < $height; $y++)
|
{
|
$c = imagecolorat($img, $x, $y);
|
$r = ($c & 0xFF0000 ) >> 16;
|
$v = ($c & 0x00FF00 ) >> 8;
|
$b = ($c & 0x0000FF );
|
|
$lut_r[$r] = @$lut_r[$r] + 1;
|
$lut_v[$v] = @$lut_v[$v] + 1;
|
$lut_b[$b] = @$lut_b[$b] + 1;
|
|
if ($r > $r_max)
|
$r_max = $r;
|
if ($r < $r_min)
|
$r_min = $r;
|
|
if ($v > $v_max)
|
$v_max = $v;
|
if ($v < $v_min)
|
$v_min = $v;
|
|
if ($b > $b_max)
|
$b_max = $b;
|
if ($b < $b_min)
|
$b_min = $b;
|
}
|
}
|
return array('r'=>$lut_r, 'v'=>$lut_v, 'b'=>$lut_b,
|
'r_min'=>$r_min, 'r_max'=>$r_max,
|
'v_min'=>$v_min, 'v_max'=>$v_max,
|
'b_min'=>$b_min, 'b_max'=>$b_max);
|
}
|
|
/**
|
* Equalize a channel components.
|
* @param lut : Look Up Table of channel occurence
|
* @param min : minimum channel component value (usually 0)
|
* @param max : minimum channel component value (usually 255)
|
* @param $num_pixels : number of pixels (whidth x height) of the image
|
* @return a new equalized look up table
|
*/
|
function lut_equalizechannel($lut, $min, $max, $num_pixels)
|
{
|
$c = count($lut);
|
$new_lut = array_fill(0, $c, 0);
|
$sum = 0;
|
for($i = 0; $i < $c; $i++)
|
{
|
$sum += $lut[$i];
|
$result = $sum * $max / $num_pixels;
|
$new_lut[$i] = (int)$result;
|
}
|
return $new_lut;
|
}
|
|
|
$lut = image_getcolorstable($img);
|
|
$MAX_SAMPLE = $lut['r_max'];
|
|
$lut_r = lut_equalizechannel($lut['r'], 0, $MAX_SAMPLE, $NUM_PIXELS);
|
$lut_v = lut_equalizechannel($lut['v'], 0, $MAX_SAMPLE, $NUM_PIXELS);
|
$lut_b = lut_equalizechannel($lut['b'], 0, $MAX_SAMPLE, $NUM_PIXELS);
|
|
$width = imagesx($img);
|
$height = imagesy($img);
|
|
for ($x = 0; $x < $width; $x++)
|
{
|
for ($y = 0; $y < $height; $y++)
|
{
|
$c = imagecolorat($img, $x, $y);
|
$r = ($c & 0xFF0000 ) >> 16;
|
$v = ($c & 0x00FF00 ) >> 8;
|
$b = ($c & 0x0000FF );
|
|
$new_r = $lut_r[$r];
|
$new_v = $lut_v[$v];
|
$new_b = $lut_b[$b];
|
imagesetpixel($img, $x, $y, ($new_r << 16) | ($new_v << 8) |
|
$new_b);
|
}
|
}
|
|
}
|
|
?>
|
|
|