-
Notifications
You must be signed in to change notification settings - Fork 1
/
KinemeQuickLookPatch.m
176 lines (153 loc) · 5.24 KB
/
KinemeQuickLookPatch.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
#import <QuickLook/QuickLook.h>
#import "KinemeQuickLookPatch.h"
typedef struct
{
NSString *filename;
CGSize size;
BOOL iconMode;
BOOL useQL;
QCImagePort *outputImage;
QCBooleanPort *outputDone;
} ThreadSettings;
@implementation KinemeQuickLookPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeProcessor;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeNone;
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
[inputWidth setDoubleValue: 128.];
[inputWidth setMinDoubleValue: 1.];
[inputHeight setDoubleValue: 128.];
[inputHeight setMinDoubleValue: 1.];
[inputIconMode setBooleanValue: YES];
[[self userInfo] setObject:@"Kineme QuickLook" forKey:@"name"];
}
return self;
}
-(void)cleanup:(QCOpenGLContext*)context
{
[renderThread cancel];
[renderThread release];
renderThread = nil;
}
-(void)disable:(QCOpenGLContext*)context
{
[renderThread cancel];
[renderThread release];
renderThread = nil;
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
NSString *path = KIExpandPath(self,[inputPath stringValue]);
[outputDone setBooleanValue: NO];
if([path length] == 0)
{
[outputImage setImageValue: nil];
return YES;
}
ThreadSettings *settings = (ThreadSettings*)malloc(sizeof(ThreadSettings));
settings->filename = [path retain];
settings->outputImage = [outputImage retain];
settings->outputDone = [outputDone retain];
settings->size.width = [inputWidth doubleValue];
settings->size.height = [inputHeight doubleValue];
settings->iconMode = [inputIconMode booleanValue];
settings->useQL = ![inputSkipQuickLook booleanValue];
NSValue *settingsValue = [NSValue valueWithPointer: settings];
// _generateImageFromSettings frees settings (and releases path)
if([inputAsynchronous booleanValue])
{
if([renderThread isExecuting])
{
[renderThread cancel];
[renderThread release];
}
renderThread = [[NSThread allocWithZone:NULL] initWithTarget: self selector: @selector(_generateImageFromSettings:) object: settingsValue];
[renderThread start];
//[NSThread detachNewThreadSelector: @selector(_generateImageFromSettings:) toTarget: self withObject:settings];
}
else
[self _generateImageFromSettings: settingsValue];
return YES;
}
-(void)_generateImageFromSettings:(NSValue*)settingsValue
{
NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:NULL] init];
ThreadSettings *settings = [settingsValue pointerValue];
QCImage *img;
CGImageRef thumbnail = NULL;
if(settings->useQL)
{
NSMutableDictionary *options = [[NSMutableDictionary allocWithZone:NULL] init];
if(settings->iconMode)
[options setObject: (id)kCFBooleanTrue forKey: (NSString*)kQLThumbnailOptionIconModeKey];
// scale is dumb -- just set the size directly....
// [options setObject:[NSNumber numberWithDouble:[inputScale doubleValue]] forKey: (NSString*)kQLThumbnailOptionScaleFactorKey];
thumbnail = QLThumbnailImageCreate(NULL,
(CFURLRef)[NSURL fileURLWithPath: settings->filename],
settings->size,
(CFDictionaryRef)options);
[options release];
}
if(thumbnail)
{
img = [[QCImage allocWithZone:NULL] initWithCGImage: thumbnail options: nil];
CGImageRelease(thumbnail);
}
else // QL failed, or user requested a QL bypass (finder-only icons)
{
NSImage *icon = [[[NSWorkspace sharedWorkspace] iconForFile: settings->filename] retain];
/* NSWorkspace gives us an NSImage with lots of reps inside in no particular order. So we have
to iterate over all of them, and select the largest. With the largest, we then resample it to be the user's
desired size. A bit roundabout, but it works :)
*/
/*NSArray *reps = [icon representations];
float max = -INFINITY;//[[reps objectAtIndex: 0] size].width;
NSImageRep *biggest = nil;
for(NSImageRep *rep in reps)
{
if(biggest == nil || [rep size].width > max)
{
biggest = rep;
max = [rep size].width;
}
}*/
// QC resizing doesn't work in 64bit mode (for whatever reason), so we resize manually using NSImage.. blah.
NSImage *tmp = [[NSImage allocWithZone:NULL] initWithSize: NSSizeFromCGSize(settings->size)];
[tmp lockFocus];
NSSize iconSize = [icon size];
NSRect bounds = NSMakeRect(0, 0, iconSize.width, iconSize.height);
[icon drawInRect:NSMakeRect(0, 0, settings->size.width, settings->size.height) fromRect:bounds operation: NSCompositeSourceOver fraction: 1.0];
[tmp unlockFocus];
img = [[QCImage allocWithZone:NULL] initWithNSImage:tmp options:nil];
[tmp release];
[icon release];
// This Crashes in 64bit mode (Leopard and Snow Leopard)
//QCImage *tmp = [[QCImage allocWithZone:NULL] initWithNSBitmapImageRep: biggest options: nil];
//img = [tmp createScaledImageByX: settings->size.width/[biggest size].width Y: settings->size.height/[biggest size].height];
//[tmp release];
}
if([[NSThread currentThread] isCancelled] == FALSE)
{
[settings->outputImage setImageValue: img];
[settings->outputDone setBooleanValue: YES];
}
[img release];
[settings->filename release];
[settings->outputImage release];
[settings->outputDone release];
free(settings);
[pool drain];
}
@end