diff options
author | dynaflash <[email protected]> | 2008-01-12 22:45:15 +0000 |
---|---|---|
committer | dynaflash <[email protected]> | 2008-01-12 22:45:15 +0000 |
commit | adf21e86d7831dfc99cbb8248f3a9b6e8c08d83d (patch) | |
tree | 1ca117033ba43f1b9278cb735b54fcf7471ad7cb /macosx/PictureController.mm | |
parent | 0577ab41b7759e760df1058ed87a75605fcb1d85 (diff) |
MacGui: Switch PicturePreview to use NSImageView instead of the openGL view.
- thanks travistex
- uses a new method makeImageForPicture which gets the previews into an NS image.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1189 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'macosx/PictureController.mm')
-rw-r--r-- | macosx/PictureController.mm | 279 |
1 files changed, 190 insertions, 89 deletions
diff --git a/macosx/PictureController.mm b/macosx/PictureController.mm index 132d3b151..fc356c7c5 100644 --- a/macosx/PictureController.mm +++ b/macosx/PictureController.mm @@ -15,16 +15,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 +23,21 @@ static int GetAlignedSize( int size ) { 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 +61,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]; @@ -173,46 +141,13 @@ are maintained across different sources */ [fDenoisePopUp selectItemAtIndex: fPictureFilterSettings.denoise]; } -- (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 ) - { - /* 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 )]; - + [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 @@ -434,7 +369,10 @@ are maintained across different sources */ 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]; } } @@ -445,7 +383,7 @@ are maintained across different sources */ return; } fPicture--; - [self Display: HB_ANIMATE_BACKWARD]; + [self displayPreview]; } - (IBAction) NextPicture: (id) sender @@ -455,7 +393,7 @@ are maintained across different sources */ return; } fPicture++; - [self Display: HB_ANIMATE_FORWARD]; + [self displayPreview]; } - (IBAction) ClosePanel: (id) sender @@ -546,6 +484,169 @@ are maintained across different sources */ contextInfo:NULL]; } + +// 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++) + *dst++ = Endian32_Swap(*src++); + 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++) + *dst++ = Endian32_Swap(*src++); + + 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]; +} + - (BOOL) loadMyNibFile { if(![NSBundle loadNibNamed:@"PictureSettings" owner:self]) @@ -576,7 +677,7 @@ are maintained across different sources */ // 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; @@ -618,7 +719,7 @@ are maintained across different sources */ - (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; @@ -653,13 +754,13 @@ are maintained across different sources */ // - (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]; } // @@ -669,7 +770,7 @@ are maintained across different sources */ // - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize { - NSSize viewSize = [fPictureGLView frame].size; + NSSize viewSize = [fPictureView frame].size; return (newSize.width != viewSize.width || newSize.height != viewSize.height); } |