strukturag / libheif

libheif is an HEIF and AVIF file format decoder and encoder.
Other
1.7k stars 298 forks source link

Converting to Go image.Image produces flat colors #215

Open kyroy opened 4 years ago

kyroy commented 4 years ago

I would like to use this library to convert my heic images from my iPhone to jpeg. Unfortunately, the colors of the resulting images differ from the originals.

Example code:

// error handling omitted
file := "example.heic"
f, _ := os.Open(file)
defer f.Close()

goImg, _, _ := image.Decode(f)
var writer bytes.Buffer
jpeg.Encode(&writer, goImg, &jpeg.Options{
    Quality: 100,
})
ioutil.WriteFile("example.jpeg", writer.Bytes(), 0644)

This is also true when using the example.heic from this repo.

Result: res

countingpine commented 4 years ago

Hi kyroy, I hope you don't mind my input on this issue. If I interpret it correctly: you're experiencing some loss of quality when converting the image to JPEG? If that's right, then I would say the issue is that JPEG is a "lossy" format, even at quality 100, and that if you try outputting to PNG, which is a lossless format, it should perfectly preserve the colour information.

silverbacknet commented 4 years ago

@countingpine It seems more like a ICC or colorspace that isn't being carried over, though I didn't see any on a cursory glance, since everything else lines up. It could be just a special bit Apple throws in because Apple. It's definitely not JPEG; if you compare the two you'll see that there's no perceptual difference at all unless you're on iPhone.