function varargout = manipulate_dwt(varargin)
% MANIPULATE_DWT M-file for manipulate_dwt.fig
%      MANIPULATE_DWT, by itself, creates a new MANIPULATE_DWT or raises the existing
%      singleton*.
%
%      H = MANIPULATE_DWT returns the handle to a new MANIPULATE_DWT or the handle to
%      the existing singleton*.
%
%      MANIPULATE_DWT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MANIPULATE_DWT.M with the given input arguments.
%
%      MANIPULATE_DWT('Property','Value',...) creates a new MANIPULATE_DWT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before manipulate_dwt_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to manipulate_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 manipulate_dwt

% Last Modified by GUIDE v2.5 24-Jan-2010 20:00:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @manipulate_dwt_OpeningFcn, ...
                   'gui_OutputFcn',  @manipulate_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 manipulate_dwt is made visible.
function manipulate_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 manipulate_dwt (see VARARGIN)

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

handles.opendir='.';

Jmax=6;
nblevel=1;

N=2^(Jmax+1);
W=zeros(N,N);
I=zeros(N,N);
handles.I=I;
handles.W=W;
handles.N=N;
handles.nblevel=nblevel;
handles.Jmin=Jmax+1-nblevel;
handles.Jmax=Jmax;

% Table des options d'ondelette
wavelet_table = {...
    'Haar', struct('wavelet_type','haar'); ...
    'Biorthogonal (CDF 9/7)', struct('wavelet_type','biorthogonal','wavelet_vm',4); ...
    'Sinus cardinal', struct('wavelet_type','sinc'); ...
    %'Biorthogonal (LeGall 5/3)', struct('wavelet_type','biorthogonal','wavelet_vm',2); ...
    %'Orthogonal (Daubechies 4)', struct('wavelet_type','daubechies','wavelet_vm',2); ...
    };

wavelet_names = wavelet_table(:,1);
wavelet_options_list = wavelet_table(:,2);

wavelet_id=1;
options=wavelet_options_list{wavelet_id};

handles.wavelet_names = wavelet_names;
handles.wavelet_options_list = wavelet_options_list;
handles.wavelet_id = wavelet_id;
handles.options=options;
set(handles.popupmenu4,'String',wavelet_names,'Value',handles.wavelet_id);

% Quadrant selection
handles.process=0;
set(handles.popupmenu1,'Value',handles.process+1);

handles.T=1;
handles.j=handles.Jmin;
handles.q=0;

set(handles.edit1,'String',num2str(handles.T));
set(handles.edit2,'String',num2str(handles.nblevel));

% Display error
handles.auxDisplay=5;
set(handles.popupmenu3,'Value',handles.auxDisplay);

set(handles.edit3,'String',int2str(handles.N));
if 1
    name='peppers.png';
    I=imread(name);
    if (ndims(I)==3)
        I=double(rgb2gray(I));
    else
        I=double(I);
    end
    
    I=imresize(I,[N,N],'bicubic');
    
    Jmin=handles.Jmin;
    options=handles.options;
    [W]=perform_wavelet_transform(I, Jmin, 1, options);

    handles.I=I;
    handles.W=W;
    guidata(hObject, handles);
end

% Update handles structure
guidata(hObject, handles);

redraw(hObject,[],handles);


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


% --- Outputs from this function are returned to the command line.
function varargout = manipulate_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.axes2);
[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.axes2);
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);
options=handles.options;

[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);
options=handles.options;
[W]=perform_wavelet_transform(I, Jmin, 1, options);

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



