英文:
PNG Encode producing corrupt image
问题
我正在使用golang读取一个视频游戏主机的帧缓冲区,该缓冲区的格式是BRGA(然后我将其转换为RGBA)。当我将信息传递给Go PNG编码器时,输出的图像无效。我正在使用以下代码:
其中:
data
是一个长度为0x398000的RGBA像素切片,pitch
是5120,width
是1270,height
是720。
img := &image.RGBA{
Pix: data,
Stride: pitch,
Rect: image.Rect(0, 0, width, height),
}
os.Remove("./img.png")
file, _ := os.Create("./img.png")
defer file.Close()
filewriter := bufio.NewWriter(file)
if err := png.Encode(filewriter, img); err != nil {
panic(err)
}
但实际结果是(只在Windows上渲染或在Chrome中查看时出现问题):
如果有人需要,我已经上传了RGBA切片的二进制转储文件:https://1drv.ms/u/s!Ak-aZ3z7Ea8KwvUsqdP5OgWpZqxsGA
英文:
I'm reading a framebuffer from a video game console with golang - the buffer is in the format BRGA (which I then convert to RGBA). When I pass the information into the Go PNG Encoder, the image that comes out is not valid. The code i'm using is - where:
> where data
is a slice of RGBA pixels - 0x398000 in length, pitch
is 5120, width
is 1270, and height
is 720)
img := &image.RGBA{
Pix: data,
Stride: pitch,
Rect: image.Rect(0, 0, width, height),
}
os.Remove("./img.png")
file, _ := os.Create("./img.png")
defer file.Close()
filewriter := bufio.NewWriter(file)
if err := png.Encode(filewriter, img); err != nil {
panic(err)
}
The expected outcome would be:
But the actual outcome is (only renders on Windows or when view in Chrome.. weird):
I have uploaded a binary dump of the RGBA slice if anybody would like it - https://1drv.ms/u/s!Ak-aZ3z7Ea8KwvUsqdP5OgWpZqxsGA
答案1
得分: 1
你没有刷新缓冲区的写入器。你应该这样做:
filewriter := bufio.NewWriter(file)
defer filewriter.Flush()
在进行这个修复之后,我得到了一个有效的图像:
英文:
You are not flushing the buffered writer. You should do:
filewriter := bufio.NewWriter(file)
defer filewriter.Flush()
After this fix, I get a valid image:
答案2
得分: 0
不是一个解决方法,我想发表评论,但由于声望不够,暂时不能评论,但会补充关于Mac OS的差异。
MacOS部分的问题似乎是新出现的,可能是由于最新的10.12.3更新或者Safari的某些问题。我还没有确定问题的来源。但是是的,Mac系统在对图像进行编码/解码时有一些新的变化,导致图像变成透明或者灰色。我参与的一个项目在过去几周也遇到了这个问题,我仍在调查它出现的原因。
英文:
Not a fix, and I want to comment but can't yet due to reputation, but will add to the Mac OS discrepancy.
The MacOS part of the problem appears to be new, showing up since either the latest 10.12.3 update, or something with Safari. I haven't narrowed down the source yet. But yes, there is something new about how a Mac system will encode/decode an image, causing it to be transparent or grey as a result. A project I am on is also suffering from this problem for the past few weeks and I'm still investigating where it breaks down.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论