private unsafe void ApplyExposureCompensationToPixel(byte *blue, double factor) { for (Argb i = Argb.blue; i <= Argb.red; ++i) { byte componentValue = *(blue + (byte)i); *(blue + (byte)i) = RgbComponentOperations.Exposure(componentValue, factor); } }
private unsafe void ApplyGammaCorrectionToPixel(byte *blue, double factor) { for (Argb i = Argb.blue; i <= Argb.red; ++i) { byte componentValue = *(blue + (byte)i); *(blue + (byte)i) = RgbComponentOperations.Gamma(componentValue, factor); } }
private unsafe void ApplyInversionToPixel(byte *blue, double factor) { for (Argb i = Argb.blue; i <= Argb.red; ++i) { byte componentValue = *(blue + (byte)i); *(blue + (byte)i) = RgbComponentOperations.Invert(componentValue, (int)factor); } }
private unsafe void ApplyBrightnessToPixel(byte *blue, double factor) { for (Argb i = Argb.blue; i <= Argb.red; ++i) { byte componentValue = *(blue + (byte)i); *(blue + (byte)i) = RgbComponentOperations.ChangeBrightness(componentValue, factor); } }
public static Image GrayScaleImage(Image srcImage) { try { int width = srcImage.Width; int height = srcImage.Height; Rectangle rect = new Rectangle(0, 0, width, height); Bitmap newBmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed) { Palette = GrayScalePalette }; using (Bitmap srcBmp = new Bitmap(width, height, PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(srcBmp)) g.DrawImage(srcImage, 0, 0); BitmapData srcData = srcBmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData newData = newBmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); Debug.Assert(srcData.Stride > 0); Debug.Assert(newData.Stride > 0); int srcStride = srcData.Stride; int newStride = newData.Stride; unsafe { byte *pSrc = (byte *)srcData.Scan0.ToPointer(); byte *pNew = (byte *)newData.Scan0.ToPointer(); Argb *ps; byte *pn; for (int y = 0; y < height; ++y) { ps = (Argb *)(pSrc + (srcStride * y)); pn = (byte *)(pNew + (newStride * y)); for (int x = 0; x < width; ++x) { Argb color = *ps; * pn = (byte)((color.red * 0.299) + (color.green * 0.587) + (color.blue * 0.114) + 0.5); ++ps; ++pn; } } } srcBmp.UnlockBits(srcData); newBmp.UnlockBits(newData); } return(newBmp); } catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; } }
public override int GetHashCode() { unchecked { int hash = 397; hash = hash * 23 + Argb.GetHashCode(); hash = hash * 23 + Auto?.GetHashCode() ?? 0; hash = hash * 23 + Indexed?.GetHashCode() ?? 0; hash = hash * 23 + Theme?.GetHashCode() ?? 0; hash = hash * 23 + Tint?.GetHashCode() ?? 0; return(hash); } }
public void Argb() { // Test the limits. Assert.Equal((uint)0x0, new Argb(Vector4.Zero).PackedValue); Assert.Equal(0xFFFFFFFF, new Argb(Vector4.One).PackedValue); // Test ToVector4. Assert.True(Equal(Vector4.One, new Argb(Vector4.One).ToVector4())); Assert.True(Equal(Vector4.Zero, new Argb(Vector4.Zero).ToVector4())); Assert.True(Equal(Vector4.UnitX, new Argb(Vector4.UnitX).ToVector4())); Assert.True(Equal(Vector4.UnitY, new Argb(Vector4.UnitY).ToVector4())); Assert.True(Equal(Vector4.UnitZ, new Argb(Vector4.UnitZ).ToVector4())); Assert.True(Equal(Vector4.UnitW, new Argb(Vector4.UnitW).ToVector4())); // Test clamping. Assert.True(Equal(Vector4.Zero, new Argb(Vector4.One * -1234.0f).ToVector4())); Assert.True(Equal(Vector4.One, new Argb(Vector4.One * +1234.0f).ToVector4())); float x = +0.1f; float y = -0.3f; float z = +0.5f; float w = -0.7f; Argb argb = new Argb(x, y, z, w); Assert.Equal(0x001a0080u, argb.PackedValue); // Test ordering byte[] rgb = new byte[3]; byte[] rgba = new byte[4]; byte[] bgr = new byte[3]; byte[] bgra = new byte[4]; argb.ToXyzBytes(rgb, 0); Assert.Equal(rgb, new byte[] { 0x1a, 0, 0x80 }); argb.ToXyzwBytes(rgba, 0); Assert.Equal(rgba, new byte[] { 0x1a, 0, 0x80, 0 }); argb.ToZyxBytes(bgr, 0); Assert.Equal(bgr, new byte[] { 0x80, 0, 0x1a }); argb.ToZyxwBytes(bgra, 0); Assert.Equal(bgra, new byte[] { 0x80, 0, 0x1a, 0 }); }
public Module(Keys key, int x, int y) { name = this.GetType().Name; //AutoLaugh => Auto Laugh for (int i = 1; i < name.Length; i++) { if (Char.IsUpper(name[i])) { name = name.Insert(i, " "); i += 1; } } displayText = name + "(" + Enum.GetName(typeof(Keys), key) + "): " + ((on) ? "On" : "Off"); on = false; this.x = x; this.y = y; color = new Argb(255, 0, 255, 255); this.Key = key; }
private unsafe void ApplyFilterToBitmap(LockedBitmap intermediate, LockedBitmap bitmap) { Parallel.For(0, bitmap.HeightInPixels, iY => { byte *outputCurrentByte = bitmap.FirstByte + (iY * bitmap.Stride); byte *intermediateCurrentByte = intermediate.FirstByte + ((iY + gap) * intermediate.Stride) + gapInBytes; for (int i = 0; i < bitmap.WidthInBytes; i += bytesPerPixel) { for (Argb j = Argb.blue; j <= Argb.red; ++j) { *(outputCurrentByte + (byte)j) = ComputeNewRgbComponentValue(GetRgbComponentNeighborhood(intermediate, intermediateCurrentByte + (byte)j)); } *(outputCurrentByte + (byte)Argb.alfa) = *(intermediateCurrentByte + (byte)Argb.alfa); outputCurrentByte += bytesPerPixel; intermediateCurrentByte += bytesPerPixel; } }); }
private static WriteableBitmap _Get(string name) { lock (_lock) { if (name != _loadedName) { ManagedBitmap managedBitmap; using (var stream = new System.IO.FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); var frame = decoder.Frames[0]; managedBitmap = new ManagedBitmap(frame); } using (var lockedBitmap = managedBitmap.Lock()) { int height = lockedBitmap.Height; int width = lockedBitmap.Width; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { var color = lockedBitmap[x, y]; if (color.Red < 10 && color.Green < 10 && color.Blue < 10) { lockedBitmap[x, y] = new Argb(); } } } } _bitmap = managedBitmap.WriteableBitmap; _loadedName = name; } return(_bitmap); } }
public BasicStyle(Argb stroke, Argb fill, double thickness) { Stroke = stroke; Fill = fill; Thickness = thickness; }
public static Hsv RgbToHsv(Argb argb) { // In this function, R, G, and B values must be scaled // to be between 0 and 1. // HSV.Hue will be a value between 0 and 360, and // HSV.Saturation and value are between 0 and 1. // The code must scale these to be between 0 and 255 for // the purposes of this application. var r = (double)argb.Red / 255; var g = (double)argb.Green / 255; var b = (double)argb.Blue / 255; var min = Math.Min(Math.Min(r, g), b); var max = Math.Max(Math.Max(r, g), b); double h; double s; var v = max; var delta = max - min; if (Math.Abs(max) < 0.01 || Math.Abs(delta) < 0.01) { // R, G, and B must be 0, or all the same. // In this case, S is 0, and H is undefined. // Using H = 0 is as good as any... s = 0; h = 0; } else { s = delta / max; if (Math.Abs(r - max) < 0.01) { // Between Yellow and Magenta h = (g - b) / delta; } else if (Math.Abs(g - max) < 0.01) { // Between Cyan and Yellow h = 2 + (b - r) / delta; } else { // Between Magenta and Cyan h = 4 + (r - g) / delta; } } // Scale h to be between 0 and 360. // This may require adding 360, if the value // is negative. h *= 60; if (h < 0) { h += 360; } // Scale to the requirements of this // application. All values are between 0 and 255. return(new Hsv(argb.Alpha, (int)(h / 360 * 255), (int)(s * 255), (int)(v * 255))); }
private static WriteableBitmap _Get(string name) { lock(_lock) { if (name != _loadedName) { ManagedBitmap managedBitmap; using (var stream = new System.IO.FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); var frame = decoder.Frames[0]; managedBitmap = new ManagedBitmap(frame); } using(var lockedBitmap = managedBitmap.Lock()) { int height = lockedBitmap.Height; int width = lockedBitmap.Width; for(int y=0; y<height; y++) { for(int x=0; x<width; x++) { var color = lockedBitmap[x, y]; if (color.Red < 10 && color.Green < 10 && color.Blue < 10) lockedBitmap[x, y] = new Argb(); } } } _bitmap = managedBitmap.WriteableBitmap; _loadedName = name; } return _bitmap; } }
private static void PaintColorSchemeToBitmapT <T>(this IRaster raster, T noData, Func <int, int, T> getValue, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) where T : struct, IEquatable <T>, IComparable <T> { if (raster == null) { throw new ArgumentNullException(nameof(raster)); } if (rasterSymbolizer == null) { throw new ArgumentNullException(nameof(rasterSymbolizer)); } if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) { return; } BitmapData bmpData; var numRows = raster.NumRows; var numColumns = raster.NumColumns; var rect = new Rectangle(0, 0, numColumns, numRows); try { bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch { var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.MemoryBmp); ms.Position = 0; bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } // Prepare progress meter var pm = new ProgressMeter(progressHandler, SymbologyMessageStrings.DesktopRasterExt_PaintingColorScheme, numRows); if (numRows * numColumns < 100000) { pm.StepPercent = 50; } if (numRows * numColumns < 500000) { pm.StepPercent = 10; } if (numRows * numColumns < 1000000) { pm.StepPercent = 5; } var sets = GetColorSets <T>(rasterSymbolizer.Scheme.Categories); var noDataColor = Argb.FromColor(rasterSymbolizer.NoDataColor); var alpha = Argb.ByteRange(Convert.ToInt32(rasterSymbolizer.Opacity * 255)); var ptr = bmpData.Scan0; for (var row = 0; row < numRows; row++) { for (var col = 0; col < numColumns; col++) { var val = getValue(row, col); Argb argb; if (val.Equals(noData)) { argb = noDataColor; } else { // Usually values are not random, so check neighboring previous cells for same color int?srcOffset = null; if (col > 0) { if (val.Equals(getValue(row, col - 1))) { srcOffset = Offset(row, col - 1, bmpData.Stride); } } if (srcOffset == null && row > 0) { if (val.Equals(getValue(row - 1, col))) { srcOffset = Offset(row - 1, col, bmpData.Stride); } } if (srcOffset != null) { argb = new Argb(Marshal.ReadByte(ptr, (int)srcOffset + 3), Marshal.ReadByte(ptr, (int)srcOffset + 2), Marshal.ReadByte(ptr, (int)srcOffset + 1), Marshal.ReadByte(ptr, (int)srcOffset)); } else { var color = GetColor(sets, val); argb = new Argb(alpha, color.R, color.G, color.B); } } var offset = Offset(row, col, bmpData.Stride); Marshal.WriteByte(ptr, offset, argb.B); Marshal.WriteByte(ptr, offset + 1, argb.G); Marshal.WriteByte(ptr, offset + 2, argb.R); Marshal.WriteByte(ptr, offset + 3, argb.A); } pm.CurrentValue = row; } pm.Reset(); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(bmpData.Stride, bmpData.Width, bmpData.Height, bmpData.Scan0, progressHandler); mySmoother.Smooth(); } bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; }
public string ToHex() { return("#" + Argb.ToString("X8")); }
private static Argb GetColor <T>(IEnumerable <ColorSet <T> > sets, T value) where T : struct, IComparable <T> { foreach (var set in sets) { if (set.Contains(value)) { if (!set.Gradient) { return(set.Color); } if (set.Min == null || set.Max == null) { return(set.Color); } double lowVal = Convert.ToDouble(set.Min.Value); double range = Math.Abs(Convert.ToDouble(set.Max.Value) - lowVal); double p = 0; // the portion of the range, where 0 is LowValue & 1 is HighValue double ht; double dVal = Convert.ToDouble(value); switch (set.GradientModel) { case GradientModel.Linear: p = (dVal - lowVal) / range; break; case GradientModel.Exponential: ht = dVal; if (ht < 1) { ht = 1.0; } if (range > 1) { p = Math.Pow(ht - lowVal, 2) / Math.Pow(range, 2); } else { return(set.Color); } break; case GradientModel.Logarithmic: ht = dVal; if (ht < 1) { ht = 1.0; } if (range > 1.0 && ht - lowVal > 1.0) { p = Math.Log(ht - lowVal) / Math.Log(range); } else { return(set.Color); } break; } return(new Argb(set.MinA + (int)(set.RangeA * p), set.MinR + (int)(set.RangeR * p), set.MinG + (int)(set.RangeG * p), set.MinB + (int)(set.RangeB * p))); } } return(Argb.FromColor(Color.Transparent)); }
private static List <ColorSet <T> > GetColorSets <T>(IEnumerable <IColorCategory> categories) where T : struct, IComparable <T> { var result = new List <ColorSet <T> >(); foreach (var c in categories) { var cs = new ColorSet <T>(); Color high = c.HighColor; Color low = c.LowColor; cs.Color = Argb.FromColor(low); if (high != low) { cs.GradientModel = c.GradientModel; cs.Gradient = true; cs.MinA = low.A; cs.MinR = low.R; cs.MinG = low.G; cs.MinB = low.B; cs.RangeA = high.A - cs.MinA; cs.RangeR = high.R - cs.MinR; cs.RangeG = high.G - cs.MinG; cs.RangeB = high.B - cs.MinB; } cs.Max = Global.MaximumValue <T>(); var testMax = Convert.ToDouble(cs.Max); cs.Min = Global.MinimumValue <T>(); var testMin = Convert.ToDouble(cs.Min); if (c.Range.Maximum != null && c.Range.Maximum < testMax) { if (c.Range.Maximum < testMin) { cs.Max = cs.Min; } else { cs.Max = (T)Convert.ChangeType(c.Range.Maximum.Value, typeof(T)); } } if (c.Range.Minimum != null && c.Range.Minimum > testMin) { if (c.Range.Minimum > testMax) { cs.Min = Global.MaximumValue <T>(); } else { cs.Min = (T)Convert.ChangeType(c.Range.Minimum.Value, typeof(T)); } } cs.MinInclusive = c.Range.MinIsInclusive; cs.MaxInclusive = c.Range.MaxIsInclusive; result.Add(cs); } // The normal order uses "overwrite" behavior, so that each color is drawn // if it qualifies until all the ranges are tested, overwriting previous. // This can be mimicked by going through the sets in reverse and choosing // the first that qualifies. For lots of color ranges, opting out of // a large portion of the range testing should be faster. result.Reverse(); return(result); }
private static void DrawToBitmapT <T>(IRaster raster, T noData, Func <int, int, T> getValue, Func <int, byte> getByte, Action <int, byte> setByte, IRasterSymbolizer rasterSymbolizer, int stride, ProgressMeter pm) where T : struct, IEquatable <T>, IComparable <T> { if (raster == null) { throw new ArgumentNullException(nameof(raster)); } if (rasterSymbolizer == null) { throw new ArgumentNullException(nameof(rasterSymbolizer)); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) { return; } float[][] hillshade = null; if (rasterSymbolizer.ShadedRelief.IsUsed) { pm.BaseMessage = "Calculating Shaded Relief"; hillshade = rasterSymbolizer.HillShade ?? raster.CreateHillShadeT(getValue, rasterSymbolizer.ShadedRelief, pm); } pm.BaseMessage = "Calculating Colors"; var sets = GetColorSets <T>(rasterSymbolizer.Scheme.Categories); var noDataColor = Argb.FromColor(rasterSymbolizer.NoDataColor); for (int row = 0; row < raster.NumRows; row++) { for (int col = 0; col < raster.NumColumns; col++) { var value = getValue(row, col); Argb argb; if (value.Equals(noData)) { argb = noDataColor; } else { // Usually values are not random, so check neighboring previous cells for same color int?srcOffset = null; if (col > 0) { if (value.Equals(getValue(row, col - 1))) { srcOffset = Offset(row, col - 1, stride); } } if (srcOffset == null && row > 0) { if (value.Equals(getValue(row - 1, col))) { srcOffset = Offset(row - 1, col, stride); } } if (srcOffset != null) { argb = new Argb(getByte((int)srcOffset + 3), getByte((int)srcOffset + 2), getByte((int)srcOffset + 1), getByte((int)srcOffset)); } else { argb = GetColor(sets, value); } } if (hillshade != null) { if (hillshade[row][col] == -1 || float.IsNaN(hillshade[row][col])) { argb = new Argb(argb.A, noDataColor.R, noDataColor.G, noDataColor.B); } else { var red = (int)(argb.R * hillshade[row][col]); var green = (int)(argb.G * hillshade[row][col]); var blue = (int)(argb.B * hillshade[row][col]); argb = new Argb(argb.A, red, green, blue); } } var offset = Offset(row, col, stride); setByte(offset, argb.B); setByte(offset + 1, argb.G); setByte(offset + 2, argb.R); setByte(offset + 3, argb.A); } pm.Next(); } }
public static Brush ToBrush(Argb color) { return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B)); }
public override string ToString() { return("#" + Argb.ToString("X8")); }
public static IBrush ToBrush(Argb color) { return(new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B))); }