private static void ConvertFile(string file) { using (var image = Pfim.FromFile(file)) { byte[] newData; // Since image sharp can't handle data with line padding in a stride // we create an stripped down array if any padding is detected var tightStride = image.Width * image.BitsPerPixel / 8; if (image.Stride != tightStride) { newData = new byte[image.Height * tightStride]; for (int i = 0; i < image.Height; i++) { Buffer.BlockCopy(image.Data, i * image.Stride, newData, i * tightStride, tightStride); } } else { newData = image.Data; } SaveAsPng(file, image, newData); } }
public void ParseTarga24TrueRunLength() { byte[] data = new byte[64 * 3]; int i = 0; for (; i < 32 * 3; i += 3) { data[i] = 0; data[i + 1] = 216; data[i + 2] = 255; } for (; i < 32 * 3 + 16 * 3; i += 3) { data[i] = 255; data[i + 1] = 148; data[i + 2] = 0; } for (; i < 32 * 3 + 16 * 3 + 8 * 3; i += 3) { data[i] = 0; data[i + 1] = 255; data[i + 2] = 76; } for (; i < 32 * 3 + 16 * 3 + 8 * 3 + 8 * 3; i += 3) { data[i] = 0; data[i + 1] = 0; data[i + 2] = 255; } var image = Pfim.FromFile(Path.Combine("data", "true-24-rle.tga")); Assert.Equal(data, image.Data); }
public void TestImageProperties(string allPath, ulong hash, ImageFormat format) { var path = Path.Combine("data", Path.Combine(allPath.Split('\\'))); var data = File.ReadAllBytes(path); var image = Pfim.FromFile(path); var image2 = Pfim.FromStream(new MemoryStream(data), new PfimConfig()); Assert.NotEqual(0, image.DataLen); Assert.NotEqual(0, image2.DataLen); var allocator = new PfimAllocator(); Assert.Equal(0, allocator.Rented); using (var image3 = Pfim.FromStream(new MemoryStream(data), new PfimConfig(allocator: allocator))) { Assert.Equal(format, image.Format); Assert.Equal(image.Format, image2.Format); Assert.Equal(hash, Hash64(image.Data, image.DataLen)); Assert.Equal(hash, Hash64(image2.Data, image2.DataLen)); Assert.Equal(hash, Hash64(image3.Data, image3.DataLen)); var mipMapSuffix = image.MipMaps.Sum(x => x.DataLen); Assert.Equal(image.Data.Length - mipMapSuffix, image.DataLen); Assert.Equal(image.Data.Length, image2.Data.Length); Assert.Equal(image3.DataLen, image.Data.Length - mipMapSuffix); Assert.NotEqual(0, image.DataLen); Assert.NotEqual(0, allocator.Rented); } Assert.Equal(0, allocator.Rented); }
public void ParseTransparentTarga() { var image = Pfim.FromFile(Path.Combine("data", "flag_t32.tga")); for (int i = 0; i < image.Data.Length; i += 4) { Assert.Equal(0, image.Data[i + 3]); } }
public void ParseMarblesTarga() { var image = Pfim.FromFile(Path.Combine("data", "marbles2.tga")); Assert.Equal(100 * 71 * 3, image.Data.Length); Assert.Equal(2, image.Data[0]); Assert.Equal(3, image.Data[1]); Assert.Equal(3, image.Data[2]); }
public void ParseLargeTargaBottomLeft() { var image = Pfim.FromFile(Path.Combine("data", "marbles.tga")); Assert.Equal(4264260, image.Data.Length); Assert.Equal(0, image.Data[0]); Assert.Equal(0, image.Data[1]); Assert.Equal(0, image.Data[2]); }
public void ParseLargeTargaTopLeft() { var image = Pfim.FromFile(Path.Combine("data", "large-top-left.tga")); foreach (byte bt in image.Data) { Assert.Equal(0, bt); } }
public void ParseWoseBc1Snorm() { var image = Pfim.FromFile(Path.Combine("data", "wose_BC1_UNORM_SRGB.DDS")); Assert.IsAssignableFrom <Dds>(image); var dds = (Dds)image; Assert.Equal(DxgiFormat.BC1_UNORM_SRGB, dds.Header10?.DxgiFormat); }
public void ParseSimpleDxt51x1() { var image = Pfim.FromFile(Path.Combine("data", "dxt5-simple-1x1.dds")); Assert.Equal(16, image.Stride); Assert.Equal(0, image.Data[0]); Assert.Equal(0, image.Data[1]); Assert.Equal(128, image.Data[2]); Assert.Equal(255, image.Data[3]); Assert.Equal(64, image.Data.Length); Assert.Equal(1, image.Height); Assert.Equal(1, image.Width); }
public void ParseTrueTarga32MixedEncoding() { var image = Pfim.FromFile(Path.Combine("data", "true-32-mixed.tga")); byte[] data = new byte[256]; for (int i = 0; i < 16 * 4; i += 4) { data[i] = 0; data[i + 1] = 216; data[i + 2] = 255; data[i + 3] = 255; } Array.Copy(new byte[] { 0, 0, 0, 255 }, 0, data, 64, 4); Array.Copy(new byte[] { 64, 64, 64, 255 }, 0, data, 68, 4); Array.Copy(new byte[] { 0, 0, 255, 255 }, 0, data, 72, 4); Array.Copy(new byte[] { 0, 106, 255, 255 }, 0, data, 76, 4); Array.Copy(new byte[] { 0, 216, 255, 255 }, 0, data, 80, 4); Array.Copy(new byte[] { 0, 255, 182, 255 }, 0, data, 84, 4); Array.Copy(new byte[] { 0, 255, 76, 255 }, 0, data, 88, 4); Array.Copy(new byte[] { 33, 255, 0, 255 }, 0, data, 92, 4); Array.Copy(new byte[] { 144, 255, 0, 255 }, 0, data, 96, 4); Array.Copy(new byte[] { 255, 255, 0, 255 }, 0, data, 100, 4); Array.Copy(new byte[] { 255, 148, 0, 255 }, 0, data, 104, 4); Array.Copy(new byte[] { 255, 38, 0, 255 }, 0, data, 108, 4); Array.Copy(new byte[] { 255, 0, 72, 255 }, 0, data, 112, 4); Array.Copy(new byte[] { 255, 0, 178, 255 }, 0, data, 116, 4); Array.Copy(new byte[] { 220, 0, 255, 255 }, 0, data, 120, 4); Array.Copy(new byte[] { 110, 0, 255, 255 }, 0, data, 124, 4); for (int i = 128; i < 192; i += 4) { data[i] = 255; data[i + 1] = 148; data[i + 2] = 0; data[i + 3] = 255; } for (int i = 192; i < 224; i += 4) { data[i] = 0; data[i + 1] = 255; data[i + 2] = 76; data[i + 3] = 255; } for (int i = 224; i < 256; i += 4) { data[i] = 0; data[i + 1] = 0; data[i + 2] = 255; data[i + 3] = 255; } Assert.Equal(data, image.Data); }
public void ParseLarge32TargetImage() { var image = Pfim.FromFile(Path.Combine("data", "true-32-rle-large.tga")); byte[] data = new byte[1200 * 1200 * 4]; for (int i = 0; i < data.Length; i += 4) { data[i] = 0; data[i + 1] = 51; data[i + 2] = 127; data[i + 3] = 255; } Assert.Equal(data, image.Data); }
public void ParseUncompressedNonSquareTga() { var image = Pfim.FromFile(Path.Combine("data", "tiny-rect.tga")); byte[] data = new byte[12 * 20 * 4]; for (int i = 0; i < data.Length; i += 4) { data[i] = 0; data[i + 1] = 216; data[i + 2] = 255; data[i + 3] = 255; } Assert.Equal(data, image.Data); }
public void ParseTargaTrue24SingleColor() { byte[] expected = new byte[64 * 64 * 3]; for (int i = 0; i < expected.Length; i += 3) { expected[i] = 255; expected[i + 1] = 176; expected[i + 2] = 0; } var image = Pfim.FromFile(Path.Combine("data", "true-24.tga")); Assert.Equal(expected, image.Data); }
public void TestMipMapProperties(string allPath, ulong hash) { var path = Path.Combine("data", Path.Combine(allPath.Split('\\'))); var data = File.ReadAllBytes(path); var image = Pfim.FromFile(path); var image2 = Dds.Create(data, new PfimConfig()); Assert.Equal(image.MipMaps, image2.MipMaps); var mipMapLengths = image.MipMaps.Sum(x => x.DataLen); var hash1 = Hash64(image.Data, image.DataLen + mipMapLengths); Assert.Equal(hash1, Hash64(image2.Data, image2.DataLen + mipMapLengths)); Assert.Equal(hash, hash1); }
public void ParseSimpleDxt1() { var image = Pfim.FromFile(Path.Combine("data", "dxt1-simple.dds")); byte[] data = new byte[64 * 64 * 3]; for (int i = 0; i < data.Length; i += 3) { data[i] = 0; data[i + 1] = 0; data[i + 2] = 127; } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void ParseTargaTopLeftColorMap() { var image = Pfim.FromFile(Path.Combine("data", "rgb24_top_left_colormap.tga"), new PfimConfig(applyColorMap: false)); Assert.Equal(8, image.BitsPerPixel); Assert.Equal(4096, image.Data.Length); Assert.NotEqual(ImageFormat.Rgb24, image.Format); image.ApplyColorMap(); Assert.Equal(ImageFormat.Rgb24, image.Format); Assert.Equal(255, image.Data[0]); Assert.Equal(255, image.Data[1]); Assert.Equal(255, image.Data[2]); Assert.Equal(255, image.Data[3]); Assert.Equal(255, image.Data[4]); Assert.Equal(255, image.Data[5]); }
private Texture2D CreateTexture(string file) { var image = Pfim.FromFile(file); image.ApplyColorMap(); byte[] newData; // Since mono game can't handle data with line padding in a stride // we create an stripped down array if any padding is detected var tightStride = image.Width * image.BitsPerPixel / 8; if (image.Stride != tightStride) { newData = new byte[image.Height * tightStride]; for (int i = 0; i < image.Height; i++) { Buffer.BlockCopy(image.Data, i * image.Stride, newData, i * tightStride, tightStride); } } else { newData = image.Data; } // I believe mono game core is limited in its texture support // so we're assuming 32bit data format is needed. One can always // upscale 24bit / 16bit / 15bit data (not shown in sample). var newTexture = new Texture2D(GraphicsDevice, image.Width, image.Height, false, SurfaceFormat.Color); switch (image.Format) { case ImageFormat.Rgba32: // Flip red and blue color channels. for (int i = 0; i < newData.Length; i += 4) { var temp = newData[i + 2]; newData[i + 2] = newData[i]; newData[i] = temp; } newTexture.SetData(newData); break; } return(newTexture); }
public void Parse32BitUncompressedDds() { var image = Pfim.FromFile(Path.Combine("data", "32-bit-uncompressed.dds")); byte[] data = new byte[64 * 64 * 4]; for (int i = 0; i < data.Length; i += 4) { data[i] = 0; data[i + 1] = 0; data[i + 2] = 127; data[i + 3] = 255; } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void ParseSimpleBc4() { var image = Pfim.FromFile(Path.Combine("data", "bc4-simple.dds")); Assert.True(image is Bc4Dds); Assert.Equal(CompressionAlgorithm.BC4U, ((Bc4Dds)image).Header?.PixelFormat.FourCC); byte[] data = new byte[64 * 64]; for (int i = 0; i < data.Length; ++i) { data[i] = 128; } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void TargaColorMapIdempotent() { var image = Pfim.FromFile(Path.Combine("data", "rgb24_top_left_colormap.tga"), new PfimConfig(applyColorMap: false)); var firstData = image.Data; var firstLen = image.DataLen; image.ApplyColorMap(); var secondData = image.Data; var secondLen = image.DataLen; image.ApplyColorMap(); var thirdData = image.Data; var thirdLen = image.DataLen; Assert.NotEqual(firstLen, secondLen); Assert.Equal(secondLen, thirdLen); Assert.True(ReferenceEquals(secondData, thirdData)); }
public void Parse24bitUncompressedOdd() { var image = Pfim.FromFile(Path.Combine("data", "24-bit-uncompressed-odd.dds")); Assert.Equal(4, image.Stride); Assert.Equal(12, image.Data.Length); Assert.Equal(3, image.Height); Assert.Equal(1, image.Width); for (int i = 0; i < 3; i++) { for (int j = 0; j < 1; j++) { Assert.Equal(0, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 0]); Assert.Equal(0, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 1]); Assert.Equal(128, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 2]); } } }
public void ParseSimpleBc5s() { var image = Pfim.FromFile(Path.Combine("data", "bc5-simple-snorm.dds")); Assert.True(image is Bc5sDds); Assert.Equal(CompressionAlgorithm.BC5S, ((Bc5sDds)image).Header?.PixelFormat.FourCC); byte[] data = new byte[64 * 64 * 3]; for (int i = 0; i < data.Length; i += 3) { data[i] = 0; data[i + 1] = 64; data[i + 2] = 128; } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog { Multiselect = true, Filter = "Images (*.tga;*.dds)|*.tga;*.dds|All files (*.*)|*.*", Title = "Open Files with Pfim" }; if (dialog.ShowDialog(this) != true) { return; } ImagePanel.Children.Clear(); foreach (var handle in handles) { handle.Free(); } Progress.Visibility = Visibility.Visible; Progress.Value = 0; Progress.IsIndeterminate = true; var images = dialog.FileNames; Progress.IsIndeterminate = false; Progress.Maximum = images.Length; foreach (var file in images) { IImage image = await Task.Run(() => Pfim.FromFile(file)); foreach (var im in WpfImage(image)) { ImagePanel.Children.Add(im); } Progress.Value += 1; } Progress.Visibility = Visibility.Collapsed; }
public void ParseSimpleUncompressedOdd() { var image = Pfim.FromFile(Path.Combine("data", "32-bit-uncompressed-odd.dds")); Assert.Equal(20, image.Stride); Assert.Equal(image.Data.Length, 5 * 9 * 4); Assert.Equal(9, image.Height); Assert.Equal(5, image.Width); for (int i = 0; i < 9; i++) { for (int j = 0; j < 5; j++) { Assert.Equal(0, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8)]); Assert.Equal(0, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 1]); Assert.Equal(128, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 2]); Assert.Equal(255, image.Data[i * image.Stride + (j * image.BitsPerPixel / 8) + 3]); } } }
public void ParseSimpleBc3() { var image = Pfim.FromFile(Path.Combine("data", "bc3-simple-srgb.dds")); Assert.True(image is Dxt5Dds); Assert.Equal(DxgiFormat.BC3_UNORM_SRGB, ((Dxt5Dds)image).Header10?.DxgiFormat); byte[] data = new byte[64 * 64 * 4]; for (int i = 0; i < data.Length; i += 4) { data[i] = 255; data[i + 1] = 189; data[i + 2] = 189; data[i + 3] = 255; } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void ParseSimpleBc6h() { var image = Pfim.FromFile(Path.Combine("data", "bc6h-simple.dds")); Assert.True(image is Bc6hDds); Assert.Equal(DxgiFormat.BC6H_UF16, ((Bc6hDds)image).Header10?.DxgiFormat); byte[] data = new byte[64 * 64 * 4]; for (int i = 0; i < data.Length; i += 4) { data[i] = 255; // b data[i + 1] = 128; // g data[i + 2] = 128; // r data[i + 3] = 255; // a } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void TestDdsMipMap1() { var image = Pfim.FromFile(Path.Combine("data", "wose_BC1_UNORM.DDS")); var expectedMips = new[] { new MipMapOffset(36, 36, 108, 15552, 3888), new MipMapOffset(18, 18, 60, 19440, 1200), new MipMapOffset(9, 9, 36, 20640, 432), new MipMapOffset(4, 4, 12, 21072, 48), new MipMapOffset(2, 2, 12, 21120, 48), new MipMapOffset(1, 1, 12, 21168, 48) }; Assert.Equal(expectedMips, image.MipMaps); Assert.Equal(21168 + 48, image.Data.Length); image = Dds.Create(File.ReadAllBytes(Path.Combine("data", "wose_BC1_UNORM.DDS")), new PfimConfig()); Assert.Equal(expectedMips, image.MipMaps); Assert.Equal(21168 + 48, image.Data.Length); }
public void TestDdsMipMap1() { var image = Pfim.FromFile(Path.Combine("data", "wose_BC1_UNORM.DDS")); var expectedMips = new[] { new MipMapOffset(36, 36, 144, 20736, 5184), new MipMapOffset(18, 18, 80, 25920, 1600), new MipMapOffset(9, 9, 48, 27520, 576), new MipMapOffset(4, 4, 16, 28096, 64), new MipMapOffset(2, 2, 16, 28160, 64), new MipMapOffset(1, 1, 16, 28224, 64) }; Assert.Equal(expectedMips, image.MipMaps); Assert.Equal(28224 + 64, image.Data.Length); image = Dds.Create(File.ReadAllBytes(Path.Combine("data", "wose_BC1_UNORM.DDS")), new PfimConfig()); Assert.Equal(expectedMips, image.MipMaps); Assert.Equal(28224 + 64, image.Data.Length); }
public void ParseSimpleBc7() { var image = Pfim.FromFile(Path.Combine("data", "bc7-simple.dds")); byte[] data = new byte[64 * 64 * 4]; for (int i = 0; i < data.Length; i += 4) { // Format says Rgba32 (r at least significant) // but it is Bgra32 (b at least significant) // this mistake seems to be the case with all // decoders, so I followed suit data[i] = 255; // b data[i + 1] = 128; // g data[i + 2] = 129; // r data[i + 3] = 255; // a } Assert.Equal(data, image.Data); Assert.Equal(64, image.Height); Assert.Equal(64, image.Width); }
public void ParseTargaTopLeft() { bool seenBlue = false; var image = Pfim.FromFile(Path.Combine("data", "rgb24_top_left.tga")); for (int i = 0; i < image.Data.Length; i += 3) { seenBlue |= image.Data[i] == 12 && image.Data[i + 1] == 0 && image.Data[i + 2] == 255; if (image.Data[i] == 255 && image.Data[i + 1] == 4 && image.Data[i + 2] == 4 && !seenBlue) { Assert.True(false, "Expected to see blue before red (this could mean that the color channels are swapped)"); } if (!((image.Data[i] == 0 && image.Data[i + 1] == 255 && image.Data[i + 2] == 0) || (image.Data[i] == 255 && image.Data[i + 1] == 0 && image.Data[i + 2] == 12) || (image.Data[i] == 255 && image.Data[i + 1] == 255 && image.Data[i + 2] == 255) || (image.Data[i + 2] == 255 && image.Data[i + 1] == image.Data[i]))) { Assert.True(false, $"Did not expect pixel {image.Data[i]} {image.Data[i + 1]} {image.Data[i + 2]}"); } } }