mahboudz
mahboudz

Reputation: 39376

Cropping a Pixel Buffer

I am using the following to turn image data from the camera into a UIImage. In order to save some time, and memory, I'd like to crop the image data before I turn it into an UIImage.

Ideally I pass in a cropRect, and get back a cropped UIImage. However, since the camera output could be sized differently based on whether I am using a photo or video preset, I may not know what dimensions to use for the cropRect. I could use a cropRect, similar to the focus or exposure points, that uses a CGPoint between (0,0) and (1,1) and do similarly for the CGSizeof the cropRect. Or I can get the dimensions of the sampleBuffer, before I call the following, and pass in an appropriate cropRect. I'd like some advice as to which I should use.

I also would like to know how best to crop in order not to have to create an entire UIImage and then crop it back down. Typically, I am only interested in keeping about 10-20% of the pixels. I assume I have to iterate through the pixels, and start copying the cropRect into a different pixel buffer, until I have all the pixels I want.

And keep in mind that there is possible rotation happening according to orientation.

+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer orientation:(UIImageOrientation) orientation
{
    // Create a UIImage from sample buffer data
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:(CGFloat)1.0 orientation:orientation];
    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}

In summary:

  1. Should I pass in a cropRect which specifies a rect between (0,0,0,0) and (1,1,1,1) or do I pass in a cropRect that specifies exact pixel locations like (50,50,100,100)?
  2. How best do I crop the pixel buffer?

Upvotes: 3

Views: 3520

Answers (1)

Jonathan Cichon
Jonathan Cichon

Reputation: 4406

I think you should use pixel as cropRect, as you have to convert the float-values to pixel-values at least at some point. The following code is not tested, but should give you the idea.

CGRect cropRect = CGRectMake(50, 50, 100, 100); // cropRect
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVReturn lock = CVPixelBufferLockBaseAddress(pixelBuffer, 0);
if (lock == kCVReturnSuccess) {
    int w = 0;
    int h = 0;
    int r = 0;
    int bytesPerPixel = 0;
    unsigned char *buffer;
    w = CVPixelBufferGetWidth(pixelBuffer);
    h = CVPixelBufferGetHeight(pixelBuffer);
    r = CVPixelBufferGetBytesPerRow(pixelBuffer);
    bytesPerPixel = r/w;
    buffer = CVPixelBufferGetBaseAddress(pixelBuffer);
    UIGraphicsBeginImageContext(cropRect.size); // create context for image storage, use cropRect as size
    CGContextRef c = UIGraphicsGetCurrentContext();
    unsigned char* data = CGBitmapContextGetData(c);
    if (data != NULL) {
        // iterate over the pixels in cropRect
        for(int y = cropRect.origin.y, yDest = 0; y<CGRectGetMaxY(cropRect); y++, yDest++) { 
            for(int x = cropRect.origin.x, xDest = 0; x<CGRectGetMaxX(cropRect); x++, xDest++) {
                int offset = bytesPerPixel*((w*y)+x); // offset calculation in cropRect
                int offsetDest = bytesPerPixel*((cropRect.size.width*yDest)+xDest); // offset calculation for destination image
                for (int i = 0; i<bytesPerPixel; i++) {
                    data[offsetDest+i]   = buffer[offset+i];
                }
            }
        }
    } 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

Upvotes: 2

Related Questions