This Image Processing Reference section displays the code for an example program that demonstrates how to calculate a Haar wavelet transform for the color channels of an image.
<?php function Clamp($iGray) { if ($iGray < 0) { return 0; } elseif ($iGray > 255) { return 255; } else { return $iGray; } } function CreateHaarWaveletTransformImage($qColorImage) { $iW = ImageSX($qColorImage); $iH = ImageSY($qColorImage); // Allocate the color channel wavelet images $qImageR = ImageCreate(256, 256); $qImageG = ImageCreate(256, 256); $qImageB = ImageCreate(256, 256); $qExtra = ImageCreate(256, 256); for ($iC = 0; $iC < 256; ++$iC) { ImageColorAllocate($qImageR, $iC, 0, 0); ImageColorAllocate($qImageG, 0, $iC, 0); ImageColorAllocate($qImageB, 0, 0, $iC); ImageColorAllocate($qExtra, $iC, $iC, $iC); } // Create the Red, Green, and Blue Channel images for ($j = 0; $j < $iH; ++$j) { for ($i = 0; $i < $iW; ++$i) { $qColor = ImageColorAt($qColorImage, $i, $j); $iRed = (($qColor >> 16) & 0xFF); $iGreen = (($qColor >> 8) & 0xFF); $iBlue = ($qColor & 0xFF); ImageSetPixel($qImageR, $i, $j, $iRed); ImageSetPixel($qImageG, $i, $j, $iGreen); ImageSetPixel($qImageB, $i, $j, $iBlue); } } $qaChannelImages = array(); $qaChannelImages[0] = $qImageR; $qaChannelImages[1] = $qImageG; $qaChannelImages[2] = $qImageB; $iaaIn = array(array(0,0), array(0,0)); $iaaOut = array(array(0,0), array(0,0)); for ($iCH = 0; $iCH < 3; ++$iCH) { $qCurrImage = $qaChannelImages[$iCH]; $iLowPassWidth = $iW/2; $iaaT = array(array(0,0), array(0,0)); while ($iLowPassWidth > 1) { $iDoubleWidth = 2*$iLowPassWidth; for ($j = 0; $j < $iDoubleWidth; $j += 2) { for ($i = 0; $i < $iDoubleWidth; $i += 2) { // First the red image $iaaIn[0][0] = ImageColorAt($qCurrImage, $i, $j); $iaaIn[0][1] = ImageColorAt($qCurrImage, $i + 1, $j); $iaaIn[1][0] = ImageColorAt($qCurrImage, $i, $j + 1); $iaaIn[1][1] = ImageColorAt($qCurrImage, $i + 1, $j + 1); $iaaOut[0][0] = Clamp(($iaaIn[0][0] + $iaaIn[0][1] + $iaaIn[1][0] + $iaaIn[1][1]) >> 2); $iaaOut[0][1] = Clamp((($iaaIn[0][0] - $iaaIn[0][1] + $iaaIn[1][0] - $iaaIn[1][1])) + 128); $iaaOut[1][0] = Clamp((($iaaIn[0][0] + $iaaIn[0][1] - $iaaIn[1][0] - $iaaIn[1][1])) + 128); $iaaOut[1][1] = Clamp((($iaaIn[0][0] - $iaaIn[0][1] - $iaaIn[1][0] + $iaaIn[1][1])) + 128); ImageSetPixel($qExtra, ($i >> 1), ($j >> 1), $iaaOut[0][0]); ImageSetPixel($qExtra, ($i >> 1) + $iLowPassWidth, ($j >> 1), $iaaOut[0][1]); ImageSetPixel($qExtra, ($i >> 1), ($j >> 1) + $iLowPassWidth, $iaaOut[1][0]); ImageSetPixel($qExtra, ($i >> 1) + $iLowPassWidth, ($j >> 1) + $iLowPassWidth, $iaaOut[1][1]); ImageSetPixel($qCurrImage, ($i >> 1), ($j >> 1), $iaaOut[0][0]); } } $iLowPassWidth >>= 1; } // Copy the transformed image back to the channel for ($j = 0; $j < $iH; ++$j) { for ($i = 0; $i < $iW; ++$i) { $iPixel = ImageColorAt($qExtra, $i, $j); ImageSetPixel($qCurrImage, $i, $j, $iPixel); } } } $qHaarChannelImage = ImageCreateTrueColor(2*$iW, 2*$iH); // Copy the transformed color channel images ImageCopyMerge($qHaarChannelImage, $qColorImage, 0, 0, 0, 0, $iW, $iH, 100); ImageCopyMerge($qHaarChannelImage, $qImageR, $iW, 0, 0, 0, $iW, $iH, 100); ImageCopyMerge($qHaarChannelImage, $qImageG, $iW, $iH, 0, 0, $iW, $iH, 100); ImageCopyMerge($qHaarChannelImage, $qImageB, 0, $iH, 0, 0, $iW, $iH, 100); ImageDestroy($qImageR); ImageDestroy($qImageG); ImageDestroy($qImageB); ImageDestroy($qExtra); return $qHaarChannelImage; } $qOriginalImage = ImageCreateFromPng("CrucifixionDali256x256.png"); $qHaarChannelImage = CreateHaarWaveletTransformImage($qOriginalImage); Header('Content-type: image/png'); ImagePng($qHaarChannelImage); ImageDestroy($qOriginalImage); ImageDestroy($qHaarChannelImage); ?>
© 20072024 XoaX.net LLC. All rights reserved.