SmileyHackathon#6 いってきた

 
SmileyHackathon#6に参加して、久しぶりに自分のコードをぶっつづけで書きましたっ。
 
IRCで雑談するとのことで、MacbookTigerにLimeChatを入れようとするも入らず。
・→開始後2時間格闘するも、「MacbookTigerに乗せたLimeChatが起動しなくなった」というブログ記事を見つけて、あぁOSアップデートの弊害かな?とIRCは諦める。。
・最初の自己紹介の時に宣言した、『C言語で画像処理をするサンプルを動かそう』と思うも、参考にしようとブクマしてたサイトがなぜか落ちてた
・→駄目じゃん・・・と、GD@PHPで、なんとなく画像処理のコードを書くことにした。
・→動くところまで行きたかったけど、動かなかったので、LTはできませんでした。。この次はっ。
 
なんで動かなかったかは
MAMP慣れてなかったので、パーミッションがよくわかんなかったです。。
・そして家にてWAMPで動かしてみたら、メモリオーバーですって。。
・→これはCで書き直したほうがいいかもしれん。。
・→今日のコードは徒労!?そ、そんな、、
・→でもメソッドの分け方はけっこううまくいった気がする?テストかいてないので、メモリの問題が大丈夫になっても、意図する動きをすんなりするかはわからないけど。。
 
ということで、[動かないコード]ですが。。今日の成果物として載せます。
Cで移植して、動いたらまた載せます。
 

<?php
/*
 * Gyouretsu filter class
 * 20090822 mana
 * usage...
 * $f = new filter();
 * $f->setRect(array(array(1,1,1),array(1,1,1),array(1,1,1)));
 * $f->setImgpath("/home/me/tmp/0908221848.png");
 * header("Content-type:image/png");
 * $f->getImage();
 */
class filter {
    private $filter;
    private $imgpath;
    private $w;
    private $h;
    public function __construct(){
    }
    /* @return bool */
    public function setRect($arr){
        if($this->checkRect($arr)===true){
            $this->filter = $rect;
            return true;
        }
        return false;
    }
    /* is it 2D?
     * @return bool */
    private function checkRect($arr){
        if(!is_array($arr)){
            return false;
        }
        foreach($arr as $line){
            if(!is_array($line)){
                return false;
            }
            foreach($line as $num){
                if(!is_numeric($num)){
                    return false;
                }
            }
        }
        return true;
    }
    /* @return void */
    public function setImgpath($imgpath){
        $this->imgpath = $imgpath;
        list($this->w,$this->h) = getimagesize($imgpath);
    }
    /* @return string or binary */
    public function getImage($savename = null){
        $resource = $this->process($this->imgpath);
        imagepng($resource,$savename);
    }
    /* @return bool = success/failed */
    private function process($imgpath){
        $resource = $this->imageOpen($imgpath);
        $img_arr = $this->resource2arr($resource);
        $img_after_arr = img_arr;
        $filter = $this->filter;
        foreach($img_arr as $color=>$image){
            foreach($image as $x => $line){
                foreach($line as $y => $num){
                    $afterfilter = filter($num,$filter);
                    $filter_w = count($afterfilter);
                    $filter_h = count($afterfilter[0]);
                    $x0 = round($filter_w / 2);
                    $y0 = round($filter_h / 2);
                    foreach($afterfilter as $f_x => $f_line){
                        foreach($f_line as $f_y => $f_num){
                            if(isset($img_after_arr[$color][$x+$f_x-$x0][$y+$f_y-$y0])){
                                $img_after_arr[$color][$x+$f_x-$x0][$y+$f_y-$y0] 
                                 += $f_num;
                            }
                        }
                    }
                }
            }
        }
        $this->round_arr($img_after_arr);
        $this->arr2resource($img_after_arr,@$resource);
        return true;
    }
    /* @param int(color), arr(filter);
     * @return arr(after filter)
     */
    private function filter($color,$filter){
        $ret = array();
        foreach($filter as $line){
            $ret_line = array();
            foreach($line as $num){
                $ret_line[] = $color * $num;
            }
            $ret[] = $ret_line;
        }
        return $ret;
    }
    /* @param $array resource2arr
     * @return $array resource2arr
     */
    private function round_arr($arr){
        $ret = array(); 
        foreach($arr as $key=>$line){
            $ret[$key] = array();
            foreach($line as $keyval=>$num){
                $ret[$key][$keyval] = round($num);
            }
        }
        return $ret;
    }
    /* @return resource
     * switch vir extention
    */
    private function imageOpen($imgpath){
        $split = split(".",$imgpath);
        $ext = $split[count($split)-1];
        $ext = strtolower($ext);
        switch($ext){
            case "jpg":
            case "jpeg":
                $ret = imagecreatefromjpeg($imgpath);
                break;

            case "gif":
                $ret = imagecreatefromgif($imgpath);
                break;

            case "png":
                $ret = imagecreatefrompng($imgpath);
                break;

            case "bmp":
                $ret = imagecreatefromwbmp($imgpath);
                break;

            default:
                $ret = imagecreate($this->w,$this->h);
            
        }
        return $ret;
    }
    /* @return array(r=>arr(arr,arr),g=>arr(arr,arr),b=>arr(arr,arr))
     * img resource 2 array
     */
    private function resource2arr($resource){
        $r_array = array();
        $g_array = array();
        $b_array = array();
        for($y=0; $y<$this->h; $y++){
            for($x=0; $x<$this->w; $x++){
                $pixel = imagecolorat($resource,$x,$y);
                $r = ($pixel >> 16) & 0xFF;
                $g = ($pixel >> 8 ) & 0xFF;
                $b = $pixel & 0xFF;
                $r_array[$x][$y] = $r;
                $g_array[$x][$y] = $g;
                $b_array[$x][$y] = $b;
            }
        }
        $ret = array("r"=>$r_array, "g"=>$g_array, "b"=>$b_array);
        return $ret;
    }
    /* @return bool
     * @param array(3D), 2nd param changes direct
     * array 2 img resource
     */
    private function arr2resource($array,$resource){
        $r_array = $array["r"];
        $g_array = $array["g"];
        $b_array = $array["b"];
        for($y=0; $y<$this->h; $y++){
            for($x=0; $x<$this->w; $x++){
                $r = $r_array[$x][$y];
                $g = $g_array[$x][$y];
                $b = $b_array[$x][$y];
                $color = ($r << 16) | ($g << 8) | $b;
                imagesetpixel($resource,$x,$y,$color);
            }
        }
        return true;
    }
}
$f = new filter();
$f->setRect(array(array(1,1,1),array(1,1,1),array(1,1,1)));
$f->setImgpath("C:\\windowstare.jpg");
//header("Content-type:image/png");
$f->getImage();

?>