Browse Source

Move resolution related methods to the extensions

pull/1570/head
Ildar Khayrutdinov 5 years ago
parent
commit
c14b7eb31f
  1. 96
      src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs
  2. 101
      src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs

96
src/ImageSharp/Formats/Tiff/TiffFrameMetadata.cs

@ -3,7 +3,6 @@
using System.Collections.Generic;
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Experimental.Tiff.Constants;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
@ -16,7 +15,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
public class TiffFrameMetadata : IDeepCloneable
{
// 2 (Inch)
private const ushort DefaultResolutionUnit = 2;
internal const ushort DefaultResolutionUnit = 2;
private const TiffPlanarConfiguration DefaultPlanarConfiguration = TiffPlanarConfiguration.Chunky;
@ -168,18 +167,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
/// <summary>
/// Gets the unit of measurement for XResolution and YResolution.
/// </summary>
public PixelResolutionUnit ResolutionUnit
{
get
{
if (!this.TryGetSingle(ExifTag.ResolutionUnit, out ushort res))
{
res = DefaultResolutionUnit;
}
return (PixelResolutionUnit)(res - 1);
}
}
public PixelResolutionUnit ResolutionUnit => this.GetResolutionUnit();
/// <summary>
/// Gets or sets the name and version number of the software package(s) used to create the image.
@ -247,7 +235,6 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
/// </summary>
public TiffSampleFormat[] SampleFormat => this.GetEnumArray<TiffSampleFormat, ushort>(ExifTag.SampleFormat, true);
/// <summary>
/// Clears the metadata.
/// </summary>
@ -288,84 +275,5 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
return new TiffFrameMetadata() { FrameTags = tags };
}
internal void SetResolutions(PixelResolutionUnit unit, double horizontal, double vertical)
{
switch (unit)
{
case PixelResolutionUnit.AspectRatio:
case PixelResolutionUnit.PixelsPerInch:
case PixelResolutionUnit.PixelsPerCentimeter:
break;
case PixelResolutionUnit.PixelsPerMeter:
{
unit = PixelResolutionUnit.PixelsPerCentimeter;
horizontal = UnitConverter.MeterToCm(horizontal);
vertical = UnitConverter.MeterToCm(vertical);
}
break;
default:
unit = PixelResolutionUnit.PixelsPerInch;
break;
}
this.SetSingle(ExifTag.ResolutionUnit, (ushort)unit + 1);
this.SetResolution(ExifTag.XResolution, horizontal);
this.SetResolution(ExifTag.YResolution, vertical);
}
private double? GetResolution(ExifTag tag)
{
if (!this.TryGetSingle(tag, out Rational resolution))
{
return null;
}
double res = resolution.ToDouble();
switch (this.ResolutionUnit)
{
case PixelResolutionUnit.AspectRatio:
return 0;
case PixelResolutionUnit.PixelsPerCentimeter:
return UnitConverter.CmToInch(res);
case PixelResolutionUnit.PixelsPerMeter:
return UnitConverter.MeterToInch(res);
case PixelResolutionUnit.PixelsPerInch:
default:
// DefaultResolutionUnit is Inch
return res;
}
}
private void SetResolution(ExifTag tag, double? value)
{
if (value == null)
{
this.Remove(tag);
return;
}
else
{
double res = value.Value;
switch (this.ResolutionUnit)
{
case PixelResolutionUnit.AspectRatio:
res = 0;
break;
case PixelResolutionUnit.PixelsPerCentimeter:
res = UnitConverter.InchToCm(res);
break;
case PixelResolutionUnit.PixelsPerMeter:
res = UnitConverter.InchToMeter(res);
break;
case PixelResolutionUnit.PixelsPerInch:
default:
break;
}
this.SetSingle(tag, new Rational(res));
}
}
}
}

101
src/ImageSharp/Formats/Tiff/TiffFrameMetadataResolutionExtensions.cs

@ -0,0 +1,101 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
namespace SixLabors.ImageSharp.Formats.Experimental.Tiff
{
internal static class TiffFrameMetadataResolutionExtensions
{
public static void SetResolutions(this TiffFrameMetadata meta, PixelResolutionUnit unit, double horizontal, double vertical)
{
switch (unit)
{
case PixelResolutionUnit.AspectRatio:
case PixelResolutionUnit.PixelsPerInch:
case PixelResolutionUnit.PixelsPerCentimeter:
break;
case PixelResolutionUnit.PixelsPerMeter:
{
unit = PixelResolutionUnit.PixelsPerCentimeter;
horizontal = UnitConverter.MeterToCm(horizontal);
vertical = UnitConverter.MeterToCm(vertical);
}
break;
default:
unit = PixelResolutionUnit.PixelsPerInch;
break;
}
meta.SetSingle(ExifTag.ResolutionUnit, (ushort)unit + 1);
meta.SetResolution(ExifTag.XResolution, horizontal);
meta.SetResolution(ExifTag.YResolution, vertical);
}
public static PixelResolutionUnit GetResolutionUnit(this TiffFrameMetadata meta)
{
if (!meta.TryGetSingle(ExifTag.ResolutionUnit, out ushort res))
{
res = TiffFrameMetadata.DefaultResolutionUnit;
}
return (PixelResolutionUnit)(res - 1);
}
public static double? GetResolution(this TiffFrameMetadata meta, ExifTag tag)
{
if (!meta.TryGetSingle(tag, out Rational resolution))
{
return null;
}
double res = resolution.ToDouble();
switch (meta.ResolutionUnit)
{
case PixelResolutionUnit.AspectRatio:
return 0;
case PixelResolutionUnit.PixelsPerCentimeter:
return UnitConverter.CmToInch(res);
case PixelResolutionUnit.PixelsPerMeter:
return UnitConverter.MeterToInch(res);
case PixelResolutionUnit.PixelsPerInch:
default:
// DefaultResolutionUnit is Inch
return res;
}
}
private static void SetResolution(this TiffFrameMetadata meta, ExifTag tag, double? value)
{
if (value == null)
{
meta.Remove(tag);
return;
}
else
{
double res = value.Value;
switch (meta.ResolutionUnit)
{
case PixelResolutionUnit.AspectRatio:
res = 0;
break;
case PixelResolutionUnit.PixelsPerCentimeter:
res = UnitConverter.InchToCm(res);
break;
case PixelResolutionUnit.PixelsPerMeter:
res = UnitConverter.InchToMeter(res);
break;
case PixelResolutionUnit.PixelsPerInch:
default:
break;
}
meta.SetSingle(tag, new Rational(res));
}
}
}
}
Loading…
Cancel
Save