Re: Getting immutable UIImage data pointer without copy?
Re: Getting immutable UIImage data pointer without copy?
- Subject: Re: Getting immutable UIImage data pointer without copy?
- From: Steve Christensen <email@hidden>
- Date: Wed, 15 Nov 2017 12:08:45 -0800
On Nov 14, 2017, at 8:37 PM, Rick Mann <email@hidden> wrote:
>
>> On Nov 14, 2017, at 20:18 , Jens Alfke <email@hidden> wrote:
>>
>>> On Nov 14, 2017, at 8:11 PM, Rick Mann <email@hidden> wrote:
>>>
>>> Maybe, at least for the bonus question. The bigger question is around the
>>> copy.
>>
>> Where pixmaps live is a pretty complex issue…
>
> Well, then let me rephrase it as "unnecessary copies." In this case, I
> (currently) need to manipulate the pixels to generate a new image. I will be
> moving this to a Metal 2 filter soon enough, but for pre-iOS 11 users, I'd
> still like to use as little memory as possible (we use a lot of memory in our
> app).
Rick, if you're trying to work with pixel data in a buffer then how about
something like this? It delivers premultiplied 8-bit RGBA pixels to manipulate.
I believe the only buffer of any significant size should be the pixel buffer
for the bitmap context. (The docs say that makeImage() is only supposed to do a
buffer copy on write so it should be using the context's buffer.)
func manipulateImage(_ image: UIImage) -> UIImage
{
let imageBounds = CGRect(origin: CGPoint.zero, size: image.size)
let width = Int(imageBounds.size.width)
let height = Int(imageBounds.size.height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue:
CGImageAlphaInfo.premultipliedLast.rawValue)
if let bitmap = CGContext(data: nil,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: 0,
space: colorSpace,
bitmapInfo: bitmapInfo.rawValue)
{
bitmap.draw(image.cgImage!, in: imageBounds)
let bytesPerRow = bitmap.bytesPerRow
var buffer = bitmap.data!.bindMemory(to: UInt8.self, capacity:
(bytesPerRow * height))
for row in 0 ..< height
{
var pixel = buffer
for _ in 0 ..< width
{
var red = pixel[0]
var green = pixel[1]
var blue = pixel[2]
var alpha = pixel[3]
processPixel(&red, &green, &blue, &alpha)
pixel[0] = red
pixel[1] = green
pixel[2] = blue
pixel[3] = alpha
pixel += 4
}
buffer += bytesPerRow
}
if let processedCGImage = bitmap.makeImage()
{
return UIImage(cgImage: processedCGImage)
}
}
return nil
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden