OSDN Git Service

MacGui: Add "Auto" to audio sample rate selections
[handbrake-jp/handbrake-jp-git.git] / macosx / PictureController.mm
index a2a456e..a4785ab 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id: PictureController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $
 
    This file is part of the HandBrake source code.
-   Homepage: <http://handbrake.m0k.org/>.
+   Homepage: <http://handbrake.fr/>.
    It may be used under the terms of the GNU General Public License. */
 
 #include "PictureController.h"
 
 @end
 
-static int GetAlignedSize( int size )
-{
-    int result = 1;
-    while( result < size )
-    {
-        result *= 2;
-    }
-    return result;
-}
-
 @implementation PictureController
 
 - (id)initWithDelegate:(id)del
 {
-       if (self = [super init])
+       if (self = [super initWithWindowNibName:@"PictureSettings"])
        {
+        // NSWindowController likes to lazily load its window. However since
+        // this controller tries to set all sorts of outlets before the window
+        // is displayed, we need it to load immediately. The correct way to do
+        // this, according to the documentation, is simply to invoke the window
+        // getter once.
+        //
+        // If/when we switch a lot of this stuff to bindings, this can probably
+        // go away.
+        [self window];
+
                delegate = del;
-        [self loadMyNibFile];
+        fPicturePreviews = [[NSMutableDictionary dictionaryWithCapacity: HB_NUM_HBLIB_PICTURES] retain];
        }
        return self;
 }
 
+- (void) dealloc
+{
+    [fPicturePreviews release];
+    [super dealloc];
+}
+
 - (void) SetHandle: (hb_handle_t *) handle
 {
     fHandle = handle;
 
-    fHasQE = CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay );
-
-    fBuffer     = NULL;
-    fBufferSize = 0;
-    fTexBuf[0]  = NULL;
-    fTexBuf[1]  = NULL;
-    fTexBufSize = 0;
-
     [fWidthStepper  setValueWraps: NO];
     [fWidthStepper  setIncrement: 16];
     [fWidthStepper  setMinValue: 64];
@@ -72,27 +70,6 @@ static int GetAlignedSize( int size )
 
     fTitle = title;
 
-    /* Make sure we have big enough buffers */
-    int newSize;
-    newSize = ( title->width + 2 ) * (title->height + 2 ) * 4;
-    if( fBufferSize < newSize )
-    {
-        fBufferSize = newSize;
-        fBuffer     = (uint8_t *) realloc( fBuffer, fBufferSize );
-    }
-    if( !fHasQE )
-    {
-        newSize = ( GetAlignedSize( title->width + 2 ) *
-            GetAlignedSize( title->height + 2 ) * 4 );
-    }
-    if( fTexBufSize < newSize )
-    {
-        fTexBufSize = newSize;
-        fTexBuf[0]  = (uint8_t *) realloc( fTexBuf[0], fTexBufSize );
-        fTexBuf[1]  = (uint8_t *) realloc( fTexBuf[1], fTexBufSize );
-    }
-
-
     [fWidthStepper      setMaxValue: title->width];
     [fWidthStepper      setIntValue: job->width];
     [fWidthField        setIntValue: job->width];
@@ -104,21 +81,19 @@ static int GetAlignedSize( int size )
     [fCropBottomStepper setMaxValue: title->height/2-2];
     [fCropLeftStepper   setMaxValue: title->width/2-2];
     [fCropRightStepper  setMaxValue: title->width/2-2];
-    
-       
-       /* we use a popup to show the deinterlace settings */
-       [fDeinterlacePopUp removeAllItems];
-    [fDeinterlacePopUp addItemWithTitle: @"None"];
-    [fDeinterlacePopUp addItemWithTitle: @"Fast"];
-    [fDeinterlacePopUp addItemWithTitle: @"Slow"];
-       [fDeinterlacePopUp addItemWithTitle: @"Slower"];
-       [fDeinterlacePopUp addItemWithTitle: @"Slowest"];
-    
-       /* Set deinterlaces level according to the integer in the main window */
-       [fDeinterlacePopUp selectItemAtIndex: fPictureFilterSettings.deinterlace];
-
-       [fPARCheck setState:(job->pixel_ratio ? NSOnState : NSOffState)];
 
