This letter describes a context-based entropy coding suitable for any causal spatial differential pulse code modulation (DPCM) scheme performing lossless or near-lossless image coding. The proposed method is based on partitioning of prediction errors into homogeneous classes before arithmetic coding. A context function is measured on prediction errors lying within a two-dimensional (2-D) causal neighborhood, comprising the prediction support of the current pixel, as the root mean square (RMS) of residuals weighted by the reciprocal of their Euclidean distances. Its effectiveness is demonstrated in comparative experiments concerning both lossless and near-lossless coding. The proposed context coding/decoding is strictly real-time.