Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function histeq( $im ){
- if ( is_string( $im ) )
- $im = imagecreatefromjpeg( $im );
- $_h = imagesy( $im );
- $_w = imagesx( $im );
- $_f = $_h * $_w / 255;
- for ( $y = 0; $y < $_h ; $y++ ) for ( $x = 0; $x < $_w; $x ++ ){
- $col = imagecolorat( $im, $x, $y );
- $originals[0][ ( $col >> 16 ) & 0xFF ]++;
- $originals[1][ ( $col >> 8 ) & 0xFF ]++;
- $originals[2][ $col & 0xFF ]++;
- }
- foreach ( array( 0, 1, 2 ) as $chn ){
- $ori = $originals[ $chn ];
- $i = 0;
- $sum = array();
- for ( $j = 0; $j < 255; $j++ ){
- $counter = $ori[$j];
- $map = array();
- while ( $counter ){
- while ( $sum[$i] >= $_f )
- $i++;
- $count = min( $counter, $_f - $sum[$i] );
- $map[ min( $i,255 ) ] += $count / $ori[$j];
- $sum[$i] += $count;
- $counter -= $count;
- }
- $cum = 0;
- foreach( $map as $k => $v){
- $cum += $v;
- $matrix[$chn][$j][$k] = $cum;
- }
- }
- }
- for ( $y = 0; $y < $_h ; $y++ ) for ( $x = 0; $x < $_w; $x ++ ){
- $col = imagecolorat( $im, $x, $y );
- $kol[0] = ( $col >> 16 ) & 0xFF;
- $kol[1] = ( $col >> 8 ) & 0xFF;
- $kol[2] = $col & 0xFF;
- $rand = rand( 0, 1000000 ) / 1000000;
- foreach ( array( 0, 1, 2 ) as $chn ){
- foreach ( (array) $matrix[$chn][ $kol[$chn] ] as $k => $v ){
- if ( $rand <= $v ){
- $kol[$chn] = $k;
- break;
- }
- }
- }
- // FF:8bit, FE:7bit, FC:6bit, F8:5bit, F0:4bit, E0:3bit, C0:2bit, 80:1bit
- // ex FFFFFF:RGB888 E0E0C0:RGB332 // 0xF0FCE0;//0xE0F0C0;
- $col = ( ( $kol[0] << 16 ) | ( $kol[1] << 8 ) | $kol[2] ) & 0xE0E0C0;
- imagesetpixel( $im, $x, $y, $col );
- }
- ob_start();
- imagejpeg( $im );
- return ob_get_clean();
- }
- header( "Content-Type: image/jpg" );
- echo histeq( __DIR__ . "/test.jpg" );
Advertisement
Add Comment
Please, Sign In to add comment