/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime;

import javax.microedition.lcdui.Graphics;
import minime.core.ResourceManager;

/**
 * The Image class is used to hold graphical image data. It can create image by
 * image id, or a sourde javax.microedition.lcdui.Image instance,or image uri.If
 * create image by image id, it will get image data from image resource file
 * using ResourceManager file.
 * 
 * @author aaa
 * 
 */
public class Image //extends Resource
{
    private javax.microedition.lcdui.Image nativeImage;
    int rscId ;
    
    // static final Logger LOG = Logger.getLogger( "minime.Image" );

    /**
     * Creates an image object from a source image.
     * 
     * @param image
     */
    public Image(javax.microedition.lcdui.Image image)
    {
        nativeImage = image;
        rscId = -1 ;
    }

    /**
     * Create an instance image, it accesses ResourceManager to get the image
     * data from resource file(.bin) based on image resource id.
     * 
     * @param imRscId
     *            the image resource id
     */
    public Image(int imRscId)
    {
        rscId = imRscId ;
        nativeImage = null;
    }

    /**
     * Checks if the native image is null.
     * 
     * @return true if the image is not null, false otherwise
     */
    public boolean realized()
    {
        return nativeImage != null;
    }

    /**
     * Purges the native image to null.
     */
    public void purge()
    {
        nativeImage = null;
    }

    /**
     * Gets the height of the native image in pixels. The value returned must
     * reflect the actual height of the image when rendered.
     * 
     * @return height of the image
     */
    public int getHeight()
    {
        if (nativeImage == null && !realize())
        {
            return -1;
        }

        return nativeImage.getHeight();
    }

    /**
     * Gets the width of the native image in pixels. The value returned must
     * reflect the actual width of the image when rendered.
     * 
     * @return width of the image
     */
    public int getWidth()
    {
        if (nativeImage == null && !realize())
        {
            return -1;
        }

        return nativeImage.getWidth();
    }

    /**
     * Draws the image at x,y coordinates.The anchor point for positioning the
     * image is Graphics.TOP | Graphics.LEFT
     * 
     * @param gc
     *            the Graphics object to be used for rendering image
     * @param x
     *            the x coordinate of the anchor point
     * @param y
     *            the y coordinate of the anchor point
     */
    public void draw(Graphics gc, int x, int y)
    {
        if (nativeImage == null && !realize())
        {
            return;
        }

        gc.drawImage(nativeImage, x, y, Graphics.TOP | Graphics.LEFT);
    }

    /**
     * Renders a series of RGB+transparency values in a specified region
     * 
     * @param gc
     *            the Graphics object to be used for rendering image
     * @param x
     *            the x coordinate of the anchor point
     * @param y
     *            the y coordinate of the anchor point
     * @param alf
     *            the value of alpha
     */
    public void drawAlf(Graphics gc, int x, int y, int alf)
    {
        if (nativeImage == null && !realize())
        {
            return;
        }

        int argb[] = new int[nativeImage.getWidth() * nativeImage.getHeight()];
        nativeImage.getRGB(argb, 0, nativeImage.getWidth(), 0, 0, nativeImage.getWidth(), nativeImage.getHeight());
        for (int i = 0; i < argb.length; i++)
        {
            argb[i] = (alf << 24) | (argb[i] & 0x00FFFFFF);
        }

        gc.drawRGB(argb, 0, nativeImage.getWidth(), x, y, nativeImage.getWidth(), nativeImage.getHeight(), true);
    }

    /**
     * Copies a region of the native image to a location within the destination.
     * 
     * @param gc
     *            the Graphics object to be used for rendering image
     * @param srcX
     *            the x coordinate of the upper left corner of the region within
     *            the native image to copy
     * @param srcY
     *            the Y coordinate of the upper left corner of the region within
     *            the native image to copy
     * @param srcW
     *            the width of the region to copy
     * @param srcH
     *            the height of the region to copy
     * @param x
     *            the x coordinate of the upper left corner of the destination
     *            drawing area
     * @param y
     *            the y coordinate of the upper left corner of the destination
     *            drawing area
     */
    public void draw(Graphics gc, int srcX, int srcY, int srcW, int srcH, int x, int y)
    {
        if (nativeImage == null && !realize())
        {
            return;
        }

        gc.drawRegion(nativeImage, srcX, srcY, srcW, srcH, 0, x, y, Graphics.TOP | Graphics.LEFT);
    }

    /**
     * Creates native image instance(javax.microedition.lcdui.Image)
     * 
     * @return false if failed. true if successful.
     */
    private boolean realize()
    {
        nativeImage = ResourceManager.getImage(rscId);
        return (null!=nativeImage) ;
    }
}
