%===============================================================================
%
% Demo program for the wavelet decomposition and reconstruction
% of an image, using nondecimated transform with the quadratic spline wavelet.
% Implementation with two detail images at each resolution scale.
%
% File name: wt_2det.m
% Last modification Date:	09/04/2001	
%
% Author: Aleksandra Pizurica <sanja@telin.ugent.be> 
%         Ghent University 
%         Dept. Telecommunications and Information Processing  
%
% Copyright: All software, documentation, and related files in this distribution
%            are Copyright (c) 2001 Aleksandra Pizurica. All rights reserved.
%
% Permission is granted for use and non-profit distribution providing that this
% notice be clearly maintained. The right to distribute any portion for profit
% or as part of any commercial product is specifically reserved for the author.
%===============================================================================



clear all;
close all;

global InputType SCAN filename;
 
InputType=0;
 
while (InputType~=1 & InputType~=2 & InputType~=3 & InputType~=4)  
   InputType=input('\nInput image format:\n1. "*.tif"\n2. "*.bmp"\n3. "*.mat"\n4. "*.bin"\n>>  ');
end
imload;
X=SCAN;
[M,N]=size(X);


%Decomposition
  
  [A1,Dx1,Dy1]=wt2_mz(X,0);   
  [A2,Dx2,Dy2]=wt2_mz(A1,1);   
  [A3,Dx3,Dy3]=wt2_mz(A2,2);  
  
% Correction factors from Mallat&Zhong'92
% needed for exact estimation of the Lipschitz regularity

lambda1=1.5;
lambda2=1.12;
lambda3=1.03;
lambda4=1.01;
%=============

Dx1=Dx1/lambda1;
Dx2=Dx2/lambda2;
Dx3=Dx3/lambda3;

Dy1=Dy1/lambda1;
Dy2=Dy2/lambda2;
Dy3=Dy3/lambda3;
%-------------------------------------------------------  
 
figure('Units','normalized','Position',[0.1 0.3 0.5 0.5]);

subplot(3,3,1),imagesc(A1),axis off,title('A1');
subplot(3,3,2),imagesc(Dx1),axis off,title('Dx1');
subplot(3,3,3),imagesc(Dy1),axis off,title('Dy1');
subplot(3,3,4),imagesc(A2),axis off,title('A2')
subplot(3,3,5),imagesc(Dx2),axis off,title('Dx2')
subplot(3,3,6),imagesc(Dy2),axis off,title('Dy2');
subplot(3,3,7),imagesc(A3),axis off,title('A3');
subplot(3,3,8),imagesc(Dx3),axis off,title('Dx3');
subplot(3,3,9),imagesc(Dy3),axis off,title('Dy3');


%Reconstruction

 A2r=iwt2_mz(A3,lambda3*Dx3,lambda3*Dy3,2);
 A1r=iwt2_mz(A2r,lambda2*Dx2,lambda2*Dy2,1);
 A0r=iwt2_mz(A1r,lambda1*Dx1,lambda1*Dy1,0);
 
 colormap(gray);
  
 figure('Units','normalized','Position',[0.62 0.3 0.25 0.5]);
 subplot(2,1,1),imagesc(X);
 title('Original image');
 subplot(2,1,2),imagesc(A0r);
 title('Reconstructed image');
 
 colormap(gray);



