Coding wavelets the easy way

function [wb,wr] = colorCompress(Cb,Cr,ratio)
% color compression using the Haar basis
% with given a wavelet compression ratio
% color compression in the Haar basis with 2 scales, i.Ee.
% on a grid of 4x4 pixels
[m,n] = size(Cb);
Cr=double(Cr);
Cr.s = Cr;Cr.rd=0;Cr.cd=0;
Cb=double(Cb);
Cb.s = Cb;Cb.rd=0;Cb.cd=0;

load Daub2
h.s=h;h.d=0;
g.s=g;g.d=0;
rh.s=rh;rh.d=1;
rg.s=rg;rg.d=1;

scale =9;
numEl=round(512*512/ratio);

WT = WaveTransform2D(Cr,h,g,scale);
t = thresholdValue2D(WT,numEl);
WT = threshold2D(WT,t);
vr = InvWaveTransform2D(WT,rh,rg,scale);
% trim columns at left that comes from the rd field and adjust length
wr = vr.s(:,vr.rd+1:vr.rd+n);
% trim rows
wr = wr(vr.cd+1:vr.cd+m,:);
wr = uint8(wr);

WT = WaveTransform2D(Cb,h,g,scale);
t = thresholdValue2D(WT,numEl);
WT = threshold2D(WT,t);
vb = InvWaveTransform2D(WT,rh,rg,scale);
% trim columns at left that comes from the rd field and adjust length
wb = vb.s(:,vb.rd+1:vb.rd+n);
% trim rows
wb = wb(vb.cd+1:vb.cd+m,:);
wb = uint8(wb);