public static clamp ( float val, float min, float max ) : float | ||
val | float | |
min | float | |
max | float | |
리턴 | float |
/// <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> /// #### Invert /// </summary> /// <param name="image"></param> /// <returns></returns> public BitmapW invert(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 = 255 - cr; cg = 255 - cg; cb = 255 - 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> /// #### 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> /// #### 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> /// #### 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); }
Color func(string fn, Color b, Color t) { int cr = 0, cg = 0, cb = 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; } 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); } if (fn == "overlay") { cr = coverlay(b.R, t.R); cg = coverlay(b.G, t.G); cb = coverlay(b.B, t.B); } // 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); } if (fn == "addition") { cr = b.R + t.R; cg = b.G + t.G; cb = b.B + t.B; } 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; } if (fn == "difference") { cr = Math.Abs(b.R - t.R); cg = Math.Abs(b.G - t.G); cb = Math.Abs(b.B - t.B); } return(Color.FromArgb((int)Util.clamp(cr, 0, 255), (int)Util.clamp(cg, 0, 255), (int)Util.clamp(cb, 0, 255))); }
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); }
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); }
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))); }
/// <summary> /// #### Adjust [-255,255 for each channel] /// </summary> /// <param name="image"></param> /// <param name="pr"></param> /// <param name="pg"></param> /// <param name="pb"></param> /// <param name="pa"></param> /// <returns></returns> public BitmapW adjust(BitmapW image, float pr, float pg, float pb, float pa) { float cr = 0, cg = 0, cb = 0, ca = 0; pr /= 100; pg /= 100; pb /= 100; pa /= 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; ca = temp.A; 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); } if (pa > 0) { ca += (255 - ca) * pa; } else { ca -= ca * Math.Abs(pa); } /* * cr *= (1.0f + pr); * cg *= (1.0f + pg); * cb *= (1.0f + pb); * ca *= (1.0f + ca); */ 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> /// #### 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); }
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> /// #### 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); }