Index: macosx/PictureController.mm =================================================================== --- macosx/PictureController.mm (revision 1076) +++ macosx/PictureController.mm (working copy) @@ -6,6 +6,9 @@ #include "PictureController.h" +// hbilb generates 10 preview pictures +#define HB_NUM_HBLIB_PICTURES 10 + @interface PictureController (Private) - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize; @@ -15,16 +18,6 @@ @end -static int GetAlignedSize( int size ) -{ - int result = 1; - while( result < size ) - { - result *= 2; - } - return result; -} - @implementation PictureController - (id)initWithDelegate:(id)del @@ -33,22 +26,21 @@ { delegate = del; [self loadMyNibFile]; + fPicturePreviews = [[NSMutableArray arrayWithCapacity: 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 +64,6 @@ 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]; @@ -156,46 +127,8 @@ [self SettingsChanged: nil]; } -- (void) Display: (int) anim +- (void) adjustViewForSettings { - hb_get_preview( fHandle, fTitle, fPicture, fBuffer ); - - /* Backup previous picture (for effects) */ - memcpy( fTexBuf[1], fTexBuf[0], fTexBufSize ); - - if( fHasQE ) - { - /* Simply copy */ - memcpy( fTexBuf[0], fBuffer, fTexBufSize ); - } - else - { - /* Copy line by line */ - uint8_t * in = fBuffer; - uint8_t * out = fTexBuf[0]; - - for( int i = fTitle->height + 2; i--; ) - { - memcpy( out, in, 4 * ( fTitle->width + 2 ) ); - in += 4 * ( fTitle->width + 2 ); - out += 4 * GetAlignedSize( fTitle->width + 2 ); - } - - } - - 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; @@ -238,11 +171,28 @@ [fInfoField setStringValue: [[fInfoField stringValue] stringByAppendingString:scaleString]]; } +} +// Removes all previous picture previews and creates new ones. +- (void) createPicturePreviews +{ + [fPicturePreviews removeAllObjects]; + for (int i = 0; i < HB_NUM_HBLIB_PICTURES; i++) + [fPicturePreviews addObject:[self makeImageForPicture: i]]; +} + +// Sets the preview picture displayed to the libhb preview identified by index. +- (void) setPreviewPicture: (int)pictureIndex +{ + fPicture = pictureIndex; + [fPictureView setImage: [fPicturePreviews objectAtIndex: fPicture]]; + [fPrevButton setEnabled: ( fPicture > 0 )]; - [fNextButton setEnabled: ( fPicture < 9 )]; + [fNextButton setEnabled: ( fPicture < (HB_NUM_HBLIB_PICTURES - 1) )]; } +#pragma mark - + - (IBAction) SettingsChanged: (id) sender { hb_job_t * job = fTitle->job; @@ -372,13 +322,16 @@ [fCropLeftField setIntValue: job->crop[2]]; [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 */ if (job->width >= 64 && job->height >= 64) { - [self Display: HB_ANIMATE_NONE]; + [self createPicturePreviews]; + [self setPreviewPicture: fPicture]; + [self adjustViewForSettings]; } } @@ -388,18 +341,16 @@ { return; } - fPicture--; - [self Display: HB_ANIMATE_BACKWARD]; + [self setPreviewPicture: (fPicture - 1)]; } - (IBAction) NextPicture: (id) sender { - if( fPicture >= 9 ) + if( fPicture >= (HB_NUM_HBLIB_PICTURES - 1) ) { return; } - fPicture++; - [self Display: HB_ANIMATE_FORWARD]; + [self setPreviewPicture: (fPicture + 1)]; } - (IBAction) ClosePanel: (id) sender @@ -411,6 +362,8 @@ [fPicturePanel orderOut: self]; } +#pragma mark - + - (BOOL) autoCrop { return autoCrop; @@ -469,6 +422,8 @@ fPictureFilterSettings.deblock = setting; } +#pragma mark - + - (void)showPanelInWindow: (NSWindow *)fWindow forTitle: (hb_title_t *)title { [self SetTitle:title]; @@ -484,13 +439,67 @@ { if(![NSBundle loadNibNamed:@"PictureSettings" owner:self]) { - NSLog(@"Warning! Could not load myNib file.\n"); + NSLog(@"Warning! Could not load PictureSettings nib."); return NO; } return YES; } +#pragma mark - + ++ (NSImage *) makeImageForPicture: (int)pictureIndex libhb:(hb_handle_t*)handle title:(hb_title_t*)title +{ + // 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++) + *dst++ = Endian32_Swap(*src++); + + NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(width, height)] autorelease]; + [img addRepresentation:imgrep]; + + return img; +} + +- (NSImage *) makeImageForPicture: (int) pictureIndex +{ + return [PictureController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle]; +} + @end @implementation PictureController (Private) @@ -510,7 +519,7 @@ // 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 viewAreaSize = [fPictureViewArea frame].size; float paddingX = sheetSize.width - viewAreaSize.width; float paddingY = sheetSize.height - viewAreaSize.height; float maxWidth = (0.85 * screenSize.width) - paddingX; @@ -552,7 +561,7 @@ - (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; @@ -587,13 +596,13 @@ // - (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]; } // @@ -603,7 +612,7 @@ // - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize { - NSSize viewSize = [fPictureGLView frame].size; + NSSize viewSize = [fPictureView frame].size; return (newSize.width != viewSize.width || newSize.height != viewSize.height); } Index: macosx/PictureController.h =================================================================== --- macosx/PictureController.h (revision 1076) +++ macosx/PictureController.h (working copy) @@ -7,24 +7,19 @@ #include #include "hb.h" -#include "PictureGLView.h" @interface PictureController : NSObject { hb_handle_t * fHandle; hb_title_t * fTitle; - bool fHasQE; - uint8_t * fBuffer; - int fBufferSize; - uint8_t * fTexBuf[2]; - int fTexBufSize; int fPicture; + NSMutableArray * fPicturePreviews; // NSImages, one for each preview libhb creates IBOutlet NSPanel * fPicturePanel; - IBOutlet HBPictureGLView * fPictureGLView; - IBOutlet NSBox * fPictureGLViewArea; + IBOutlet NSImageView * fPictureView; + IBOutlet NSBox * fPictureViewArea; IBOutlet NSTextField * fWidthField; IBOutlet NSStepper * fWidthStepper; IBOutlet NSTextField * fHeightField; @@ -45,7 +40,6 @@ IBOutlet NSButton * fDeblockCheck; IBOutlet NSPopUpButton * fDenoisePopUp; IBOutlet NSButton * fPARCheck; - IBOutlet NSButton * fEffectsCheck; IBOutlet NSButton * fPrevButton; IBOutlet NSButton * fNextButton; IBOutlet NSTextField * fInfoField; @@ -74,7 +68,7 @@ - (void) SetHandle: (hb_handle_t *) handle; - (void) SetTitle: (hb_title_t *) title; -- (void) Display: (int) anim; +- (void) adjustViewForSettings; - (IBAction) SettingsChanged: (id) sender; - (IBAction) PreviousPicture: (id) sender; @@ -97,6 +91,9 @@ - (void)showPanelInWindow: (NSWindow *)fWindow forTitle: (hb_title_t *)title; - (BOOL) loadMyNibFile; + +- (NSImage *) makeImageForPicture: (int) pictureIndex; ++ (NSImage *) makeImageForPicture: (int) pictureIndex libhb: (hb_handle_t*) handle title: (hb_title_t*) title; @end @interface NSObject (PictureControllertDelegateMethod)