function attackgui(action);
%/------------------------------------------\
%| ELE 585                                  |
%| Final Project                            | 
%|                                          |
%| Watermarking an Image using blue channel |
%| amplitude modulation                     |
%\------------------------------------------/
%
% University of Rhode Island
% Dept. of Electrical Engineering
% Roger Meier, May '99
%
%
% -------> to start type 'attackgui'
%

if nargin<1,
   action='0';
end
if strmatch('#', action) 
	action=action(2:end);
else
	action='0';
end;


switch action
case '0',
   step0;
case '1',
   step1;
case '2',
   step2;
case 'rotate',
   atrot
case 'rotate2',
   atrot2
case 'rotate3',
   atrot3
case 'crop',
   atcrop
case 'crop3',
   atcrop3
case 'resize',
   atresize
case 'resize2',
   atresize2
case 'resize3',
   atresize3
case 'blur',
   atblur
case 'blur2',
   atblur2
case 'blur3',
   atblur3
case 'sharpen',
   atsharp
case 'sharpen2',
   atsharp2
case 'sharpen3',
   atsharp3
case 'minimum',
   atmin
case 'minimum2',
   atmin2
case 'minimum3',
   atmin3
case 'median',
   atmed
case 'median2',
   atmed2
case 'median3',
   atmed3
case 'maximum',
   atmax
case 'maximum2',
   atmax2
case 'maximum3',
   atmax3
case 'gaussian',
   atgauss
case 'gaussian2',
   atgauss2
case 'gaussian3',
   atgauss3
case 'uniform',
   atuni
case 'uniform2',
   atuni2
case 'uniform3',
   atuni3
case 'saltandpepper',
   atsnp
case 'saltandpepper2',
   atsnp2
case 'saltandpepper3',
   atsnp3
case 'histogram',
   athist
case 'histogram3',
   athist3
case 'jpeg',
   atjpeg
case 'jpeg2',
   atjpeg2
case 'jpeg3',
   atjpeg3
case 'savetif',
   savetif
case 'savejpeg',
   savejpeg
case 'updateslider1',
   updateslider(1);
case 'updateslider2',
   updateslider(2);
case 'show1',
   show(1);
case 'show2',
   show(2);
case 'show2',
   show(2);
case 'updateradio',
   updateradio;
end;    % switch action


% End of function retrievegui


% ================================================= %
%  STEP 0: Initializing GUI and loading image file  %
% ================================================= %
function step0;

% Initializing Figure
% -------------------
screen=get(0,'ScreenSize');
switch computer
case 'PCWIN',
   screen=[3 -20 screen(3)-6 screen(4)];
end
%screen=[100 100 640 480];
figNumber=figure( ...
        'Name','Attack Watermarked Image', ...
        'NumberTitle','off', ...
        'MenuBar','none',...
        'IntegerHandle','off', ...
        'PaperOrientation','Landscape',...
        'Position',screen);

% Creating title bar
% ------------------
h1  = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[0 0.917 1 0.083],...	
                'BackgroundColor',[0.50 0.50 0.50], ...
				'ForegroundColor',[0 0 0]);
h2  = uicontrol('Style','Text',...
                'String','Load Image File...',...
				'Units','Normalized',...
				'HorizontalAlignment','left',...
			    'Position',[0.031 0.934 0.938 0.05],...
                'BackgroundColor',[0.50 0.50 0.50], ...
				'ForegroundColor',[0.9 0.9 0.9],...
			    'FontSize',[18],...
			    'FontWeight','bold');


% Creating bottom bar with controls
% ---------------------------------
h3  = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[0 0 1 0.083],...	
                'BackgroundColor',[0.50 0.50 0.50], ...
				'ForegroundColor',[0 0 0]);
h4  = uicontrol('Style','PushButton',...
                'String','Prev',...
				'Units','Normalized',...
				'Position',[0.031 0.021 0.094 0.042],...
				'Visible','off',...
				'Callback','close; attackgui #0');
