gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.52k stars 7.38k forks source link

resources/images: Poor overlay quality when background image is an indexed (not true color) PNG #12543

Open jmooring opened 1 month ago

jmooring commented 1 month ago

Reference: https://discourse.gohugo.io/t/image-overlay-quality-is-poor/50034

Before getting into the details, I have verified that this is not an upstream issue with disintegration/gift. I tested with this:

main.go ```go package main import ( "image" "image/png" "log" "os" _ "image/png" "github.com/disintegration/gift" ) func main() { // Get the background image (24 bit: indexed not true color) f, err := os.Open("background.png") if err != nil { log.Fatal(err) } bgImage, _, err := image.Decode(f) if err != nil { log.Fatal(err) } // Get the foreground image (24 bit with 8 bit alpha channel) f, err = os.Open("foreground.png") if err != nil { log.Fatal(err) } fgImage, _, err := image.Decode(f) if err != nil { log.Fatal(err) } // Create a new image with dimensions of the bgImage. dstImage := image.NewRGBA(bgImage.Bounds()) // Copy the bgImage to the dstImage. gift.New().Draw(dstImage, bgImage) // Draw the fgImage over the dstImage at the (20, 20) position. gift.New().DrawAt(dstImage, fgImage, image.Pt(20, 20), gift.OverOperator) // Create the composite file. f, err = os.Create("composite.png") if err != nil { log.Fatal(err) } defer f.Close() // Encode the composite image. err = png.Encode(f, dstImage) if err != nil { log.Fatal(err) } } ```

Details:

Simple example:

git clone --single-branch -b hugo-github-issue-12543 https://github.com/jmooring/hugo-testing hugo-github-issue-12543
cd hugo-github-issue-12543
hugo server

Note that GIF images (also indexed) work fine.