GPUImage源碼閱讀(三)

概述

GPUImage是一個(gè)著名的圖像處理開源庫,它讓你能夠在圖片、視頻、相機(jī)上使用GPU加速的濾鏡和其它特效茂洒。與CoreImage框架相比,可以根據(jù)GPUImage提供的接口瓶竭,使用自定義的濾鏡。項(xiàng)目地址:https://github.com/BradLarson/GPUImage
這篇文章主要是閱讀GPUImage框架中的GPUImageInput協(xié)議以及GPUImageOutput類的源碼斤贰。這兩個(gè)是GPUImage響應(yīng)鏈的基礎(chǔ)。以下是源碼內(nèi)容:
*** GPUImageInput***
GPUImageOutput

GPUImageInput

GPUImageInput 是GPUImage中的一個(gè)重要的協(xié)議盏触,實(shí)現(xiàn)這個(gè)協(xié)議的類表示這個(gè)類能接受幀緩存的輸入渗蟹,在響應(yīng)鏈中每一個(gè)中間節(jié)點(diǎn)都能夠接受輸入經(jīng)過它的處理之后又能輸出給下一個(gè)節(jié)點(diǎn)块饺。正式這樣的過程構(gòu)成了一個(gè)響應(yīng)鏈條雌芽,這也是疊加濾鏡授艰、組合濾鏡的基礎(chǔ)。

  • GPUImageInput 協(xié)議提供了方法列表世落,細(xì)節(jié)由實(shí)現(xiàn)的對(duì)象實(shí)現(xiàn)淮腾。GPUImage中實(shí)現(xiàn)GPUImageInput的協(xié)議的類比較多,常見的有 GPUImageFilter屉佳、GPUImageViewGPUImageRawDataOutput圆凰、GPUImageMovieWriter 等体箕。
