Permalink
Browse files

Added new draw manipulator and added new crop method

  • Loading branch information...
unixpickle committed Jul 1, 2012
1 parent 019d50c commit f529a612ef4287d0fda64fd17259a0a844db035d
View
Binary file not shown.
@@ -10,6 +10,7 @@
#import "BitmapScaleManipulator.h"
#import "BitmapCropManipulator.h"
#import "BitmapRotationManipulator.h"
+#import "BitmapDrawManipulator.h"
#import "UIImage+ANImageBitmapRep.h"
typedef struct {
@@ -26,7 +27,7 @@ UIColor * UIColorFromBMPixel (BMPixel pixel);
NSColor * NSColorFromBMPixel (BMPixel pixel);
#endif
-@interface ANImageBitmapRep : BitmapContextRep <BitmapScaleManipulator, BitmapCropManipulator, BitmapRotationManipulator, NSCopying> {
+@interface ANImageBitmapRep : BitmapContextRep <BitmapScaleManipulator, BitmapCropManipulator, BitmapRotationManipulator, BitmapDrawManipulator, NSCopying> {
#if __has_feature(objc_arc) == 1
__strong NSArray * baseClasses;
#else
@@ -147,11 +147,13 @@ - (void)generateBaseClasses {
BitmapCropManipulator * croppable = [[BitmapCropManipulator alloc] initWithContext:self];
BitmapScaleManipulator * scalable = [[BitmapScaleManipulator alloc] initWithContext:self];
BitmapRotationManipulator * rotatable = [[BitmapRotationManipulator alloc] initWithContext:self];
- baseClasses = [[NSArray alloc] initWithObjects:croppable, scalable, rotatable, nil];
+ BitmapDrawManipulator * drawable = [[BitmapDrawManipulator alloc] initWithContext:self];
+ baseClasses = [[NSArray alloc] initWithObjects:croppable, scalable, rotatable, drawable, nil];
#if __has_feature(objc_arc) != 1
[rotatable release];
[scalable release];
[croppable release];
+ [drawable release];
#endif
}
@@ -14,6 +14,7 @@
@optional
- (void)cropFrame:(CGRect)frame;
- (void)cropTopFrame:(CGRect)frame;
+- (void)cropTopEllipse:(CGRect)frame;
- (CGImageRef)croppedImageWithFrame:(CGRect)frame;
@end
@@ -44,6 +45,15 @@
*/
- (void)cropTopFrame:(CGRect)frame;
+/**
+ * Cuts an ellipse of the bitmap out for a new bitmap.
+ * @param frame The rectangle around the ellipse to be cut. The
+ * coordinates for this start at (0,0).
+ * @discussion The coordinates for this method begin in the top
+ * left corner. There is no alternative.
+ */
+- (void)cropTopEllipse:(CGRect)frame;
+
/**
* Creates a new CGImageRef by cutting out a portion of this one.
* This takes its behavoir from cropFrame.
@@ -37,6 +37,31 @@ - (void)cropTopFrame:(CGRect)frame {
CGContextRelease(newBitmap);
}
+- (void)cropTopEllipse:(CGRect)frame {
+ frame.origin.x = round(frame.origin.x);
+ frame.origin.y = round(frame.origin.y);
+ frame.size.width = round(frame.size.width);
+ frame.size.height = round(frame.size.height);
+
+ BMPoint size = [bitmapContext bitmapSize];
+ // It's kind of rude to prevent them from doing something kind of cool, so let's not.
+ // NSAssert(frame.origin.x >= 0 && frame.origin.x + frame.size.width <= size.x, @"Cropping frame must be within the bitmap.");
+ // NSAssert(frame.origin.y >= 0 && frame.origin.y + frame.size.height <= size.y, @"Cropping frame must be within the bitmap.");
+
+ CGContextRef newBitmap = [CGContextCreator newARGBBitmapContextWithSize:frame.size];
+ CGPoint offset = CGPointMake(-frame.origin.x, -(size.y - (frame.origin.y + frame.size.height)));
+
+ CGContextSaveGState(newBitmap);
+ CGContextBeginPath(newBitmap);
+ CGContextAddEllipseInRect(newBitmap, CGRectMake(0, 0, frame.size.width, frame.size.height));
+ CGContextClip(newBitmap);
+ CGContextDrawImage(newBitmap, CGRectMake(offset.x, offset.y, size.x, size.y), [bitmapContext CGImage]);
+ CGContextRestoreGState(newBitmap);
+
+ [bitmapContext setContext:newBitmap];
+ CGContextRelease(newBitmap);
+}
+
- (CGImageRef)croppedImageWithFrame:(CGRect)frame {
BMPoint size = [bitmapContext bitmapSize];
// It's kind of rude to prevent them from doing something kind of cool, so let's not.
@@ -0,0 +1,39 @@
+//
+// BitmapDrawManipulator.h
+// FaceBlur
+//
+// Created by Alex Nichol on 7/1/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "BitmapContextManipulator.h"
+
+@protocol BitmapDrawManipulator
+
+@optional
+- (void)drawImage:(CGImageRef)image inRect:(CGRect)rect;
+- (void)drawEllipseInFrame:(CGRect)frame color:(CGColorRef)color;
+
+@end
+
+@interface BitmapDrawManipulator : BitmapContextManipulator
+
+/**
+ * Overlays an image on the existing bitmap.
+ * @param image The image to be overlayed.
+ * @param rect The frame in which the image will be drawn.
+ * The coordinates for this begin at the top-left hand
+ * corner of the view.
+ */
+- (void)drawImage:(CGImageRef)image inRect:(CGRect)rect;
+
+/**
+ * Draws a colored ellipse in a given rectangle.
+ * @param frame The rectangle in which to draw the ellipse
+ * @param color The fill color for the ellipse. The coordinates
+ * for this begin at the top-left hand corner of the view.
+ */
+- (void)drawEllipseInFrame:(CGRect)frame color:(CGColorRef)color;
+
+@end
@@ -0,0 +1,39 @@
+//
+// BitmapDrawManipulator.m
+// FaceBlur
+//
+// Created by Alex Nichol on 7/1/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "BitmapDrawManipulator.h"
+
+@implementation BitmapDrawManipulator
+
+- (void)drawImage:(CGImageRef)image inRect:(CGRect)rect {
+ BMPoint size = [bitmapContext bitmapSize];
+ // It's kind of rude to prevent them from doing something kind of cool, so let's not.
+ // NSAssert(frame.origin.x >= 0 && frame.origin.x + frame.size.width <= size.x, @"Cropping frame must be within the bitmap.");
+ // NSAssert(frame.origin.y >= 0 && frame.origin.y + frame.size.height <= size.y, @"Cropping frame must be within the bitmap.");
+
+ CGPoint offset = CGPointMake(rect.origin.x, (size.y - (rect.origin.y + rect.size.height)));
+
+ CGContextRef context = [[self bitmapContext] context];
+ CGContextSaveGState(context);
+ CGContextDrawImage(context, CGRectMake(offset.x, offset.y, rect.size.width, rect.size.height), image);
+ CGContextRestoreGState(context);
+ [self.bitmapContext setNeedsUpdate:YES];
+}
+
+- (void)drawEllipseInFrame:(CGRect)frame color:(CGColorRef)color {
+ CGContextRef context = [[self bitmapContext] context];
+ CGContextSaveGState(context);
+ CGContextScaleCTM(context, 1, -1);
+ CGContextTranslateCTM(context, 0, -[bitmapContext bitmapSize].y);
+ CGContextSetFillColorWithColor(context, color);
+ CGContextFillEllipseInRect(context, frame);
+ CGContextRestoreGState(context);
+ [self.bitmapContext setNeedsUpdate:YES];
+}
+
+@end
Oops, something went wrong.

0 comments on commit f529a61

Please sign in to comment.