This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JGPixelData.m
139 lines (106 loc) · 3.82 KB
/
JGPixelData.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// JGPixelData.m
// JGPixelDataExample
//
// Created by Jaden Geller on 2/3/14.
// Copyright (c) 2014 Jaden Geller. All rights reserved.
//
#import "JGPixelData.h"
@interface JGPixelData ()
@property (nonatomic, readonly) NSData *data;
@property (nonatomic, readonly) CGImageRef imageRef;
@end
@implementation JGPixelData
@synthesize stop = _stop, width = _width, height = _height;
- (id)copyWithZone:(NSZone *)zone
{
JGPixelData *copy = [[[self class] alloc] init];
if (copy) {
copy->_data = _data.copy;
copy->_imageRef = _imageRef;
}
return copy;
}
-(id)initWithImage:(UIImage*)image{
self = [super init];
if (self) {
[self setRawDataWithImage:image];
}
return self;
}
-(id)initWithSize:(CGSize)size{
UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
//CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height));
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [self initWithImage:blank];
}
-(void)setRawDataWithImage:(UIImage*)image{
_imageRef = image.CGImage;
_data = (NSData *)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(self.imageRef)));
}
-(JGPixel*)pixelWithX:(NSUInteger)xIndex y:(NSUInteger)yIndex{
return (JGPixel*)self.data.bytes + xIndex + self.width * yIndex;
}
-(UIImage*)image{
// Create new image from data
size_t bitsPerComponent = CGImageGetBitsPerComponent(self.imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(self.imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(self.imageRef);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(self.imageRef);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, self.data.bytes, [self.data length], NULL);
CGImageRef newImageRef = CGImageCreate (
self.width,
self.height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorspace,
bitmapInfo,
provider,
NULL,
false,
kCGRenderingIntentDefault
);
// the modified image
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
// cleanup
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
CGImageRelease(newImageRef);
return newImage;
}
-(NSUInteger)width{
if (!_width) _width = CGImageGetWidth(self.imageRef);
return _width;
}
-(NSUInteger)height{
if (!_height) _height = CGImageGetHeight(self.imageRef);
return _height;
}
+(JGPixelData*)pixelDataWithImage:(UIImage*)image{
return [[JGPixelData alloc]initWithImage:image];
}
+(JGPixelData*)pixelDataWithSize:(CGSize)size{
return [[JGPixelData alloc]initWithSize:size];
}
-(void)processPixelsWithBlock:(void (^)(JGPixel *color, int x, int y))updatePixelColor{
JGPixel *pixel = self.start;
for (int x = 0; x < self.width; x++) {
for (int y = 0; y < self.height; y++) {
updatePixelColor(pixel++, x, y);
}
}
}
-(JGPixel*)start{
return (JGPixel*)self.data.bytes;
}
-(JGPixel*)stop{
if (!_stop) _stop = ((JGPixel*)self.data.bytes + self.width * self.height);
return _stop;
}
-(BOOL)inRange:(JGPixel*)pixel{
return (pixel >= self.start && pixel < self.stop);
}
@end