h5  = uicontrol('Style','PushButton',...
                'String','Next',...
				'Units','Normalized',...
				'Position',[0.125 0.021 0.094 0.042],...
				'Callback','attackgui #1');
h5a = uicontrol('Style','PushButton',...
                'String','Show Original',...
				'Units','Normalized',...
				'Position',[0.24 0.021 0.16 0.042],...
				'Visible','off',...
				'Callback','attackgui #show1');
h6  = uicontrol('Style','PushButton',...
                'String','Quit',...
				'Units','Normalized',...
			    'Position',[0.875 0.021 0.094 0.042],...
			    'Callback','close');
h7  = uicontrol('Style','Text',...
                'String','Step 0',...
				'Units','Normalized',...
				'HorizontalAlignment','center',...
			    'Position',[0.4 0.021 0.450 0.05],...
                'BackgroundColor',[0.50 0.50 0.50], ...
				'ForegroundColor',[0.9 0.9 0.9],...
			    'FontSize',[18],...
             'FontWeight','bold');
          
% Loading image file and converting it to RGB
% -------------------------------------------
[fname,pname] = uigetfile('*.*','Choose image file to be attacked',100,100);
if fname == 0
	close;
	break;
end
[im,map]=imread([pname fname]);
if size(im,3) ~= 3
	fig.RGB=uint8(255*ind2rgb(im,map));
else
	fig.RGB=im;
end
fig.RGBnew=fig.RGB;

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.fname=fname;
fig.axes=[];
fig.text=[];
fig.topbar=[h1 h2];
fig.botbar=[h3 h4 h5 h6 h7 h5a];
fig.radio=[];
fig.radioframe=[];
fig.radionum=uint8([1 0 0 0 0 0 0 0 0 0 0 0 0]);
fig.slider=[];
fig.button=[];
set(figNumber, 'UserData', fig);

% Go to Step 1
% ------------
attackgui #1;


% ============================= %
%  STEP 1: Entering parameters  %
% ============================= %
function step1;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Select Attack')
set(fig.botbar(2),'Visible','on',...
                  'Callback','close; attackgui #0')
set(fig.botbar(3),'Visible','on',...
                  'Callback','attackgui #2')
set(fig.botbar(5),'String','Step 1')
delete(fig.axes); fig.axes=[];
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end
if length(fig.slider) > 0
	delete(fig.slider);
	fig.slider=[];
end
if length(fig.button) > 0
	delete(fig.button);
	fig.button=[];
end

% Creating menu with radiobuttons
% -------------------------------
posx=0.75; posy=0.1; dx=0.25; dy=0.8;
h11  = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h12 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.04 dx-0.05 0.04],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Select Attack');
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.09 dx-0.05 0.04],...
				'HorizontalAlignment','left', ...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Geometrical Attacks');
r1  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.12 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','rotate',...
				'Callback','attackgui #updateradio');            
r2  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.15 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','crop',...
				'Callback','attackgui #updateradio');            
r3  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.18 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','resize',...
				'Callback','attackgui #updateradio');            
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.23 dx-0.05 0.04],...
				'HorizontalAlignment','left', ...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Filters');
r4  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.26 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','blur',...
				'Callback','attackgui #updateradio');            
r5  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.29 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','sharpen',...
				'Callback','attackgui #updateradio');            
r6  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.32 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','minimum filter',...
				'Callback','attackgui #updateradio');            
r7  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.35 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','median filter',...
				'Callback','attackgui #updateradio');            
r8  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.38 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','maximum filter',...
				'Callback','attackgui #updateradio');            
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.43 dx-0.05 0.04],...
				'HorizontalAlignment','left', ...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Noise Attacks');
r9  = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.46 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','gaussian',...
				'Callback','attackgui #updateradio');            
r10 = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.49 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','uniform (speckle)',...
				'Callback','attackgui #updateradio');            
r11 = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.52 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','salt & pepper',...
				'Callback','attackgui #updateradio');            
