1. A method for encoding images, the method comprising: receiving, in an encoder, an input image in a first dynamic range; generating, based on the input image, a first image in a second dynamic range, wherein the second dynamic range is lower than the first dynamic range; generating, based on the input image and the first image, an image prediction function which maps pixel values from the second dynamic range to pixel values in the first dynamic range; applying the image prediction function to the first image to obtain a predicted image; computing a residual image representing differences between pixel values in the predicted image and corresponding pixel values in the input image; and encoding and storing data representing the first image, the prediction function, and the residual image, wherein the method is performed by one or more processors.