The aim of this work is the development of a specific and effective algorithm for lossy and lossless compression of binary images. Our proposed framework can be divided into two main steps: contour approximation and residue encoding. In the first step, the contours of the objects within the image are approximated as succession of vertices connected with polynomial curves. Once the approximation is obtained, the information is encoded using Improved Adaptive Arithmetic Coding. At the end of this procedure, lossy compression of the input image is obtained. In the second step, we propose an efficient algorithm based on morphological operation to detect the residues, i.e. the points of the original image that are not in the reconstructed image. Moreover, we introduce two different methods for encoding the residues. The first one is based on Context Adaptive Arithmetic Coding, while in the second one the residues are sorted according to their relative Chebyshev distance and encoded through Improved Adaptive Arithmetic Coding. Finally, we show a simple way to combine our proposed framework with the existing chain codes methods. Simulations results shows that, on average, the proposed framework achieves compression ratio lower than the existing methods does. Moreover, thanks to our formulation in two steps, it can provide both lossy and lossless compression of the input image.