├── count.m ├── reverse.m ├── bin_to_dec.m ├── dec_to_bin.m ├── row_col.m ├── quantise.m ├── JPEG.m ├── encode.m ├── insert_to_array.m ├── decode.m ├── zig_zag.m ├── discrete_cosine.m ├── R_Z.m ├── discrete_cosine_transform.m ├── check_coeff.m └── huffman.m /count.m: -------------------------------------------------------------------------------- 1 | function [ s ] = count(x) 2 | %UNTITLED2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | s=0; 5 | while(x>0) 6 | s=s+1; 7 | x=floor(x/10); 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /reverse.m: -------------------------------------------------------------------------------- 1 | function [ s ] = count(x) 2 | %UNTITLED2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | s=0; 5 | while(x>0) 6 | s=s+1; 7 | x=floor(x/10); 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /bin_to_dec.m: -------------------------------------------------------------------------------- 1 | function [ dec ] = bin_to_dec( bin ) 2 | %UNTITLED2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | dec=0;i=0; 5 | while(bin>0) 6 | r=mod(bin,10); 7 | dec=dec+r*2^i; 8 | bin=floor(bin/10); 9 | i=i+1; 10 | end 11 | end 12 | 13 | -------------------------------------------------------------------------------- /dec_to_bin.m: -------------------------------------------------------------------------------- 1 | function [ s ] = dec_to_bin(k) 2 | %UNTITLED2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | i=0; 5 | s=0; 6 | if(k>=1) 7 | while(k>=1) 8 | j=10^i; 9 | r=mod(k,2); 10 | k=floor(k/2); 11 | s=(r*j)+s; 12 | i=i+1; 13 | end 14 | 15 | elseif (k==0) 16 | s=0; 17 | end 18 | end 19 | 20 | -------------------------------------------------------------------------------- /row_col.m: -------------------------------------------------------------------------------- 1 | function [ R,C ] = row_col( value ) 2 | %UNTITLED Summary of this function goes here 3 | % Detailed explanation goes here 4 | 5 | R=0; 6 | for i=-11:1:11 7 | k=2^i; 8 | if (k==abs(value)) 9 | R=i+1; 10 | break; 11 | elseif (k>abs(value) && abs(value)>0) 12 | R=i; 13 | break; 14 | end 15 | end 16 | C=0; 17 | if (value>0) 18 | C=((2^R)-1)-((2^(R-1))-1)+(value-((2^(R-1))-1)); 19 | elseif (value<0) 20 | C=((2^R)-1)-abs(value)+1; 21 | else 22 | C=1; 23 | end 24 | C=C-1; 25 | end 26 | 27 | -------------------------------------------------------------------------------- /quantise.m: -------------------------------------------------------------------------------- 1 | function [ J ] = quantise( J,t,Ia,Ib ) 2 | %UNTITLED4 Summary of this function goes here 3 | % Detailed explanation goes here 4 | Q=[16 11 10 16 24 40 51 61; 5 | 12 12 14 19 26 58 60 55; 6 | 14 13 16 24 40 57 69 56; 7 | 14 17 22 29 51 87 80 62; 8 | 18 22 37 56 68 109 103 77; 9 | 24 35 55 64 81 104 113 92; 10 | 49 64 78 87 103 121 120 101; 11 | 72 92 95 98 112 100 103 99]; 12 | %---quantise--% 13 | if(t==1) 14 | for i=1:8:Ia 15 | for j=1:8:Ib 16 | J(i:i+7,j:j+7)=J(i:i+7,j:j+7)./Q; 17 | end 18 | end 19 | J=round(J); 20 | %--dequantise--% 21 | else 22 | for i=1:8:Ia 23 | for j=1:8:Ib 24 | J(i:i+7,j:j+7)=J(i:i+7,j:j+7).*Q; 25 | end 26 | end 27 | end 28 | end -------------------------------------------------------------------------------- /JPEG.m: -------------------------------------------------------------------------------- 1 | I=imread('passport_jaishree.jpg'); %initial image 2 | T = I; %backup of image 3 | I=rgb2gray(I); 4 | I=double(I); 5 | [size1,size2]=size(I); 6 | if(mod(size1,8)==0) 7 | Ia=size1-8; 8 | else 9 | Ia=size1-(8+mod(size1,8)); 10 | end 11 | if(mod(size2,8)==0) 12 | Ib=size2-8; 13 | else 14 | Ib=size2-(8+mod(size2,8)); 15 | end 16 | 17 | D_C=discrete_cosine(I,1,Ia,Ib); 18 | Q=quantise(D_C,1,Ia,Ib); 19 | Z=zig_zag(Q,1,Ia,Ib); 20 | 21 | A2=[]; 22 | P2=[]; 23 | 24 | disp('This loop will run till the iteration value reaches') 25 | disp(Ia*Ib/64); 26 | 27 | for i=1:1:Ia*Ib/64 28 | a=length(A2); 29 | A2=encode(Z(i,:),A2); 30 | P2=decode(A2,P2,a+1,i); 31 | disp(i); 32 | end 33 | 34 | J=zig_zag(P2,2,Ia,Ib); 35 | J=quantise(J,2,Ia,Ib); 36 | I2=discrete_cosine(J,2,Ia,Ib); 37 | I2=uint8(I2); 38 | I=uint8(I); %final image on decoding -------------------------------------------------------------------------------- /encode.m: -------------------------------------------------------------------------------- 1 | function [ A2 ] = encode(zig_zag,A2 ) 2 | %UNTITLED Summary of this function goes here 3 | % Detailed explanation goes here 4 | [R,C]=row_col(zig_zag(1)); 5 | k=0; 6 | for i=1:1:R 7 | k=k+2^i; 8 | end 9 | 10 | D1=dec_to_bin(k); 11 | D2=dec_to_bin(C); 12 | A2=insert_to_array(A2,D1,-1,1); 13 | A2=insert_to_array(A2,D2,R,1); 14 | zeroes=0; 15 | for i=2:1:length(zig_zag) 16 | if zig_zag(i)==0 17 | zeroes=zeroes+1; 18 | else 19 | [R,C]=row_col(zig_zag(i)); 20 | if(zeroes<16) 21 | A2=insert_to_array(A2,R_Z(R,zeroes),-1,2); 22 | A2=insert_to_array(A2,dec_to_bin(C),R,2); 23 | else 24 | for l=1:1:floor(zeroes/15) 25 | A2=insert_to_array(A2,11111111001,-1,2); 26 | end 27 | zeroes=zeroes-(floor(zeroes/15)*15); 28 | A2=insert_to_array(A2,R_Z(R,zeroes),-1,2); 29 | A2=insert_to_array(A2,dec_to_bin(C),R,2); 30 | end 31 | zeroes=0; 32 | end 33 | end 34 | if zeroes~=0 35 | A2=insert_to_array(A2,1010,-1,2); 36 | end 37 | 38 | end -------------------------------------------------------------------------------- /insert_to_array.m: -------------------------------------------------------------------------------- 1 | function [ A ] = insert_to_array( A,s,r,t) 2 | %UNTITLED3 Summary of this function goes here 3 | % Detailed explanation goes here 4 | len=length(A); 5 | if(t==1) %--dc coeff encode 6 | if(r==-1) 7 | 8 | if(s==0) 9 | A(len+1)=0; 10 | else 11 | len=len+count(s); 12 | end 13 | else 14 | if(r>0) 15 | if(s==0) 16 | A(len+r)=0; 17 | else 18 | len=len+r; 19 | end 20 | else 21 | A(len+1)=0; 22 | end 23 | end 24 | end 25 | if(t==2) %--ac coeff encode 26 | if(r==-1) 27 | if(s==0) 28 | A(len+2)=0; 29 | elseif(s==1) 30 | A(len+1)=0; 31 | len=length(A)+count(s); 32 | else 33 | len=len+count(s); 34 | end 35 | else 36 | if(s==0) 37 | A(len+r)=0; 38 | else 39 | len=len+r; 40 | end 41 | end 42 | end 43 | while(s>0) 44 | A(len)=mod(s,10); 45 | s=floor(s/10); 46 | len=len-1; 47 | end 48 | end 49 | 50 | 51 | -------------------------------------------------------------------------------- /decode.m: -------------------------------------------------------------------------------- 1 | function [ P2,m ] = decode( P,P2,m,p2 ) 2 | %UNTITLED Summary of this function goes here 3 | % Detailed explanation goes here 4 | 5 | len=length(P); 6 | cunt=0; 7 | for j=m:len 8 | [row_no,zeroes]=check_coeff(P(m:j),0); 9 | if row_no~=-1 10 | col_no=0;k=0; 11 | temp=P(j+1:j+row_no); 12 | for i=length(temp):-1:1 13 | col_no=col_no+temp(i)*2^k; 14 | k=k+1; 15 | end 16 | if(col_no0) 48 | P2(p2,cunt+zeroes)=0; 49 | end 50 | cunt=cunt+zeroes; 51 | if(col_no