public BitmapLayer(int width, int height, ColorBgra fillColor) : base(width, height) { this.surface = new Surface(width, height); // clear to see-through white, 0x00ffffff this.Surface.Clear(fillColor); this.properties = new BitmapLayerProperties(UserBlendOps.CreateDefaultBlendOp()); }
public unsafe override void Apply(ColorBgra* dst, ColorBgra* src, int length) { while (length > 0) { *dst = setColor; ++dst; --length; } }
public unsafe override void Apply(ColorBgra* ptr, int length) { while (length > 0) { *ptr = setColor; ++ptr; --length; } }
public HistogramRgb() : base(3, 256) { visualColors = new ColorBgra[]{ ColorBgra.Blue, ColorBgra.Green, ColorBgra.Red }; }
public static ColorBgra Blend(this ColorBgra a, ColorBgra b, double strength = 0.5d) { var bstr = Math.Min(1d, Math.Max(0d, strength)); var astr = 1 - bstr; return ColorBgra.FromBgra( (byte)(a.B * astr + b.B * bstr), (byte)(a.G * astr + b.G * bstr), (byte)(a.R * astr + b.R * bstr), (byte)(a.A * astr + b.A * bstr)); }
protected override ColorBgra Render(int x, int y, ColorBgra initial, Surface source) { var targetA = source[Offset(Bounds.Left, x, Bounds.Right), y]; initial = initial.Blend(targetA, GetBorderBlendValue(Bounds.Left, x, Bounds.Right)); var targetC = source[Offset(Bounds.Left, x, Bounds.Right), Offset(Bounds.Top, y, Bounds.Bottom)]; var targetB = source[x, Offset(Bounds.Top, y, Bounds.Bottom)]; targetB = targetB.Blend(targetC, GetBorderBlendValue(Bounds.Left, x, Bounds.Right)); initial = initial.Blend(targetB, GetBorderBlendValue(Bounds.Top, y, Bounds.Bottom)); return initial; }
public CurveControlLuminosity() : base(1, 256) { this.mask = new bool[1]{true}; visualColors = new ColorBgra[]{ ColorBgra.Black }; channelNames = new string[]{ PdnResources.GetString("CurveControlLuminosity.Luminosity") }; ResetControlPoints(); }
public unsafe virtual void Apply(ColorBgra* ptr, int length) { unsafe { while (length > 0) { *ptr = Apply(*ptr); ++ptr; --length; } } }
public override ColorBgra Apply(ColorBgra color) { int a = blendColor.A; int invA = 255 - a; int r = ((color.R * invA) + (blendColor.R * a)) / 256; int g = ((color.G * invA) + (blendColor.G * a)) / 256; int b = ((color.B * invA) + (blendColor.B * a)) / 256; byte a2 = ComputeAlpha(color.A, blendColor.A); return ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, a2); }
public unsafe override void Apply(ColorBgra *dst, ColorBgra *src, int length) { unsafe { while (length > 0) { *dst = Apply(*src); ++dst; ++src; --length; } } }
public unsafe virtual void Apply(ColorBgra *dst, ColorBgra *lhs, ColorBgra *rhs, int length) { unsafe { while (length > 0) { *dst = Apply(*lhs, *rhs); ++dst; ++lhs; ++rhs; --length; } } }
/// <summary> /// Override this to process the pixel in the second pass of the algorithm /// </summary> /// <param name="pixel">The pixel to quantize</param> /// <returns>The quantized value</returns> protected override byte QuantizePixel(ColorBgra *pixel) { byte paletteIndex = 0; if (!this.enableTransparency || pixel->A == 255) { paletteIndex = (byte)this.octree.GetPaletteIndex(pixel); } else { paletteIndex = (byte)this.maxColors; // maxColors will have a maximum value of 255 is enableTransparency is true } return paletteIndex; }
public CurveControlRgb() : base(3, 256) { this.mask = new bool[3] { true, true, true }; visualColors = new ColorBgra[] { ColorBgra.Red, ColorBgra.Green, ColorBgra.Blue }; channelNames = new string[]{ PdnResources.GetString("CurveControlRgb.Red"), PdnResources.GetString("CurveControlRgb.Green"), PdnResources.GetString("CurveControlRgb.Blue") }; ResetControlPoints(); }
public static ColorBgra AddColor(ColorBgra original, ColorBgra addition) { if (original.A == 255) { return original; } if (original.A == 0) { return addition; } byte addition_alpha = Math.Min(addition.A, (byte) (255 - original.A)); int total_alpha = original.A + addition_alpha; double orig_frac = original.A / (double) total_alpha; double add_frac = addition_alpha / (double) total_alpha; return ColorBgra.FromBgra(Int32Util.ClampToByte((int)(original.B * orig_frac + addition.B * add_frac)), Int32Util.ClampToByte((int)(original.G * orig_frac + addition.G * add_frac)), Int32Util.ClampToByte((int)(original.R * orig_frac + addition.R * add_frac)), Int32Util.ClampToByte(total_alpha)); }
public BarcodeSurface Create(Rectangle rect, Surface source, String text, ColorBgra primaryColor, ColorBgra secondaryColor) { BarcodeSurface barcode = new BarcodeSurface(rect); String encodedText = this.Encode(text); int barWidth = (int)System.Math.Floor((double)barcode.Width / encodedText.Length); int halfBarHeight = (int)System.Math.Floor((double)barcode.Height / 2.0); int currentHeight = 0; for (int y = rect.Top; y < rect.Bottom; y++) { int loc = 0; int step = 0; for (int x = rect.Left; x < rect.Right; x++) { if (loc < encodedText.Length && barWidth > 0 && halfBarHeight > 0) { if (encodedText[loc] == 'w' || (encodedText[loc] == 'b' && currentHeight <= halfBarHeight)) { barcode[x, y] = secondaryColor; } else if (encodedText[loc] == 'B' || (encodedText[loc] == 'b' && currentHeight > halfBarHeight)) { barcode[x, y] = primaryColor; } else { barcode[x,y] = source[x,y]; } step++; if (step % barWidth == 0) loc++; } else { barcode[x, y] = source[x, y]; } } currentHeight++; } return barcode; }
protected override ColorBgra Render(int x, int y, ColorBgra initial, Surface source) { var shade = initial.ToColor().GetBrightness(); if (Math.Abs(shade - BgShade) > ShadeThreshold) return initial; var norm = Normalize(initial); if (Math.Abs(BgNormal.R - norm.R) > ColorThreshold || Math.Abs(BgNormal.G - norm.G) > ColorThreshold || Math.Abs(BgNormal.B - norm.B) > ColorThreshold) { return initial; } var alpha = (byte)(Math.Min(255, Math.Max(0, Math.Max(Math.Max( Math.Abs(BgColor.R - initial.R), Math.Abs(BgColor.G - initial.G)), Math.Abs(BgColor.B - initial.B))) * (1d - AlphaFalloff))); return ColorBgra.FromBgra(initial.B, initial.G, initial.R, alpha); }
/// <summary> /// Blends four colors together based on the given weight values. /// </summary> /// <returns>The blended color.</returns> /// <remarks> /// The weights should be 16-bit fixed point numbers that add up to 65536 ("1.0"). /// 4W16IP means "4 colors, weights, 16-bit integer precision" /// </remarks> public static ColorBgra BlendColors4W16IP(ColorBgra c1, uint w1, ColorBgra c2, uint w2, ColorBgra c3, uint w3, ColorBgra c4, uint w4) { #if DEBUG if ((w1 + w2 + w3 + w4) != 65536) { throw new ArgumentOutOfRangeException("w1 + w2 + w3 + w4 must equal 65536!"); } #endif const uint ww = 32768; uint af = (c1.A * w1) + (c2.A * w2) + (c3.A * w3) + (c4.A * w4); uint a = (af + ww) >> 16; uint b; uint g; uint r; if (a == 0) { b = 0; g = 0; r = 0; } else { b = (uint)((((long)c1.A * c1.B * w1) + ((long)c2.A * c2.B * w2) + ((long)c3.A * c3.B * w3) + ((long)c4.A * c4.B * w4)) / af); g = (uint)((((long)c1.A * c1.G * w1) + ((long)c2.A * c2.G * w2) + ((long)c3.A * c3.G * w3) + ((long)c4.A * c4.G * w4)) / af); r = (uint)((((long)c1.A * c1.R * w1) + ((long)c2.A * c2.R * w2) + ((long)c3.A * c3.R * w3) + ((long)c4.A * c4.R * w4)) / af); } return(ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, (byte)a)); }
public void SetFromLeveledHistogram(HistogramRgb inputHistogram, UnaryPixelOps.Level upo) { if (inputHistogram == null || upo == null) { return; } Clear(); float[] before = new float[3]; float[] slopes = new float[3]; for (int c = 0; c < 3; c++) { long[] channelHistogramOutput = histogram[c]; long[] channelHistogramInput = inputHistogram.histogram[c]; for (int v = 0; v <= 255; v++) { ColorBgra after = ColorBgra.FromBgr((byte)v, (byte)v, (byte)v); upo.UnApply(after, before, slopes); if (after[c] > upo.ColorOutHigh[c] || after[c] < upo.ColorOutLow[c] || (int)Math.Floor(before[c]) < 0 || (int)Math.Ceiling(before[c]) > 255 || float.IsNaN(before[c])) { channelHistogramOutput[v] = 0; } else if (before[c] <= upo.ColorInLow[c]) { channelHistogramOutput[v] = 0; for (int i = 0; i <= upo.ColorInLow[c]; i++) { channelHistogramOutput[v] += channelHistogramInput[i]; } } else if (before[c] >= upo.ColorInHigh[c]) { channelHistogramOutput[v] = 0; for (int i = upo.ColorInHigh[c]; i < 256; i++) { channelHistogramOutput[v] += channelHistogramInput[i]; } } else { channelHistogramOutput[v] = (int)(slopes[c] * Utility.Lerp( channelHistogramInput[(int)Math.Floor(before[c])], channelHistogramInput[(int)Math.Ceiling(before[c])], before[c] - Math.Floor(before[c]))); } } } OnHistogramUpdated(); }
public override ColorBgra Apply(ColorBgra lhs, ColorBgra rhs) { rhs.A = lhs.A; return(rhs); }
protected override void OnSetRenderInfo(PropertyBasedEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs) { Amount1 = newToken.GetProperty<Int32Property>(PropertyNames.Amount1).Value; Amount2 = newToken.GetProperty<Int32Property>(PropertyNames.Amount2).Value; Amount3 = (byte)((int)newToken.GetProperty<StaticListChoiceProperty>(PropertyNames.Amount3).Value); Amount4 = ColorBgra.FromOpaqueInt32(newToken.GetProperty<Int32Property>(PropertyNames.Amount4).Value); Amount5 = ColorBgra.FromOpaqueInt32(newToken.GetProperty<Int32Property>(PropertyNames.Amount5).Value); Amount6 = ColorBgra.FromOpaqueInt32(newToken.GetProperty<Int32Property>(PropertyNames.Amount6).Value); Amount7 = ColorBgra.FromOpaqueInt32(newToken.GetProperty<Int32Property>(PropertyNames.Amount7).Value); Rectangle selection = EnvironmentParameters.GetSelection(srcArgs.Bounds).GetBoundsInt(); Bitmap tattersallBitmap = new Bitmap(selection.Width, selection.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics tattersallGraphics = Graphics.FromImage(tattersallBitmap); // Fill with white Rectangle backgroundRect = new Rectangle(0, 0, selection.Width, selection.Height); using (SolidBrush backColor = new SolidBrush(Amount7)) tattersallGraphics.FillRectangle(backColor, backgroundRect); // Set Brush Styles Brush brush1, brush2, brush3; switch (Amount3) { case 0: // Solid 33% Opacity brush1 = new SolidBrush(Color.FromArgb(85, Amount4)); brush2 = new SolidBrush(Color.FromArgb(85, Amount5)); brush3 = new SolidBrush(Color.FromArgb(85, Amount6)); break; case 1: // Solid 66% Opacity brush1 = new SolidBrush(Color.FromArgb(170, Amount4)); brush2 = new SolidBrush(Color.FromArgb(170, Amount5)); brush3 = new SolidBrush(Color.FromArgb(170, Amount6)); break; case 2: // Diagonal Lines Up brush1 = new HatchBrush(HatchStyle.DarkUpwardDiagonal, Amount4, Amount7); brush2 = new HatchBrush(HatchStyle.DarkUpwardDiagonal, Amount5, Amount7); brush3 = new HatchBrush(HatchStyle.DarkUpwardDiagonal, Amount6, Amount7); break; case 3: // Diagonal Lines Down brush1 = new HatchBrush(HatchStyle.DarkDownwardDiagonal, Amount4, Amount7); brush2 = new HatchBrush(HatchStyle.DarkDownwardDiagonal, Amount5, Amount7); brush3 = new HatchBrush(HatchStyle.DarkDownwardDiagonal, Amount6, Amount7); break; case 4: // 50/50 Dots brush1 = new HatchBrush(HatchStyle.Percent50, Amount4, Amount7); brush2 = new HatchBrush(HatchStyle.Percent50, Amount5, Amount7); brush3 = new HatchBrush(HatchStyle.Percent50, Amount6, Amount7); break; default: brush1 = new SolidBrush(Color.FromArgb(85, Amount4)); brush2 = new SolidBrush(Color.FromArgb(85, Amount5)); brush3 = new SolidBrush(Color.FromArgb(85, Amount6)); break; } // Set pen styles. Pen pen1 = new Pen(brush1, Amount1); brush1.Dispose(); Pen pen2 = new Pen(brush2, Amount1); brush2.Dispose(); Pen pen3 = new Pen(brush3, Amount1); brush3.Dispose(); // Calculate the number of lines will fit in the selection int xLoops = (int)Math.Ceiling((double)selection.Height / ((Amount1 + Amount2) * 3)); int yLoops = (int)Math.Ceiling((double)selection.Width / ((Amount1 + Amount2) * 3)); // Draw Horizontal Lines for (int i = 0; i < xLoops; i++) { // Create points that define line. Point point1 = new Point(0, Amount1 / 2 + (Amount1 + Amount2) * i * 3); Point point2 = new Point(selection.Width, Amount1 / 2 + (Amount1 + Amount2) * i * 3); // Draw line to screen. tattersallGraphics.DrawLine(pen1, point1, point2); // Create points that define line. Point point3 = new Point(0, Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2)); Point point4 = new Point(selection.Width, Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2)); // Draw line to screen. tattersallGraphics.DrawLine(pen2, point3, point4); // Create points that define line. Point point5 = new Point(0, Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2) * 2); Point point6 = new Point(selection.Width, Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2) * 2); // Draw line to screen. tattersallGraphics.DrawLine(pen3, point5, point6); } // Draw Vertical Lines for (int i = 0; i < yLoops; i++) { // Create points that define line. Point point1 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3, 0); Point point2 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3, selection.Height); // Draw line to screen. tattersallGraphics.DrawLine(pen1, point1, point2); // Create points that define line. Point point3 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2), 0); Point point4 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2), selection.Height); // Draw line to screen. tattersallGraphics.DrawLine(pen2, point3, point4); // Create points that define line. Point point5 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2) * 2, 0); Point point6 = new Point(Amount1 / 2 + (Amount1 + Amount2) * i * 3 + (Amount1 + Amount2) * 2, selection.Height); // Draw line to screen. tattersallGraphics.DrawLine(pen3, point5, point6); } pen1.Dispose(); pen2.Dispose(); pen3.Dispose(); tattersallSurface = Surface.CopyFromBitmap(tattersallBitmap); tattersallBitmap.Dispose(); base.OnSetRenderInfo(newToken, dstArgs, srcArgs); }
public override ColorBgra Apply(ColorBgra lhs, ColorBgra rhs) { return(lhs); }
public static ColorBgra ParseHexString(string hexString) { uint value = Convert.ToUInt32(hexString, 16); return(ColorBgra.FromUInt32(value)); }
public abstract ColorBgra Apply(ColorBgra lhs, ColorBgra rhs);
private void RenderChannel(Graphics g, ColorBgra color, int channel, long max, float mean) { Rectangle innerRect = ClientRectangle; int l = innerRect.Left; int t = innerRect.Top; int b = innerRect.Bottom; int r = innerRect.Right; int channels = histogram.Channels; int entries = histogram.Entries; long[] hist = Histogram.HistogramValues[channel]; ++max; if (flipHorizontal) { Utility.Swap(ref l, ref r); } if (!flipVertical) { Utility.Swap(ref t, ref b); } PointF[] points = new PointF[entries + 2]; points[entries] = new PointF(Utility.Lerp(l, r, -1), Utility.Lerp(t, b, 20)); points[entries + 1] = new PointF(Utility.Lerp(l, r, -1), Utility.Lerp(b, t, 20)); for (int i = 0; i < entries; i += entries - 1) { points[i] = new PointF( Utility.Lerp(l, r, (float)hist[i] / (float)max), Utility.Lerp(t, b, (float)i / (float)entries)); } long sum3 = hist[0] + hist[1]; for (int i = 1; i < entries - 1; ++i) { sum3 += hist[i + 1]; points[i] = new PointF( Utility.Lerp(l, r, (float)(sum3) / (float)(max * 3.1f)), Utility.Lerp(t, b, (float)i / (float)entries)); sum3 -= hist[i - 1]; } byte intensity = selected[channel] ? (byte)96 : (byte)32; ColorBgra colorPen = ColorBgra.Blend(ColorBgra.Black, color, intensity); ColorBgra colorBrush = color; colorBrush.A = intensity; Pen pen = new Pen(colorPen.ToColor(), 1.3f); SolidBrush brush = new SolidBrush(colorBrush.ToColor()); g.FillPolygon(brush, points, FillMode.Alternate); g.DrawPolygon(pen, points); }
public override ColorBgra GetPercentileColor(float fraction) { int[] perc = GetPercentile(fraction); return(ColorBgra.FromBgr((byte)(perc[0]), (byte)(perc[1]), (byte)(perc[2]))); }
public override ColorBgra GetMeanColor() { float[] mean = GetMean(); return(ColorBgra.FromBgr((byte)(mean[0] + 0.5f), (byte)(mean[1] + 0.5f), (byte)(mean[2] + 0.5f))); }
public override ColorBgra Apply(ColorBgra color) { return(ColorBgra.FromBgra((byte)(255 - color.B), (byte)(255 - color.G), (byte)(255 - color.R), (byte)(255 - color.A))); }
public override ColorBgra Apply(ColorBgra color) { return(ColorBgra.FromUInt32(color.Bgra | 0xff000000)); }
public override ColorBgra Apply(ColorBgra color) { return(ColorBgra.FromUInt32((color.Bgra & 0x00ffffff) + addValue)); }
public override ColorBgra Apply(ColorBgra color) { color[channel] = setValue; return(color); }
public BlendConstant(ColorBgra blendColor) { this.blendColor = blendColor; }
public unsafe override void Apply(ColorBgra* dst, ColorBgra* src, int length) { while (length > 0) { dst->Bgra = src->Bgra | 0xff000000; ++dst; ++src; --length; } }
public override ColorBgra Apply(ColorBgra color) { color[channel] = setValue; return color; }
protected override void OnPaint(PaintEventArgs e) { e.Graphics.CompositingMode = CompositingMode.SourceOver; int scaledSwatchSize = UI.ScaleWidth(this.unscaledSwatchSize); int swatchColumns = this.ClientSize.Width / scaledSwatchSize; Point mousePt = Control.MousePosition; mousePt = PointToClient(mousePt); int activeIndex = MouseXYToColorIndex(mousePt.X, mousePt.Y); for (int i = 0; i < this.colors.Count; ++i) { ColorBgra c = this.colors[i]; int swatchX = i % swatchColumns; int swatchY = i / swatchColumns; Rectangle swatchRect = new Rectangle( swatchX * scaledSwatchSize, swatchY * scaledSwatchSize, scaledSwatchSize, scaledSwatchSize); PushButtonState state; if (this.mouseDown) { if (i == this.mouseDownIndex) { state = PushButtonState.Pressed; } else { state = PushButtonState.Normal; } } else if (i == activeIndex) { state = PushButtonState.Hot; } else { state = PushButtonState.Normal; } bool drawOutline; switch (state) { case PushButtonState.Hot: drawOutline = true; break; case PushButtonState.Pressed: drawOutline = false; break; case PushButtonState.Default: case PushButtonState.Disabled: case PushButtonState.Normal: drawOutline = false; break; default: throw new InvalidEnumArgumentException(); } Utility.DrawColorRectangle(e.Graphics, swatchRect, c.ToColor(), drawOutline); } if (this.blinkHighlight) { int period = (Math.Abs(Environment.TickCount) / blinkInterval) % 2; Color color; switch (period) { case 0: color = SystemColors.Window; break; case 1: color = SystemColors.Highlight; break; default: throw new InvalidOperationException(); } using (Pen pen = new Pen(color)) { e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1)); } } base.OnPaint(e); }
/// <summary> /// Override this to process the pixel in the second pass of the algorithm /// </summary> /// <param name="pixel">The pixel to quantize</param> /// <returns>The quantized value</returns> protected override byte QuantizePixel(ColorBgra *pixel) { byte paletteIndex = (byte)_maxColors; // The color at [_maxColors] is set to transparent // Get the palette index if this non-transparent if (pixel->A > 0) { paletteIndex = (byte)_octree.GetPaletteIndex(pixel); } return paletteIndex; }
public unsafe void Render(Surface surface, Rectangle[] rois, int startIndex, int length) { byte startAlpha; byte endAlpha; if (this.alphaOnly) { ComputeAlphaOnlyValuesFromColors(this.startColor, this.endColor, out startAlpha, out endAlpha); } else { startAlpha = this.startColor.A; endAlpha = this.endColor.A; } for (int ri = startIndex; ri < startIndex + length; ++ri) { Rectangle rect = rois[ri]; if (this.startPoint == this.endPoint) { // Start and End point are the same ... fill with solid color. for (int y = rect.Top; y < rect.Bottom; ++y) { ColorBgra *pixelPtr = surface.GetPointAddress(rect.Left, y); for (int x = rect.Left; x < rect.Right; ++x) { ColorBgra result; if (this.alphaOnly && this.alphaBlending) { byte resultAlpha = (byte)Utility.FastDivideShortByByte((ushort)(pixelPtr->A * endAlpha), 255); result = *pixelPtr; result.A = resultAlpha; } else if (this.alphaOnly && !this.alphaBlending) { result = *pixelPtr; result.A = endAlpha; } else if (!this.alphaOnly && this.alphaBlending) { result = this.normalBlendOp.Apply(*pixelPtr, this.endColor); } else //if (!this.alphaOnly && !this.alphaBlending) { result = this.endColor; } *pixelPtr = result; ++pixelPtr; } } } else { for (int y = rect.Top; y < rect.Bottom; ++y) { ColorBgra *pixelPtr = surface.GetPointAddress(rect.Left, y); if (this.alphaOnly && this.alphaBlending) { for (int x = rect.Left; x < rect.Right; ++x) { float lerpUnbounded = ComputeUnboundedLerp(x, y); float lerpBounded = BoundLerp(lerpUnbounded); byte lerpByte = (byte)(lerpBounded * 255.0f); byte lerpAlpha = this.lerpAlphas[lerpByte]; byte resultAlpha = Utility.FastScaleByteByByte(pixelPtr->A, lerpAlpha); pixelPtr->A = resultAlpha; ++pixelPtr; } } else if (this.alphaOnly && !this.alphaBlending) { for (int x = rect.Left; x < rect.Right; ++x) { float lerpUnbounded = ComputeUnboundedLerp(x, y); float lerpBounded = BoundLerp(lerpUnbounded); byte lerpByte = (byte)(lerpBounded * 255.0f); byte lerpAlpha = this.lerpAlphas[lerpByte]; pixelPtr->A = lerpAlpha; ++pixelPtr; } } else if (!this.alphaOnly && (this.alphaBlending && (startAlpha != 255 || endAlpha != 255))) { // If we're doing all color channels, and we're doing alpha blending, and if alpha blending is necessary for (int x = rect.Left; x < rect.Right; ++x) { float lerpUnbounded = ComputeUnboundedLerp(x, y); float lerpBounded = BoundLerp(lerpUnbounded); byte lerpByte = (byte)(lerpBounded * 255.0f); ColorBgra lerpColor = this.lerpColors[lerpByte]; ColorBgra result = this.normalBlendOp.Apply(*pixelPtr, lerpColor); * pixelPtr = result; ++pixelPtr; } } else //if (!this.alphaOnly && !this.alphaBlending) // or sC.A == 255 && eC.A == 255 { for (int x = rect.Left; x < rect.Right; ++x) { float lerpUnbounded = ComputeUnboundedLerp(x, y); float lerpBounded = BoundLerp(lerpUnbounded); byte lerpByte = (byte)(lerpBounded * 255.0f); ColorBgra lerpColor = this.lerpColors[lerpByte]; * pixelPtr = lerpColor; ++pixelPtr; } } } } } AfterRender(); }
/// <summary> /// Get the palette index for the passed color /// </summary> /// <param name="pixel"></param> /// <returns></returns> public int GetPaletteIndex(ColorBgra *pixel) { int ret = -1; ret = _root.GetPaletteIndex(pixel, 0); if (ret < 0) { if (this.paletteTable == null) { this.paletteTable = new PaletteTable(this._palette); } ret = this.paletteTable.FindClosestPaletteIndex(pixel->ToColor()); } return ret; }
private static void ComputeAlphaOnlyValuesFromColors(ColorBgra startColor, ColorBgra endColor, out byte startAlpha, out byte endAlpha) { startAlpha = startColor.A; endAlpha = (byte)(255 - endColor.A); }
/// <summary> /// Return the palette index for the passed color /// </summary> public int GetPaletteIndex(ColorBgra *pixel, int level) { int paletteIndex = _paletteIndex; if (!_leaf) { int shift = 7 - level; int index = ((pixel->R & mask[level]) >> (shift - 2)) | ((pixel->G & mask[level]) >> (shift - 1)) | ((pixel->B & mask[level]) >> (shift)); if (null != _children[index]) { paletteIndex = _children[index].GetPaletteIndex(pixel, level + 1); } else { paletteIndex = -1; } } return paletteIndex; }
private static string FormatColor(ColorBgra color) { return(color.ToHexString()); }
/// <summary> /// Process the pixel in the first pass of the algorithm /// </summary> /// <param name="pixel">The pixel to quantize</param> /// <remarks> /// This function need only be overridden if your quantize algorithm needs two passes, /// such as an Octree quantizer. /// </remarks> protected override void InitialQuantizePixel(ColorBgra *pixel) { this.octree.AddColor(pixel); }
public override ColorBgra Apply(ColorBgra color) { byte average = (byte)(((int)color.R + (int)color.G + (int)color.B) / 3); return(ColorBgra.FromBgra(average, average, average, color.A)); }
public override ColorBgra Apply(ColorBgra lhs, ColorBgra rhs) { return(swapMyArgs.Apply(rhs, lhs)); }
public override ColorBgra Apply(ColorBgra color) { return ColorBgra.FromUInt32(color.Bgra | 0xff000000); }
public override ColorBgra Apply(ColorBgra color) { byte i = color.GetIntensityByte(); return(ColorBgra.FromBgra(i, i, i, color.A)); }
public unsafe override void Apply(ColorBgra* ptr, int length) { while (length > 0) { ptr->Bgra |= 0xff000000; ++ptr; --length; } }
public override ColorBgra Apply(ColorBgra color) { return(ColorBgra.FromBgra(CurveB[color.B], CurveG[color.G], CurveR[color.R], color.A)); }
public unsafe override void Apply(ColorBgra* dst, ColorBgra* src, int length) { while (length > 0) { *dst = *src; (*dst)[channel] = setValue; ++dst; ++src; --length; } }
public override ColorBgra Apply(ColorBgra color) { return(setColor); }
/// <summary> /// Add a given color value to the octree /// </summary> /// <param name="pixel"></param> public void AddColor(ColorBgra *pixel) { // Check if this request is for the same color as the last if (_previousColor == pixel->Bgra) { // If so, check if I have a previous node setup. This will only ocurr if the first color in the image // happens to be black, with an alpha component of zero. if (null == _previousNode) { _previousColor = pixel->Bgra; _root.AddColor(pixel, _maxColorBits, 0, this); } else { // Just update the previous node _previousNode.Increment(pixel); } } else { _previousColor = pixel->Bgra; _root.AddColor(pixel, _maxColorBits, 0, this); } }
public Level(ColorBgra in_lo, ColorBgra in_hi, float[] gamma, ColorBgra out_lo, ColorBgra out_hi) { colorInLow = in_lo; colorInHigh = in_hi; colorOutLow = out_lo; colorOutHigh = out_hi; if (gamma.Length != 3) { throw new ArgumentException("gamma", "gamma must be a float[3]"); } this.gamma = gamma; UpdateLookupTable(); }
/// <summary> /// Add a color into the tree /// </summary> /// <param name="pixel">The color</param> /// <param name="colorBits">The number of significant color bits</param> /// <param name="level">The level in the tree</param> /// <param name="octree">The tree to which this node belongs</param> public void AddColor(ColorBgra *pixel, int colorBits, int level, Octree octree) { // Update the color information if this is a leaf if (_leaf) { Increment(pixel); // Setup the previous node octree.TrackPrevious(this); } else { // Go to the next level down in the tree int shift = 7 - level; int index = ((pixel->R & mask[level]) >> (shift - 2)) | ((pixel->G & mask[level]) >> (shift - 1)) | ((pixel->B & mask[level]) >> (shift)); OctreeNode child = _children[index]; if (null == child) { // Create a new child node & store in the array child = new OctreeNode(level + 1, colorBits, octree); _children[index] = child; } // Add the color to the child node child.AddColor(pixel, colorBits, level + 1, octree); } }
public Constant(ColorBgra setColor) { this.setColor = setColor; }
/// <summary> /// Increment the pixel count and add to the color information /// </summary> public void Increment(ColorBgra *pixel) { ++_pixelCount; _red += pixel->R; _green += pixel->G; _blue += pixel->B; }
private void DrawGradient(Graphics g) { g.PixelOffsetMode = PixelOffsetMode.Half; Rectangle gradientRect; float gradientAngle; switch (this.orientation) { case Orientation.Horizontal: gradientAngle = 180.0f; break; case Orientation.Vertical: gradientAngle = 90.0f; break; default: throw new InvalidEnumArgumentException(); } // draw gradient gradientRect = ClientRectangle; switch (this.orientation) { case Orientation.Horizontal: gradientRect.Inflate(-triangleHalfLength, -triangleSize + 3); break; case Orientation.Vertical: gradientRect.Inflate(-triangleSize + 3, -triangleHalfLength); break; default: throw new InvalidEnumArgumentException(); } if (this.customGradient != null && gradientRect.Width > 1 && gradientRect.Height > 1) { Surface gradientSurface = new Surface(gradientRect.Width, gradientRect.Height); using (RenderArgs ra = new RenderArgs(gradientSurface)) { Utility.DrawColorRectangle(ra.Graphics, ra.Bounds, Color.Transparent, false); if (Orientation == Orientation.Horizontal) { for (int x = 0; x < gradientSurface.Width; ++x) { // TODO: refactor, double buffer, save this computation in a bitmap somewhere double index = (double)(x * (this.customGradient.Length - 1)) / (double)(gradientSurface.Width - 1); int indexL = (int)Math.Floor(index); double t = 1.0 - (index - indexL); int indexR = (int)Math.Min(this.customGradient.Length - 1, Math.Ceiling(index)); Color colorL = this.customGradient[indexL]; Color colorR = this.customGradient[indexR]; double a1 = colorL.A / 255.0; double r1 = colorL.R / 255.0; double g1 = colorL.G / 255.0; double b1 = colorL.B / 255.0; double a2 = colorR.A / 255.0; double r2 = colorR.R / 255.0; double g2 = colorR.G / 255.0; double b2 = colorR.B / 255.0; double at = (t * a1) + ((1.0 - t) * a2); double rt; double gt; double bt; if (at == 0) { rt = 0; gt = 0; bt = 0; } else { rt = ((t * a1 * r1) + ((1.0 - t) * a2 * r2)) / at; gt = ((t * a1 * g1) + ((1.0 - t) * a2 * g2)) / at; bt = ((t * a1 * b1) + ((1.0 - t) * a2 * b2)) / at; } int ap = Utility.Clamp((int)Math.Round(at * 255.0), 0, 255); int rp = Utility.Clamp((int)Math.Round(rt * 255.0), 0, 255); int gp = Utility.Clamp((int)Math.Round(gt * 255.0), 0, 255); int bp = Utility.Clamp((int)Math.Round(bt * 255.0), 0, 255); for (int y = 0; y < gradientSurface.Height; ++y) { ColorBgra src = gradientSurface[x, y]; // we are assuming that src.A = 255 int rd = ((rp * ap) + (src.R * (255 - ap))) / 255; int gd = ((gp * ap) + (src.G * (255 - ap))) / 255; int bd = ((bp * ap) + (src.B * (255 - ap))) / 255; // TODO: proper alpha blending! gradientSurface[x, y] = ColorBgra.FromBgra((byte)bd, (byte)gd, (byte)rd, 255); } } g.DrawImage(ra.Bitmap, gradientRect, ra.Bounds, GraphicsUnit.Pixel); } else if (Orientation == Orientation.Vertical) { // TODO } else { throw new InvalidEnumArgumentException(); } } gradientSurface.Dispose(); } else { using (LinearGradientBrush lgb = new LinearGradientBrush(this.ClientRectangle, maxColor, minColor, gradientAngle, false)) { g.FillRectangle(lgb, gradientRect); } } // fill background using (Region nonGradientRegion = new Region()) { nonGradientRegion.MakeInfinite(); nonGradientRegion.Exclude(gradientRect); using (SolidBrush sb = new SolidBrush(this.BackColor)) { g.FillRegion(sb, nonGradientRegion); } } // draw value triangles for (int i = 0; i < this.vals.Length; i++) { int pos = ValueToPosition(vals[i]); Brush brush; Pen pen; if (i == highlight) { brush = Brushes.Blue; pen = (Pen)Pens.White.Clone(); } else { brush = Brushes.Black; pen = (Pen)Pens.Gray.Clone(); } g.SmoothingMode = SmoothingMode.AntiAlias; Point a1; Point b1; Point c1; Point a2; Point b2; Point c2; switch (this.orientation) { case Orientation.Horizontal: a1 = new Point(pos - triangleHalfLength, 0); b1 = new Point(pos, triangleSize - 1); c1 = new Point(pos + triangleHalfLength, 0); a2 = new Point(a1.X, Height - 1 - a1.Y); b2 = new Point(b1.X, Height - 1 - b1.Y); c2 = new Point(c1.X, Height - 1 - c1.Y); break; case Orientation.Vertical: a1 = new Point(0, pos - triangleHalfLength); b1 = new Point(triangleSize - 1, pos); c1 = new Point(0, pos + triangleHalfLength); a2 = new Point(Width - 1 - a1.X, a1.Y); b2 = new Point(Width - 1 - b1.X, b1.Y); c2 = new Point(Width - 1 - c1.X, c1.Y); break; default: throw new InvalidEnumArgumentException(); } if (this.drawNearNub) { g.FillPolygon(brush, new Point[] { a1, b1, c1, a1 }); } if (this.drawFarNub) { g.FillPolygon(brush, new Point[] { a2, b2, c2, a2 }); } if (pen != null) { if (this.drawNearNub) { g.DrawPolygon(pen, new Point[] { a1, b1, c1, a1 }); } if (this.drawFarNub) { g.DrawPolygon(pen, new Point[] { a2, b2, c2, a2 }); } pen.Dispose(); } } }
protected unsafe override sealed void OnSaveT( Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback) { // flatten the document -- render w/ transparent background scratchSurface.Clear(ColorBgra.Transparent); using (RenderArgs ra = new RenderArgs(scratchSurface)) { input.Render(ra, false); } // load properties from token int thresholdFromToken = GetThresholdFromToken(token); int ditherLevel = GetDitherLevelFromToken(token); Set <SavableBitDepths> allowedBitDepths = CreateAllowedBitDepthListFromToken(token); if (allowedBitDepths.Count == 0) { throw new ArgumentException("there must be at least 1 element returned from CreateAllowedBitDepthListFromToken()"); } // allowedBitDepths.Count >= 1 // set to 1 unless allowedBitDepths contains only Rgb8 and Rgba8 int threshold; if (allowedBitDepths.IsSubsetOf(Set.Create(SavableBitDepths.Rgb8, SavableBitDepths.Rgba8))) { threshold = thresholdFromToken; } else { threshold = 1; } // Analyze image, try to detect what bit-depth or whatever to use, based on allowedBitDepths bool allOpaque; bool all0or255Alpha; int uniqueColorCount; Analyze(scratchSurface, out allOpaque, out all0or255Alpha, out uniqueColorCount); Set <SavableBitDepths> losslessBitDepths = new Set <SavableBitDepths>(); losslessBitDepths.Add(SavableBitDepths.Rgba32); if (allOpaque) { losslessBitDepths.Add(SavableBitDepths.Rgb24); if (uniqueColorCount <= 256) { losslessBitDepths.Add(SavableBitDepths.Rgb8); } } else if (all0or255Alpha && uniqueColorCount < 256) { losslessBitDepths.Add(SavableBitDepths.Rgba8); } SavableBitDepths bitDepth = ChooseBitDepth(allowedBitDepths, losslessBitDepths, allOpaque, all0or255Alpha, uniqueColorCount); if (bitDepth == SavableBitDepths.Rgba8 && threshold == 0 && allowedBitDepths.Contains(SavableBitDepths.Rgba8) && allowedBitDepths.Contains(SavableBitDepths.Rgb8)) { // threshold of 0 should effectively force full 256 color palette, instead of 255+1 transparent bitDepth = SavableBitDepths.Rgb8; } // if bit depth is 24 or 8, then we have to do away with the alpha channel // for 8-bit, we must have pixels that have either 0 or 255 alpha if (bitDepth == SavableBitDepths.Rgb8 || bitDepth == SavableBitDepths.Rgba8 || bitDepth == SavableBitDepths.Rgb24) { UserBlendOps.NormalBlendOp blendOp = new UserBlendOps.NormalBlendOp(); for (int y = 0; y < scratchSurface.Height; ++y) { for (int x = 0; x < scratchSurface.Width; ++x) { ColorBgra p = scratchSurface[x, y]; if (p.A < threshold && bitDepth == SavableBitDepths.Rgba8) { p = ColorBgra.FromBgra(0, 0, 0, 0); } else { p = blendOp.Apply(ColorBgra.White, p); } scratchSurface[x, y] = p; } } } Tracing.Ping("Chose " + bitDepth + ", ditherLevel=" + ditherLevel + ", threshold=" + threshold); // finally, do the save. FinalSave(input, output, scratchSurface, ditherLevel, bitDepth, token, progressCallback); }
public abstract ColorBgra Apply(ColorBgra color);
public virtual void Apply(ColorBgra *dst, ColorBgra *src, int length) { throw new System.NotImplementedException("Derived class must implement Apply(ColorBgra*,ColorBgra*,int)"); }
/// <summary> /// Returns a new ColorBgra with the same color values but with a new alpha component value. /// </summary> public ColorBgra NewAlpha(byte newA) { return(ColorBgra.FromBgra(B, G, R, newA)); }