diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-01-21 03:23:02 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-01-21 03:23:02 -0500 |
| commit | faa9127a2081f40433b54795422dad10bb1bba9c (patch) | |
| tree | d4bad2416459fedfb8f9e8dfd929d72a7466e001 | |
| parent | d678059d1385e395aa87f667b8a0d9235b063bc6 (diff) | |
support photo orientation
| -rw-r--r-- | Emby.Drawing/GDI/GDIImageEncoder.cs | 2 | ||||
| -rw-r--r-- | Emby.Drawing/IImageEncoder.cs | 3 | ||||
| -rw-r--r-- | Emby.Drawing/ImageMagick/ImageMagickEncoder.cs | 20 | ||||
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 31 | ||||
| -rw-r--r-- | Emby.Drawing/NullImageEncoder.cs | 2 | ||||
| -rw-r--r-- | MediaBrowser.Server.Implementations/Dto/DtoService.cs | 26 |
6 files changed, 76 insertions, 8 deletions
diff --git a/Emby.Drawing/GDI/GDIImageEncoder.cs b/Emby.Drawing/GDI/GDIImageEncoder.cs index 3df84cf11..51b4605c3 100644 --- a/Emby.Drawing/GDI/GDIImageEncoder.cs +++ b/Emby.Drawing/GDI/GDIImageEncoder.cs @@ -89,7 +89,7 @@ namespace Emby.Drawing.GDI } } - public void EncodeImage(string inputPath, string cacheFilePath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) + public void EncodeImage(string inputPath, string cacheFilePath, int rotationAngle, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0; diff --git a/Emby.Drawing/IImageEncoder.cs b/Emby.Drawing/IImageEncoder.cs index bfd382bb7..f36498be7 100644 --- a/Emby.Drawing/IImageEncoder.cs +++ b/Emby.Drawing/IImageEncoder.cs @@ -27,12 +27,13 @@ namespace Emby.Drawing /// </summary> /// <param name="inputPath">The input path.</param> /// <param name="outputPath">The output path.</param> + /// <param name="rotationAngle">The rotation angle.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="quality">The quality.</param> /// <param name="options">The options.</param> /// <param name="outputFormat">The output format.</param> - void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat outputFormat); + void EncodeImage(string inputPath, string outputPath, int rotationAngle, int width, int height, int quality, ImageProcessingOptions options, ImageFormat outputFormat); /// <summary> /// Creates the image collage. diff --git a/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs index 6858c6d52..fd9ed5f1e 100644 --- a/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs +++ b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs @@ -139,7 +139,7 @@ namespace Emby.Drawing.ImageMagick string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase); } - public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) + public void EncodeImage(string inputPath, string outputPath, int rotationAngle, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { // Even if the caller specified 100, don't use it because it takes forever quality = Math.Min(quality, 99); @@ -150,6 +150,11 @@ namespace Emby.Drawing.ImageMagick { ScaleImage(originalImage, width, height); + if (rotationAngle > 0) + { + RotateImage(originalImage, rotationAngle); + } + DrawIndicator(originalImage, width, height, options); originalImage.CurrentImage.CompressionQuality = quality; @@ -166,6 +171,11 @@ namespace Emby.Drawing.ImageMagick { ScaleImage(originalImage, width, height); + if (rotationAngle > 0) + { + RotateImage(originalImage, rotationAngle); + } + wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0); DrawIndicator(wand, width, height, options); @@ -179,6 +189,14 @@ namespace Emby.Drawing.ImageMagick SaveDelay(); } + public static void RotateImage(MagickWand wand, float angle) + { + using (var pixelWand = new PixelWand("none", 1)) + { + wand.CurrentImage.RotateImage(pixelWand, angle); + } + } + private void ScaleImage(MagickWand wand, int width, int height) { var highQuality = false; diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index e1b92bbff..9de6d2042 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -257,7 +257,7 @@ namespace Emby.Drawing imageProcessingLockTaken = true; - _imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options, outputFormat); + _imageEncoder.EncodeImage(originalImagePath, cacheFilePath, GetRotationAngle(options.Item), newWidth, newHeight, quality, options, outputFormat); } return new Tuple<string, string>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath)); @@ -281,6 +281,35 @@ namespace Emby.Drawing } } + private int GetRotationAngle(IHasImages item) + { + var photo = item as Photo; + if (photo != null && photo.Orientation.HasValue) + { + switch (photo.Orientation.Value) + { + case ImageOrientation.TopLeft: + return 0; + case ImageOrientation.TopRight: + return 0; + case ImageOrientation.BottomLeft: + return 270; + case ImageOrientation.BottomRight: + return 180; + case ImageOrientation.LeftBottom: + return -90; + case ImageOrientation.LeftTop: + return 0; + case ImageOrientation.RightBottom: + return 0; + case ImageOrientation.RightTop: + return 90; + } + } + + return 0; + } + private string GetMimeType(ImageFormat format, string path) { if (format == ImageFormat.Bmp) diff --git a/Emby.Drawing/NullImageEncoder.cs b/Emby.Drawing/NullImageEncoder.cs index 1638a675a..032335a46 100644 --- a/Emby.Drawing/NullImageEncoder.cs +++ b/Emby.Drawing/NullImageEncoder.cs @@ -32,7 +32,7 @@ namespace Emby.Drawing throw new NotImplementedException(); } - public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) + public void EncodeImage(string inputPath, string outputPath, int rotationAngle, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { throw new NotImplementedException(); } diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index e47ebc551..ec54033df 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -1786,11 +1786,31 @@ namespace MediaBrowser.Server.Implementations.Dto } } - if (size.Width > 0 && size.Height > 0) + var width = size.Width; + var height = size.Height; + + if (width == 0 || height == 0) { - return size.Width / size.Height; + return null; } - return null; + + var photo = item as Photo; + if (photo != null && photo.Orientation.HasValue) + { + switch (photo.Orientation.Value) + { + case ImageOrientation.LeftBottom: + case ImageOrientation.LeftTop: + case ImageOrientation.RightBottom: + case ImageOrientation.RightTop: + var temp = height; + height = width; + width = temp; + break; + } + } + + return width / height; } } } |
