Class JpegLosslessWriter
Writes lossless JPEG (ITU-T T.81 annex H, SOF3 or SOF11) from samples that are not pixels: TIFF compression 7 strips and tiles, DNG's sensor tiles and any other format that stores lossless JPEG streams. The samples are coded exactly as saving the same samples as pixels with JpegLosslessOptions codes them.
public static class JpegLosslessWriter
- Inheritance
-
JpegLosslessWriter
- Inherited Members
Examples
// Samples that are not pixels, such as a camera's 14-bit sensor data, as a lossless JPEG stream (a DNG or TIFF tile).
ushort[] samples = new ushort[64 * 32];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = (ushort)((i * 37) % 16384);
}
using (MemoryStream tile = new())
{
JpegLosslessWriter.Write(tile, samples, components: 1, width: 64, precision: 14, predictor: null);
// A colour filter array tile as DNG lays it out: each row's photosites coded in pairs, half as wide.
using MemoryStream cfa = new();
JpegLosslessWriter.WriteCfa(cfa, samples, width: 64, precision: 14);
Assert.True(tile.Length > 0 && cfa.Length > 0);
}
Methods
Write(Stream, ReadOnlySpan<ushort>, int, int, int, int?, int, bool)
Writes samples, rows of width pixels of components samples each,
interleaved by component, as a whole lossless JPEG stream: SOI, the frame, its Huffman tables (or none when arithmetic
coded), the scan and EOI, with no APP segment. Every sample must be below 2^precision. A
null predictor picks the one that codes smallest; restartInterval
is in samples per component, rounded up to whole rows. The component ids are 1 for one component, R, G, B for three, C,
M, Y, K for four and 1, 2 for two.
public static void Write(Stream output, ReadOnlySpan<ushort> samples, int components, int width, int precision, int? predictor, int restartInterval = 0, bool arithmetic = false)
Parameters
outputStreamWhere to write. It is left open.
samplesReadOnlySpan<ushort>The samples, row after row, the components of each pixel next to each other.
componentsintThe components of a pixel, 1 to 4.
widthintThe pixels in a row, 1 to 65535.
precisionintThe bits of a sample, 2 to 16.
predictorint?The T.81 predictor, 1 to 7, or null for the one that codes smallest.
restartIntervalintThe restart interval in pixels, rounded up to whole rows, or 0 for none.
arithmeticboolWhether to code with the arithmetic coder (SOF11) instead of Huffman tables (SOF3).
Exceptions
- ArgumentOutOfRangeException
A count, the precision or the predictor is out of range.
- ArgumentException
samplesis not 1 to 65535 whole rows.
WriteCfa(Stream, ReadOnlySpan<ushort>, int, int, int)
Writes a colour filter array mosaic, rows of width photosites, as DNG lays its lossless JPEG tiles out:
each row coded as pairs of photosites, a two-component frame half as wide, so the two colours of a row each predict from
their own kind. Predictor 1, as Adobe DNG Converter writes. The width must be even.
public static void WriteCfa(Stream output, ReadOnlySpan<ushort> mosaic, int width, int precision, int predictor = 1)
Parameters
outputStreamWhere to write. It is left open.
mosaicReadOnlySpan<ushort>The photosites, row after row.
widthintThe photosites in a row: even, 2 to 131070.
precisionintThe bits of a sample, 2 to 16.
predictorintThe T.81 predictor, 1 to 7.
Exceptions
- ArgumentException
widthis odd, ormosaicis not whole rows.