Class SampleImage
An image as its codestream stores it: every component's samples at their own precision and sign, with no color conversion, widening or offset. For a container that keeps its own pixel description, such as DICOM, which needs the stored values to apply its rescale, window and color rules itself. JPEG (baseline, extended 12-bit and lossless at 2 to 16 bits), JPEG 2000 and JPEG XL load this way.
[Experimental("EGAMIX001", UrlFormat = "https://github.com/inexin/Egami/blob/main/docs/articles/formats/index.md#stored-samples")]
public sealed class SampleImage
- Inheritance
-
SampleImage
- Inherited Members
Examples
// The values the codestream stores: no color conversion, no widening to 8 or 16 bits, signed samples kept signed.
SampleImage image = SampleImage.Load("duck.jpg");
Console.WriteLine($"{image.Color}, {image.Components.Count} components of {image.Components[0].Precision} bits");
// A YCbCr JPEG stays YCbCr: luma, then the two chroma samples of each pixel, at full resolution.
Span<int> row = image.Row(0);
int luma = row[0], cb = row[1], cr = row[2];
Remarks
Samples are interleaved in component order, one int each, so signed values keep their sign. Components sampled at less than full resolution are brought to the image's size: JPEG's chroma upsampled as Load(Stream, EgamiContext?, ReaderOptions?) does it (or replicated with FastUpsampling), JPEG 2000's replicated.
A preview, as DICOM is: marked [Experimental] with the diagnostic EGAMIX001, so a call does not compile until the
app suppresses that id, and it may change in any minor release.
Properties
Color
Gets what the components are, as far as the codestream says: YCbCr means no conversion to RGB was made.
public SampleColor Color { get; }
Property Value
Components
Gets each component's precision and sign, in the order the samples are interleaved.
public IReadOnlyList<SampleComponent> Components { get; }
Property Value
Height
Gets the height in pixels.
public int Height { get; }
Property Value
Metadata
Gets the metadata the file holds, with any warnings from a lenient read of a damaged codestream.
public RasterMetadata Metadata { get; }
Property Value
Samples
Gets every sample, row after row, each pixel's components together: sample c of pixel (x, y) is at
((y * Width) + x) * Components.Count + c.
public Memory<int> Samples { get; }
Property Value
Width
Gets the width in pixels.
public int Width { get; }
Property Value
Methods
Load(Stream, EgamiContext?, ReaderOptions?)
Decodes an image to its stored samples. The format comes from the data, as for Load(Stream, EgamiContext?, ReaderOptions?).
public static SampleImage Load(Stream stream, EgamiContext? context = null, ReaderOptions? options = null)
Parameters
streamStreamThe encoded image. It is read from its current position and left open.
contextEgamiContextoptionsReaderOptionsHow to read, or null for Default. Format options apply where they make sense, such as FastUpsampling and MaxQualityLayers.
Returns
- SampleImage
The samples.
Examples
// The values the codestream stores: no color conversion, no widening to 8 or 16 bits, signed samples kept signed.
SampleImage image = SampleImage.Load("duck.jpg");
Console.WriteLine($"{image.Color}, {image.Components.Count} components of {image.Components[0].Precision} bits");
// A YCbCr JPEG stays YCbCr: luma, then the two chroma samples of each pixel, at full resolution.
Span<int> row = image.Row(0);
int luma = row[0], cb = row[1], cr = row[2];
Exceptions
- NotSupportedException
The format has no stored samples to give, or this image's do not fit: floating-point or more than 31 bits, or a JPEG with 2 or 4 components. Also thrown for Region, MinimumSize and ConvertColorTo.
- UnsupportedFormatException
No registered format matches the data.
- CorruptDataException
The data breaks the format's rules.
- LimitExceededException
The image breaks a safety limit.
- ArgumentNullException
streamis null.
Load(ReadOnlyMemory<byte>, EgamiContext?, ReaderOptions?)
Decodes an image in memory to its stored samples. See Load(Stream, EgamiContext?, ReaderOptions?).
public static SampleImage Load(ReadOnlyMemory<byte> data, EgamiContext? context = null, ReaderOptions? options = null)
Parameters
dataReadOnlyMemory<byte>The encoded image.
contextEgamiContextoptionsReaderOptions
Returns
- SampleImage
The samples.
Examples
// The values the codestream stores: no color conversion, no widening to 8 or 16 bits, signed samples kept signed.
SampleImage image = SampleImage.Load("duck.jpg");
Console.WriteLine($"{image.Color}, {image.Components.Count} components of {image.Components[0].Precision} bits");
// A YCbCr JPEG stays YCbCr: luma, then the two chroma samples of each pixel, at full resolution.
Span<int> row = image.Row(0);
int luma = row[0], cb = row[1], cr = row[2];
Exceptions
- NotSupportedException
The format or image has no stored samples to give.
- UnsupportedFormatException
No registered format matches the data.
- CorruptDataException
The data breaks the format's rules.
- LimitExceededException
The image breaks a safety limit.
Load(ReadOnlyMemory<byte>, FormatInfo, EgamiContext?, ReaderOptions?)
Decodes an image of a known format to its stored samples, without detecting the format: a codestream taken from a container, such as a DICOM pixel data fragment. See Load(Stream, EgamiContext?, ReaderOptions?).
public static SampleImage Load(ReadOnlyMemory<byte> data, FormatInfo format, EgamiContext? context = null, ReaderOptions? options = null)
Parameters
dataReadOnlyMemory<byte>The encoded image.
formatFormatInfoThe format to read with, such as Info. It need not be registered in the context.
contextEgamiContextoptionsReaderOptions
Returns
- SampleImage
The samples.
Examples
// A codestream taken out of a container, such as a DICOM pixel data fragment, is read as the format the container names.
SampleImage image = SampleImage.Load(fragment, JpegFormat.Info);
SampleComponent gray = image.Components[0];
int first = image.Samples.Span[0]; // 0 to 4095 for 12 unsigned bits
Exceptions
- NotSupportedException
The format or image has no stored samples to give.
- UnsupportedFormatException
formathas no reader.- CorruptDataException
The data breaks the format's rules.
- LimitExceededException
The image breaks a safety limit.
- ArgumentNullException
formatis null.
Load(string, EgamiContext?, ReaderOptions?)
Decodes an image file to its stored samples. See Load(Stream, EgamiContext?, ReaderOptions?).
public static SampleImage Load(string path, EgamiContext? context = null, ReaderOptions? options = null)
Parameters
pathstringThe file path.
contextEgamiContextoptionsReaderOptions
Returns
- SampleImage
The samples.
Examples
// The values the codestream stores: no color conversion, no widening to 8 or 16 bits, signed samples kept signed.
SampleImage image = SampleImage.Load("duck.jpg");
Console.WriteLine($"{image.Color}, {image.Components.Count} components of {image.Components[0].Precision} bits");
// A YCbCr JPEG stays YCbCr: luma, then the two chroma samples of each pixel, at full resolution.
Span<int> row = image.Row(0);
int luma = row[0], cb = row[1], cr = row[2];
Exceptions
- NotSupportedException
The format or image has no stored samples to give.
- UnsupportedFormatException
No registered format matches the data.
- CorruptDataException
The data breaks the format's rules.
- LimitExceededException
The image breaks a safety limit.
- FileNotFoundException
The file does not exist.
Row(int)
Returns row y: Width pixels of Components samples each.
public Span<int> Row(int y)
Parameters
yintThe row, from 0 at the top.
Returns
Exceptions
- ArgumentOutOfRangeException
yis outside the image.