public BitmapW opacity(int value, BitmapW bottom, BitmapW top) // in Form1.cs for example: "layer3 = layers.opacity(50,layer2, layer1);" *"50" = 50% the value from 0-100 { if (value >= 0 && value <= 100) { float percentge = (float)value / 100; int i = 0, j = 0, h = Math.Min(bottom.Height(), top.Height()), w = Math.Min(bottom.Width(), top.Width()); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color b = bottom.GetPixel(i, j); Color t = top.GetPixel(i, j); float cr = 0, cg = 0, cb = 0, ca = 0; ca = b.A * (1 - percentge) + t.A * percentge; cr = b.R * (1 - percentge) + t.R * percentge; cg = b.G * (1 - percentge) + t.G * percentge; cb = b.B * (1 - percentge) + t.B * percentge; bottom.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } } return(bottom); }
BitmapW curves(BitmapW image, Point s, Point c1, Point c2, Point e) { Util.Bezier bezier = new Util.Bezier(s, c1, c2, e); int[] points = bezier.genColorTable(); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = points[(int)cr]; cg = points[(int)cg]; cb = points[(int)cb]; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Constrast [-100, 100] /// </summary> /// <param name="f"></param> /// <param name="c"></param> /// <returns></returns> public BitmapW contrast(BitmapW image, float p) { p = Util.normalize(p, 0, 2, -100, 100); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = 255 * contrastc(cr / 255, p); cg = 255 * contrastc(cg / 255, p); cb = 255 * contrastc(cb / 255, p); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Saturate [-100, 100] /// </summary> /// <param name="image"></param> /// <param name="p"></param> /// <returns></returns> public BitmapW saturate(BitmapW image, float p) { p = Util.normalize(p, 0, 2, -100, 100); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; float avg = (cr + cg + cb) / 3; cr = avg + p * (cr - avg); cg = avg + p * (cg - avg); cb = avg + p * (cb - avg); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Posterize [1, 255] /// </summary> /// <param name="image"></param> /// <param name="p"></param> /// <returns></returns> public BitmapW posterize(BitmapW image, float p) { p = Util.clamp(p, 1, 255); int step = (int)Math.Floor(255 / p); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = (float)Math.Floor(cr / (float)(step)) * step; cg = (float)Math.Floor(cg / (float)(step)) * step; cb = (float)Math.Floor(cb / (float)(step)) * step; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Adjust [-255,255 for each channel] /// </summary> /// <param name="image"></param> /// <param name="pr"></param> /// <param name="pg"></param> /// <param name="pb"></param> /// <returns></returns> public BitmapW adjust(BitmapW image, float pr, float pg, float pb) { float cr = 0, cg = 0, cb = 0; pr /= 100; pg /= 100; pb /= 100; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; if (pr > 0) cr += (255 - cr) * pr; else cr -= cr * Math.Abs(pr); if (pg > 0) cg += (255 - cg) * pg; else cg -= cg * Math.Abs(pg); if (pb > 0) cb += (255 - cb) * pb; else cb -= cb * Math.Abs(pb); /* cr *= (1.0f + pr); cg *= (1.0f + pg); cb *= (1.0f + pb); */ image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Vignette /// (red,green,blue) of the vignette effect to apply /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW vignette(BitmapW image, int r, int g, int b) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(), centerw = w / 2, centerh = h / 2, maxdist = dist(0, 0, centerw, centerh); maxdist = (int)(maxdist * 0.6f); int radius = maxdist / 2; for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; int distance = dist(i, j, centerw, centerh); distance = (distance > radius)?distance - radius:0; float ratio = ((distance / (float)(maxdist))); cr = (1 - ratio) * cr + (ratio * r); cg = (1 - ratio) * cg + (ratio * g); cb = (1 - ratio) * cb + (ratio * b); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Noise [0 - 100] /// </summary> /// <param name="image"></param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public BitmapW noise(BitmapW image, int p) { int adjust = (int)(p * 2.55f); Random rand = new Random(adjust); int temprand = 0; float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; temprand = rand.Next(adjust * -1, adjust); cr += temprand; cg += temprand; cb += temprand; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Adjust [-255,255 for each channel] /// </summary> /// <param name="image"></param> /// <param name="pr"></param> /// <param name="pg"></param> /// <param name="pb"></param> /// <returns></returns> public BitmapW adjust(BitmapW image, float pr, float pg, float pb) { float cr = 0, cg = 0, cb = 0; pr /= 100; pg /= 100; pb /= 100; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; if (pr > 0) { cr += (255 - cr) * pr; } else { cr -= cr * Math.Abs(pr); } if (pg > 0) { cg += (255 - cg) * pg; } else { cg -= cg * Math.Abs(pg); } if (pb > 0) { cb += (255 - cb) * pb; } else { cb -= cb * Math.Abs(pb); } /* * cr *= (1.0f + pr); * cg *= (1.0f + pg); * cb *= (1.0f + pb); */ image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
BitmapW apply(BitmapW bottom, BitmapW top, string fn) { int i = 0, j = 0, h = Math.Min(bottom.Height(), top.Height()), w = Math.Min(bottom.Width(), top.Width()); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { // Execute blend on each pixel. bottom.SetPixel(i, j, func(fn, bottom.GetPixel(i, j), top.GetPixel(i, j))); } } return bottom; }
public Blur CustomBlur = new Blur(); //the use in Form1.cs for example: "layer1 = effects.CustomBlur.Gaussain(layer1, 10);" BitmapW convolve(BitmapW image, float[,] kernel, int kw, int kh) { BitmapW temp = image.Clone(); // int kh = kernel; //int kw = kh; //kernel[0].Length / 2; int i = 0, j = 0, n = 0, m = 0, cr, cg, cb, ca, h = image.Height(), w = image.Width(); for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { //kernel loop float r = 0, g = 0, b = 0, a = 0; for (n = -kh; n <= kh; n++) { for (m = -kw; m <= kw; m++) { if (i + n >= 0 && i + n < h) { if (j + m >= 0 && j + m < w) { float f = kernel[m + kw, n + kh]; if (f == 0) { continue; } Color colortemp = image.GetPixel(j + m, i + n); cr = colortemp.R; cg = colortemp.G; cb = colortemp.B; ca = colortemp.A; r += cr * f; g += cg * f; b += cb * f; a += ca * f; } } } } //kernel loop end temp.SetPixel(j, i, Color.FromArgb((int)Util.clamp(a, 0, 255), (int)Util.clamp(r, 0, 255), (int)Util.clamp(g, 0, 255), (int)Util.clamp(b, 0, 255))); } } return(temp); }
/// <summary> /// #### Alpha [-100, 100] /// </summary> /// <param name="image"></param> /// <param name="p"></param> /// <returns></returns> public BitmapW alpha(BitmapW image, float p) { p = Util.normalize(p, 0, 255, -100, 100); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; ca = (p); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Brighten [-100, 100] /// </summary> /// <param name="image"></param> /// <param name="p"></param> /// <returns></returns> public BitmapW brighten(BitmapW image, float p) { p = Util.normalize(p, -255, 255, -100, 100); float cr = 0, cg = 0, cb = 0; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; cr += (p); cg += (p); cb += (p); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Sepia /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW sepia(BitmapW image) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; float tcr = cr, tcg = cg, tcb = cb; cr = (tcr * 0.393f) + (tcg * 0.769f) + (tcb * 0.189f); cg = (tcr * 0.349f) + (tcg * 0.686f) + (tcb * 0.168f); cb = (tcr * 0.272f) + (tcg * 0.534f) + (tcb * 0.131f); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
/// <summary> /// #### Fill [No Range] /// </summary> /// <param name="image"></param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public BitmapW fill(BitmapW image, int r, int g, int b) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = r; cg = g; cb = b; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return(image); }
BitmapW curves(BitmapW image, Point s, Point c1, Point c2, Point e) { Util.Bezier bezier = new Util.Bezier(s, c1, c2, e); int[] points = bezier.genColorTable(); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = points[(int)cr]; cg = points[(int)cg]; cb = points[(int)cb]; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
BitmapW convolve(BitmapW image, float[,] kernel, int kw, int kh) { BitmapW temp = image.Clone(); // int kh = kernel; //int kw = kh; //kernel[0].Length / 2; int i = 0, j = 0, n = 0, m = 0, cr, cg, cb, h = image.Height(), w = image.Width(); for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { //kernel loop float r = 0, g = 0, b = 0; for (n = -kh; n <= kh; n++) { for (m = -kw; m <= kw; m++) { if (i + n >= 0 && i + n < h) { if (j + m >= 0 && j + m < w) { float f = kernel[m + kw, n + kh]; if (f == 0) { continue; } Color colortemp = image.GetPixel(j + m, i + n); cr = colortemp.R; cg = colortemp.G; cb = colortemp.B; r += cr * f; g += cg * f; b += cb * f; } } } } //kernel loop end temp.SetPixel(j, i, Color.FromArgb(255, (int)Util.clamp(r, 0, 255), (int)Util.clamp(g, 0, 255), (int)Util.clamp(b, 0, 255))); } } return temp; }
/// <summary> /// #### Vignette /// (red,green,blue) of the vignette effect to apply /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW vignette(BitmapW image, int r, int g, int b) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(), centerw = w / 2, centerh = h / 2, maxdist = dist(0, 0, centerw, centerh); maxdist = (int)(maxdist*0.6f); int radius = maxdist / 2; for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; int distance = dist(i, j, centerw, centerh); distance = (distance>radius)?distance-radius:0; float ratio = ((distance / (float)(maxdist))); cr = (1 - ratio) * cr + (ratio * r); cg = (1 - ratio) * cg + (ratio * g); cb = (1 - ratio) * cb + (ratio * b); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Subtract [No Range] /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW subtract(BitmapW image) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr -= cr; cg -= cg; cb -= cb; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Sepia /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW sepia(BitmapW image) { float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; float tcr = cr, tcg = cg, tcb = cb; cr = (tcr * 0.393f) + (tcg * 0.769f) + (tcb * 0.189f); cg = (tcr * 0.349f) + (tcg * 0.686f) + (tcb * 0.168f); cb = (tcr * 0.272f) + (tcg * 0.534f) + (tcb * 0.131f); image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Posterize [1, 255] /// </summary> /// <param name="image"></param> /// <param name="p"></param> /// <returns></returns> public BitmapW posterize(BitmapW image, float p) { p = Util.clamp(p, 1, 255); int step = (int)Math.Floor(255 / p); float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; cr = (float)Math.Floor(cr / (float)(step)) * step; cg = (float)Math.Floor(cg / (float)(step)) * step; cb = (float)Math.Floor(cb / (float)(step)) * step; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
/// <summary> /// #### Noise [0 - 100] /// </summary> /// <param name="image"></param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public BitmapW noise(BitmapW image, int p) { int adjust = (int)(p * 2.55f); Random rand = new Random(adjust); int temprand = 0; float cr = 0, cg = 0, cb = 0, ca; int i = 0, j = 0, h = image.Height(), w = image.Width(); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color temp = image.GetPixel(i, j); cr = temp.R; cg = temp.G; cb = temp.B; ca = temp.A; temprand = rand.Next(adjust * -1, adjust); cr += temprand; cg += temprand; cb += temprand; image.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } return image; }
private BitmapW FastBoxBlur(BitmapW img, int radius) { int kSize = radius; if (kSize % 2 == 0) { kSize++; } BitmapW Hblur = img.Clone(); float Avg = (float)1 / kSize; for (int j = 0; j < img.Height(); j++) { float[] hSum = new float[] { 0f, 0f, 0f, 0f }; float[] iAvg = new float[] { 0f, 0f, 0f, 0f }; for (int x = 0; x < kSize; x++) { Color tmpColor = img.GetPixel(x, j); hSum[0] += tmpColor.A; hSum[1] += tmpColor.R; hSum[2] += tmpColor.G; hSum[3] += tmpColor.B; } iAvg[0] = hSum[0] * Avg; iAvg[1] = hSum[1] * Avg; iAvg[2] = hSum[2] * Avg; iAvg[3] = hSum[3] * Avg; for (int i = 0; i < img.Width(); i++) { if (i - kSize / 2 >= 0 && i + 1 + kSize / 2 < img.Width()) { Color tmp_pColor = img.GetPixel(i - kSize / 2, j); hSum[0] -= tmp_pColor.A; hSum[1] -= tmp_pColor.R; hSum[2] -= tmp_pColor.G; hSum[3] -= tmp_pColor.B; Color tmp_nColor = img.GetPixel(i + 1 + kSize / 2, j); hSum[0] += tmp_nColor.A; hSum[1] += tmp_nColor.R; hSum[2] += tmp_nColor.G; hSum[3] += tmp_nColor.B; // iAvg[0] = hSum[0] * Avg; iAvg[1] = hSum[1] * Avg; iAvg[2] = hSum[2] * Avg; iAvg[3] = hSum[3] * Avg; } Hblur.SetPixel(i, j, Color.FromArgb((int)iAvg[0], (int)iAvg[1], (int)iAvg[2], (int)iAvg[3])); } } BitmapW total = Hblur.Clone(); for (int i = 0; i < Hblur.Width(); i++) { float[] tSum = new float[] { 0f, 0f, 0f, 0f }; float[] iAvg = new float[] { 0f, 0f, 0f, 0f }; for (int y = 0; y < kSize; y++) { Color tmpColor = Hblur.GetPixel(i, y); tSum[0] += tmpColor.A; tSum[1] += tmpColor.R; tSum[2] += tmpColor.G; tSum[3] += tmpColor.B; } iAvg[0] = tSum[0] * Avg; iAvg[1] = tSum[1] * Avg; iAvg[2] = tSum[2] * Avg; iAvg[3] = tSum[3] * Avg; for (int j = 0; j < Hblur.Height(); j++) { if (j - kSize / 2 >= 0 && j + 1 + kSize / 2 < Hblur.Height()) { Color tmp_pColor = Hblur.GetPixel(i, j - kSize / 2); tSum[0] -= tmp_pColor.A; tSum[1] -= tmp_pColor.R; tSum[2] -= tmp_pColor.G; tSum[3] -= tmp_pColor.B; Color tmp_nColor = Hblur.GetPixel(i, j + 1 + kSize / 2); tSum[0] += tmp_nColor.A; tSum[1] += tmp_nColor.R; tSum[2] += tmp_nColor.G; tSum[3] += tmp_nColor.B; // iAvg[0] = tSum[0] * Avg; iAvg[1] = tSum[1] * Avg; iAvg[2] = tSum[2] * Avg; iAvg[3] = tSum[3] * Avg; } total.SetPixel(i, j, Color.FromArgb((int)iAvg[0], (int)iAvg[1], (int)iAvg[2], (int)iAvg[3])); } } return(total); }
public BitmapW opacity(int value, BitmapW bottom, BitmapW top) // in Form1.cs for example: "layer3 = layers.opacity(50,layer2, layer1);" *"50" = 50% the value from 0-100 { if (value >= 0 && value <= 100) { float percentge =(float)value/100; int i = 0, j = 0, h = Math.Min(bottom.Height(), top.Height()), w = Math.Min(bottom.Width(), top.Width()); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { Color b = bottom.GetPixel(i, j); Color t = top.GetPixel(i, j); float cr = 0, cg = 0, cb = 0, ca = 0; ca = b.A * (1 - percentge) + t.A * percentge; cr = b.R * (1 - percentge) + t.R * percentge; cg = b.G * (1 - percentge) + t.G * percentge; cb = b.B * (1 - percentge) + t.B * percentge; bottom.SetPixel(i, j, Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); } } Color func(string fn, Color b, Color t) { int cr = 0, cg = 0, cb = 0, ca = 0; //check which function to apply and apply it if (fn == "multiply") { cr = (t.R * b.R) / 255; cg = (t.G * b.G) / 255; cb = (t.B * b.B) / 255; ca = (t.A * b.A) / 255; } if (fn == "screen") { cr = 255 - (((255 - t.R) * (255 - b.R)) / 255); cg = 255 - (((255 - t.G) * (255 - b.G)) / 255); cb = 255 - (((255 - t.B) * (255 - b.B)) / 255); ca = 255 - (((255 - t.A) * (255 - b.A)) / 255); } if (fn == "overlay") { cr = coverlay(b.R, t.R); cg = coverlay(b.G, t.G); cb = coverlay(b.B, t.B); ca = coverlay(b.A, t.A); } // Thanks to @olivierlesnicki for suggesting a better algoritm. if (fn == "softLight") { cr = csoftLight(b.R, t.R); cg = csoftLight(b.G, t.G); cb = csoftLight(b.B, t.B); ca = csoftLight(b.A, t.A); } if (fn == "addition") { cr = b.R + t.R; cg = b.G + t.G; cb = b.B + t.B; ca = b.A + t.A; } if (fn == "exclusion") { cr = 128 - 2 * (b.R - 128) * (t.R - 128) / 255; cg = 128 - 2 * (b.G - 128) * (t.G - 128) / 255; cb = 128 - 2 * (b.B - 128) * (t.B - 128) / 255; ca = 128 - 2 * (b.A - 128) * (t.A - 128) / 255; } if (fn == "difference") { cr = Math.Abs(b.R - t.R); cg = Math.Abs(b.G - t.G); cb = Math.Abs(b.B - t.B); ca = Math.Abs(b.A - t.A); } if (fn == "colordodge") { double cr_d, cg_d, cb_d, ca_d; if ((t.R) != 255) cr_d = ((double)b.R /(255 - t.R))*255 ; else cr_d = 0; if ((t.G) != 255) cg_d = ((double)b.G/(255 - t.G))*255; else cg_d = 0; if ((t.B) != 255) cb_d = ((double)b.B/(255 - t.B))*255; else cb_d = 0; if ((t.A) != 255) ca_d = ((double)b.A/(255 - t.A))*255; else ca_d = 0; cr = (int)cr_d; cg = (int)cg_d; cb = (int)cb_d; ca = (int)ca_d; } return Color.FromArgb((int)Util.clamp(ca, 0, 255), (int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255)); } int coverlay(int b, int t) { return (b > 128) ? 255 - 2 * (255 - t) * (255 - b) / 255 : (b * t * 2) / 255; } int csoftLight(float b, float t) { b /= 255; t /= 255; return (int)((t < 0.5) ? 255 * ((1 - 2 * t) * b * b + 2 * t * b) : 255 * ((1 - (2 * t - 1)) * b + (2 * t - 1) * (Math.Pow(b, 0.5)))); }