h16 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.57 dx-0.05 0.04],...
				'HorizontalAlignment','left', ...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Contrast Enhancement');
r12 = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.60 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','histogram eq',...
				'Callback','attackgui #updateradio');            
h17 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+dy-0.025-0.65 dx-0.05 0.04],...
				'HorizontalAlignment','left', ...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','Text',...
				'FontSize',[10],...
				'FontWeight','bold',...
				'String','Compression');
r13 = uicontrol('Units','normalized',...
				'Position',[posx+0.075 posy+dy-0.025-0.68 dx-0.1 0.03],...
				'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[1 1 1],...
				'Style','RadioButton',...
				'String','JPEG',...
				'Callback','attackgui #updateradio');            
         
% Displaying image file
% ---------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGB))

% Displaying image properties
% ---------------------------
H=size(fig.RGB,1);
W=size(fig.RGB,2);
totalpixels=H*W;
prop=['Filename: ' fig.fname,...
      '     Height: ' num2str(H),...
      '   Width: ' num2str(W),...
      '     Total Pixels: ' num2str(totalpixels)];
h8a = uicontrol('Units','normalized', ...
				'Position',[0.025 0.114 0.7 0.04],...
                'Style','Text',...
             	'BackgroundColor',[0.8 0.8 0.8],...
             'FontSize',[10],...
			    'String',prop);

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.radio=[r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13];
fig.radioframe=[h11 h12 h13 h14 h15 h16 h17];
fig.axes=[h8];
fig.text=[h8a];
set(gcf, 'UserData', fig);
attackgui #updateradio



% ===================================================== %
%  STEP 2: Attacking Image                              %
% ===================================================== %
function step2;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
if length(fig.radio) > 0
	delete(fig.radio); fig.radio=[];
	delete(fig.radioframe); fig.radioframe=[];
end

switch find(fig.radionum);
case 1,
   attackgui #rotate
case 2,
   attackgui #crop
case 3,
   attackgui #resize
case 4,
   attackgui #blur
case 5,
   attackgui #sharpen
case 6,
   attackgui #minimum
case 7,
   attackgui #median
case 8,
   attackgui #maximum
case 9,
   attackgui #gaussian
case 10,
   attackgui #uniform
case 11,
   attackgui #saltandpepper
case 12,
   attackgui #histogram
case 13,
   attackgui #jpeg
end




% ==================== %
%  ROTATE              %
% ==================== %
function atrot;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Rotate')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Angle');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.09,...
				'Min',0,'Max',0.359,...
				'SliderStep',[1/359 10/359],...
				'Callback','attackgui #rotate2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(1000*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #rotate3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atrot2

function atrot2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(1000*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atrot3;

% Get parameters and filter the image
% -----------------------------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Rotate image, and rotate back
%------------------------------
fig.RGBnew=imrotate(fig.RGB,fig.param,'nearest','crop');
fig.RGBnew=imrotate(fig.RGBnew,-fig.param,'nearest','crop');


% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('rotated image, after rotating back')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  CROP                %
% ==================== %
function atcrop;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Crop')

% Creating 'Do it!' button
% ------------------------
posx=0.75; posy=0.314;
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #crop3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.button=h16;
set(gcf, 'UserData', fig);

function atcrop3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
delete(fig.axes);

% Displaying image file
% ---------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGB))
fig.axes=[h8];
drawnow
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Crop image
%-----------
H=size(fig.RGB,1);
W=size(fig.RGB,2);
title('Select rectangle with mouse','FontSize',12,'FontWeight','bold')
ok=0;
while ok == 0
	rect=round(getrect(gca));
	x1=rect(1);
	x2=rect(1)+rect(3);
	y1=rect(2);
	y2=rect(2)+rect(4);
	if and(x1>=1,y1>=1)
		if and(x2<=W,y2<=H)
			if and(x2>x1,y2>y1)
				ok = 1;
			end
		end
	end
end
cropped=imcrop(fig.RGB,rect);
fig.RGBnew=uint8(zeros(H,W,3));
fig.RGBnew(y1:y2,x1:x2,:)=cropped;

