Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 139 lines (114 sloc) 5.237 kb
720b0e7b »
2010-03-04 Added licensing information to the source files.
1 //
2 // NSWorkspace+PreviewFile.m
3 // Filie
4 //
5 // Created by Uli Kusterer on 5.12.2004
6 // Copyright 2004 by Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
dc5be1c0 »
2009-07-04 Initial check-in.
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "NSWorkspace+PreviewFile.h"
33 #import "NSImage+Epeg.h"
34 #import "NSString+CarbonUtilities.h"
35
36
37 // -----------------------------------------------------------------------------
38 // Data Structures:
39 // -----------------------------------------------------------------------------
40
41 // 'pnot' resource's data structure used by QuickTime for its previews:
42
43 #pragma options align=mac68k
44
45 typedef struct PnotResItem {
46 unsigned long modDate;/* last modification date of item */
47 OSType useType; /* what type of data */
48 OSType resType;/* resource type containing item */
49 short resID; /* resource ID containing this item */
50 short rgnCode;/* region code */
51 long reserved;/* set to 0 */
52 } PnotResItem;
53
54 typedef struct PnotResource {
55 unsigned long modDate; /* modification date */
56 short version;/* version number of preview resource */
57 OSType resType;/* type of resource used as preview cache */
58 short resID; /* resource identification number of resource used as preview cache */
59 signed short numResItems;/* number of additional file descriptions */
60 PnotResItem resItem[0]; /* array of file descriptions */
61 } PnotResource;
62 typedef PnotResource **PnotResHandle;
63
64 #pragma options align=reset
65
66
67 @implementation NSWorkspace (UKPreviewFile)
68
69 // -----------------------------------------------------------------------------
70 // previewImageForFile:size:
71 // Generates a preview image for an image file and gives it the specified
72 // dimensions.
73 // Uses libepeg for JPEG files, otherwise attempts to load a QuickTime
74 // preview if there is one. Returns NIL if both of these fail.
75 //
76 // REVISIONS:
77 // 2004-12-05 UK Created.
78 // -----------------------------------------------------------------------------
79
80 -(NSImage*) previewImageForFile: (NSString*)fpath size: (NSSize)neededSize
81 {
82 FSRef fileRef;
83 NSImage* img = nil;
84 NSString* suf = [[fpath pathExtension] lowercaseString];
85
86 // Amazingly, this is faster than loading QuickTime's preview:
87 if( [suf isEqualToString: @"jpg"] || [suf isEqualToString: @"jpeg"] )
88 img = [NSImage previewImageWithContentsOfFile: fpath boundingBox: neededSize];
89
90 // If it's not a JPEG, we can't quick-preview, so try the QuickTime Preview:
91 if( !img && [fpath getFSRef: &fileRef] )
92 {
93 short refNum = FSOpenResFile( &fileRef, fsRdPerm );
94
95 if( refNum > 0 && ResError() == noErr )
96 {
97 OSType resType = 0;
98 short resID = 0;
99
100 UseResFile( refNum ); // In case it was already open.
101
102 PnotResHandle theRes = (PnotResHandle) Get1Resource( 'pnot', 0 );
103 if( theRes != NULL && ResError() == noErr && MemError() == noErr )
104 {
105 int x;
106
107 resType = (**theRes).resType;
108 resID = (**theRes).resID;
109
110 if( (**theRes).numResItems < 16384 ) // 16384 == -1.
111 {
112 for( x = 0; x < (**theRes).numResItems; x++ )
113 {
114 if( (**theRes).resItem[x].useType == 'Prev' )
115 {
116 resType = (**theRes).resItem[x].resType;
117 resID = (**theRes).resItem[x].resID;
118 }
119 }
120 }
121 }
122
123 // Now, if we've found a preview we know how to handle, generate an image from it:
124 if( resType == 'PICT' )
125 {
126 Handle thePic = Get1Resource( 'PICT', resID );
127 NSData* data = [NSData dataWithBytes: *thePic length: GetHandleSize(thePic)];
128 img = [[[NSImage alloc] initWithData: data] autorelease];
129 }
130
131 CloseResFile( refNum );
132 }
133 }
134
135 return img;
136 }
137
138 @end
Something went wrong with that request. Please try again.