Skip to content

Commit

Permalink
Fix PixImage/Volume conversion with redundant channels
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jul 17, 2024
1 parent aeffccd commit 5858862
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Aardvark.Base.Tensors.CSharp/PixImage/PixImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ public PixImage(Col.Format format, PixImage pixImage)
var volume = CreateVolume<T>(srcInfo.Size.X, srcInfo.Size.Y, dstChannels.Length);
volume.F = srcInfo.F;

if (format == pixImage.Format)
if (format == pixImage.Format && srcInfo.Size == volume.Size)
{
pixImage.CopyVolumeTo(volume);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aardvark.Base.Tensors.CSharp/PixImage/PixVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public PixVolume(Col.Format format, PixVolume pixVolume)
var tensor4 = CreateTensor4<T>(srcInfo.Size.X, srcInfo.Size.Y, srcInfo.Size.Z, dstChannels.Length);
tensor4.F = pixVolume.Tensor4Info.F;

if (format == pixVolume.Format)
if (format == pixVolume.Format && srcInfo.Size == tensor4.Size)
{
pixVolume.CopyTensor4To(tensor4);
}
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/Aardvark.Base.Tests/Images/PixImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,23 @@ public void FormatConversionBGRAToRG()

#region From RGB

[Test]
public void FormatConversionRGBToRGBWithAdditionalChannel()
{
var getRandom = s_randomValues[typeof(byte)];
var volume = ImageTensors.CreateImageVolume<byte>(new V3l(54, 23, 4));
volume.Data.SetByIndex((_) => (byte)getRandom(rnd));
var src = new PixImage<byte>(Col.Format.RGB, volume);
var dst = new PixImage<byte>(Col.Format.RGB, src);

src.GetChannel(0L).ForeachCoord((coord) =>
{
var expected = GetColor<byte, C3b>(src, coord);
var actual = GetColor<byte, C3b>(dst, coord);
Assert.AreEqual(expected, actual);
});
}

[Test]
public void FormatConversionRGBToBGRA()
=> FormatConversion<byte, byte, C3b, C4b>(Col.Format.RGB, Col.Format.BGRA, c => c.ToC4b());
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/Aardvark.Base.Tests/Images/PixVolumeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ public void TypeConversionDoubleToFloat()

#region From RGB

[Test]
public void FormatConversionRGBToRGBWithAdditionalChannel()
{
var getRandom = PixImageTests.s_randomValues[typeof(byte)];
var tensor = ImageTensors.CreateImageTensor4<byte>(new V4l(54, 23, 11, 4));
tensor.Data.SetByIndex((_) => (byte)getRandom(rnd));
var src = new PixVolume<byte>(Col.Format.RGB, tensor);
var dst = new PixVolume<byte>(Col.Format.RGB, src);

src.GetChannel(0L).ForeachCoord((coord) =>
{
var expected = GetColor<byte, C3b>(src, coord);
var actual = GetColor<byte, C3b>(dst, coord);
Assert.AreEqual(expected, actual);
});
}

[Test]
public void FormatConversionRGBToBGRA()
=> FormatConversion<byte, byte, C3b, C4b>(Col.Format.RGB, Col.Format.BGRA, c => c.ToC4b());
Expand Down

0 comments on commit 5858862

Please sign in to comment.