Reputation: 21
I'm using GPUImage in my app, and I'm stuck trying to change the brightness and contrast from image.
I created a GPUImageView with a source picture, and set the brightness filter and contrast filter that will be modified with a UISlider
so here is my code:
first at all
.h
__weak IBOutlet GPUImageView* imageView;
.m
GPUImagePicture* sourcePicture;
GPUImageBrightnessFilter *brightnessFilter;
GPUImageContrastFilter *contrastFilter;
when my view controller, is loaded I do the next thing:
UIImage* image = "[image fromAnotherViewController]";
sourcePicture = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];
brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
contrastFilter = [[GPUImageContrastFilter alloc] init];
[brightnessFilter forceProcessingAtSize:imageView.sizeInPixels];
[contrastFilter forceProcessingAtSize:imageView.sizeInPixels];
[sourcePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:imageViewBewo];
[sourcePicture processImage];
and here is Contrast and Brightness value changed methods
- (IBAction)brightnessValueChanged:(id)sender
{
[brightnessFilter setBrightness:brightnessSlider.value];
[sourcePicture processImage];
}
- (IBAction)contrastValueChanged:(id)sender
{
[contrastFilter setContrast:contrastSlider.value];
[sourcePicture processImage];
}
everything is ok until here,so I have a button to apply a filter to the image
- (IBAction) applyFilterAction:(id)sender
{
[sourcePicture removeAllTargets];
GPUImageLordKelvinFilter* filterEffect = [[GPUImageLordKelvinFilter alloc] init];
[filterEffect forceProcessingAtSize:imageView.sizeInPixels];
[brightnessFilter forceProcessingAtSize:imageView.sizeInPixels];
[contrastFilter forceProcessingAtSize:imageView.sizeInPixels];
[sourcePicture addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[brightnessFilter addTarget:filterEffect];
[filterEffect addTarget:imageView];
[brightnessFilter setBrightness:brightnessSlider.value];
[contrastFilter setContrast:contrastSlider.value];
[sourcePicture processImage];
}
the filter is applied correctly, but when I move my slider, the imageView back to the original image, with contrast and brightness correctly modified but without the filter effect.
but, if I modify the brightness or contrast at the same time I apply the filter effect, brightness or contrast are applied correctly in the filter effect, for example something like that:
// in applyFilterAction: methods
.
.
.
[filterEffect addTarget:imageView];
[brightnessFilter setBrightness:-0.5];
[contrastFilter setContrast:2.5];
[sourcePicture processImage];
.
.
.
Can anyone give me any help to apply filters and modify their values, I think that I'm doing something wrong with addTarget
methods, I was searching something about that, but couldn't found nothing
Upvotes: 2
Views: 1476
Reputation: 864
try these..i hope it helps you..
-(void)viewDidLoad
{
[sliderChange setMinimumValue:-0.5];
[sliderChange setMaximumValue:0.5];
[sliderChange setValue:0.0];
brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
}
-(IBAction)upDateSliderValue:(id)sender
{
GPUImagePicture *fx_image;
fx_image = [[GPUImagePicture alloc] initWithImage:originalImage];
[brightnessFilter setBrightness:self.sliderChange.value];
[fx_image addTarget:brightnessFilter];
[fx_image processImage];
UIImage *final_image = [brightnessFilter imageFromCurrentlyProcessedOutput];
self.selectedImageView.image = final_image;
}
Upvotes: 0
Reputation: 170317
Your -applyFilterAction:
method is breaking your filter chain to introduce your new filter. If that's what you want, fine, but in doing so things are getting chained a little oddly.
In your original chain you go
sourcePicture -> brightnessFilter -> contrastFilter > imageView
In the new chain, this is now
sourcePicture -> brightnessFilter -> filterEffect -> imageView
-> contrastFilter > imageView
You have introduced a branching at the brightnessFilter by making it target both the filterEffect and contrastFilter filters. The contrastFilter was never removed from targeting imageView, so it keeps targeting that.
Sending two targets to something that expects a single input (like a GPUImageView) may result in undefined behavior. Something about that chaining is wrong. Either you mean to chain the filterEffect after your contrastFilter, or your filterEffect and contrastFilter need to target two separate GPUImageViews. In the former case, you'll need to remove the target from the contrastFilter to the imageView before setting up the rest of the chain.
It might be best to call -removeAllTargets
on each of the filters in the chain before resetting the filter chain, in order to make sure that there aren't any dangling connections like this.
Upvotes: 1