/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *   AndroidWorld Library, Copyright 2011 Bryan Chadwick                   *
 *                                                                         *
 *   FILE: ./android/image/Image.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.image;

import android.world.Posn;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Matrix;

/** An abstract class representing an Image.  See subclasses for
 *    specific implementations. */
public abstract class Image{
    protected static int OUTLINE = 0;
    protected static int SOLID = 1;
    
    public static Paint WHITE, BLACK_OUTLINE, CLEAR;
    static{
        WHITE = Image.painter(color("white"), Image.SOLID);
        BLACK_OUTLINE = Image.painter(color("black"), Image.OUTLINE);
        CLEAR = Image.painter(color("#00000000"), Image.SOLID);
    }
    
    
    /** Pinhole for alignment to other images */
    protected double pinholeX, pinholeY;
    /** Only for subclasses */
    protected Image(double px, double py){
        pinholeX = px;
        pinholeY = py;
    }
    // Calculate Image size to the left/right/up/down of the pinhole
    protected double leftOfPin(){ return pinholeX; }
    protected double rightOfPin(){ return width()-pinholeX; }
    protected double upOfPin(){ return pinholeY; }
    protected double downOfPin(){ return height()-pinholeY; }
    
    /** Overlay the given Image over this Image */
    public Image overlay(Image top)
    { return new Overlay(top,this); }
    /** Overlay the given Images over this Image */
    public Image overlay(Image top, Image next, Image ... imgs){
        return new Overlay(new Overlay(top, make(next, imgs, 0)), this);
    }
    /** Overlay the given Image over this Image offset (x,y) */
    public Image overlayxy(Image top, int x, int y)
    { return new OverlayXY(top,x,y,this); }
    /** Overlay the given Image over this Image offset (x,y) */
    public Image overlayxy(Image top, double x, double y)
    { return overlayxy(top,(int)x,(int)y); }
    /** Overlay the given Image over this Image offset Posn */
    public Image overlayxy(Image top, Posn p)
    { return overlayxy(top,p.x,p.y); }

    /** Overlay the given Image over this Image offset (x,y) */
    public Image overlayxy(int x, int y, Image top)
    { return overlayxy(top,x,y); }
    /** Overlay the given Image over this Image offset (x,y) */
    public Image overlayxy(double x, double y, Image top)
    { return overlayxy(top,(int)x,(int)y); }
    /** Overlay the given Image over this Image offset Posn */
    public Image overlayxy(Posn p, Image top)
    { return overlayxy(top,p.x,p.y); }
    
    /** Make a layered Image with top t and bottoms of imgs[i..] */
    private static Image make(Image t, Image[] imgs, int i){
        if(i == imgs.length)
            return t;
        return new Overlay(t, make(imgs[i], imgs, i+1));    
    }
    
    
    /** Draw this image into a Graphics */
    public abstract void paint(Canvas c, int x, int y);
    /** Calculate the width of this Image */
    public abstract int width();
    /** Calculate the height of this Image */
    public abstract int height();
    
    /** Convert this image into a Scene containing the image without a background */
    public Scene toScene(){
        return new MT(ceil(this.width()+2), ceil(this.height()+2)).placeImage(this, this.pinholeX+1, this.pinholeY+1);
    }
    /** Convert this image into a Scene containing the image with a white background */
    public Scene toWhiteScene(){
        return new White(ceil(this.width()+2), ceil(this.height()+2)).placeImage(this, this.pinholeX+1, this.pinholeY+1);
    }
    /** Represents a truly empty scene... */
    private static class MT extends EmptyScene{
        MT(int w, int h){ super(w,h); }
        /** Leave the graphics empty (translucent) */
        public void paint(Canvas c, int x, int y){}
    }
    /** Represents a truly empty scene... */
    private static class White extends EmptyScene{
        White(int w, int h){ super(w,h); }
        /** Fill with a white background */
        public void paint(Canvas c, int x, int y){
            c.drawRect(x, y, this.width, this.height, Image.WHITE);
        }
    }
    
