-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImagePreview.m
110 lines (94 loc) · 3.19 KB
/
ImagePreview.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
//
// ImagePreview.m
// Crossbow
//
// Copyright (C) 2009 Roland Rabien
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#import "ImagePreview.h"
#import "NSImageAdditions.h"
#import "DirEntry.h"
#import "Util.h"
@implementation ImagePreview
- (void)previewImage:(DirEntry*)de
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(updateNow:) withObject:de afterDelay:0.1];
}
- (void)updateNow:(DirEntry*)de
{
if (de && file && [de isEqual:file])
return;
if (de)
{
NSImage* img = [de image];
if (img)
{
file = de;
[NSThread detachNewThreadSelector:@selector(decodeProc:) toTarget:self withObject:[NSArray arrayWithObjects:de, img, nil]];
return;
}
}
file = nil;
cachedImage = nil;
self.image = nil;
}
- (void)decodeProc:(NSArray*)params
{
@autoreleasepool {
NSAssert(params.count == 2, @"params error");
if (params.count >= 2)
{
DirEntry* de = [params objectAtIndex:0];
NSImage* image = [params objectAtIndex:1];
NSImage* resizedImage = [image imageByScalingProportionallyToSize:NSMakeSize(512,512)];
if (resizedImage)
[self performSelectorOnMainThread:@selector(decodeFinished:) withObject:[NSArray arrayWithObjects:de, resizedImage, nil] waitUntilDone:NO];
NSAssert(image != nil && resizedImage != nil, @"nil images");
}
}
}
- (void)decodeFinished:(NSArray*)params
{
NSAssert(params.count == 2, @"params error");
if (params.count >= 2)
{
DirEntry* de = [params objectAtIndex:0];
NSImage* image = [params objectAtIndex:1];
if ([[file filetype] isEqual:@"com.adobe.pdf"])
{
NSImage* newImage = [[NSImage alloc] initWithSize:[image size]];
[newImage lockFocus];
[[NSColor whiteColor] set];
CGRect rc = NSMakeRect(0,0,[newImage size].width, [newImage size].height);
NSRectFill(rc);
[image drawInRect:rc];
[newImage unlockFocus];
image = newImage;
}
if (de && file && [de isEqual:file])
{
self.image = image;
cachedImage = image;
}
}
}
- (void)drawRect:(NSRect)dirtyRect
{
NSColor* bk = [NSColor blackColor];
[bk drawSwatchInRect:dirtyRect];
[super drawRect:dirtyRect];
}
@end