// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Formats { using System; using System.IO; /// /// Encoder for writing the data image to a stream in jpeg format. /// public class JpegEncoder : IImageEncoder { /// /// The quality used to encode the image. /// private int quality = 75; /// /// The subsamples scheme used to encode the image. /// private JpegSubsample subsample = JpegSubsample.Ratio420; /// /// Whether subsampling has been specifically set. /// private bool subsampleSet; /// /// Gets or sets the quality, that will be used to encode the image. Quality /// index must be between 0 and 100 (compression from max to min). /// /// /// If the quality is less than or equal to 80, the subsampling ratio will switch to /// /// The quality of the jpg image from 0 to 100. public int Quality { get { return this.quality; } set { this.quality = value.Clamp(1, 100); } } /// /// Gets or sets the subsample ration, that will be used to encode the image. /// /// The subsample ratio of the jpg image. public JpegSubsample Subsample { get { return this.subsample; } set { this.subsample = value; this.subsampleSet = true; } } /// public void Encode(Image image, Stream stream) where TColor : struct, IPackedPixel, IEquatable { JpegEncoderCore encode = new JpegEncoderCore(); if (this.subsampleSet) { encode.Encode(image, stream, this.Quality, this.Subsample); } else { // Match Photoshop and use 4:2:0 SUpsampling at quality < 51% encode.Encode(image, stream, this.Quality, this.Quality >= 51 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420); } } } }