function [ im ] = decoder( codebook, index_vector, im_height, im_width )
% ? Decoder function 
%     ? should realize the compression itself 
%     ? parameters: 
%         ? input1: codebook 
%         ? input2: index vector 
%         ? input3: number of rows of the image to be reconstructed 
%         ? input4: number of columns of the image to be reconstructed 
%         ? output: the reconstructed/decoded image 
%     ? (things to place here: 
%         constructing an array here as the output image; 
%         filling it from blocks to blocks as horizontal tile-series, 
%             where the content of the squared tiles should come from the codebook and the index vector)

% Restore image from compressed data
im_vector = codebook(:, index_vector);

% Start the tiresome process of rearrage blocks from colunms to image
bsl = sqrt(size(codebook,1)); % bsl = Block Side Length
im = zeros(im_height, im_width);
hor_blocks = im_width/bsl;
ver_blocks = im_height/bsl;

for i = 0:ver_blocks-1
    for j = 0:hor_blocks-1
        im(i*bsl+1:(i+1)*bsl,j*bsl+1:(j+1)*bsl) = reshape(im_vector(:,i*hor_blocks+j+1), bsl, bsl);
    end
end

% Ennyi lenne, ha nem sorfolytonosan kéne...     >:(
%im = uint8(col2im(im_vector, [bsl bsl], [im_width im_height], 'distinct')*255);

im = uint8(im*255);

end

