BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.25k stars 4.61k forks source link

Best way to resample & downsize images? #432

Open iamcam opened 12 years ago

iamcam commented 12 years ago

I'm running into enough memory problems on the 4S because it takes very large photos. I'm attempting to use forceProcessingAtSize: to take a smaller photograph (not offered by the AV presets), but that has two downsides: (assume the new size is 1000px wide)

  1. During preview, the output will be much larger than the preview size - 1000px vs 640px; That gets slow on devices
  2. Not forcing the preview size, but waiting to force right before the camera takes the photo now "zooms" in on the original, so I see only 1000px of the original 2448px.

Is there any way to resample the image with this framework so that I see the same things as the 2448px wide image, but in a new 1000px wide size?

If not, I assume I'll have to do something like this to the full-size image in the background and update my filters once the smaller image is available. http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

Any input is appreciated.

Thanks!

BradLarson commented 12 years ago

When you say that you used -forceProcessingAtSize: right before you took the photo, where did you place that call? Did you happen to have a crop filter that you were using this with? I know that there's currently something odd with crop filters and forced processing sizes, but that zooming in on the image sounds like a bug somewhere.

iamcam commented 12 years ago

Yes - it's literally the capturePhoto... call. My filter chain looks like [camera] -> [resize] -> [crop] -> [Fx]... So, in situation # 2 I described above,

[self.resizeFilter forceProcessingAtSize:CGSizeMake(1200.0, 1600.0)];

[self.stillCamera capturePhotoAsImageProcessedUpToFilter: self.squareCropFilter 
                                   withCompletionHandler:^(UIImage *processedImage, NSError *error) {

This does remind me of the zoom bug when trying to use the -forceProcessingAtSize: on crop filters, so I was surprised to see this pop up when using on the preceding filter.

iamcam commented 12 years ago

@BradLarson Do you have any ideas what the problem is? In theory the changes should be immediate and affect all downstream filters, but it really only works if the forced size is applied well before capture.

For now, though I don't think it's the best way, I will probably resize the image immediately after capture and discard the full-sized image to free up memory. This should be fine, right (I'm not even sure if the @autoreleasepool is necessary)?

GPUImageBrightnessFilter *tmpResize = [[GPUImageBrightnessFilter alloc] init];
[tmpResize forceProcessingAtSize:CGSizeMake(1200,1200)];

@autoreleasepool {
    self.selectedImage = [[GPUImagePicture alloc] initWithImage:[tmpResize imageByFilteringImage:processedImage] smoothlyScaleOutput:YES];
    processedImage = nil;
    tmpResize = nil;
}

I think the next question if/when this is solved, is how to even know the device camera(s) resolution before capture so we don't overshoot it, but I'll leave that for another ticket.