0

In Swift I have an image: (but {I think} the problem is how the 'self' is implemented) (in my viewDidLoad)

let colorSpace       = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel    = 4
let bitsPerComponent = 8
let bytesPerRow      = bytesPerPixel * self.cols // width
let bitmapInfo       = RGBA32.bitmapInfo
let minimapContext = CGContext(data: nil, width: self.cols, height: self.rows, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)
let buffer = minimapContext!.data
self.pixelBuffer = buffer!.bindMemory(to: RGBA32.self, capacity: self.cols * self.rows) // was let

var pixelIndex = 1

Then I loop over an array of like:

self.pixelBuffer[pixelIndex] = .blue

This all works fine without the 'self'.

I want to change some of the pixels, so I added the 'self' and defined it at the top of the ViewController class

Now change the pixels like:

self.pixelBuffer[newPixelIndex] = .lightblue

But I get a variety of errors: Cannot assign value of type 'UnsafeMutablePointer' to type 'RGBA32'

Cannot infer contextual base in reference to member 'lightblue'

Value of type 'RGBA32?' has no subscripts

Don't know if this helps but the pixelBuffer is defined ike:

var pixelBuffer: RGBA32? = nil

and here is RGBA32:

struct RGBA32: Equatable {
private var color: UInt32

init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
    color = (UInt32(red) << 24) | (UInt32(green) << 16) | (UInt32(blue) << 8) | (UInt32(alpha) << 0)
}

static let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Little.rawValue

static func ==(lhs: RGBA32, rhs: RGBA32) -> Bool {
    return lhs.color == rhs.color
}

static let black = RGBA32(red: 0, green: 0, blue: 0, alpha: 255)
static let red   = RGBA32(red: 255, green: 0, blue: 0, alpha: 255)
static let green = RGBA32(red: 24, green: 183, blue: 3, alpha: 255)
static let darkgreen = RGBA32(red: 70, green: 105, blue: 35, alpha: 255)
static let blue  = RGBA32(red: 0, green: 127, blue: 255, alpha: 255)
static let lightblue = RGBA32(red: 33, green: 255, blue: 255, alpha: 255)
static let brown = RGBA32(red: 127, green: 63, blue: 0, alpha: 255)

}

THANKS!!

Mattman85208
  • 1,858
  • 2
  • 29
  • 51

1 Answers1

1

The main problem is that you have declared self.pixelBuffer to be an Optional RGBA32. But that is not what pixelBuffer was before. It was a UnsafeMutablePointer<RGBA32>. Here is the declaration you need:

var pixelBuffer : UnsafeMutablePointer<RGBA32>!

All your issues will then go away, I think. Your

self.pixelBuffer[newPixelIndex] = .lightblue

then compiles for me.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You are absolutely correct! This is what I like so much about SO! So can I remove the ? and RGBA32, like: self.pixelBuffer[pixelIndex] = .blue or is it just better to leave them like: self.pixelBuffer?[pixelIndex] = RGBA32.blue ? – Mattman85208 Sep 22 '20 at 04:18
  • Depends whether you use `!` like I did or `?` like you did. – matt Sep 22 '20 at 04:26
  • When it runs I get Thread 1: EXC_BAD_ACCESS (code=1, address=0x10ce091e0) . on the line: self.pixelBuffer[pixelIndex] = .green – Mattman85208 Sep 22 '20 at 04:37
  • Sorry, the question was about compiling, I thought. What happens when you run is another matter. – matt Sep 22 '20 at 04:38