Beispiel #1
0
        public void DecodeBmp256()
        {
            string bmp256 = Path.Join("Assets", "Images", "256colorbmp.bmp");

            byte[] bytes = File.ReadAllBytes(bmp256);
            Assert.True(BmpFormat.IsBmp(bytes));

            byte[] decodedPixelData = BmpFormat.Decode(bytes, out BmpFileHeader fileHeader);
            Assert.True(decodedPixelData != null);

            Runner.ExecuteAsLoop(_ =>
            {
                var r = new Texture(
                    new Vector2(fileHeader.Width, fileHeader.Height),
                    decodedPixelData,
                    PixelFormat.Bgra
                    )
                {
                    FlipY = true
                };

                RenderComposer composer = Engine.Renderer.StartFrame();
                composer.RenderSprite(Vector3.Zero, new Vector2(fileHeader.Width, fileHeader.Height), Color.White, r);
                Engine.Renderer.EndFrame();
                Runner.VerifyScreenshot(ResultDb.Bmp256ColorDecode);
                r.Dispose();
            }).WaitOne();
        }
        protected override void CreateInternal(byte[] data)
        {
            byte[] pixels  = null;
            var    width   = 0;
            var    height  = 0;
            var    flipped = false; // Whether the image was uploaded flipped - top to bottom.

            PerfProfiler.ProfilerEventStart("Decoding Image", "Loading");

            // Check if PNG.
            if (PngFormat.IsPng(data))
            {
                pixels = PngFormat.Decode(data, out PngFileHeader header);
                width  = header.Width;
                height = header.Height;
            }
            // Check if BMP.
            else if (BmpFormat.IsBmp(data))
            {
                pixels  = BmpFormat.Decode(data, out BmpFileHeader header);
                width   = header.Width;
                height  = header.Height;
                flipped = true;
            }

            if (pixels == null || width == 0 || height == 0)
            {
                Engine.Log.Warning($"Couldn't load texture - {Name}.", MessageSource.AssetLoader);
                return;
            }

            PerfProfiler.ProfilerEventEnd("Decoding Image", "Loading");
            UploadTexture(new Vector2(width, height), pixels, flipped);
        }
Beispiel #3
0
        protected override void CreateInternal(ReadOnlyMemory <byte> data)
        {
            byte[]  pixels  = null;
            Vector2 size    = Vector2.Zero;
            var     flipped = false; // Whether the image was uploaded flipped - top to bottom.
            var     format  = PixelFormat.Unknown;

            PerfProfiler.ProfilerEventStart("Decoding Image", "Loading");

            // Magic number check to find out format.
            if (PngFormat.IsPng(data))
            {
                pixels = PngFormat.Decode(data, out PngFileHeader header);
                size   = header.Size;
                format = header.PixelFormat;
            }
            else if (BmpFormat.IsBmp(data))
            {
                pixels  = BmpFormat.Decode(data, out BmpFileHeader header);
                size    = new Vector2(header.Width, header.HeaderSize);
                flipped = true;
                format  = PixelFormat.Bgra;
            }
            else if (ImgBinFormat.IsImgBin(data))
            {
                pixels = ImgBinFormat.Decode(data, out ImgBinFileHeader header);
                size   = header.Size;
                format = header.Format;
            }

            if (pixels == null || size.X == 0 || size.Y == 0)
            {
                Texture = Texture.EmptyWhiteTexture;
                Engine.Log.Warning($"Couldn't load texture - {Name}.", MessageSource.AssetLoader);
                return;
            }

            ByteSize = pixels.Length;
            PerfProfiler.ProfilerEventEnd("Decoding Image", "Loading");
            UploadTexture(size, pixels, flipped, format);
        }