@protocol GPUImageInput <NSObject>
// 準(zhǔn)備下一個(gè)要使用的幀
- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
// 設(shè)置輸入的幀緩沖對(duì)象以及紋理索引
- (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
// 下一個(gè)有效的紋理索引
- (NSInteger)nextAvailableTextureIndex;
// 設(shè)置目標(biāo)的尺寸
- (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
// 設(shè)置旋轉(zhuǎn)模式
- (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
// 輸出緩沖區(qū)的最大尺寸
- (CGSize)maximumOutputSize;
// 輸入處理結(jié)束
- (void)endProcessing;
// 是否忽略渲染目標(biāo)的更新
- (BOOL)shouldIgnoreUpdatesToThisTarget;
// 是否啟用渲染目標(biāo)
- (BOOL)enabled;
// 是否為單色輸入
- (BOOL)wantsMonochromeInput;
// 設(shè)置單色輸入
- (void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;
@end

GPUImageOutput

GPUImageOutput 表示該類能夠作為輸出挑童,輸出的是 GPUImageFramebuffer 對(duì)象跃须。該類的實(shí)現(xiàn)比較簡單,主要是實(shí)現(xiàn)了一些最基本的方法尽楔,這些方法不需要依賴具體細(xì)節(jié)第练,細(xì)節(jié)處理在子類中完成翔试。繼承 GPUImageOutput 的類也比較多复旬,比如:GPUImageFilterGPUImageVideoCamera壁涎、GPUImageStillCamera志秃、GPUImagePicture

  • 基本屬性
@interface GPUImageOutput : NSObject
{
    // 輸出的幀緩存對(duì)象
    GPUImageFramebuffer *outputFramebuffer;
   
   // target列表,target紋理索引列表
    NSMutableArray *targets, *targetTextureIndices;

    // 紋理尺寸
    CGSize inputTextureSize, cachedMaximumOutputSize, forcedMaximumSize;
    BOOL overrideInputSize;
    BOOL allTargetsWantMonochromeData;

    // 設(shè)置下一幀提取圖片
    BOOL usingNextFrameForImageCapture;
}
// 是否使用mipmaps
@property(readwrite, nonatomic) BOOL shouldSmoothlyScaleOutput;
// 是否忽略處理當(dāng)前Target
@property(readwrite, nonatomic) BOOL shouldIgnoreUpdatesToThisTarget;
@property(readwrite, nonatomic, retain) GPUImageMovieWriter *audioEncodingTarget;
// 當(dāng)前忽略處理的Target
@property(readwrite, nonatomic, unsafe_unretained) id<GPUImageInput> targetToIgnoreForUpdates;
// 每幀處理完回調(diào)
@property(nonatomic, copy) void(^frameProcessingCompletionBlock)(GPUImageOutput*, CMTime);
// 是否啟用渲染目標(biāo)
@property(nonatomic) BOOL enabled;
// 紋理選項(xiàng)
@property(readwrite, nonatomic) GPUTextureOptions outputTextureOptions;

  • 方法列表竟坛。GPUImageOutput 提供方法主要是以下幾種類型:1钧舌、幀緩沖對(duì)象管理;2崭歧、響應(yīng)鏈的管理撞牢;3、圖像提取屋彪。
// 設(shè)置輸入的幀緩沖對(duì)象以及紋理索引
- (void)setInputFramebufferForTarget:(id<GPUImageInput>)target atIndex:(NSInteger)inputTextureIndex;
// 輸出的幀緩沖對(duì)象
- (GPUImageFramebuffer *)framebufferForOutput;
// 刪除幀緩沖對(duì)象
- (void)removeOutputFramebuffer;
// 通知所有的Target
- (void)notifyTargetsAboutNewOutputTexture;

// 所有的Target列表
- (NSArray*)targets;
// 增加Target
- (void)addTarget:(id<GPUImageInput>)newTarget;
- (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
// 刪除Target
- (void)removeTarget:(id<GPUImageInput>)targetToRemove;
- (void)removeAllTargets;

// 強(qiáng)制按照傳入的尺寸處理
- (void)forceProcessingAtSize:(CGSize)frameSize;
- (void)forceProcessingAtSizeRespectingAspectRatio:(CGSize)frameSize;

// 從幀緩沖對(duì)象提取CGImage圖像
- (void)useNextFrameForImageCapture;
- (CGImageRef)newCGImageFromCurrentlyProcessedOutput;
// 使用靜態(tài)圖片做濾鏡紋理
- (CGImageRef)newCGImageByFilteringCGImage:(CGImageRef)imageToFilter;

// 從幀緩沖對(duì)象提取UIImage圖像
- (UIImage *)imageFromCurrentFramebuffer;
- (UIImage *)imageFromCurrentFramebufferWithOrientation:(UIImageOrientation)imageOrientation;
// 使用靜態(tài)圖片做濾鏡紋理
- (UIImage *)imageByFilteringImage:(UIImage *)imageToFilter;
- (CGImageRef)newCGImageByFilteringImage:(UIImage *)imageToFilter;

// 是否提供單色輸出
- (BOOL)providesMonochromeOutput;
  • 幀緩沖對(duì)象管理。包含了設(shè)置輸入的幀緩沖對(duì)象畜挥,獲取輸出的幀緩沖對(duì)象,以及移除輸?shù)膸彌_對(duì)象砰嘁。
- (void)setInputFramebufferForTarget:(id<GPUImageInput>)target atIndex:(NSInteger)inputTextureIndex;
{
    [target setInputFramebuffer:[self framebufferForOutput] atIndex:inputTextureIndex];
}

- (GPUImageFramebuffer *)framebufferForOutput;
{
    return outputFramebuffer;
}

- (void)removeOutputFramebuffer;
{
    outputFramebuffer = nil;
}
  • 響應(yīng)鏈管理。包含了增加Target斟冕,刪除Target,處理完成后通知各個(gè)Target處理景描。
- (void)notifyTargetsAboutNewOutputTexture;
{
    for (id<GPUImageInput> currentTarget in targets)
    {
        NSInteger indexOfObject = [targets indexOfObject:currentTarget];
        NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
        
        [self setInputFramebufferForTarget:currentTarget atIndex:textureIndex];
    }
}

- (NSArray*)targets;
{
    return [NSArray arrayWithArray:targets];
}

- (void)addTarget:(id<GPUImageInput>)newTarget;
{
    NSInteger nextAvailableTextureIndex = [newTarget nextAvailableTextureIndex];
    [self addTarget:newTarget atTextureLocation:nextAvailableTextureIndex];
    
    if ([newTarget shouldIgnoreUpdatesToThisTarget])
    {
        _targetToIgnoreForUpdates = newTarget;
    }
}

- (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
{
    if([targets containsObject:newTarget])
    {
        return;
    }
    
    cachedMaximumOutputSize = CGSizeZero;
    runSynchronouslyOnVideoProcessingQueue(^{
        [self setInputFramebufferForTarget:newTarget atIndex:textureLocation];
        [targets addObject:newTarget];
        [targetTextureIndices addObject:[NSNumber numberWithInteger:textureLocation]];
        
        allTargetsWantMonochromeData = allTargetsWantMonochromeData && [newTarget wantsMonochromeInput];
    });
}

- (void)removeTarget:(id<GPUImageInput>)targetToRemove;
{
    if(![targets containsObject:targetToRemove])
    {
        return;
    }
    
    if (_targetToIgnoreForUpdates == targetToRemove)
    {
        _targetToIgnoreForUpdates = nil;
    }
    
    cachedMaximumOutputSize = CGSizeZero;
    
    NSInteger indexOfObject = [targets indexOfObject:targetToRemove];
    NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];

    runSynchronouslyOnVideoProcessingQueue(^{
        [targetToRemove setInputSize:CGSizeZero atIndex:textureIndexOfTarget];
        [targetToRemove setInputRotation:kGPUImageNoRotation atIndex:textureIndexOfTarget];

        [targetTextureIndices removeObjectAtIndex:indexOfObject];
        [targets removeObject:targetToRemove];
        [targetToRemove endProcessing];
    });
}

- (void)removeAllTargets;
{
    cachedMaximumOutputSize = CGSizeZero;
    runSynchronouslyOnVideoProcessingQueue(^{
        for (id<GPUImageInput> targetToRemove in targets)
        {
            NSInteger indexOfObject = [targets indexOfObject:targetToRemove];
            NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
            
            [targetToRemove setInputSize:CGSizeZero atIndex:textureIndexOfTarget];
            [targetToRemove setInputRotation:kGPUImageNoRotation atIndex:textureIndexOfTarget];
        }
        [targets removeAllObjects];
        [targetTextureIndices removeAllObjects];
        
        allTargetsWantMonochromeData = YES;
    });
}
  • 提取圖像超棺。包含了提取得到 CGImage 和 UIImage 呵燕。
- (void)useNextFrameForImageCapture;
{

}

- (CGImageRef)newCGImageFromCurrentlyProcessedOutput;
{
    return nil;
}

- (CGImageRef)newCGImageByFilteringCGImage:(CGImageRef)imageToFilter;
{
    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithCGImage:imageToFilter];
    
    [self useNextFrameForImageCapture];
    [stillImageSource addTarget:(id<GPUImageInput>)self];
    [stillImageSource processImage];
    
    CGImageRef processedImage = [self newCGImageFromCurrentlyProcessedOutput];
    
    [stillImageSource removeTarget:(id<GPUImageInput>)self];
    return processedImage;
}

- (UIImage *)imageFromCurrentFramebuffer;
{
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    UIImageOrientation imageOrientation = UIImageOrientationLeft;
    switch (deviceOrientation)
    {
        case UIDeviceOrientationPortrait:
            imageOrientation = UIImageOrientationUp;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            imageOrientation = UIImageOrientationDown;
            break;
        case UIDeviceOrientationLandscapeLeft:
            imageOrientation = UIImageOrientationLeft;
            break;
        case UIDeviceOrientationLandscapeRight:
            imageOrientation = UIImageOrientationRight;
            break;
        default:
            imageOrientation = UIImageOrientationUp;
            break;
    }
    
    return [self imageFromCurrentFramebufferWithOrientation:imageOrientation];
}

- (UIImage *)imageFromCurrentFramebufferWithOrientation:(UIImageOrientation)imageOrientation;
{
    CGImageRef cgImageFromBytes = [self newCGImageFromCurrentlyProcessedOutput];
    UIImage *finalImage = [UIImage imageWithCGImage:cgImageFromBytes scale:1.0 orientation:imageOrientation];
    CGImageRelease(cgImageFromBytes);
    
    return finalImage;
}

- (UIImage *)imageByFilteringImage:(UIImage *)imageToFilter;
{
    CGImageRef image = [self newCGImageByFilteringCGImage:[imageToFilter CGImage]];
    UIImage *processedImage = [UIImage imageWithCGImage:image scale:[imageToFilter scale] orientation:[imageToFilter imageOrientation]];
    CGImageRelease(image);
    return processedImage;
}

總結(jié)

GPUImageInput 氧苍、GPUImageOutput 是構(gòu)成GPUImage響應(yīng)鏈的基礎(chǔ)泛范。如果一個(gè)類實(shí)現(xiàn)了 GPUImageInput 協(xié)議我們可以知道它能夠接收幀緩存對(duì)象的輸入,如果繼承了 GPUImageOutput 類罢荡,我們可以知道它能夠輸出幀緩存對(duì)象。如果兩個(gè)都具備惭缰,則表明既能處理輸入又可以輸出惧笛,比如 GPUImageFilter 逞泄,而這就是響應(yīng)鏈的基本要求。

源碼地址:GPUImage源碼閱讀系列 https://github.com/QinminiOS/GPUImage
系列文章地址:GPUImage源碼閱讀 http://www.reibang.com/nb/11749791

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末各谚,一起剝皮案震驚了整個(gè)濱河市到千,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌憔四,老刑警劉巖般眉,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甸赃,死亡現(xiàn)場(chǎng)離奇詭異冗酿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)裁替,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門弱判,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昌腰,你說我怎么就攤上這事」柙颍” “怎么了株婴?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長大审。 經(jīng)常有香客問我座哩,道長,這世上最難降的妖魔是什么根穷? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任屿良,我火速辦了婚禮,結(jié)果婚禮上尘惧,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好登舞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布悬荣。 她就那樣靜靜地躺著,像睡著了一般稽煤。 火紅的嫁衣襯著肌膚如雪囚戚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天匾二,我揣著相機(jī)與錄音拳芙,去河邊找鬼。 笑死舟扎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的譬猫。 我是一名探鬼主播羡疗,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼柳刮!你這毒婦竟也來了痒钝?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤午乓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敏释,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年钥顽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了靠汁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奶浦,死狀恐怖踢星,靈堂內(nèi)的尸體忽然破棺而出澳叉,到底是詐尸還是另有隱情沐悦,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布瓶殃,位于F島的核電站副签,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏修壕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一慈鸠、第九天 我趴在偏房一處隱蔽的房頂上張望灌具。 院中可真熱鬧,春花似錦督笆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凭豪。三九已至晒杈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拯钻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工拼余, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留刊驴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓舅柜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親致份。 傳聞我的和親對(duì)象是個(gè)殘疾皇子础拨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容