Swift中CGImage的RGB数据 [英] RGB Data from CGImage in Swift

查看:253
本文介绍了Swift中CGImage的RGB数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试在Swift中构建 RGB像素数据. 获取基本的图像信息没问题,但是我认为指针和字节偏移存在问题:

I've tried to build reading RGB pixel data in Swift. Getting the basic Image Information is no problem, but I think I've got something wrong with the pointer and the byte-offset:

let provider:CGDataProviderRef = CGImageGetDataProvider(inImage)
let data:NSData = CGDataProviderCopyData(provider)
let bytes: COpaquePointer = data.bytes // Needs to be changed to ConstUnsafePointer<()> in xCode beta3
let bytePointer = UnsafePointer<UInt8>(bytes)

println("Pixel Data:\n")

var printString:String = ""

    for var row:Int = 0; row < Int(height); row++ {
    for var col:Int = 0; col < Int(width); col++ {

        var pixel:UInt8 = bytePointer[row * Int(bytesPerRow) + col * Int(bytesPerPixel)]
        printString += "("

        for var x = 0; x < Int(bitsPerPixel); x++ {
            printString += pixel[x]
        }

        printString += ")"

        if col < Int(width) - 1 {
            printString += ","
        }

    }
    printString += "\n"
}

println(printString)

(我将Int强制转换为变量bytePerRow等,因为它们是UInt8)

(I cast Int to the variables bytesPerRow, etc because they are UInt8)

我得到了一个4x2px的图像,该图像包含两行以下像素:红色,绿色,蓝色,白色.因此,我希望得到此输出(没有Alpha):

I got a 4x2px Image wich contains two rows of following pixels: Red, Green, Blue, White. So I expect this output (There is no alpha):

(ff0000), (00ff00), (0000ff), (ffffff)
(ff0000), (00ff00), (0000ff), (ffffff)

printString += pixel[x]

"UInt8"没有名为下标"的成员

'UInt8' does not have a member named 'subscript'

我认为可以立即将其追加到字符串中.我试图将其转换为字符串,但这也不起作用.所以也许我的指针做错了.

I thought it could just be appended to a string in swift. I've tried to convert it to a string but this won't work either. So maybe I'm doing something wrong with the pointers.

我如何获得适当的输出?

How do i get a proper output?

@David我认为我在正确的轨道上.但是在第一个8字节之后发生了奇怪的事情.可能会被@ heinrich-giesen建议填充吗?

@David got me on a right track I think. But after the first 8 Byte weird thing happen. Might this be padding byted @heinrich-giesen suggested?

我3次测试的原始图像数据如下:

My raw image data for 3 tests looks like this:

Pic1: 3Byte/Pixel, no Alpha, 4 pixelWidth, 2 pixelHeight, [red, green, blue white; red, green, blue white]
Raw Data: <ff000000 ff000000 ffffffff ff000000 ff000000 ffffffff>

Pic2: 4Byte/Pixel, Alpha, 4 pixelWidth, 2 pixelHeight, [red, green, blue white (alpha); red, green, blue white]
Raw Data: <ff000000 00ff0000 0000ff00 ffffff00 ff0000ff 00ff00ff 0000ffff ffffffff>

如果您查看原始数据,那么很明显,我希望得到类似以下的输出:

If you look at the raw data its pretty clear that I expect a output like:

Pic1
(ff,00,00), (00,ff,00), (00,00,ff), (ff,ff,ff)
(ff,00,00), (00,ff,00), (00,00,ff), (ff,ff,ff)

Pic2
(ff,00,00,00), (00,ff,00,00), (00,00,ff,00), (ff,ff,ff,00)
(ff,00,00,ff), (00,ff,00,ff), (00,00,ff,ff), (ff,ff,ff,ff)

但是我的代码生成的实际输出是这样的:

But the real output my code generates is this:

Pic1   
((ff, 00, 00, ), (00, 00, ff, ), (00, ff, ff, ), (ff, ff, 00, ))
((ff, 00, 00, ), (00, 00, ff, ), (00, ff, 00, ), (ff, 00, 00, ))

Pic2 
((ff, 00, 00, ff, ), (00, 00, ff, ff, ), (00, ff, ff, 00, ), (ff, ff, 00, 00, )) 
((ff, 00, 00, ff, ), (00, 00, ff, 00, ), (00, ff, 00, 00, ), (ff, 00, 00, 00, ))

构建输出的代码现在是这样的:

The code to build the output is this now:

    for var row:Int = 0; row < Int(height) ; row++ {

        printString += "("

        for var col:Int = 0; col < Int(width); col++ {

            printString += "("

            for var x = 0; x < Int(bytesPerPixel); x++ {

                var pixel:UInt8 = bytePointer[row * Int(bytesPerRow) + col * Int(bytesPerPixel) + x * Int(bytesPerPixel)]
                printString += NSString(format:"%02x, ", pixel)

            }

            printString += ")"

            if col < Int(width) - 1 {
                printString += ", "
            }

        }
        printString += ")\n"
    }

println(printString)

那么是否存在填充(例如原始数据输出中的空格)?我试图在最初的8个之后跳过一两个,但这不会产生混乱的输出. 8个字节后,某些东西中断了.有建议吗?

So is there a padding (Like the space in the output of the raw data)? I've tried to skip one and two after the initial 8 but this won't yield less confusing outputs. Something breaks after 8 Bytes. Suggestions?

推荐答案

您的代码看起来有些奇怪.确定行:

Your code looks a bit strange. Are you sure the line:

    for var x = 0; x < Int(bitsPerPixel); x++ {

是正确的吗?您是真的意思是bitsPerPixel而不是bytesPerPixel吗? (或samplesPerPixel或其他快速显示的内容).并且您确定组件(样本)具有8位吗?一个像素没有填充字节?

is correct? Did you really mean bitsPerPixel and not bytesPerPixel ? (or samplesPerPixel or whatever it is in swift). And are you sure a component (a sample) has 8 bits? And a pixel has no padding bytes?

另一点:heightwidth是什么?它们是图像size的组成部分吗?那是错的.您必须使用pixelsWidepixelsHigh.图像的size仅表示以多大(以英寸或厘米或(1/72)英寸(称为点)表示)表示图像.这与像素数量无关.但是您需要像素!

Another point: what are height and width? Are they the components of the size of an image? that is wrong. You have to use pixelsWide and pixelsHigh. The size of an image says only how great --expressed in inch or centimeter or (1/72)of an inch (called a point)-- an image shall be depicted. That is independent of the number of pixels. But you work on pixels!

这篇关于Swift中CGImage的RGB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