Ejemplo n.º 1
0
        public static Task <int> Optimize(FileInfo source, FileInfo output)
        {
            using var bytes = new MemoryPoolBufferWriter();

            using (FileStream stream = source.OpenRead())
            {
                ReadAllBytes(stream, bytes);
            }

            var optimizer = new JpegOptimizer();

            optimizer.SetInput(bytes.GetReadOnlySequence());
            optimizer.Scan();

            using var writer = new MemoryPoolBufferWriter();
            optimizer.SetOutput(writer);
            optimizer.Optimize();

            using (FileStream stream = output.OpenWrite())
            {
                WriteAllBytes(writer.GetReadOnlySequence(), stream);
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        public void TestOptimize(string path, bool strip)
        {
            byte[] jpegBytes = File.ReadAllBytes(path);

            using var refImage = Image.Load <Rgb24>(path);

            var optimizer = new JpegOptimizer();

            optimizer.SetInput(jpegBytes);
            optimizer.Scan();

            var buffer = new ArrayBufferWriter <byte>();

            optimizer.SetOutput(buffer);
            optimizer.Optimize(strip);

            Assert.True(buffer.WrittenCount < jpegBytes.Length);

            using var testImage = Image.Load <Rgb24>(buffer.WrittenSpan);

            AssertEqual(refImage, testImage);
        }