-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSImage_PDCategories.m
299 lines (232 loc) · 9.43 KB
/
NSImage_PDCategories.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
//
// NSImage_PDCategories.m
// SproutedUtilities
//
// Created by Philip Dow on 9/9/06.
// Copyright Sprouted. All rights reserved.
// All inquiries should be directed to [email protected]
//
#import <SproutedUtilities/NSImage_PDCategories.h>
#import <SproutedUtilities/CTGradient.h>
#import <QuickLook/QuickLook.h>
@implementation NSImage (PDCategories)
+ (NSImage*) imageByReferencingImageNamed:(NSString*)imageName
{
return [[[NSImage alloc] initByReferencingFile:[[NSBundle mainBundle]
pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]]] autorelease];
}
+ (BOOL) canInitWithFile:(NSString*)path
{
NSString *app = nil;
NSString *fileType = nil;
NSArray *allImageFileTypes = [NSImage imageFileTypes];
[[NSWorkspace sharedWorkspace] getInfoForFile:path application:&app type:&fileType];
if ( !fileType || [fileType length] == 0 || [fileType isEqualToString:@"pdf"] )
return NO; //pdf documents are a special case
/*
BOOL canInit = [allImageFileTypes containsObject:fileType];
if ( canInit ) return YES;
else
{
// try a lazy initialization
if ( [[[NSImage alloc] initByReferencingFile:path] autorelease] != nil )
return YES;
else
return NO;
}
*/
return ( [allImageFileTypes containsObject:fileType] );
}
+ (NSImage*) iconWithContentsOfFile:(NSString*)path edgeSize:(float)size inset:(float)padding
{
//#warning not completely implemented - inset
NSImage *original = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
if ( original == nil )
return nil;
NSRect targetFrame;
NSImage *product = [[[NSImage alloc] initWithSize:NSMakeSize(size,size)] autorelease];
NSSize mySize = [original size];
if ( mySize.width > mySize.height )
{
int targetWidth = ( mySize.width < size ? mySize.width : size );
int targetHeight = targetWidth*mySize.height/mySize.width;
targetFrame = NSMakeRect(size/2 - targetWidth/2, size/2 - targetHeight/2, targetWidth, targetHeight);
}
else
{
int targetHeight = ( mySize.height < size ? mySize.height : size );
int targetWidth = targetHeight*mySize.width/mySize.height;
targetFrame = NSMakeRect(size/2 - targetWidth/2, size/2 - targetHeight/2, targetWidth, targetHeight);
}
[product lockFocus];
[original drawInRect:targetFrame fromRect:NSMakeRect(0,0,mySize.width,mySize.height)
operation:NSCompositeSourceOver fraction:1.0];
[product unlockFocus];
return product;
}
+ (NSImage *)imageWithPreviewOfFileAtPath:(NSString *)path ofSize:(NSSize)size asIcon:(BOOL)icon
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
if (!path || !fileURL) {
return nil;
}
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:icon]
forKey:(NSString *)kQLThumbnailOptionIconModeKey];
CGImageRef ref = QLThumbnailImageCreate(kCFAllocatorDefault,
(CFURLRef)fileURL,
CGSizeMake(size.width, size.height),
(CFDictionaryRef)dict);
if (ref != NULL) {
// Take advantage of NSBitmapImageRep's -initWithCGImage: initializer, new in Leopard,
// which is a lot more efficient than copying pixel data into a brand new NSImage.
// Thanks to Troy Stephens @ Apple for pointing this new method out to me.
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
NSImage *newImage = nil;
if (bitmapImageRep) {
newImage = [[NSImage alloc] initWithSize:[bitmapImageRep size]];
[newImage addRepresentation:bitmapImageRep];
[bitmapImageRep release];
if (newImage) {
return [newImage autorelease];
}
}
CFRelease(ref);
} else {
// If we couldn't get a Quick Look preview, fall back on the file's Finder icon.
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
if (icon) {
[icon setSize:size];
}
return icon;
}
return nil;
}
#pragma mark -
- (NSImage *)reflectedImage:(float)fraction
{
// based on NSImage+BHReflectedImage.m by Jeff Ganyard
NSImage *reflection = [[NSImage alloc] initWithSize:[self size]];
[reflection setFlipped:YES];
[reflection lockFocus];
CTGradient *fade = [CTGradient gradientWithBeginningColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] endingColor:[NSColor clearColor]];
[fade fillRect:NSMakeRect(0, 0, [self size].width, [self size].height*fraction) angle:90.0];
[self drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0];
[reflection unlockFocus];
return [reflection autorelease];
}
- (NSImage*) imageWithWidth:(float)width height:(float)height {
// returns a WxH version of the image
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width,height)];
[image lockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[self drawInRect:NSMakeRect(0,0,width,height) fromRect:NSMakeRect(0,0,[self size].width,[self size].height)
operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[image unlockFocus];
return [image autorelease];
}
- (NSImage*) imageWithWidth:(float)width height:(float)height inset:(float)inset {
// returns a WxH version of the image but with the image inset on all sides
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width,height)];
NSRect dRect = NSMakeRect(inset,inset,width-inset*2,height-inset*2);
[image lockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[self drawInRect:dRect fromRect:NSMakeRect(0,0,[self size].width,[self size].height)
operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[image unlockFocus];
return [image autorelease];
}
#pragma mark -
- (NSData*) pngData
{
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]];
return [rep representationUsingType:NSPNGFileType properties:nil];
}
- (NSAttributedString*) attributedString:(int)qual maxWidth:(int)mWidth
{
NSFileWrapper *pngFileWrapper;
NSBitmapImageRep *bitmapRep;
NSTextAttachment *attachment;
NSAttributedString *picAsAttrStr;
int quality = ( qual ? qual : 5 );
if ( mWidth != 0 && [self size].width > mWidth ) {
int newWidth = mWidth;
int newHeight = mWidth * [self size].height / [self size].width;
//draw the image into our resized image
NSImage *resizedImage = [[NSImage alloc] initWithSize:NSMakeSize(newWidth,newHeight)];
[resizedImage lockFocus];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[self drawInRect:NSMakeRect(0.0,0.0,newWidth, newHeight)
fromRect:NSMakeRect(0.0,0.0,[self size].width, [self size].height)
operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[resizedImage unlockFocus];
//
//and grab our data
bitmapRep = [[NSBitmapImageRep alloc] initWithData:[resizedImage TIFFRepresentation]];
//
//clean up
[resizedImage release];
}
else {
//just need the bitmap rep
bitmapRep = [[NSBitmapImageRep alloc] initWithData:[self TIFFRepresentation]];
}
//
// encountered an error, return nil without worrry about releasing anything
if ( !bitmapRep )
return nil;
//
// depending on the quality, create a file wrapper with the appropriate representation
if ( quality == 10 ) {
pngFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[bitmapRep representationUsingType:NSPNGFileType properties:nil]];
if ( !pngFileWrapper )
return nil;
[pngFileWrapper setPreferredFilename:@"journaled pic.png"];
}
else {
NSDictionary *imageProps = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat:0.85], NSImageCompressionFactor, NULL];
pngFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:
[bitmapRep representationUsingType:NSJPEGFileType properties:imageProps]];
if ( !pngFileWrapper )
return nil;
[pngFileWrapper setPreferredFilename:@"journaled pic.jpg"];
}
//
// create the attachment
attachment = [[NSTextAttachment alloc] initWithFileWrapper:pngFileWrapper];
if ( !attachment )
return nil;
//
// create the attributed string
picAsAttrStr = [NSAttributedString attributedStringWithAttachment:attachment];
//
// clean up
[attachment release];
// return whatever value the attributed string is, nil okay
return picAsAttrStr;
}
#pragma mark -
+ (NSImage *) imageFromCIImage:(CIImage *)ciImage
{
NSImage *image = [[[NSImage alloc] initWithSize:NSMakeSize([ciImage extent].size.width, [ciImage extent].size.height)] autorelease];
[image addRepresentation:[NSCIImageRep imageRepWithCIImage:ciImage]];
return image;
}
+ (CIImage *) CIImageFromImage:(NSImage*)anImage
{
/*
NSBitmapImageRep *bitmapRep = [anImage bestRepresentationForDevice:nil];
CIImage *ciImage = [[[CIImage alloc] initWithBitmapImageRep:bitmapRep] autorelease];
return ciImage;
*/
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
CIImage *ciImage = [[[CIImage alloc] initWithBitmapImageRep:imageRep] autorelease];
return ciImage;
}
@end