Hello everyone, It's a nice holiday. I changed pixel value from uiimage, so produce other image. Code is this.
func normalize(_ image: UIImage) -> UIImage {
let width = 320
let height = 240
let bytesPerComponent = 4
let channel = 1
let imageSize = width * height * bytesPerComponent
guard Int(image.size.width) == width,
Int(image.size.height) == height,
image.cgImage?.bitsPerComponent == 8,
image.cgImage?.bitsPerPixel == 8
else { // setting include width, height type is uint8 and 1 channel
fatalError("\(#function) : you should fixed image setting")
}
let cgimage = image.cgImage
guard let originalData = cgimage?.dataProvider?.data,
let original = CFDataGetBytePtr(originalData)
else {
fatalError("\(#function) : get original buffer failed.")
}
imageData = UnsafeMutablePointer<Float32>.allocate(capacity: width * height)
guard let imageData = imageData else { fatalError("\(#function) : unknwon error") }
// 0 ~ 255 (uint8) -> -1.0 ~ 1.0 (float32)
let normValue: Float32 = 127.5
for w in 0..<width {
for h in 0..<height {
imageData[h * width + w] = (Float32(original[h * width + w]) - normValue) / normValue
}
}
guard let provider = CGDataProvider(data: Data(bytesNoCopy: imageData,
count: imageSize,
deallocator: .none) as CFData)
else {
fatalError("\(#function) : provider load failed.")
}
guard let cgImage = CGImage(width: width,
height: height,
bitsPerComponent: bytesPerComponent * 8,
bitsPerPixel: bytesPerComponent * channel * 8,
bytesPerRow: bytesPerComponent * channel * width,
space: CGColorSpaceCreateDeviceGray(),
bitmapInfo: CGBitmapInfo(rawValue: 0),
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent)
else {
fatalError("\(#function) : create cgimage failed by unknown error")
}
return UIImage(cgImage: cgImage)
}
This code is done every frame, and produce this log, sometimes and logs are too much.
[Mars] mapData:843: *** ImageIO - mmapped file changed (old: 6782 new: 6875)
I don't know "843" mean and following number paste at tail of log, changed every times. I don't know it means warning or error and so on.
It works well, it seems no problem, But I worried about too many logs and thread problem. I just feel bad. Have you see this log anyone?