function compImage = compressImage(xd, ni, nj, C, Q)

% Centers data
xc = xd - 128;

% Matrix for compressed image data
compImage = zeros(ni*8,nj*8);

% Compression
for i = 1:ni
  for j = 1:nj
    if (i == 1)
        index1 = 1;
    else
        index1 = ((i-1)*8) + 1;
    end
    if (j == 1)
        index2 = 1;
    else
        index2 = ((j-1)*8) + 1;
    end
    chunk = xc(index1:((i)*8),index2:((j)*8));
    chunk = C*chunk*C';
    chunk = round(chunk./Q);
    compImage(index1:((i)*8),index2:((j)*8)) = chunk;
  end
end