package com.dolejsky.imagemanipulation;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageManipulation {
	private static final String GIF = ".gif";
	private static final String INPUT_FOLDER = whatever_path_here; 

	static class RGB {
		private int alpha;
		private int red;
		private int green;
		private int blue;
		
		private float hue;
		private float sat;
		private float brigh;

		public RGB(int pixel) {
			alpha = (pixel >> 24) & 0xff;
			red   = (pixel >> 16) & 0xff;
			green = (pixel >>  8) & 0xff;
			blue  = (pixel      ) & 0xff;
			
			float hsb[] = Color.RGBtoHSB(red, green, blue, null);

			hue = hsb[0];
			sat = hsb[1];
			brigh = hsb[2];
		}

		/**
		 * @return an alpha value (0-255)
		 */
		public int getAlpha() {
			return alpha;
		}

		/**
		 * @return an red value (0-255)
		 */
		public int getRed() {
			return red;
		}

		/**
		 * @return an green value (0-255)
		 */
		public int getGreen() {
			return green;
		}

		/**
		 * @return an blue value (0-255)
		 */
		public int getBlue() {
			return blue;
		}

		/**
		 * @return an hue value (0.0-1.0)
		 */
		public float getHue() {
			return hue;
		}

		/**
		 * @return an saturation value (0.0-1.0)
		 */
		public float getSat() {
			return sat;
		}

		/**
		 * @return an brightness value (0.0-1.0)
		 */
		public float getBrigh() {
			return brigh;
		}
		
	}
	
	public static int rgb(int r, int g, int b) {
		 int alpha = 0xff << 24;
		 g = (g & 0xff) << 8; 
		 r = (r & 0xff) << 16; 
		 b = (b & 0xff); 
		 
		 return alpha | r | g | b;
	 }

	public static void main(String[] args) throws IOException, InterruptedException {
	    File dir = new File(INPUT_FOLDER);
	    
	    File[] children = dir.listFiles(new FilenameFilter(){

			@Override
			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(GIF);
			}});
	    if (children == null) {
	        // Either dir does not exist or is not a directory
	    	System.out.println("cannot process directory: "+INPUT_FOLDER);
	    } else {
	        for (int i=0; i<children.length; i++) {
	        	process(children[i].getAbsolutePath());
	        }
	    }
		
	}

	private static void process(String filename) throws IOException, InterruptedException {
    	System.out.println("processing: "+filename+"...");

    	BufferedImage sshot = ImageIO.read(new File(filename));

		PixelGrabber grabber = new PixelGrabber(sshot, 0, 0, -1, -1, true);

		if (grabber.grabPixels()) {
			int w = grabber.getWidth();
			int h = grabber.getHeight();

			System.out.println("  size of image: "+w+" x "+h);

			if (grabber.getPixels() instanceof byte[]) {
				byte[] data = (byte[]) grabber.getPixels();
				System.out.println("  grayscale images not supported, skipping!");
			} else {
				int[] data = (int[]) grabber.getPixels();

				BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

				for (int y = 0; y < h; y++) {
				    for (int x = 0; x < w; x++) {
						RGB rgb = new RGB(data[y * w + x]);
						
						result.setRGB(x, y, transformPixel(rgb));
				    }
				}
				
				int i = filename.lastIndexOf("\\");
				String outFileName = filename.substring(0, i) + "\\processed\\" + filename.substring(i+1);
				File file = new File(outFileName);
		        try {
		        	System.out.println("  writting "+file.getAbsolutePath()+"...");
		            ImageIO.write((RenderedImage) result, "gif", file);
		        } catch (IOException ioe) {
		            ioe.printStackTrace();
		        }		
			}
		}
		
		System.out.println();
	}

	private static int transformPixel(RGB rgb) {
		//this is dummy transformation that doesn't really do anything useful... 
		return Color.HSBtoRGB(rgb.getHue(),rgb.getSat(),rgb.getBrigh());
	}
}

