Beispiel #1
0
        private static Bitmap ReadBLP(String filePath)
        {
            FileStream fs      = File.OpenRead(filePath);
            BlpFile    blpFile = new BlpFile(fs);
            int        width;
            int        height;

            // The library does not determine what's BLP1 and BLP2 properly, so we manually set bool bgra in GetPixels depending on the checkbox.
            byte[] bytes         = blpFile.GetPixels(0, out width, out height, blpFile._isBLP2); // 0 indicates first mipmap layer. width and height are assigned width and height in GetPixels().
            var    actualImage   = blpFile.GetBitmapSource(0);
            int    bytesPerPixel = (actualImage.Format.BitsPerPixel + 7) / 8;
            int    stride        = bytesPerPixel * actualImage.PixelWidth;

            // blp read and convert
            Bitmap image = new Bitmap(width, height);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    var offset = (y * stride) + (x * bytesPerPixel);

                    byte red;
                    byte green;
                    byte blue;
                    byte alpha = 0;

                    red   = bytes[offset + 0];
                    green = bytes[offset + 1];
                    blue  = bytes[offset + 2];
                    alpha = bytes[offset + 3];

                    image.SetPixel(x, y, Color.FromArgb(alpha, blue, green, red)); // assign color to pixel
                }
            }

            blpFile.Dispose();

            return(image);
        }
Beispiel #2
0
        public void TestGetBlpBitmapSource(string inputImagePath, string expectedImagePath, int mipMapLevel)
        {
            using (var fileStream = File.OpenRead(inputImagePath))
            {
                var expectedImage = new Bitmap(expectedImagePath);
                var blpFile       = new BlpFile(fileStream);
                var actualImage   = blpFile.GetBitmapSource(mipMapLevel);

                Assert.AreEqual(expectedImage.Width, actualImage.PixelWidth);
                Assert.AreEqual(expectedImage.Height, actualImage.PixelHeight);

                var bytesPerPixel = (actualImage.Format.BitsPerPixel + 7) / 8;
                var stride        = bytesPerPixel * actualImage.PixelWidth;
                var bytes         = new byte[stride * actualImage.PixelHeight];
                actualImage.CopyPixels(bytes, stride, 0);

                for (var y = 0; y < expectedImage.Height; y++)
                {
                    for (var x = 0; x < expectedImage.Width; x++)
                    {
                        var offset = (y * stride) + (x * bytesPerPixel);

                        // Assumes actualImage.Format is either PixelFormats.Bgr32 or PixelFormats.Bgra32
                        Assert.AreEqual(expectedImage.GetPixel(x, y).B, bytes[offset + 0]);
                        Assert.AreEqual(expectedImage.GetPixel(x, y).G, bytes[offset + 1]);
                        Assert.AreEqual(expectedImage.GetPixel(x, y).R, bytes[offset + 2]);

                        if (bytesPerPixel > 3)
                        {
                            Assert.AreEqual(expectedImage.GetPixel(x, y).A, bytes[offset + 3]);
                        }
                    }
                }

                expectedImage.Dispose();
                blpFile.Dispose();
            }
        }