Badal Shah
Badal Shah

Reputation: 7602

GPUImage Take time to apply filter

I am working on GPUImage Library. I have set that particular filter will be applied on Collectionview's didselectitematindexpath method. This is my 1st project based on GPUImage library. I am successfully able to apply Filter on GPUImageview but it will take lot of time to apply filter. Please guide me , How can i make it speedy to apply.

Here is my code ,

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
       [filter removeAllTargets];

    if (indexPath.row==0) {
        NSLog(@"Normal Filter");
    }
    else if (indexPath.row==1)
    {
        filter = [[GPUImageSepiaFilter alloc] init];

    }
    else if (indexPath.row==2)
    {
        filter = [[GPUImagePixellateFilter alloc] init];

    }
    else if (indexPath.row==3)
    {
        filter = [[GPUImagePixellatePositionFilter alloc] init];

    }
    else if (indexPath.row==4)
    {
        filter = [[GPUImagePolkaDotFilter alloc] init];

    }
    else if (indexPath.row==5)
    {
        filter = [[GPUImageHalftoneFilter alloc] init];

    }
    else if (indexPath.row==6)
    {
        filter = [[GPUImageCrosshatchFilter alloc] init];

    }
    else if (indexPath.row==7)
    {
        filter = [[GPUImageColorInvertFilter alloc] init];

    }
    else if (indexPath.row==8)
    {
        filter = [[GPUImageGrayscaleFilter alloc] init];

    }
    else if (indexPath.row==9)
    {
        filter = [[GPUImageMonochromeFilter alloc] init];
        [(GPUImageMonochromeFilter *)filter setColor:(GPUVector4){0.0f, 0.0f, 1.0f, 1.f}];
    }
    else if (indexPath.row==10)
    {
        filter = [[GPUImageFalseColorFilter alloc] init];

    }
    else if (indexPath.row==11)
    {
        filter = [[GPUImageSoftEleganceFilter alloc] init];

    }
    else if (indexPath.row==12)
    {
        filter = [[GPUImageMissEtikateFilter alloc] init];

    }
    else if (indexPath.row==13)
    {
        filter = [[GPUImageAmatorkaFilter alloc] init];

    }
    else if (indexPath.row==14)
    {
        filter = [[GPUImageSaturationFilter alloc] init];
       [(GPUImageSaturationFilter *)filter setSaturation:2.0];
    }
    else if (indexPath.row==15)
    {
        filter = [[GPUImageContrastFilter alloc] init];
        [(GPUImageContrastFilter *)filter setContrast:1.5];


    }
    else if (indexPath.row==16)
    {
        filter = [[GPUImageBrightnessFilter alloc] init];
        [(GPUImageBrightnessFilter *)filter setBrightness:0.6];


    }
    else if (indexPath.row==17)
    {
        filter = [[GPUImageLevelsFilter alloc] init];

    }
    else if (indexPath.row==18)
    {
        filter = [[GPUImageRGBFilter alloc] init];

    }
    else if (indexPath.row==19)
    {
        filter = [[GPUImageHueFilter alloc] init];

    }
    else if (indexPath.row==20)
    {
        filter = [[GPUImageWhiteBalanceFilter alloc] init];

    }
    else if (indexPath.row==21)
    {
        filter = [[GPUImageExposureFilter alloc] init];

    }
    else if (indexPath.row==22)
    {
        filter = [[GPUImageSharpenFilter alloc] init];

    }
    else if (indexPath.row==23)
    {
        filter = [[GPUImageUnsharpMaskFilter alloc] init];

    }
    else if (indexPath.row==24)
    {
        filter = [[GPUImageGammaFilter alloc] init];

    }
    else if (indexPath.row==25)
    {
        filter = [[GPUImageToneCurveFilter alloc] init];
        [(GPUImageToneCurveFilter *)filter setBlueControlPoints:[NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)], [NSValue valueWithCGPoint:CGPointMake(0.5, 0.5)], [NSValue valueWithCGPoint:CGPointMake(1.0, 0.75)], nil]];
    }
    else if (indexPath.row==26)
    {
        filter = [[GPUImageHighlightShadowFilter alloc] init];

    }
    else if (indexPath.row==27)
    {
        filter = [[GPUImageHazeFilter alloc] init];

    }
    else if (indexPath.row==28)
    {
        filter = [[GPUImageAverageColor alloc] init];

    }
    else if (indexPath.row==29)
    {
        filter = [[GPUImageLuminosity alloc] init];

    }
    else if (indexPath.row==30)
    {
        filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramRGB];

    }
    else if (indexPath.row==31)
    {
        filter = [[GPUImageLuminanceThresholdFilter alloc] init];

    }
    else if (indexPath.row==32)
    {
        filter = [[GPUImageAdaptiveThresholdFilter alloc] init];

    }
    else if (indexPath.row==33)
    {
        filter = [[GPUImageAverageLuminanceThresholdFilter alloc] init];

    }
    else if (indexPath.row==34)
    {
        filter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0.0, 0.0, 1.0, 0.25)];
    }


    [sourcePicture addTarget:filter];
    [filter addTarget:self.vwGPUImagview];
    [sourcePicture processImage];


}

Output :-

enter image description here

EDIT :- When i write [filter removeAllTarget] some filter did not worked. and if i remove it then it worked completely. where is my mistake ?

EDIT 2 :- Demo Link

Upvotes: 3

Views: 968

Answers (1)

Brad Larson
Brad Larson

Reputation: 170309

Based on the code provided, you're doing a few things here that will really slow this down. First, you have an apparently useless GPUImageVideoCamera that you recreate multiple times. That can be removed.

Second, you're creating a new GPUImagePicture based on a UIImage every time you want to display a cell. That's going to be really slow to do. Instead, try using a single shared image and swap it out as cells are displayed. Also, restrict your filter size to the size of your thumbnails in the cells using -forceProcessingAtSize:. There's no sense in filtering a giant image, only to display a tiny thumbnail.

Finally, as I point out above, each time you swap in a new filter you remove the filter's outputs but not the image's. This means that each filter you swap to gets added as an output for your image, but never removed. Your image will build up an increasing list of output filters and will get slower and slower to process. Remove all outputs from your picture at the same time you remove all outputs from your previous filter.

You could also use -forceProcessingAtSize: on your filters there to restrict them to only the size of your full display, which may still be smaller than your source image.

Upvotes: 5

Related Questions