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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算