Browse Source

More encoder cleanup.

Former-commit-id: fe7074b76c3bb682667c1f00077a87373cf15b4b
Former-commit-id: 875477a03fa94ab5c72ab2ebd4fc8303a36403fe
Former-commit-id: b06f1d713c57778d7d6d29dd2e6000becc5ff929
pull/1/head
James Jackson-South 10 years ago
parent
commit
46254f2a62
  1. 518
      src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs

518
src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs

@ -9,35 +9,12 @@ namespace ImageProcessorCore.Formats
internal class JpegEncoderCore internal class JpegEncoderCore
{ {
// "APPlication specific" markers aren't part of the JPEG spec per se, /// <summary>
// but in practice, their use is described at /// Maps from the zig-zag ordering to the natural ordering. For example,
// http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html /// unzig[3] is the column and row of the fourth element in zig-zag order. The
private const int app0Marker = 0xe0; /// value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
/// </summary>
private const int app14Marker = 0xee; private static readonly int[] Unzig =
private const int app15Marker = 0xef;
// bitCount counts the number of bits needed to hold an integer.
private readonly byte[] bitCount =
{
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8,
};
// unzig maps from the zig-zag ordering to the natural ordering. For example,
// unzig[3] is the column and row of the fourth element in zig-zag order. The
// value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
private static readonly int[] unzig = new[]
{ {
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26,
33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57,
@ -45,165 +22,154 @@ namespace ImageProcessorCore.Formats
39, 46, 53, 60, 61, 54, 47, 55, 62, 63, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
}; };
private const int nQuantIndex = 2; private const int NQuantIndex = 2;
private const int nHuffIndex = 4; /// <summary>
/// Counts the number of bits needed to hold an integer.
private enum quantIndex /// </summary>
{ private readonly byte[] bitCount =
quantIndexLuminance = 0,
quantIndexChrominance = 1,
}
private enum huffIndex
{ {
huffIndexLuminanceDC = 0, 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
huffIndexLuminanceAC = 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
huffIndexChrominanceDC = 2, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
huffIndexChrominanceAC = 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
} 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8,
};
// unscaledQuant are the unscaled quantization tables in zig-zag order. Each /// <summary>
// encoder copies and scales the tables according to its quality parameter. /// The unscaled quantization tables in zig-zag order. Each
// The values are derived from section K.1 after converting from natural to /// encoder copies and scales the tables according to its quality parameter.
// zig-zag order. /// The values are derived from section K.1 after converting from natural to
private byte[,] unscaledQuant = new byte[,] /// zig-zag order.
{ /// </summary>
{ private readonly byte[,] unscaledQuant = {
// Luminance. {
16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, // Luminance.
26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40,
56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51,
87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81,
101, 103, 99, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92,
}, 101, 103, 99,
{ },
// Chrominance.
17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
},
};
private class huffmanSpec
{
public huffmanSpec(byte[] c, byte[] v)
{ {
this.count = c; // Chrominance.
this.values = v; 17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
} }
};
public byte[] count; /// <summary>
/// The Huffman encoding specifications.
public byte[] values; /// This encoder uses the same Huffman encoding for all images.
} /// </summary>
private readonly HuffmanSpec[] theHuffmanSpec = {
// theHuffmanSpec is the Huffman encoding specifications.
// This encoder uses the same Huffman encoding for all images.
private huffmanSpec[] theHuffmanSpec = new[]
{
// Luminance DC. // Luminance DC.
new huffmanSpec( new HuffmanSpec(
new byte[] new byte[]
{ {
0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
}, },
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }), new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }),
new huffmanSpec( new HuffmanSpec(
new byte[] new byte[]
{ {
0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125
}, },
new byte[] new byte[]
{ {
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21,
0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1,
0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5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HuffmanSpec(
new byte[] new byte[]
{ {
0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
}, },
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }), new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }),
// Chrominance AC. // Chrominance AC.
new huffmanSpec( new HuffmanSpec(
new byte[] new byte[]
{ {
0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119
}, },
new byte[] new byte[]
{ {
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31,
0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,
0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1,
0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36,
0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46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 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
}) })
}; };
// huffmanLUT is a compiled look-up table representation of a huffmanSpec. /// <summary>
// Each value maps to a uint32 of which the 8 most significant bits hold the /// A compiled look-up table representation of a huffmanSpec.
// codeword size in bits and the 24 least significant bits hold the codeword. /// Each value maps to a uint32 of which the 8 most significant bits hold the
// The maximum codeword size is 16 bits. /// codeword size in bits and the 24 least significant bits hold the codeword.
private class huffmanLUT /// The maximum codeword size is 16 bits.
/// </summary>
private class HuffmanLut
{ {
public uint[] values; public readonly uint[] Values;
public huffmanLUT(huffmanSpec s) public HuffmanLut(HuffmanSpec s)
{ {
int maxValue = 0; int maxValue = 0;
foreach (var v in s.values) foreach (var v in s.Values)
{ {
if (v > maxValue) maxValue = v; if (v > maxValue) maxValue = v;
} }
this.values = new uint[maxValue + 1]; this.Values = new uint[maxValue + 1];
int code = 0; int code = 0;
int k = 0; int k = 0;
for (int i = 0; i < s.count.Length; i++) for (int i = 0; i < s.Count.Length; i++)
{ {
int nBits = (i + 1) << 24; int nBits = (i + 1) << 24;
for (int j = 0; j < s.count[i]; j++) for (int j = 0; j < s.Count[i]; j++)
{ {
this.values[s.values[k]] = (uint)(nBits | code); this.Values[s.Values[k]] = (uint)(nBits | code);
code++; code++;
k++; k++;
} }
@ -217,32 +183,52 @@ namespace ImageProcessorCore.Formats
// writing. All attempted writes after the first error become no-ops. // writing. All attempted writes after the first error become no-ops.
private Stream outputStream; private Stream outputStream;
// buf is a scratch buffer. /// <summary>
private byte[] buffer = new byte[16]; /// A scratch buffer to reduce allocations.
/// </summary>
private readonly byte[] buffer = new byte[16];
// bits and nBits are accumulated bits to write to w. /// <summary>
/// The accumulated bits to write to the stream.
/// </summary>
private uint bits; private uint bits;
/// <summary>
/// The accumulated bits to write to the stream.
/// </summary>
private uint nBits; private uint nBits;
// quant is the scaled quantization tables, in zig-zag order. /// <summary>
private byte[][] quant = new byte[nQuantIndex][]; // [Block.blockSize]; /// The scaled quantization tables, in zig-zag order.
/// </summary>
private readonly byte[][] quant = new byte[NQuantIndex][]; // [Block.blockSize];
// theHuffmanLUT are compiled representations of theHuffmanSpec. // The compiled representations of theHuffmanSpec.
private huffmanLUT[] theHuffmanLUT = new huffmanLUT[4]; private readonly HuffmanLut[] theHuffmanLUT = new HuffmanLut[4];
/// <summary>
/// The subsampling method to use.
/// </summary>
private JpegSubsample subsample; private JpegSubsample subsample;
private void writeByte(byte b) /// <summary>
/// Writes the given byte to the stream.
/// </summary>
/// <param name="b"></param>
private void WriteByte(byte b)
{ {
var data = new byte[1]; var data = new byte[1];
data[0] = b; data[0] = b;
this.outputStream.Write(data, 0, 1); this.outputStream.Write(data, 0, 1);
} }
// emit emits the least significant nBits bits of bits to the bit-stream. /// <summary>
// The precondition is bits < 1<<nBits && nBits <= 16. /// Emits the least significant nBits bits of bits to the bit-stream.
private void emit(uint bits, uint nBits) /// The precondition is bits <example>&lt; 1&lt;&lt;nBits &amp;&amp; nBits &lt;= 16</example>.
/// </summary>
/// <param name="bits"></param>
/// <param name="nBits"></param>
private void Emit(uint bits, uint nBits)
{ {
nBits += this.nBits; nBits += this.nBits;
bits <<= (int)(32 - nBits); bits <<= (int)(32 - nBits);
@ -250,8 +236,8 @@ namespace ImageProcessorCore.Formats
while (nBits >= 8) while (nBits >= 8)
{ {
byte b = (byte)(bits >> 24); byte b = (byte)(bits >> 24);
this.writeByte(b); this.WriteByte(b);
if (b == 0xff) this.writeByte(0x00); if (b == 0xff) this.WriteByte(0x00);
bits <<= 8; bits <<= 8;
nBits -= 8; nBits -= 8;
} }
@ -260,52 +246,75 @@ namespace ImageProcessorCore.Formats
this.nBits = nBits; this.nBits = nBits;
} }
// emitHuff emits the given value with the given Huffman encoder. /// <summary>
private void emitHuff(huffIndex h, int v) /// Emits the given value with the given Huffman encoder.
/// </summary>
/// <param name="index">The index of the Huffman encoder</param>
/// <param name="value">The value to encode.</param>
private void EmitHuff(HuffIndex index, int value)
{ {
uint x = this.theHuffmanLUT[(int)h].values[v]; uint x = this.theHuffmanLUT[(int)index].Values[value];
this.emit(x & ((1 << 24) - 1), x >> 24); this.Emit(x & ((1 << 24) - 1), x >> 24);
} }
// emitHuffRLE emits a run of runLength copies of value encoded with the given /// <summary>
// Huffman encoder. /// Emits a run of runLength copies of value encoded with the given Huffman encoder.
private void emitHuffRLE(huffIndex h, int runLength, int v) /// </summary>
/// <param name="index">The index of the Huffman encoder</param>
/// <param name="runLength">The number of copies to encode.</param>
/// <param name="value">The value to encode.</param>
private void EmitHuffRLE(HuffIndex index, int runLength, int value)
{ {
int a = v; int a = value;
int b = v; int b = value;
if (a < 0) if (a < 0)
{ {
a = -v; a = -value;
b = v - 1; b = value - 1;
} }
uint nBits = 0; uint bt;
if (a < 0x100) nBits = this.bitCount[a]; if (a < 0x100)
else nBits = 8 + (uint)this.bitCount[a >> 8]; {
bt = this.bitCount[a];
}
else
{
bt = 8 + (uint)this.bitCount[a >> 8];
}
this.emitHuff(h, (int)((uint)(runLength << 4) | nBits)); this.EmitHuff(index, (int)((uint)(runLength << 4) | bt));
if (nBits > 0) this.emit((uint)b & (uint)((1 << ((int)nBits)) - 1), nBits); if (bt > 0)
{
this.Emit((uint)b & (uint)((1 << ((int)bt)) - 1), bt);
}
} }
// writeBlock writes a block of pixel data using the given quantization table, /// <summary>
// returning the post-quantized DC value of the DCT-transformed block. b is in /// Writes a block of pixel data using the given quantization table,
// natural (not zig-zag) order. /// returning the post-quantized DC value of the DCT-transformed block.
private int writeBlock(Block b, quantIndex q, int prevDC) /// The block is in natural (not zig-zag) order.
/// </summary>
/// <param name="block">The block to write.</param>
/// <param name="index">The quantization table index.</param>
/// <param name="prevDC">The previous DC value.</param>
/// <returns></returns>
private int WriteBlock(Block block, QuantIndex index, int prevDC)
{ {
FDCT.Transform(b); FDCT.Transform(block);
// Emit the DC delta. // Emit the DC delta.
int dc = Round(b[0], 8 * this.quant[(int)q][0]); int dc = Round(block[0], 8 * this.quant[(int)index][0]);
this.emitHuffRLE((huffIndex)(2 * (int)q + 0), 0, dc - prevDC); this.EmitHuffRLE((HuffIndex)(2 * (int)index + 0), 0, dc - prevDC);
// Emit the AC components. // Emit the AC components.
var h = (huffIndex)(2 * (int)q + 1); var h = (HuffIndex)(2 * (int)index + 1);
int runLength = 0; int runLength = 0;
for (int zig = 1; zig < Block.BlockSize; zig++) for (int zig = 1; zig < Block.BlockSize; zig++)
{ {
int ac = Round(b[unzig[zig]], 8 * this.quant[(int)q][zig]); int ac = Round(block[Unzig[zig]], 8 * this.quant[(int)index][zig]);
if (ac == 0) if (ac == 0)
{ {
@ -315,22 +324,22 @@ namespace ImageProcessorCore.Formats
{ {
while (runLength > 15) while (runLength > 15)
{ {
this.emitHuff(h, 0xf0); this.EmitHuff(h, 0xf0);
runLength -= 16; runLength -= 16;
} }
this.emitHuffRLE(h, runLength, ac); this.EmitHuffRLE(h, runLength, ac);
runLength = 0; runLength = 0;
} }
} }
if (runLength > 0) this.emitHuff(h, 0x00); if (runLength > 0) this.EmitHuff(h, 0x00);
return dc; return dc;
} }
// toYCbCr converts the 8x8 region of m whose top-left corner is p to its // toYCbCr converts the 8x8 region of m whose top-left corner is p to its
// YCbCr values. // YCbCr values.
private void toYCbCr(PixelAccessor pixels, int x, int y, Block yBlock, Block cbBlock, Block crBlock) private void ToYCbCr(PixelAccessor pixels, int x, int y, Block yBlock, Block cbBlock, Block crBlock)
{ {
int xmax = pixels.Width - 1; int xmax = pixels.Width - 1;
int ymax = pixels.Height - 1; int ymax = pixels.Height - 1;
@ -347,9 +356,13 @@ namespace ImageProcessorCore.Formats
} }
} }
// scale scales the 16x16 region represented by the 4 src blocks to the 8x8 /// <summary>
// dst block. /// Scales the 16x16 region represented by the 4 src blocks to the 8x8
private void scale_16x16_8x8(Block dst, Block[] src) /// dst block.
/// </summary>
/// <param name="destination">The destination block array</param>
/// <param name="source">The source block array.</param>
private void Scale16X16_8X8(Block destination, Block[] source)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
@ -359,8 +372,8 @@ namespace ImageProcessorCore.Formats
for (int x = 0; x < 4; x++) for (int x = 0; x < 4; x++)
{ {
int j = 16 * y + 2 * x; int j = 16 * y + 2 * x;
int sum = src[i][j] + src[i][j + 1] + src[i][j + 8] + src[i][j + 9]; int sum = source[i][j] + source[i][j + 1] + source[i][j + 8] + source[i][j + 9];
dst[8 * y + x + dstOff] = (sum + 2) / 4; destination[8 * y + x + dstOff] = (sum + 2) / 4;
} }
} }
} }
@ -429,10 +442,10 @@ namespace ImageProcessorCore.Formats
// TODO: This should be static should it not? // TODO: This should be static should it not?
for (int i = 0; i < this.theHuffmanSpec.Length; i++) for (int i = 0; i < this.theHuffmanSpec.Length; i++)
{ {
this.theHuffmanLUT[i] = new huffmanLUT(this.theHuffmanSpec[i]); this.theHuffmanLUT[i] = new HuffmanLut(this.theHuffmanSpec[i]);
} }
for (int i = 0; i < nQuantIndex; i++) for (int i = 0; i < NQuantIndex; i++)
{ {
this.quant[i] = new byte[Block.BlockSize]; this.quant[i] = new byte[Block.BlockSize];
} }
@ -452,7 +465,7 @@ namespace ImageProcessorCore.Formats
} }
// Initialize the quantization tables. // Initialize the quantization tables.
for (int i = 0; i < nQuantIndex; i++) for (int i = 0; i < NQuantIndex; i++)
{ {
for (int j = 0; j < Block.BlockSize; j++) for (int j = 0; j < Block.BlockSize; j++)
{ {
@ -516,11 +529,11 @@ namespace ImageProcessorCore.Formats
/// </summary> /// </summary>
private void WriteDQT() private void WriteDQT()
{ {
int markerlen = 2 + nQuantIndex * (1 + Block.BlockSize); int markerlen = 2 + NQuantIndex * (1 + Block.BlockSize);
this.WriteMarkerHeader(JpegConstants.Markers.DQT, markerlen); this.WriteMarkerHeader(JpegConstants.Markers.DQT, markerlen);
for (int i = 0; i < nQuantIndex; i++) for (int i = 0; i < NQuantIndex; i++)
{ {
this.writeByte((byte)i); this.WriteByte((byte)i);
this.outputStream.Write(this.quant[i], 0, this.quant[i].Length); this.outputStream.Write(this.quant[i], 0, this.quant[i].Length);
} }
} }
@ -587,7 +600,7 @@ namespace ImageProcessorCore.Formats
{ {
byte[] headers = { 0x00, 0x10, 0x01, 0x11 }; byte[] headers = { 0x00, 0x10, 0x01, 0x11 };
int markerlen = 2; int markerlen = 2;
huffmanSpec[] specs = this.theHuffmanSpec; HuffmanSpec[] specs = this.theHuffmanSpec;
if (nComponent == 1) if (nComponent == 1)
{ {
@ -597,17 +610,17 @@ namespace ImageProcessorCore.Formats
foreach (var s in specs) foreach (var s in specs)
{ {
markerlen += 1 + 16 + s.values.Length; markerlen += 1 + 16 + s.Values.Length;
} }
this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen);
for (int i = 0; i < specs.Length; i++) for (int i = 0; i < specs.Length; i++)
{ {
huffmanSpec spec = specs[i]; HuffmanSpec spec = specs[i];
this.writeByte(headers[i]); this.WriteByte(headers[i]);
this.outputStream.Write(spec.count, 0, spec.count.Length); this.outputStream.Write(spec.Count, 0, spec.Count.Length);
this.outputStream.Write(spec.values, 0, spec.values.Length); this.outputStream.Write(spec.Values, 0, spec.Values.Length);
} }
} }
@ -631,9 +644,11 @@ namespace ImageProcessorCore.Formats
} }
// Pad the last byte with 1's. // Pad the last byte with 1's.
this.emit(0x7f, 7); this.Emit(0x7f, 7);
} }
/// <summary> /// <summary>
/// Encodes the image with no subsampling. /// Encodes the image with no subsampling.
/// </summary> /// </summary>
@ -649,10 +664,10 @@ namespace ImageProcessorCore.Formats
{ {
for (int x = 0; x < pixels.Width; x += 8) for (int x = 0; x < pixels.Width; x += 8)
{ {
this.toYCbCr(pixels, x, y, b, cb, cr); this.ToYCbCr(pixels, x, y, b, cb, cr);
prevDCY = this.writeBlock(b, (quantIndex)0, prevDCY); prevDCY = this.WriteBlock(b, QuantIndex.Luminance, prevDCY);
prevDCCb = this.writeBlock(cb, (quantIndex)1, prevDCCb); prevDCCb = this.WriteBlock(cb, QuantIndex.Chrominance, prevDCCb);
prevDCCr = this.writeBlock(cr, (quantIndex)1, prevDCCr); prevDCCr = this.WriteBlock(cr, QuantIndex.Chrominance, prevDCCr);
} }
} }
} }
@ -681,14 +696,14 @@ namespace ImageProcessorCore.Formats
int xOff = (i & 1) * 8; int xOff = (i & 1) * 8;
int yOff = (i & 2) * 4; int yOff = (i & 2) * 4;
this.toYCbCr(pixels, x + xOff, y + yOff, b, cb[i], cr[i]); this.ToYCbCr(pixels, x + xOff, y + yOff, b, cb[i], cr[i]);
prevDCY = this.writeBlock(b, (quantIndex)0, prevDCY); prevDCY = this.WriteBlock(b, QuantIndex.Luminance, prevDCY);
} }
this.scale_16x16_8x8(b, cb); this.Scale16X16_8X8(b, cb);
prevDCCb = this.writeBlock(b, (quantIndex)1, prevDCCb); prevDCCb = this.WriteBlock(b, QuantIndex.Chrominance, prevDCCb);
this.scale_16x16_8x8(b, cr); this.Scale16X16_8X8(b, cr);
prevDCCr = this.writeBlock(b, (quantIndex)1, prevDCCr); prevDCCr = this.WriteBlock(b, QuantIndex.Chrominance, prevDCCr);
} }
} }
} }
@ -707,5 +722,62 @@ namespace ImageProcessorCore.Formats
this.buffer[3] = (byte)(length & 0xff); this.buffer[3] = (byte)(length & 0xff);
this.outputStream.Write(this.buffer, 0, 4); this.outputStream.Write(this.buffer, 0, 4);
} }
/// <summary>
/// Enumerates the Huffman tables
/// </summary>
private enum HuffIndex
{
LuminanceDC = 0,
LuminanceAC = 1,
ChrominanceDC = 2,
ChrominanceAC = 3,
}
/// <summary>
/// Enumerates the quantization tables
/// </summary>
private enum QuantIndex
{
/// <summary>
/// Luminance
/// </summary>
Luminance = 0,
/// <summary>
/// Chrominance
/// </summary>
Chrominance = 1,
}
/// <summary>
/// The Huffman encoding specifications.
/// </summary>
private struct HuffmanSpec
{
/// <summary>
/// Initializes a n ew instance of the <see cref="HuffmanSpec"/> struct.
/// </summary>
/// <param name="count">The number of codes.</param>
/// <param name="values">The decoded values.</param>
public HuffmanSpec(byte[] count, byte[] values)
{
this.Count = count;
this.Values = values;
}
/// <summary>
/// Gets count[i] - The number of codes of length i bits.
/// </summary>
public readonly byte[] Count;
/// <summary>
/// Gets value[i] - The decoded value of the i'th codeword.
/// </summary>
public readonly byte[] Values;
}
} }
} }

Loading…
Cancel
Save