]> granicus.if.org Git - handbrake/commitdiff
MacGui: update the preview window to reflect the rotate filter state.
authorDamiano Galassi <damiog@gmail.com>
Fri, 15 Jan 2016 08:51:23 +0000 (09:51 +0100)
committerDamiano Galassi <damiog@gmail.com>
Fri, 15 Jan 2016 08:51:23 +0000 (09:51 +0100)
macosx/HBCore.h
macosx/HBCore.m
macosx/HBPreviewGenerator.m

index 2c1830f5be6a186974305835a602055fef470297..6a50142caadcaa8fd9ea46d752e77a532a6af9dc 100644 (file)
@@ -144,9 +144,11 @@ typedef void (^HBCoreCompletionHandler)(HBCoreResult result);
  *  @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;
+                      forTitle:(HBTitle *)title
+                  pictureFrame:(HBPicture *)frame
+                   deinterlace:(BOOL)deinterlace
+                        rotate:(int)angle
+                       flipped:(BOOL)flipped CF_RETURNS_RETAINED;
 
 /**
  *  Returns the counts of the available previews images.
index cdb831fb3e7fe62432e9d4b653f7c161a060c356..829fcfdce0f3463585b6ea4e2652d019d5e32ff1 100644 (file)
@@ -279,10 +279,62 @@ static void hb_error_handler(const char *errmsg)
 
 #pragma mark - Preview images
 
+- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle flipped:(BOOL)flipped CF_RETURNS_RETAINED
+{
+    CGFloat angleInRadians = angle * (M_PI / 180);
+    CGFloat width = CGImageGetWidth(imgRef);
+    CGFloat height = CGImageGetHeight(imgRef);
+
+    CGRect imgRect = CGRectMake(0, 0, width, height);
+    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
+    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
+
+    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+    CGContextRef bmContext = CGBitmapContextCreate(NULL,
+                                                   (size_t)rotatedRect.size.width,
+                                                   (size_t)rotatedRect.size.height,
+                                                   8,
+                                                   0,
+                                                   colorSpace,
+                                                   kCGImageAlphaPremultipliedFirst);
+    CGContextSetAllowsAntialiasing(bmContext, FALSE);
+    CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
+    CGColorSpaceRelease(colorSpace);
+
+    // Rotate
+    CGContextTranslateCTM(bmContext,
+                          + (rotatedRect.size.width / 2),
+                          + (rotatedRect.size.height / 2));
+    CGContextRotateCTM(bmContext, -angleInRadians);
+    CGContextTranslateCTM(bmContext,
+                          - (rotatedRect.size.width / 2),
+                          - (rotatedRect.size.height / 2));
+
+    // Flip
+    if (flipped) {
+        CGAffineTransform flipHorizontal = CGAffineTransformMake(-1, 0, 0, 1, floor(rotatedRect.size.width), 0);
+        CGContextConcatCTM(bmContext, flipHorizontal);
+    }
+
+    CGContextDrawImage(bmContext,
+                       CGRectMake((rotatedRect.size.width - width)/2.0f,
+                                  (rotatedRect.size.height - height)/2.0f,
+                                  width,
+                                  height),
+                       imgRef);
+
+    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
+    CFRelease(bmContext);
+
+    return rotatedImage;
+}
+
 - (CGImageRef)copyImageAtIndex:(NSUInteger)index
                       forTitle:(HBTitle *)title
                   pictureFrame:(HBPicture *)frame
                    deinterlace:(BOOL)deinterlace
+                        rotate:(int)angle
+                       flipped:(BOOL)flipped CF_RETURNS_RETAINED
 {
     CGImageRef img = NULL;
 
@@ -339,8 +391,17 @@ static void hb_error_handler(const char *errmsg)
 
         hb_image_close(&image);
     }
-    
-    return img;
+
+    if (angle || flipped)
+    {
+        CGImageRef rotatedImg = [self CGImageRotatedByAngle:img angle:angle flipped:flipped];
+        CGImageRelease(img);
+        return rotatedImg;
+    }
+    else
+    {
+        return img;
+    }
 }
 
 - (NSUInteger)imagesCountForTitle:(HBTitle *)title
index d85f102e33e77f27d3cb4fed5176fca853113c6e..06f4423170b1638f6f2a51fd160f561a8066855c 100644 (file)
@@ -81,7 +81,9 @@
         theImage = (CGImageRef)[self.scanCore copyImageAtIndex:index
                                                            forTitle:self.job.title
                                                        pictureFrame:self.job.picture
-                                                        deinterlace:deinterlace];
+                                                        deinterlace:deinterlace
+                                                        rotate:self.job.filters.rotate
+                                                       flipped:self.job.filters.flip];
         if (cache && theImage)
         {
             // The cost is the number of pixels of the image