GPUImageRawDataOutput.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #import "GPUImageRawDataOutput.h"
  2. #import "GPUImageContext.h"
  3. #import "GLProgram.h"
  4. #import "GPUImageFilter.h"
  5. #import "GPUImageMovieWriter.h"
  6. @interface GPUImageRawDataOutput ()
  7. {
  8. GPUImageFramebuffer *firstInputFramebuffer, *outputFramebuffer, *retainedFramebuffer;
  9. BOOL hasReadFromTheCurrentFrame;
  10. GLProgram *dataProgram;
  11. GLint dataPositionAttribute, dataTextureCoordinateAttribute;
  12. GLint dataInputTextureUniform;
  13. GLubyte *_rawBytesForImage;
  14. BOOL lockNextFramebuffer;
  15. }
  16. // Frame rendering
  17. - (void)renderAtInternalSize;
  18. @end
  19. @implementation GPUImageRawDataOutput
  20. @synthesize rawBytesForImage = _rawBytesForImage;
  21. @synthesize newFrameAvailableBlock = _newFrameAvailableBlock;
  22. @synthesize enabled;
  23. #pragma mark -
  24. #pragma mark Initialization and teardown
  25. - (id)initWithImageSize:(CGSize)newImageSize resultsInBGRAFormat:(BOOL)resultsInBGRAFormat;
  26. {
  27. if (!(self = [super init]))
  28. {
  29. return nil;
  30. }
  31. self.enabled = YES;
  32. lockNextFramebuffer = NO;
  33. outputBGRA = resultsInBGRAFormat;
  34. imageSize = newImageSize;
  35. hasReadFromTheCurrentFrame = NO;
  36. _rawBytesForImage = NULL;
  37. inputRotation = kGPUImageNoRotation;
  38. [GPUImageContext useImageProcessingContext];
  39. if ( (outputBGRA && ![GPUImageContext supportsFastTextureUpload]) || (!outputBGRA && [GPUImageContext supportsFastTextureUpload]) )
  40. {
  41. dataProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageColorSwizzlingFragmentShaderString];
  42. }
  43. else
  44. {
  45. dataProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImagePassthroughFragmentShaderString];
  46. }
  47. if (!dataProgram.initialized)
  48. {
  49. [dataProgram addAttribute:@"position"];
  50. [dataProgram addAttribute:@"inputTextureCoordinate"];
  51. if (![dataProgram link])
  52. {
  53. NSString *progLog = [dataProgram programLog];
  54. NSLog(@"Program link log: %@", progLog);
  55. NSString *fragLog = [dataProgram fragmentShaderLog];
  56. NSLog(@"Fragment shader compile log: %@", fragLog);
  57. NSString *vertLog = [dataProgram vertexShaderLog];
  58. NSLog(@"Vertex shader compile log: %@", vertLog);
  59. dataProgram = nil;
  60. NSAssert(NO, @"Filter shader link failed");
  61. }
  62. }
  63. dataPositionAttribute = [dataProgram attributeIndex:@"position"];
  64. dataTextureCoordinateAttribute = [dataProgram attributeIndex:@"inputTextureCoordinate"];
  65. dataInputTextureUniform = [dataProgram uniformIndex:@"inputImageTexture"];
  66. return self;
  67. }
  68. - (void)dealloc
  69. {
  70. if (_rawBytesForImage != NULL && (![GPUImageContext supportsFastTextureUpload]))
  71. {
  72. free(_rawBytesForImage);
  73. _rawBytesForImage = NULL;
  74. }
  75. }
  76. #pragma mark -
  77. #pragma mark Data access
  78. - (void)renderAtInternalSize;
  79. {
  80. [GPUImageContext setActiveShaderProgram:dataProgram];
  81. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:imageSize onlyTexture:NO];
  82. [outputFramebuffer activateFramebuffer];
  83. if(lockNextFramebuffer)
  84. {
  85. retainedFramebuffer = outputFramebuffer;
  86. [retainedFramebuffer lock];
  87. [retainedFramebuffer lockForReading];
  88. lockNextFramebuffer = NO;
  89. }
  90. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  91. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. static const GLfloat squareVertices[] = {
  93. -1.0f, -1.0f,
  94. 1.0f, -1.0f,
  95. -1.0f, 1.0f,
  96. 1.0f, 1.0f,
  97. };
  98. static const GLfloat textureCoordinates[] = {
  99. 0.0f, 0.0f,
  100. 1.0f, 0.0f,
  101. 0.0f, 1.0f,
  102. 1.0f, 1.0f,
  103. };
  104. glActiveTexture(GL_TEXTURE4);
  105. glBindTexture(GL_TEXTURE_2D, [firstInputFramebuffer texture]);
  106. glUniform1i(dataInputTextureUniform, 4);
  107. glVertexAttribPointer(dataPositionAttribute, 2, GL_FLOAT, 0, 0, squareVertices);
  108. glVertexAttribPointer(dataTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
  109. glEnableVertexAttribArray(dataPositionAttribute);
  110. glEnableVertexAttribArray(dataTextureCoordinateAttribute);
  111. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  112. [firstInputFramebuffer unlock];
  113. }
  114. - (GPUByteColorVector)colorAtLocation:(CGPoint)locationInImage;
  115. {
  116. GPUByteColorVector *imageColorBytes = (GPUByteColorVector *)self.rawBytesForImage;
  117. // NSLog(@"Row start");
  118. // for (unsigned int currentXPosition = 0; currentXPosition < (imageSize.width * 2.0); currentXPosition++)
  119. // {
  120. // GPUByteColorVector byteAtPosition = imageColorBytes[currentXPosition];
  121. // NSLog(@"%d - %d, %d, %d", currentXPosition, byteAtPosition.red, byteAtPosition.green, byteAtPosition.blue);
  122. // }
  123. // NSLog(@"Row end");
  124. // GPUByteColorVector byteAtOne = imageColorBytes[1];
  125. // GPUByteColorVector byteAtWidth = imageColorBytes[(int)imageSize.width - 3];
  126. // GPUByteColorVector byteAtHeight = imageColorBytes[(int)(imageSize.height - 1) * (int)imageSize.width];
  127. // 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);
  128. CGPoint locationToPickFrom = CGPointZero;
  129. locationToPickFrom.x = MIN(MAX(locationInImage.x, 0.0), (imageSize.width - 1.0));
  130. locationToPickFrom.y = MIN(MAX((imageSize.height - locationInImage.y), 0.0), (imageSize.height - 1.0));
  131. if (outputBGRA)
  132. {
  133. GPUByteColorVector flippedColor = imageColorBytes[(int)(round((locationToPickFrom.y * imageSize.width) + locationToPickFrom.x))];
  134. GLubyte temporaryRed = flippedColor.red;
  135. flippedColor.red = flippedColor.blue;
  136. flippedColor.blue = temporaryRed;
  137. return flippedColor;
  138. }
  139. else
  140. {
  141. return imageColorBytes[(int)(round((locationToPickFrom.y * imageSize.width) + locationToPickFrom.x))];
  142. }
  143. }
  144. #pragma mark -
  145. #pragma mark GPUImageInput protocol
  146. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
  147. {
  148. hasReadFromTheCurrentFrame = NO;
  149. if (_newFrameAvailableBlock != NULL)
  150. {
  151. _newFrameAvailableBlock();
  152. }
  153. }
  154. - (NSInteger)nextAvailableTextureIndex;
  155. {
  156. return 0;
  157. }
  158. - (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
  159. {
  160. firstInputFramebuffer = newInputFramebuffer;
  161. [firstInputFramebuffer lock];
  162. }
  163. - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
  164. {
  165. inputRotation = newInputRotation;
  166. }
  167. - (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
  168. {
  169. }
  170. - (CGSize)maximumOutputSize;
  171. {
  172. return imageSize;
  173. }
  174. - (void)endProcessing;
  175. {
  176. }
  177. - (BOOL)shouldIgnoreUpdatesToThisTarget;
  178. {
  179. return NO;
  180. }
  181. - (BOOL)wantsMonochromeInput;
  182. {
  183. return NO;
  184. }
  185. - (void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;
  186. {
  187. }
  188. #pragma mark -
  189. #pragma mark Accessors
  190. - (GLubyte *)rawBytesForImage;
  191. {
  192. if ( (_rawBytesForImage == NULL) && (![GPUImageContext supportsFastTextureUpload]) )
  193. {
  194. _rawBytesForImage = (GLubyte *) calloc(imageSize.width * imageSize.height * 4, sizeof(GLubyte));
  195. hasReadFromTheCurrentFrame = NO;
  196. }
  197. if (hasReadFromTheCurrentFrame)
  198. {
  199. return _rawBytesForImage;
  200. }
  201. else
  202. {
  203. runSynchronouslyOnVideoProcessingQueue(^{
  204. // Note: the fast texture caches speed up 640x480 frame reads from 9.6 ms to 3.1 ms on iPhone 4S
  205. [GPUImageContext useImageProcessingContext];
  206. [self renderAtInternalSize];
  207. if ([GPUImageContext supportsFastTextureUpload])
  208. {
  209. glFinish();
  210. _rawBytesForImage = [outputFramebuffer byteBuffer];
  211. }
  212. else
  213. {
  214. glReadPixels(0, 0, imageSize.width, imageSize.height, GL_RGBA, GL_UNSIGNED_BYTE, _rawBytesForImage);
  215. // GL_EXT_read_format_bgra
  216. // glReadPixels(0, 0, imageSize.width, imageSize.height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, _rawBytesForImage);
  217. }
  218. hasReadFromTheCurrentFrame = YES;
  219. });
  220. return _rawBytesForImage;
  221. }
  222. }
  223. - (NSUInteger)bytesPerRowInOutput;
  224. {
  225. return [retainedFramebuffer bytesPerRow];
  226. }
  227. - (void)setImageSize:(CGSize)newImageSize {
  228. imageSize = newImageSize;
  229. if (_rawBytesForImage != NULL && (![GPUImageContext supportsFastTextureUpload]))
  230. {
  231. free(_rawBytesForImage);
  232. _rawBytesForImage = NULL;
  233. }
  234. }
  235. - (void)lockFramebufferForReading;
  236. {
  237. lockNextFramebuffer = YES;
  238. }
  239. - (void)unlockFramebufferAfterReading;
  240. {
  241. [retainedFramebuffer unlockAfterReading];
  242. [retainedFramebuffer unlock];
  243. retainedFramebuffer = nil;
  244. }
  245. @end