PHP » Quellcode betrachten

Download

<?php
/* (C) 2008 by Julian von Mendel (prog@derjulian.net)
 * License: LGPL3
 * $LastChangedBy: jvm $
 * $LastChangedDate: 2008-04-03 14:04:24 +0000 (Thu, 03 Apr 2008) $
 * $Revision: 5 $
 *
 * Todo: generalize
 */

require_once("image.php");

class 
image_sobel extends imagealgorithmmask {
    
/* sobel mask */
    
public static $gx = array(
            array(-
101),
            array(-
202),
            array(-
101));
    public static 
$gy = array(
            array(
121),
            array(
000),
            array(-
1, -2, -1));
    protected 
$colors true;
    protected 
$alpha = array();
    protected 
$maxbrightness false;

    public function 
setmaxbrightness($yes) {
        
$this->maxbrightness $yes;
        return 
$this;
    }

    public function 
setcolors($yes) {
        
$this->colors $yes;
        return 
$this;
    }

    public function 
getalpha() {
        return 
$this->alpha;
    }

    public function 
algorithm($x$y) {
        if ((
$y == || $y == $this->image->height() - 1) ||
                (
$x == || $x == $this->image->width() - 1))
            return 
0;

        
$sumx 0;
        
$sumy 0;

        for(
$i = -1$i <= 1$i++) { for($j = -1$j <= 1$j++) {
            if (
self::$gx[$i 1][$j 1] == &&
                    
self::$gy[$i 1][$j 1] == 0)
                continue;
            
            
$intensity $this->image->getpixel($x $i$y $jfalse);
            
$intensity reset($intensity);
            
$sumx += $intensity self::$gx[$i 1][$j 1];
            
$sumy += $intensity self::$gy[$i 1][$j 1];
        }}
        
        
$sum sqrt(pow($sumx2) + pow($sumy2));

        
$alpha atan2($sumy$sumx) * 180 M_PI;
        
$alpha round($alpha 45) * 45;
        if (
$alpha 0)
            
$alpha += 180;
        
$alpha %= 180;

        if (
$y == 0)
            
$this->alpha[$x] = array();
        
$this->alpha[$x][$y] = $alpha;

        if (
$sum $this->threshold)
            return 
0;

        if (
$this->maxbrightness)
            
$sum 255;

        if (!
$this->colors)
            return 
$sum;

        switch (
$alpha) {
            case 
0:
                return array(
"red" => $sum"green" => $sum"blue" => 0);
            case 
45/* red */
                
return array("red" => $sum"green" => 0"blue" => 0);
            case 
90/* blue */
                
return array("red" => 0"green" => 0"blue" => $sum);
            case 
135/* green */
               
return array("red" => 0"green" => $sum"blue" => 0);
            default:
                throw new 
AlgorithmException("Impossible.");
        }
    }
}

© 2009 Julian von Mendel (http://derjulian.net) | Datum: 28.04.2024