ちなみにガンマ補正とは、画像の「暗すぎ」とか「明るすぎ」というものを補正するのが目的の画像処理のことです。 Windows用のディスプレイはガンマ補正値が「2.2」となっているそうです。
/*
* ガンマ補正用クラス
*/
class Gamma
{
private $image; // 入力画像リソース
private $new_image; // 出力画像リソース
private $size_x;
private $size_y;
/**
コンストラクタ
@param $image 入力画像のリソース
*/
public function __construct( $image )
{
$this->image = $image;
$this->size_x = imagesx( $this->image );
$this->size_y = imagesy( $this->image );
$this->new_image = imagecreatetruecolor( $this->size_x, $this->size_y );
}
/**
デストラクタ
*/
public function __destruct()
{
imagedestroy( $this->image );
}
/**
変更後の画像リソース取得
@return 画像リソース
*/
public function get_image()
{
return $this->new_image;
}
/**
出力用画像リソースを新しく作成する
*/
public function renew_image()
{
unset( $this->new_image );
$this->new_image = imagecreatetruecolor( $this->size_x, $this->size_y );
}
/**
ガンマ補正
@param $gamma ガンマ補正値 ※ 0 < $gamma <= 5
@return 成功:true 失敗:false
*/
public function gamma( $gamma )
{
if( $gamma < 0 ) {
return false;
}
if( $gamma > 5 ) {
return false;
}
for( $x = 0 ; $x < $this->size_x ; $x += 1 ) {
for( $y = 0 ; $y < $this->size_y ; $y += 1 ) {
$this->culc_gamma( $x, $y, $gamma );
}
}
return true;
}
/*
ガンマ補正用計算
*/
private function culc_gamma( $x, $y, $gamma )
{
$rgb = imagecolorat( $this->image, $x, $y );
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$temp_gamma = 1 / $gamma;
$r_base = $r / 255;
$g_base = $g / 255;
$b_base = $b / 255;
$nr = (int)floor( 255 * pow( $r_base, $temp_gamma ) );
$ng = (int)floor( 255 * pow( $g_base, $temp_gamma ) );
$nb = (int)floor( 255 * pow( $b_base, $temp_gamma ) );
$nrgb = imagecolorexact( $this->image, $nr, $ng, $nb );
imagesetpixel( $this->new_image, $x, $y, $nrgb );
}
}
ちなみに、以下にプログラムを使って編集した画像を載せてみます。
1枚目が元画像、2枚目が補正値0.5のもの(暗くしたもの)、3枚目が補正値2.0のもの(明るくしたもの)です。



