nfnt / resize

Pure golang image resizing
ISC License
3.02k stars 321 forks source link

Fix wrong color with RGBA input #34

Closed minodisk closed 9 years ago

minodisk commented 9 years ago

When resize image.RGBA, output image is very white. This problem is maybe related to https://github.com/nfnt/resize/issues/31#issuecomment-104170311.

In the premultiplication in resizeRGBA, color shouldn't be divided by 0xffff but 0xff, because the color of color.RGBA is uint8, IMO.

minodisk commented 9 years ago

Sample code

using image at #31 (comment).

func main() {
    resp, err := http.Get("https://cloud.githubusercontent.com/assets/574362/7743940/9c3608e0-ff9f-11e4-84f2-b7b6f3e109d0.png")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    src, err := png.Decode(resp.Body)
    if err != nil {
        panic(err)
    }

    size := src.Bounds().Size().Mul(2)
    dst := Resize(uint(size.X), uint(size.Y), src, Lanczos3)
    file, err := os.Create("dist.png")
    if err != nil {
        panic(err)
    }
    if err := png.Encode(file, dst); err != nil {
        panic(err)
    }
}

Result without this patch

fail :sob:

dist

Result with this patch

success :smile:

dist

nfnt commented 9 years ago

Thanks for the pull request!