import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class ImageLoader {

	public static int[] loadImage(String urlString){

		int[] pixels = null;
		int[] grays = null;

		try{
			URL url = new URL(urlString);
			BufferedImage image = ImageIO.read(url);
			pixels = image.getRGB(0, 0, image.getWidth(), image.getWidth(), null, 0, image.getWidth());

		}catch(IOException e){
			System.out.println("Image could not be loaded.");
		}
		grays = new int[pixels.length];

		for(int i = 0; i<grays.length; i++){

			Color color = new Color(pixels[i]);
			int red = color.getRed();
			int green = color.getGreen();
			int blue = color.getBlue();

			grays[i] = (int)(red+green+blue)/3;
		}

		return ImageLoader.normalizeArray(grays);

	}

	private static int[] normalizeArray(int[] a){
		int min = 255;
		for(int i = 0; i < a.length; i++){
			if(a[i]<min)
				min = a[i];
		}

		for(int i = 0; i < a.length; i++){
			a[i] = a[i] - min;

		}

		return a;

	}

}
