Skip to content

Commit

Permalink
RGB Compressions method for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusfriedman committed Oct 19, 2024
1 parent 5b241e6 commit 4b3e283
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions Codecs/Image/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void SaveBitmap(Stream stream)
int width = Width;
int height = Height;

var compressionFormat = (int)BitmapInfoHeader.CompressionMethodType.BitFields;
var compressionFormat = (int)BitmapInfoHeader.CompressionMethodType.RGB;

// Convert pixels to meters: 1 inch = 0.0254 meters
float horizontalResolutionMeters = width / DefaultDpi * 0.0254f;
Expand All @@ -171,7 +171,7 @@ public void SaveBitmap(Stream stream)
int ypelsPerMeter = (int)Math.Round(1.0f / verticalResolutionMeters);

// Create a new BitmapInfoHeader based on the ImageFormat
BitmapInfoHeader header = new(width, height, (short)ImageFormat.Length, (short)ImageFormat.Size, compressionFormat, Data.Count, xpelsPerMeter, ypelsPerMeter, 0, 0);
BitmapInfoHeader header = new(width, height, (short)ImageFormat.Components.Length, (short)ImageFormat.Size, compressionFormat, Data.Count, xpelsPerMeter, ypelsPerMeter, 0, 0);
SaveBitmap(header, stream);
}

Expand Down Expand Up @@ -517,7 +517,7 @@ public static void TestSave()
{
for (int j = 0; j < image.Height; j += 2)
{
for (int c = 0; c < image.ImageFormat.Length; ++c)
for (int c = 0; c < image.ImageFormat.Components.Length; ++c)
{
var data = image.GetComponentData(i, j, image.ImageFormat[c]);

Expand Down Expand Up @@ -552,7 +552,7 @@ public static void TestSave()
{
for (int j = 0; j < image.Height; j += 2)
{
for (int c = 0; c < image.ImageFormat.Length; ++c)
for (int c = 0; c < image.ImageFormat.Components.Length; ++c)
{
var data = image.GetComponentVector(i, j, c);

Expand All @@ -570,13 +570,36 @@ public static void TestSave()
}
}

using (var image = new Codecs.Image.Image(ImageFormat.Binary(8), 696, 564))
{
for (int i = 0; i < image.Width; i += 4)
{
for (int j = 0; j < image.Height; j += 4)
{
for (int c = 0; c < image.ImageFormat.Components.Length; ++c)
{
var data = image.GetComponentVector(i, j, c);

data = Vector<byte>.One * byte.MaxValue;

image.SetComponentVector(i, j, c, data);
}
}
}

using (var outputBmpStream = new System.IO.FileStream(Path.Combine(outputDirectory.FullName, "Binary_packed_line2.bmp"), FileMode.OpenOrCreate))
{
image.SaveBitmap(outputBmpStream);
}
}

using (var image = new Codecs.Image.Image(Media.Codecs.Image.ImageFormat.RGB(8), 696, 564))
{
for (int i = 0; i < image.Width; i += 4)
{
for (int j = 0; j < image.Height; j += 4)
{
for (int c = 0; c < image.ImageFormat.Length; ++c)
for (int c = 0; c < image.ImageFormat.Components.Length; ++c)
{
var data = image.GetComponentVector(i, j, c);

Expand Down Expand Up @@ -757,7 +780,7 @@ public static void TestConversionRGB()
//Create the source image
using (Media.Codecs.Image.Image rgbImage = new(Media.Codecs.Image.ImageFormat.RGB(8), testWidth, testHeight))
{
if (rgbImage.ImageFormat.HasAlphaComponent) throw new System.Exception("HasAlphaComponent should be false");
if (rgbImage.ImageFormat.HasAlphaComponent) throw new System.Exception("HasAlphaComponent should be false");

//Create the ImageFormat based on YUV packed but in Planar format with a full height luma plane and half hight chroma planes
var Yuv420P = new Codecs.Image.ImageFormat(Media.Codecs.Image.ImageFormat.YUV(8, Common.Binary.ByteOrder.Little, Codec.DataLayout.Planar), new int[] { 0, 1, 1 });
Expand Down Expand Up @@ -809,9 +832,27 @@ public static void TestConversionRGB()

//Compare the two sequences
if (false == left.SequenceEqual(right)) throw new System.InvalidOperationException();

string currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

//Draw some lines
for (int i = 0; i < yuvImage.Width; i += 16)
{
for (int j = 0; j < yuvImage.Height; j += 16)
{
for (int c = 0; c < yuvImage.ImageFormat.Length; ++c)
{
var data = yuvImage.GetComponentVector(i, j, c);

data = Vector<byte>.One * byte.MaxValue;

yuvImage.SetComponentVector(i, j, c, data);
}
}
}

var currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

var outputDirectory = Directory.CreateDirectory(Path.Combine(currentPath, "Media", "BmpTest", "output"));

using (var outputStream = new System.IO.FileStream(Path.Combine(outputDirectory.FullName, $"Yuv420P_{Yuv420P.DataLayout}.yuv"), FileMode.OpenOrCreate))
{
outputStream.Write(yuvImage.Data.Array, yuvImage.Data.Offset, yuvImage.Data.Count);
Expand Down

0 comments on commit 4b3e283

Please sign in to comment.