import java.awt.Color;

/******************************
 * Least Significant Bit Encoding and Decoding
 * CSC 350 Fall 2008
 * <Your Name Here> 
 *
 * This class will bring in an image, and either encode
 * a message in the least significant bit of the pixels,
 * or decode a message that is already there.
 * For encoding, you must load a jpg, and save it as a png.
 * For decoding, you must load a png
 ******************************/
public class LSB {

    private Picture p;
    private String ctype;
    private String message;
    private String saveFile;

    /*************
     * The constructor for an LSB object. It brings in the 
     * command line arguments and sets up the data members
     *************/
    public LSB (String[] args) {
        try {
            // get the picture from the user
            p = new Picture(args[0]);

            // get the conversion type from the user, 
            // E for Encode or D for Decode
            ctype = args[1];
    
            // if encrypting, 
            if (ctype.equals("E")) {
        
                // get the message from the user  
                message = args[2];
        
                // get name of file for saving after encoding
                String saveFile = args[3];
            }
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            System.out.println("Usage: java LSB <fileToRead> <codingType> <message> <fileToSave>");
            System.out.println("  where codingType is E for Encode or D for Decode");
            System.out.println("        message is a string enclosed in \"\"");
            System.exit(-1);    
        } catch (RuntimeException re) {
        	System.out.println(args[0] + " cannot be found to read.");
			System.exit(-1);
		}
    }
    
    /*************
     * Encodes or decodes the image.  This is where you will
     * need to add code, based on the algorithm discussed in class
     * and in the book on page 11.  You will need to use the Picture class
     * also provided, and the Color class from the standard java API.
     *************/
    public void convert() {

        // ***ADD CODE HERE*** 
        // Turn message into bits if we're encoding

        // analyze the image accordingly
        for (int i = 0; i < p.width(); i++) {
            for (int j = 0; j < p.height(); j++) {
                Color c = p.get(i, j);

                // ***ADD CODE HERE*** 
                // if decoding, get the least significant bit from each color in each pixel, RGB
                // and convert them to ascii characters. 

                // ***ADD CODE HERE*** 
                // otherwise if encoding, change the color based on the ith bit in the 
                // message bitstring. Use the set method of the Picture p
            }
        
        }
        
        // save images as new if encoding
        if (ctype.equals("E")) {
            p.save(saveFile);
        }
    }
    
    /*************
     * The main method for our class.  We pass the args to create
     * an LSB object, then call the convert method to process the data.
     *************/
    public static void main(String[] args) {
        LSB lsb = new LSB(args);
        lsb.convert();    
    }
}
