package buntil;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.awt.image.ReplicateScaleFilter;
import java.io.File;
import java.io.IOException;

public class ILoader {
	public static Image loader(String name)
	{
		Image bf = null;
		try
		{
			bf = javax.imageio.ImageIO.read(new File(name));
		}catch(IOException e)
		{


		}
		return bf;
	}
	
	public static Image[] iL(String[] images)
	{
		
		Image[] selfa = new Image[images.length];
		for (int a = 0; a <selfa.length; a++)
		{
			selfa[a] = ILoader.loader(images[a]);
		}
		return selfa;
	}
	
	public static Image scaler(Image source, double scale1, double scale2, ImageObserver iO)
	{
		ImageFilter next = new ReplicateScaleFilter((int)(source.getWidth(iO)*scale1), (int)(source.getHeight(iO)* scale2));
		ImageProducer produce = new FilteredImageSource(source.getSource(), next);		
		Image Imagea = Toolkit.getDefaultToolkit().createImage(produce);
		return Imagea;
	}
	public static Image[] flip(Image[] imagea, ImageObserver iO)
	{
		Image[] newa = new Image[imagea.length];
		for(int a = 0; a<imagea.length; a++)
		{
			try 
			{
				
				int widthImage = imagea[a].getWidth(iO);
				int heightImage = imagea[a].getHeight(iO);
				int[] pixelsImage = new int[widthImage * heightImage];
				PixelGrabber pig;
				pig = new PixelGrabber(imagea[a], 0, 0, widthImage, heightImage,
						pixelsImage, 0, widthImage);
				if (pig.grabPixels()) 
				{
					int[] pix = colFlipPixels(pixelsImage, widthImage, heightImage);
					MemoryImageSource mis = new MemoryImageSource(widthImage,
					heightImage, pix, 0, widthImage);
					newa[a] = Toolkit.getDefaultToolkit().createImage(mis);
				}
			}catch(Exception e)
			{
			}
		}
		return newa;
	}
	
	public static int[] colFlipPixels(int pixels[], int width, int height) 
	{
		int newPixels[] = null;
		if ((width * height) == pixels.length) 
		{
			newPixels = new int[width * height];
			int newIndex = 0;
			for (int y = 0; y < height; y++)
				for (int x = width - 1; x >= 0; x--)
					newPixels[newIndex++] = pixels[y * width + x];
		}
		return newPixels;
	}

	public static AffineTransform dri(Graphics a, Image sourcea, int x, int y, double angle)
	{
		AffineTransform ATAT = new AffineTransform();
		ATAT.setToTranslation(x,y);
		ATAT.rotate(Math.toRadians(angle));
		return ATAT;
	}
	
}