+    /* Populate the Anamorphic NSPopUp button here */
+    [fAnamorphicPopUp removeAllItems];
+    [fAnamorphicPopUp addItemWithTitle: @"None"];
+    [fAnamorphicPopUp addItemWithTitle: @"Strict"];
+    if (allowLooseAnamorphic)
+    {
+    [fAnamorphicPopUp addItemWithTitle: @"Loose"];
+    }
+    [fAnamorphicPopUp selectItemAtIndex: job->pixel_ratio];
+    
+    /* We initially set the previous state of keep ar to on */
+    keepAspectRatioPreviousState = 1;
        if (!autoCrop)
        {
         [fCropMatrix  selectCellAtRow: 1 column:0];
@@ -137,20 +112,33 @@ static int GetAlignedSize( int size )
         [fCropMatrix  selectCellAtRow: 0 column:0];
        }
        
-       /* set the detelecine state according to the state in main window */
-       /* if framerate is 23.976 we do not allow detelecine, otherwise, enable and set according to fDetelecineMainWindow outlet */ 
-       if (fTitle->rate_base == 1126125)
-       {
-               [fDetelecineCheck setEnabled: NO];
-               [fDetelecineCheck setState: NSOffState];
-               
-       }
-       else
-       {
-        [fDetelecineCheck setEnabled: YES];
-        [fDetelecineCheck setState: fPictureFilterSettings.detelecine];
-       }
-       
+       /* Set filters widgets according to the filters struct */
+       [fVFRCheck setState:fPictureFilterSettings.vfr];
+    [fDetelecineCheck setState:fPictureFilterSettings.detelecine];
+    [fDeinterlacePopUp selectItemAtIndex: fPictureFilterSettings.deinterlace];
+    [fDenoisePopUp selectItemAtIndex: fPictureFilterSettings.denoise];
+    [fDeblockCheck setState: fPictureFilterSettings.deblock];
+    
+    fPicture = 0;
+    MaxOutputWidth = title->width - job->crop[2] - job->crop[3];
+    MaxOutputHeight = title->height - job->crop[0] - job->crop[1];
+    [self SettingsChanged: nil];
+}
+
+/* we use this to setup the initial picture filters upon first launch, after that their states
+are maintained across different sources */
+- (void) setInitialPictureFilters
+{
+       /* we use a popup to show the deinterlace settings */
+       [fDeinterlacePopUp removeAllItems];
+    [fDeinterlacePopUp addItemWithTitle: @"None"];
+    [fDeinterlacePopUp addItemWithTitle: @"Fast"];
+    [fDeinterlacePopUp addItemWithTitle: @"Slow"];
+       [fDeinterlacePopUp addItemWithTitle: @"Slower"];
+    
+       /* Set deinterlaces level according to the integer in the main window */
+       [fDeinterlacePopUp selectItemAtIndex: fPictureFilterSettings.deinterlace];
+
        /* we use a popup to show the denoise settings */
        [fDenoisePopUp removeAllItems];
     [fDenoisePopUp addItemWithTitle: @"None"];
@@ -159,76 +147,57 @@ static int GetAlignedSize( int size )
     [fDenoisePopUp addItemWithTitle: @"Strong"];
        /* Set denoises level according to the integer in the main window */
        [fDenoisePopUp selectItemAtIndex: fPictureFilterSettings.denoise];
-       
-    MaxOutputWidth = job->width;
-       MaxOutputHeight = job->height;
-    fPicture = 0;
-    [self SettingsChanged: nil];
+    
+    /* we use a popup to show the decomb settings */
+       [fDecombPopUp removeAllItems];
+    [fDecombPopUp addItemWithTitle: @"None"];
+    [fDecombPopUp addItemWithTitle: @"Default"];
+    [fDecombPopUp addItemWithTitle: @"Custom"];
+       /* Set denoises level according to the integer in the main window */
+       [fDecombPopUp selectItemAtIndex: fPictureFilterSettings.decomb];
+
 }
 