function display_bit_alloc(handles, Wq, K)

        v=zeros(K+1,3*(handles.Jmax-handles.Jmin+1)+1);

        n=1;
        [selx,sely] = compute_quadrant_selection(handles.Jmin,0);
        for k=0:K
            v(k+1,n)=sum(sum((Wq(sely,selx)==k))) / 4^handles.Jmin;
        end
        for j=handles.Jmin:handles.Jmax
            for q=1:3
                n=n+1;
                [selx,sely] = compute_quadrant_selection(j,q);
                for k=0:K
                    v(k+1,n)=sum(sum((Wq(sely,selx)==k))) / 4^j;
                end
            end
        end
        %    v=flipud(cumsum(flipud(v)));
        N=n;
        imagesc([1:N],[0:K],v);
        %    imagesc(v);
        %    axis normal;
        %    axis([0.5 N+0.5 -0.5 K+0.5]);
        axis xy;
        axis tight

        ticks={};
        for j=handles.Jmin:handles.Jmax
            n=4+3*(j-handles.Jmin);
            hold on; plot([0.5 0.5]+n,[-0.5 K+0.5],'r'); hold off
            if (j==handles.Jmin)
                ticks={ticks{:}, 'q=0','1','2','3'};
            else
                ticks={ticks{:}, 'q=1','2','3'};
            end
            text(n-1,double(K),sprintf('j=%i',j),'Color','r');
        end
        set(gca,'XTick',[1:N],'XTickLabel',ticks);
        %    imagesc([1:n],[0:K],v');
        %title('Bit allocation by quadrant');
        

function redraw(hObject, eventdata, handles)

I=handles.I;
[h,w]=size(I);
Jmin=handles.Jmin;
T=handles.T;

options=handles.options;
[W]=perform_wavelet_transform(I, Jmin, 1, options);
[R]=perform_wavelet_transform(W, Jmin, -1, options);

% DISPLAY ORIGINAL IMAGE

axes(handles.axes1);
imagesc(I,[0 255]);
colormap(gray(256));
%colorbar;

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

switch (handles.process)
    case 0, % All
        set(handles.text2,'String',sprintf('Pas de restriction'));
        W2=W;
        [W2, Wint]=perform_quantization(W2, handles.T);
        LL_selected=1;
    case 1, % Restrict cadran   
        set(handles.text2,'String',sprintf('Restriction � cadran j=%i q=%i',handles.j,handles.q));
        W2=zeros(size(W));
        [selx,sely] = compute_quadrant_selection(handles.j,handles.q);
        W2(sely,selx)=W(sely,selx);
        
        [W2, Wint]=perform_quantization(W2, handles.T);
        LL_selected=(handles.q==0);
    case 2, % Restrict scale
        set(handles.text2,'String',sprintf('Restriction � �chelle j=%i',handles.j));
        W2=zeros(size(W));
        if ((handles.j==handles.Jmin)&&(handles.q==0))
            [selx,sely] = compute_quadrant_selection(handles.j,0);
            W2(sely,selx)=W(sely,selx);
        else
            for q=1:3
                [selx,sely] = compute_quadrant_selection(handles.j,q);
                W2(sely,selx)=W(sely,selx);
            end
        end
        [W2, Wint]=perform_quantization(W2, handles.T);
        LL_selected=((handles.j==handles.Jmin)&&(handles.q==0));
    case 3, % Restrict <=j, q
        set(handles.text2,'String',sprintf('Restriction � cadrans j<=%i q=%i',handles.j,handles.q));
        W2=zeros(size(W));
        if ((handles.j==handles.Jmin)&&(handles.q==0))
            [selx,sely] = compute_quadrant_selection(handles.j,0);
            W2(sely,selx)=W(sely,selx);
        else
            for j=Jmin:handles.j
                [selx,sely] = compute_quadrant_selection(j,handles.q);
                W2(sely,selx)=W(sely,selx);
            end
        end
        [W2, Wint]=perform_quantization(W2, handles.T);
        LL_selected=(handles.q==0);
    case 4, % Restrict <=j
        set(handles.text2,'String',sprintf('Restriction � �chelles j<=%i',handles.j));
        W2=zeros(size(W));

        [selx,sely] = compute_quadrant_selection(handles.j,0);
        W2(sely,selx)=W(sely,selx);
        if (handles.q>0)
            for j=Jmin:handles.j
                for q=1:3
                    [selx,sely] = compute_quadrant_selection(j,q);
                    W2(sely,selx)=W(sely,selx);
                end
            end
        end
        [W2, Wint]=perform_quantization(W2, handles.T);
        LL_selected=1;
end

options=handles.options;
[R2]=perform_wavelet_transform(W2, Jmin, -1, options);

options=handles.options;
[R2]=perform_wavelet_transform(W2, Jmin, -1, options);

handles.W2=W2;
handles.Wint=Wint;
handles.R2=R2;
guidata(hObject,handles);

axes(handles.axes4);
if (LL_selected)
    imagesc(R2,[0 255]);
else
    imagesc(R2);
end
colormap(gray(256));
%colorbar;

axes(handles.axes5);
show_wavelet(W2,Jmin,'real',1);
axis('image');



switch (handles.auxDisplay)
    case 5, % Erreur de reconstruction
        axes(handles.axes3);
        imagesc(R-I,[-64 64]);
        colormap('gray');
        
        axes(handles.axes6);
        imagesc(R2-I,[-64 64]);
        colormap('gray');
        
    case 1, % FFT
        axes(handles.axes3);
        if 0 % dB
            F=db(abs(fftshift(fft2(R)))+eps);
            F2=db(abs(fftshift(fft2(R2)))+eps);
            range=[-100 0]+max(F(:));
        else
            F=(abs(fftshift(fft2(R))));
            F2=(abs(fftshift(fft2(R2))));
            range=[0 10000];
        end
        
        imagesc([0:w-1]-w/2,[0:h-1]-h/2,F,range);
        line([-w/2 w/2-1],[0,0]);
        line([0,0],[-h/2 h/2-1]);
        colormap(gray(256));

        axes(handles.axes6);
        imagesc([0:w-1]-w/2,[0:h-1]-h/2,F2,range);
        line([-w/2 w/2-1],[0,0]);
        line([0,0],[-h/2 h/2-1]);
        colormap(gray(256));

        if (handles.process==1)
            s=2^(handles.j);
%            line([-s s NaN -s s],[-s -s NaN s s]/2,'Color','y');
%            line([-s -s NaN s s]/2,[-s s NaN -s s],'Color','y');
            style={'Color','r','HorizontalAlignment','center'};
            switch (handles.q)
                case 0,
                    text(0,0,'LL',style{:});
                    line([-s s s -s -s]/2,[-s -s s s -s]/2,'Color','r');
                case 2,
                    text(3*s/4,0,'HL',style{:});
                    text(-3*s/4,0,'HL',style{:});
                    line([s/2 s s s/2 s/2],[-s -s s s -s]/2,'Color','r');
                    line(-[s/2 s s s/2 s/2],[-s -s s s -s]/2,'Color','r');
                case 1,
                    text(0,3*s/4,'LH',style{:});
                    text(0,-3*s/4,'LH',style{:});
                    line([-s -s s s -s]/2,[s/2 s s s/2 s/2],'Color','r');
                    line([-s -s s s -s]/2,-[s/2 s s s/2 s/2],'Color','r');
                case 3,
                    text(3*s/4,3*s/4,'HH',style{:});
                    text(-3*s/4,3*s/4,'HH',style{:});
                    text(3*s/4,-3*s/4,'HH',style{:});
                    text(-3*s/4,-3*s/4,'HH',style{:});
                    line(-[s/2 s s s/2 s/2],-[s/2 s/2 s s s/2],'Color','r');
                    line([s/2 s s s/2 s/2],-[s/2 s/2 s s s/2],'Color','r');
                    line(-[s/2 s s s/2 s/2],[s/2 s/2 s s s/2],'Color','r');
                    line([s/2 s s s/2 s/2],[s/2 s/2 s s s/2],'Color','r');
            end
            style0={'Color','y','LineStyle',':'};
            line([-s s s -s -s],[-s -s s s -s],style0{:});
            if (handles.q>0)
                %line([-s s s -s -s]/2,[-s -s s s -s]/2,style0{:});
            end
        end
        if (handles.process==2)
            s=2^(handles.j);
            if (handles.q>0)
                line([-s s s -s -s],[-s -s s s -s],'Color','y');
            else
                line([-s s s -s -s]/2,[-s -s s s -s]/2,'Color','y');
            end
        end
    case 3,
        axes(handles.axes3);
        [Wq, Wint0]=perform_quantization(W, 1);
        bins=[min(Wint0(:)):max(Wint0(:))];
        H=hist(Wint0(:), bins);
        bins=bins*1;
        bar(bins,H);
%        xlim([-150 150]);
        %r=max(150,max(abs(bins)+handles.T/2));
        xlim([-300 300]);
        
        Q=perform_entropy(Wint0);
        xlabel(sprintf('Entropie H_{dwt}=%g bits/pixel',Q/handles.N^2));
        
        axes(handles.axes6);
        %[Wq, Wint]=perform_quantization(W, handles.T);
        bins=[(min(Wint(:))):(max(Wint(:)))];
        H=hist(Wint(:), bins);
        bins=bins*handles.T;
        bar(bins,H);
        xlim([-300 300]);
        
        Q=perform_entropy(Wint);
        title(sprintf('Entropie H_{dwt}=%g bits/pixel',Q/handles.N^2));

        
    case 4,
        axes(handles.axes3);
        Wq=uint32(floor(log2(abs(W)+1)));
        K=max( max(Wq(:)), 7 );
        display_bit_alloc(handles,Wq,K);
        
        axes(handles.axes6);
        %[Wq, Wint]=perform_quantization(W2, T);
        Wq=uint32(floor(log2(abs(Wint)+1)));
        display_bit_alloc(handles,Wq,K);
        
    case 2,
        axes(handles.axes3);
        [Iq, Iint0]=perform_quantization(I, 1);
        bins=[0:255];
        H=hist(Iint0(:), bins);
        bins=bins*1;
        bar(bins,H);
        xlim([-0.5 255.5]);
        
        Q=perform_entropy(Iint0);
        xlabel(sprintf('Entropie H_{img}=%g bits/pixel',Q/handles.N^2));
        
        axes(handles.axes6);
        [Iq, Iint0]=perform_quantization(R2, 1);
        bins=[0:255];
        H=hist(Iint0(:), bins);
        bins=bins*1;
        bar(bins,H);
        xlim([-0.5 255.5]);
        
        Q=perform_entropy(Iint0);
        title(sprintf('Entropie H_{img}=%g bits/pixel',Q/handles.N^2));
end

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)

