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.

79 lines
2.6 KiB

  1. //
  2. // SCCameraSettingUtils.m
  3. // Snapchat
  4. //
  5. // Created by Pinlin Chen on 12/09/2017.
  6. //
  7. #import "SCCameraSettingUtils.h"
  8. #import <SCFoundation/SCLog.h>
  9. #import <ImageIO/CGImageProperties.h>
  10. NSNumber *retrieveExposureTimeFromEXIFAttachments(CFDictionaryRef exifAttachments)
  11. {
  12. if (!exifAttachments) {
  13. return nil;
  14. }
  15. id value = CFDictionaryGetValue(exifAttachments, kCGImagePropertyExifExposureTime);
  16. // Fetching exposure time from the sample buffer
  17. if ([value isKindOfClass:[NSNumber class]]) {
  18. return (NSNumber *)value;
  19. }
  20. return nil;
  21. }
  22. NSNumber *retrieveBrightnessFromEXIFAttachments(CFDictionaryRef exifAttachments)
  23. {
  24. if (!exifAttachments) {
  25. return nil;
  26. }
  27. id value = CFDictionaryGetValue(exifAttachments, kCGImagePropertyExifBrightnessValue);
  28. if ([value isKindOfClass:[NSNumber class]]) {
  29. return (NSNumber *)value;
  30. }
  31. return nil;
  32. }
  33. NSNumber *retrieveISOSpeedRatingFromEXIFAttachments(CFDictionaryRef exifAttachments)
  34. {
  35. if (!exifAttachments) {
  36. return nil;
  37. }
  38. NSArray *ISOSpeedRatings = CFDictionaryGetValue(exifAttachments, kCGImagePropertyExifISOSpeedRatings);
  39. if ([ISOSpeedRatings respondsToSelector:@selector(count)] &&
  40. [ISOSpeedRatings respondsToSelector:@selector(firstObject)] && ISOSpeedRatings.count > 0) {
  41. id value = [ISOSpeedRatings firstObject];
  42. if ([value isKindOfClass:[NSNumber class]]) {
  43. return (NSNumber *)value;
  44. }
  45. }
  46. return nil;
  47. }
  48. void retrieveSampleBufferMetadata(CMSampleBufferRef sampleBuffer, SampleBufferMetadata *metadata)
  49. {
  50. CFDictionaryRef exifAttachments = CMGetAttachment(sampleBuffer, kCGImagePropertyExifDictionary, NULL);
  51. if (exifAttachments == nil) {
  52. SCLogCoreCameraWarning(@"SampleBuffer exifAttachment is nil");
  53. }
  54. // Fetching exposure time from the sample buffer
  55. NSNumber *currentExposureTimeNum = retrieveExposureTimeFromEXIFAttachments(exifAttachments);
  56. if (currentExposureTimeNum) {
  57. metadata->exposureTime = [currentExposureTimeNum floatValue];
  58. }
  59. NSNumber *currentISOSpeedRatingNum = retrieveISOSpeedRatingFromEXIFAttachments(exifAttachments);
  60. if (currentISOSpeedRatingNum) {
  61. metadata->isoSpeedRating = (int)[currentISOSpeedRatingNum integerValue];
  62. }
  63. NSNumber *currentBrightnessNum = retrieveBrightnessFromEXIFAttachments(exifAttachments);
  64. if (currentBrightnessNum) {
  65. float currentBrightness = [currentBrightnessNum floatValue];
  66. if (isfinite(currentBrightness)) {
  67. metadata->brightness = currentBrightness;
  68. } else {
  69. metadata->brightness = 0;
  70. }
  71. }
  72. }