/
IMBParser.m
531 lines (372 loc) · 14.8 KB
/
IMBParser.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2010 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2010 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Peter Baumgartner
//----------------------------------------------------------------------------------------------------------------------
#pragma mark HEADERS
#import "IMBParser.h"
#import "IMBNode.h"
#import "IMBObject.h"
#import "IMBMovieObject.h"
#import "IMBObjectPromise.h"
#import "IMBLibraryController.h"
#import "NSString+iMedia.h"
#import "NSData+SKExtensions.h"
#import <Quartz/Quartz.h>
#import <QTKit/QTKit.h>
#import <QuickLook/QuickLook.h>
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@interface IMBParser ()
- (CGImageSourceRef) _imageSourceForURL:(NSURL*)inURL;
- (CGImageRef) _imageForURL:(NSURL*)inURL;
- (CGImageRef) _quicklookCGImageForURL:(NSURL*)inURL;
- (NSImage*) _quicklookNSImageForURL:(NSURL*)inURL;
- (void) _loadMovieRepresentation:(NSDictionary*)inInfo;
- (QTMovie*) _movieForURL:(NSURL*)inURL;
@end
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@implementation IMBParser
@synthesize mediaSource = _mediaSource;
@synthesize mediaType = _mediaType;
@synthesize custom = _custom;
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
// The default implementation just returns a single parser instance. Subclasses like iPhoto, Aperture, or Lightroom
// may opt to return multiple instances (preconfigured with correct mediaSource) if multiple libraries are detected...
+ (NSArray*) parserInstancesForMediaType:(NSString*)inMediaType
{
IMBParser* parser = [[[self class] alloc] initWithMediaType:inMediaType];
NSArray* parserInstances = [NSArray arrayWithObject:parser];
[parser release];
return parserInstances;
}
//----------------------------------------------------------------------------------------------------------------------
- (id) initWithMediaType:(NSString*)inMediaType
{
if (self = [super init])
{
self.mediaSource = nil;
self.mediaType = inMediaType;
self.custom = NO;
}
return self;
}
- (void) dealloc
{
IMBRelease(_mediaSource);
IMBRelease(_mediaType);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
// The following two methods must be overridden by subclasses...
- (IMBNode*) nodeWithOldNode:(const IMBNode*)inOldNode options:(IMBOptions)inOptions error:(NSError**)outError
{
return nil;
}
- (BOOL) populateNode:(IMBNode*)inNode options:(IMBOptions)inOptions error:(NSError**)outError
{
return NO;
}
//----------------------------------------------------------------------------------------------------------------------
// Optional methods that do nothing in the base class and can be overridden in subclasses, e.g. to update
// or get rid of cached data...
- (void) willUseParser
{
}
- (void) didStopUsingParser
{
}
- (void) watchedPathDidChange:(NSString*)inWatchedPath
{
}
//----------------------------------------------------------------------------------------------------------------------
// This helper method can be used by subclasses to construct identifiers of form "classname://path/to/node"...
- (NSString*) identifierForPath:(NSString*)inPath
{
NSString* parserClassName = NSStringFromClass([self class]);
return [NSString stringWithFormat:@"%@:/%@",parserClassName,inPath];
}
//----------------------------------------------------------------------------------------------------------------------
// This method can be overridden by subclasses if the default promise is not useful...
- (IMBObjectPromise*) objectPromiseWithObjects:(NSArray*)inObjects
{
return [[(IMBObjectPromise*)[IMBLocalObjectPromise alloc] initWithObjects:inObjects] autorelease];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
- (id) loadThumbnailForObject:(IMBObject*)inObject
{
id imageRepresentation = nil;
NSString* type = inObject.imageRepresentationType;
NSString* path = nil;
NSURL* url = nil;
// Get path/url location of our object...
id location = inObject.imageLocation;
if (location == nil) location = inObject.location;
if ([location isKindOfClass:[NSString class]])
{
path = (NSString*)location;
url = [NSURL fileURLWithPath:path];
}
else if ([location isKindOfClass:[NSURL class]])
{
url = (NSURL*)location;
path = [url path];
}
// Get the uti for out object...
NSString* uti = [NSString UTIForFileAtPath:path];
// Path...
if ([type isEqualToString:IKImageBrowserPathRepresentationType])
{
imageRepresentation = path;
}
else if ([type isEqualToString:IKImageBrowserQTMoviePathRepresentationType])
{
imageRepresentation = path;
}
else if ([type isEqualToString:IKImageBrowserIconRefPathRepresentationType])
{
imageRepresentation = path;
}
else if ([type isEqualToString:IKImageBrowserQuickLookPathRepresentationType])
{
imageRepresentation = path;
}
// URL...
else if ([type isEqualToString:IKImageBrowserNSURLRepresentationType])
{
imageRepresentation = url;
}
// NSImage...
else if ([type isEqualToString:IKImageBrowserNSImageRepresentationType])
{
if (UTTypeConformsTo((CFStringRef)uti,kUTTypeImage))
{
imageRepresentation = [[[NSImage alloc] initByReferencingURL:url] autorelease];
}
else
{
imageRepresentation = [self _quicklookNSImageForURL:url];
}
}
// CGImage...
else if ([type isEqualToString:IKImageBrowserCGImageRepresentationType])
{
if (UTTypeConformsTo((CFStringRef)uti,kUTTypeImage))
{
imageRepresentation = (id)[self _imageForURL:url];
}
else
{
imageRepresentation = (id)[self _quicklookCGImageForURL:url];
}
}
// CGImageSourceRef...
else if ([type isEqualToString:IKImageBrowserCGImageSourceRepresentationType])
{
CGImageSourceRef source = [self _imageSourceForURL:url];
imageRepresentation = (id)source;
}
// NSData...
else if ([type isEqualToString:IKImageBrowserNSDataRepresentationType])
{
NSData* data = [NSData dataWithContentsOfURL:url];
imageRepresentation = data;
}
// NSBitmapImageRep...
else if ([type isEqualToString:IKImageBrowserNSBitmapImageRepresentationType])
{
if (UTTypeConformsTo((CFStringRef)uti,kUTTypeImage))
{
CGImageRef image = [self _imageForURL:url];
imageRepresentation = [[[NSBitmapImageRep alloc] initWithCGImage:image] autorelease];
}
else
{
CGImageRef image = [self _quicklookCGImageForURL:url];
imageRepresentation = [[[NSBitmapImageRep alloc] initWithCGImage:image] autorelease];
}
}
// QTMovie...
else if ([type isEqualToString:IKImageBrowserQTMovieRepresentationType])
{
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:url,@"url",inObject,@"object",nil];
[self performSelectorOnMainThread:@selector(_loadMovieRepresentation:)
withObject:info
waitUntilDone:NO
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
// Return the result to the main thread...
if (imageRepresentation)
{
[inObject
performSelectorOnMainThread:@selector(setImageRepresentation:)
withObject:imageRepresentation
waitUntilDone:NO
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
return imageRepresentation;
}
//----------------------------------------------------------------------------------------------------------------------
- (void) loadMetadataForObject:(IMBObject*)inObject
{
// to be overridden by subclasses. This method may be called on a background thread, so subclasses need to
// take appropriate safety measures...
}
//----------------------------------------------------------------------------------------------------------------------
// Invalidate the thumbnails for all object in this node tree. That way thumbnails are forced to be re-generated...
- (void) invalidateThumbnailsForNode:(IMBNode*)inNode
{
for (IMBNode* node in inNode.subNodes)
{
[self invalidateThumbnailsForNode:node];
}
for (IMBObject* object in inNode.objects)
{
object.needsImageRepresentation = YES;
object.imageVersion = object.imageVersion + 1;
}
}
- (void) invalidateThumbnails
{
IMBLibraryController* controller = [IMBLibraryController sharedLibraryControllerWithMediaType:self.mediaType];
IMBNode* rootNode = [controller rootNodeForParser:self];
[self invalidateThumbnailsForNode:rootNode];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Helpers
// Returns an autoreleased source for the given url...
- (CGImageSourceRef) _imageSourceForURL:(NSURL*)inURL
{
CGImageSourceRef source = NULL;
if (inURL)
{
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
nil];
source = CGImageSourceCreateWithURL((CFURLRef)inURL,(CFDictionaryRef)options);
[NSMakeCollectable(source) autorelease];
}
return source;
}
// Returns an autoreleased image for the given url...
- (CGImageRef) _imageForURL:(NSURL*)inURL
{
CGImageRef image = NULL;
if (inURL)
{
CGImageSourceRef source = [self _imageSourceForURL:inURL];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue,(id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanFalse,(id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
(id)kCFBooleanTrue,(id)kCGImageSourceCreateThumbnailFromImageAlways, // bug in rotation so let's use the full size always
[NSNumber numberWithInteger:kIMBMaxThumbnailSize],(id)kCGImageSourceThumbnailMaxPixelSize,
nil];
image = CGImageSourceCreateThumbnailAtIndex(source,0,(CFDictionaryRef)options);
[NSMakeCollectable(image) autorelease];
}
return image;
}
//----------------------------------------------------------------------------------------------------------------------
// Quicklook methods to create images from non-image files...
- (CGImageRef) _quicklookCGImageForURL:(NSURL*)inURL
{
CGSize size = CGSizeMake(128,128);
CGImageRef image = QLThumbnailImageCreate(kCFAllocatorDefault,(CFURLRef)inURL,size,NULL);
return (CGImageRef) [NSMakeCollectable(image) autorelease];
}
- (NSImage*) _quicklookNSImageForURL:(NSURL*)inURL
{
NSImage* nsimage = nil;
CGImageRef cgimage = [self _quicklookCGImageForURL:inURL];
if (cgimage)
{
NSSize size = NSZeroSize;
size.width = CGImageGetWidth(cgimage);
size.height = CGImageGetWidth(cgimage);
nsimage = [[[NSImage alloc] initWithSize:size] autorelease];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
[nsimage addRepresentation:rep];
[rep release];
}
return nsimage;
}
//----------------------------------------------------------------------------------------------------------------------
// Loading movies must be done on the main thread (as many components are not threadsafe). Alas, this blocks
// the main runloop, but what are we to do...
- (void) _loadMovieRepresentation:(NSDictionary*)inInfo
{
// Load the QTMovie object...
NSURL* url = (NSURL*)[inInfo objectForKey:@"url"];
IMBObject* object = (IMBObject*)[inInfo objectForKey:@"object"];
QTMovie* movie = [self _movieForURL:url];
[object setImageRepresentation:movie];
// Set a better poster time...
QTTime d = movie.duration;
double duration = (double)d.timeValue / (double)d.timeScale;
double posterTime = 0.5 * duration;
[movie setAttribute:[NSNumber numberWithDouble:posterTime] forKey:QTMoviePosterTimeAttribute];
// Load and cache the poster frame...
if ([object isKindOfClass:[IMBMovieObject class]])
{
NSError* error = nil;
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:QTMovieFrameImageTypeCGImageRef,QTMovieFrameImageType,nil];
QTTime t = QTMakeTimeWithTimeInterval(posterTime);
CGImageRef image = (CGImageRef) [movie frameImageAtTime:t withAttributes:attributes error:&error];
[(IMBMovieObject*)object setPosterFrame:image];
}
}
// Returns an autoreleased movie for the given url...
- (QTMovie*) _movieForURL:(NSURL*)inURL
{
QTMovie* movie = NULL;
if (inURL)
{
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
inURL,QTMovieURLAttribute,
[NSNumber numberWithBool:YES],QTMovieOpenAsyncOKAttribute,
// [NSNumber numberWithBool:YES],@"QTMovieOpenForPlaybackAttribute", // constant is not available with 10.5.sdk!
nil];
NSError* error = nil;
movie = [QTMovie movieWithAttributes:attributes error:&error];
}
return movie;
}
//----------------------------------------------------------------------------------------------------------------------
@end