Skip to content

Commit

Permalink
reworked naming of ImageProcessor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyknight-me committed Sep 17, 2023
1 parent b775987 commit 7a83011
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 64 deletions.
12 changes: 0 additions & 12 deletions PhotoCollageScreensaver/IImageProcessor.cs

This file was deleted.

8 changes: 4 additions & 4 deletions PhotoCollageScreensaver/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PhotoCollageScreensaver;

internal abstract class ImageProcessor : IImageProcessor
internal abstract class ImageProcessor
{
protected readonly string ImagePath;
protected readonly CollageSettings Configuration;
Expand All @@ -18,7 +18,9 @@ public ImageProcessor(string imagePathToUse, CollageSettings configurationToUse)
this.Configuration = configurationToUse;
}

public BitmapSource GetImage()
public abstract ImageSource GetImageSource(ICollageView view, BitmapSource sourceImage = null);

protected BitmapSource GetBitmapImage()
{
BitmapSource rawImage = this.GetRawImage();
var sourceImage = this.Configuration.IsGrayscale
Expand All @@ -30,8 +32,6 @@ public BitmapSource GetImage()
return sourceImage;
}

public virtual ImageSource GetScaledImage(ICollageView view, BitmapSource sourceImage = null) => this.GetImage();

private BitmapImage GetRawImage()
{
var image = new BitmapImage();
Expand Down
12 changes: 6 additions & 6 deletions PhotoCollageScreensaver/ImageProcessorCollage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public ImageProcessorCollage(string imagePathToUse, CollageSettings configuratio
{
}

public override ImageSource GetScaledImage(ICollageView view, BitmapSource sourceImage = null)
public override ImageSource GetImageSource(ICollageView view, BitmapSource sourceImage = null)
{
if (sourceImage == null)
{
sourceImage = this.GetImage();
sourceImage = this.GetBitmapImage();
}

return this.DoesImageNeedScaling(sourceImage.Height, sourceImage.Width)
? this.GetScaledImage(sourceImage)
? this.GetScaledTransformedImage(sourceImage)
: sourceImage;
}

Expand All @@ -29,11 +29,11 @@ private bool DoesImageNeedScaling(double height, double width)
|| width > this.MaximumSizeDiu
|| this.DpiScale > 1;

private TransformedBitmap GetScaledImage(BitmapSource original)
private TransformedBitmap GetScaledTransformedImage(BitmapSource original)
{
var scale = original.Height > original.Width
? this.GetScale(original.Height)
: this.GetScale(original.Width);
? this.GetScale(original.Height)
: this.GetScale(original.Width);
RenderOptions.SetBitmapScalingMode(original, BitmapScalingMode.HighQuality);
var transform = new ScaleTransform(scale, scale);
return new TransformedBitmap(original, transform);
Expand Down
75 changes: 36 additions & 39 deletions PhotoCollageScreensaver/ImageProcessorFullscreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@ public ImageProcessorFullscreen(string imagePathToUse, CollageSettings configura

public bool ImageIsRotatedPlusMinusNinetyDegrees { get; private set; }

public new BitmapSource GetImage()
{
var sourceImage = base.GetImage();
if (this.Configuration.RotateBasedOnEXIF)
{
using var image = Image.FromFile(this.ImagePath);
this.GetExifRotationData(image);
}

return sourceImage;
}

public override ImageSource GetScaledImage(ICollageView view, BitmapSource sourceImage=null)
public override ImageSource GetImageSource(ICollageView view, BitmapSource sourceImage = null)
{
if (sourceImage == null)
{
sourceImage = this.GetImage();
sourceImage = this.GetBitmapImage();
}

if (this.Configuration.RotateBasedOnEXIF && this.rotationAngle != 0)
Expand All @@ -48,35 +36,45 @@ public override ImageSource GetScaledImage(ICollageView view, BitmapSource sourc
return this.GetFullScreenScaledImage(sourceImage, view);
}

public BitmapSource GetImage()
{
var sourceImage = this.GetBitmapImage();
if (this.Configuration.RotateBasedOnEXIF)
{
using var image = Image.FromFile(this.ImagePath);
this.GetExifRotationData(image);
}

return sourceImage;
}

private void GetExifRotationData(Image image)
{
using (var memoryStream = new MemoryStream())
using var memoryStream = new MemoryStream();
// Save the image to the memory stream in a suitable format (e.g., PNG, JPEG)
image.Save(memoryStream, ImageFormat.Png); // You can change the format if needed

if (image.PropertyIdList.Contains(0x0112)) // 0x0112 is the EXIF tag for orientation
{
// Save the image to the memory stream in a suitable format (e.g., PNG, JPEG)
image.Save(memoryStream, ImageFormat.Png); // You can change the format if needed
var propertyItem = image.GetPropertyItem(0x0112);
int rotationValue = BitConverter.ToUInt16(propertyItem.Value, 0);

if (image.PropertyIdList.Contains(0x0112)) // 0x0112 is the EXIF tag for orientation
// EXIF rotation values (defined by the EXIF specification)
this.rotationAngle = 0;
this.ImageIsRotatedPlusMinusNinetyDegrees = false;
switch (rotationValue)
{
var propertyItem = image.GetPropertyItem(0x0112);
int rotationValue = BitConverter.ToUInt16(propertyItem.Value, 0);

// EXIF rotation values (defined by the EXIF specification)
this.rotationAngle = 0;
this.ImageIsRotatedPlusMinusNinetyDegrees = false;
switch (rotationValue)
{
case 3:
this.rotationAngle = 180;
break;
case 6:
this.rotationAngle = 90;
this.ImageIsRotatedPlusMinusNinetyDegrees = true;
break;
case 8:
this.rotationAngle = 270;
this.ImageIsRotatedPlusMinusNinetyDegrees = true;
break;
}
case 3:
this.rotationAngle = 180;
break;
case 6:
this.rotationAngle = 90;
this.ImageIsRotatedPlusMinusNinetyDegrees = true;
break;
case 8:
this.rotationAngle = 270;
this.ImageIsRotatedPlusMinusNinetyDegrees = true;
break;
}
}
}
Expand All @@ -96,7 +94,6 @@ private TransformedBitmap GetFullScreenScaledImage(BitmapSource original, IColla
{
scaledHeight = view.WindowActualHeight / original.Height;
scaledWidth = view.WindowActualWidth / original.Width;

scaledWidth = scaledHeight = scaledHeight > scaledWidth ? scaledWidth : scaledHeight;
}
RenderOptions.SetBitmapScalingMode(original, BitmapScalingMode.HighQuality);
Expand Down
5 changes: 2 additions & 3 deletions PhotoCollageScreensaver/UserControls/CollageImage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ private void RotateImageFrame()

private void LoadImage()
{
IImageProcessor processor = this.presenter.Configuration.IsFullScreen
ImageProcessor processor = this.presenter.Configuration.IsFullScreen
? new ImageProcessorFullscreen(this.filePath, this.presenter.Configuration)
: new ImageProcessorCollage(this.filePath, this.presenter.Configuration);
this.MainImage.Source = processor.GetScaledImage(this.view);

this.MainImage.Source = processor.GetImageSource(this.view);

if (!this.presenter.Configuration.IsFullScreen)
{
Expand Down

0 comments on commit 7a83011

Please sign in to comment.