/// <summary> /// Initializes static members of the <see cref="JpegEncoderCore"/> class. /// </summary> static JpegEncoderCore() { // Initialize the Huffman tables for (int i = 0; i < TheHuffmanSpecs.Length; i++) { TheHuffmanLut[i] = new HuffmanLut(TheHuffmanSpecs[i]); } }
/// <summary> /// Initializes static members of the <see cref="HuffmanLut"/> struct. /// </summary> static HuffmanLut() { // Initialize the Huffman tables for (int i = 0; i < HuffmanSpec.TheHuffmanSpecs.Length; i++) { TheHuffmanLut[i] = new HuffmanLut(HuffmanSpec.TheHuffmanSpecs[i]); } }
public void Encode(ImageBase image, Stream stream, int quality, JpegSubsample sample) { if (image == null || stream == null){ throw new ArgumentNullException(); } ushort max = JpegConstants.MaxLength; if (image.Width >= max || image.Height >= max){ throw new Exception($"Image is too large to encode at {image.Width}x{image.Height}."); } outputStream = stream; subsample = sample; for (int i = 0; i < theHuffmanSpec.Length; i++){ theHuffmanLut[i] = new HuffmanLut(theHuffmanSpec[i]); } for (int i = 0; i < nQuantIndex; i++){ quant[i] = new byte[Block.blockSize]; } if (quality < 1){ quality = 1; } if (quality > 100){ quality = 100; } int scale; if (quality < 50){ scale = 5000/quality; } else{ scale = 200 - quality*2; } for (int i = 0; i < nQuantIndex; i++){ for (int j = 0; j < Block.blockSize; j++){ int x = unscaledQuant[i, j]; x = (x*scale + 50)/100; if (x < 1){ x = 1; } if (x > 255){ x = 255; } quant[i][j] = (byte) x; } } int componentCount = 3; double densityX = ((Image2) image).HorizontalResolution; double densityY = ((Image2) image).VerticalResolution; WriteApplicationHeader((short) densityX, (short) densityY); WriteDqt(); WriteSof0(image.Width, image.Height, componentCount); WriteDht(componentCount); using (IPixelAccessor pixels = image.Lock()){ WriteSos(pixels); } buffer[0] = 0xff; buffer[1] = 0xd9; stream.Write(buffer, 0, 2); stream.Flush(); }