2

I'm trying to convert objective c code into swift.

UInt8* data = calloc(bytesPerRow, height);

when I convert it in swift, It returning "UnsafeMutableRawPointer". I need of UInt8.

let data = calloc(bytesPerRow, height)! // UnsafeMutableRawPointer

I've tried different solution but didn't worked for me. Can anyone please tell me how I can resolved this issue. Thanks

Value of type 'UnsafeMutableRawPointer' has no subscripts

enter image description here

Swift Code.

 // data pointer – stores an array of the pixel components. For example (r0, b0, g0, a0, r1, g1, b1, a1 .... rn, gn, bn, an)
        //*calloc(size_t __count, size_t __size) __result_use_check __alloc_size(1,2);
        let data = calloc(bytesPerRow, height)!

        // get new RGB color space
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        // create bitmap context
        let ctx = CGContext(data: data, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

        // draw image into context (populating the data array while doing so)
        ctx?.draw(rawImage!, in: rect)

        // get the index of the pixel (4 components times the x position plus the y position times the row width)
        var pixelIndex = 4 * (pixelPosition.x + (pixelPosition.y * width))

        // set the pixel components to the color components
        data[pixelIndex] = color255Components[0]; // r
        data[pixelIndex+1] = color255Components[1]; // g
        data[pixelIndex+2] = color255Components[2]; // b
        data[pixelIndex+3] = color255Components[3]; // a

Objective C code.

// data pointer – stores an array of the pixel components. For example (r0, b0, g0, a0, r1, g1, b1, a1 .... rn, gn, bn, an)
    UInt8* data = calloc(bytesPerRow, height);

    // get new RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create bitmap context
    CGContextRef ctx = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

    // draw image into context (populating the data array while doing so)
    CGContextDrawImage(ctx, rect, rawImage);

    // get the index of the pixel (4 components times the x position plus the y position times the row width)
    NSInteger pixelIndex = 4*(pixelPosition.x+(pixelPosition.y*width));

    // set the pixel components to the color components
    data[pixelIndex] = color255Components[0]; // r
    data[pixelIndex+1] = color255Components[1]; // g
    data[pixelIndex+2] = color255Components[2]; // b
    data[pixelIndex+3] = color255Components[3]; // a
ZAFAR007
  • 3,049
  • 1
  • 34
  • 45
  • Possible duplicate of: https://stackoverflow.com/q/52733309/1032151 – MANIAK_dobrii Sep 02 '19 at 12:23
  • @MANIAK_dobrii, I've seen this already. there is only one parameter in " UnsafeMutablePointer.allocate(capacity: Int)". If you see objective c code "*calloc(size_t __count, size_t __size)" it taking two parameters – ZAFAR007 Sep 02 '19 at 12:29
  • @ZAFAR007: That answer applies to your case as well. `calloc()` takes two parameter which are simply multiplied. Therefore `UnsafeMutablePointer.allocate(capacity: bytesPerRow * height)` would do the trick. – Martin R Sep 02 '19 at 12:38
  • thanks @Martin R and MANIAK_dobrii. I had no idea about multiplied, It also working. – ZAFAR007 Sep 02 '19 at 12:49
  • 1
    thanks @Rob, yes I'm freeing it using this 'free(data)' on end. – ZAFAR007 Sep 02 '19 at 21:57

1 Answers1

5

You need to assign a type to your mutable pointer. In the current state it is a mutable pointer to anything but you want it to be a mutable pointer to a buffer of UInt8 values.

To do so simply call assumingMemoryBound on your data:

let data = calloc(bytesPerRow, height)!.assumingMemoryBound(to: UInt8.self)
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • thanks, it working, you saved my time. I was trying to resolve this from many hours. – ZAFAR007 Sep 02 '19 at 12:32
  • @ZAFAR007 good to hear that. I was a bit unsure in this case if `bindMemory` should be called instead of `assumingMemoryBound`. These things are never clear enough. – Matic Oblak Sep 02 '19 at 12:35