Histogram added. Histogram added.
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Histogram added.

Started by Spaz, June 09, 2005, 11:15:22 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Spaz

Hi everyone,

This is my first real post here..I've been using Coppermine for a while now..it just does such a better job than the others.

I've written some code, that I've added to my site, that I thought you may like.  It is linked from the displayimage.php page, and creates a histogram of the image being viewed.  The histogram has a total (white), plus the individual RGB (lines) values.  The histogram is saved to your normal image folder, and if the image is relooked at, the saved file is loaded, rather than trying to recreate it.

There isnt much error checking in it.  I know that it requires GD, but it doesnt check for it.  It also assumes you have a JPG image.

In displayimage.php there is one line added in function html_picinfo()


    $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$CURRENT_PIC_DATA['filename']."&filepath=".$CURRENT_PIC_DATA['filepath']."'>";

    return theme_html_picinfo($info);
}


In the same directory as displayimage.php create a file called histo.php

Here is the code.  Feel free to use/abuse/edit/change the code to your hearts content.  Please leave the opening comments in, so I get credit at least :) 

Oh, and feel free to email me anton_spaz@yahoo.com any thoughts, comments, or changes.


<?php
//      Histogram creation
//      Created by Anton Sparrius (Spaz) 6/9/05  anton_spaz@yahoo.com
//      Free to use and change, provided you keep these lines :)
//
                        
$image $_REQUEST['image'];
                        
$filepath $_REQUEST['filepath'];
                        
$path "albums/".$filepath;

                        
$hist_file="hist_".substr($image,0,strlen($image)-4).".png";


                        if (
file_exists($path.$hist_file)) {
                          
$im=imagecreatefromPNG($path $hist_file);
                          
imagePNG($im);
                          
imagedestroy($im);
                        } else {
                        
$im=imagecreatefromjpeg($path.$image);
                        for(
$i=0;$i<imagesx($im);$i+=2)
                        {
                                for(
$j=0;$j<imagesy($im);$j++)
                                {
                                        
$rrggbb=imagecolorsforindex ($imimagecolorat($im,$i,$j));
                                        
$r[$rrggbb['red']]+=1;
                                        
$g[$rrggbb['green']]+=1;
                                        
$b[$rrggbb['blue']]+=1;
                                }
                        }
                        for (
$i=0;$i<256;$i++)
                        {
                                
$max[$i]=($r[$i]+$g[$i]+$b[$i])/3;
                        }
                        
$max_value=max($max)/150;
                        
$m[0]=max($r);
                        
$m[1]=max($b);
                        
$m[2]=max($g);
                        
$max_rgb=max($m)/150;

                        
$im_out imageCreate (280164);
                        
$background imageColorAllocate($im_out,70,70,70);
                        
$hist=ImageColorAllocate($im_out,171,205,239);
                        
$white=ImageColorAllocate($im_out,255,255,255);
                        
$red=ImageColorAllocate($im_out,255,0,0);
                        
$green=ImageColorAllocate($im_out,0,255,0);
                        
$blue=ImageColorAllocate($im_out,0,0,255);
                        
$ry=107;
                        
$gy=107;
                        
$by=107;

                        for(
$i=0;$i<256;$i++)
                        {
                                
imageLine($im_out$i+14157$i+14157-($max[$i]/$max_value),$white);
                                
imageLine($im_out$i+13$ry$i+14157-($r[$i]/$max_rgb), $red);
                                
imageLine($im_out$i+13$gy$i+14157-($g[$i]/$max_rgb), $green);
                                
imageLine($im_out$i+13$by$i+14157-($b[$i]/$max_rgb), $blue);
                                
$ry=157-($r[$i]/$max_rgb);
                                
$gy=157-($g[$i]/$max_rgb);
                                
$by=157-($b[$i]/$max_rgb);
                        }
                        
imageLine($im_out,13,158,270,158,$hist);
                        
imageLine($im_out,13,6,270,6,$hist);
                        
imageLine($im_out,13,6,13,158,$hist);
                        
imageLine($im_out,270,6,270,158,$hist);

                        
imagePNG($im_out,$path $hist_file);
                        
imageDestroy($im);
                        
imagedestroy($im_out);
                        
$im=imagecreatefromPNG($path $hist_file);
                        
imagePNG($im);
                        
imagedestroy($im);
                        }
?>



Spaz

I size my images prior to uploading to CM, since I apply sharpening to the image size, rather than let ImageMagick do as it wishes, I didnt notice a bug.  If you try to go off full sized images eg 8mpix, the histogram will not build, as there is a limit that gets reached.  I will fix this tomorrow.  I will also add detection for JPG/GIF/PNG.  And, I believe I can add an easy check to avoid running if GD is not installed.

Stay tuned tomorrow....same bat channel..

kegobeer

You'll have to check for GD 2.0.28+, which includes GIF creation support.  Earlier versions don't support GIF creation.  Any GIF manipulation with an old version will result in failure.
Do not send me a private message unless I ask for one.  Make your post public so everyone can benefit.

