GeneratePreviewForURL.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Adapted from QuickLook-pfm-master/src/GeneratePreviewForURL.m by Andreas
  2. // Steinel (https://github.com/lnxbil/quicklook-pfm)
  3. #import <CoreFoundation/CoreFoundation.h>
  4. #import <CoreServices/CoreServices.h>
  5. #import <QuickLook/QuickLook.h>
  6. #import <Foundation/Foundation.h>
  7. #import <AppKit/NSImage.h>
  8. #include "render_to_buffer.h"
  9. // Generate a preview of a given file.
  10. OSStatus GeneratePreviewForURL(
  11. void *thisInterface,
  12. QLPreviewRequestRef preview,
  13. CFURLRef url,
  14. CFStringRef contentTypeUTI,
  15. CFDictionaryRef options)
  16. {
  17. if (QLPreviewRequestIsCancelled(preview))
  18. return noErr;
  19. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  20. CFStringRef file = CFURLCopyFileSystemPath(url,kCFURLPOSIXPathStyle);
  21. if (QLPreviewRequestIsCancelled(preview))
  22. return noErr;
  23. // http://stackoverflow.com/a/3797923/148668
  24. // Multi-sampling for anti-aliasing.
  25. const double MS = 2;
  26. const int width = 720;
  27. const int height = 405;
  28. const int bwidth = MS*width;
  29. const int bheight = MS*height;
  30. const int bchannels = 4;
  31. UInt8 buffer[bwidth * bheight * bchannels];
  32. const char *cs = CFStringGetCStringPtr( file, CFStringGetSystemEncoding()) ;
  33. char cs_buf[1024];
  34. // http://stackoverflow.com/a/1609664/148668
  35. if(cs == NULL)
  36. {
  37. CFStringGetCString(
  38. file,
  39. cs_buf,
  40. [(NSString*)file length]+1,
  41. kCFStringEncodingUTF8);
  42. cs = cs_buf;
  43. }
  44. const float TRANSPARENT_BLACK[4] = {0,0,0,0};
  45. render_to_buffer(cs,TRANSPARENT_BLACK,bwidth,bheight,buffer);
  46. CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  47. CFDataRef rgbData = CFDataCreate(NULL, buffer, bwidth * bheight * 4);
  48. CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
  49. // http://forum.sparrow-framework.org/topic/create-uiimage-from-pixel-data-problems
  50. CGImageRef image =
  51. CGImageCreate(
  52. bwidth,
  53. bheight,
  54. 8,
  55. 8*4,
  56. bwidth * 4,
  57. colorspace,
  58. kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast,
  59. provider,
  60. NULL,
  61. true,
  62. kCGRenderingIntentDefault);
  63. CFRelease(rgbData);
  64. CGDataProviderRelease(provider);
  65. CGColorSpaceRelease(colorspace);
  66. NSSize size = {width,height};
  67. if (QLPreviewRequestIsCancelled(preview))
  68. return noErr;
  69. // Draw onto context as textured rectangle
  70. CGContextRef cgContext = QLPreviewRequestCreateContext(preview, *(CGSize *)&size, true, NULL);
  71. CGRect rect = CGRectMake(0,0, width, height);
  72. CGContextDrawImage(cgContext, rect, image);
  73. QLPreviewRequestFlushContext(preview, cgContext);
  74. CFRelease(cgContext);
  75. CGImageRelease(image);
  76. [pool release];
  77. return noErr;
  78. }
  79. void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
  80. {
  81. // implement only if supported
  82. }