forked from btrask/EasyCapViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECVPixelBuffer.m
executable file
·365 lines (302 loc) · 10.3 KB
/
ECVPixelBuffer.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Copyright (c) 2011, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "ECVPixelBuffer.h"
// Other Sources
#import "ECVPixelFormat.h"
typedef struct {
NSInteger location;
NSUInteger length;
} ECVRange;
NS_INLINE ECVRange ECVRangeFromNSRange(NSRange const r)
{
return (ECVRange){r.location, r.length};
}
NS_INLINE NSInteger ECVMaxRange(ECVRange const r)
{
return r.location + r.length;
}
NS_INLINE ECVRange ECVIntersectionRange(ECVRange const r1, ECVRange const r2)
{
ECVRange r;
r.location = MAX(r1.location, r2.location);
r.length = MAX(0, SUB_ZERO(MIN(ECVMaxRange(r1), ECVMaxRange(r2)), r.location));
return r;
}
NS_INLINE ECVRange ECVRebaseRange(ECVRange const range, ECVRange const base)
{
ECVRange r = ECVIntersectionRange(range, base);
r.location -= base.location;
return r;
}
NS_INLINE NSRange ECVNSRebaseRange(NSRange const range, NSRange const base)
{
NSRange r = NSIntersectionRange(range, base);
r.location -= base.location;
return r;
}
typedef struct {
size_t bytesPerRow;
size_t bytesPerPixel;
ECVRange validRange;
} const ECVFastPixelBufferInfo;
NS_INLINE ECVRange ECVValidRows(ECVFastPixelBufferInfo *info)
{
return (ECVRange){info->validRange.location / info->bytesPerRow, info->validRange.length / info->bytesPerRow + 2};
}
NS_INLINE void ECVDraw(UInt8 *dst, UInt8 const *src, size_t length, BOOL blended)
{
if(blended) {
size_t i;
for(i = 0; i < length; ++i) dst[i] = dst[i] / 2 + src[i] / 2;
} else memcpy(dst, src, length);
}
NS_INLINE void ECVDrawRow(UInt8 *dst, ECVFastPixelBufferInfo *dstInfo, UInt8 const *src, ECVFastPixelBufferInfo *srcInfo, ECVIntegerPoint dstPoint, ECVIntegerPoint srcPoint, size_t length, BOOL blended)
{
ECVRange const dstDesiredRange = (ECVRange){dstPoint.y * dstInfo->bytesPerRow + dstPoint.x * dstInfo->bytesPerPixel, length * dstInfo->bytesPerPixel};
ECVRange const srcDesiredRange = (ECVRange){srcPoint.y * srcInfo->bytesPerRow + srcPoint.x * srcInfo->bytesPerPixel, length * srcInfo->bytesPerPixel};
ECVRange const dstRowRange = (ECVRange){dstPoint.y * dstInfo->bytesPerRow, dstInfo->bytesPerRow};
ECVRange const srcRowRange = (ECVRange){srcPoint.y * srcInfo->bytesPerRow, srcInfo->bytesPerRow};
ECVRange const dstValidRange = ECVIntersectionRange(ECVIntersectionRange(dstDesiredRange, dstRowRange), dstInfo->validRange);
ECVRange const srcValidRange = ECVIntersectionRange(ECVIntersectionRange(srcDesiredRange, srcRowRange), srcInfo->validRange);
NSInteger const dstMinOffset = dstValidRange.location - dstDesiredRange.location;
NSInteger const srcMinOffset = srcValidRange.location - srcDesiredRange.location;
NSInteger const commonOffset = MAX(dstMinOffset, srcMinOffset);
NSUInteger const dstMaxLength = SUB_ZERO(dstValidRange.length, (NSUInteger)(commonOffset - dstMinOffset));
NSUInteger const srcMaxLength = SUB_ZERO(srcValidRange.length, (NSUInteger)(commonOffset - srcMinOffset));
NSUInteger const commonLength = MIN(dstMaxLength, srcMaxLength);
if(!commonLength) return;
ECVRange const dstRange = ECVRebaseRange((ECVRange){dstDesiredRange.location + commonOffset, commonLength}, dstInfo->validRange);
ECVRange const srcRange = ECVRebaseRange((ECVRange){srcDesiredRange.location + commonOffset, commonLength}, srcInfo->validRange);
UInt8 *const dstBytes = dst + dstRange.location;
UInt8 const *const srcBytes = src + srcRange.location;
ECVDraw(dstBytes, srcBytes, commonLength, blended);
}
static void ECVDrawRect(ECVMutablePixelBuffer *dst, ECVPixelBuffer *src, ECVIntegerPoint dstPoint, ECVIntegerPoint srcPoint, ECVIntegerSize size, ECVPixelBufferDrawingOptions options)
{
ECVFastPixelBufferInfo dstInfo = {
.bytesPerRow = [dst bytesPerRow],
.bytesPerPixel = ECVPixelFormatBytesPerPixel([dst pixelFormat]),
.validRange = ECVRangeFromNSRange([dst validRange]),
};
ECVFastPixelBufferInfo srcInfo = {
.bytesPerRow = [src bytesPerRow],
.bytesPerPixel = ECVPixelFormatBytesPerPixel([src pixelFormat]),
.validRange = ECVRangeFromNSRange([src validRange]),
};
UInt8 *const dstBytes = [dst mutableBytes];
UInt8 const *const srcBytes = [src bytes];
BOOL const useFields = ECVDrawToHighField & options || ECVDrawToLowField & options;
NSUInteger const dstRowSpacing = useFields ? 2 : 1;
BOOL const blended = !!(ECVDrawBlended & options);
ECVRange const srcRows = ECVIntersectionRange((ECVRange){srcPoint.y, size.height}, ECVValidRows(&srcInfo));
for(NSInteger i = srcRows.location; i < ECVMaxRange(srcRows); ++i) {
if(ECVDrawToHighField & options || !useFields) {
ECVDrawRow(dstBytes, &dstInfo, srcBytes, &srcInfo, (ECVIntegerPoint){dstPoint.x, dstPoint.y + 0 + (i * dstRowSpacing)}, (ECVIntegerPoint){srcPoint.x, srcPoint.y + i}, size.width, blended);
}
if(ECVDrawToLowField & options) {
ECVDrawRow(dstBytes, &dstInfo, srcBytes, &srcInfo, (ECVIntegerPoint){dstPoint.x, dstPoint.y + 1 + (i * dstRowSpacing)}, (ECVIntegerPoint){srcPoint.x, srcPoint.y + i}, size.width, blended);
}
}
}
@implementation ECVPixelBuffer
#pragma mark -ECVPixelBuffer
- (NSRange)fullRange
{
return NSMakeRange(0, [self bytesPerRow] * [self pixelSize].height);
}
@end
@implementation ECVPointerPixelBuffer
#pragma mark -ECVPointerPixelBuffer
- (id)initWithPixelSize:(ECVIntegerSize)pixelSize bytesPerRow:(size_t)bytesPerRow pixelFormat:(OSType)pixelFormat bytes:(void const *)bytes validRange:(NSRange)validRange
{
if((self = [super init])) {
_pixelSize = pixelSize;
_bytesPerRow = bytesPerRow;
_pixelFormat = pixelFormat;
_bytes = bytes;
_validRange = validRange;
}
return self;
}
#pragma mark -ECVPixelBuffer(ECVAbstract)
- (ECVIntegerSize)pixelSize
{
return _pixelSize;
}
- (size_t)bytesPerRow
{
return _bytesPerRow;
}
- (OSType)pixelFormat
{
return _pixelFormat;
}
#pragma mark -
- (void const *)bytes
{
return _bytes;
}
- (NSRange)validRange
{
return _validRange;
}
#pragma mark -ECVPixelBuffer(ECVAbstract) <NSLocking>
- (void)lock {}
- (void)unlock {}
@end
@implementation ECVMutablePixelBuffer
#pragma mark -ECVMutablePixelBuffer
- (void)drawPixelBuffer:(ECVPixelBuffer *)src
{
[self drawPixelBuffer:src options:kNilOptions];
}
- (void)drawPixelBuffer:(ECVPixelBuffer *)src options:(ECVPixelBufferDrawingOptions)options
{
[self drawPixelBuffer:src options:options atPoint:(ECVIntegerPoint){0, 0}];
}
- (void)drawPixelBuffer:(ECVPixelBuffer *)src options:(ECVPixelBufferDrawingOptions)options atPoint:(ECVIntegerPoint)point
{
ECVIntegerPoint const dstPoint = point;
ECVIntegerPoint const srcPoint = (ECVIntegerPoint){0, 0};
ECVDrawRect(self, src, dstPoint, srcPoint, [src pixelSize], options);
}
#pragma mark -
- (void)clearRange:(NSRange)range
{
NSRange const r = ECVNSRebaseRange(range, [self validRange]);
if(!r.length) return;
uint64_t const val = ECVPixelFormatBlackPattern([self pixelFormat]);
memset_pattern8([self mutableBytes] + r.location, &val, r.length);
}
- (void)clear
{
NSUInteger const length = [self validRange].length;
if(!length) return;
uint64_t const val = ECVPixelFormatBlackPattern([self pixelFormat]);
memset_pattern8([self mutableBytes], &val, length);
}
@end
@implementation ECVCVPixelBuffer : ECVMutablePixelBuffer
#pragma mark -ECVCVPixelBuffer
- (id)initWithPixelBuffer:(CVPixelBufferRef const)pixelBuffer
{
NSParameterAssert(pixelBuffer);
if((self = [super init])) {
_pixelBuffer = CVPixelBufferRetain(pixelBuffer);
}
return self;
}
#pragma mark -ECVMutablePixelBuffer(ECVAbstract)
- (void *)mutableBytes
{
return CVPixelBufferGetBaseAddress(_pixelBuffer);
}
#pragma mark -ECVPixelBuffer(ECVAbstract)
- (ECVIntegerSize)pixelSize
{
return (ECVIntegerSize){CVPixelBufferGetWidth(_pixelBuffer), CVPixelBufferGetHeight(_pixelBuffer)};
}
- (size_t)bytesPerRow
{
return CVPixelBufferGetBytesPerRow(_pixelBuffer);
}
- (OSType)pixelFormat
{
return CVPixelBufferGetPixelFormatType(_pixelBuffer);
}
#pragma mark -
- (void const *)bytes
{
return CVPixelBufferGetBaseAddress(_pixelBuffer);
}
- (NSRange)validRange
{
return [self fullRange];
}
#pragma mark -ECVPixelBuffer(ECVAbstract) <NSLocking>
- (void)lock
{
CVPixelBufferLockBaseAddress(_pixelBuffer, kNilOptions);
}
- (void)unlock
{
CVPixelBufferUnlockBaseAddress(_pixelBuffer, kNilOptions);
}
#pragma mark -NSObject
- (void)dealloc
{
CVPixelBufferRelease(_pixelBuffer);
[super dealloc];
}
@end
@implementation ECVDataPixelBuffer
#pragma mark -ECVDataPixelBuffer
- (id)initWithPixelSize:(ECVIntegerSize)pixelSize bytesPerRow:(size_t)bytesPerRow pixelFormat:(OSType)pixelFormat data:(NSMutableData *)data offset:(NSUInteger)offset
{
if((self = [super init])) {
_pixelSize = pixelSize;
_bytesPerRow = bytesPerRow;
_pixelFormat = pixelFormat;
_data = [data retain];
_offset = offset;
}
return self;
}
- (NSMutableData *)mutableData
{
return [[_data retain] autorelease];
}
#pragma mark -ECVMutablePixelBuffer(ECVAbstract)
- (void *)mutableBytes
{
return [_data mutableBytes];
}
#pragma mark -ECVPixelBuffer(ECVAbstract)
- (ECVIntegerSize)pixelSize
{
return _pixelSize;
}
- (size_t)bytesPerRow
{
return _bytesPerRow;
}
- (OSType)pixelFormat
{
return _pixelFormat;
}
#pragma mark -
- (void const *)bytes
{
return [_data bytes];
}
- (NSRange)validRange
{
return NSMakeRange(_offset, [_data length]);
}
#pragma mark -ECVPixelBuffer(ECVAbstract) <NSLocking>
- (void)lock {}
- (void)unlock {}
#pragma mark -NSObject
- (void)dealloc
{
[_data release];
[super dealloc];
}
@end