function varargout = view_dwt(varargin)
% VIEW_DWT M-file for view_dwt.fig
%      VIEW_DWT, by itself, creates a new VIEW_DWT or raises the existing
%      singleton*.
%
%      H = VIEW_DWT returns the handle to a new VIEW_DWT or the handle to
%      the existing singleton*.
%
%      VIEW_DWT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in VIEW_DWT.M with the given input arguments.
%
%      VIEW_DWT('Property','Value',...) creates a new VIEW_DWT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before view_dwt_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to view_dwt_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help view_dwt

% Last Modified by GUIDE v2.5 06-Nov-2007 16:29:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @view_dwt_OpeningFcn, ...
                   'gui_OutputFcn',  @view_dwt_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before view_dwt is made visible.
function view_dwt_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to view_dwt (see VARARGIN)

% Choose default command line output for view_dwt
handles.output = hObject;

N=128;
W=zeros(128,128);
handles.W=W;
handles.N=N;
handles.Jmin=7-3;

% Update handles structure
guidata(hObject, handles);

redraw(hObject,[],handles);


% UIWAIT makes view_dwt wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = view_dwt_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

function axes2_ButtonDownFcn(hObject, eventdata, handles)


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


N=handles.N;
Jmin=handles.Jmin;

axes(handles.axes1);
[j,i]=ginput(1);
j=max(1,min(N,round(j)));
i=max(1,min(N,round(i)));
W=handles.W;%zeros(N,N);
W(i,j)=255;

handles.W=W;
guidata(hObject, handles);
redraw(hObject,[],handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N=handles.N;
Jmin=handles.Jmin;

axes(handles.axes1);
W=handles.W;%zeros(N,N);
show_wavelet(W,Jmin,'real',1);
W=roipoly*255;

W=W+handles.W;

handles.W=W;
guidata(hObject, handles);
redraw(hObject,[],handles);

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N=handles.N;
Jmin=handles.Jmin;

axes(handles.axes2);
I=zeros(N,N);
I=roipoly*255;

options=struct('wavelet_type','biorthogonal','vm',3);
[W]=perform_wavelet_transform(I, Jmin, 1, options);

handles.W=W;
guidata(hObject, handles);
redraw(hObject,[],handles);


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N=handles.N;
Jmin=handles.Jmin;

axes(handles.axes2);
I=zeros(N,N);
I=roipoly*255;

[X,Y]=meshgrid(1:N,1:N);

I=I.*sin(2*pi*sqrt(X.^2+Y.^2)/13);

options=struct('wavelet_type','biorthogonal','vm',3);
[W]=perform_wavelet_transform(I, Jmin, 1, options);

handles.W=W;
guidata(hObject, handles);
redraw(hObject,[],handles);



function redraw(hObject, eventdata, handles)

W=handles.W;
Jmin=handles.Jmin;

axes(handles.axes1);
show_wavelet(W,Jmin,'real',1);
axis('image');
drawnow

options=struct('wavelet_type','biorthogonal','vm',3);
[R]=perform_wavelet_transform(W, Jmin, -1, options);

axes(handles.axes2);
imagesc(R);
colormap(gray(256));
%colorbar;
drawnow







% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


handles.W=zeros(handles.N);
guidata(hObject, handles);
redraw(hObject,[],handles);


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile({'*.jpg;*.png;*.gif;*.bmp', 'Image file'; ...
    '*.*','Any file'});
if isequal(filename,0) || isequal(pathname,0)
    disp('Image import canceled...')
else
    name=fullfile(pathname, filename);
    disp(['Importing image ', name,'...'])
    
    I=imread(name);
    if (ndims(I)==3)
        I=double(rgb2gray(I));
    else
        I=double(I);
    end
    N=handles.N;
    I=imresize(I,[N,N],'bicubic');
    
    Jmin=handles.Jmin;
    options=struct('wavelet_type','biorthogonal','vm',3);
    [W]=perform_wavelet_transform(I, Jmin, 1, options);
    
    handles.W=W;
    guidata(hObject, handles);
    redraw(hObject,[],handles);
end

