Skip to content

Commit 1104ed4

Browse files
Switched over to ImageSharp to load images
1 parent 54215cc commit 1104ed4

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

Camera.Simulator/Camera.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
using ASCOM.Standard.Interfaces;
4747
using ASCOM.Alpaca.Responses;
4848
using System.Runtime.CompilerServices;
49+
using SixLabors.ImageSharp;
50+
using SixLabors.ImageSharp.PixelFormats;
51+
using SixLabors.ImageSharp.ColorSpaces;
52+
using SixLabors.ImageSharp.ColorSpaces.Conversion;
4953

5054
[assembly: InternalsVisibleTo("ASCOM.Alpaca.Simulators")]
5155
namespace ASCOM.Simulators
@@ -2875,7 +2879,7 @@ private double DarkData(int x, int y, int p)
28752879
}
28762880

28772881
private delegate void GetData(int x, int y);
2878-
private Bitmap bmp;
2882+
private Image<Rgba32> bmp;
28792883
// Bayer offsets
28802884
private int x0;
28812885
private int x1;
@@ -2910,7 +2914,7 @@ private void ReadImageFile()
29102914

29112915
try
29122916
{
2913-
bmp = (Bitmap)Image.FromFile(imagePath);
2917+
bmp = Image.Load<Rgba32>(imagePath);
29142918

29152919
// x0 = bayerOffsetX;
29162920
// x1 = (bayerOffsetX + 1) & 1;
@@ -2976,73 +2980,82 @@ private void ReadImageFile()
29762980
}
29772981

29782982
}
2979-
catch
2983+
catch(Exception ex)
29802984
{
2985+
Log.log.LogError(ex.Message);
29812986
}
29822987
}
29832988