pp=pwd;
cd(handles.opendir);
[filename, pathname] = uigetfile({'*.jpg;*.png;*.gif;*.bmp', 'Image file'; ...
    '*.*','Any file'});
cd(pp);

if isequal(filename,0) || isequal(pathname,0)
    disp('Image import canceled...')
else
    N=handles.N;
    
    handles.opendir=pathname;
    
    name=fullfile(pathname, filename);
    disp(sprintf('Importing image %s and scaling to %ix%i...',name,N,N))
    
    I=imread(name);
    if (ndims(I)==3)
        I=double(rgb2gray(I));
    else
        I=double(I);
    end
    
    I=imresize(I,[N,N],'bicubic');
    
    Jmin=handles.Jmin;
    options=handles.options;
    [W]=perform_wavelet_transform(I, Jmin, 1, options);

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



function select_parameter(hObject, data, handles)

switch (handles.process)
    case 0, % All
        set(handles.text2,'String',sprintf('Pas de restriction'));
        redraw(hObject,[],handles);
        
    case {1,2,3,4}, % Choose cadran
        W=handles.W;
        axes(handles.axes2);
        [X,Y]=ginput(1);
        X=round(X); Y=round(Y);
        [j,q]=find_quadrant(handles.Jmin,handles.Jmax,X,Y);

        handles.j=j;
        handles.q=q;
        
        guidata(hObject,handles);

        redraw(hObject,[],handles);
