% [image_dct, dct_in_columns] = getBlockDCT(image,M);
%
% takes as input an image of size integer*M x integer*M,
% and returns an image composed of num_blocks x num_blocks
% (of same size as input image) of DCT coefficients for each
% block.
% 
% (num_blocks = size(im,1)/M)
%
% Example:
% im = [block1 block2
%  	block3 block4],
% then
%
% image_dct = getBlockDCT(im,4)
%
% will give [dct(block1)  dct(block2)
%            dct(block3)  dct(block4)];
%
% dct_in_columns will be
% [block1column_vector block2column_vector ... last_block_column_vector]
%
% (dct is 2-D dct here)
%
% J. Sabataitis
% November 5, 2000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [image_dct, dct_in_columns] = getBlockDCT(image,M);

image_dct = zeros(size(image));

num_blocks_down = size(image,1)/M;
num_blocks_across = size(image,2)/M;

nblocks = num_blocks_down * num_blocks_across;

dct_in_columns = zeros(M*M, nblocks);

block_count = 1;
for a = 1:num_blocks_down,
  for b = 1:num_blocks_across,
    
    block =  dct2(image((a-1)*M+1:a*M,(b-1)*M+1:b*M)); 
    image_dct((a-1)*M+1:a*M,(b-1)*M+1:b*M) = block;

    dct_col = reshape(block',M*M,1);
    dct_in_columns(:,block_count) = dct_col;    
    block_count = block_count+1;
  end
end

