-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIImage+NetPGM.m
73 lines (56 loc) · 2.08 KB
/
UIImage+NetPGM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// UIImage+NetPBM.m
// UIImage+NetPBM
//
// Created by Aleksey Garbarev on 20.04.14.
// Copyright (c) 2014 Aleksey Garbarev. All rights reserved.
//
#import "UIImage+NetPGM.h"
@implementation UIImage (NetPGM)
+ (UIImage *)imageWithPgmName:(NSString *)name
{
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"pgm"];
return [self imageFromPgmFilePath:path];
}
+ (UIImage *)imageFromPgmFilePath:(NSString *)path
{
return [self imageFromPgmData:[NSData dataWithContentsOfFile:path]];
}
+ (UIImage *)imageFromPgmData:(NSData *)data
{
const char *bytes = [data bytes];
char magicalNumber[2];
memcpy(magicalNumber, bytes, 2);
if (strcmp(magicalNumber, "P5") != 0) {
NSLog(@"file is not binary PGM! Aborting..");
return nil;
}
int imageWidth;
int imageHeight;
int location = GetImageSizeFromBytes(bytes, &imageWidth, &imageHeight);
const UInt8 *imageBytes = (UInt8 *) (bytes + location);
NSAssert([data length] - location == imageWidth * imageHeight, @"Remaining data must cover image size");
CFDataRef imageData = CFDataCreate(NULL, imageBytes, [data length] - location);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(imageData);
CGColorSpaceRef space = CGColorSpaceCreateDeviceGray();
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 8, imageWidth, space, 0, provider, NULL, NO, kCGRenderingIntentDefault);
return [UIImage imageWithCGImage:imageRef];
}
static int GetImageSizeFromBytes(const char *bytes, int *width, int *height)
{
int location = 0;
NSMutableString *imageSizeString = [NSMutableString stringWithCapacity:10];
for (int i = 3; bytes[i] != '\n'; i ++) {
[imageSizeString appendFormat:@"%c",bytes[i]];
location = i+2;
}
NSScanner *sizedScanner = [NSScanner scannerWithString:imageSizeString];
[sizedScanner scanInt:width];
[sizedScanner scanInt:height];
//Skip unused parameter
for (int i = location; bytes[i] != '\n'; i ++) {
location = i+2;
}
return location;
}
@end