Skip to content

Commit

Permalink
MacGui: use new api hb_get_preview3 for previews.
Browse files Browse the repository at this point in the history
  • Loading branch information
galad87 committed Apr 13, 2021
1 parent dc4afd8 commit 6fec869
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 53 deletions.
9 changes: 2 additions & 7 deletions macosx/HBCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,11 @@ typedef void (^HBCoreCompletionHandler)(HBCoreResult result);
* into an CGImage.
*
* @param index the index of the desired image.
* @param title Handle to hb_title_t of desired title
* @param frame a HBPicture instance that describe the image's frame.
* @param deinterlace whether the preview image must be deinterlaced or not.
* @param job a HBJob instance.
*
* @return a CGImageRef of the wanted image, NULL if the index is out of bounds.
*/
- (nullable CGImageRef)copyImageAtIndex:(NSUInteger)index
forTitle:(HBTitle *)title
pictureFrame:(HBPicture *)frame
deinterlace:(BOOL)deinterlace CF_RETURNS_RETAINED;
- (nullable CGImageRef)copyImageAtIndex:(NSUInteger)index job:(HBJob *)job CF_RETURNS_RETAINED;

/**
* Returns the counts of the available previews images.
Expand Down
41 changes: 12 additions & 29 deletions macosx/HBCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,19 @@ - (void)cancelScan

#pragma mark - Preview images

- (CGImageRef)copyImageAtIndex:(NSUInteger)index
forTitle:(HBTitle *)title
pictureFrame:(HBPicture *)frame
deinterlace:(BOOL)deinterlace CF_RETURNS_RETAINED
- (nullable CGImageRef)copyImageAtIndex:(NSUInteger)index job:(HBJob *)job CF_RETURNS_RETAINED
{
CGImageRef img = NULL;

hb_geometry_settings_t geo;
memset(&geo, 0, sizeof(geo));
geo.geometry.width = frame.displayWidth;
geo.geometry.height = frame.height;
// ignore the par.
geo.geometry.par.num = 1;
geo.geometry.par.den = 1;
int crop[4] = {frame.cropTop, frame.cropBottom, frame.cropLeft, frame.cropRight};
memcpy(geo.crop, crop, sizeof(int[4]));

hb_image_t *image = hb_get_preview2(_hb_handle, title.index, (int)index, &geo, deinterlace);
hb_job_t *hb_job = [job hb_job];
hb_dict_t *job_dict = hb_job_to_dict(hb_job);
hb_job_close(&hb_job);
hb_image_t *image = hb_get_preview3(_hb_handle, (int)index, job_dict);

if (image)
{
// Create an CGImageRef and copy the libhb image into it.
// The image data returned by hb_get_preview2 is 4 bytes per pixel, BGRA format.
// The image data returned by hb_get_preview3 is 4 bytes per pixel, BGRA format.
// Alpha is ignored.
CFMutableDataRef imgData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CFDataSetLength(imgData, 3 * image->width * image->height);
Expand All @@ -376,9 +366,9 @@ - (CGImageRef)copyImageAtIndex:(NSUInteger)index
CGDataProviderRef provider = CGDataProviderCreateWithCFData(imgData);

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
CGColorSpaceRef colorSpace = copyColorSpace(title.hb_title->color_prim,
title.hb_title->color_transfer,
title.hb_title->color_matrix);
CGColorSpaceRef colorSpace = copyColorSpace(image->color_prim,
image->color_transfer,
image->color_matrix);

img = CGImageCreate(image->width,
image->height,
Expand All @@ -399,16 +389,9 @@ - (CGImageRef)copyImageAtIndex:(NSUInteger)index
hb_image_close(&image);
}

if (img && (frame.rotate || frame.flip))
{
CGImageRef rotatedImg = CGImageRotated(img, frame.rotate, frame.flip);
CGImageRelease(img);
return rotatedImg;
}
else
{
return img;
}
hb_value_free(&job_dict);

return img;
}

- (NSUInteger)imagesCountForTitle:(HBTitle *)title
Expand Down
14 changes: 14 additions & 0 deletions macosx/HBPicture.m
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,20 @@ - (void)setPaddingColorMode:(HBPicturePaddingColorMode)paddingColorMode
}
}

- (void)setPaddingColorCustom:(NSString *)paddingColorCustom
{
if (paddingColorCustom != _paddingColorCustom)
{
[[self.undo prepareWithInvocationTarget:self] setPaddingColorCustom:_paddingColorCustom];
}
_paddingColorCustom = paddingColorCustom;

if (!self.isValidating)
{
[self validateSettings];
}
}

#pragma mark - Anamorphic

- (void)setAnamorphicMode:(HBPictureAnarmophicMode)anamorphicMode
Expand Down
7 changes: 5 additions & 2 deletions macosx/HBPreviewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,11 @@ - (void)displayPreviewAtIndex:(NSUInteger)idx
if (self.generator && self.window.isVisible)
{
CGImageRef image = [self.generator copyImageAtIndex:idx shouldCache:YES];
self.previewView.image = image;
CFRelease(image);
if (image)
{
self.previewView.image = image;
CFRelease(image);
}
}
}

Expand Down
15 changes: 3 additions & 12 deletions macosx/HBPreviewGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ - (void)dealloc
*
* @param index picture index in title.
*/
- (CGImageRef) copyImageAtIndex: (NSUInteger) index shouldCache: (BOOL) cache
- (nullable CGImageRef) copyImageAtIndex: (NSUInteger) index shouldCache: (BOOL) cache
{
if (index >= self.imagesCount)
{
Expand All @@ -92,13 +92,7 @@ - (CGImageRef) copyImageAtIndex: (NSUInteger) index shouldCache: (BOOL) cache

if (!theImage)
{
HBFilters *filters = self.job.filters;
BOOL deinterlace = (![filters.deinterlace isEqualToString:@"off"]);

theImage = (CGImageRef)[self.scanCore copyImageAtIndex:index
forTitle:self.job.title
pictureFrame:self.job.picture
deinterlace:deinterlace];
theImage = [self.scanCore copyImageAtIndex:index job:self.job];
if (cache && theImage)
{
// The cost is the number of pixels of the image
Expand Down Expand Up @@ -188,10 +182,7 @@ - (void)copySmallImageAtIndex:(NSUInteger)index completionHandler:(void (^)(__nu

if (image == NULL)
{
image = (CGImageRef)[self.scanCore copyImageAtIndex:index
forTitle:self.job.title
pictureFrame:self.job.picture
deinterlace:NO];
image = [self.scanCore copyImageAtIndex:index job:self.job];
CFAutorelease(image);
}

Expand Down
9 changes: 6 additions & 3 deletions macosx/HBPreviewViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,12 @@ - (void)updatePicture
if (self.generator && self.visible)
{
CGImageRef fPreviewImage = [self.generator copyImageAtIndex:self.selectedIndex shouldCache:YES];
self.previewView.image = fPreviewImage;
CFRelease(fPreviewImage);
self.previewView.layer.opacity = 1;
if (fPreviewImage)
{
self.previewView.image = fPreviewImage;
CFRelease(fPreviewImage);
self.previewView.layer.opacity = 1;
}
}
else
{
Expand Down

0 comments on commit 6fec869

Please sign in to comment.