I am writing a perceptual matching program (to practically learn Swift). I have the following challenge:
I am having a class to convert a CGImage to a bitmap to subsequently read the individual pixels (see How do I load and edit a bitmap file at the pixel level in Swift for iOS?)
class Bitmap {
let width: Int
let height: Int
let context: CGContextRef
init(img: CGImage) {
// Set image width, height
width = CGImageGetWidth(img)
height = CGImageGetHeight(img)
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.
let bitmapBytesPerRow = width * 4
// Use the generic RGB color space.
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
// Create the bitmap context.
context = CGBitmapContextCreate(nil, width, height, 8, bitmapBytesPerRow, colorSpace, bitmapInfo.rawValue)!
// draw the image onto the context
let rect = CGRect(x: 0, y: 0, width: width, height: height)
CGContextDrawImage(context, rect, img)
}
deinit {
}
func color_at(x: Int, y: Int)->(Int, Int, Int, Int) {
assert(0<=x && x<width)
assert(0<=y && y<height)
let uncasted_data = CGBitmapContextGetData(context)
let data = UnsafePointer<UInt8>(uncasted_data)
let offset = 4 * (y * width + x)
let alpha = data[offset]
let red = data[offset+1]
let green = data[offset+2]
let blue = data[offset+3]
let color = (Int(red), Int(green), Int(blue), Int(alpha))
return color
}
}
When I am declaring instances of Bitmap in my "main" function, all works fine. However, when I am using it in functions, the associated memory does not get released when the function terminates. As I am converting images, the RAM increases constantly and my iMac crashes. Any thoughts why the memory does not get related?
To illustrate:
func fingerprintImage(fileName: String) -> Int {
//
// Load the image and convert to bitmap
//
let url = NSURL(fileURLWithPath: fileName)
let image:CIImage=CIImage(contentsOfURL: url)!
let bitmap = Bitmap(img: convertCIImageToCGImage(image))
return 0
}
let fileManager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath("<... PATH TO DIRECTORY WITH IMAGES, EG CANON CR2>")!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("CR2") {
var fp2 = fingerprintImage(""<... PATH TO DIRECTORY WITH IMAGES, EG CANON CR2>"/"+element)
}
}
I would have expected that bitmap gets released when fingerprintImage terminates but that is not the case - after about 50 images I run out of memory.