Skip to content

Commit

Permalink
Fix quant bug.
Browse files Browse the repository at this point in the history
Remove quality from compress.

Test subsampling instead of raw.

@ DHT Parsing / Generation.
  • Loading branch information
juliusfriedman committed Oct 25, 2024
1 parent 9c92541 commit 21de2ae
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
13 changes: 4 additions & 9 deletions Codecs/Image/Jpeg/JpegCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,6 @@ internal static void HuffmanEncode(Span<short> 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<double> block, Span<short> quantizationTable, Span<short> output)
{
int VectorSize = Vector<double>.Count;
Expand Down Expand Up @@ -375,7 +369,7 @@ internal static void Quantize(Span<double> block, Span<short> 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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Codecs/Image/Jpeg/JpegImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions Codecs/Image/Jpeg/JpegUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Codecs/Image/Jpeg/Markers/QuantizationTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

}
Expand Down

0 comments on commit 21de2ae

Please sign in to comment.