1 // Copyright 2013 Howling Moon Software. All rights reserved.
2 // See http://chipmunk2d.net/legal.php for more information.
3 
4 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
5 #import "ChipmunkAutoGeometry.h"
6 
7 #import <TargetConditionals.h>
8 
9 #if TARGET_OS_IPHONE == 1
10 	#import <CoreGraphics/CoreGraphics.h>
11 #endif
12 
13 
14 /**
15 	Generic sampler used with bitmap data.
16 	Currently limited to 8 bit per component data.
17 	Bitmap samplers currently provide no filtering, but could be easily extended to do so.
18 */
19 @interface ChipmunkBitmapSampler : ChipmunkAbstractSampler {
20 @private
21 	NSUInteger _width, _height, _stride;
22 	NSUInteger _bytesPerPixel, _component;
23 
24 	bool _flip;
25 	const uint8_t *_pixels;
26 	NSData *_pixelData;
27 
28 	cpFloat _borderValue;
29 
30 	cpBB _outputRect;
31 }
32 
33 /// Width of the bitmap in pixels.
34 @property(nonatomic, readonly) NSUInteger width;
35 
36 /// Height of the bitmap in pixels.
37 @property(nonatomic, readonly) NSUInteger height;
38 
39 /// Bytes per pixel of the bitmap. (ex: RGBA8888 would be 4)
40 @property(nonatomic, readonly) NSUInteger bytesPerPixel;
41 
42 /// Zero-based ndex of the component to sample. (ex: alpha of RGBA would be 3)
43 @property(nonatomic, assign) NSUInteger component;
44 
45 /// NSData object holding the pixel data.
46 @property(nonatomic, readonly) NSData *pixelData;
47 
48 /// Rect that the image maps to.
49 /// Defaults to (0.5, 0.5, width - 0.5, height - 0.5) so that pixel centers will be cleanly sampled.
50 @property(nonatomic, assign) cpBB outputRect;
51 
52 /**
53 	Init a sampler from bitmap data.
54 	Stride refers to the length of a row of pixels in bytes. (Generally just w*h*bytesPerPixel unless there is padding)
55 	Image must use one byte per component, but can have any number of components.
56 	@c component refers to the 0-based index of the component to sample. (i.e. 3 would sample the alpha in an RGBA bitmap)
57 	@c flip allows you to flip the image vertically to match how it migh be drawn.
58 	@c pixelData can be either a NSData or NSMutableData (i.e. for deformable terrain) that contains the bitmap data.
59 */
60 -(id)initWithWidth:(NSUInteger)width height:(NSUInteger)height stride:(NSUInteger)stride bytesPerPixel:(NSUInteger)bytesPerPixel component:(NSUInteger)component flip:(bool)flip pixelData:(NSData *)pixelData;
61 
62 /// Set the border of the bitmap to repeat the edge pixels.
63 -(void)setBorderRepeat;
64 
65 /// Set the border of the bitmap to be a specific value.
66 -(void)setBorderValue:(cpFloat)borderValue;
67 
68 /// March the entire image.
69 -(ChipmunkPolylineSet *)marchAllWithBorder:(bool)bordered hard:(bool)hard;
70 
71 @end
72 
73 
74 
75 /// Sampler built on top of a CGBitmapContext to allow deformable geometry.
76 /// Very efficient when paired with a ChipmunkTileCache.
77 @interface ChipmunkCGContextSampler : ChipmunkBitmapSampler {
78 @private
79 	CGContextRef _context;
80 }
81 
82 /// CGBitmapContext for this sampler.
83 @property(nonatomic, readonly) CGContextRef context;
84 
85 /// NSMutableData object holding the pixel data.
86 @property(nonatomic, readonly) NSMutableData *pixelData;
87 
88 /// Initialize a context based sampler. Must provide options for a valid context.
89 /// Find out more here in the Quartz 2D Programming Guide.
90 -(id)initWithWidth:(unsigned long)width height:(unsigned long)height colorSpace:(CGColorSpaceRef)colorSpace bitmapInfo:(CGBitmapInfo)bitmapInfo component:(NSUInteger)component;
91 
92 @end
93 
94 
95 
96 /// A CGBitmapContext sampler initialized with an CGImage.
97 @interface ChipmunkImageSampler : ChipmunkCGContextSampler
98 
99 /// Helper method to easily load CGImageRefs by path. You are responsible for releasing the CGImage.
100 +(CGImageRef)loadImage:(NSURL *)url;
101 
102 /// Initialize an image sampler of a certain size with a CGImage.
103 /// If isMask is TRUE, the image will be loaded as a black and white image, if FALSE only the image alpha will be loaded.
104 -(id)initWithImage:(CGImageRef)image isMask:(bool)isMask contextWidth:(NSUInteger)width contextHeight:(NSUInteger)height;
105 
106 /// Initialize an image sampler with an image file.
107 /// If isMask is TRUE, the image will be loaded as a black and white image, if FALSE only the image alpha will be loaded.
108 -(id)initWithImageFile:(NSURL *)url isMask:(bool)isMask;
109 
110 /// Return an autoreleased image sampler initialized with an image file.
111 /// If isMask is TRUE, the image will be loaded as a black and white image, if FALSE only the image alpha will be loaded.
112 +(ChipmunkImageSampler *)samplerWithImageFile:(NSURL *)url isMask:(bool)isMask;
113 
114 @end
115