//import edu.toronto.psi.vincent.util.*;
import java.io.*;

/**
 * Sample code for using the ImageReaderWriter class to read multidimensional arrays from and write multidimensional arrays to an image file.
 *
 * <pre>
 * Copyright (C) 2005  Vincent Cheung (vincent@psi.toronto.edu, http://www.psi.toronto.edu/~vincent/)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.</pre>
 * 
 * @author <a href="mailto:vincent@psi.toronto.edu">Vincent Cheung</a>
 * @version 1.0 11/23/05
 */
public class ImageReaderWriterExample {
	public static void main(String[] args) {
		
		try {
			
			// ---- Basic read and write ----
			
			// read an image as an int array
			int[][][] x = ImageReaderWriter.readInt("plant0.png");
		
			// write the image to a new file as a jpg image (converting the png to jpg)
			ImageReaderWriter.write(x, "Converted.jpg");
			


			// ---- Read into a double array and sample the image in the simplest way possible ----
			
			// read the video frames as a double array with each pixel normalized between 0 and 1.
			double[][][][] y = ImageReaderWriter.read("plant", ".png", 3);
			
			// resize the video by sampling every second element
			double[][][][] z = new double[y.length][(int)y[0].length/2][(int)y[0][0].length/2][y[0][0][0].length];
			
			for (int t = 0; t < z.length; t++)
				for (int i = 0; i < z[0].length; i++)
					for (int j = 0; j < z[0][0].length; j++)
						for (int k = 0; k < z[0][0][0].length; k++)
							z[t][i][j][k] = y[t][i*2][j*2][k];
			
			// write the sampled image, the necessary directories are created
			ImageReaderWriter.write(z, "Results/Small", ".png");



			// ---- Demonstrate writing gray scale images ----

			// separate the red, green, and blue channels from the first image
			int[][][] separate = new int[x[0][0].length][x.length][x[0].length];
			
			for (int i = 0; i < x.length; i++)
				for (int j = 0; j < x[0].length; j++)
					for (int k = 0; k < x[0][0].length; k++)
						separate[k][i][j] = x[i][j][k];
			
			// write each of the RGB channels out separately as gray scale images
			ImageReaderWriter.write(separate[0], "Results/Red.png");
			ImageReaderWriter.write(separate[1], "Results/Green.png");
			ImageReaderWriter.write(separate[2], "Results/Blue.png");
			
		} catch (IOException e) {
			System.out.println(e);
		}

	}
} // end ImageReaderWriterExample class