% Displaying new image
% --------------------
imshow(uint8(fig.RGBnew))
title('')
xlabel('cropped image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
set(gcf, 'UserData', fig);







% ==================== %
%  RESIZE              %
% ==================== %
function atresize;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Resize')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Scale Factor');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.05,...
				'Min',0.01,'Max',0.2000,...
				'SliderStep',[1/19 5/19],...
				'Callback','attackgui #resize2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(10*get(h14,'Value'),'%0.1f'));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #resize3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atresize2

function atresize2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=10*get(slider,'Value');
set(fig.slider(4),'String',num2str(fig.param,'%0.1f'));
set(gcf, 'UserData', fig);

function atresize3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Resize image, and resize back
% -----------------------------
fig.RGBnew=imresize(fig.RGB,fig.param);
fig.RGBnew=imresize(fig.RGBnew,1/fig.param);


% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('resized image, after resizing back')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  BLUR                %
% ==================== %
function atblur;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Blur')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Filter Size');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.03,...
				'Min',0.02,'Max',0.15,...
				'SliderStep',[1/13 2/13],...
				'Callback','attackgui #blur2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(100*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #blur3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atblur2

function atblur2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(100*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atblur3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Create blur filter
% ------------------
filt=ones(fig.param,fig.param)/fig.param^2;

% Filter RGB image
% ----------------
fig.RGBnew(:,:,1)=filter2(filt,fig.RGB(:,:,1),'same');
fig.RGBnew(:,:,2)=filter2(filt,fig.RGB(:,:,2),'same');
fig.RGBnew(:,:,3)=filter2(filt,fig.RGB(:,:,3),'same');
fig.RGBnew=uint8(fig.RGBnew);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('blurred image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  SHARPEN             %
% ==================== %
function atsharp;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Sharpen')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Strength');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.5,...
				'Min',0,'Max',1,...
				'SliderStep',[0.05 0.1],...
				'Callback','attackgui #sharpen2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(get(h14,'Value'),'%0.2f'));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #sharpen3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atsharp2

function atsharp2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=get(slider,'Value');
set(fig.slider(4),'String',num2str(fig.param,'%0.2f'));
set(gcf, 'UserData', fig);

function atsharp3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Create sharpening filter
% ------------------------
filt=-ones(3,3);
filt(5)=9;

% Filter RGB image
% ----------------
HPF=double(fig.RGB);
HPF(:,:,1)=filter2(filt,double(fig.RGB(:,:,1)),'same');
HPF(:,:,2)=filter2(filt,double(fig.RGB(:,:,2)),'same');
HPF(:,:,3)=filter2(filt,double(fig.RGB(:,:,3)),'same');
fig.RGBnew = (1-fig.param)*double(fig.RGB) + fig.param*HPF;
under=fig.RGBnew < 0;  % setting all elements < 0 to zero
under=find(under);
fig.RGBnew(under)=0;
over=fig.RGBnew > 255; % setting all elements > 255 in new blue channel to 255
over=find(over);
fig.RGBnew(over)=255;
fig.RGBnew=uint8(fig.RGBnew);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('sharpened image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  MINIMUM FILTER      %
% ==================== %
function atmin;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Minimum Filter')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Filter Size');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.03,...
				'Min',0.02,'Max',0.15,...
				'SliderStep',[1/13 2/13],...
				'Callback','attackgui #minimum2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(100*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #minimum3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atmin2

function atmin2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(100*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atmin3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Filter RGB image
% ----------------
fig.RGBnew=ordfilt2(fig.RGB,1,ones(fig.param,fig.param));

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('minimum filtered image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  MEDIAN FILTER       %
% ==================== %
function atmed;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Median Filter')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Filter Size');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.03,...
				'Min',0.02,'Max',0.15,...
				'SliderStep',[1/13 2/13],...
				'Callback','attackgui #median2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(100*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #median3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atmed2

function atmed2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(100*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atmed3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Filter RGB image
% ----------------
fig.RGBnew=ordfilt2(fig.RGB,ceil(fig.param^2/2),ones(fig.param,fig.param));

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('median filtered image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  MAXIMUM FILTER      %
% ==================== %
function atmax;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Maximum Filter')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Filter Size');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.03,...
				'Min',0.02,'Max',0.15,...
				'SliderStep',[1/13 2/13],...
				'Callback','attackgui #maximum2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(100*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #maximum3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atmax2

function atmax2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(100*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atmax3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Filter RGB image
% ----------------
fig.RGBnew=ordfilt2(fig.RGB,fig.param^2,ones(fig.param,fig.param));

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('maximum filtered image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  GAUSSIAN NOISE      %
% ==================== %
function atgauss;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Gaussian Noise')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Variance');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.01,...
				'Min',0,'Max',1,...
				'SliderStep',[1/1000 10/1000],...
				'Callback','attackgui #gaussian2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(10*get(h14,'Value'),'%0.2f'));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #gaussian3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atgauss2

function atgauss2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=10*get(slider,'Value');
set(fig.slider(4),'String',num2str(fig.param,'%0.2f'));
set(gcf, 'UserData', fig);

function atgauss3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Add noise to the image
% ----------------------
fig.RGBnew=imnoise(fig.RGB,'gaussian',0,fig.param);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('image with added gaussian noise')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  UNIFORM  NOISE      %
% ==================== %
function atuni;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Uniform Noise')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Variance');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.01,...
				'Min',0,'Max',1,...
				'SliderStep',[1/1000 10/1000],...
				'Callback','attackgui #uniform2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(10*get(h14,'Value'),'%0.2f'));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #uniform3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atuni2

function atuni2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=10*get(slider,'Value');
set(fig.slider(4),'String',num2str(fig.param,'%0.2f'));
set(gcf, 'UserData', fig);

function atuni3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Add noise to the image
% ----------------------
fig.RGBnew=imnoise(fig.RGB,'speckle',fig.param);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('image with added uniform noise')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% =========================== %
%  SALT AND PEPPER NOISE      %
% =========================== %
function atsnp;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Salt & Pepper Noise')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Density');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.01,...
				'Min',0,'Max',1,...
				'SliderStep',[1/100 10/100],...
				'Callback','attackgui #saltandpepper2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(get(h14,'Value'),'%0.2f'));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #saltandpepper3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atsnp2

function atsnp2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=get(slider,'Value');
set(fig.slider(4),'String',num2str(fig.param,'%0.2f'));
set(gcf, 'UserData', fig);

function atsnp3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Add noise to the image
% ----------------------
fig.RGBnew=imnoise(fig.RGB,'salt & pepper',fig.param);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('image with added salt & pepper noise')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ======================= %
%  HISTOGRAM EQUALIZATION %
% ======================= %
function athist;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savetif')
set(fig.botbar(5),'String','Histogram Equalization')

% Creating 'Do it!' button
% ------------------------
posx=0.75; posy=0.314;
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #histogram3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.button=h16;
set(gcf, 'UserData', fig);

function athist3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Enhance image
%--------------
fig.RGBnew=histeq(fig.RGB);

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('enhanced image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ==================== %
%  JPEG COMPRESSION    %
% ==================== %
function atjpeg;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Attacking Image')
set(fig.botbar(2),'Visible','on',...
                  'Callback','attackgui #1')
set(fig.botbar(3),'Callback','attackgui #savejpeg')
set(fig.botbar(5),'String','JPEG Compression')

% Creating slider for parameter
% -----------------------------
posx=0.75; posy=0.314; dx=0.25; dy=0.17;
h12 = uicontrol('Style', 'Frame', ...
				'Units','Normalized',...
				'Position',[posx posy dx dy],...	
                'BackgroundColor',[0.60 0.60 0.60], ...
				'ForegroundColor',[0 0 0]);
h13 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.09 dx-0.05 0.07],...
                'Style','Text',...
			    'FontSize',[10],...
			    'FontWeight','bold',...
			    'String','Quality');
h14 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.025 dx-0.05 0.04],...
                'Style','slider',...
				'Value',0.75,...
				'Min',0,'Max',1,...
				'SliderStep',[1/100 10/100],...
				'Callback','attackgui #jpeg2');
