File: programming/cocoa/ PreviewFile.m

/* =============================================================================
    PROJECT:    Filie
    FILE:       NSWorkspace+PreviewFile.m
    COPYRIGHT:  (c) 2004 by M. Uli Kusterer, all rights reserved.
    AUTHORS:    M. Uli Kusterer - UK
    LICENSES:   GNU GPL, Modified BSD
        2004-12-05  UK  Created.
   ========================================================================== */
// -----------------------------------------------------------------------------
//  Headers:
// -----------------------------------------------------------------------------
#import "NSWorkspace+PreviewFile.h"
#import "NSImage+Epeg.h"
#import "NSString+CarbonUtilities.h"
// -----------------------------------------------------------------------------
//  Data Structures:
// -----------------------------------------------------------------------------
// 'pnot' resource's data structure used by QuickTime for its previews:
#pragma options align=mac68k
/* last modification date of item */
    OSType          useType; /* what type of data */
    OSType          resType;/* resource type containing item *//* resource ID containing this item *//* region code *//* set to 0 *//* modification date *//* version number of preview resource */
    OSType              resType;/* type of resource used as preview cache *//* resource identification number of resource used as preview cache *//* number of additional file descriptions */
    PnotResItem         resItem[0]; /* array of file descriptions */#pragma options align=reset
// -----------------------------------------------------------------------------
//  previewImageForFile:size:
//      Generates a preview image for an image file and gives it the specified
//      dimensions.
//      Uses libepeg for JPEG files, otherwise attempts to load a QuickTime
//      preview if there is one. Returns NIL if both of these fail.
//      2004-12-05  UK  Created.
// -----------------------------------------------------------------------------
// Amazingly, this is faster than loading QuickTime's preview:
"jpg""jpeg"// If it's not a JPEG, we can't quick-preview, so try the QuickTime Preview:
// In case it was already open.
            PnotResHandle  theRes = (PnotResHandle) Get1Resource( 'pnot'// 16384 == -1.
'Prev'// Now, if we've found a preview we know how to handle, generate an image from it:
'PICT' )
                Handle  thePic = Get1Resource( 'PICT'

This code uses the PclZip Zip File reading code, which is subject to the GNU LGPL. It also uses the GeSHi syntax highlighter, subject to the GPL. Ask if you want this for your own web site, it's free.