Skip to content

Commit

Permalink
Fixed 2 bugs:
Browse files Browse the repository at this point in the history
1. If no orientation was preset it would throw a divide by 0 exception
2. If metadata was not kept,  it would throw the wrong exception
  • Loading branch information
devedse committed Jul 31, 2017
1 parent faab37e commit b525f34
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
3 changes: 3 additions & 0 deletions DeveImageOptimizer.Tests/DeveImageOptimizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<None Update="TestImages\Image3B.JPG">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestImages\pexels-photo.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions DeveImageOptimizer.Tests/ImageOptimizerWorks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public async void CorrectlyOptimizesStandingRotatedImage()
await OptimizeFileTest("Image2A.JPG");
}

[SkippableFact]
public async void CorrectlyOptimizedPexelsPhoto()
{
await OptimizeFileTest("pexels-photo.jpg");
}

[Fact]
public async void RemovesExifRotationAndReapliesAfterwards()
{
Expand Down Expand Up @@ -160,7 +166,7 @@ public async void RemovesExifRotationAndReapliesAfterwards()

private async Task OptimizeFileTest(string fileName)
{
var fileOptimizerPath = @"C:\Users\Davy\Downloads\FileOptimizerFull\FileOptimizer64.exe";
var fileOptimizerPath = @"C:\Program Files\FileOptimizer\FileOptimizer64.exe";

Skip.IfNot(File.Exists(fileOptimizerPath), $"FileOptimizerFull exe file can't be found. Expected location: {fileOptimizerPath}");

Expand All @@ -187,7 +193,7 @@ private async Task OptimizeFileTest(string fileName)
finally
{
File.Delete(image1temppath);
Directory.Delete(tempfortestdir);
Directory.Delete(tempfortestdir, true);
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions DeveImageOptimizer/Exif/MathEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,11 @@ private static UFraction32 FromString(string s)
private static void Reduce(ref uint numerator, ref uint denominator)
{
uint gcd = MathEx.GCD(numerator, denominator);
numerator = numerator / gcd;
denominator = denominator / gcd;
if (gcd > 1)
{
numerator = numerator / gcd;
denominator = denominator / gcd;
}
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<OptimizedFileResult> OptimizeFile(string fileToOptimize, bool

await AsyncFileHelper.CopyFileAsync(fileToOptimize, tempFilePath, true);

Orientation jpegFileOrientation = Orientation.Normal;
Orientation? jpegFileOrientation = null;
bool shouldUseJpgWorkaround = FileTypeHelper.IsJpgFile(tempFilePath);
if (shouldUseJpgWorkaround)
{
Expand Down
45 changes: 32 additions & 13 deletions DeveImageOptimizer/Helpers/ExifImageRotator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,62 @@ namespace DeveImageOptimizer.Helpers
{
public static class ExifImageRotator
{
public async static Task<Orientation> UnrotateImageAsync(string path)
public async static Task<Orientation?> UnrotateImageAsync(string path)
{
return await Task.Run(() =>
{
return UnrotateImage(path);
});
}

private static Orientation UnrotateImage(string path)
private static Orientation? UnrotateImage(string path)
{
var file = ExifFile.Read(path);

var orientationExif = file.Properties[ExifTag.Orientation];
var retval = (Orientation)orientationExif.Value;
orientationExif.Value = Orientation.Normal;
ExifProperty orientationExif;

file.Save(path);
if (file.Properties.TryGetValue(ExifTag.Orientation, out orientationExif))
{
var retval = (Orientation)orientationExif.Value;
orientationExif.Value = Orientation.Normal;

file.Save(path);

return retval;
return retval;
}
else
{
return null;
}
}

public async static Task RerotateImageAsync(string path, Orientation newOrientation)
public async static Task RerotateImageAsync(string path, Orientation? newOrientation)
{
await Task.Run(() =>
{
RerotateImage(path, newOrientation);
});
}

private static void RerotateImage(string path, Orientation newOrientation)
private static void RerotateImage(string path, Orientation? newOrientation)
{
var file = ExifFile.Read(path);
if (newOrientation != null)
{
var file = ExifFile.Read(path);

ExifProperty orientationExif;

var orientationExif = file.Properties[ExifTag.Orientation];
orientationExif.Value = newOrientation;
if (file.Properties.TryGetValue(ExifTag.Orientation, out orientationExif))
{
orientationExif.Value = newOrientation;
}
else
{
throw new Exception("MetaData is not kept equal");
}

file.Save(path);
file.Save(path);
}
}
}
}

0 comments on commit b525f34

Please sign in to comment.