private bool DoCompress(Stream stream, bool lossless) { ImageOptimizerHelper.CheckStream(stream); bool isCompressed = false; var startPosition = stream.Position; using (var images = new MagickImageCollection(stream, new MagickReadSettings() { Format = MagickFormat.Ico })) { foreach (var image in images) { if (image.Width > 255) { image.Format = MagickFormat.Png; var pngHelper = new PngHelper(this); var memoryStream = pngHelper.FindBestStreamQuality(image, out var bestQuality); image.Format = MagickFormat.Ico; if (memoryStream != null) { memoryStream.Dispose(); image.Quality = bestQuality; } } else { if (CanUseColormap(image, lossless)) { image.ClassType = ClassType.Pseudo; } } } using (var output = new MemoryStream()) { images.Write(output); if (output.Length < (stream.Length - startPosition)) { isCompressed = true; stream.Position = startPosition; output.Position = 0; output.CopyTo(stream); stream.SetLength(startPosition + output.Length); } stream.Position = startPosition; } } return(isCompressed); }
private static void StartCompression(MagickImage image, bool lossless) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Png); if (!lossless) { image.Strip(); image.Settings.SetDefine(MagickFormat.Png, "exclude-chunks", "all"); image.Settings.SetDefine(MagickFormat.Png, "include-chunks", "tRNS,gAMA"); } }
private bool DoCompress(Stream stream, bool lossless, int quality) { ImageOptimizerHelper.CheckStream(stream); bool isCompressed = false; long startPosition = stream.Position; MemoryStream memStream = new MemoryStream(); try { DoNativeCompress(stream, memStream, Progressive, lossless, quality); if (OptimalCompression) { stream.Position = startPosition; MemoryStream memStreamOptimal = new MemoryStream(); try { DoNativeCompress(stream, memStreamOptimal, !Progressive, lossless, quality); if (memStreamOptimal.Length < memStream.Length) { memStreamOptimal = Interlocked.Exchange(ref memStream, memStreamOptimal); } } finally { memStreamOptimal.Dispose(); } } if (memStream.Length < (stream.Length - startPosition)) { isCompressed = true; stream.Position = startPosition; memStream.Position = 0; memStream.CopyTo(stream); stream.SetLength(startPosition + memStream.Length); } stream.Position = startPosition; } finally { memStream.Dispose(); } return(isCompressed); }
private bool DoLosslessCompress(FileInfo file) { bool isCompressed = false; using (MagickImage image = new MagickImage(file)) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Png); image.Strip(); image.Settings.SetDefine(MagickFormat.Png, "exclude-chunks", "all"); image.Settings.SetDefine(MagickFormat.Png, "include-chunks", "tRNS,gAMA"); CheckTransparency(image); Collection <TemporaryFile> tempFiles = new Collection <TemporaryFile>(); try { TemporaryFile bestFile = null; foreach (int quality in GetQualityList()) { TemporaryFile tempFile = new TemporaryFile(); tempFiles.Add(tempFile); image.Quality = quality; image.Write(tempFile); if (bestFile == null || bestFile.Length > tempFile.Length) { bestFile = tempFile; } } if (bestFile.Length < file.Length) { isCompressed = true; bestFile.CopyTo(file); file.Refresh(); } } finally { foreach (TemporaryFile tempFile in tempFiles) { tempFile.Dispose(); } } } return(isCompressed); }
/// <summary> /// Performs lossless compression on the specified stream. If the new stream size is not smaller /// the stream won't be overwritten. /// </summary> /// <param name="stream">The stream of the gif image to compress.</param> /// <returns>True when the image could be compressed otherwise false.</returns> public bool LosslessCompress([ValidatedNotNull] Stream stream) { ImageOptimizerHelper.CheckStream(stream); bool isCompressed = false; long startPosition = stream.Position; using (var images = new MagickImageCollection(stream)) { if (images.Count == 1) { isCompressed = DoLosslessCompress(images[0], stream, startPosition); } stream.Position = startPosition; } return(isCompressed); }
/// <summary> /// Performs lossless compression on the specified stream. If the new stream size is not smaller /// the stream won't be overwritten. /// </summary> /// <param name="stream">The stream of the gif image to compress.</param> /// <returns>True when the image could be compressed otherwise false.</returns> public bool LosslessCompress(Stream stream) { Throw.IfNull(nameof(stream), stream); ImageOptimizerHelper.CheckStream(stream); bool isCompressed = false; long startPosition = stream.Position; using (IMagickImageCollection images = new MagickImageCollection(stream)) { if (images.Count == 1) { isCompressed = DoLosslessCompress(stream, startPosition, images[0]); } stream.Position = startPosition; } return(isCompressed); }
private static bool DoLosslessCompress(FileInfo file, IMagickImage <QuantumType> image) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Gif); bool isCompressed = false; using (TemporaryFile tempFile = new TemporaryFile()) { LosslessCompress(image); image.Write(tempFile); if (tempFile.Length < file.Length) { isCompressed = true; tempFile.CopyTo(file); file.Refresh(); } } return(isCompressed); }
private static bool DoLosslessCompress(Stream stream, long startPosition, IMagickImage image) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Gif); bool isCompressed = false; using (MemoryStream memStream = new MemoryStream()) { LosslessCompress(image); image.Write(memStream); if (memStream.Length < (stream.Length - startPosition)) { isCompressed = true; memStream.CopyTo(stream); stream.SetLength(startPosition + memStream.Length); } } return(isCompressed); }
private bool DoCompress(Stream stream, bool lossless) { ImageOptimizerHelper.CheckStream(stream); var isCompressed = false; var startPosition = stream.Position; using (var image = new MagickImage(stream)) { StartCompression(image, lossless); MemoryStream bestStream = null; try { var pngHelper = new PngHelper(this); bestStream = pngHelper.FindBestStreamQuality(image, out _); if (bestStream != null && bestStream.Length < (stream.Length - startPosition)) { isCompressed = true; stream.Position = startPosition; bestStream.Position = 0; bestStream.CopyTo(stream); stream.SetLength(startPosition + bestStream.Length); } stream.Position = startPosition; } finally { if (bestStream != null) { bestStream.Dispose(); } } } return(isCompressed); }
private bool DoCompress(Stream stream, bool lossless) { ImageOptimizerHelper.CheckStream(stream); bool isCompressed = false; long startPosition = stream.Position; using (MagickImage image = new MagickImage(stream)) { StartCompression(image, lossless); MemoryStream bestStream = null; try { foreach (int quality in GetQualityList()) { MemoryStream memStream = new MemoryStream(); try { image.Quality = quality; image.Write(memStream); if (bestStream == null || memStream.Length < bestStream.Length) { if (bestStream != null) { bestStream.Dispose(); } bestStream = memStream; memStream = null; } } finally { if (memStream != null) { memStream.Dispose(); } } } if (bestStream.Length < (stream.Length - startPosition)) { isCompressed = true; stream.Position = startPosition; bestStream.Position = 0; bestStream.CopyTo(stream); stream.SetLength(startPosition + bestStream.Length); } stream.Position = startPosition; } finally { if (bestStream != null) { bestStream.Dispose(); } } } return(isCompressed); }