10 |
11 | # Installation
12 |
13 | If you want to skip compilation and just install it, [download Release 1.0](https://github.com/rsodre/QuickLookASE/releases), unzip and copy `QuickLookASE.qlgenerator` to `~/Library/QuickLook/`. To reach that folder in Finder, go to your Home, click on the Go menu on the top bar, hold the Option key and `Library` will magically appear.
14 |
15 | Or copy it from a terminal:
16 |
17 |
18 | cp -R QuickLookASE.qlgenerator ~/Library/QuickLook/
19 |
20 |
21 | Alternatively, if you use [Homebrew-Cask](https://github.com/caskroom/homebrew-cask), install with:
22 |
23 |
24 | brew cask install quicklookase
25 |
26 |
27 | # Notes
28 |
29 | How to find the UTI of a file:
30 |
31 |
32 | mdls -name kMDItemContentType MySwatch.ase
33 |
34 |
35 |
36 | # How It Looks:
37 |
38 | 
39 |
40 | 
41 |
42 |
--------------------------------------------------------------------------------
/example1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rsodre/QuickLookASE/930d7ccf59a86ecbb42220e676ab80d20c3d3bcc/example1.png
--------------------------------------------------------------------------------
/example2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rsodre/QuickLookASE/930d7ccf59a86ecbb42220e676ab80d20c3d3bcc/example2.png
--------------------------------------------------------------------------------
/src/DataReader.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | File: SKTDrawDocument.h
4 |
5 | Abstract: Part of the Sketch Sample Code
6 |
7 | Version: 1.0
8 |
9 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
10 | Computer, Inc. ("Apple") in consideration of your agreement to the
11 | following terms, and your use, installation, modification or
12 | redistribution of this Apple software constitutes acceptance of these
13 | terms. If you do not agree with these terms, please do not use,
14 | install, modify or redistribute this Apple software.
15 |
16 | In consideration of your agreement to abide by the following terms, and
17 | subject to these terms, Apple grants you a personal, non-exclusive
18 | license, under Apple's copyrights in this original Apple software (the
19 | "Apple Software"), to use, reproduce, modify and redistribute the Apple
20 | Software, with or without modifications, in source and/or binary forms;
21 | provided that if you redistribute the Apple Software in its entirety and
22 | without modifications, you must retain this notice and the following
23 | text and disclaimers in all such redistributions of the Apple Software.
24 | Neither the name, trademarks, service marks or logos of Apple Computer,
25 | Inc. may be used to endorse or promote products derived from the Apple
26 | Software without specific prior written permission from Apple. Except
27 | as expressly stated in this notice, no other rights or licenses, express
28 | or implied, are granted by Apple herein, including but not limited to
29 | any patent rights that may be infringed by your derivative works or by
30 | other works in which the Apple Software may be incorporated.
31 |
32 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37 |
38 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45 | POSSIBILITY OF SUCH DAMAGE.
46 |
47 | Copyright (C) 2007 Apple Inc. All Rights Reserved.
48 |
49 | */
50 |
51 | #import
52 |
53 | @interface DataReader : NSObject {
54 | @private
55 |
56 | NSData * _fileData;
57 | unsigned char * _buffer;
58 | int _length;
59 | int _position;
60 |
61 | }
62 |
63 | - (int)remaining;
64 | - (int)length;
65 |
66 | - (BOOL)readFromURL:(NSURL *)url;
67 | //- (unsigned char *)GetChunk:(int)size;
68 | - (unsigned char *)GetBytes:(int)size;
69 | - (NSString*)GetString:(int)size;
70 | - (int)GetInt;
71 | - (int)GetIntOfSize:(int)size;
72 | - (float)GetFloat;
73 | - (float)GetFloatOfSize:(int)size;
74 |
75 | @end
76 |
--------------------------------------------------------------------------------
/src/DataReader.m:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | File: SwatchDocument.m
4 |
5 | Abstract: Part of the Sketch Sample Code
6 |
7 | Version: 1.0
8 |
9 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
10 | Computer, Inc. ("Apple") in consideration of your agreement to the
11 | following terms, and your use, installation, modification or
12 | redistribution of this Apple software constitutes acceptance of these
13 | terms. If you do not agree with these terms, please do not use,
14 | install, modify or redistribute this Apple software.
15 |
16 | In consideration of your agreement to abide by the following terms, and
17 | subject to these terms, Apple grants you a personal, non-exclusive
18 | license, under Apple's copyrights in this original Apple software (the
19 | "Apple Software"), to use, reproduce, modify and redistribute the Apple
20 | Software, with or without modifications, in source and/or binary forms;
21 | provided that if you redistribute the Apple Software in its entirety and
22 | without modifications, you must retain this notice and the following
23 | text and disclaimers in all such redistributions of the Apple Software.
24 | Neither the name, trademarks, service marks or logos of Apple Computer,
25 | Inc. may be used to endorse or promote products derived from the Apple
26 | Software without specific prior written permission from Apple. Except
27 | as expressly stated in this notice, no other rights or licenses, express
28 | or implied, are granted by Apple herein, including but not limited to
29 | any patent rights that may be infringed by your derivative works or by
30 | other works in which the Apple Software may be incorporated.
31 |
32 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37 |
38 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45 | POSSIBILITY OF SUCH DAMAGE.
46 |
47 | Copyright (C) 2007 Apple Inc. All Rights Reserved.
48 |
49 | */
50 |
51 |
52 | #import
53 | #import "DataReader.h"
54 |
55 | @implementation DataReader
56 |
57 | - (BOOL)readFromURL:(NSURL *)url {
58 |
59 | _fileData = [[NSData alloc] initWithContentsOfURL:(NSURL*)url];
60 | if (_fileData == NULL)
61 | return false;
62 |
63 | _buffer = (unsigned char*)[_fileData bytes];
64 | _length = [_fileData length];
65 | _position = 0;
66 |
67 | return true;
68 | }
69 |
70 | - (int)length {
71 | return _length;
72 | }
73 |
74 | - (int)remaining {
75 | return (_length - _position);
76 | }
77 |
78 | - (unsigned char *)GetChunk:(int)size
79 | {
80 | if (size <= 0 || size > [self remaining])
81 | return NULL;
82 | unsigned char * chunk = &(_buffer[_position]);
83 | _position += size;
84 | return chunk;
85 | }
86 | - (unsigned char *)GetBytes:(int)size
87 | {
88 | unsigned char * chunk = [self GetChunk:size];
89 | if (chunk == NULL)
90 | {
91 | NSLog(@"SwatchPalette.Reader.GetBytes(): Invalid chunk size [%d] remaining [%d]",size,[self remaining]);
92 | return NULL;
93 | }
94 | return chunk;
95 | }
96 | - (NSString*)GetString:(int)size
97 | {
98 | unsigned char * chunk = [self GetChunk:size];
99 | if (chunk == NULL)
100 | {
101 | NSLog(@"SwatchPalette.Reader.GetString(): Invalid chunk size [%d] remaining [%d]",size,[self remaining]);
102 | return NULL;
103 | }
104 | NSString *s = [[NSString alloc] initWithBytes:chunk
105 | length:size
106 | encoding:NSUTF8StringEncoding];
107 | return s;
108 | }
109 | - (int)GetInt
110 | {
111 | return [self GetIntOfSize:4];
112 | }
113 | - (int)GetIntOfSize:(int)size
114 | {
115 | if (size > 4)
116 | {
117 | NSLog(@"SwatchPalette.Reader.GetInt(): Invalid int size [%d], expecting <= 4",size);
118 | return -1;
119 | }
120 | unsigned char * chunk = [self GetChunk:size];
121 | if (chunk == NULL)
122 | {
123 | NSLog(@"SwatchPalette.Reader.GetInt(): Invalid chunk size [%d] remaining [%d]",size,[self remaining]);
124 | return -1;
125 | }
126 | unsigned int res;
127 | NSString *hex = [self ChunkToHex:chunk size:size];
128 | NSScanner* scanner = [NSScanner scannerWithString:hex];
129 | [scanner scanHexInt:&res];
130 | return (int)res;
131 | }
132 | - (float)GetFloat
133 | {
134 | return [self GetFloatOfSize:4];
135 | }
136 | - (float)GetFloatOfSize:(int)size
137 | {
138 | if (size > 4)
139 | {
140 | NSLog(@"SwatchPalette.Reader.GetFloat(): Invalid int size [%d], expecting <= 4",size);
141 | return -1;
142 | }
143 | unsigned char * chunk = [self GetChunk:size];
144 | if (chunk == NULL)
145 | {
146 | NSLog(@"SwatchPalette.Reader.GetFloat(): Invalid chunk size [%d] remaining [%d]",size,[self remaining]);
147 | return -1;
148 | }
149 | union {
150 | float f;
151 | unsigned int i;
152 | } res;
153 | NSString *hex = [self ChunkToHex:chunk size:size];
154 | NSScanner* scanner = [NSScanner scannerWithString:hex];
155 | [scanner scanHexInt:&res.i];
156 | return res.f;
157 | }
158 |
159 | - (NSString*) ChunkToHex:(unsigned char *)chunk size:(int)size
160 | {
161 | NSMutableString * result = [[NSMutableString alloc] init];
162 | for (int i=0; i
51 | #include
52 | #include
53 | #import
54 | #import "SwatchDocument.h"
55 |
56 |
57 | /* -----------------------------------------------------------------------------
58 | Generate a preview for file
59 |
60 | This function's job is to create preview for designated file
61 | ----------------------------------------------------------------------------- */
62 |
63 | OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
64 | {
65 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
66 |
67 | SwatchDocument* document = [[SwatchDocument alloc] init];
68 |
69 | if(![document readFromURL:(NSURL *)url]) {
70 | [document release];
71 | [pool release];
72 | return noErr;
73 | }
74 |
75 | NSSize canvasSize = [document canvasSize];
76 |
77 | // Preview will be drawn in a vectorized context
78 | // Here we create a graphics context to draw the Quick Look Preview in
79 | CGContextRef cgContext = QLPreviewRequestCreateContext(preview, *(CGSize *)&canvasSize, false, NULL);
80 | if(cgContext) {
81 | NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)cgContext flipped:YES];
82 | if(context) {
83 | //These two lines of code are just good safe programming...
84 | [NSGraphicsContext saveGraphicsState];
85 | [NSGraphicsContext setCurrentContext:context];
86 |
87 | [document drawDocumentInContext:context];
88 |
89 | //This line sets the context back to what it was when we're done
90 | [NSGraphicsContext restoreGraphicsState];
91 | }
92 |
93 | // When we are done with our drawing code QLPreviewRequestFlushContext() is called to flush the context
94 | QLPreviewRequestFlushContext(preview, cgContext);
95 |
96 | CFRelease(cgContext);
97 | }
98 |
99 | [pool release];
100 | return noErr;
101 | }
102 |
103 | void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
104 | {
105 | // implement only if supported
106 | }
107 |
108 |
--------------------------------------------------------------------------------
/src/GenerateThumbnailForURL.m:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | File: GenerateThumbnailForURL.m
4 |
5 | Abstract: This is the code to generate Quick Look thumbnails for Sketch documents
6 |
7 | Version: 1.0
8 |
9 | Disclaimer: IMPORTANT: This Apple software is supplied to you by
10 | Apple Inc. ("Apple") in consideration of your agreement to the
11 | following terms, and your use, installation, modification or
12 | redistribution of this Apple software constitutes acceptance of these
13 | terms. If you do not agree with these terms, please do not use,
14 | install, modify or redistribute this Apple software.
15 |
16 | In consideration of your agreement to abide by the following terms, and
17 | subject to these terms, Apple grants you a personal, non-exclusive
18 | license, under Apple's copyrights in this original Apple software (the
19 | "Apple Software"), to use, reproduce, modify and redistribute the Apple
20 | Software, with or without modifications, in source and/or binary forms;
21 | provided that if you redistribute the Apple Software in its entirety and
22 | without modifications, you must retain this notice and the following
23 | text and disclaimers in all such redistributions of the Apple Software.
24 | Neither the name, trademarks, service marks or logos of Apple Inc.
25 | may be used to endorse or promote products derived from the Apple
26 | Software without specific prior written permission from Apple. Except
27 | as expressly stated in this notice, no other rights or licenses, express
28 | or implied, are granted by Apple herein, including but not limited to
29 | any patent rights that may be infringed by your derivative works or by
30 | other works in which the Apple Software may be incorporated.
31 |
32 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37 |
38 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45 | POSSIBILITY OF SUCH DAMAGE.
46 |
47 | Copyright (C) 2009 Apple Inc. All Rights Reserved.
48 | */
49 |
50 | #include
51 | #include
52 | #include
53 | #import
54 | #import "SwatchDocument.h"
55 |
56 | OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
57 | {
58 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
59 |
60 | SwatchDocument* document = [[SwatchDocument alloc] init];
61 |
62 | if(![document readFromURL:(NSURL *)url]) {
63 | [document release];
64 | [pool release];
65 | return noErr;
66 | }
67 |
68 | NSSize canvasSize = [document canvasSize];
69 |
70 | // Thumbnail will be drawn with maximum resolution for desired thumbnail request
71 | // Here we create a graphics context to draw the Quick Look Thumbnail in.
72 | CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, *(CGSize *)&canvasSize, false, NULL);
73 | if(cgContext) {
74 | NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)cgContext flipped:YES];
75 | if(context) {
76 | //These two lines of code are just good safe programming...
77 | [NSGraphicsContext saveGraphicsState];
78 | [NSGraphicsContext setCurrentContext:context];
79 |
80 | //Normally you'd want to do something much more lightweight for a thumbnail,
81 | //like load an pre-generated image embedded in the document or something
82 | //but for a quick and dirty sample, we'll just render the document here
83 | [document drawDocumentInContext:context];
84 |
85 | //This line sets the context back to what it was when we're done
86 | [NSGraphicsContext restoreGraphicsState];
87 | }
88 |
89 | // When we are done with our drawing code QLThumbnailRequestFlushContext() is called to flush the context
90 | QLThumbnailRequestFlushContext(thumbnail, cgContext);
91 |
92 | CFRelease(cgContext);
93 | }
94 |
95 | [pool release];
96 | return noErr;
97 | }
98 |
99 | void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
100 | {
101 | // implement only if supported
102 | }
103 |
104 |
--------------------------------------------------------------------------------
/src/SwatchDocument.h:
--------------------------------------------------------------------------------
1 | //
2 | // File: SwatchDocument.h
3 | // Version: 1.0
4 | //
5 |
6 | #import
7 |
8 | @interface SwatchDocument : NSObject {
9 | @private
10 |
11 | NSMutableArray *colors;
12 |
13 | }
14 |
15 | // Return the current value of the property.
16 | - (NSSize)canvasSize;
17 |
18 | // Added for Quick Look generator
19 | - (void)drawDocumentInContext:(NSGraphicsContext *)context;
20 | - (BOOL)readFromURL:(NSURL *)url;
21 |
22 |
23 | @end
24 |
--------------------------------------------------------------------------------
/src/SwatchDocument.m:
--------------------------------------------------------------------------------
1 | //
2 | // File: SwatchDocument.m
3 | // Version: 1.0
4 | //
5 |
6 | #import
7 | #import "SwatchDocument.h"
8 | #import "DataReader.h"
9 |
10 | #define HEIGHT 600
11 | #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
12 |
13 | const int BLOCK_GROUP_START = 0xc001;
14 | const int BLOCK_GROUP_END = 0xc002;
15 | const int BLOCK_COLOR = 0x0001;
16 |
17 | enum ColorMode
18 | {
19 | ColorModeUndefined = -1,
20 | ColorModeCMYK,
21 | ColorModeRGB,
22 | ColorModeLAB,
23 | ColorModeGray
24 | };
25 |
26 | enum ColorType
27 | {
28 | ColorTypeUndefined = -1,
29 | ColorTypeGlobal,
30 | ColorTypeSpot,
31 | ColorTypeNormal
32 | };
33 |
34 | struct SwatchColor
35 | {
36 | NSString * name;
37 | NSString * group;
38 | enum ColorMode mode;
39 | enum ColorType type;
40 | NSColor * color;
41 | float rawValues[4];
42 | int rawValuesCount;
43 | };
44 |
45 | @implementation SwatchDocument
46 |
47 | - (BOOL)readFromURL:(NSURL *)url {
48 | // [self redirectConsoleLogToDocumentFolder];
49 |
50 | // Create colors array
51 | colors = [[NSMutableArray alloc]init];
52 |
53 | // Open data reader
54 | DataReader* reader = [[DataReader alloc] init];
55 | [reader readFromURL:url];
56 | NSLog(@"SwatchPalette: File size [%d]",[reader length]);
57 | if ([reader length] < 12)
58 | {
59 | NSLog(@"SwatchPalette: Invalid asset size [%d]",[reader length]);
60 | return false;
61 | }
62 |
63 | // Get Header
64 | NSString * signature = [reader GetString:4];
65 | int v1 = [reader GetIntOfSize:2];
66 | int v2 = [reader GetIntOfSize:2];
67 | NSLog(@"SwatchPalette: signature [%@] version [%d.%d]",signature,v1,v2);
68 | if (![signature isEqualToString:@"ASEF"])
69 | {
70 | NSLog(@"SwatchPalette: Wrong signature [%@]. Must be [ASEF].",signature);
71 | return false;
72 | }
73 |
74 | // Get blocks
75 | NSString * groupName = @"";
76 | int blockCount = [reader GetInt];
77 | NSLog(@"SwatchPalette: Block count [%d]",blockCount);
78 | for (int b = 0 ; b < blockCount ; b++)
79 | {
80 | int type = [reader GetIntOfSize:2];
81 | int blockSize = [reader GetInt];
82 | NSLog(@"SwatchPalette: Block [%d] type [%d] size [%d]",b,type,blockSize);
83 |
84 | struct SwatchColor color;
85 |
86 | // Color
87 | if (type == BLOCK_GROUP_START || type == BLOCK_COLOR)
88 | {
89 | // Group/Colro name
90 | int nameSize = [reader GetIntOfSize:2];
91 | NSLog(@"SwatchPalette: nameSize [%d]",nameSize);
92 | if (nameSize > 0)
93 | {
94 | // 0-terminated string of length (uint16) double-byte characters
95 | unsigned char * bs = [reader GetBytes:nameSize*2];
96 | color.name = @"";
97 | for (int i = 0 ; i < nameSize-1 ; i++)
98 | {
99 | NSString *ch = [NSString stringWithFormat:@"%c", bs[(i*2)+1]];
100 | color.name = [color.name stringByAppendingString:ch];
101 | }
102 | NSLog(@"SwatchPalette: Name (%d) [%@]",nameSize,color.name);
103 | }
104 |
105 | if (type == BLOCK_GROUP_START)
106 | {
107 | groupName = color.name;
108 | NSLog(@"SwatchPalette: Group Start [%@]",groupName);
109 | }
110 | else //if (type == BLOCK_COLOR)
111 | {
112 | color.group = groupName;
113 |
114 | NSString * colorMode = [reader GetString:4];
115 | NSLog(@"SwatchPalette: Color mode [%@]",colorMode);
116 | if ([colorMode isEqualToString:@"CMYK"])
117 | {
118 | color.mode = ColorModeCMYK;
119 | color.rawValuesCount = 4;
120 | float C = color.rawValues[0] = [reader GetFloat];
121 | float M = color.rawValues[1] = [reader GetFloat];
122 | float Y = color.rawValues[2] = [reader GetFloat];
123 | float K = color.rawValues[3] = [reader GetFloat];
124 | float R = (1-C) * (1-K);
125 | float G = (1-M) * (1-K);
126 | float B = (1-Y) * (1-K);
127 | color.color = [NSColor colorWithRed:R green:G blue:B alpha:1];
128 | [colors addObject:color.color];
129 | // palette.Add(color);
130 | }
131 | else if ([colorMode isEqualToString:@"RGB "])
132 | {
133 | color.mode = ColorModeRGB;
134 | color.rawValuesCount = 3;
135 | float R = color.rawValues[0] = [reader GetFloat];
136 | float G = color.rawValues[1] = [reader GetFloat];
137 | float B = color.rawValues[2] = [reader GetFloat];
138 | color.color = [NSColor colorWithRed:R green:G blue:B alpha:1];
139 | [colors addObject:color.color];
140 | // palette.Add(color);
141 | }
142 |
143 | else if ([colorMode isEqualToString:@"LAB "])
144 | {
145 | color.mode = ColorModeLAB;
146 | color.rawValuesCount = 3;
147 | float L = color.rawValues[0] = [reader GetFloat];
148 | float A = color.rawValues[1] = [reader GetFloat];
149 | float B = color.rawValues[2] = [reader GetFloat];
150 | // color.color = [NSColor colorWithRed:R green:G blue:B alpha:1];
151 | // [colors addObject:color.color];
152 | // palette.Add(color);
153 | // No idea what LAB is !!!
154 | NSLog(@"SwatchPalette: LAB color not supportted (%.3f,%.3f,%.3f)",L,A,B);
155 | }
156 | else if ([colorMode isEqualToString:@"Gray"])
157 | {
158 | color.mode = ColorModeGray;
159 | color.rawValuesCount = 1;
160 | float G = color.rawValues[0] = [reader GetFloat];
161 | color.color = [NSColor colorWithWhite:G alpha:1];
162 | [colors addObject:color.color];
163 | // palette.Add(color);
164 | }
165 |
166 | // Color type
167 | color.type = [reader GetIntOfSize:2];
168 | }
169 | }
170 | else if (type == BLOCK_GROUP_END)
171 | {
172 | NSLog(@"SwatchPalette: Group End");
173 | }
174 | else
175 | {
176 | NSLog(@"SwatchPalette: Invalid type [%d]",type);
177 | }
178 | }
179 |
180 | return ([colors count] > 0);
181 | }
182 |
183 | - (void) dealloc
184 | {
185 | if (colors)
186 | [colors dealloc];
187 | [super dealloc];
188 | }
189 |
190 |
191 | - (NSSize)canvasSize {
192 | NSSize size = { CLAMP(colors.count, 2, 4)*(HEIGHT/2), HEIGHT };
193 | return size;
194 | }
195 |
196 |
197 | // Added for Quick Look generator
198 | - (void)drawDocumentInContext:(NSGraphicsContext *)context
199 | {
200 | // Init
201 | [NSGraphicsContext saveGraphicsState];
202 | [NSGraphicsContext setCurrentContext:context];
203 | [context saveGraphicsState];
204 |
205 | // Draw!
206 | {
207 | NSSize size = [self canvasSize];
208 |
209 | int w = size.width / colors.count;
210 | for (int i = 0 ; i < colors.count ; ++i)
211 | {
212 | [colors[i] setFill];
213 | NSRect aRect = NSMakeRect(w*i, 0.0, w, size.height);
214 | NSRectFill(aRect);
215 | }
216 | }
217 |
218 | // Finalize
219 | [context restoreGraphicsState];
220 | [NSGraphicsContext restoreGraphicsState];
221 | }
222 |
223 | - (void) redirectConsoleLogToDocumentFolder
224 | {
225 | NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:@"console.txt"];
226 | freopen([logPath fileSystemRepresentation],"a+",stderr);
227 | }
228 |
229 |
230 | @end
231 |
232 |
233 |
--------------------------------------------------------------------------------
/src/main.c:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | File: main.c
4 |
5 | Abstract: This is the CFPlug-in boilerplate code for a Quick Look preview generator
6 |
7 | Version: 1.0
8 |
9 | Disclaimer: IMPORTANT: This Apple software is supplied to you by
10 | Apple Inc. ("Apple") in consideration of your agreement to the
11 | following terms, and your use, installation, modification or
12 | redistribution of this Apple software constitutes acceptance of these
13 | terms. If you do not agree with these terms, please do not use,
14 | install, modify or redistribute this Apple software.
15 |
16 | In consideration of your agreement to abide by the following terms, and
17 | subject to these terms, Apple grants you a personal, non-exclusive
18 | license, under Apple's copyrights in this original Apple software (the
19 | "Apple Software"), to use, reproduce, modify and redistribute the Apple
20 | Software, with or without modifications, in source and/or binary forms;
21 | provided that if you redistribute the Apple Software in its entirety and
22 | without modifications, you must retain this notice and the following
23 | text and disclaimers in all such redistributions of the Apple Software.
24 | Neither the name, trademarks, service marks or logos of Apple Inc.
25 | may be used to endorse or promote products derived from the Apple
26 | Software without specific prior written permission from Apple. Except
27 | as expressly stated in this notice, no other rights or licenses, express
28 | or implied, are granted by Apple herein, including but not limited to
29 | any patent rights that may be infringed by your derivative works or by
30 | other works in which the Apple Software may be incorporated.
31 |
32 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37 |
38 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45 | POSSIBILITY OF SUCH DAMAGE.
46 |
47 | Copyright (C) 2007 Apple Inc. All Rights Reserved.
48 | */
49 |
50 | //==============================================================================
51 | //
52 | // DO NO MODIFY THE CONTENT OF THIS FILE
53 | //
54 | // This file contains the generic CFPlug-in code necessary for your generator
55 | // To complete your generator implement the function in GenerateThumbnailForURL/GeneratePreviewForURL.c
56 | //
57 | //==============================================================================
58 |
59 |
60 |
61 |
62 |
63 |
64 | #include
65 | #include
66 | #include
67 | #include
68 |
69 | // -----------------------------------------------------------------------------
70 | // constants
71 | // -----------------------------------------------------------------------------
72 |
73 | // Don't modify this line
74 | #define PLUGIN_ID "660EBE04-BCB7-46E1-B107-88FBB16EE5AF"
75 |
76 | //
77 | // Below is the generic glue code for all plug-ins.
78 | //
79 | // You should not have to modify this code aside from changing
80 | // names if you decide to change the names defined in the Info.plist
81 | //
82 |
83 |
84 | // -----------------------------------------------------------------------------
85 | // typedefs
86 | // -----------------------------------------------------------------------------
87 |
88 | // The thumbnail generation function to be implemented in GenerateThumbnailForURL.c
89 | OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
90 | void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail);
91 |
92 | // The preview generation function to be implemented in GeneratePreviewForURL.c
93 | OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
94 | void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
95 |
96 | // The layout for an instance of QuickLookGeneratorPlugIn
97 | typedef struct __QuickLookGeneratorPluginType
98 | {
99 | void *conduitInterface;
100 | CFUUIDRef factoryID;
101 | UInt32 refCount;
102 | } QuickLookGeneratorPluginType;
103 |
104 | // -----------------------------------------------------------------------------
105 | // prototypes
106 | // -----------------------------------------------------------------------------
107 | // Forward declaration for the IUnknown implementation.
108 | //
109 |
110 | QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID);
111 | void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance);
112 | HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
113 | void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
114 | ULONG QuickLookGeneratorPluginAddRef(void *thisInstance);
115 | ULONG QuickLookGeneratorPluginRelease(void *thisInstance);
116 |
117 | // -----------------------------------------------------------------------------
118 | // myInterfaceFtbl definition
119 | // -----------------------------------------------------------------------------
120 | // The QLGeneratorInterfaceStruct function table.
121 | //
122 | static QLGeneratorInterfaceStruct myInterfaceFtbl = {
123 | NULL,
124 | QuickLookGeneratorQueryInterface,
125 | QuickLookGeneratorPluginAddRef,
126 | QuickLookGeneratorPluginRelease,
127 | NULL,
128 | NULL,
129 | NULL,
130 | NULL
131 | };
132 |
133 |
134 | // -----------------------------------------------------------------------------
135 | // AllocQuickLookGeneratorPluginType
136 | // -----------------------------------------------------------------------------
137 | // Utility function that allocates a new instance.
138 | // You can do some initial setup for the generator here if you wish
139 | // like allocating globals etc...
140 | //
141 | QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID)
142 | {
143 | QuickLookGeneratorPluginType *theNewInstance;
144 |
145 | theNewInstance = (QuickLookGeneratorPluginType *)malloc(sizeof(QuickLookGeneratorPluginType));
146 | memset(theNewInstance,0,sizeof(QuickLookGeneratorPluginType));
147 |
148 | /* Point to the function table Malloc enough to store the stuff and copy the filler from myInterfaceFtbl over */
149 | theNewInstance->conduitInterface = malloc(sizeof(QLGeneratorInterfaceStruct));
150 | memcpy(theNewInstance->conduitInterface,&myInterfaceFtbl,sizeof(QLGeneratorInterfaceStruct));
151 |
152 | /* Retain and keep an open instance refcount for each factory. */
153 | theNewInstance->factoryID = CFRetain(inFactoryID);
154 | CFPlugInAddInstanceForFactory(inFactoryID);
155 |
156 | /* This function returns the IUnknown interface so set the refCount to one. */
157 | theNewInstance->refCount = 1;
158 | return theNewInstance;
159 | }
160 |
161 | // -----------------------------------------------------------------------------
162 | // DeallocQuickLookGeneratorPluginType
163 | // -----------------------------------------------------------------------------
164 | // Utility function that deallocates the instance when
165 | // the refCount goes to zero.
166 | // In the current implementation generator interfaces are never deallocated
167 | // but implement this as this might change in the future
168 | //
169 | void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance)
170 | {
171 | CFUUIDRef theFactoryID;
172 |
173 | theFactoryID = thisInstance->factoryID;
174 | /* Free the conduitInterface table up */
175 | free(thisInstance->conduitInterface);
176 |
177 | /* Free the instance structure */
178 | free(thisInstance);
179 | if (theFactoryID){
180 | CFPlugInRemoveInstanceForFactory(theFactoryID);
181 | CFRelease(theFactoryID);
182 | }
183 | }
184 |
185 | // -----------------------------------------------------------------------------
186 | // QuickLookGeneratorQueryInterface
187 | // -----------------------------------------------------------------------------
188 | // Implementation of the IUnknown QueryInterface function.
189 | //
190 | HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
191 | {
192 | CFUUIDRef interfaceID;
193 |
194 | interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
195 |
196 | if (CFEqual(interfaceID,kQLGeneratorCallbacksInterfaceID)){
197 | /* If the Right interface was requested, bump the ref count,
198 | * set the ppv parameter equal to the instance, and
199 | * return good status.
200 | */
201 | ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GenerateThumbnailForURL = GenerateThumbnailForURL;
202 | ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelThumbnailGeneration = CancelThumbnailGeneration;
203 | ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GeneratePreviewForURL = GeneratePreviewForURL;
204 | ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelPreviewGeneration = CancelPreviewGeneration;
205 | ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType*)thisInstance)->conduitInterface)->AddRef(thisInstance);
206 | *ppv = thisInstance;
207 | CFRelease(interfaceID);
208 | return S_OK;
209 | }else{
210 | /* Requested interface unknown, bail with error. */
211 | *ppv = NULL;
212 | CFRelease(interfaceID);
213 | return E_NOINTERFACE;
214 | }
215 | }
216 |
217 | // -----------------------------------------------------------------------------
218 | // QuickLookGeneratorPluginAddRef
219 | // -----------------------------------------------------------------------------
220 | // Implementation of reference counting for this type. Whenever an interface
221 | // is requested, bump the refCount for the instance. NOTE: returning the
222 | // refcount is a convention but is not required so don't rely on it.
223 | //
224 | ULONG QuickLookGeneratorPluginAddRef(void *thisInstance)
225 | {
226 | ((QuickLookGeneratorPluginType *)thisInstance )->refCount += 1;
227 | return ((QuickLookGeneratorPluginType*) thisInstance)->refCount;
228 | }
229 |
230 | // -----------------------------------------------------------------------------
231 | // QuickLookGeneratorPluginRelease
232 | // -----------------------------------------------------------------------------
233 | // When an interface is released, decrement the refCount.
234 | // If the refCount goes to zero, deallocate the instance.
235 | //
236 | ULONG QuickLookGeneratorPluginRelease(void *thisInstance)
237 | {
238 | ((QuickLookGeneratorPluginType*)thisInstance)->refCount -= 1;
239 | if (((QuickLookGeneratorPluginType*)thisInstance)->refCount == 0){
240 | DeallocQuickLookGeneratorPluginType((QuickLookGeneratorPluginType*)thisInstance );
241 | return 0;
242 | }else{
243 | return ((QuickLookGeneratorPluginType*) thisInstance )->refCount;
244 | }
245 | }
246 |
247 | // -----------------------------------------------------------------------------
248 | // QuickLookGeneratorPluginFactory
249 | // -----------------------------------------------------------------------------
250 | void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
251 | {
252 | QuickLookGeneratorPluginType *result;
253 | CFUUIDRef uuid;
254 |
255 | /* If correct type is being requested, allocate an
256 | * instance of kQLGeneratorTypeID and return the IUnknown interface.
257 | */
258 | if (CFEqual(typeID,kQLGeneratorTypeID)){
259 | uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
260 | result = AllocQuickLookGeneratorPluginType(uuid);
261 | CFRelease(uuid);
262 | return result;
263 | }
264 | /* If the requested type is incorrect, return NULL. */
265 | return NULL;
266 | }
267 |
--------------------------------------------------------------------------------