package mishin870;
import javax.microedition.lcdui.*;

public class ImageUtils{

public ImageUtils(){
}

public static Image getTiledLayerImage(Image[] imgs,int wid,int hei,int widNum){
if(widNum<=0){
return null;
}
int len = imgs.length;
int widTL = widNum;
int heiTL = len/widTL;
int width = widTL*wid;
int height = heiTL*hei;
Image ret = getAlphaMutableImage(width,height);
Graphics g = ret.getGraphics();
for(int x=0;x<widTL;x++){
	for(int y=0;y<heiTL;y++){
		g.drawImage(imgs[y*widTL+x],x*wid,y*hei,20);
	}
}
return ret;
}

public static void gradient(Graphics g, int red1, int green1, int blue1, int red2, int green2, int blue2, int x_grad, int y_grad, int w_grad, int h_grad) {
float redstep,greenstep,bluestep;
redstep = (float) (red2 - red1) / (float) (w_grad);
greenstep = (float) (green2 - green1) / (float) (w_grad);
bluestep = (float) (blue2 - blue1) / (float) (w_grad);
for (int i = 0; i < w_grad + 1; i++) {
g.setColor((int) (red1 + redstep * i), (int) (green1 + greenstep * i), (int) (blue1 + bluestep * i));
g.drawLine(x_grad + i, y_grad, x_grad + i, y_grad + h_grad);
}
}

public static Image getAlphaImage(int wid,int hei){
if(hei<=0 || wid<=0){
	return null;
}
Image temp = Image.createImage(wid,hei);
int ai[] = getRGB(temp);
for(int i=0;i<ai.length;i++){
	ai[i] = 0x00000000;
}
Image ret = Image.createRGBImage(ai,wid,hei,true);
return ret;
}

public static Image updateToMutable(Image img){
Image ret = Image.createImage(img.getWidth(),img.getHeight());
Graphics g = ret.getGraphics();
g.drawImage(img,0,0,20);
return ret;
}

public static Image updateAlphaInImage(Image img){
int ai[] = getRGB(img);
for(int i=0;i<ai.length;i++){
	if(ai[i]==-1){
		ai[i] = 0;
	}
}
Image ret = Image.createRGBImage(ai,img.getWidth(),img.getHeight(),true);
return ret;
}

public static Image getAlphaMutableImage(int wid,int hei){
if(hei<=0 || wid<=0){
	return null;
}
Image ret = Image.createImage(wid,hei);
Graphics g = ret.getGraphics();
g.drawImage(getAlphaImage(wid,hei),0,0,20);
return ret;
}

public static Image resize_image(Image image, int i, int j)
{
int ai[] = new int[image.getWidth() * image.getHeight()];
ai = getRGB(image);
int ai1[] = reescalaArray(ai, image.getWidth(), image.getHeight(), i, j);
Image image1 = Image.createRGBImage(ai1, i, j, true);
return image1;
}

private static int[] reescalaArray(int ai[], int i, int j, int k, int l)
{
int ai1[] = new int[k * l];
for(int i1 = 0; i1 < l; i1++)
{
int j1 = (i1 * j) / l;
for(int k1 = 0; k1 < k; k1++)
{
int l1 = (k1 * i) / k;
ai1[k * i1 + k1] = ai[i * j1 + l1];
}
}
return ai1;
}

public static Image rotate_image(Image img,int phi){
	int xc = img.getWidth()/2;
	int yc = img.getHeight()/2;
	return rotate_image(img,phi,xc,yc);
}

public static int[] getRGB(Image i){
int[] temp = new int[i.getWidth()*i.getHeight()];
i.getRGB(temp,0,i.getWidth(),0,0,i.getWidth(),i.getHeight());
return temp;
}

public static Image rotate_image(Image img,int phi,int xc,int yc){
	int ImH = img.getHeight();
	int ImW = img.getWidth();
	int ARGB_Img0[];
	int ARGB_Img1[];
	ARGB_Img0 = new int[ImW*ImH];
	ARGB_Img1 = new int[ImW*ImH];
	img.getRGB(ARGB_Img0,0,ImW,0,0,ImW,ImH);
	int x0,y0,x1,y1;
	int sn = MathUtils.sin(phi);
	int cs = MathUtils.cos(phi);
	for (y1=0;y1<ImH;y1++){
		for (x1=0;x1<ImW;x1++){
			ARGB_Img1[y1*ImW+x1] = 0x00ffffff;
			x0=(int) ((cs*(x1-xc)+sn*(y1-yc))/1000+xc);
			y0=(int) (-(sn*(x1-xc)-cs*(y1-yc))/1000+yc);
			if(x0>-1){
				if(x0<ImW){
					if(y0>-1){
						if(y0<ImH){
							ARGB_Img1[y1*ImW+x1] = ARGB_Img0[y0*ImW+x0];
						}
					}
				}
			}
		}
	}
	Image ret = Image.createRGBImage(ARGB_Img1,ImW,ImH,true);
	return ret;
}

}