1 // 2 // Copyright (c) 2010-2020 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using Xwt.Backends; 9 using Color = Xwt.Drawing.Color; 10 using BitmapImage = Xwt.Drawing.BitmapImage; 11 #if GUI_DISABLED 12 using Antmicro.Renode.Exceptions; 13 #else 14 #if PLATFORM_WINDOWS 15 using Xwt.WPFBackend; 16 using System.Windows.Media; 17 using System.Windows.Media.Imaging; 18 #else 19 using Xwt.GtkBackend; 20 using System.Runtime.InteropServices; 21 #endif 22 #endif 23 24 namespace Antmicro.Renode.Utilities 25 { 26 public static class BitmapImageExtensions 27 { Copy(this BitmapImage bmp, byte[] frame)28 public static void Copy(this BitmapImage bmp, byte[] frame) 29 { 30 #if GUI_DISABLED 31 throw new RecoverableException("The BitmapImageExtensions.Copy() method is not supported in the non-gui configuration"); 32 #else 33 var backend = bmp.GetBackend(); 34 #if PLATFORM_WINDOWS 35 var pixelFormat = PixelFormats.Bgra32; 36 var stride = (int)bmp.PixelWidth * (pixelFormat.BitsPerPixel / 8); // width * pixel size in bytes 37 var dpi = 96; // dots per inch - WPF supports automatic scaling 38 // by using the device independent pixel as its primary unit of measurement, 39 // which is 1/96 of an inch 40 ((WpfImage)backend).MainFrame = BitmapSource.Create((int)bmp.PixelWidth, (int)bmp.PixelHeight, dpi, dpi, pixelFormat, BitmapPalettes.WebPalette, frame, stride); 41 #else 42 var outBuffer = ((GtkImage)backend).Frames[0].Pixbuf.Pixels; 43 Marshal.Copy(frame, 0, outBuffer, frame.Length); 44 #endif 45 #endif 46 } 47 InvertColorOfPixel(this BitmapImage img, int x, int y)48 public static void InvertColorOfPixel(this BitmapImage img, int x, int y) 49 { 50 var color = img.GetPixel(x, y); 51 var invertedColor = Color.FromBytes((byte)(255 * (1.0 - color.Red)), (byte)(255 * (1.0 - color.Green)), (byte)(255 * (1.0 - color.Blue))); 52 img.SetPixel(x, y, invertedColor); 53 } 54 IsInImage(this BitmapImage img, int x, int y)55 public static bool IsInImage(this BitmapImage img, int x, int y) 56 { 57 return x >= 0 && x < img.PixelWidth && y >= 0 && y < img.PixelHeight; 58 } 59 DrawCursor(this BitmapImage img, int x, int y)60 public static void DrawCursor(this BitmapImage img, int x, int y) 61 { 62 const int CursorLength = 2; 63 for(var rx = -1 * CursorLength; rx <= CursorLength; rx++) 64 { 65 if(img.IsInImage(x + rx, y)) 66 { 67 img.InvertColorOfPixel(x + rx, y); 68 } 69 } 70 71 for(var ry = -1 * CursorLength; ry <= CursorLength; ry++) 72 { 73 if(img.IsInImage(x, y + ry) && ry != 0) 74 { 75 img.InvertColorOfPixel(x, y + ry); 76 } 77 } 78 } 79 } 80 } 81 82