我使用w32库允许我使用Go语言进行窗口化。我不太清楚如何处理允许我在像素缓冲区中设置像素值的kw = {}
try:
import tensorflow
except ImportError: # There is no one, declare dependency
kw['install_requires'] = ['tensorflow']
else:
if is_gpu(tensorflow):
kw['install_requires'] = ['tensorflow-gpu']
else:
kw['install_requires'] = ['tensorflow']
setup(
…
**kw
)
。
我使用unsafe.Pointer
,因为这是w32库希望我在CreateDIBSection函数中传递的内容。
unsafe.Pointer
该代码成功并向我提供指向存储DIBBits的内存位置的指针。我该如何用它来写值?
var p unsafe.Pointer
bitmap := w32.CreateDIBSection( srcDC, &bmi, w32.DIB_RGB_COLORS, &p, w32.HANDLE(0), 0 )
会给我一个错误p[idx] = 0xff
。我已经阅读了关于unsafe.Pointer的相关文档,但无法弄清楚如何将其视为我可以写入的字节缓冲区。
我是Go的新手,并在gobyexample.com处完成了很多示例,但无法理解这一点。
答案 0 :(得分:1)
只需将unsafe.Pointer以适当的方式转换回数组(可索引)即可。
在尝试各种演员表后,这是有效的(假设wid
和hgt
都声明为const
):
pixels := (*[wid*hgt*4]uint8)(ptr)
然后我可以用以下方式更改它们:
pixels[(y*wid+x)*4+0] = 0x00 // Blue
pixels[(y*wid+x)*4+1] = 0x00 // Green
pixels[(y*wid+x)*4+2] = 0x00 // Red