Skip to content

Commit cf270cd

Browse files
committed
normalize resolution
1 parent d19dcee commit cf270cd

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

SFI.Formats/Images/SharpImage.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.CSharp.RuntimeBinder;
44
using SixLabors.ImageSharp;
55
using SixLabors.ImageSharp.Advanced;
6+
using SixLabors.ImageSharp.Formats;
7+
using SixLabors.ImageSharp.Metadata;
68
using SixLabors.ImageSharp.PixelFormats;
79
using SixLabors.ImageSharp.Processing;
810
using System;
@@ -21,6 +23,10 @@ public class SharpImage : ImageBase<IImage>
2123
{
2224
Image Image => UnderlyingImage as Image ?? throw new NotSupportedException();
2325

26+
ImageMetadata Metadata => UnderlyingImage.Metadata;
27+
28+
PixelTypeInfo PixelType => UnderlyingImage.PixelType;
29+
2430
public SharpImage(IImage underlyingImage, IFileFormat<IImage> format) : base(underlyingImage, format)
2531
{
2632

@@ -30,13 +36,31 @@ public SharpImage(IImage underlyingImage, IFileFormat<IImage> format) : base(und
3036

3137
public override int Height => UnderlyingImage.Height;
3238

33-
public override double HorizontalResolution => UnderlyingImage.Metadata.HorizontalResolution;
39+
public override double HorizontalResolution => ResolutionUnit * Metadata.HorizontalResolution;
3440

35-
public override double VerticalResolution => UnderlyingImage.Metadata.VerticalResolution;
41+
public override double VerticalResolution => ResolutionUnit * Metadata.VerticalResolution;
42+
43+
private double ResolutionUnit{
44+
get{
45+
switch(Metadata.ResolutionUnits)
46+
{
47+
case PixelResolutionUnit.AspectRatio:
48+
return 0;
49+
case PixelResolutionUnit.PixelsPerInch:
50+
return 1;
51+
case PixelResolutionUnit.PixelsPerCentimeter:
52+
return 2.54d;
53+
case PixelResolutionUnit.PixelsPerMeter:
54+
return 0.0254d;
55+
default:
56+
throw new NotSupportedException();
57+
}
58+
}
59+
}
3660

37-
public override bool HasAlpha => UnderlyingImage.PixelType.AlphaRepresentation is not (0 or null);
61+
public override bool HasAlpha => PixelType.AlphaRepresentation is not (0 or null);
3862

39-
public override int BitDepth => UnderlyingImage.PixelType.BitsPerPixel;
63+
public override int BitDepth => PixelType.BitsPerPixel;
4064

4165
public override IReadOnlyList<Color> Palette => Array.Empty<Color>(); // TODO
4266

0 commit comments

Comments
 (0)