end

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1

value=get(hObject,'Value');
handles.process=value-1;
guidata(hObject,handles);

redraw(hObject,[], handles);

%select_parameter(hObject,[], handles);

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




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

select_parameter(hObject, [], handles);


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





% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3

value=get(hObject,'Value');
handles.auxDisplay=value;
guidata(hObject,handles);

redraw(hObject,[],handles);

% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu4 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu4


value=get(hObject,'Value');
%options=handles.options;

handles.wavelet_id=value;
options=handles.wavelet_options_list{value};

%options

handles.options=options;
guidata(hObject,handles);

redraw(hObject,[],handles);

% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

get(hObject,'String');
T=str2double(get(hObject,'String'));

if (T<1) 
    T=1;
elseif (T>256)
    T=256;
end

set(hObject,'String',num2str(T));
handles.T=T;
guidata(hObject,handles);

redraw(hObject, [], handles);

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double

get(hObject,'String');
nb=round(str2double(get(hObject,'String')));

if (nb<1) 
    nb=1;
elseif (nb>handles.Jmax-1)
    nb=handles.Jmax-1;
end

set(hObject,'String',num2str(nb));
handles.nblevel=nb;
handles.Jmin=handles.Jmax+1-nb;

if (handles.j<=handles.Jmin)
    handles.j=handles.Jmin;
end

guidata(hObject,handles);

redraw(hObject, [], handles);

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




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


h=guidata(hObject);

export=[];h=guidata(hObject);

export=[];
export.I=h.I;
export.W=h.W;
export.W2=h.W2;
export.Wint=h.Wint;
export.R2=h.R2;

F=getframe(handles.axes6);
export.a6=F;

assignin('base','export',export);



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double

N = str2num(get(hObject,'String'));
Jmax = round(log2(N)-1);

if ~( (Jmax>3) && (Jmax<9)  ) return; end

nblevel=handles.nblevel;
handles.Jmax=Jmax;
handles.N=2^(Jmax+1);
handles.Jmin=Jmax+1-nblevel;

guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


