2016-09-29 15 views
0

私の迅速なプロジェクトでは、赤、緑、青、アルファ値を変更できるように画像のピクセル値を保持するために2つのクラスがあります。 UnsafeMutableBufferPointerは、Pixelクラスオブジェクトで構成される多くのビットを保持します。スレッド1:EXC_BAD_ACCESS(コード= EXC_I386_GPFLT)

UnsafeMutableBufferPointer<Pixel>プロパティを保持するクラスと対話できます。私はそのオブジェクトのすべてのプロパティにアクセスでき、すべて正常に動作します。私がUnsafeMutableBufferPoint<Pixel>で抱えている唯一の問題は、Pixelオブジェクトでループさせようとしていて、スレッド1でクラッシュしています。EXC_BAD_ACCESS (code=EXC_I386_GPFLT)例外です。

init!(image: UIImage) 
{ 
    _width = Int(image.size.width) 
    _height = Int(image.size.height) 

    guard let cgImage = image.cgImage else { return nil } 

    _width = Int(image.size.width) 
    _height = Int(image.size.height) 
    let bitsPerComponent = 8 

    let bytesPerPixel = 4 
    let bytesPerRow = _width * bytesPerPixel 
    let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: _width * _height) 
    let colorSpace = CGColorSpaceCreateDeviceRGB() 

    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 
    guard let imageContext = CGContext(data: imageData, width: _width, height: _height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil } 
    imageContext.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: image.size)) 

    _pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: _width * _height) 
} 

この機能は、プログラムをクラッシュさせる部分です。クラッシュしている部分は、rgba.pixelsをループしているforループです。 rgba.pixelsはUnsafeMutableBufferPointerです。

​​

これはUnsafeMutableBufferPointer<Pixel>を作成するコンストラクタです。これを行う簡単な方法はありますか?RBGA値を取得して簡単に変更できる方法はありますか?

ピクセルクラスはUInt32の値で、これは4つのUInt 8値に分割されています。

私はそれらの値を保持するために間違った構造を使用していますか?もしそうなら、より安全で簡単な構造を使用していますか?または、Pixel値にアクセスする際に何か問題がありますか?

答えて

0

これは私がイメージのピクセルを得た方法である -

// Grab and set up variables for the original image 
    let inputCGImage = inputImage.CGImage 
    let inputWidth: Int = CGImageGetWidth(inputCGImage) 
    let inputHeight: Int = CGImageGetHeight(inputCGImage) 

    // Get the colorspace that will be used for image processing (RGB/HSV) 
    let colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()! 

    // Hardcode memory variables 
    let bytesPerPixel = 4 // 32 bits = 4 bytes 
    let bitsPerComponent = 8 // 32 bits div. by 4 components (RGBA) = 8 bits per component 
    let inputBytesPerRow = bytesPerPixel * inputWidth 

    // Get a pointer pointing to an allocated array to hold all the pixel data of the image 
    let inputPixels = UnsafeMutablePointer<UInt32>(calloc(inputHeight * inputWidth, sizeof(UInt32))) 

    // Create a context to draw the original image in (aka put the pixel data into the above array) 
    let context: CGContextRef = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight, bitsPerComponent, inputBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue)! 

    CGContextDrawImage(context, CGRect(x: 0, y: 0, width: inputWidth, height: inputHeight), inputCGImage) 

が、これはそれはあなたが使用しているものだ包みスウィフト3構文ではありませんが、それは基本的なアルゴリズムだ覚えておいてください。今、各ピクセルの個々のカラー値をつかむために、あなたはこれらの機能を実装する必要があります -

func RGBAMake(r: UInt32, g: UInt32, b: UInt32, a: UInt32) -> UInt32 
{ 
    return (Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24) 
} 

-

func Mask8(x: UInt32) -> UInt32 
{ 
    return x & 0xFF 
} 

func R(x: UInt32) -> UInt32 
{ 
    return Mask8(x) 
} 

func G(x: UInt32) -> UInt32 
{ 
    return Mask8(x >> 8) 
} 

func B(x: UInt32) -> UInt32 
{ 
    return Mask8(x >> 16) 
} 

func A(x: UInt32) -> UInt32 
{ 
    return Mask8(x >> 24) 
} 

はRGBA値を処理した後、完全に新しい色を作成するには、この機能を使用しますピクセル配列を反復処理するには、そうする -

var currentPixel = inputPixels 
for _ in 0..<height 
    { 
     for i in 0..<width 
     { 
      let color: UInt32 = currentPixel.memory 
      if i < width - 1 
      { 
       print(NSString(format: "%3.0f", R(x: color), terminator: " ")) 
      } 
      else 
      { 
       print(NSString(format: "%3.0f", R(x: color))) 
      } 
      currentPixel += 1 
     } 
    } 
関連する問題