-- (void) Display: (int) anim
+// Adjusts the window to draw the current picture (fPicture) adjusting its size as
+// necessary to display as much of the picture as possible.
+- (void) displayPreview
 {
-    hb_get_preview( fHandle, fTitle, fPicture, fBuffer );
-
-    /* Backup previous picture (for effects) */
-    memcpy( fTexBuf[1], fTexBuf[0], fTexBufSize );
-
-    if( fHasQE )
+    [fPictureView setImage: [self imageForPicture: fPicture]];
+       
+       NSSize displaySize = NSMakeSize( (float)fTitle->width, (float)fTitle->height );
+    /* Set the picture size display fields below the Preview Picture*/
+    if( fTitle->job->pixel_ratio == 1 ) // Original PAR Implementation
     {
-        /* Simply copy */
-        memcpy( fTexBuf[0], fBuffer, fTexBufSize );
+        output_width = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
+        output_height = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
+        display_width = output_width * fTitle->job->pixel_aspect_width / fTitle->job->pixel_aspect_height;
+        [fInfoField setStringValue:[NSString stringWithFormat:
+                                    @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d",
+                                    fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
+        displaySize.width *= ((float)fTitle->job->pixel_aspect_width) / ((float)fTitle->job->pixel_aspect_height);   
     }
-    else
+    else if (fTitle->job->pixel_ratio == 2) // Loose Anamorphic
     {
-        /* Copy line by line */
-        uint8_t * in  = fBuffer;
-        uint8_t * out = fTexBuf[0];
-               
-        for( int i = fTitle->height + 2; i--; )
+        display_width = output_width * output_par_width / output_par_height;
+        [fInfoField setStringValue:[NSString stringWithFormat:
+                                    @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d",
+                                    fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
+        
+        /* FIXME: needs to be fixed so that the picture window does not resize itself on the first
+         anamorphic width drop
+         */
+        if (fTitle->width - 8 < output_width)
         {
-            memcpy( out, in, 4 * ( fTitle->width + 2 ) );
-            in  += 4 * ( fTitle->width + 2 );
-            out += 4 * GetAlignedSize( fTitle->width + 2 );
+            displaySize.width *= ((float)output_par_width) / ((float)output_par_height);
         }
-       
-    }
-
-    if( [fEffectsCheck state] == NSOffState )
-    {
-        anim = HB_ANIMATE_NONE;
-    }
-    else if( [[NSApp currentEvent] modifierFlags] & NSShiftKeyMask )
-    {
-        anim |= HB_ANIMATE_SLOW;
-    }
-
-    [fPictureGLView Display: anim buffer1: fTexBuf[0]
-        buffer2: fTexBuf[1] width: ( fTitle->width + 2 )
-        height: ( fTitle->height + 2 )];
-       
-       /* Set the Output Display below the Preview Picture*/
-       int titlewidth = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
-       int arpwidth = fTitle->job->pixel_aspect_width;
-       int arpheight = fTitle->job->pixel_aspect_height;
-       int displayparwidth = titlewidth * arpwidth / arpheight;
-       int displayparheight = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
-
-    NSSize displaySize = NSMakeSize( (float)fTitle->width, (float)fTitle->height );
-    if( fTitle->job->pixel_ratio == 1 )
-    {
-        [fInfoField setStringValue:[NSString stringWithFormat:
-                            @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d",
-                            fTitle->width, fTitle->height, titlewidth,
-                            displayparheight, displayparwidth, displayparheight]];
-        displaySize.width *= ((float)arpwidth) / ((float)arpheight);
     }
-    else
+    else // No Anamorphic
     {
-    [fInfoField setStringValue: [NSString stringWithFormat:
-        @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
-        fTitle->job->width, fTitle->job->height]];
+        [fInfoField setStringValue: [NSString stringWithFormat:
+                                     @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
+                                     fTitle->job->width, fTitle->job->height]];
     }
-
+    
     NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
     if( [self viewNeedsToResizeToSize:viewSize] )
     {
@@ -242,13 +211,13 @@ static int GetAlignedSize( int size )
     {
         float scale = viewSize.width / ((float)fTitle->width);
         NSString *scaleString = [NSString stringWithFormat:
-            NSLocalizedString( @" (Preview scaled to %.0f%% actual size)",
-                               @"String shown when a preview is scaled" ),
-            scale * 100.0];
+                                 NSLocalizedString( @" (Preview scaled to %.0f%% actual size)",
+                                                   @"String shown when a preview is scaled" ),
+                                 scale * 100.0];
         [fInfoField setStringValue:
-            [[fInfoField stringValue] stringByAppendingString:scaleString]];
+         [[fInfoField stringValue] stringByAppendingString:scaleString]];
     }
-
+    
     [fPrevButton setEnabled: ( fPicture > 0 )];
     [fNextButton setEnabled: ( fPicture < 9 )];
 }
@@ -257,64 +226,110 @@ static int GetAlignedSize( int size )
 {
     hb_job_t * job = fTitle->job;
     
-       if( [fPARCheck state] == NSOnState )
+       if( [fAnamorphicPopUp indexOfSelectedItem] > 0 )
        {
-        [fWidthStepper      setIntValue: MaxOutputWidth];
-        [fWidthField        setIntValue: MaxOutputWidth];
-        
-        /* This will show correct anamorphic height values, but
-            show distorted preview picture ratio */
-        [fHeightStepper      setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
-        [fHeightField        setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
+        if ([fAnamorphicPopUp indexOfSelectedItem] == 2) // Loose anamorphic
+        {
+            job->pixel_ratio = 2;
+            [fWidthStepper setEnabled: YES];
+            [fWidthField setEnabled: YES];
+            /* We set job->width and call hb_set_anamorphic_size in libhb to do a "dry run" to get
+             * the values to be used by libhb for loose anamorphic
+             */
+            job->width       = [fWidthStepper  intValue];
+            hb_set_anamorphic_size(job, &output_width, &output_height, &output_par_width, &output_par_height);
+            [fHeightStepper      setIntValue: output_height];
+            [fHeightField        setIntValue: output_height];
+            job->height      = [fHeightStepper intValue];
+            
+        }
+        else // must be "1" or strict anamorphic
+        {
+            [fWidthStepper      setIntValue: fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3]];
+            [fWidthField        setIntValue: fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3]];
+            
+            /* This will show correct anamorphic height values, but
+             show distorted preview picture ratio */
+            [fHeightStepper      setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
+            [fHeightField        setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
+            job->width       = [fWidthStepper  intValue];
+            job->height      = [fHeightStepper intValue];
+            
+            job->pixel_ratio = 1;
+            [fWidthStepper setEnabled: NO];
+            [fWidthField setEnabled: NO];
+        }
         
-        /* This will show wrong anamorphic height values, but
-            show proper preview picture ratio */
-        //[fHeightStepper      setIntValue: MaxOutputHeight];
-        //[fHeightField        setIntValue: MaxOutputHeight];
+        /* if the sender is the Anamorphic checkbox, record the state
+         of KeepAspect Ratio so it can be reset if Anamorphic is unchecked again */
+        if (sender == fAnamorphicPopUp)
+        {
+            keepAspectRatioPreviousState = [fRatioCheck state];
+        }
         [fRatioCheck setState:NSOffState];
+        [fRatioCheck setEnabled: NO];
+        
         
-        [fWidthStepper setEnabled: NO];
-        [fWidthField setEnabled: NO];
         [fHeightStepper setEnabled: NO];
         [fHeightField setEnabled: NO];
-        [fRatioCheck setEnabled: NO];
+        
     }
-    
-       else
+    else
        {
+        job->width       = [fWidthStepper  intValue];
+        job->height      = [fHeightStepper intValue];
+        job->pixel_ratio = 0;
         [fWidthStepper setEnabled: YES];
         [fWidthField setEnabled: YES];
         [fHeightStepper setEnabled: YES];
         [fHeightField setEnabled: YES];
         [fRatioCheck setEnabled: YES];
+        /* if the sender is the Anamorphic checkbox, we return the
+         keep AR checkbox to its previous state */
+        if (sender == fAnamorphicPopUp)
+        {
+            [fRatioCheck setState:keepAspectRatioPreviousState];
+        }
+        
        }
        
-    job->width       = [fWidthStepper  intValue];
-    job->height      = [fHeightStepper intValue];
+    
     job->keep_ratio  = ( [fRatioCheck state] == NSOnState );
     
        fPictureFilterSettings.deinterlace = [fDeinterlacePopUp indexOfSelectedItem];
     /* if the gui deinterlace settings are fast through slowest, the job->deinterlace
-       value needs to be set to one, for the job as well as the previews showing deinterlacing
-       otherwise set job->deinterlace to 0 or "off" */
+     value needs to be set to one, for the job as well as the previews showing deinterlacing
+     otherwise set job->deinterlace to 0 or "off" */
     if (fPictureFilterSettings.deinterlace > 0)
     {
-    job->deinterlace  = 1;
+        job->deinterlace  = 1;
     }
     else
     {
-    job->deinterlace  = 0;
+        job->deinterlace  = 0;
     }
     fPictureFilterSettings.denoise     = [fDenoisePopUp indexOfSelectedItem];
+    fPictureFilterSettings.vfr  = [fVFRCheck state];
+    if (fPictureFilterSettings.vfr > 0)
+    {
+        [fDetelecineCheck setState:NSOnState];
+        [fDetelecineCheck setEnabled: NO];
+    }
+    else
+    {
+        [fDetelecineCheck setEnabled: YES];
+    }
     fPictureFilterSettings.detelecine  = [fDetelecineCheck state];
-       job->pixel_ratio = ( [fPARCheck state] == NSOnState );
-
+    fPictureFilterSettings.deblock  = [fDeblockCheck state];
+    
+    fPictureFilterSettings.decomb = [fDecombPopUp indexOfSelectedItem];
+       
     autoCrop = ( [fCropMatrix selectedRow] == 0 );
     [fCropTopStepper    setEnabled: !autoCrop];
     [fCropBottomStepper setEnabled: !autoCrop];
     [fCropLeftStepper   setEnabled: !autoCrop];
     [fCropRightStepper  setEnabled: !autoCrop];
-//     [fAutoCropMainWindow  setStringValue: [NSString stringWithFormat:@"%d",autocrop]];
+    
     if( autoCrop )
     {
         memcpy( job->crop, fTitle->crop, 4 * sizeof( int ) );
@@ -326,11 +341,11 @@ static int GetAlignedSize( int size )
         job->crop[2] = [fCropLeftStepper   intValue];
         job->crop[3] = [fCropRightStepper  intValue];
     }
-
+    
     if( job->keep_ratio )
     {
         if( sender == fWidthStepper || sender == fRatioCheck ||
-            sender == fCropTopStepper || sender == fCropBottomStepper )
+           sender == fCropTopStepper || sender == fCropBottomStepper )
         {
             hb_fix_aspect( job, HB_KEEP_WIDTH );
             if( job->height > fTitle->height )
@@ -352,8 +367,11 @@ static int GetAlignedSize( int size )
     
     [fWidthStepper      setIntValue: job->width];
     [fWidthField        setIntValue: job->width];
-    [fHeightStepper     setIntValue: job->height];
-    [fHeightField       setIntValue: job->height];
+    if( [fAnamorphicPopUp indexOfSelectedItem] < 2 )
+       {
+        [fHeightStepper     setIntValue: job->height];
+        [fHeightField       setIntValue: job->height];
+    }
     [fCropTopStepper    setIntValue: job->crop[0]];
     [fCropTopField      setIntValue: job->crop[0]];
     [fCropBottomStepper setIntValue: job->crop[1]];
@@ -363,12 +381,15 @@ static int GetAlignedSize( int size )
     [fCropRightStepper  setIntValue: job->crop[3]];
     [fCropRightField    setIntValue: job->crop[3]];
     /* Sanity Check Here for < 16 px preview to avoid
-       crashing hb_get_preview. In fact, just for kicks
-       lets getting previews at a min limit of 32, since
-       no human can see any meaningful detail below that */
+     crashing hb_get_preview. In fact, just for kicks
+     lets getting previews at a min limit of 32, since
+     no human can see any meaningful detail below that */
     if (job->width >= 64 && job->height >= 64)
     {
-        [self Display: HB_ANIMATE_NONE];
+        // Purge the existing picture previews so they get recreated the next time
+        // they are needed.
+        [self purgeImageCache];
+        [self displayPreview];
     }
 }
 
@@ -379,7 +400,7 @@ static int GetAlignedSize( int size )
         return;
     }
     fPicture--;
-    [self Display: HB_ANIMATE_BACKWARD];
+    [self displayPreview];
 }
 
 - (IBAction) NextPicture: (id) sender
@@ -389,16 +410,16 @@ static int GetAlignedSize( int size )
         return;
     }
     fPicture++;
-    [self Display: HB_ANIMATE_FORWARD];
+    [self displayPreview];
 }
 
 - (IBAction) ClosePanel: (id) sender
 {
     if ([delegate respondsToSelector:@selector(pictureSettingsDidChange)])
         [delegate pictureSettingsDidChange];
-        
-    [NSApp endSheet: fPicturePanel];
-    [fPicturePanel orderOut: self];
+
+    [NSApp endSheet:[self window]];
+    [[self window] orderOut:self];
 }
 
 - (BOOL) autoCrop
@@ -410,6 +431,16 @@ static int GetAlignedSize( int size )
     autoCrop = setting;
 }
 
+- (BOOL) allowLooseAnamorphic
+{
+    return allowLooseAnamorphic;
+}
+
+- (void) setAllowLooseAnamorphic: (BOOL) setting
+{
+    allowLooseAnamorphic = setting;
+}
+
 - (int) detelecine
 {
     return fPictureFilterSettings.detelecine;
@@ -420,6 +451,16 @@ static int GetAlignedSize( int size )
     fPictureFilterSettings.detelecine = setting;
 }
 
+- (int) vfr
+{
+    return fPictureFilterSettings.vfr;
+}
+
+- (void) setVFR: (int) setting
+{
+    fPictureFilterSettings.vfr = setting;
+}
+
 - (int) deinterlace
 {
     return fPictureFilterSettings.deinterlace;
@@ -428,7 +469,14 @@ static int GetAlignedSize( int size )
 - (void) setDeinterlace: (int) setting {
     fPictureFilterSettings.deinterlace = setting;
 }
+- (int) decomb
+{
+    return fPictureFilterSettings.decomb;
+}
 
+- (void) setDecomb: (int) setting {
+    fPictureFilterSettings.decomb = setting;
+}
 - (int) denoise
 {
     return fPictureFilterSettings.denoise;
@@ -439,26 +487,196 @@ static int GetAlignedSize( int size )
     fPictureFilterSettings.denoise = setting;
 }
 
+- (int) deblock
+{
+    return fPictureFilterSettings.deblock;
+}
+
+- (void) setDeblock: (int) setting
+{
+    fPictureFilterSettings.deblock = setting;
+}
+
 - (void)showPanelInWindow: (NSWindow *)fWindow forTitle: (hb_title_t *)title
 {
     [self SetTitle:title];
-    
-    [NSApp beginSheet:fPicturePanel
+
+    [NSApp beginSheet:[self window]
        modalForWindow:fWindow
         modalDelegate:nil
        didEndSelector:nil
           contextInfo:NULL];
 }
 
-- (BOOL) loadMyNibFile
+
+// This function converts an image created by libhb (specified via pictureIndex) into
+// an NSImage suitable for the GUI code to use. If removeBorders is YES,
+// makeImageForPicture crops the image generated by libhb stripping off the gray
+// border around the content. This is the low-level method that generates the image.
+// -imageForPicture calls this function whenever it can't find an image in its cache.
++ (NSImage *) makeImageForPicture: (int)pictureIndex
+                libhb:(hb_handle_t*)handle
+                title:(hb_title_t*)title
+                removeBorders:(BOOL)removeBorders
+{
+    if (removeBorders)
+    {
+        //     |<---------- title->width ----------->|
+        //     |   |<---- title->job->width ---->|   |
+        //     |   |                             |   |
+        //     .......................................
+        //     ....+-----------------------------+....
+        //     ....|                             |....<-- gray border
+        //     ....|                             |....
+        //     ....|                             |....
+        //     ....|                             |<------- image
+        //     ....|                             |....
+        //     ....|                             |....
+        //     ....|                             |....
+        //     ....|                             |....
+        //     ....|                             |....
+        //     ....+-----------------------------+....
+        //     .......................................
+
+        static uint8_t * buffer;
+        static int bufferSize;
+
+        // Make sure we have a big enough buffer to receive the image from libhb. libhb
+        // creates images with a one-pixel border around the original content. Hence we
+        // add 2 pixels horizontally and vertically to the buffer size.
+        int srcWidth = title->width + 2;
+        int srcHeight= title->height + 2;
+        int newSize;
+        newSize = srcWidth * srcHeight * 4;
+        if( bufferSize < newSize )
+        {
+            bufferSize = newSize;
+            buffer     = (uint8_t *) realloc( buffer, bufferSize );
+        }
+
+        hb_get_preview( handle, title, pictureIndex, buffer );
+
+        // Create an NSBitmapImageRep and copy the libhb image into it, converting it from
+        // libhb's format to one suitable for NSImage. Along the way, we'll strip off the
+        // border around libhb's image.
+        
+        // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
+        // Alpha is ignored.
+        
+        int dstWidth = title->job->width;
+        int dstHeight = title->job->height;
+        NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
+        NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
+                initWithBitmapDataPlanes:nil
+                pixelsWide:dstWidth
+                pixelsHigh:dstHeight
+                bitsPerSample:8
+                samplesPerPixel:3   // ignore alpha
+                hasAlpha:NO
+                isPlanar:NO
+                colorSpaceName:NSCalibratedRGBColorSpace
+                bitmapFormat:bitmapFormat
+                bytesPerRow:dstWidth * 4
+                bitsPerPixel:32] autorelease];
+
+        int borderTop = (srcHeight - dstHeight) / 2;
+        int borderLeft = (srcWidth - dstWidth) / 2;
+        
+        UInt32 * src = (UInt32 *)buffer;
+        UInt32 * dst = (UInt32 *)[imgrep bitmapData];
+        src += borderTop * srcWidth;    // skip top rows in src to get to first row of dst
+        src += borderLeft;              // skip left pixels in src to get to first pixel of dst
+        for (int r = 0; r < dstHeight; r++)
+        {
+            for (int c = 0; c < dstWidth; c++)
+#if TARGET_RT_LITTLE_ENDIAN
+                *dst++ = Endian32_Swap(*src++);
+#else
+                *dst++ = *src++;
+#endif
+            src += (srcWidth - dstWidth);   // skip to next row in src
+        }
+
+        NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(dstWidth, dstHeight)] autorelease];
+        [img addRepresentation:imgrep];
+
+        return img;
+    }
+    else
+    {
+        // Make sure we have big enough buffer
+        static uint8_t * buffer;
+        static int bufferSize;
+
+        int newSize;
+        newSize = ( title->width + 2 ) * (title->height + 2 ) * 4;
+        if( bufferSize < newSize )
+        {
+            bufferSize = newSize;
+            buffer     = (uint8_t *) realloc( buffer, bufferSize );
+        }
+
+        hb_get_preview( handle, title, pictureIndex, buffer );
+
+        // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
+        // We'll copy that into an NSImage swapping it to ARGB in the process. Alpha is
+        // ignored.
+        int width = title->width + 2;      // hblib adds a one-pixel border to the image
+        int height = title->height + 2;
+        int numPixels = width * height;
+        NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
+        NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
+                initWithBitmapDataPlanes:nil
+                pixelsWide:width
+                pixelsHigh:height
+                bitsPerSample:8
+                samplesPerPixel:3   // ignore alpha
+                hasAlpha:NO
+                isPlanar:NO
+                colorSpaceName:NSCalibratedRGBColorSpace
+                bitmapFormat:bitmapFormat
+                bytesPerRow:width * 4
+                bitsPerPixel:32] autorelease];
+
+        UInt32 * src = (UInt32 *)buffer;
+        UInt32 * dst = (UInt32 *)[imgrep bitmapData];
+        for (int i = 0; i < numPixels; i++)
+#if TARGET_RT_LITTLE_ENDIAN
+            *dst++ = Endian32_Swap(*src++);
+#else
+            *dst++ = *src++;
+#endif
+
+        NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(width, height)] autorelease];
+        [img addRepresentation:imgrep];
+
+        return img;
+    }
+}
+
+// Returns the preview image for the specified index, retrieving it from its internal
+// cache or by calling makeImageForPicture if it is not cached. Generally, you should
+// use imageForPicture so that images are cached. Calling makeImageForPicture will
+// always generate a new copy of the image.
+- (NSImage *) imageForPicture: (int) pictureIndex
 {
-    if(![NSBundle loadNibNamed:@"PictureSettings" owner:self])
+    // The preview for the specified index may not currently exist, so this method
+    // generates it if necessary.
+    NSString * key = [NSString stringWithFormat:@"%d", pictureIndex];
+    NSImage * theImage = [fPicturePreviews objectForKey:key];
+    if (!theImage)
     {
-        NSLog(@"Warning! Could not load myNib file.\n");
-        return NO;
+        theImage = [PictureController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle removeBorders: NO];
+        [fPicturePreviews setObject:theImage forKey:key];
     }
-    
-    return YES;
+    return theImage;
+}
+
+// Purges all images from the cache. The next call to imageForPicture will cause a new
+// image to be generated.
+- (void) purgeImageCache
+{
+    [fPicturePreviews removeAllObjects];
 }
 
 @end
