-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratePreviewForURL.c
66 lines (51 loc) · 2.39 KB
/
GeneratePreviewForURL.c
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
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#include "apd.h"
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
----------------------------------------------------------------------------- */
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
CFStringRef rawPath = CFURLCopyPath(url);
CFStringRef path = CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, rawPath, CFSTR(""));
CFRelease(rawPath);
CFIndex length = CFStringGetLength(path);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char* c_path = (char*)malloc(maxSize);
CFStringGetCString(path, c_path, maxSize, kCFStringEncodingUTF8);
CFRelease(path);
aPicture picture;
int ret = load_picture(c_path, &picture);
free(c_path);
if (ret != 0) {
return noErr;
}
int samples = 3;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGSize imageSize = CGSizeMake(picture.width, picture.height);
CGContextRef ctx = QLPreviewRequestCreateContext(preview, imageSize, true, options);
if(!ctx) {
free(picture.rgbdata);
return noErr;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, picture.rgbdata, picture.width*picture.height*samples, NULL);
CGColorSpaceRef deviceRGBColorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef image = CGImageCreate(picture.width, picture.height, 8, 8 * samples, picture.width * samples, deviceRGBColorSpace, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault);
CGContextDrawImage(ctx, CGRectMake(0, 0, picture.width, picture.height), image);
CGDataProviderRelease(provider);
CGColorSpaceRelease(deviceRGBColorSpace);
CGImageRelease(image);
free(picture.rgbdata);
CGContextFlush(ctx);
QLPreviewRequestFlushContext(preview, ctx);
CFRelease(ctx);
return noErr;
}
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview)
{
// Implement only if supported
}