1 //
2 // Copyright (c) 2010-2021 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System.IO;
9 using BigGustave;
10 using Antmicro.Renode.Backends.Display;
11 using Antmicro.Renode.Exceptions;
12 
13 namespace Antmicro.Renode.Utilities
14 {
15     public struct RawImageData
16     {
RawImageDataAntmicro.Renode.Utilities.RawImageData17         public RawImageData(byte[] bytes, int width, int height)
18         {
19             if(bytes.Length != width * height * PixelFormat.GetColorDepth())
20             {
21                 throw new RecoverableException("Number of bytes does not correspond with specified dimensions.");
22             }
23             Bytes = bytes;
24             Width = width;
25             Height = height;
26         }
27 
ToPngAntmicro.Renode.Utilities.RawImageData28         public Stream ToPng()
29         {
30             var stream = new MemoryStream();
31             var builder = PngBuilder.Create(Width, Height, false);
32             for(int y = 0; y < Height; ++y)
33             {
34                 for(int x = 0; x < Width; ++x)
35                 {
36                     var p = (Width * y + x) * 4;
37                     builder.SetPixel(new BigGustave.Pixel(Bytes[p], Bytes[p + 1], Bytes[p + 2], Bytes[p + 3], false), x, y);
38                 }
39             }
40             builder.Save(stream);
41             stream.Seek(0, SeekOrigin.Begin);
42             return stream;
43         }
44 
45         public byte[] Bytes { get; }
46         public int Width { get; }
47         public int Height { get; }
48         public const PixelFormat PixelFormat = Antmicro.Renode.Backends.Display.PixelFormat.RGBA8888;
49     }
50 }
51