There are no stupid questions
But there are a LOT of inquisitive idiots

snork13

@Spaz


thanks for the cool mod, I attached a screen shot for others ;D

--snork13

Spaz

Quote from: kegobeer on June 10, 2005, 03:54:05 AM
You'll have to check for GD 2.0.28+, which includes GIF creation support.  Earlier versions don't support GIF creation.  Any GIF manipulation with an old version will result in failure.

Should I even bother with GIF support?  Since we are talking about photos being displayed, for web viewing, I would have to assume that JPG is used pretty much all the time.  TIFFs would be too large, GIF wouldnt have good enough quality.

Is there any information available about what file format people use to display their images?

As for standard GD detection, I've changed the line in displayimage.php to include a simple check.  If it doesnt find the ImageType function, than no GD is available, and it wont ever run histogram.php

   

if (function_exists('ImageTypes')) {
      $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$CURRENT_PIC_DATA['filename']."&filepath=".$CURRENT_PIC_DATA['filepath']."'>";
    }
    return theme_html_picinfo($info);
}


Spaz

Here is the new code.  Changes include :

displayimage.php :

1) Copied code to get file name of the image displayed.  If the image is an intermediate image, a user-definable prefix (defaults to normal_ is added to the filename.  The intermediate image is used for the histogram creation, because it returns results that are the same as the full sized image, and the histogram is created quicker with smaller file sizes.

2) Since a histogram is really only relevant for photos, and since Coppermine is a photo gallery software, and since JPG is used to get a good quality image, with low file size, I've added code to only do the histogram from a JPG

3) The entire code is wrapped in a simple GD detection.  If GD is not installed, none of the code will be run.

Again, this is in the function html_picinfo() in displayimage.php

// Added 6/10/05 - Anton Sparrius
// Creation/Display of histogram

    if (function_exists('ImageTypes')) {  // Only do if GD exists

      if($CONFIG['thumb_use']=='ht' && $CURRENT_PIC_DATA['pheight'] > $CONFIG['picture_width'] ){ // The wierd comparision is because only picture_width is $
        $condition = true;
      }elseif($CONFIG['thumb_use']=='wd' && $CURRENT_PIC_DATA['pwidth'] > $CONFIG['picture_width']){
        $condition = true;
      }elseif($CONFIG['thumb_use']=='any' && max($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight']) > $CONFIG['picture_width']){
        $condition = true;
      }else{
       $condition = false;
      }

      if ($CONFIG['make_intermediate'] && $condition ) {
          $picture_url = get_pic_url($CURRENT_PIC_DATA, 'normal');
      } else {
          $picture_url = get_pic_url($CURRENT_PIC_DATA, 'fullsize');
      }

      if (strtoupper(substr($picture_url,strlen($picture_url)-3,3))=="JPG") {
        $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$picture_url."'>";
      }
    }

// End Addition

    return theme_html_picinfo($info);
}


In the Coppermine root folder, create a file called histo.php with the following code.

Changes include adjustments for the new file name (normal_ etc) and cosmetic changes to the actual histogram.  A PNG is created in the same folder as the image.  It looks like the PNG is normally around 2k.


<?php
//      Histogram creation
//      Created by Anton Sparrius (Spaz) 6/9/05  anton_spaz@yahoo.com
//      Free to use and change, provided you keep these lines :)
//
                        
$tmp_image $_REQUEST['image'];
                        
$image_array=split('[/\]',$tmp_image);
                        
$image=$image_array[count($image_array)-1];
                        
