public void TestJpegLibraryEncode444() { var encoder = new JpegEncoder(); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), 75)); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetChrominanceTable(JpegElementPrecision.Precision8Bit, 1), 75)); encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); encoder.SetHuffmanTable(true, 1, JpegStandardHuffmanEncodingTable.GetChrominanceDCTable()); encoder.SetHuffmanTable(false, 1, JpegStandardHuffmanEncodingTable.GetChrominanceACTable()); encoder.AddComponent(1, 0, 0, 0, 1, 1); // Y component encoder.AddComponent(2, 1, 1, 1, 1, 1); // Cb component encoder.AddComponent(3, 1, 1, 1, 1, 1); // Cr component byte[] ycbcr = ArrayPool <byte> .Shared.Rent(3 *_width *_height); try { JpegRgbToYCbCrConverter.Shared.ConvertRgba32ToYCbCr8(MemoryMarshal.AsBytes(_rgba.AsSpan()), ycbcr, _width * _height); encoder.SetInputReader(new JpegBufferInputReader(_width, _height, 3, ycbcr)); using var bufferWriter = new NullBufferWriter(); encoder.SetOutput(bufferWriter); encoder.Encode(); } finally { ArrayPool <byte> .Shared.Return(ycbcr); } }
public static Task <int> Encode(FileInfo source, FileInfo output, int quality, bool optimizeCoding) { if (quality <= 0 || quality > 100) { throw new ArgumentOutOfRangeException(nameof(quality)); } Image <Rgb24> image; using (FileStream stream = source.OpenRead()) { image = Image.Load <Rgb24>(stream); } // Convert RGB to YCbCr byte[] ycbcr = new byte[image.Width * image.Height * 3]; for (int i = 0; i < image.Height; i++) { JpegRgbToYCbCrConverter.Shared.ConvertRgb24ToYCbCr8(MemoryMarshal.AsBytes(image.GetPixelRowSpan(i)), ycbcr.AsSpan(3 * image.Width * i, 3 * image.Width), image.Width); } var encoder = new JpegEncoder(); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), quality)); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetChrominanceTable(JpegElementPrecision.Precision8Bit, 1), quality)); if (optimizeCoding) { encoder.SetHuffmanTable(true, 0); encoder.SetHuffmanTable(false, 0); encoder.SetHuffmanTable(true, 1); encoder.SetHuffmanTable(false, 1); } else { encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); encoder.SetHuffmanTable(true, 1, JpegStandardHuffmanEncodingTable.GetChrominanceDCTable()); encoder.SetHuffmanTable(false, 1, JpegStandardHuffmanEncodingTable.GetChrominanceACTable()); } encoder.AddComponent(0, 0, 0, 0, 1, 1); // Y component encoder.AddComponent(1, 1, 1, 1, 2, 2); // Cb component encoder.AddComponent(2, 1, 1, 1, 2, 2); // Cr component encoder.SetInputReader(new JpegBufferInputReader(image.Width, image.Height, 3, ycbcr)); var writer = new ArrayBufferWriter <byte>(); encoder.SetOutput(writer); encoder.Encode(); using (FileStream stream = output.OpenWrite()) { stream.Write(writer.WrittenSpan); } return(Task.FromResult(0)); }
public void TestJpegLibraryEncode420_NoBuffer() { var encoder = new JpegEncoder(); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), 75)); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetChrominanceTable(JpegElementPrecision.Precision8Bit, 1), 75)); encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); encoder.SetHuffmanTable(true, 1, JpegStandardHuffmanEncodingTable.GetChrominanceDCTable()); encoder.SetHuffmanTable(false, 1, JpegStandardHuffmanEncodingTable.GetChrominanceACTable()); encoder.AddComponent(1, 0, 0, 0, 1, 1); // Y component encoder.AddComponent(2, 1, 1, 1, 2, 2); // Cb component encoder.AddComponent(3, 1, 1, 1, 2, 2); // Cr component encoder.SetInputReader(new JpegRgbaInputReader(_width, _height, _rgba)); using var bufferWriter = new NullBufferWriter(); encoder.SetOutput(bufferWriter); encoder.Encode(); }
private void Initialize(int quality, bool optimizeCoding) { TiffJpegEncoder encoder; switch (_photometricInterpretation) { case TiffPhotometricInterpretation.BlackIsZero: case TiffPhotometricInterpretation.WhiteIsZero: _componentCount = 1; encoder = new TiffJpegEncoder(minimumBufferSegmentSize: MinimumBufferSegmentSize); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), quality)); if (optimizeCoding) { encoder.SetHuffmanTable(true, 0); encoder.SetHuffmanTable(false, 0); } else { encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); } encoder.AddComponent(0, 0, 0, 0, 1, 1); // Y component break; case TiffPhotometricInterpretation.RGB: _componentCount = 3; encoder = new TiffJpegEncoder(minimumBufferSegmentSize: MinimumBufferSegmentSize); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), quality)); if (optimizeCoding) { encoder.SetHuffmanTable(true, 0); encoder.SetHuffmanTable(false, 0); } else { encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); } encoder.AddComponent(0, 0, 0, 0, 1, 1); // R component encoder.AddComponent(1, 0, 0, 0, 1, 1); // G component encoder.AddComponent(2, 0, 0, 0, 1, 1); // B component break; case TiffPhotometricInterpretation.Seperated: _componentCount = 4; encoder = new TiffJpegEncoder(minimumBufferSegmentSize: MinimumBufferSegmentSize); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), quality)); if (optimizeCoding) { encoder.SetHuffmanTable(true, 0); encoder.SetHuffmanTable(false, 0); } else { encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); } encoder.AddComponent(0, 0, 0, 0, 1, 1); // C component encoder.AddComponent(1, 0, 0, 0, 1, 1); // M component encoder.AddComponent(2, 0, 0, 0, 1, 1); // Y component encoder.AddComponent(3, 0, 0, 0, 1, 1); // K component break; case TiffPhotometricInterpretation.YCbCr: _componentCount = 3; encoder = new TiffJpegEncoder(minimumBufferSegmentSize: MinimumBufferSegmentSize); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetLuminanceTable(JpegElementPrecision.Precision8Bit, 0), quality)); encoder.SetQuantizationTable(JpegStandardQuantizationTable.ScaleByQuality(JpegStandardQuantizationTable.GetChrominanceTable(JpegElementPrecision.Precision8Bit, 1), quality)); if (optimizeCoding) { encoder.SetHuffmanTable(true, 0); encoder.SetHuffmanTable(false, 0); encoder.SetHuffmanTable(true, 1); encoder.SetHuffmanTable(false, 1); } else { encoder.SetHuffmanTable(true, 0, JpegStandardHuffmanEncodingTable.GetLuminanceDCTable()); encoder.SetHuffmanTable(false, 0, JpegStandardHuffmanEncodingTable.GetLuminanceACTable()); encoder.SetHuffmanTable(true, 1, JpegStandardHuffmanEncodingTable.GetChrominanceDCTable()); encoder.SetHuffmanTable(false, 1, JpegStandardHuffmanEncodingTable.GetChrominanceACTable()); } encoder.AddComponent(0, 0, 0, 0, (byte)_horizontalSubsampling, (byte)_verticalSubsampling); // Y component encoder.AddComponent(1, 1, 1, 1, 1, 1); // Cb component encoder.AddComponent(2, 1, 1, 1, 1, 1); // Cr component break; default: throw new NotSupportedException("JPEG compression only supports BlackIsZero, WhiteIsZero, RGB, YCbCr and CMYK photometric interpretation."); } _encoder = encoder; }