%jpeg解压
function imgdecArray=jpeg_decoding(imgWidth,imgHeight,imgYArray,imgUArray,imgVArray,Ycol,Ucol,Vcol)
T=dctmtx(8);
lighttable=...
[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;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
colortable=...
[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];
sequence=[1 3 4 10 11 21 22 36 2 5 9 12 20 23 35 37 6 8 13 19 24 34 ...
38 49 7 14 18 25 33 39 48 50 15 17 26 32 40 47 51 58 16 27 31 41 ...
46 52 57 59 28 30 42 45 53 56 60 63 29 43 44 54 55 61 62 64];
tic;
%载入文件
%load jpegrar.mat
p1=imgYArray;
p2=imgUArray;
p3=imgVArray;
col1=Ycol;
col2=Ucol;
col3=Vcol;
row=imgWidth;
col=imgHeight;
%DC系数差分解码
for(i=2:size(p1,1))
p1(i,1)=p1(i-1,1)+p1(i,1);
end
for(i=2:size(p2,1))
p2(i,1)=p2(i-1,1)+p2(i,1);
end
for(i=2:size(p3,1))
p3(i,1)=p3(i-1,1)+p3(i,1);
end
%还原p1,p2,p3
pp1=int8(zeros(size(p1,1),64));
pp1(:,col1)=p1;
p1=pp1;
clear pp1
pp2=int8(zeros(size(p2,1),64));
pp2(:,col2)=p2;
p2=pp2;
clear pp2
pp3=int8(zeros(size(p3,1),64));
pp3(:,col3)=p3;
p3=pp3;
clear pp3
%z字形解码->反量化->DCT逆变换
rY=ceil(row/8);
cY=ceil(col/8);
%YUV420
rU=rY/2;
cU=cY/2;
rV=rY/2;
cV=cY/2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%处理亮度维
count=1;
t1=zeros(8*rY,8*cY);
for(i=1:rY)
for(j=1:cY)
tmp=p1(count,:);
tmp=reshape(tmp(sequence),[8,8]);%z字形解码
t1(8*i-7:8*i,8*j-7:8*j)=T'*(double(tmp).*lighttable)*T;%反量化,DCT逆变换
count=count+1;
end
end
clear p1;
%处理色度维
count=1;
t2=zeros(8*rU,8*cU);
for(i=1:rU)
for(j=1:cU)
tmp=p2(count,:);
tmp=reshape(tmp(sequence),[8,8]);
t2(8*i-7:8*i,8*j-7:8*j)=T'*(double(tmp).*colortable)*T;
count=count+1;
end
end
clear p2;
%处理色度维
count=1;
t3=zeros(8*rV,8*cV);
for(i=1:rV)
for(j=1:cV)
tmp=p3(count,:);
tmp=reshape(tmp(sequence),[8,8]);
t3(8*i-7:8*i,8*j-7:8*j)=T'*(double(tmp).*colortable)*T;
count=count+1;
end
end
clear p3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%每一维输出转化为(0~255)
t1=uint8(t1+2^7);
t2=uint8(t2+2^7);
t3=uint8(t3+2^7);
%ycbcr->rgb
% pic=ycbcr2rgb(cat(3,t1,t2,t3));
% %转化为原始图像大小
% pic=pic(1:row,1:col,:);
% filename2=[filename(1:end-4),'_r',filename(end-3:end)];
% imwrite(pic,filename2);
% fprintf('解压文件保存为 %s \n',filename2);
% fprintf('解压花费时间为 %f 秒\n',toc);
% %原图像和压缩解压后图像的对比
% in=rgb2gray(imread(filename));
% out=rgb2gray(imread(filename2));
% fprintf('比较原图像与压缩解压后图象之间的误差\n');
% e_rms=sqrt(mean2((double(in)-double(out)).^2));
% fprintf('两者之间的均方误差为 %1.4f \n',e_rms);
% snr_rms=sum(sum(double(out).^2))/sum(sum((double(in)-double(out)).^2));
% fprintf('两者之间的信噪比为 %1.4f \n',snr_rms);
imgdecArray=[reshape(t1',imgWidth*imgHeight,1);reshape(t2',imgWidth*imgHeight/4,1);reshape(t3',imgWidth*imgHeight/4,1)];
评论0