static void Draw(dynamic map, WDT.WDTFileDataId chunk, ref NetVips.Image canvas, int positionX, int positionY) { Stream chunkStream = null; try { var casc = new CASC(); chunkStream = casc.File(BuildConfig, chunk.fileDataId); } catch { Console.WriteLine($"Failed to download chunk {chunk.x}_{chunk.y} for map {map.MapName_lang} ({map.ID})"); return; } var blpStream = new MemoryStream(); var blpFile = new BlpFile(chunkStream); var bitmap = blpFile.GetBitmap(0); bitmap.Save(blpStream, System.Drawing.Imaging.ImageFormat.Png); var image = NetVips.Image.NewFromBuffer(blpStream.ToArray(), access: Enums.Access.Sequential); canvas = canvas.Insert(image, positionX * bitmap.Width, positionY * bitmap.Height, true); blpStream.Dispose(); blpFile.Dispose(); bitmap.Dispose(); image.Dispose();; }
public void TestGetBlpBitmap(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.GetBitmap(mipMapLevel); Assert.AreEqual(expectedImage.Width, actualImage.Width); Assert.AreEqual(expectedImage.Height, actualImage.Height); for (var y = 0; y < expectedImage.Height; y++) { for (var x = 0; x < expectedImage.Width; x++) { // Allow pixel values to be slightly different, since some testcases were decoded with WPF (BitmapSource), not SkiaSharp. const int delta = 1; var expectedPixel = expectedImage.GetPixel(x, y); var actualPixel = actualImage.GetPixel(x, y); var message = $"Expected:<{expectedPixel}>. Actual:<{actualPixel}>"; Assert.IsTrue(Math.Abs(expectedPixel.A - actualPixel.A) <= delta, message); Assert.IsTrue(Math.Abs(expectedPixel.R - actualPixel.R) <= delta, message); Assert.IsTrue(Math.Abs(expectedPixel.G - actualPixel.G) <= delta, message); Assert.IsTrue(Math.Abs(expectedPixel.B - actualPixel.B) <= delta, message); } } expectedImage.Dispose(); blpFile.Dispose(); } }
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); }
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(); } }