waqas2505
Newbie level 4
Could somebody please explain to me what is happening in this program, line by line because I am having problems understanding it.
Code:
close all;
clear all;
qy=[...
3 2 2 3 5 8 10 12
2 2 3 4 5 12 12 11
3 3 3 5 8 11 14 11
3 3 4 6 10 17 16 12
4 4 7 11 14 22 21 15
5 7 11 13 16 21 23 18
10 13 16 17 21 24 24 20
14 18 19 20 22 20 21 20];
%qy=[...
%16 11 10 16 24 40 51 61
%12 12 14 19 26 58 60 55
%14 13 16 24 40 57 69 56
%14 17 22 29 51 87 80 62
%18 22 37 56 68 109 103 77
%24 35 55 64 81 104 113 92
%9 64 78 87 103 121 120 101
%72 92 95 98 112 100 103 99];
qc=[...
17 18 24 47 99 99 99 99
18 21 26 66 99 99 99 99
24 26 56 99 99 99 99 99
47 66 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99];
row= [1 1 2 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 6 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 3 4 5 6 7 8 8 7 6 5 4 5 6 7 8 8 7 6 7 8 8];
col= [1 2 1 1 2 3 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 8 7 6 5 4 3 4 5 6 7 8 8 7 6 5 6 7 8 8 7 8];
tabledc=[...
3 0 1 0 0 0 0 0 0 0
3 0 1 1 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0
3 1 0 1 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0
4 1 1 1 0 0 0 0 0 0
5 1 1 1 1 0 0 0 0 0
6 1 1 1 1 1 0 0 0 0
7 1 1 1 1 1 1 0 0 0
8 1 1 1 1 1 1 1 0 0
9 1 1 1 1 1 1 1 1 0];
% run - no of bits required to encode data- total length - base code length(imp) - base code use for both luminance and chr as in hex file...
tableac=[...
0 1 3 2 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1 0 0 0
15 5 21 16 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1
15 6 22 16 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0
15 7 23 16 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
15 8 24 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
15 9 25 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
15 10 26 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0];
img=imread('Tiff.tif');
i=imageresize(img);
img=imread('asd.tif','tif');
prop=imfinfo('asd.tif','tif');
%prop=imread('Water lilies.bmp');
h=ceil((prop.Height)/8);
w=ceil((prop.Width)/8);
ycbcrmap=zeros(h*8,w*8,3);
ycbcrmap(1:prop.Height,1:prop.Width,1:3) = rgb2ycbcr(img);
fid1= fopen('save1.zsh','w');
fid2= fopen('header','r');
hread=fread(fid2);
fwrite(fid1,hread);
fclose(fid2);
buf=[];
bufstr='';
accode=[];
for i=1:h
for j=1:w
temp=zeros(8,8);
for y=1:3
for k=1:8
for l=1:8
temp(k,l)=ycbcrmap(((i-1)*8)+k,((j-1)*8)+l,y);
end
end
temp=temp-128;
temp=dct2(temp);
if(y==1)
temp=temp./qy;
else
temp=temp./qc;
end
temp=round(temp);
for ct=1:64
str(ct)=temp(row(ct),col(ct));
end
if j==1 & i==1
dc=temp(1);
dcl(y)=temp(1);
else
dc=temp(1)-dcl(y);
dcl(y)=temp(1);
end
if dc==0
dccode=[0 0];
else
dcabs=abs(dc);
dcbin=dec2bin(dcabs);
dclen=length(dcbin);
if dc<0
comp=repmat(49,[1 dclen]);
dcbin=comp-dcbin;
else
comp=repmat(48,[1 dclen]);
dcbin=dcbin-comp;
end
dccode=tabledc(dclen,2:1+tabledc(dclen,1));
dccode=[dccode dcbin];
end
%ac dec for lum
str=str(2:64);
nozac=find(str~=0);
acno=length(nozac);
for o=1:acno
ac=str(nozac(o));
acabs=abs(ac);
acbin=dec2bin(acabs);
aclen=length(acbin);
if ac<0
comp=repmat(49,[1 aclen]);
acbin=comp-acbin;
else
comp=repmat(48,[1 aclen]);
acbin=acbin-comp;
end
if(nozac(o)==1)
acseq=tableac(aclen,5:4+tableac(aclen,4));
accode=[accode acseq acbin];
else
if o==1
z=nozac(o)-1;
else
z=nozac(o)-nozac(o-1)-1;
end
%check 4 if z>=15
noz=floor(z/15);
zrl=[1 1 1 1 1 1 1 1 0 0 1];
accode=[accode repmat(zrl,[1,noz])];
z=rem(z,15);
%generate the huffman sequence from table
acseq=tableac((z*10)+aclen,5:4+tableac((z*10)+aclen,4));
accode=[accode acseq acbin];
end
end
accode=[accode 1 0 1 0];
accode=[dccode accode];
if(i==h)
if(j==w)
accode=[accode 0 0 1 0 1 0];
end
end
%fwrite to file
q=1;
while(q<=length(accode))
if length(buf)~=8
buf=[buf accode(q)];
q=q+1;
else
for e=1:8
if buf(e)==0
bufstr=[bufstr '0'];
else
bufstr=[bufstr '1'];
end
end
kk=bin2dec(bufstr);
fwrite(fid1,kk,'uint8');
if kk==255
fwrite(fid1,0,'uint8');
end
buf=[];
bufstr=[];
end
end
temp=[];
accode=[];
dccode=[];
end
end
end
if length(buf)~=0
buf=[buf zeros(1,8-length(buf))];
for e=1:8
if buf(e)==0
bufstr=[bufstr '0'];
else
bufstr=[bufstr '1'];
end
end
kk=bin2dec(bufstr);
fwrite(fid1,kk,'uint8');
if kk==255
fwrite(fid1,0,'uint8');
end
end
fwrite(fid1,255,'uint8');
%corresponding to ff
fwrite(fid1,217,'uint8');
%corresponding to d9
fseek(fid1,163,-1);
if(prop.Height<255)
fwrite(fid1,0,'uint8');
fwrite(fid1,prop.Height,'uint8');
else
fwrite(fid1,(prop.Height/256),'uint8');
fwrite(fid1,rem(prop.Height,256),'uint8');
end
fseek(fid1,165,-1);
%specify height at an offset of a4
if(prop.Width<255)
fwrite(fid1,0,'uint8');
fwrite(fid1,prop.Width,'uint8');
else
fwrite(fid1,(prop.Width/256),'uint8');
fwrite(fid1,rem(prop.Width,256),'uint8');
end
%specify width at an offset of a6
%fseek(fid1,0,1);
fclose(fid1);
Last edited by a moderator: