Image Processing Computer Science

Add an Attribute to an XML Element

This Image Processing Reference section displays the code for an example program that demonstrates how to create red, green, and blue histograms for a color image with GD.

ColorHistograms.php

<?php
  function CreateColorHistogramsImage($qOriginalImage) {
    $iWidth        = ImageSX($qOriginalImage);
    $iHeight       = ImageSY($qOriginalImage);
    // Allocate the color histogram images
    $qHistImageR   = ImageCreate(256, 256);
    $qHistImageG   = ImageCreate(256, 256);
    $qHistImageB   = ImageCreate(256, 256);
    $iaHistR       = array();
    $iaHistG       = array();
    $iaHistB       = array();
    // Initialize the entries 0 to 255 to zero
    $iaHistR       = array_fill(0, 256, 0);
    $iaHistG       = array_fill(0, 256, 0);
    $iaHistB       = array_fill(0, 256, 0);
    // Color the histogram backgrounds
    ImageColorAllocate($qHistImageR, 255, 196, 196);
    ImageColorAllocate($qHistImageG, 196, 255, 196);
    ImageColorAllocate($qHistImageB, 196, 196, 255);
    $qRed   = ImageColorAllocate($qHistImageR, 255, 0, 0);
    $qGreen = ImageColorAllocate($qHistImageG, 0, 255, 0);
    $qBlue  = ImageColorAllocate($qHistImageB, 0, 0, 255);
    for ($j = 0; $j < $iHeight; ++$j) {
      for ($i = 0; $i < $iWidth; ++$i) {
        $qColor  = ImageColorAt($qOriginalImage, $i, $j);
        $iRed    = (($qColor >> 16) & 0xFF);
        $iGreen  = (($qColor >> 8) & 0xFF);
        $iBlue   = ($qColor & 0xFF);
        ++$iaHistR[$iRed];
        ++$iaHistG[$iGreen];
        ++$iaHistB[$iBlue];
      }
    }
    $iMax = 1;
    // Find the largest value in histograms for scaling
    for ($iC = 0; $iC < 256; ++$iC) {
      $iMax = (($iaHistR[$iC] > $iMax) ? $iaHistR[$iC] : $iMax);
      $iMax = (($iaHistG[$iC] > $iMax) ? $iaHistG[$iC] : $iMax);
      $iMax = (($iaHistB[$iC] > $iMax) ? $iaHistB[$iC] : $iMax);
    }
    // Draw the histogram lines in each image
    for ($iC = 0; $iC < 256; ++$iC) {
      $iHeightR = IntVal(Round(255*$iaHistR[$iC]/$iMax));
      $iHeightG = IntVal(Round(255*$iaHistG[$iC]/$iMax));
      $iHeightB = IntVal(Round(255*$iaHistB[$iC]/$iMax));
      ImageLine($qHistImageR, $iC, 255, $iC, 255-$iHeightR, $qRed);
      ImageLine($qHistImageG, $iC, 255, $iC, 255-$iHeightG, $qGreen);
      ImageLine($qHistImageB, $iC, 255, $iC, 255-$iHeightB, $qBlue);
    }
	$iHistImageWidth = ((768 > $iWidth) ? 768 : $iWidth);
    $qHistogramsImage = ImageCreateTrueColor($iHistImageWidth, 256+$iHeight);
    // Copy the histogram images to the main image
    ImageCopyMerge($qHistogramsImage, $qHistImageR, 0, 0, 0, 0, 256, 256, 100);
    ImageCopyMerge($qHistogramsImage, $qHistImageG, 256, 0, 0, 0, 256, 256, 100);
    ImageCopyMerge($qHistogramsImage, $qHistImageB, 512, 0, 0, 0, 256, 256, 100);
    ImageCopyMerge($qHistogramsImage, $qOriginalImage, 0, 256, 0, 0, $iWidth, $iHeight, 100);
    ImageDestroy($qHistImageR);
    ImageDestroy($qHistImageG);
    ImageDestroy($qHistImageB);
    return $qHistogramsImage;
  }

  $qOriginalImage = ImageCreateFromPng("LastJudgment.png");

  $qHistogramsImage = CreateColorHistogramsImage($qOriginalImage);

  Header('Content-type: image/png');
  ImagePng($qHistogramsImage);
  ImageDestroy($qOriginalImage);
  ImageDestroy($qHistogramsImage);
?>
 
 

Output

 
 

© 2007–2022 XoaX.net LLC. All rights reserved.