Authored by DreamPiggy

Add image format detect of HEIC (One type of HEIF which use HEVC codec). This is…

… supported natively in iOS 11 & macOS 10.13
@@ -16,7 +16,8 @@ typedef NS_ENUM(NSInteger, SDImageFormat) { @@ -16,7 +16,8 @@ typedef NS_ENUM(NSInteger, SDImageFormat) {
16 SDImageFormatPNG, 16 SDImageFormatPNG,
17 SDImageFormatGIF, 17 SDImageFormatGIF,
18 SDImageFormatTIFF, 18 SDImageFormatTIFF,
19 - SDImageFormatWebP 19 + SDImageFormatWebP,
  20 + SDImageFormatHEIC
20 }; 21 };
21 22
22 @interface NSData (ImageContentType) 23 @interface NSData (ImageContentType)
@@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
21 return SDImageFormatUndefined; 21 return SDImageFormatUndefined;
22 } 22 }
23 23
  24 + // File signatures table: http://www.garykessler.net/library/file_sigs.html
24 uint8_t c; 25 uint8_t c;
25 [data getBytes:&c length:1]; 26 [data getBytes:&c length:1];
26 switch (c) { 27 switch (c) {
@@ -33,16 +34,26 @@ @@ -33,16 +34,26 @@
33 case 0x49: 34 case 0x49:
34 case 0x4D: 35 case 0x4D:
35 return SDImageFormatTIFF; 36 return SDImageFormatTIFF;
36 - case 0x52:  
37 - // R as RIFF for WEBP  
38 - if (data.length < 12) {  
39 - return SDImageFormatUndefined; 37 + case 0x52: {
  38 + if (data.length >= 12) {
  39 + //RIFF....WEBP
  40 + NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
  41 + if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
  42 + return SDImageFormatWebP;
  43 + }
40 } 44 }
41 -  
42 - NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];  
43 - if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {  
44 - return SDImageFormatWebP; 45 + break;
  46 + }
  47 + case 0x00: {
  48 + if (data.length >= 12) {
  49 + //....ftypheic
  50 + NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding];
  51 + if ([testString hasPrefix:@"ftyp"] && [testString hasSuffix:@"heic"]) {
  52 + return SDImageFormatHEIC;
  53 + }
45 } 54 }
  55 + break;
  56 + }
46 } 57 }
47 return SDImageFormatUndefined; 58 return SDImageFormatUndefined;
48 } 59 }