/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *   AndroidWorld Library, Copyright 2011 Bryan Chadwick                   *
 *                                                                         *
 *   FILE: ./android/world/test/RasterImageTest.java                       *
 *                                                                         *
 *   This file is part of AndroidWorld.                                    *
 *                                                                         *
 *   AndroidWorld is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License           *
 *   as published by the Free Software Foundation, either version          *
 *   3 of the License, or (at your option) any later version.              *
 *                                                                         *
 *   AndroidWorld is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with AndroidWorld.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package android.world.test;

import android.app.Activity;
import android.os.Bundle;
import android.image.*;
import android.world.World;

public class RasterImageTest extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new ImageWorld(new FromURL("http://bryanchadwick.com/images/spongebob.jpg"))
              .bigBang(this);
    }
}

class ImageWorld extends World{
    RasterImage img;
    RasterImage img2;
    
    static RasterImage scale(RasterImage img, int size){
        RasterImage res = new RasterImage(img.width()*size, img.height()*size);
        for(int i = 0; i < res.width(); i++){
            for(int j = 0; j < res.height(); j++){
                int color = img.getPixel(i/size, j/size);
                if(ColorDatabase.argbAlpha(color) > 0){
                    res.setPixel(i, j,
                            ColorDatabase.makeColor(ColorDatabase.argbAlpha(color),
                                                    ColorDatabase.argbRed(color), 
                                                    ColorDatabase.argbGreen(color), 
                                                    ColorDatabase.argbBlue(color)));
                }
            }
        }
        return res;
    }
    static RasterImage gray(RasterImage img){
        RasterImage res = new RasterImage(img.width(), img.height());
        for(int i = 0; i < res.width(); i++){
            for(int j = 0; j < res.height(); j++){
                int color = img.getPixel(i, j);
                int avg = (int)(0.3*ColorDatabase.argbRed(color)+
                                0.6*ColorDatabase.argbGreen(color)+
                                0.1*ColorDatabase.argbBlue(color));
                res.setPixel(i, j, ColorDatabase.argbAlpha(color), avg, avg, avg);
            }
        }
        return res;
    }

    ImageWorld(RasterImage img){
        this.img = ImageWorld.scale(img, 4);
        this.img2 = ImageWorld.gray(this.img);
    }
    
    public Scene onDraw(){
        return this.img.overlayxy(this.img2, this.img.width(), 0).toScene();    
    }
}