Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 90 lines (72 sloc) 2.778 kb
3a8900a @MrNoodle - Initial checkin.
authored
1 //
2 // NSImage-NoodleExtensions.m
3 // NoodleKit
4 //
5 // Created by Paul Kim on 3/24/07.
6 // Copyright 2007-2009 Noodlesoft, LLC. All rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person
9 // obtaining a copy of this software and associated documentation
10 // files (the "Software"), to deal in the Software without
11 // restriction, including without limitation the rights to use,
12 // copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following
15 // conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 // OTHER DEALINGS IN THE SOFTWARE.
28
29 #import "NSImage-NoodleExtensions.h"
30
31
32 @implementation NSImage (NoodleExtensions)
33
34 - (void)drawAdjustedAtPoint:(NSPoint)aPoint fromRect:(NSRect)srcRect operation:(NSCompositingOperation)op fraction:(CGFloat)delta
35 {
36 NSSize size = [self size];
37
38 [self drawAdjustedInRect:NSMakeRect(aPoint.x, aPoint.y, size.width, size.height) fromRect:srcRect operation:op fraction:delta];
39 }
40
41 - (void)drawAdjustedInRect:(NSRect)dstRect fromRect:(NSRect)srcRect operation:(NSCompositingOperation)op fraction:(CGFloat)delta
42 {
43 NSGraphicsContext *context;
44 BOOL contextIsFlipped;
45
46 context = [NSGraphicsContext currentContext];
47 contextIsFlipped = [context isFlipped];
48
49 if (contextIsFlipped)
50 {
51 NSAffineTransform *transform;
52
53 [context saveGraphicsState];
54
55 // Flip the coordinate system back.
56 transform = [NSAffineTransform transform];
57 [transform translateXBy:0 yBy:NSMaxY(dstRect)];
58 [transform scaleXBy:1 yBy:-1];
59 [transform concat];
60
61 // The transform above places the y-origin right where the image should be drawn.
62 dstRect.origin.y = 0.0;
63 }
64
65 [self drawInRect:dstRect fromRect:srcRect operation:op fraction:delta];
66
67 if (contextIsFlipped)
68 {
69 [context restoreGraphicsState];
70 }
71 }
72
73 - (NSImage *)unflippedImage
74 {
75 if ([self isFlipped])
76 {
77 NSImage *newImage;
78
79 newImage = [[[NSImage alloc] initWithSize:[self size]] autorelease];
80 [newImage lockFocus];
81 [self drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
82 [newImage unlockFocus];
83
84 return newImage;
85 }
86 return self;
87 }
88
89 @end
Something went wrong with that request. Please try again.