From 21de2aed67b81317f597ded87c289d3760041a91 Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Fri, 25 Oct 2024 17:01:24 -0400 Subject: [PATCH] Fix quant bug. Remove quality from compress. Test subsampling instead of raw. @ DHT Parsing / Generation. --- Codecs/Image/Jpeg/JpegCodec.cs | 13 ++++--------- Codecs/Image/Jpeg/JpegImage.cs | 2 +- Codecs/Image/Jpeg/JpegUnitTests.cs | 4 +--- Codecs/Image/Jpeg/Markers/QuantizationTable.cs | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Codecs/Image/Jpeg/JpegCodec.cs b/Codecs/Image/Jpeg/JpegCodec.cs index 17d474ec..19498c1b 100644 --- a/Codecs/Image/Jpeg/JpegCodec.cs +++ b/Codecs/Image/Jpeg/JpegCodec.cs @@ -335,12 +335,6 @@ internal static void HuffmanEncode(Span block, BitWriter writer, IEnumera //Todo: Implement Huffman encoding } - private static int GetBitSize(int value) - { - if (value == 0) return 0; - return (int)Math.Floor(Math.Log2(Math.Abs(value))) + 1; - } - internal static void VQuantize(Span block, Span quantizationTable, Span output) { int VectorSize = Vector.Count; @@ -375,7 +369,7 @@ internal static void Quantize(Span block, Span quantizationTable, } } - internal static void Compress(JpegImage jpegImage, Stream outputStream, int quality) + internal static void Compress(JpegImage jpegImage, Stream outputStream) { // Create a stream around the raw data and compress it to the stream using var inputStream = new MemoryStream(jpegImage.Data.Array, jpegImage.Data.Offset, jpegImage.Data.Count, true); @@ -513,7 +507,7 @@ internal static void WriteStartOfFrame(JpegImage jpegImage, Stream stream) { var componentCount = jpegImage.ImageFormat.Components.Length; using StartOfFrame sof = new StartOfFrame(jpegImage.JpegState.StartOfFrameFunctionCode, componentCount); - sof.P = Binary.Clamp(jpegImage.ImageFormat.Size, Binary.BitsPerByte, byte.MaxValue); + sof.P = Binary.Clamp(jpegImage.ImageFormat.Size, Binary.BitsPerByte, Binary.BitsPerShort); sof.Y = jpegImage.Height; sof.X = jpegImage.Width; for (var i = 0; i < componentCount; ++i) @@ -547,7 +541,8 @@ internal static void WriteQuantizationTableMarker(Stream stream, int quality) // Calculate the quantization table based on the quality var quantizationTable = GetQuantizationTable(quality, QuantizationTableType.Luminance); - var outputMarker = new QuantizationTable(QuantizationTableLength * 2 + 1); + //Output 2 tables + var outputMarker = new QuantizationTable(QuantizationTableLength * 2 + QuantizationTable.Length); outputMarker.Pq = 0; // 8-bit precision diff --git a/Codecs/Image/Jpeg/JpegImage.cs b/Codecs/Image/Jpeg/JpegImage.cs index 116f17da..08de8289 100644 --- a/Codecs/Image/Jpeg/JpegImage.cs +++ b/Codecs/Image/Jpeg/JpegImage.cs @@ -268,7 +268,7 @@ public void Save(Stream stream, int quality = 99) else { // Compress this image data to the stream - JpegCodec.Compress(this, stream, quality); + JpegCodec.Compress(this, stream); JpegCodec.WriteInformationMarker(Jpeg.Markers.EndOfInformation, stream); } diff --git a/Codecs/Image/Jpeg/JpegUnitTests.cs b/Codecs/Image/Jpeg/JpegUnitTests.cs index 031f38a4..d5aeee26 100644 --- a/Codecs/Image/Jpeg/JpegUnitTests.cs +++ b/Codecs/Image/Jpeg/JpegUnitTests.cs @@ -3,13 +3,11 @@ using Media.Codec.Jpeg; using Media.Codecs.Image; using Media.Common; -using Media.Common.Classes.Loggers; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; -using System.Security.Cryptography; namespace Media.UnitTests; @@ -119,7 +117,7 @@ public static void TestSave() } } - using (var image = new JpegImage(Media.Codecs.Image.ImageFormat.YUV(8), 696, 564)) + using (var image = new JpegImage(ImageFormat.WithSubSampling(Media.Codecs.Image.ImageFormat.YUV(8), [2, 1, 1], [2, 1, 1]), 696, 564)) { for (int i = 0; i < image.Width; i += 4) { diff --git a/Codecs/Image/Jpeg/Markers/QuantizationTable.cs b/Codecs/Image/Jpeg/Markers/QuantizationTable.cs index 726dec4a..2425c87d 100644 --- a/Codecs/Image/Jpeg/Markers/QuantizationTable.cs +++ b/Codecs/Image/Jpeg/Markers/QuantizationTable.cs @@ -6,7 +6,7 @@ public class QuantizationTable : Marker { public new const int Length = 1; - public QuantizationTable(int size) : base(LengthBytes + Length + Markers.QuantizationTable, size) + public QuantizationTable(int size) : base(Markers.QuantizationTable, LengthBytes + Length + size) { }