%% Script Name: pex2
%% Purpose: 
%   To create a smooth program to manipulate an image in multiple ways. 
%
%% Record of Revisions:
%   Programmer:     Matthew Karges
%   Date Due:       3/9/2011
%   Date Turned In: 3/9/2011
%   Help Recieved:  None
%   Resources used: www.mathworks.com
%
%% Variables used:
%   MyImage     matrix, original image selected from file
%   operation   scalar, value of the button selected from the menu
%   New         matrix, the current image 
%   Scale       matrix, a black image used to imprint scaled and cropped 
%                       images
%   x           scalar, value from 0 to 1 that the user inputs to scale the
%                       image horizontally
%   y           scalar, value from 0 to 1 that the user inputs to scale the
%                       image vertically
%   Current     empty matrix, a variable to imprint the rotated image
%   ytop        scalar, lowest (top) pixel value on the y axis to crop 
%   ybottom     scalar, largest (bottom) pixel value on y axis to crop
%   xleft       scalar, lowest (most left) pixel value on x axis to crop
%   xright      scalar, highest (most right) pixel value on x axis to crop
%
%% Assumptions:
%   In order for this program to work, the picture needs to be a .jpg file.
%   The scaling factor needs to be greater than 0 and less than or equal
%   to 1. For the Crop Image button, the pixel values need to be 
%   non-negative integers, the top pixel value needs to be less than the bottom 
%   pixel value, and the left pixel value needs to be less than the right 
%   pixel value. 
%
%% Initialize Program
% Set up parameters and initialize variables

% clear workspace and command window
clc
clear

% Get picture from file and set to a variable
[Photo_name, Photo_dir] = uigetfile('*.jpg','Select Photo for Processing');
MyImage = imread([Photo_dir Photo_name]);

% Show image
image(MyImage);
axis image;

% Set initial variables
operation=0;
New=MyImage;

%% Program Execution
% Execute the program commands

% Create loop to manipulate picture multiple times
while operation~=7;

% Set up menu for user to choose manipulation
    operation=menu('Select an Option','Original Picture',...
        'Flip Horizontally','Flip Vertically','Rotate 90 degrees',...
        'Scale Image','Crop Image','Exit');
    [rows columns tints] = size(New);

% Command for user prompt
    switch operation;
        
        case 1
% Original Image
            image(MyImage);
            colormap(lines);
            axis image;
            New=MyImage;
            
        case 2
% Flip Horizontally
            for i=1:3;
                New(:,:,i)=fliplr(New(:,:,i));
            end
            image(New);
            axis image;
            
        case 3
% Flip Vertically
            for i=1:3;
                New(:,:,i)=flipud(New(:,:,i));
            end
            image(New);
            axis image;
            
        case 4
% Rotate 90 degrees
            for i=1:3;
                Current(:,:,i)=rot90(New(:,:,i));
            end
            image(Current);
            axis image;
            New=Current;
            clear Current;
            
        case 5
% Scale Image to a fraction of its size
            Scale=ones(rows,columns,3,'uint8');
            x=input('Enter a horizontal scaling factor between 0 and 1: ');
            y=input('Enter a vertical scaling factor between 0 and 1: ');
            for i=1:rows;
                for j=1:columns;
                    Scale(ceil(i*y),ceil(j*x),:)=New(i,j,:);
                end
            end
            image(Scale);
            axis image;
            New=Scale;
            
        case 6
% Crop Image to a portion of the picture
            Scale=zeros(rows,columns,3,'uint8');
            ytop=input('Enter the top pixel value: ');
            ybottom=input('Enter the bottom pixel value: ');
            xleft=input('Enter the left pixel value: ');
            xright=input('Enter the right pixel value: ');
            for i=xleft:xright;
                for j=ytop:ybottom;
                    Scale(j,i,:)=New(j,i,:);
                end
            end
            image(Scale);
            axis image;
            New=Scale;
            
        case 7
% Exit the program
            close all;
            clc;
            
    end
end
