% dcttest.m - Read Input Data from block8x8.txt and the Quantization table
%                             from Qtable.xls for a dct2 test
%
Q=xlsread('Qtable.xls','A2:H9');
fin=fopen('block8x8.txt','r');
fout=fopen('dctO.txt','w');
fgetl(fin);
X=fscanf(fin,'%f',[8,8]); fclose(fin); X=X';
Y=dct2(X-128,[8,8]);
fprintf(fout,'DCT coefficients\n');
for i=1:8
  for j=1:8  fprintf(fout,'%6.1f',Y(i,j)); end
  fprintf(fout,'\n');
end
Y=Y./Q; % Y=fix(Y+0.5*(Y>0));
for i=1:8 
  for j=1:8 
    if (Y(i,j)>0) 
      Y(i,j)=fix(Y(i,j)+0.5);
    else
      Y(i,j)=fix(Y(i,j)-0.5);
    end
  end
end
fprintf(fout,'\n');
fprintf(fout,'Quantized DCT coefficients\n');
for i=1:8
  for j=1:8  fprintf(fout,'%4d',Y(i,j)); end
  fprintf(fout,'\n');
end
fclose(fout);