h15 = uicontrol('Units','normalized', ...
				'Position',[posx+0.025 posy+0.065 dx-0.05 0.045],...
                'Style','Text',...
			    'FontSize',[14],...
			    'String',num2str(100*get(h14,'Value')));

% Creating 'Do it!' button
% ------------------------
h16  = uicontrol('Style','PushButton',...
                'String','Do it!',...
				'Units','Normalized',...
			    'Position',[posx+0.075 posy-0.1 0.1 0.05],...
			    'Callback','attackgui #jpeg3');

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.slider=[h12 h13 h14 h15];
fig.button=h16;
set(gcf, 'UserData', fig);
atjpeg2

function atjpeg2;

% Get parameter and update display
% --------------------------------
fig=get(gcf,'UserData');
slider=fig.slider(3);
fig.param=round(100*get(slider,'Value'));
set(fig.slider(4),'String',num2str(fig.param));
set(gcf, 'UserData', fig);

function atjpeg3;

% Get parameters
% --------------
fig=get(gcf,'UserData');
set(fig.button,'Visible','off')
drawnow
delete(fig.axes);
if length(fig.text) > 0
	delete(fig.text);
	fig.text=[];
end

% Compress Image
%---------------
imwrite(fig.RGB,'temp.jpg','jpg','Quality',fig.param)
fig.RGBnew=imread('temp.jpg');
delete('temp.jpg')

