2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.3 KiB

  1. //
  2. // SCStillImageDepthBlurFilter.m
  3. // Snapchat
  4. //
  5. // Created by Brian Ng on 10/11/17.
  6. //
  7. #import "SCStillImageDepthBlurFilter.h"
  8. #import "SCCameraTweaks.h"
  9. #import "SCProcessingModuleUtils.h"
  10. @import CoreMedia;
  11. @implementation SCStillImageDepthBlurFilter {
  12. CIContext *_context;
  13. CIFilter *_filter;
  14. CVPixelBufferPoolRef _bufferPool;
  15. }
  16. - (instancetype)init
  17. {
  18. if (self = [super init]) {
  19. _context = [CIContext contextWithOptions:@{ kCIContextWorkingFormat : @(kCIFormatRGBAh) }];
  20. _filter = [CIFilter filterWithName:@"CIDepthBlurEffect"];
  21. }
  22. return self;
  23. }
  24. - (void)dealloc
  25. {
  26. CVPixelBufferPoolFlush(_bufferPool, kCVPixelBufferPoolFlushExcessBuffers);
  27. CVPixelBufferPoolRelease(_bufferPool);
  28. }
  29. - (NSData *)renderWithPhotoData:(NSData *)photoData renderData:(RenderData)renderData NS_AVAILABLE_IOS(11_0)
  30. {
  31. CIImage *mainImage = [CIImage imageWithData:photoData];
  32. CVPixelBufferRef disparityImagePixelBuffer = renderData.depthDataMap;
  33. CIImage *disparityImage = [CIImage imageWithCVPixelBuffer:disparityImagePixelBuffer];
  34. if (!disparityImage) {
  35. return photoData;
  36. }
  37. [_filter setValue:mainImage forKey:kCIInputImageKey];
  38. [_filter setValue:disparityImage forKey:kCIInputDisparityImageKey];
  39. if (renderData.depthBlurPointOfInterest && SCCameraTweaksEnableFilterInputFocusRect()) {
  40. CGPoint pointOfInterest = *renderData.depthBlurPointOfInterest;
  41. [_filter setValue:[CIVector vectorWithX:pointOfInterest.x Y:pointOfInterest.y Z:1 W:1]
  42. forKey:@"inputFocusRect"];
  43. }
  44. CIImage *result = [_filter outputImage];
  45. if (!result) {
  46. return photoData;
  47. }
  48. CGColorSpaceRef deviceRGBColorSpace = CGColorSpaceCreateDeviceRGB();
  49. NSData *processedPhotoData = [_context JPEGRepresentationOfImage:result colorSpace:deviceRGBColorSpace options:@{}];
  50. CGColorSpaceRelease(deviceRGBColorSpace);
  51. if (!processedPhotoData) {
  52. return photoData;
  53. }
  54. renderData.sampleBuffer = [SCProcessingModuleUtils sampleBufferFromImage:result
  55. oldSampleBuffer:renderData.sampleBuffer
  56. bufferPool:_bufferPool
  57. context:_context];
  58. return processedPhotoData;
  59. }
  60. @end