Lossy and lossless image encoding using multi-scale recurrent pattern matching