package cn.com.origin.autocodemanager.common;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.WritableRaster;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.ImageIcon;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class CacheImage {
	private final Map<String, Image> imageMap = new HashMap<String, Image>();

	private static CacheImage INSTANCE;

	private CacheImage() {
	}

	// 单例模式，获得CacheImage实例
	public static CacheImage getINSTANCE() {
		if (INSTANCE == null)
			INSTANCE = new CacheImage();
		return INSTANCE;
	}

	// 获得图像
	public Image getImage(String applicationID,String imageName) {
		if (imageName == null)
			return null;
		Image image = (Image) imageMap.get(imageName);
		if (image == null) {
			image =AbstractUIPlugin.imageDescriptorFromPlugin(
					applicationID,imageName).createImage();
			imageMap.put(imageName, image);
		}
		
		return image;
	}
	
	// 获得图像
	public Image getImageIco(URL url,String imageName) {
		if (imageName == null)
			return null;
		Image image = (Image) imageMap.get(imageName);
		if (image == null) {
			ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
			image =imageDescriptor.createImage();
			imageMap.put(imageName, image);
		}
		
		return image;
	}

	// 释放图像资源
	public void dispose() {
		Iterator iterator = imageMap.values().iterator();
		while (iterator.hasNext())
			((Image) iterator.next()).dispose();
		imageMap.clear();
	}
	
	/**
     * change ImageIcon to BufferedImage
     * @param icon
     * @return
     */
    public static BufferedImage getBufferedImage(ImageIcon icon){
        int width = icon.getIconWidth();
        int height = icon.getIconHeight();
        ImageObserver observer = icon.getImageObserver();
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
       Graphics gc = bufferedImage.createGraphics(); 
       gc.drawImage(icon.getImage(), 0, 0, observer);
        return bufferedImage;
    }
    
   /**
     * change BufferedImage to ImageData
     * @param bufferedImage
     * @return
     */
    public static ImageData getImageData(BufferedImage bufferedImage){
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel
                .getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel
                .getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    }
    
    
    public void testSwingToSwtImage()
    {
    	   ImageIcon icon = new ImageIcon("C:\\temp\\freshwind.jpg");
           BufferedImage bufferedImage = getBufferedImage(icon);
           ImageData data = getImageData(bufferedImage);
           Image image = new Image(null, data);
      
    }
}