Table of Contents

Class JpegLosslessWriter

Namespace
Egami.Formats.Jpeg
Assembly
Egami.dll

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

output Stream

Where to write. It is left open.

samples ReadOnlySpan<ushort>

The samples, row after row, the components of each pixel next to each other.

components int

The components of a pixel, 1 to 4.

width int

The pixels in a row, 1 to 65535.

precision int

The bits of a sample, 2 to 16.

predictor int?

The T.81 predictor, 1 to 7, or null for the one that codes smallest.

restartInterval int

The restart interval in pixels, rounded up to whole rows, or 0 for none.

arithmetic bool

Whether 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

samples is 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

output Stream

Where to write. It is left open.

mosaic ReadOnlySpan<ushort>

The photosites, row after row.

width int

The photosites in a row: even, 2 to 131070.

precision int

The bits of a sample, 2 to 16.

predictor int

The T.81 predictor, 1 to 7.

Exceptions

ArgumentException

width is odd, or mosaic is not whole rows.