GPUImage源码解读(十五)
GPUImageRawDataOutput
GPUImageRawDataOutput实现了GPUImageInput协议,它可以将输入的帧缓存转换为原始数据。
构造方法。构造方法最主要的任务是构造GL程序。
```
(id)initWithImageSize:(CGSize)newImageSize resultsInBGRAFormat:(BOOL)resultsInBGRAFormat;
```
初始化的时候需要指定纹理大小以及是否以BGRA形式的数据输入。如果是以BGRA的形式输入,则在选择片段着色器的时候会选择kGPUImageColorSwizzlingFragmentShaderString着色器来进行从BGRA-到RGBA的转换。
其他方法
```
// Data access
// 获取特定位置的像素向量
(GPUByteColorVector)colorAtLocation:(CGPoint)locationInImage;
// 每行数据大小
(NSUInteger)bytesPerRowInOutput;
// 设置纹理大小
(void)setImageSize:(CGSize)newImageSize;
// 锁定、与解锁帧缓存
(void)lockFramebufferForReading;
(void)unlockFramebufferAfterReading;
```
方法实现如下:
``` // 获取特定位置的像素向量
(GPUByteColorVector)colorAtLocation:(CGPoint)locationInImage;
{
// 将数据转为GPUByteColorVector类型
GPUByteColorVector imageColorBytes = (GPUByteColorVector )self.rawBytesForImage;
// NSLog(@"Row start");
// for (unsigned int currentXPosition = 0; currentXPosition < (imageSize.width * 2.0); currentXPosition++)
// {
// GPUByteColorVector byteAtPosition = imageColorBytes[currentXPosition];
// NSLog(@"%d - %d, %d, %d", currentXPosition, byteAtPosition.red, byteAtPosition.green, byteAtPosition.blue);
// }
// NSLog(@"Row end");
// GPUByteColorVector byteAtOne = imageColorBytes[1]; // GPUByteColorVector byteAtWidth = imageColorBytes[(int)imageSize.width - 3]; // GPUByteColorVector byteAtHeight = imageColorBytes[(int)(imageSize.height - 1) * (int)imageSize.width]; // NSLog(@"Byte 1: %d, %d, %d, byte 2: %d, %d, %d, byte 3: %d, %d, %d", byteAtOne.red, byteAtOne.green, byteAtOne.blue, byteAtWidth.red, byteAtWidth.green, byteAtWidth.blue, byteAtHeight.red, byteAtHeight.green, byteAtHeight.blue);
}
// 每行数据大小
(NSUInteger)bytesPerRowInOutput;
{
return [retainedFramebuffer bytesPerRow];
}
// 设置输出纹理大小
(void)setImageSize:(CGSize)newImageSize {
imageSize = newImageSize;
if (_rawBytesForImage != NULL && (![GPUImageContext supportsFastTextureUpload]))
{
}
}
// 锁定帧缓存
(void)lockFramebufferForReading;
{
lockNextFramebuffer = YES;
}
// 解锁帧缓存
(void)unlockFramebufferAfterReading;
{
[retainedFramebuffer unlockAfterReading];
[retainedFramebuffer unlock];
retainedFramebuffer = nil;
}
// 获取RGBA数据
(GLubyte )rawBytesForImage; { if ( (_rawBytesForImage == NULL) && (![GPUImageContext supportsFastTextureUpload]) ) { // 申请空间,储存读取的数据 _rawBytesForImage = (GLubyte ) calloc(imageSize.width imageSize.height 4, sizeof(GLubyte)); hasReadFromTheCurrentFrame = NO; }
if (hasReadFromTheCurrentFrame) { return _rawBytesForImage; } else { runSynchronouslyOnVideoProcessingQueue(^{ // Note: the fast texture caches speed up 640x480 frame reads from 9.6 ms to 3.1 ms on iPhone 4S // 设置GL下文对象 [GPUImageContext useImageProcessingContext]; // 渲染到帧缓存 [self renderAtInternalSize];
} }
```
Last updated