// // 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 string MimeType => "image/jpeg"; /// public string Extension => "jpg"; /// public bool IsSupportedFileExtension(string extension) { Guard.NotNullOrEmpty(extension, "extension"); if (extension.StartsWith(".")) { extension = extension.Substring(1); } return extension.Equals(this.Extension, StringComparison.OrdinalIgnoreCase) || extension.Equals("jpeg", StringComparison.OrdinalIgnoreCase) || extension.Equals("jfif", StringComparison.OrdinalIgnoreCase); } /// public void Encode(Image image, Stream stream) where TColor : struct, IPackedPixel where TPacked : struct { JpegEncoderCore encode = new JpegEncoderCore(); if (this.subsampleSet) { encode.Encode(image, stream, this.Quality, this.Subsample); } else { encode.Encode(image, stream, this.Quality, this.Quality >= 80 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420); } } } }