summaryrefslogtreecommitdiff
path: root/macosx/Pcsxr-QL/GeneratePreviewForURL.m
blob: 20cee6ba3442f6d5ce096272c512424ad6155fc3 (plain) (blame)
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
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#include "MyQuickLook.h"
#include <zlib.h>
#import <Foundation/Foundation.h>
#include "nopic.h"

/* -----------------------------------------------------------------------------
   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)
{
	@autoreleasepool {
		NSData *data;
		NSURL *urlNS = (__bridge NSURL *)(url);
		gzFile f;
		const char* state_filename;
		if ([urlNS respondsToSelector:@selector(fileSystemRepresentation)]) {
			state_filename = [urlNS fileSystemRepresentation];
		} else {
			state_filename = [[urlNS path] fileSystemRepresentation];
		}
		
		unsigned char *pMem = (unsigned char *) malloc(128*96*3);
		if (pMem == NULL)
			return mFulErr;
		
		f = gzopen(state_filename, "rb");
		if (f != NULL) {
			gzseek(f, 32, SEEK_SET); // skip header
			gzseek(f, sizeof(uint32_t), SEEK_CUR);
			gzseek(f, sizeof(uint8_t), SEEK_CUR);
			gzread(f, pMem, 128*96*3);
			gzclose(f);
		} else {
			memcpy(pMem, NoPic_Image.pixel_data, 128*96*3);
		}
		
		NSBitmapImageRep *imRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pMem pixelsWide:NoPic_Image.width pixelsHigh:NoPic_Image.height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bitmapFormat:0 bytesPerRow:NoPic_Image.width * NoPic_Image.bytes_per_pixel bitsPerPixel:24];
		if (imRep) {
			data = [imRep TIFFRepresentation];
			QLPreviewRequestSetDataRepresentation(preview, (__bridge CFDataRef)(data), kUTTypeImage, NULL);
		}
		free(pMem);
	}
	return noErr;
}

void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview)
{
	// Implement only if supported
}