Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang PNG color manipulation won't reach 255

The image I'm using as image.png is:

enter image description here

When running my program with 254, it works and the output is:

enter image description here

Here is my code. If I change the 254 to 255, the output file becomes completely empty (0 bytes in size):

package main

import "fmt"
import "os"
import "bufio"
import "image/png"
import "image"

func main() {
    f, _ := os.Open("image.png")
    im, _ := png.Decode(bufio.NewReader(f))
    img := im.(*image.RGBA)
    s := img.Bounds().Size()
    w := s.X
    h := s.Y
    fmt.Println(w, h)
    newPix := make([]uint8, len(img.Pix))
    iy := 0
    for ; iy < h; iy++ {
        ix := 0
        for ; ix < w; ix++ {
            i := img.PixOffset(ix, iy)
            r := img.Pix[i+0]
            g := img.Pix[i+1]
            b := img.Pix[i+2]
            a := img.Pix[i+3]
            var max uint8 = 254 /*255*/
            newPix[i+0] = uint8(max - r)
            newPix[i+1] = uint8(max - g)
            newPix[i+2] = uint8(max - b)
            newPix[i+3] = uint8(max - a)
        }
    }
    newImg := image.NewRGBA(img.Bounds())
    newImg.Pix = newPix
    o, _ := os.Create("out.png")
    defer o.Close()
    png.Encode(bufio.NewWriter(o), newImg)
    fmt.Println("DONE")
}

Why is this and how can I fix it? 255 is still uint8 so it should work...

like image 840
Tom Avatar asked Mar 01 '14 02:03

Tom


2 Answers

I found a discussion on the golang-nuts group where this issue had come up before.

Basically, the output is too small to automatically trigger the writer to flush. Manually flushing the writer (recommended in any case) fixes the issue.

package main

import "fmt"
import "os"
import "bufio"
import "image/png"
import "image"

func main() {
    f, _ := os.Open("image.png")
    im, _ := png.Decode(bufio.NewReader(f))
    img := im.(*image.RGBA)
    s := img.Bounds().Size()
    w := s.X
    h := s.Y
    fmt.Println(w, h)
    newPix := make([]uint8, len(img.Pix))
    iy := 0
    for ; iy < h; iy++ {
        ix := 0
        for ; ix < w; ix++ {
            i := img.PixOffset(ix, iy)
            r := img.Pix[i+0]
            g := img.Pix[i+1]
            b := img.Pix[i+2]
            a := img.Pix[i+3]
            var max uint8 = 255
            newPix[i+0] = uint8(max - r)
            newPix[i+1] = uint8(max - g)
            newPix[i+2] = uint8(max - b)
            newPix[i+3] = uint8(max - a)
        }
    }
    newImg := image.NewRGBA(img.Bounds())
    newImg.Pix = newPix
    o, _ := os.Create("out.png")
    defer o.Close()
    writer := bufio.NewWriter(o)
    png.Encode(writer, newImg)
    writer.Flush()
    fmt.Println("DONE")
}

Normal caveats regarding ignoring errors apply.

like image 121
Intermernet Avatar answered Oct 27 '22 09:10

Intermernet


I think it is a mistake to try to change the alpha channel the way you are doing. You will set every transparent part to be visible and every visible part to be transparent if you succeed, and that would make the image look like nonsense.

So try replacing your newPix[i+3] line with:

newPix[i+3] = a
like image 31
David Grayson Avatar answered Oct 27 '22 11:10

David Grayson