clear all; close all; clc;
% Octave 4.2.2: you might need to execute 'pkg load image' in the command line

% Reading image file and converting it to grayscale ([0,255])
  img_file = 'uchip.jpg'; %img_file = 'frog.jpg', 'lena.bmp', 'bird.jpg'
  IM = rgb2gray(imread(img_file)); % IM is uint8.
  [sY, sX]  = size(IM);  % sY: # of rows, sX: # of columns
  
fname_i = 'uchip.bif'; % Input binary file

t = 1; % t =1 --> dilation. t = 2 --> erosion

% Defining the kernel and the output binary file name.
if t == 1
    fname_o = 'uchip_d.bof'; % Output binary file (dilation)
else
    fname_o = 'uchip_e.bof'; % Output binary file (erosion)
end

% Performing grayscale dilation/erosion in MATLAB
  figure; imshow(IM,[]); title ('Input image');
  se = strel('disk',2); % structural element
  
% Image dilation (MATLAB):
  DIM = imdilate(IM,se);
  figure; imshow(DIM,[]); title 'Output image: dilation');

% Image erosion (MATLAB):
  EIM = imerode(IM,se);
  figure; imshow(EIM,[]); title ('Output image: erosion');

 test = 0;
 if (test == 1) 
      % ******************************************
      % Grayscale image dilation (instead of imdilate):
        SE = [0 0 1 0 0; 0 1 1 1 0; 1 1 1 1 1; 0 1 1 1 0; 0 0 1 0 0];
        % SE = se
        [kY,kX] = size(SE);
        % Center position of SE (half of kernel size, flooring)
        kCX = floor (kX/2); kCY = floor (kY/2);
        
        dIM = zeros (sY,sX);
        for i = 1:sY % over rows
            for j = 1:sX % over columns
                t = 0;
                for m = 1:kY
                    for n = 1:kX
                        i_I = (i-1) + (m-kCY);
                        j_I = (j-1) + (n-kCX);
                        if SE(m,n) == 1
                            if (i_I > 0 && i_I <= sY && j_I > 0 && j_I <= sX)
                                %if IM(i_I,j_I) >= dIM(i,j)
                                    %dIM(i,j) = IM(i_I,j_I);
                                 if IM(i_I,j_I) >= t
                                    t = IM(i_I,j_I);
                                end
                            end
                        end
                    end
                end
                dIM(i,j) = t;
            end
        end

      dd = DIM - uint8(dIM);
      
      % *******************************************
      % Grayscale image erosion (instead of imdilate):
        eIM = IM; % initial value.
        for i = 1:sY % over rows
            for j = 1:sX % over columns
                for m = 1:kX
                    for n = 1:kY
                        i_I = (i-1) + (m-kCY);
                        j_I = (j-1) + (n-kCX);
                        if SE(m,n) == 1
                            if (i_I > 0 && i_I <= sY && j_I > 0 && j_I <= sX)
                                if IM(i_I,j_I) <= eIM(i,j)
                                    eIM(i,j) = IM(i_I,j_I);
                                end
                            end
                        end
                    end
                end
            end
        end
    %  
      ee = EIM - uint8(eIM);  
 end 
  
disp ('ECE4900: Lab 4 - Image test');
disp ('1. Create .bif file that stores image in a binary file as a row vector');
disp ('2. Compare .bof file (generated) with the MATLAB processed image');
disp ('Any other number to exit');

while(1)
    method = input ('Select Operator (1-2)', 's');
    switch method
        case '1'
            B = reshape (IM', 1,sY*sX); % raster scan into row vector
            fid = fopen (fname_i, 'w'); % bif: binary image file
            fwrite (fid, B, 'uint8');
            fclose (fid);
            
        case '2'
            % Opening output file:
            %   Note that each element is interpreted as a 32-bit integer value.
              fod = fopen (fname_o, 'r');
              raster_in = fread(fod, sY*sX, 'uint8=>uint8');
              
            % OMr: 2D array of 32-bit signed integers.
              OMr = reshape (raster_in, sX, sY)';
            
            % uint8(OMr): Output image (from Terasic board) saturated to [0 255]
            figure; imshow(uint8(OMr), []);  title ('Output image (Terasic board): [0, 255]');
            
            % Comparison between MATLAB convolution and your C code:
            % OMr: data from the output binary file: uint8
            if t == 1
                diff = uint8(DIM) - OMr;
            else
                diff = uint8(EIM) - OMr;
            end
            
            figure; imshow( abs(double(diff)) , []); title ('difference');
            fclose(fod);
            break;
        otherwise
            disp ('unknown option, bye!'); break;
    end
end