    /** Save this image to a file */
    public boolean toFile(String name){
        return toScene().toFile(name);
    }
    /** Save this image to a file */
    public boolean toWhiteFile(String name){
        return toWhiteScene().toFile(name);
    }

    /** Convert the mode string into an int representing Outline/Solid */
    protected static int mode(String s){
        if(s.equals("outline")) return OUTLINE;
        if(s.equals("solid")) return SOLID;
        throw new RuntimeException("Mode expected \"solid\" or \"outline\", got: \""+s+"\"");
    }

    /** Round the given Double to an Int */
    protected static int round(double d){
        return (int)Math.round(d);
    }
    /** Round the given Double to an Int */
    protected static int ceil(double d){
        return (int)Math.ceil(d);
    }

    /** Get the java.awt.Color represented by the given string */
    protected static int color(String s){
        return ColorDatabase.color(s);
    }
    
    /** Create a Paint */
    protected static Paint painter(int color, int style){
        Paint p = new Paint();
        p.setColor(color);
        p.setAntiAlias(true);
        p.setStyle(style == Image.OUTLINE?Paint.Style.STROKE:Paint.Style.FILL);
        p.setStrokeWidth(1);
        return p;
    }
    
    /** Return a raster (buffered) version of this Image */
    public RasterImage rasterize(){
        RasterImage img = new RasterImage(ceil(this.width()), ceil(this.height()));
        Canvas c = img.getCanvas();
        this.paint(c, round(this.pinholeX), round(this.pinholeY));
        return img;
    }
    /** Return a rotated version of this image by the given angle in degrees. */
    public Image rotate(int ang){
        return this.rotate((double)ang);
    }
    /** Return a rotated version of this image by the given angle in degrees.
     *  <p>
     *    Any Image may be rotated several times, though for efficiency
     *    (when possible) the rotated image should be saved, rather than
     *    recreated.
     *  </p>
     */
    public Image rotate(double ang){
        ang = (ang + 360.0) % 360.0;
        int max = round(Math.max(Math.max(phDist(0,0),
                                          phDist(0,this.height())),
                                 Math.max(phDist(this.width(),0),
                                          phDist(this.width(),this.height()))));
        Matrix matrix = new Matrix();
        matrix.postRotate(-(float)ang, max, max);
        return this.transform(matrix, max*2, max*2, max, max);
    }
    /** Return a version of this Image scaled by the given int multiplier.
    private RasterImage scale(int s){
        return this.scale((double)s);
    }
    /** Return a version of this Image scaled by the given int X and Y multipliers.
    private RasterImage scale(int sx, int sy){
        return this.scale((double)sx, sy);
    }
    /** Return a version of this Image scaled by the given double multiplier.
    private RasterImage scale(double s){
        return this.scale(s, s);
    }*/
    /** Return a version of this Image scaled by the given double X and Y multipliers. */
    private RasterImage scale(double sx, double sy){
        Matrix matrix = new Matrix();
        matrix.postScale((float)sx, (float)sy);
        return this.transform(matrix, Math.abs(sx*this.width()), Math.abs(sy*this.height()),
                this.width()/2.0/sx, this.height()/2.0/sy);
    }
    /** Return a version of this Image flipped horizontally (left to right). */
    public RasterImage flipHorizontal(){
        return scale(-1.0, 1.0);
    }
    /** Return a version of this Image flipped vertically (top to bottom). */
    public RasterImage flipVertical(){
        return scale(1.0, -1.0);
    }
    /** Return a transformed version of this Image */
    protected RasterImage transform(Matrix newtform, double nw, double nh, double cx, double cy){
        RasterImage img = new RasterImage(round(nw), round(nh));
        Canvas c = img.getCanvas();
        Matrix old = c.getMatrix();
        c.setMatrix(newtform);
        this.rasterize().paint(c, round(cx), round(cy));
        c.setMatrix(old);
        return img;
    }
    /** Maximum distance from the Pinhole to the given XY of this Image */
    private double phDist(double x, double y){
        double dx = x - this.pinholeX,
               dy = y - this.pinholeY;
        return Math.sqrt((dx*dx)+(dy*dy));
    }
}