Browse Source

Use qualifier

Former-commit-id: e7f5254e6a05eef6e2c41a7113ec43117419f3ad
Former-commit-id: d8fb7b19aa30b69f2c83f6e5e93a965f0e95a9ae
Former-commit-id: 07ec0c3ba4120079b88203fbc98e3d73df7080bf
af/merge-core
James Jackson-South 10 years ago
parent
commit
dda1420780
  1. 485
      src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs

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

@ -2,7 +2,6 @@
// Copyright (c) James Jackson-South and contributors. // Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
namespace ImageProcessorCore.Formats namespace ImageProcessorCore.Formats
{ {
using System; using System;
@ -11,173 +10,194 @@ namespace ImageProcessorCore.Formats
internal class JpegEncoderCore internal class JpegEncoderCore
{ {
private const int sof0Marker = 0xc0; // Start Of Frame (Baseline). private const int sof0Marker = 0xc0; // Start Of Frame (Baseline).
private const int sof1Marker = 0xc1; // Start Of Frame (Extended Sequential). private const int sof1Marker = 0xc1; // Start Of Frame (Extended Sequential).
private const int sof2Marker = 0xc2; // Start Of Frame (Progressive). private const int sof2Marker = 0xc2; // Start Of Frame (Progressive).
private const int dhtMarker = 0xc4; // Define Huffman Table. private const int dhtMarker = 0xc4; // Define Huffman Table.
private const int rst0Marker = 0xd0; // ReSTart (0). private const int rst0Marker = 0xd0; // ReSTart (0).
private const int rst7Marker = 0xd7; // ReSTart (7). private const int rst7Marker = 0xd7; // ReSTart (7).
private const int soiMarker = 0xd8; // Start Of Image. private const int soiMarker = 0xd8; // Start Of Image.
private const int eoiMarker = 0xd9; // End Of Image. private const int eoiMarker = 0xd9; // End Of Image.
private const int sosMarker = 0xda; // Start Of Scan. private const int sosMarker = 0xda; // Start Of Scan.
private const int dqtMarker = 0xdb; // Define Quantization Table. private const int dqtMarker = 0xdb; // Define Quantization Table.
private const int driMarker = 0xdd; // Define Restart Interval. private const int driMarker = 0xdd; // Define Restart Interval.
private const int comMarker = 0xfe; // COMment. private const int comMarker = 0xfe; // COMment.
// "APPlication specific" markers aren't part of the JPEG spec per se, // "APPlication specific" markers aren't part of the JPEG spec per se,
// but in practice, their use is described at // but in practice, their use is described at
// http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html
private const int app0Marker = 0xe0; private const int app0Marker = 0xe0;
private const int app14Marker = 0xee; private const int app14Marker = 0xee;
private const int app15Marker = 0xef; private const int app15Marker = 0xef;
// bitCount counts the number of bits needed to hold an integer. // bitCount counts the number of bits needed to hold an integer.
private readonly byte[] bitCount = { 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, 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,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 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, 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, 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,
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, 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 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 // 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). // value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
private static readonly int[] unzig = new int[] { private static readonly int[] unzig = new[]
0, 1, 8, 16, 9, 2, 3, 10, {
17, 24, 32, 25, 18, 11, 4, 5, 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26,
12, 19, 26, 33, 40, 48, 41, 34, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57,
27, 20, 13, 6, 7, 14, 21, 28, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31,
35, 42, 49, 56, 57, 50, 43, 36, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
29, 22, 15, 23, 30, 37, 44, 51, };
58, 59, 52, 45, 38, 31, 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; private const int nHuffIndex = 4;
private enum quantIndex private enum quantIndex
{ {
quantIndexLuminance = 0, quantIndexLuminance = 0,
quantIndexChrominance = 1,
quantIndexChrominance = 1,
} }
private enum huffIndex private enum huffIndex
{ {
huffIndexLuminanceDC = 0, huffIndexLuminanceDC = 0,
huffIndexLuminanceAC = 1,
huffIndexChrominanceDC = 2, huffIndexLuminanceAC = 1,
huffIndexChrominanceAC = 3,
huffIndexChrominanceDC = 2,
huffIndexChrominanceAC = 3,
} }
// unscaledQuant are the unscaled quantization tables in zig-zag order. Each // unscaledQuant are the unscaled quantization tables in zig-zag order. Each
// encoder copies and scales the tables according to its quality parameter. // encoder copies and scales the tables according to its quality parameter.
// The values are derived from section K.1 after converting from natural to // The values are derived from section K.1 after converting from natural to
// zig-zag order. // zig-zag order.
private byte[,] unscaledQuant = new byte[,] { private byte[,] unscaledQuant = new byte[,]
// Luminance. {
{ {
16, 11, 12, 14, 12, 10, 16, 14, // Luminance.
13, 14, 18, 17, 16, 19, 24, 40, 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40,
26, 24, 22, 22, 24, 49, 35, 37, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51,
29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81,
56, 55, 64, 72, 92, 78, 64, 68, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92,
87, 69, 55, 56, 80, 109, 81, 87, 101, 103, 99,
95, 98, 103, 104, 103, 62, 77, 113, },
121, 112, 100, 120, 92, 101, 103, 99, {
}, // Chrominance.
// 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,
17, 18, 18, 24, 21, 24, 47, 26, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
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,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
},
};
private class huffmanSpec private class huffmanSpec
{ {
public huffmanSpec(byte[] c, byte[] v) { count = c; values = v; } public huffmanSpec(byte[] c, byte[] v)
{
this.count = c;
this.values = v;
}
public byte[] count; public byte[] count;
public byte[] values; public byte[] values;
} }
// theHuffmanSpec is the Huffman encoding specifications. // theHuffmanSpec is the Huffman encoding specifications.
// This encoder uses the same Huffman encoding for all images. // This encoder uses the same Huffman encoding for all images.
private huffmanSpec[] theHuffmanSpec = new huffmanSpec[] { private huffmanSpec[] theHuffmanSpec = new[]
{
// Luminance DC. // Luminance DC.
new huffmanSpec( new huffmanSpec(
new byte[] { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, new byte[]
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }), {
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 huffmanSpec( new huffmanSpec(
new byte[] { 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, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, },
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, new byte[]
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, {
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x26, 0x27, 0x28,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8, 0xe9, 0xea, 0xf1,
0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
}),
new huffmanSpec( new huffmanSpec(
new byte[] { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, new byte[]
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }), {
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 }),
// Chrominance AC. // Chrominance AC.
new huffmanSpec( new huffmanSpec(
new byte[] { 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, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, },
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, new byte[]
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, {
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x0a, 0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0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xf9, 0xfa, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
}) 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
})
}; };
// huffmanLUT is a compiled look-up table representation of a huffmanSpec. // huffmanLUT is a compiled look-up table representation of a huffmanSpec.
@ -194,11 +214,10 @@ namespace ImageProcessorCore.Formats
foreach (var v in s.values) foreach (var v in s.values)
{ {
if (v > maxValue) if (v > maxValue) maxValue = v;
maxValue = v;
} }
values = new uint[maxValue + 1]; this.values = new uint[maxValue + 1];
int code = 0; int code = 0;
int k = 0; int k = 0;
@ -208,10 +227,11 @@ namespace ImageProcessorCore.Formats
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++)
{ {
values[s.values[k]] = (uint)(nBits | code); this.values[s.values[k]] = (uint)(nBits | code);
code++; code++;
k++; k++;
} }
code <<= 1; code <<= 1;
} }
} }
@ -220,21 +240,28 @@ namespace ImageProcessorCore.Formats
// w is the writer to write to. err is the first error encountered during // w is the writer to write to. err is the first error encountered during
// 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. // buf is a scratch buffer.
private byte[] buf = new byte[16]; private byte[] buf = new byte[16];
// bits and nBits are accumulated bits to write to w. // bits and nBits are accumulated bits to write to w.
private uint bits, nBits; private uint bits;
private uint nBits;
// quant is the scaled quantization tables, in zig-zag order. // quant is the scaled quantization tables, in zig-zag order.
private byte[][] quant = new byte[nQuantIndex][];//[Block.blockSize]; private byte[][] quant = new byte[nQuantIndex][]; // [Block.blockSize];
// theHuffmanLUT are compiled representations of theHuffmanSpec. // theHuffmanLUT are compiled representations of theHuffmanSpec.
private huffmanLUT[] theHuffmanLUT = new huffmanLUT[4]; private huffmanLUT[] theHuffmanLUT = new huffmanLUT[4];
private JpegSubsample subsample; private JpegSubsample subsample;
private void writeByte(byte b) private void writeByte(byte b)
{ {
var data = new byte[1]; var data = new byte[1];
data[0] = b; data[0] = b;
outputStream.Write(data, 0, 1); this.outputStream.Write(data, 0, 1);
} }
// emit emits the least significant nBits bits of bits to the bit-stream. // emit emits the least significant nBits bits of bits to the bit-stream.
@ -247,12 +274,12 @@ namespace ImageProcessorCore.Formats
while (nBits >= 8) while (nBits >= 8)
{ {
byte b = (byte)(bits >> 24); byte b = (byte)(bits >> 24);
writeByte(b); this.writeByte(b);
if (b == 0xff) if (b == 0xff) this.writeByte(0x00);
writeByte(0x00);
bits <<= 8; bits <<= 8;
nBits -= 8; nBits -= 8;
} }
this.bits = bits; this.bits = bits;
this.nBits = nBits; this.nBits = nBits;
} }
@ -260,8 +287,8 @@ namespace ImageProcessorCore.Formats
// emitHuff emits the given value with the given Huffman encoder. // emitHuff emits the given value with the given Huffman encoder.
private void emitHuff(huffIndex h, int v) private void emitHuff(huffIndex h, int v)
{ {
uint x = theHuffmanLUT[(int)h].values[v]; uint x = this.theHuffmanLUT[(int)h].values[v];
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 // emitHuffRLE emits a run of runLength copies of value encoded with the given
@ -275,46 +302,45 @@ namespace ImageProcessorCore.Formats
a = -v; a = -v;
b = v - 1; b = v - 1;
} }
uint nBits = 0; uint nBits = 0;
if (a < 0x100) if (a < 0x100) nBits = this.bitCount[a];
nBits = bitCount[a]; else nBits = 8 + (uint)this.bitCount[a >> 8];
else
nBits = 8 + (uint)bitCount[a >> 8];
emitHuff(h, (int)((uint)(runLength << 4) | nBits)); this.emitHuff(h, (int)((uint)(runLength << 4) | nBits));
if (nBits > 0) emit((uint)b & (uint)((1 << ((int)nBits)) - 1), nBits); if (nBits > 0) this.emit((uint)b & (uint)((1 << ((int)nBits)) - 1), nBits);
} }
// writeMarkerHeader writes the header for a marker with the given length. // writeMarkerHeader writes the header for a marker with the given length.
private void writeMarkerHeader(byte marker, int markerlen) private void writeMarkerHeader(byte marker, int markerlen)
{ {
buf[0] = 0xff; this.buf[0] = 0xff;
buf[1] = marker; this.buf[1] = marker;
buf[2] = (byte)(markerlen >> 8); this.buf[2] = (byte)(markerlen >> 8);
buf[3] = (byte)(markerlen & 0xff); this.buf[3] = (byte)(markerlen & 0xff);
outputStream.Write(buf, 0, 4); this.outputStream.Write(this.buf, 0, 4);
} }
// writeDQT writes the Define Quantization Table marker. // writeDQT writes the Define Quantization Table marker.
private void writeDQT() private void writeDQT()
{ {
int markerlen = 2 + nQuantIndex * (1 + Block.BlockSize); int markerlen = 2 + nQuantIndex * (1 + Block.BlockSize);
writeMarkerHeader(dqtMarker, markerlen); this.writeMarkerHeader(dqtMarker, markerlen);
for (int i = 0; i < nQuantIndex; i++) for (int i = 0; i < nQuantIndex; i++)
{ {
writeByte((byte)i); this.writeByte((byte)i);
outputStream.Write(quant[i], 0, quant[i].Length); this.outputStream.Write(this.quant[i], 0, this.quant[i].Length);
} }
} }
// writeSOF0 writes the Start Of Frame (Baseline) marker. // writeSOF0 writes the Start Of Frame (Baseline) marker.
private void writeSOF0(int wid, int hei, int nComponent) private void writeSOF0(int wid, int hei, int nComponent)
{ {
//"default" to 4:2:0 // "default" to 4:2:0
byte[] subsamples = new byte[] { 0x22, 0x11, 0x11 }; byte[] subsamples = { 0x22, 0x11, 0x11 };
byte[] chroma = new byte[] { 0x00, 0x01, 0x01 }; byte[] chroma = { 0x00, 0x01, 0x01 };
switch (subsample) switch (this.subsample)
{ {
case JpegSubsample.Ratio444: case JpegSubsample.Ratio444:
subsamples = new byte[] { 0x11, 0x11, 0x11 }; subsamples = new byte[] { 0x11, 0x11, 0x11 };
@ -325,31 +351,34 @@ namespace ImageProcessorCore.Formats
} }
int markerlen = 8 + 3 * nComponent; int markerlen = 8 + 3 * nComponent;
writeMarkerHeader(sof0Marker, markerlen); this.writeMarkerHeader(sof0Marker, markerlen);
buf[0] = 8; // 8-bit color. this.buf[0] = 8; // 8-bit color.
buf[1] = (byte)(hei >> 8); this.buf[1] = (byte)(hei >> 8);
buf[2] = (byte)(hei & 0xff); this.buf[2] = (byte)(hei & 0xff);
buf[3] = (byte)(wid >> 8); this.buf[3] = (byte)(wid >> 8);
buf[4] = (byte)(wid & 0xff); this.buf[4] = (byte)(wid & 0xff);
buf[5] = (byte)(nComponent); this.buf[5] = (byte)nComponent;
if (nComponent == 1) if (nComponent == 1)
{ {
buf[6] = 1; this.buf[6] = 1;
// No subsampling for grayscale image. // No subsampling for grayscale image.
buf[7] = 0x11; this.buf[7] = 0x11;
buf[8] = 0x00; this.buf[8] = 0x00;
} }
else else
{ {
for (int i = 0; i < nComponent; i++) for (int i = 0; i < nComponent; i++)
{ {
buf[3 * i + 6] = (byte)(i + 1); this.buf[3 * i + 6] = (byte)(i + 1);
// We use 4:2:0 chroma subsampling. // We use 4:2:0 chroma subsampling.
buf[3 * i + 7] = subsamples[i]; this.buf[3 * i + 7] = subsamples[i];
buf[3 * i + 8] = chroma[i]; this.buf[3 * i + 8] = chroma[i];
} }
} }
outputStream.Write(buf, 0, 3 * (nComponent - 1) + 9);
this.outputStream.Write(this.buf, 0, 3 * (nComponent - 1) + 9);
} }
// writeDHT writes the Define Huffman Table marker. // writeDHT writes the Define Huffman Table marker.
@ -357,12 +386,12 @@ namespace ImageProcessorCore.Formats
{ {
byte[] headers = new byte[] { 0x00, 0x10, 0x01, 0x11 }; byte[] headers = new byte[] { 0x00, 0x10, 0x01, 0x11 };
int markerlen = 2; int markerlen = 2;
huffmanSpec[] specs = theHuffmanSpec; huffmanSpec[] specs = this.theHuffmanSpec;
if (nComponent == 1) if (nComponent == 1)
{ {
// Drop the Chrominance tables. // Drop the Chrominance tables.
specs = new huffmanSpec[] { theHuffmanSpec[0], theHuffmanSpec[1] }; specs = new[] { this.theHuffmanSpec[0], this.theHuffmanSpec[1] };
} }
foreach (var s in specs) foreach (var s in specs)
@ -370,14 +399,14 @@ namespace ImageProcessorCore.Formats
markerlen += 1 + 16 + s.values.Length; markerlen += 1 + 16 + s.values.Length;
} }
writeMarkerHeader(dhtMarker, markerlen); this.writeMarkerHeader(dhtMarker, markerlen);
for (int i = 0; i < specs.Length; i++) for (int i = 0; i < specs.Length; i++)
{ {
var s = specs[i]; var s = specs[i];
writeByte(headers[i]); this.writeByte(headers[i]);
outputStream.Write(s.count, 0, s.count.Length); this.outputStream.Write(s.count, 0, s.count.Length);
outputStream.Write(s.values, 0, s.values.Length); this.outputStream.Write(s.values, 0, s.values.Length);
} }
} }
@ -389,8 +418,8 @@ namespace ImageProcessorCore.Formats
FDCT.Transform(b); FDCT.Transform(b);
// Emit the DC delta. // Emit the DC delta.
int dc = div(b[0], 8 * quant[(int)q][0]); int dc = div(b[0], 8 * this.quant[(int)q][0]);
emitHuffRLE((huffIndex)(2 * (int)q + 0), 0, dc - prevDC); this.emitHuffRLE((huffIndex)(2 * (int)q + 0), 0, dc - prevDC);
// Emit the AC components. // Emit the AC components.
var h = (huffIndex)(2 * (int)q + 1); var h = (huffIndex)(2 * (int)q + 1);
@ -398,7 +427,7 @@ namespace ImageProcessorCore.Formats
for (int zig = 1; zig < Block.BlockSize; zig++) for (int zig = 1; zig < Block.BlockSize; zig++)
{ {
int ac = div(b[unzig[zig]], 8 * quant[(int)q][zig]); int ac = div(b[unzig[zig]], 8 * this.quant[(int)q][zig]);
if (ac == 0) if (ac == 0)
{ {
@ -408,16 +437,16 @@ namespace ImageProcessorCore.Formats
{ {
while (runLength > 15) while (runLength > 15)
{ {
emitHuff(h, 0xf0); this.emitHuff(h, 0xf0);
runLength -= 16; runLength -= 16;
} }
emitHuffRLE(h, runLength, ac); this.emitHuffRLE(h, runLength, ac);
runLength = 0; runLength = 0;
} }
} }
if (runLength > 0)
emitHuff(h, 0x00); if (runLength > 0) this.emitHuff(h, 0x00);
return dc; return dc;
} }
@ -460,48 +489,46 @@ namespace ImageProcessorCore.Formats
} }
// sosHeaderY is the SOS marker "\xff\xda" followed by 8 bytes: // sosHeaderY is the SOS marker "\xff\xda" followed by 8 bytes:
// - the marker length "\x00\x08", // - the marker length "\x00\x08",
// - the number of components "\x01", // - the number of components "\x01",
// - component 1 uses DC table 0 and AC table 0 "\x01\x00", // - component 1 uses DC table 0 and AC table 0 "\x01\x00",
// - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for // - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for
// sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al) // sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al)
// should be 0x00, 0x3f, 0x00<<4 | 0x00. // should be 0x00, 0x3f, 0x00<<4 | 0x00.
private readonly byte[] sosHeaderY = new byte[] { private readonly byte[] sosHeaderY = new byte[] { 0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, };
0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00,
};
// sosHeaderYCbCr is the SOS marker "\xff\xda" followed by 12 bytes: // sosHeaderYCbCr is the SOS marker "\xff\xda" followed by 12 bytes:
// - the marker length "\x00\x0c", // - the marker length "\x00\x0c",
// - the number of components "\x03", // - the number of components "\x03",
// - component 1 uses DC table 0 and AC table 0 "\x01\x00", // - component 1 uses DC table 0 and AC table 0 "\x01\x00",
// - component 2 uses DC table 1 and AC table 1 "\x02\x11", // - component 2 uses DC table 1 and AC table 1 "\x02\x11",
// - component 3 uses DC table 1 and AC table 1 "\x03\x11", // - component 3 uses DC table 1 and AC table 1 "\x03\x11",
// - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for // - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for
// sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al) // sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al)
// should be 0x00, 0x3f, 0x00<<4 | 0x00. // should be 0x00, 0x3f, 0x00<<4 | 0x00.
private readonly byte[] sosHeaderYCbCr = new byte[] { private readonly byte[] sosHeaderYCbCr = new byte[]
0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, {
0x11, 0x03, 0x11, 0x00, 0x3f, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
0x00, 0x3f, 0x00,
}; };
// writeSOS writes the StartOfScan marker. // writeSOS writes the StartOfScan marker.
private void writeSOS(PixelAccessor pixels) private void writeSOS(PixelAccessor pixels)
{ {
outputStream.Write(sosHeaderYCbCr, 0, sosHeaderYCbCr.Length); this.outputStream.Write(this.sosHeaderYCbCr, 0, this.sosHeaderYCbCr.Length);
switch (subsample) switch (this.subsample)
{ {
case JpegSubsample.Ratio444: case JpegSubsample.Ratio444:
encode444(pixels); this.encode444(pixels);
break; break;
case JpegSubsample.Ratio420: case JpegSubsample.Ratio420:
encode420(pixels); this.encode420(pixels);
break; break;
} }
// Pad the last byte with 1's. // Pad the last byte with 1's.
emit(0x7f, 7); this.emit(0x7f, 7);
} }
private void encode444(PixelAccessor pixels) private void encode444(PixelAccessor pixels)
@ -515,10 +542,10 @@ namespace ImageProcessorCore.Formats
{ {
for (int x = 0; x < pixels.Width; x += 8) for (int x = 0; x < pixels.Width; x += 8)
{ {
toYCbCr(pixels, x, y, b, cb, cr); this.toYCbCr(pixels, x, y, b, cb, cr);
prevDCY = writeBlock(b, (quantIndex)0, prevDCY); prevDCY = this.writeBlock(b, (quantIndex)0, prevDCY);
prevDCCb = writeBlock(cb, (quantIndex)1, prevDCCb); prevDCCb = this.writeBlock(cb, (quantIndex)1, prevDCCb);
prevDCCr = writeBlock(cr, (quantIndex)1, prevDCCr); prevDCCr = this.writeBlock(cr, (quantIndex)1, prevDCCr);
} }
} }
} }
@ -542,37 +569,43 @@ namespace ImageProcessorCore.Formats
int xOff = (i & 1) * 8; int xOff = (i & 1) * 8;
int yOff = (i & 2) * 4; int yOff = (i & 2) * 4;
toYCbCr(pixels, x + xOff, y + yOff, b, cb[i], cr[i]); this.toYCbCr(pixels, x + xOff, y + yOff, b, cb[i], cr[i]);
prevDCY = writeBlock(b, (quantIndex)0, prevDCY); prevDCY = this.writeBlock(b, (quantIndex)0, prevDCY);
} }
scale_16x16_8x8(b, cb);
prevDCCb = writeBlock(b, (quantIndex)1, prevDCCb); this.scale_16x16_8x8(b, cb);
scale_16x16_8x8(b, cr); prevDCCb = this.writeBlock(b, (quantIndex)1, prevDCCb);
prevDCCr = writeBlock(b, (quantIndex)1, prevDCCr); this.scale_16x16_8x8(b, cr);
prevDCCr = this.writeBlock(b, (quantIndex)1, prevDCCr);
} }
} }
} }
// Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given // Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given
// options. Default parameters are used if a nil *Options is passed. // options. Default parameters are used if a nil *Options is passed.
public void Encode(Stream stream, ImageBase image, int quality, JpegSubsample subsample) public void Encode(Stream stream, ImageBase image, int quality, JpegSubsample sample)
{ {
this.outputStream = stream; Guard.NotNull(image, nameof(image));
this.subsample = subsample; Guard.NotNull(stream, nameof(stream));
for (int i = 0; i < theHuffmanSpec.Length; i++) ushort max = JpegConstants.MaxLength;
if (image.Width >= max || image.Height >= max)
{ {
theHuffmanLUT[i] = new huffmanLUT(theHuffmanSpec[i]); throw new ImageFormatException($"Image is too large to encode at {image.Width}x{image.Height}.");
} }
for (int i = 0; i < nQuantIndex; i++) this.outputStream = stream;
this.subsample = sample;
// TODO: This should be static should it not?
for (int i = 0; i < this.theHuffmanSpec.Length; i++)
{ {
quant[i] = new byte[Block.BlockSize]; this.theHuffmanLUT[i] = new huffmanLUT(this.theHuffmanSpec[i]);
} }
if (image.Width >= (1 << 16) || image.Height >= (1 << 16)) for (int i = 0; i < nQuantIndex; i++)
{ {
throw new ImageFormatException($"Image is too large to encode at {image.Width}x{image.Height}."); this.quant[i] = new byte[Block.BlockSize];
} }
if (quality < 1) quality = 1; if (quality < 1) quality = 1;
@ -594,11 +627,11 @@ namespace ImageProcessorCore.Formats
{ {
for (int j = 0; j < Block.BlockSize; j++) for (int j = 0; j < Block.BlockSize; j++)
{ {
int x = unscaledQuant[i, j]; int x = this.unscaledQuant[i, j];
x = (x * scale + 50) / 100; x = (x * scale + 50) / 100;
if (x < 1) x = 1; if (x < 1) x = 1;
if (x > 255) x = 255; if (x > 255) x = 255;
quant[i][j] = (byte)x; this.quant[i][j] = (byte)x;
} }
} }
@ -606,39 +639,37 @@ namespace ImageProcessorCore.Formats
int nComponent = 3; int nComponent = 3;
// Write the Start Of Image marker. // Write the Start Of Image marker.
buf[0] = 0xff; this.buf[0] = 0xff;
buf[1] = 0xd8; this.buf[1] = 0xd8;
stream.Write(buf, 0, 2); stream.Write(this.buf, 0, 2);
// Write the quantization tables. // Write the quantization tables.
writeDQT(); this.writeDQT();
// Write the image dimensions. // Write the image dimensions.
writeSOF0(image.Width, image.Height, nComponent); this.writeSOF0(image.Width, image.Height, nComponent);
// Write the Huffman tables. // Write the Huffman tables.
writeDHT(nComponent); this.writeDHT(nComponent);
// Write the image data. // Write the image data.
using (PixelAccessor pixels = image.Lock()) using (PixelAccessor pixels = image.Lock())
{ {
writeSOS(pixels); this.writeSOS(pixels);
} }
// Write the End Of Image marker. // Write the End Of Image marker.
buf[0] = 0xff; this.buf[0] = 0xff;
buf[1] = 0xd9; this.buf[1] = 0xd9;
stream.Write(buf, 0, 2); stream.Write(this.buf, 0, 2);
stream.Flush(); stream.Flush();
} }
// div returns a/b rounded to the nearest integer, instead of rounded to zero. // div returns a/b rounded to the nearest integer, instead of rounded to zero.
private static int div(int a, int b) private static int div(int a, int b)
{ {
if (a >= 0) if (a >= 0) return (a + (b >> 1)) / b;
return (a + (b >> 1)) / b; else return -((-a + (b >> 1)) / b);
else
return -((-a + (b >> 1)) / b);
} }
} }
} }

Loading…
Cancel
Save