douwei6478 2017-06-26 22:23 采纳率: 100%
浏览 39
已采纳

如何在Go中写入DIBits结构?

I'm using a w32 library to allow me to do Windowing with the Go language. I'm not quite sure what to do with an unsafe.Pointer that will allow me to start setting pixel values in the pixel buffer.

I use an unsafe.Pointer, because that's what the w32 library expects me to pass in the CreateDIBSection function.

var p unsafe.Pointer
bitmap := w32.CreateDIBSection( srcDC, &bmi, w32.DIB_RGB_COLORS, &p, w32.HANDLE(0), 0 )

That code succeeds and gives me a pointer to the memory location where the DIBBits are stored. How can I use that to write values?

p[idx] = 0xff

will give me an error type unsafe.Pointer does not allow indexing. I've read the relevant docs on the unsafe.Pointer, but can't figure out how to treat it as a byte buffer that I can write into.

I'm new to Go and have worked through a lot of the examples at gobyexample.com, but cannot figure this out.

  • 写回答

1条回答 默认 最新

  • dongwang788787 2017-06-27 00:09
    关注

    It's just a matter of casting the unsafe.Pointer back to an array (which is indexable) in the proper way.

    After trying various casts, this is the one that worked (assuming wid and hgt are each declare as const):

    pixels := (*[wid*hgt*4]uint8)(ptr)
    

    then I was able to change them with:

    pixels[(y*wid+x)*4+0] = 0x00 // Blue
    pixels[(y*wid+x)*4+1] = 0x00 // Green
    pixels[(y*wid+x)*4+2] = 0x00 // Red
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示