1

I am trying to use ImageSharp for some image processing. I would like to get HSL values for an individual pixel. For that I think that I need to convert PixelFormat to a ColorSpace. How do I convert to/access Hsl color space values?

I have tried the following ColorSpaceConverter to no avail.

for (int y = 0; y < image.Height; y++)
{
 Span<Rgb24> pixelRowSpan = image.GetPixelRowSpan(y);
 Span<Hsl> hslRowSpan = new Span<Hsl>();
 var converter = new ColorSpaceConverter();
 converter.Convert(pixelRowSpan, hslRowSpan);
}

I do get the following errors:

error CS1503: Argument 1: cannot convert from
    'System.Span<SixLabors.ImageSharp.PixelFormats.Rgb24>' to
    'System.ReadOnlySpan<SixLabors.ImageSharp.ColorSpaces.CieLch>' 
error CS1503: Argument 2: cannot convert from 
    'System.Span<SixLabors.ImageSharp.ColorSpaces.Hsl>' to 'System.Span<SixLabors.ImageSharp.ColorSpaces.CieLab>'
Stanislaw T
  • 420
  • 1
  • 6
  • 20
  • The formulas are pretty simple if you want to do it yourself: https://stackoverflow.com/questions/39118528/rgb-to-hsl-conversion – adv12 Jan 19 '22 at 16:21
  • @adv12 You are right. I shall resort to that if somebody from the ImageSharp community doesn't asnwer. – Stanislaw T Jan 19 '22 at 16:24

1 Answers1

1

Rgb24 has an implicit conversion to Rgb but as you have discovered that doesn't allow implicit conversion of spans.

I would allocate a pooled buffer equivalent to one row of Rgb outside the loop and populate the buffer for each y.

// I would probably pool these buffers.
Span<Rgb> rgb = new Rgb[image.Width];
Span<Hsl> hsl = new Hsl[image.Width];

ColorSpaceConverter converter = new();
for (int y = 0; y < image.Height; y++)
{
    Span<Rgb24> row = image.GetPixelRowSpan(y);
    for (int x = 0; x < row.Length; x++)
    {
        rgb[x] = row[x];
    }

    converter.Convert(rgb, hsl);
}
James South
  • 10,147
  • 4
  • 59
  • 115