% Template Script for Image Histogram Equalization
% COSC 190, Spring 2017, Dr. Fred Park, Whittier College
% This code reads in an image and equalizes the histogram for it.
% We use a branching statment that equalizes channel by
% channel in the case of a color image and for black and white images, 
% it equalizes the image outright.
% This script will call the functions:
% my_histcounts() and my_histeq()
% both of which you should have already written
% Computer science is fun!


%************Read in an Image********************
%uncomment to read in the one you want
%f = imread('chapel3.jpg'); %read in chapel image
%f = imread('pout.tif'); %read in pout image
%f = imread('landscape.jpg'); %read in landscape image
%f = imread('field.jpg'); %load the field image
%f = imread('people.png'); %read in people image
f = imread('factory.jpg'); %read in factory image
%f = zeros(5,5,5); %testing for non-image case

numdims =  length(size(f)); %number of dimensions 
numchannels = size(f,numdims); %number of channels

if (numdims == 2) %first branching case. f is graycale.
    
    L = 256; %intensity levels
    B = (0:1:L-1); %bin centers, AKA intensity values

    tic %start timer 
    fp = my_histeq(f,L); % apply histogram equalization
    toc %end timer

    figure(1); %view images
    subplot(1,2,1);
    image(f); colormap(gray(256)); title('Original Image')
    subplot(1,2,2);
    image(fp); colormap(gray(256)); title('Histogram Equalized Image')

    hcf = my_histcounts(f,B); %histcounts for f
    hcfp = my_histcounts(fp,B); %histcounts for fp

    figure(2); %view histograms
    subplot(1,2,1); bar(B,hcf,'r'); title('Original Image')
    subplot(1,2,2); bar(B,hcfp,'r'); title('Histogram Equalized')
    % *** Add plots of the corresponding cumulative distribution functions
    % to the histcounts for orig and processed images
    % with the line y = x super-imposed on each graph
    % you will need to change subplots to subplot(2,2,...) 
    % here ... indicates which place you want the plot to be in the subplot
    % environment
    
elseif ((numdims == 3) && (numchannels == 3)) % i.e. color image
    % &&: denotes and, thus 3-dims and 3-channels for it to be color image
    
    L = 256; % intensity levels
    B = (0:1:L-1); %bin centers, AKA intensity values
    fp = 0*f; %initialize fp to a zero version of f
    Rf = f(:,:,1); Gf = f(:,:,2); Bf = f(:,:,3); %extract channels

    tic %start timer
    fp(:,:,1) = my_histeq(Rf,L); % apply histogram equalization
    fp(:,:,2) = my_histeq(Gf,L); % apply histogram equalization
    fp(:,:,3) = my_histeq(Bf,L); % apply histogram equalization
    toc %end timer

    hcf = my_histcounts(f,B); %hist counts
    hcfp = my_histcounts(fp,B); %hist counts
    
    figure(1); %view images
    subplot(1,2,1); bar(B,hcf,'r'); title('Original Image')
    subplot(1,2,2); bar(B,hcfp,'r'); title('Histogram equalized')
    
    figure(2); %view histograms
    subplot(1,2,1);
    image(f); title('Original Image')
    subplot(1,2,2);
    image(fp); title('Histogram Equalized Image');
    % *** Add plots of the corresponding cumulative distribution functions
    % to the histcounts for orig and processed images
    % with the line y = x super-imposed on each graph
    % you will need to change subplots to subplot(2,2,...) 
    % here ... indicates which place you want the plot to be in the subplot
    % environment
    
else % if other cases fail, it is not an image in the strict sense
     % output an error in this case
    fprintf('******* Error in Image File. Check Your Image!!! *******\n');
end