@@ -476,11 +694,11 @@ static int GetAlignedSize( int size )
     // The min size is 320x240
     float minWidth = 320.0;
     float minHeight = 240.0;
-    
+
     // The max size of the view is when the sheet is taking up 85% of the screen.
     NSSize screenSize = [[NSScreen mainScreen] frame].size;
-    NSSize sheetSize = [fPicturePanel frame].size;
-    NSSize viewAreaSize = [fPictureGLViewArea frame].size;
+    NSSize sheetSize = [[self window] frame].size;
+    NSSize viewAreaSize = [fPictureViewArea frame].size;
     float paddingX = sheetSize.width - viewAreaSize.width;
     float paddingY = sheetSize.height - viewAreaSize.height;
     float maxWidth = (0.85 * screenSize.width) - paddingX;
@@ -522,14 +740,14 @@ static int GetAlignedSize( int size )
 - (void)resizeSheetForViewSize: (NSSize)viewSize
 {
     // Figure out the deltas for the new frame area
-    NSSize currentSize = [fPictureGLViewArea frame].size;
+    NSSize currentSize = [fPictureViewArea frame].size;
     float deltaX = viewSize.width - currentSize.width;
     float deltaY = viewSize.height - currentSize.height;
-    
+
     // Now resize the whole panel by those same deltas, but don't exceed the min
-    NSRect frame = [fPicturePanel frame];
-    NSSize maxSize = [fPicturePanel maxSize];
-    NSSize minSize = [fPicturePanel minSize];
+    NSRect frame = [[self window] frame];
+    NSSize maxSize = [[self window] maxSize];
+    NSSize minSize = [[self window] minSize];
     frame.size.width += deltaX;
     frame.size.height += deltaY;
     if( frame.size.width < minSize.width )
@@ -546,7 +764,7 @@ static int GetAlignedSize( int size )
     frame.origin.x -= (deltaX / 2.0);
     frame.origin.y -= deltaY;
 
-    [fPicturePanel setFrame:frame display:YES animate:YES];
+    [[self window] setFrame:frame display:YES animate:YES];
 }
 
 //
@@ -557,13 +775,13 @@ static int GetAlignedSize( int size )
 //
 - (void)setViewSize: (NSSize)viewSize
 {
-    [fPictureGLView setFrameSize:viewSize];
+    [fPictureView setFrameSize:viewSize];
     
     // center it vertically
-    NSPoint origin = [fPictureGLViewArea frame].origin;
-    origin.y += ([fPictureGLViewArea frame].size.height -
-                 [fPictureGLView frame].size.height) / 2.0;
-    [fPictureGLView setFrameOrigin:origin];
+    NSPoint origin = [fPictureViewArea frame].origin;
+    origin.y += ([fPictureViewArea frame].size.height -
+                 [fPictureView frame].size.height) / 2.0;
+    [fPictureView setFrameOrigin:origin];
 }
 
 //
@@ -573,7 +791,7 @@ static int GetAlignedSize( int size )
 //
 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize
 {
-    NSSize viewSize = [fPictureGLView frame].size;
+    NSSize viewSize = [fPictureView frame].size;
     return (newSize.width != viewSize.width || newSize.height != viewSize.height);
 }