GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Help with blend filter inside a group

Open peterhwong opened this issue 14 years ago • 6 comments

Hi Brad, I've successfully added a blend filter inside a filter group and have this working in FilterShowcase as well as SimpleVideoFilter test apps. However when I try this code in my app it doesn't work. The overlay doesn't appear, it just looks darker. I have tried the overlay filter outside of a group and get the same results. I made a simple test app and same thing happens. What is special about your test apps that work and mine which don't? I'm hoping for a AHA! moment from you. I have the test app zipped up if you have the time to look at it. I'm at wits end. Here is the code if it helps but like I said, this code pasted into your apps work fine:

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack]; camera.outputImageOrientation = UIDeviceOrientationPortrait;

    GPUImageFilterGroup *filter = [[GPUImageFilterGroup alloc] init];

    GPUImageGammaFilter *passthroughFilter = [[GPUImageGammaFilter alloc] init]; [filter addFilter:passthroughFilter];

    GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init]; [filter addFilter:sepiaFilter]; [passthroughFilter addTarget:sepiaFilter];

    GPUImageOverlayBlendFilter *blendFilter = [[GPUImageOverlayBlendFilter alloc] init]; [filter addFilter:blendFilter];

    UIImage *inputImage = [UIImage imageNamed:@"WID-small.jpg"]; GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];

    [sourcePicture addTarget:blendFilter atTextureLocation:1]; [sourcePicture processImage]; [sepiaFilter addTarget:blendFilter];

    [filter setInitialFilters:[NSArray arrayWithObject:passthroughFilter]]; [filter setTerminalFilter:blendFilter];

    [camera addTarget:filter]; [filter addTarget:(GPUImageView *)self.view]; [camera startCameraCapture]; }

Thanks!

peterhwong avatar Jul 31 '12 15:07 peterhwong

Hey Brad, I figured out the difference. If you turn on ARC in either of your apps, this problem will occur too. Do you know what is going on here?

Thanks Peter

peterhwong avatar Jul 31 '12 16:07 peterhwong

You need to retain your GPUImageFilterGroup as an instance variable. You never refer to it beyond the -viewDidLoad method, so ARC will deallocate it at the end of that method. Turning ARC off simply causes the above code to leak, because you've allocated an object that will never be deallocated afterward.

BradLarson avatar Jul 31 '12 17:07 BradLarson

Hi Brad, Sorry for the mistake. In my real app I do retain the group in an instance variable and I've now added this to my test app and I still get the same problem. I think there is something else in the group that is getting deallocated.

peterhwong avatar Jul 31 '12 18:07 peterhwong

Well, the FilterShowcase example is ARC-enabled and doesn't exhibit this, and I'm not sure what you mean by "either of your apps" given that there are 11 example applications. Some are ARC, some are not, so that I can test across both conditions. The framework itself is ARC-enabled, so changing the build settings of the application will only affect the code you introduced.

Again, check to make sure that every filter and input (like your picture in the code above, which will also be deallocated under ARC) is being retained by something. You'll need to hold on to that picture as an instance variable as well.

Also, you might need to move the [sourcePicture processImage]; line to after you've targeted the sepiaFilter to the blend filter, but I'm not sure about that.

BradLarson avatar Jul 31 '12 18:07 BradLarson

Hey Brad, That was it. The source picture wasn't being retained. Sorry, I'm not used to ARC as you can tell. Thanks for your help!!!

peterhwong avatar Jul 31 '12 18:07 peterhwong

Don't worry about it, the cleanup that ARC does has caught me a couple of times in throwaway projects when I forgot to root all of my inputs as instance variables or the like. It forces us to code cleanly, sometimes in surprising ways.

BradLarson avatar Jul 31 '12 18:07 BradLarson