+- (int) deinterlace
+{
+ return fPictureFilterSettings.deinterlace;
+}
+
+- (void) setDeinterlace: (int) setting {
+ fPictureFilterSettings.deinterlace = setting;
+}
+- (int) decomb
+{
+ return fPictureFilterSettings.decomb;
+}
+
+- (void) setDecomb: (int) setting {
+ fPictureFilterSettings.decomb = setting;
+}
+- (int) denoise
+{
+ return fPictureFilterSettings.denoise;
+}
+
+- (void) setDenoise: (int) setting
+{
+ fPictureFilterSettings.denoise = setting;
+}
+
+- (int) deblock
+{
+ return fPictureFilterSettings.deblock;
+}
+
+- (void) setDeblock: (int) setting
+{
+ fPictureFilterSettings.deblock = setting;
+}
+
+- (IBAction)showPreviewPanel: (id)sender forTitle: (hb_title_t *)title
+{
+ [self SetTitle:title];
+ [self showWindow:sender];
+
+}
+
+
+// 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
+{
+ // 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)
+ {
+ theImage = [PictureController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle removeBorders: NO];
+ [fPicturePreviews setObject:theImage forKey:key];
+ }
+ 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
+
+@implementation PictureController (Private)
+
+//
+// -[PictureController(Private) optimalViewSizeForImageSize:]
+//
+// Given the size of the preview image to be shown, returns the best possible
+// size for the view.
+//
+- (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize
+{
+ // The min size is 320x240
+ CGFloat minWidth = 320.0;
+ CGFloat minHeight = 240.0;
+
+ NSSize screenSize = [[NSScreen mainScreen] frame].size;
+ NSSize sheetSize = [[self window] frame].size;
+ NSSize viewAreaSize = [fPictureViewArea frame].size;
+ CGFloat paddingX = sheetSize.width - viewAreaSize.width;
+ CGFloat paddingY = sheetSize.height - viewAreaSize.height;
+ /* Since we are now non-modal, lets go ahead and allow the mac size to
+ * go up to the full screen height or width below. Am leaving the original
+ * code here that blindjimmy setup for 85% in case we don't like it.
+ */
+ // The max size of the view is when the sheet is taking up 85% of the screen.
+ //CGFloat maxWidth = (0.85 * screenSize.width) - paddingX;
+ //CGFloat maxHeight = (0.85 * screenSize.height) - paddingY;
+ CGFloat maxWidth = screenSize.width - paddingX;
+ CGFloat maxHeight = screenSize.height - paddingY;
+
+ NSSize resultSize = imageSize;
+
+ // Its better to have a view that's too small than a view that's too big, so
+ // apply the maximum constraints last.
+ if( resultSize.width < minWidth )
+ {
+ resultSize.height *= (minWidth / resultSize.width);
+ resultSize.width = minWidth;
+ }
+ if( resultSize.height < minHeight )
+ {
+ resultSize.width *= (minHeight / resultSize.height);
+ resultSize.height = minHeight;
+ }
+ if( resultSize.width > maxWidth )
+ {
+ resultSize.height *= (maxWidth / resultSize.width);
+ resultSize.width = maxWidth;
+ }
+ if( resultSize.height > maxHeight )
+ {
+ resultSize.width *= (maxHeight / resultSize.height);
+ resultSize.height = maxHeight;
+ }
+
+ return resultSize;
+}
+
+//
+// -[PictureController(Private) resizePanelForViewSize:animate:]
+//
+// Resizes the entire sheet to accomodate a view of a particular size.
+//
+- (void)resizeSheetForViewSize: (NSSize)viewSize
+{
+ // Figure out the deltas for the new frame area
+ NSSize currentSize = [fPictureViewArea frame].size;
+ CGFloat deltaX = viewSize.width - currentSize.width;
+ CGFloat deltaY = viewSize.height - currentSize.height;
+
+ // Now resize the whole panel by those same deltas, but don't exceed the min
+ 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 )
+ {
+ frame.size.width = minSize.width;
+ }
+ if( frame.size.height < minSize.height )
+ {
+ frame.size.height = minSize.height;
+ }
+
+ // But now the sheet is off-center, so also shift the origin to center it and
+ // keep the top aligned.
+ if( frame.size.width != [[self window] frame].size.width )
+ frame.origin.x -= (deltaX / 2.0);
+
+ if( frame.size.height != [[self window] frame].size.height )
+ frame.origin.y -= deltaY;
+
+ [[self window] setFrame:frame display:YES animate:YES];
+}
+
+//
+// -[PictureController(Private) setViewSize:]
+//
+// Changes the view's size and centers it vertically inside of its area.
+// Assumes resizeSheetForViewSize: has already been called.
+//
+- (void)setViewSize: (NSSize)viewSize
+{
+ [fPictureView setFrameSize:viewSize];
+
+ // center it vertically
+ NSPoint origin = [fPictureViewArea frame].origin;
+ origin.y += ([fPictureViewArea frame].size.height -
+ [fPictureView frame].size.height) / 2.0;
+ [fPictureView setFrameOrigin:origin];
+}
+
+//
+// -[PictureController(Private) viewNeedsToResizeToSize:]
+//
+// Returns YES if the view will need to resize to match the given size.
+//
+- (BOOL)viewNeedsToResizeToSize: (NSSize)newSize
+{
+ NSSize viewSize = [fPictureView frame].size;
+ return (newSize.width != viewSize.width || newSize.height != viewSize.height);