/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="nComponent">The number of components to write.</param> private void WriteDHT(int nComponent) { byte[] headers = { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = this.theHuffmanSpec; if (nComponent == 1) { // Drop the Chrominance tables. specs = new[] { this.theHuffmanSpec[0], this.theHuffmanSpec[1] }; } foreach (var s in specs) { markerlen += 1 + 16 + s.Values.Length; } this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++) { HuffmanSpec spec = specs[i]; this.WriteByte(headers[i]); this.outputStream.Write(spec.Count, 0, spec.Count.Length); this.outputStream.Write(spec.Values, 0, spec.Values.Length); } }
/// <summary> /// Initializes a new instance of the <see cref="HuffmanLut"/> struct. /// </summary> /// <param name="spec">dasd</param> public HuffmanLut(HuffmanSpec spec) { int maxValue = 0; foreach (byte v in spec.Values) { if (v > maxValue) { maxValue = v; } } this.Values = new uint[maxValue + 1]; int code = 0; int k = 0; for (int i = 0; i < spec.Count.Length; i++) { int bits = (i + 1) << 24; for (int j = 0; j < spec.Count[i]; j++) { this.Values[spec.Values[k]] = (uint)(bits | code); code++; k++; } code <<= 1; } }
/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="componentCount">The number of components to write.</param> private void WriteDefineHuffmanTables(int componentCount) { // Table identifiers. byte[] headers = { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = TheHuffmanSpecs; if (componentCount == 1) { // Drop the Chrominance tables. specs = new[] { TheHuffmanSpecs[0], TheHuffmanSpecs[1] }; } foreach (HuffmanSpec s in specs) { markerlen += 1 + 16 + s.Values.Length; } this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++) { HuffmanSpec spec = specs[i]; // TODO: Investigate optimizing this. It might be better to create a single array. this.WriteByte(headers[i]); this.outputStream.Write(spec.Count, 0, spec.Count.Length); this.outputStream.Write(spec.Values, 0, spec.Values.Length); } }
public HuffmanLut(HuffmanSpec s) { int maxValue = 0; foreach (byte v in s.values) { if (v > maxValue) { maxValue = v; } } values = new uint[maxValue + 1]; int code = 0; int k = 0; for (int i = 0; i < s.count.Length; i++) { int nBits = (i + 1) << 24; for (int j = 0; j < s.count[i]; j++) { values[s.values[k]] = (uint)(nBits | code); code++; k++; } code <<= 1; } }
/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="componentCount">The number of components to write.</param> private void WriteDefineHuffmanTables(int componentCount) { // This uses a C#'s compiler optimization that refers to the static data segment of the assembly, // and doesn't incur any allocation at all. // Table identifiers. ReadOnlySpan <byte> headers = new byte[] { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = HuffmanSpec.TheHuffmanSpecs; if (componentCount == 1) { // Drop the Chrominance tables. specs = new[] { HuffmanSpec.TheHuffmanSpecs[0], HuffmanSpec.TheHuffmanSpecs[1] }; } for (int i = 0; i < specs.Length; i++) { ref HuffmanSpec s = ref specs[i]; markerlen += 1 + 16 + s.Values.Length; }
/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="componentCount">The number of components to write.</param> private void WriteDefineHuffmanTables(int componentCount) { // Table identifiers. ReadOnlySpan <byte> headers = stackalloc byte[] { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = HuffmanSpec.TheHuffmanSpecs; if (componentCount == 1) { // Drop the Chrominance tables. specs = new[] { HuffmanSpec.TheHuffmanSpecs[0], HuffmanSpec.TheHuffmanSpecs[1] }; } for (int i = 0; i < specs.Length; i++) { ref HuffmanSpec s = ref specs[i]; markerlen += 1 + 16 + s.Values.Length; } this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++) { this.outputStream.WriteByte(headers[i]); this.outputStream.Write(specs[i].Count); this.outputStream.Write(specs[i].Values); } }
/// <summary> /// Initializes a new instance of the <see cref="HuffmanLookUpTable"/> class. /// </summary> /// <param name="spec">The spec.</param> public HuffmanLookUpTable(HuffmanSpec spec) { int maxValue = 0; foreach (var v in spec.Values) { if (v > maxValue) { maxValue = v; } } Values = new uint[maxValue + 1]; int code = 0; int k = 0; for (int i = 0; i < spec.Count.Length; i++) { int nBits = (i + 1) << 24; for (int j = 0; j < spec.Count[i]; j++) { Values[spec.Values[k]] = (uint)(nBits | code); code++; k++; } code <<= 1; } }
/// <summary> /// Initializes a new instance of the <see cref="HuffmanLut"/> struct. /// </summary> /// <param name="spec">dasd</param> public HuffmanLut(HuffmanSpec spec) { int maxValue = 0; foreach (byte v in spec.Values) { if (v > maxValue) { maxValue = v; } } this.Values = new int[maxValue + 1]; int code = 0; int k = 0; for (int i = 0; i < spec.Count.Length; i++) { int len = i + 1; for (int j = 0; j < spec.Count[i]; j++) { this.Values[spec.Values[k]] = len | (code << 8); code++; k++; } code <<= 1; } }
/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="componentCount">The number of components to write.</param> private void WriteDefineHuffmanTables(int componentCount) { // Table identifiers. Span <byte> headers = stackalloc byte[] { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = HuffmanSpec.TheHuffmanSpecs; if (componentCount == 1) { // Drop the Chrominance tables. specs = new[] { HuffmanSpec.TheHuffmanSpecs[0], HuffmanSpec.TheHuffmanSpecs[1] }; } for (int i = 0; i < specs.Length; i++) { ref HuffmanSpec s = ref specs[i]; markerlen += 1 + 16 + s.Values.Length; } this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++) { ref HuffmanSpec spec = ref specs[i]; int len = 0; fixed(byte *huffman = this.huffmanBuffer) fixed(byte *count = spec.Count) fixed(byte *values = spec.Values) { huffman[len++] = headers[i]; for (int c = 0; c < spec.Count.Length; c++) { huffman[len++] = count[c]; } for (int v = 0; v < spec.Values.Length; v++) { huffman[len++] = values[v]; } } this.outputStream.Write(this.huffmanBuffer, 0, len); }
private void WriteDht(int nComponent) { byte[] headers = {0x00, 0x10, 0x01, 0x11}; int markerlen = 2; HuffmanSpec[] specs = theHuffmanSpec; if (nComponent == 1){ specs = new[]{theHuffmanSpec[0], theHuffmanSpec[1]}; } foreach (var s in specs){ markerlen += 1 + 16 + s.values.Length; } WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++){ HuffmanSpec spec = specs[i]; WriteByte(headers[i]); outputStream.Write(spec.count, 0, spec.count.Length); outputStream.Write(spec.values, 0, spec.values.Length); } }
/// <summary> /// Writes the Define Huffman Table marker and tables. /// </summary> /// <param name="componentCount">The number of components to write.</param> private void WriteDefineHuffmanTables(int componentCount) { // Table identifiers. byte[] headers = { 0x00, 0x10, 0x01, 0x11 }; int markerlen = 2; HuffmanSpec[] specs = HuffmanSpec.TheHuffmanSpecs; if (componentCount == 1) { // Drop the Chrominance tables. specs = new[] { HuffmanSpec.TheHuffmanSpecs[0], HuffmanSpec.TheHuffmanSpecs[1] }; } for (int i = 0; i < specs.Length; i++) { ref HuffmanSpec s = ref specs[i]; markerlen += 1 + 16 + s.Values.Length; }
public HuffmanLut(HuffmanSpec s) { int maxValue = 0; foreach (var v in s.values){ if (v > maxValue){ maxValue = v; } } values = new uint[maxValue + 1]; int code = 0; int k = 0; for (int i = 0; i < s.count.Length; i++){ int nBits = (i + 1) << 24; for (int j = 0; j < s.count[i]; j++){ values[s.values[k]] = (uint) (nBits | code); code++; k++; } code <<= 1; } }