$path=rtrim($tmp_image,$image);

                        
$hist_file="hist_".substr($image,0,strlen($image)-4).".png";

                        if (
file_exists($path.$hist_file)) {
                          
$im=imagecreatefromPNG($path $hist_file);
                          
imagePNG($im);
                          
imagedestroy($im);
                        } else {
                        
$im=imagecreatefromjpeg($path.$image);
                        for(
$i=0;$i<imagesx($im);$i+=2)
                        {
                                for(
$j=0;$j<imagesy($im);$j++)
                                {
                                        
$rrggbb=imagecolorsforindex ($imimagecolorat($im,$i,$j));
                                        
$r[$rrggbb['red']]+=1;
                                        
$g[$rrggbb['green']]+=1;
                                        
$b[$rrggbb['blue']]+=1;
                                }
                        }
                        for (
$i=0;$i<256;$i++)
                        {
                                
$max[$i]=($r[$i]+$g[$i]+$b[$i])/3;
                        }
                        
$max_value=max($max)/150;
                        
$m[0]=max($r);
                        
$m[1]=max($b);
                        
$m[2]=max($g);
                        
$max_rgb=max($m)/150;

                        
$im_out imageCreate (284164);
                        
$background imageColorAllocate($im_out,100,100,100);
                        
$box_fill   imageColorAllocate($im_out,70,70,70);
                        
$white=ImageColorAllocate($im_out,200,200,200);
                        
$black=ImageColorAllocate($im_out,20,20,20);
                        
$grey=ImageColorAllocate($im_out,160,160,160);
                        
$red=ImageColorAllocate($im_out,255,0,0);
                        
$green=ImageColorAllocate($im_out,0,200,0);
                        
$blue=ImageColorAllocate($im_out,0,0,255);
                        
$ry=107;
                        
$gy=107;
                        
$by=107;

                        
imageFilledRectangle($im_out,13,6,270,158,$box_fill);

                        for(
$i=0;$i<256;$i++)
                        {
                                
imageLine($im_out$i+14157$i+14157-($max[$i]/$max_value),$white);
                                
imageLine($im_out$i+13$ry$i+14157-($r[$i]/$max_rgb), $red);
                                
imageLine($im_out$i+13$gy$i+14157-($g[$i]/$max_rgb), $green);
                                
imageLine($im_out$i+13$by$i+14157-($b[$i]/$max_rgb), $blue);
                                
$ry=157-($r[$i]/$max_rgb);
                                
$gy=157-($g[$i]/$max_rgb);
                                
$by=157-($b[$i]/$max_rgb);
                        }
                        
imageSetThickness($im_out,3);
                        
imageLine($im_out,13,158,272,158,$grey);
                        
imageLine($im_out,13,6,272,6,$black);
                        
imageLine($im_out,13,6,13,158,$black);
                        
imageLine($im_out,272,6,272,158,$grey);

                        
imagePNG($im_out,$path $hist_file);
                        
imageDestroy($im);
                        
imagedestroy($im_out);
                        
$im=imagecreatefromPNG($path $hist_file);
                        
imagePNG($im);
                        
imagedestroy($im);
                        }
?>



Example of the histogram.  Note, if you have the image information turned off, you will not see the histogram.

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fspaz.is-a-geek.com%2Fpics%2Falbums%2Fuserpics%2F10001%2Fhisto_example.jpg&hash=8a5c87ed71487055bf1d029482f412650d62a5c8)

Spaz

Quote from: snork13 on June 10, 2005, 04:47:31 AM
@Spaz


thanks for the cool mod, I attached a screen shot for others ;D

--snork13

Thanks :)  Question..did you have to change code somewhere to get the word "Histogram" to show up?  On mine, it doesnt list anything, just ":"

djarnis

#7
Quote from: Spaz on June 10, 2005, 05:21:31 PM
Thanks :) Question..did you have to change code somewhere to get the word "Histogram" to show up? On mine, it doesnt list anything, just ":"
Just to make this mod complete, then you have to add a line in the language-file in order to get the word "Histogram" to show up.to
i.e. lang/english.php
Find "$lang_picinfo = array(" and add the following line to that array:
        'Histogram'=>'Histogram',
/dj

Spaz

This patch works fine in 1.41 Beta as well.

flux

hi that mod looks interesting but i've got a really stupid question..

What are histograms good for ? can it be used to compare two pictures to see if they look alike ?

got redirected to this thread by Gaugau answering a question i posted there : http://forum.coppermine-gallery.net/index.php?topic=25818.0

I was wondering if there were any ways to build some kind of CRC value of a picture in order to "visually" compare two pictures even though their technical caracteristics are different (size, resolution, depth..)..

I've been trying to find some keywords on the subject and read all i could find using ImageMagick or GDlib but no luck so far..

If you're into image analysis maybe you can help me with that or point me in the right direction..
thx


ecto

Quote from: flux on January 03, 2006, 03:13:27 PM
What are histograms good for ? can it be used to compare two pictures to see if they look alike ?

Here's a really good introduction to histograms: http://www.luminous-landscape.com/tutorials/understanding-series/understanding-histograms.shtml

flux

very interesting indeed.
unfortunately that's not what i'm looking for..

thx

Tomi1977

hallo!
i want to change the background for the histogramm.. i want to insert an image as background for it.. how can i do this..??

Spaz

You cant.

The histograph is created live, by anaylising the image you feed it.  It's then generates a image file by drawing lines.

Sorry.

Quote from: Tomi1977 on April 25, 2006, 08:56:07 PM
hallo!
i want to change the background for the histogramm.. i want to insert an image as background for it.. how can i do this..??

Tomi1977

i`ve made it..  :P
so it`s possible to do it..

Joachim Müller

then why don't you tell us how you did?

hlabout

Thanks for this really cool mod.

Regards,

Harald

www.haraldlabout.nl/fotografie

mykee

I use ImageMagick, no GD. Can change this mod to ImageMagick users?
My gallery and demo for AnythingSlider, Photocols and CollagePlus plugin: http://foto.acegem.hu/index.php

Joachim Müller


Heroe

I have question about this mod,i have at on my gallery but every time when i do batch upload i have the histo_png selected,how to made the histo_png files to be not selected,because if i have 50 pictures i will have 50 histo_png and i must unmark them one by one.