internal static Avalonia.Media.Imaging.WriteableBitmap GetHB(double S) { Avalonia.Media.Imaging.WriteableBitmap bmp = new Avalonia.Media.Imaging.WriteableBitmap(new PixelSize(256, 256), new Vector(96, 96), Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Unpremul); using (var fb = bmp.Lock()) { unsafe { byte *rgbaValues = (byte *)fb.Address; for (int x = 0; x < 256; x++) { for (int y = 0; y < 256; y++) { HSVToRGB(x / 255.0, S, 1 - y / 255.0, out rgbaValues[x * 4 + y * 256 * 4], out rgbaValues[x * 4 + y * 256 * 4 + 1], out rgbaValues[x * 4 + y * 256 * 4 + 2] ); rgbaValues[x * 4 + y * 256 * 4 + 3] = 255; } } } } return(bmp); }
public static Avalonia.Media.Imaging.WriteableBitmap GetG(byte R, byte B) { Avalonia.Media.Imaging.WriteableBitmap bmp = new Avalonia.Media.Imaging.WriteableBitmap(new PixelSize(1, 256), new Vector(96, 96), Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Unpremul); using (var fb = bmp.Lock()) { unsafe { byte *rgbaValues = (byte *)fb.Address; for (int y = 0; y < 256; y++) { rgbaValues[y * 4] = R; rgbaValues[y * 4 + 1] = (byte)(255 - y); rgbaValues[y * 4 + 2] = B; rgbaValues[y * 4 + 3] = 255; } } } return(bmp); }
internal static Avalonia.Media.Imaging.WriteableBitmap GetHS(double B) { Avalonia.Media.Imaging.WriteableBitmap bmp = new Avalonia.Media.Imaging.WriteableBitmap(new PixelSize(256, 256), new Vector(96, 96), Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Unpremul); using (var fb = bmp.Lock()) { unsafe { byte *rgbaValues = (byte *)fb.Address; for (int x = 0; x < 256; x++) { for (int y = 0; y < 256; y++) { double r = Math.Sqrt((x - 128) * (x - 128) + (y - 128) * (y - 128)); if (r <= 130) { double theta = Math.Atan2(y - 128, x - 128) / Math.PI; HSVToRGB(theta * 0.5, Math.Min(1, r / 128.0), B, out rgbaValues[x * 4 + y * 256 * 4], out rgbaValues[x * 4 + y * 256 * 4 + 1], out rgbaValues[x * 4 + y * 256 * 4 + 2] ); if (r >= 126) { rgbaValues[x * 4 + y * 256 * 4 + 3] = (byte)Math.Max(0, Math.Min(255, 32640 - 255 * r)); } else { rgbaValues[x * 4 + y * 256 * 4 + 3] = 255; } } } } } } return(bmp); }
internal static WriteableBitmap GetShiftedS(WriteableBitmap originalS, double vRadius, bool front) { int delta = (int)Math.Round(vRadius * 256 / 56); int[] shifts = new int[128]; for (int i = 0; i < 128; i++) { double cos = (63.5 - i) / 63.5; shifts[i] = (int)Math.Round(Math.Sqrt(1 - cos * cos) * delta); } Avalonia.Media.Imaging.WriteableBitmap bmp = new Avalonia.Media.Imaging.WriteableBitmap(new PixelSize(128, 256 + delta), new Vector(96, 96), Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Unpremul); if (front) { using (var fb = bmp.Lock()) { using (var fb2 = originalS.Lock()) { unsafe { byte *rgbaValues = (byte *)fb.Address; byte *rgbaValuesOrig = (byte *)fb2.Address; for (int x = 0; x < 128; x++) { for (int y = shifts[x]; y < 256 + shifts[x]; y++) { rgbaValues[x * 4 + y * 128 * 4] = rgbaValuesOrig[(127 - x) * 4 + (y - shifts[x]) * 256 * 4]; rgbaValues[x * 4 + y * 128 * 4 + 1] = rgbaValuesOrig[(127 - x) * 4 + (y - shifts[x]) * 256 * 4 + 1]; rgbaValues[x * 4 + y * 128 * 4 + 2] = rgbaValuesOrig[(127 - x) * 4 + (y - shifts[x]) * 256 * 4 + 2]; rgbaValues[x * 4 + y * 128 * 4 + 3] = 255; } } } } } } else { using (var fb = bmp.Lock()) { using (var fb2 = originalS.Lock()) { unsafe { byte *rgbaValues = (byte *)fb.Address; byte *rgbaValuesOrig = (byte *)fb2.Address; for (int x = 0; x < 128; x++) { for (int y = delta - shifts[x]; y < 256 + delta - shifts[x]; y++) { rgbaValues[x * 4 + y * 128 * 4] = rgbaValuesOrig[(128 + x) * 4 + (y - delta + shifts[x]) * 256 * 4]; rgbaValues[x * 4 + y * 128 * 4 + 1] = rgbaValuesOrig[(128 + x) * 4 + (y - delta + shifts[x]) * 256 * 4 + 1]; rgbaValues[x * 4 + y * 128 * 4 + 2] = rgbaValuesOrig[(128 + x) * 4 + (y - delta + shifts[x]) * 256 * 4 + 2]; rgbaValues[x * 4 + y * 128 * 4 + 3] = 255; } } } } } } return(bmp); }
/* * public void Resize(PixelSize size) { * if (size == Size) return; * Size = size; * * Dispose(); * * if (IsEmpty()) return; * * AvaloniaBitmap = new AI.WriteableBitmap( * Size, * new Vector(1, 1), // DPI scale ? * //Avalonia.Platform.PixelFormat.Rgba8888 * Avalonia.Platform.PixelFormat.Bgra8888 // seems faster for me! // like System.Drawing.Imaging.PixelFormat.Format32bppArgb * ); * * for (int i = 0; i < SystemBitmaps.Length; ++i) { * SystemBitmaps[i] = new SD.Bitmap( * Size.Width, Size.Height, * SD.Imaging.PixelFormat.Format32bppArgb * ); * } * } */ public bool CopyPixels(int sourceIndex) // copy pixels from SystemBitmap to AvaloniaBitmap. UI thread { //Debug.WriteLine("CopyPixels begin"); SD.Bitmap systemBitmap = SystemBitmaps[sourceIndex]; if (systemBitmap == null) { return(false); } if (AvaloniaBitmap == null) { return(false); } using (var buf1 = AvaloniaBitmap.Lock()) { long length1 = buf1.Size.Height * buf1.RowBytes; var buf0 = systemBitmap.LockBits( new SD.Rectangle(SD.Point.Empty, systemBitmap.Size), SD.Imaging.ImageLockMode.ReadOnly, systemBitmap.PixelFormat ); long length0 = buf0.Height * buf0.Stride; if (length1 == length0) { // quick. just copy memory unsafe { System.Buffer.MemoryCopy( buf0.Scan0.ToPointer(), buf1.Address.ToPointer(), length1, length0 ); } } else { // slow. copy by line. may occure on resizing int h = Math.Min(buf0.Height, buf1.Size.Height); // in pixels int w = Math.Min(buf0.Stride, buf1.RowBytes); // in bytes unsafe { for (int i = 0; i < h; ++i) { System.Buffer.MemoryCopy( (buf0.Scan0 + buf0.Stride * i).ToPointer(), (buf1.Address + buf1.RowBytes * i).ToPointer(), w, w ); } } } systemBitmap.UnlockBits(buf0); } //Debug.WriteLine("CopyPixels end"); return(true); }