29842989
// get data using the sensor types
29852990
private void MonochromeData(int x, int y)
29862991
{
2987-
imageData[x, y, 0] = (bmp.GetPixel(x, y).GetBrightness() * 255);
2992+
2993+
ColorSpaceConverter converter = new ColorSpaceConverter();
2994+
2995+
2996+
imageData[x, y, 0] = (converter.ToHsl(bmp[x, y]).L * 255);
29882997
}
29892998
private void RGGBData(int x, int y)
29902999
{
2991-
Color px = bmp.GetPixel(x / 2, y / 2);
3000+
var px = bmp[x / 2, y / 2];
29923001
imageData[x + x0, y + y0, 0] = px.R; // red
29933002
imageData[x + x1, y + y0, 0] = px.G; // green
29943003
imageData[x + x0, y + y1, 0] = px.G; // green
29953004
imageData[x + x1, y + y1, 0] = px.B; // blue
29963005
}
29973006
private void CMYGData(int x, int y)
29983007
{
2999-
Color px = bmp.GetPixel(x / 2, y / 2);
3008+
var px = bmp[x / 2, y / 2];
30003009
imageData[x + x0, y + y0, 0] = (px.R + px.G) / 2; // yellow
30013010
imageData[x + x1, y + y0, 0] = (px.G + px.B) / 2; // cyan
30023011
imageData[x + x0, y + y1, 0] = px.G; // green
30033012
imageData[x + x1, y + y1, 0] = (px.R + px.B) / 2; // magenta
30043013
}
30053014
private void CMYG2Data(int x, int y)
30063015
{
3007-
Color px = bmp.GetPixel(x / 2, y / 2);
3016+
var px = bmp[x / 2, y / 2];
30083017
imageData[x + x0, y + y0, 0] = (px.G);
30093018
imageData[x + x1, y + y0, 0] = (px.B + px.R) / 2; // magenta
30103019
imageData[x + x0, y + y1, 0] = (px.G + px.B) / 2; // cyan
30113020
imageData[x + x1, y + y1, 0] = (px.R + px.G) / 2; // yellow
3012-
px = bmp.GetPixel(x / 2, (y / 2) + 1);
3021+
px = bmp[x / 2, (y / 2) + 1];
30133022
imageData[x + x0, y + y2, 0] = (px.B + px.R) / 2; // magenta
30143023
imageData[x + x1, y + y2, 0] = (px.G);
30153024
imageData[x + x0, y + y3, 0] = (px.G + px.B) / 2; // cyan
30163025
imageData[x + x1, y + y3, 0] = (px.R + px.G) / 2; // yellow
30173026
}
30183027
private void LRGBData(int x, int y)
30193028
{
3020-
Color px = bmp.GetPixel(x / 2, y / 2);
3021-
imageData[x + x0, y + y0, 0] = px.GetBrightness() * 255;
3029+
ColorSpaceConverter converter = new ColorSpaceConverter();
3030+
3031+
3032+
3033+
var px = bmp[x / 2, y / 2];
3034+
imageData[x + x0, y + y0, 0] = converter.ToHsl(px).L * 255;
30223035
imageData[x + x1, y + y0, 0] = (px.R);
30233036
imageData[x + x0, y + y1, 0] = (px.R);
3024-
imageData[x + x1, y + y1, 0] = px.GetBrightness() * 255;
3025-
px = bmp.GetPixel((x / 2) + 1, y / 2);
3026-
imageData[x + x2, y + y0, 0] = px.GetBrightness() * 255;
3037+
imageData[x + x1, y + y1, 0] = converter.ToHsl(px).L * 255;
3038+
px = bmp[(x / 2) + 1, y / 2];
3039+
imageData[x + x2, y + y0, 0] = converter.ToHsl(px).L * 255;
30273040
imageData[x + x3, y + y0, 0] = (px.G);
30283041
imageData[x + x2, y + y1, 0] = (px.G);
3029-
imageData[x + x3, y + y1, 0] = px.GetBrightness() * 255;
3030-
px = bmp.GetPixel(x / 2, (y / 2) + 1);
3031-
imageData[x + x0, y + y2, 0] = px.GetBrightness() * 255;
3042+
imageData[x + x3, y + y1, 0] = converter.ToHsl(px).L * 255;
3043+
px = bmp[x / 2, (y / 2) + 1];
3044+
imageData[x + x0, y + y2, 0] = converter.ToHsl(px).L * 255;
30323045
imageData[x + x1, y + y2, 0] = (px.G);
30333046
imageData[x + x0, y + y3, 0] = (px.G);
3034-
imageData[x + x1, y + y3, 0] = px.GetBrightness() * 255;
3035-
px = bmp.GetPixel((x / 2) + 1, (y / 2) + 1);
3036-
imageData[x + x2, y + y2, 0] = px.GetBrightness() * 255;
3047+
imageData[x + x1, y + y3, 0] = converter.ToHsl(px).L * 255;
3048+
px = bmp[(x / 2) + 1, (y / 2) + 1];
3049+
imageData[x + x2, y + y2, 0] = converter.ToHsl(px).L * 255;
30373050
imageData[x + x3, y + y2, 0] = (px.B);
30383051
imageData[x + x2, y + y3, 0] = (px.B);
3039-
imageData[x + x3, y + y3, 0] = px.GetBrightness() * 255;
3052+
imageData[x + x3, y + y3, 0] = converter.ToHsl(px).L * 255;
30403053
}
30413054
private void ColorData(int x, int y)
30423055
{
3043-
imageData[x, y, 0] = (bmp.GetPixel(x, y).R);
3044-
imageData[x, y, 1] = (bmp.GetPixel(x, y).G);
3045-
imageData[x, y, 2] = (bmp.GetPixel(x, y).B);
3056+
imageData[x, y, 0] = (bmp[x, y].R);
3057+
imageData[x, y, 1] = (bmp[x, y].G);
3058+
imageData[x, y, 2] = (bmp[x, y].B);
30463059
}
30473060

30483061
/// <summary>

Camera.Simulator/CameraSimulator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="ASCOM.Exceptions" Version="6.5.1" />
99
<PackageReference Include="ASCOM.Standard.Types" Version="1.0.0-alpha15" />
10-
<PackageReference Include="System.Drawing.Common" Version="5.0.1" />
10+
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

0 commit comments

Comments
 (0)