【问题标题】:Accessing image data from a bitmap given a pointer给定指针从位图中访问图像数据
【发布时间】:2010-09-30 23:33:39
【问题描述】:

我的问题是一旦有了指针就可以正确读取像素数据。

所以我的图像占据了整个 iPhone 屏幕并且没有 alpha 通道(每像素 24 位,每个组件 8 位,每行 960 字节),我想找出特定像素的颜色。

我有指向数据的指针

UInt8 *data = CFDataGetBytePtr(bitmapData);

但现在我不确定如何在给定坐标的情况下正确索引数据?

【问题讨论】:

    标签: ios iphone image pointers bitmap


    【解决方案1】:
    UInt8 *data = CFDataGetBytePtr(bitmapData);
    
    unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
    unsigned long x_offset = x * no_of_channels;
    
    /* assuming RGB byte order (as opposed to BGR) */
    UInt8 r = *(data + row_stride * y + x_offset );
    UInt8 g = *(data + row_stride * y + x_offset + 1);
    UInt8 b = *(data + row_stride * y + x_offset + 2);
    
    
    /* less portable but if you want to access it in as a packed UInt32, you could do */
    UInt32 color = *(data + row_stride * y + x) & 0x00FFFF;  /* little endian byte ordering */
    

    【讨论】:

    • 你好,谢谢你的帮助,今晚下班我试试看!
    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多