1 // Copyright 2013 Howling Moon Software. All rights reserved.
2 // See http://chipmunk2d.net/legal.php for more information.
4 #import "ObjectiveChipmunk/ObjectiveChipmunk.h"
5 #import "ChipmunkAutoGeometry.h"
7 #import <TargetConditionals.h>
9 #if TARGET_OS_IPHONE == 1
10 #import <CoreGraphics/CoreGraphics.h>
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.
19 @interface ChipmunkBitmapSampler : ChipmunkAbstractSampler {
21 NSUInteger _width, _height, _stride;
22 NSUInteger _bytesPerPixel, _component;
25 const uint8_t *_pixels;
33 /// Width of the bitmap in pixels.
34 @property(nonatomic, readonly) NSUInteger width;
36 /// Height of the bitmap in pixels.
37 @property(nonatomic, readonly) NSUInteger height;
39 /// Bytes per pixel of the bitmap. (ex: RGBA8888 would be 4)
40 @property(nonatomic, readonly) NSUInteger bytesPerPixel;
42 /// Zero-based ndex of the component to sample. (ex: alpha of RGBA would be 3)
43 @property(nonatomic, assign) NSUInteger component;
45 /// NSData object holding the pixel data.
46 @property(nonatomic, readonly) NSData *pixelData;
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;
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.
60 -(id)initWithWidth:(NSUInteger)width height:(NSUInteger)height stride:(NSUInteger)stride bytesPerPixel:(NSUInteger)bytesPerPixel component:(NSUInteger)component flip:(bool)flip pixelData:(NSData *)pixelData;
62 /// Set the border of the bitmap to repeat the edge pixels.
63 -(void)setBorderRepeat;
65 /// Set the border of the bitmap to be a specific value.
66 -(void)setBorderValue:(cpFloat)borderValue;
68 /// March the entire image.
69 -(ChipmunkPolylineSet *)marchAllWithBorder:(bool)bordered hard:(bool)hard;
75 /// Sampler built on top of a CGBitmapContext to allow deformable geometry.
76 /// Very efficient when paired with a ChipmunkTileCache.
77 @interface ChipmunkCGContextSampler : ChipmunkBitmapSampler {
79 CGContextRef _context;
82 /// CGBitmapContext for this sampler.
83 @property(nonatomic, readonly) CGContextRef context;
85 /// NSMutableData object holding the pixel data.
86 @property(nonatomic, readonly) NSMutableData *pixelData;
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;
96 /// A CGBitmapContext sampler initialized with an CGImage.
97 @interface ChipmunkImageSampler : ChipmunkCGContextSampler
99 /// Helper method to easily load CGImageRefs by path. You are responsible for releasing the CGImage.
100 +(CGImageRef)loadImage:(NSURL *)url;
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;
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;
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;