% Displaying new image
% --------------------
h8  = axes('Units','normalized', ...
           'Position',[0.025 0.16 0.7 0.7]);
imshow(uint8(fig.RGBnew))
xlabel('JPEG compressed image')
set(fig.button,'Visible','on')

% Initiating userdata and attach it to the current figure
% -------------------------------------------------------
fig.axes=[h8];
set(gcf, 'UserData', fig);







% ========================== %
%  Saving new Image (TIF)    %
% ========================== %
function savetif;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Saving new Image File...')
set(fig.botbar(2),'Visible','off')
set(fig.botbar(3),'Visible','off')
set(fig.botbar(5),'String','TIF')
set(fig.botbar(6),'Visible','off');

% Saving image file
% -----------------
[fname,pname] = uiputfile('*.tif','Save Image File (TIF)',100,100);
if fname == 0
	attackgui #1;
else
   imwrite(fig.RGBnew,[pname fname],'tif')
   attackgui #1;
end



% ========================== %
%  Saving new Image (JPG)    %
% ========================== %
function savejpeg;

% Recovering user data, updating GUI
% ----------------------------------
fig=get(gcf,'UserData');
set(fig.topbar(2),'String','Saving new Image File...')
set(fig.botbar(2),'Visible','off')
set(fig.botbar(3),'Visible','off')
set(fig.botbar(5),'String','JPG')
set(fig.botbar(6),'Visible','off');

% Saving image file
% -----------------
[fname,pname] = uiputfile('*.tif','Save Image File (JPG)',100,100);
if fname == 0
	attackgui #1;
else
   imwrite(fig.RGB,[pname fname],'jpg','Quality',fig.param)
   attackgui #1;
end



% ========================= %
%  Various Local Functions  %
% ========================= %
   
function updateradio;
   
fig=get(gcf,'UserData');
newnum=zeros(1,length(fig.radio));
for k = 1:length(fig.radio)
   newnum(k)=(get(fig.radio(k),'Value'));
end
newnum=uint8(newnum);
if sum(newnum ~= fig.radionum) ~= 0 
   fig.radionum=xor(newnum,fig.radionum);
end
for k = 1:length(fig.radio);
   set(fig.radio(k),'Value',fig.radionum(k));
end
set(gcf, 'UserData', fig);
