swiftimage-processingnsbitmapimagerep

Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors


I'm trying to create an NSBitmapImageRep object with 32 bits per sample and an alpha channel (128 bits per pixel in total).

My code looks like this:

let renderSize = NSSize(width: 640, height: 360)
let bitmapRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(renderSize.width), pixelsHigh: Int(renderSize.height), bitsPerSample: 32, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSCalibratedRGBColorSpace, bytesPerRow: 16 * Int(renderSize.width), bitsPerPixel: 128)
println(bitmapRep) //prints "nil"
println(16 * Int(renderSize.width)) //prints 10240
println()

//So what does a 'valid' 32bpc TIFF file with an alpha channel look like?
let imgFile = NSImage(named: "32grad") //http://i.peterwunder.de/32grad.tif
let imgRep = imgFile?.representations[0] as NSBitmapImageRep
println(imgRep.bitsPerSample) //prints 32
println(imgRep.bitsPerPixel) //prints 128
println(imgRep.samplesPerPixel) //prints 4
println(imgRep.bytesPerRow) //prints 10240
println(imgRep.bytesPerRow / Int(imgFile!.size.width)) //prints 16

This appears in the console after executing line 2:

2014-12-31 04:49:16.639 PixelTestBed[4413:2775703] Inconsistent set of values to create NSBitmapImageRep

What's going on here? Why can't I manually create an NSBitmapImageRep with the exact same values that TIFF image has?

By the way, I can't upload the image here because imgur would butcher the image's quality. It's a 3,7 MB 32bpc TIFF with an alpha channel, after all.


Solution

  • You are trying to assign 32 bitsPerSample which is out of the range specified in the docs:

    bps

    The number of bits used to specify one pixel in a single component of the data. All components are assumed to have the same bits per sample. bps should be one of these values: 1, 2, 4, 8, 12, or 16.

    NSBitmapImageRep Class Reference