From ad03cd53905c52742cb2790dad58389aa68d5b00 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 14 Apr 2016 09:29:46 +1000 Subject: [PATCH] Strip out dominant pixel methods. The two methods added yesterday are too generic and not best placed in core. Could be an extension method for the web version though. Replaced with ToString() override. Former-commit-id: fe3126102c16e1499536b13dfdfe705246f29869 Former-commit-id: 7f5b08ed913df21285b01d6ded3d7922203c050c Former-commit-id: 70d8c9dc185b715402b6092be342755020ddcc70 --- src/ImageProcessorCore/Image.cs | 17 +++++++- src/ImageProcessorCore/ImageExtensions.cs | 42 +++---------------- .../Formats/EncoderDecoderTests.cs | 12 +++--- 3 files changed, 26 insertions(+), 45 deletions(-) diff --git a/src/ImageProcessorCore/Image.cs b/src/ImageProcessorCore/Image.cs index 745b617b38..e772ad2c03 100644 --- a/src/ImageProcessorCore/Image.cs +++ b/src/ImageProcessorCore/Image.cs @@ -223,6 +223,21 @@ namespace ImageProcessorCore encoder.Encode(this, stream); } + /// + /// Returns a Base64 encoded string from the given image. + /// + /// data:image/gif;base64,R0lGODlhAQABAIABAEdJRgAAACwAAAAAAQABAAACAkQBAA== + /// The + public override string ToString() + { + using (MemoryStream stream = new MemoryStream()) + { + this.Save(stream); + stream.Flush(); + return $"data:{this.CurrentImageFormat.Encoder.MimeType};base64,{Convert.ToBase64String(stream.ToArray())}"; + } + } + /// protected override void Dispose(bool disposing) { @@ -277,7 +292,7 @@ namespace ImageProcessorCore if (maxHeaderSize > 0) { byte[] header = new byte[maxHeaderSize]; - + stream.Position = 0; stream.Read(header, 0, maxHeaderSize); stream.Position = 0; diff --git a/src/ImageProcessorCore/ImageExtensions.cs b/src/ImageProcessorCore/ImageExtensions.cs index 420132236b..7edb5aaa5d 100644 --- a/src/ImageProcessorCore/ImageExtensions.cs +++ b/src/ImageProcessorCore/ImageExtensions.cs @@ -50,52 +50,20 @@ namespace ImageProcessorCore /// The stream to save the image to. /// The quality to save the image to representing the number of colors. Between 1 and 100. /// Thrown if the stream is null. - public static void SaveAsGif(this ImageBase source, Stream stream, int quality = 256) => new GifEncoder() { Quality = quality }.Encode(source, stream); + public static void SaveAsGif(this ImageBase source, Stream stream, int quality = 256) => new GifEncoder { Quality = quality }.Encode(source, stream); /// - /// Returns a 1x1 pixel Base64 encoded gif from the given image containing a single dominant color. + /// Returns a Base64 encoded string from the given image. /// - /// - /// The idea and code is based on the article at - /// - /// The image this method extends. - /// The - public static string ToBase64GifPixelString(this Image source) - { - // Leave the original image intact - using (Image temp = new Image(source)) - { - Bgra32 color = new OctreeQuantizer().Quantize(temp.Resize(250, 250), 1).Palette[0]; - - byte[] gif = { - 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, // Header - 0x01, 0x00, 0x01, 0x00, 0x80, 0x01, 0x00, // Logical Screen Descriptor - color.R, color.G, color.B, 0x00, 0x00, 0x00, // Global Color Table - 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, // Image Descriptor - 0x02, 0x02, 0x44, 0x01, 0x00 // Image Data - }; - - return "data:image/gif;base64," + Convert.ToBase64String(gif); - } - } - - /// - /// Returns a 3x3 pixel Base64 encoded gif from the given image. - /// - /// - /// The idea and code is based on the article at - /// /// The image this method extends. /// The - public static string ToBase64GifString(this Image source) + public static string ToBase64String(this Image source) { - // Leave the original image intact - using (Image temp = new Image(source)) using (MemoryStream stream = new MemoryStream()) { - temp.Resize(3, 3).SaveAsGif(stream); + source.Save(stream); stream.Flush(); - return "data:image/gif;base64," + Convert.ToBase64String(stream.ToArray()); + return $"data:{source.CurrentImageFormat.Encoder.MimeType};base64,{Convert.ToBase64String(stream.ToArray())}"; } } diff --git a/tests/ImageProcessorCore.Tests/Formats/EncoderDecoderTests.cs b/tests/ImageProcessorCore.Tests/Formats/EncoderDecoderTests.cs index 488e570381..05585284b4 100644 --- a/tests/ImageProcessorCore.Tests/Formats/EncoderDecoderTests.cs +++ b/tests/ImageProcessorCore.Tests/Formats/EncoderDecoderTests.cs @@ -18,11 +18,11 @@ namespace ImageProcessorCore.Tests public class EncoderDecoderTests : FileTestBase { [Fact] - public void ImageCanEncodeToDominantPixel() + public void ImageCanEncodeToString() { - if (!Directory.Exists("TestOutput/Dominant")) + if (!Directory.Exists("TestOutput/ToString")) { - Directory.CreateDirectory("TestOutput/Dominant"); + Directory.CreateDirectory("TestOutput/ToString"); } foreach (string file in Files) @@ -32,10 +32,8 @@ namespace ImageProcessorCore.Tests Stopwatch watch = Stopwatch.StartNew(); using (Image image = new Image(stream)) { - string filename = "TestOutput/Dominant/" + Path.GetFileNameWithoutExtension(file) + ".txt"; - string filename2 = "TestOutput/Dominant/Pixel-" + Path.GetFileNameWithoutExtension(file) + ".txt"; - File.WriteAllText(filename, image.ToBase64GifString()); - File.WriteAllText(filename2, image.ToBase64GifPixelString()); + string filename = "TestOutput/ToString/" + Path.GetFileNameWithoutExtension(file) + ".txt"; + File.WriteAllText(filename, image.ToString()); } Trace.WriteLine($"{watch.ElapsedMilliseconds}ms");