% Matlab implementation of SPIHT (without Arithmatic coding stage)
%
% By Jing Tian, scuteejtian@hotmail.com
fprintf('----------- Welcome to SPIHT Matlab Demo! ----------------\n');
fprintf('----------- Load Image ----------------\n');
infilename = 'lena512.bmp';
outfilename = 'lena512_reconstruct.bmp';
Orig_I = double(imread(infilename));
rate = 1;
OrigSize = size(Orig_I, 1);
max_bits = floor(rate * OrigSize^2);
OutSize = OrigSize;
image_spiht = zeros(size(Orig_I));
[nRow, nColumn] = size(Orig_I);
fprintf('done!\n');
fprintf('----------- Wavelet Decomposition ----------------\n');
n = size(Orig_I,1);
n_log = log2(n);
level = n_log;
% wavelet decomposition level can be defined by users manually.
type = 'bior4.4';
[Lo_D,Hi_D,Lo_R,Hi_R] = wfilters(type);
[I_W, S] = func_DWT(Orig_I, level, Lo_D, Hi_D);
fprintf('done!\n');
fprintf('----------- Encoding ----------------\n');
img_enc = func_SPIHT_Enc(I_W, max_bits, nRow*nColumn, level);
fprintf('done!\n');
fprintf('----------- Decoding ----------------\n');
img_dec = func_SPIHT_Dec(img_enc);
fprintf('done!\n');
fprintf('----------- Wavelet Reconstruction ----------------\n');
img_spiht = func_InvDWT(img_dec, S, Lo_R, Hi_R, level);
fprintf('done!\n');
fprintf('----------- PSNR analysis ----------------\n');
imwrite(img_spiht, gray(256), outfilename, 'bmp');
Q = 255;
MSE = sum(sum((img_spiht-Orig_I).^2))/nRow / nColumn;
fprintf('The psnr performance is %.2f dB\n', 10*log10(Q*Q/MSE));
function [h,s,v] = rgb2hsv(r,g,b)
%RGB2HSV Convert red-green-blue colors to hue-saturation-value.
% H = RGB2HSV(M) converts an RGB color map to an HSV color map.
% Each map is a matrix with any number of rows, exactly three columns,
% and elements in the interval 0 to 1. The columns of the input matrix,
% M, represent intensity of red, blue and green, respectively. The
% columns of the resulting output matrix, H, represent hue, saturation
% and color value, respectively.
%
% HSV = RGB2HSV(RGB) converts the RGB image RGB (3-D array) to the
% equivalent HSV image HSV (3-D array).