Skip to content

Commit

Permalink
normalize resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
IS4Code committed Jan 17, 2024
1 parent d19dcee commit cf270cd
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions SFI.Formats/Images/SharpImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.CSharp.RuntimeBinder;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
Expand All @@ -21,6 +23,10 @@ public class SharpImage : ImageBase<IImage>
{
Image Image => UnderlyingImage as Image ?? throw new NotSupportedException();

ImageMetadata Metadata => UnderlyingImage.Metadata;

PixelTypeInfo PixelType => UnderlyingImage.PixelType;

public SharpImage(IImage underlyingImage, IFileFormat<IImage> format) : base(underlyingImage, format)
{

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

public override int Height => UnderlyingImage.Height;

public override double HorizontalResolution => UnderlyingImage.Metadata.HorizontalResolution;
public override double HorizontalResolution => ResolutionUnit * Metadata.HorizontalResolution;

public override double VerticalResolution => UnderlyingImage.Metadata.VerticalResolution;
public override double VerticalResolution => ResolutionUnit * Metadata.VerticalResolution;

private double ResolutionUnit{
get{
switch(Metadata.ResolutionUnits)
{
case PixelResolutionUnit.AspectRatio:
return 0;
case PixelResolutionUnit.PixelsPerInch:
return 1;
case PixelResolutionUnit.PixelsPerCentimeter:
return 2.54d;
case PixelResolutionUnit.PixelsPerMeter:
return 0.0254d;
default:
throw new NotSupportedException();
}
}
}

public override bool HasAlpha => UnderlyingImage.PixelType.AlphaRepresentation is not (0 or null);
public override bool HasAlpha => PixelType.AlphaRepresentation is not (0 or null);

public override int BitDepth => UnderlyingImage.PixelType.BitsPerPixel;
public override int BitDepth => PixelType.BitsPerPixel;

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

Expand Down

0 comments on commit cf270cd

Please sign in to comment.