## -*- coding: utf-8 -*- from numpy import array, vstack, hstack, kron, ones from random import SystemRandom sysrnd = SystemRandom() def uMask(u=7): return array([[(x + y < u) & (x + y > 0) for x in range(8)] for y in range(8)]) acMaskBlock = array( [[False] + [True for x in range(7)]] + [[True for x in range(8)] for y in range(7)] ) def acMask(h1, w1, mask=acMaskBlock): """Return a mask of the given size for the AC coefficients of a JPEG coefficient matrix.""" (h, w) = (h1 / 8, w1 / 8) A = vstack([mask for x in range(h)]) A = hstack([A for x in range(w)]) return A def repmat(M, rep): return kron(ones(rep), M) def getfreq(A, i, j): """ Return a submatrix of a JPEG matrix A including only frequency (i,j) from each block. """ (M, N) = A.shape return A[xrange(i, M, 8), :][:, xrange(j, N, 8)]