import java.awt.*;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.plugin.PlugIn;

/** This a prototype ImageJ plug-in. */
public class Translation_ implements PlugIn {

	public void run(String arg) {
		ImagePlus imp1 = WindowManager.getCurrentImage();
		int prof=imp1.getBitDepth();
		if (prof != 8)
		{
			IJ.error("L'image doit être du type 8 bits,\nconvertissez la avant");
		return;
	}

		int ht=imp1.getHeight();
		int lg=imp1.getWidth();
		ImageProcessor ip;
		ByteProcessor ip2=null;
		if (imp1==null) {
			ip = new ByteProcessor(lg, ht);
			ip.setColor(Color.red);
			ip.fill();
		} else {
			ip = imp1.getProcessor();
			ip.setRoi(null);
			ip = ip.crop(); // clone pixel data
			byte[] pixels = (byte[])ip.getPixels();
			ip2=new ByteProcessor(lg,ht);
	int largeur = ip.getWidth();
		int hauteur=ip.getHeight();
		int taille=largeur*hauteur;
		byte[] pixels2=new byte[taille];

	int pixi;
	for (int jj=0;jj<taille;jj++){
		pixi=0;
	pixels2[jj]=(byte)(pixi&0xFF);
}
		int xOffset, yOffset,i;
	GenericDialog gd = new GenericDialog("Translations");
gd.addNumericField("en x:",10.0,0);
gd.addNumericField("en y:",10.0,0);
gd.showDialog();
if (gd.wasCanceled()) {
IJ.error("PlugIn canceled!");
return;
}
		xOffset=(int) gd.getNextNumber();
		yOffset=(int) gd.getNextNumber();
		int offset;
		int vxOffset;
		int j;
		int xOrig,yOrig,xFin,yFin;
		xOrig=Math.max(0,xOffset);
		yOrig=Math.max(0,yOffset);
		xFin=Math.min(largeur,largeur+xOffset);
		yFin=Math.min(hauteur,hauteur+yOffset);
		for (int y=yOrig; y<yFin; y++) {
			offset = y*largeur;
			vxOffset=(y-yOffset)*largeur;
			for (int x=xOrig; x<xFin; x++) {
				i = offset + x;
				j=vxOffset+x-xOffset;
				pixi=pixels[j]&0xff;
				pixels2[i] =(byte) pixi;
			}
		ip2.setPixels(pixels2);
}
		}

		ImagePlus imp2 = new ImagePlus("Richard", ip2);
imp2.show();
	}

}

