Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Windows.Forms;
- using PlugAPI;
- using System.Reflection;
- using AutoGui;
- using System.Drawing.Imaging;
- namespace PlugsLib
- {
- public struct HSBColor
- {
- float h;
- float s;
- float b;
- int a;
- public HSBColor(float h, float s, float b)
- {
- this.a = 0xff;
- this.h = Math.Min(Math.Max(h, 0), 255);
- this.s = Math.Min(Math.Max(s, 0), 255);
- this.b = Math.Min(Math.Max(b, 0), 255);
- }
- public HSBColor(int a, float h, float s, float b)
- {
- this.a = a;
- this.h = Math.Min(Math.Max(h, 0), 255);
- this.s = Math.Min(Math.Max(s, 0), 255);
- this.b = Math.Min(Math.Max(b, 0), 255);
- }
- public HSBColor(Color color)
- {
- HSBColor temp = FromColor(color);
- this.a = temp.a;
- this.h = temp.h;
- this.s = temp.s;
- this.b = temp.b;
- }
- public float H
- {
- get { return h; }
- }
- public float S
- {
- get { return s; }
- }
- public float B
- {
- get { return b; }
- }
- public int A
- {
- get { return a; }
- }
- public Color Color
- {
- get
- {
- return FromHSB(this);
- }
- }
- public static Color ShiftHue(Color c, float hueDelta)
- {
- HSBColor hsb = HSBColor.FromColor(c);
- hsb.h += hueDelta;
- hsb.h = Math.Min(Math.Max(hsb.h, 0), 255);
- return FromHSB(hsb);
- }
- public static Color ShiftSaturation(Color c, float saturationDelta)
- {
- HSBColor hsb = HSBColor.FromColor(c);
- hsb.s += saturationDelta;
- hsb.s = Math.Min(Math.Max(hsb.s, 0), 255);
- return FromHSB(hsb);
- }
- public static Color ShiftBrighness(Color c, float brightnessDelta)
- {
- HSBColor hsb = HSBColor.FromColor(c);
- hsb.b += brightnessDelta;
- hsb.b = Math.Min(Math.Max(hsb.b, 0), 255);
- return FromHSB(hsb);
- }
- public static Color FromHSB(HSBColor hsbColor)
- {
- float r = hsbColor.b;
- float g = hsbColor.b;
- float b = hsbColor.b;
- if (hsbColor.s != 0)
- {
- float max = hsbColor.b;
- float dif = hsbColor.b * hsbColor.s / 255f;
- float min = hsbColor.b - dif;
- float h = hsbColor.h * 360f / 255f;
- if (h < 60f)
- {
- r = max;
- g = h * dif / 60f + min;
- b = min;
- }
- else if (h < 120f)
- {
- r = -(h - 120f) * dif / 60f + min;
- g = max;
- b = min;
- }
- else if (h < 180f)
- {
- r = min;
- g = max;
- b = (h - 120f) * dif / 60f + min;
- }
- else if (h < 240f)
- {
- r = min;
- g = -(h - 240f) * dif / 60f + min;
- b = max;
- }
- else if (h < 300f)
- {
- r = (h - 240f) * dif / 60f + min;
- g = min;
- b = max;
- }
- else if (h <= 360f)
- {
- r = max;
- g = min;
- b = -(h - 360f) * dif / 60 + min;
- }
- else
- {
- r = 0;
- g = 0;
- b = 0;
- }
- }
- return Color.FromArgb
- (
- hsbColor.a,
- (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
- (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
- (int)Math.Round(Math.Min(Math.Max(b, 0), 255))
- );
- }
- public static HSBColor FromColor(Color color)
- {
- HSBColor ret = new HSBColor(0f, 0f, 0f);
- ret.a = color.A;
- float r = color.R;
- float g = color.G;
- float b = color.B;
- float max = Math.Max(r, Math.Max(g, b));
- if (max <= 0)
- {
- return ret;
- }
- float min = Math.Min(r, Math.Min(g, b));
- float dif = max - min;
- if (max > min)
- {
- if (g == max)
- {
- ret.h = (b - r) / dif * 60f + 120f;
- }
- else if (b == max)
- {
- ret.h = (r - g) / dif * 60f + 240f;
- }
- else if (b > g)
- {
- ret.h = (g - b) / dif * 60f + 360f;
- }
- else
- {
- ret.h = (g - b) / dif * 60f;
- }
- if (ret.h < 0)
- {
- ret.h = ret.h + 360f;
- }
- }
- else
- {
- ret.h = 0;
- }
- ret.h *= 255f / 360f;
- ret.s = (dif / max) * 255f;
- ret.b = max;
- return ret;
- }
- }
- public static class LibHelper
- {
- public static float Display(float x, float minA, float maxA, float minB, float maxB)
- {
- double lenA = Math.Abs(maxA - minA);
- double lenB = Math.Abs(maxB - minB);
- double y = lenB * (x - minA) / lenA + minB;
- return (float)y;
- }
- public static int Clamp(int x, int min, int max)
- {
- return (x < min ? min : (x > max ? max : x));
- }
- public static BitmapData LockImage(Image img, Rectangle rect)
- {
- if (img == null) return null;
- return (img as Bitmap).LockBits(rect, ImageLockMode.ReadWrite, img.PixelFormat);
- }
- public static void UnlockImage(Image img, BitmapData data)
- {
- if (img != null && data != null) (img as Bitmap).UnlockBits(data);
- }
- public static Image Additive(Image srcA, Image srcB)
- {
- Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
- for (int i = 0; i < res.Width; ++i)
- for (int j = 0; j < res.Height; ++j)
- {
- var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
- var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
- (res as Bitmap).SetPixel(i, j, pixA + pixB);
- }
- return res;
- }
- public static Image Substractive(Image srcA, Image srcB)
- {
- Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
- for (int i = 0; i < res.Width; ++i)
- for (int j = 0; j < res.Height; ++j)
- {
- var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
- var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
- (res as Bitmap).SetPixel(i, j, pixA - pixB);
- }
- return res;
- }
- public static Image Multiply(Image srcA, Image srcB)
- {
- Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
- for (int i = 0; i < res.Width; ++i)
- for (int j = 0; j < res.Height; ++j)
- {
- var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
- var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
- (res as Bitmap).SetPixel(i, j, pixA * pixB);
- }
- return res;
- }
- }
- public class ImageCircles : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- private struct circ
- {
- public PointF p;
- public float r;
- public Color c;
- public circ(PointF p, float r, Color c)
- : this()
- {
- this.p = p;
- this.r = r;
- this.c = c;
- }
- }
- private float rgb(Color c)
- {
- return (c.R + c.B + c.G) / (3f * 255f);
- }
- private float clamp(float value, float min, float max)
- {
- return value >= max ? max : value <= min ? min : value;
- }
- [LabelText("Background color")]
- public Color ClearColor { get; set; }
- public string Name
- {
- get { return "Circles image"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Artistic; }
- }
- public string Description
- {
- get { return "Make all pixels in source image as small circles."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap bmp = null;
- lock(image)
- bmp = new Bitmap(image, new Size(image.Width, image.Height));
- float maxr = 5f, minr = 0.2f;
- Bitmap res = new Bitmap((int)(bmp.Width * maxr), (int)(bmp.Height * maxr));
- Graphics graphics = Graphics.FromImage((Image)res);
- graphics.Clear(ClearColor);
- int max = image.Width * image.Height;
- int it = 0;
- for (int i = 0; i < bmp.Width; i++)
- for (int j = 0; j < bmp.Height; j++)
- {
- Color pix = bmp.GetPixel(i, j);
- float r = clamp(rgb(pix) * maxr, minr, maxr - 2);
- PointF pos = new PointF(i * maxr - r, j * maxr - r);
- graphics.FillEllipse(new SolidBrush(pix),
- new RectangleF(pos, new SizeF(r * 2f, r * 2f)));
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- graphics.Flush();
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class Grayscale : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- private float rgb(Color c)
- {
- return (c.R + c.B + c.G) / (3f * 255f);
- }
- public string Name
- {
- get { return "Grayscale"; }
- }
- public string SubMenuName
- {
- get { throw new NotImplementedException(); }
- }
- public string Description
- {
- get { return "Make image grayscale."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap bmp = (Bitmap)image;
- Bitmap res = new Bitmap(image.Width, image.Height);
- int max = image.Width * image.Height;
- int it = 0;
- for (int i = 0; i < image.Width; i++)
- for (int j = 0; j < image.Height; j++)
- {
- int wbColor = (int)(rgb(bmp.GetPixel(i, j)) * 255);
- res.SetPixel(i, j, Color.FromArgb(255, wbColor, wbColor, wbColor));
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class WhiteOrBlack : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- private float rgb(Color c)
- {
- return (c.R + c.B + c.G) / (3f * 255f);
- }
- public string Name
- {
- get { return "White or Black"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Adjustments; }
- }
- public string Description
- {
- get { return "Replece all colors to white or black."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- int max = image.Width * image.Height;
- int it = 0;
- for (int i = 0; i < img.Width; i++)
- for (int j = 0; j < img.Height; j++)
- {
- float pix = rgb(img.GetPixel(i, j));
- if (pix > .5f) res.SetPixel(i, j, Color.White);
- else res.SetPixel(i, j, Color.Black);
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class WBRGBCMY : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- public string Name
- {
- get { return "WBRGBCMY"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Adjustments; }
- }
- public string Description
- {
- get { return "Effect same to WhiteOrBlack, but here are more colors: white, black, red, green, blue, cyan, magenta, yellow."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- int max = image.Width * image.Height;
- int it = 0;
- for (int i = 0; i < img.Width; i++)
- for (int j = 0; j < img.Height; j++)
- {
- Color pix = img.GetPixel(i, j);
- Color resc = Color.Transparent;
- float hr = (float)pix.R / 255.0f, hb = (float)pix.B / 255.0f, hg = (float)pix.G / 255.0f;
- if (hr >= .5f) resc = Color.FromArgb(255, resc.G, resc.B);
- else resc = Color.FromArgb(0, resc.G, resc.B);
- if (hg >= .5f) resc = Color.FromArgb(resc.R, 255, resc.B);
- else resc = Color.FromArgb(resc.R, 0, resc.B);
- if (hb >= .5f) resc = Color.FromArgb(resc.R, resc.G, 255);
- else resc = Color.FromArgb(resc.R, resc.G, 0);
- res.SetPixel(i, j, resc);
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class Negative : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- public string Name
- {
- get { return "Negative"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Adjustments; }
- }
- public string Description
- {
- get { return "Make image negative."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- int max = image.Width * image.Height;
- int it = 0;
- for (int i = 0; i < img.Width; i++)
- for (int j = 0; j < img.Height; j++)
- {
- Color pix = img.GetPixel(i, j);
- res.SetPixel(i, j, Color.FromArgb(255 - pix.R, 255 - pix.G, 255 - pix.B));
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class Outline : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- [DefaultValue(20), LabelText("Koeficient")]
- public int Koef { get; set; }
- public string Name
- {
- get { return "Outline"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Stylize; }
- }
- public string Description
- {
- get { return "Mark in source image hot spots."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- int factor = Koef;
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- // lines
- int max = image.Width * image.Height * 2 - 2;
- int it = 0;
- for (int i = 1; i < img.Width; i++)
- for (int j = 0; j < img.Height; j++)
- {
- Color currPix = img.GetPixel(i, j);
- Color prewPix = img.GetPixel(i - 1, j);
- int rf = Math.Abs(currPix.R - prewPix.R),
- gf = Math.Abs(currPix.G - prewPix.G),
- bf = Math.Abs(currPix.B - prewPix.B);
- if (bf > factor || gf > factor || rf > factor) res.SetPixel(i, j, Color.White);
- else res.SetPixel(i, j, Color.Black);
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- //colums
- for (int i = 1; i < img.Height; i++)
- for (int j = 0; j < img.Width; j++)
- {
- Color currPix = img.GetPixel(j, i);
- Color prewPix = img.GetPixel(j, i - 1);
- int rf = Math.Abs(currPix.R - prewPix.R),
- gf = Math.Abs(currPix.G - prewPix.G),
- bf = Math.Abs(currPix.B - prewPix.B);
- if (bf > factor || gf > factor || rf > factor) res.SetPixel(j, i, Color.White);
- else res.SetPixel(j, i, Color.Black);
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- //---------------------------------------------
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class GaussianBlur : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- [DefaultValue(3), LabelText("Horizontal length")]
- public int XFactor { get; set; }
- [DefaultValue(3), LabelText("Vertical length")]
- public int YFactor { get; set; }
- public string Name
- {
- get { return "Gaussian Blur"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Blurs; }
- }
- public string Description
- {
- get { return "Gaussian blur."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- int max = image.Width * image.Height * 3 * 3;
- int it = 0;
- int xf = XFactor / 2;
- int yf = YFactor / 2;
- for (int i = 0; i < img.Width; i++)
- for (int j = 0; j < img.Height; j++)
- {
- int rr = 0, rg = 0, rb = 0;
- int count = 0;
- for (int i0 = -xf; i0 <= xf; i0++)
- for (int j0 = -yf; j0 <= yf; j0++)
- {
- int x = i + i0, y = j + j0;
- if (x >= 0 && x < img.Width &&
- y >= 0 && y < img.Height)
- {
- Color pix = img.GetPixel(x, y);
- rr += pix.R;
- rg += pix.G;
- rb += pix.B;
- count++;
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- }
- if (count > 0)
- {
- rr /= count;
- rg /= count;
- rb /= count;
- }
- res.SetPixel(i, j, Color.FromArgb(rr, rg, rb));
- }
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class Pixelezer : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- [DefaultValue(20), LabelText("Horizontal length")]
- public int XFactor { get; set; }
- [DefaultValue(20), LabelText("Vertical length")]
- public int YFactor { get; set; }
- public string Name
- {
- get { return "Pixelezer"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Stylize; }
- }
- public string Description
- {
- get { return "Pixelezer"; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public Image ApplyEffect(Image image)
- {
- completed = false;
- int xfactor = XFactor, yfactor = YFactor;
- Bitmap img = (Bitmap)image;
- int hparts = img.Width / xfactor;
- int vparts = img.Height / yfactor;
- int divnum = xfactor * yfactor;
- int cr = 0, cg = 0, cb = 0;
- Bitmap res = new Bitmap(hparts * xfactor, vparts * yfactor);
- Graphics g = Graphics.FromImage((Image)res);
- int max = hparts * vparts;
- int it = 0;
- for (int i = 0; i < img.Width - xfactor; i += xfactor)
- for (int j = 0; j < img.Height - yfactor; j += yfactor)
- {
- for (int i0 = i; i0 < i + xfactor; i0++)
- for (int j0 = j; j0 < j + yfactor; j0++)
- {
- Color pix = img.GetPixel(i0, j0);
- cr += pix.R;
- cg += pix.G;
- cb += pix.B;
- }
- cr /= divnum;
- cg /= divnum;
- cb /= divnum;
- g.FillRectangle(new SolidBrush(Color.FromArgb(cr, cg, cb)), new Rectangle(new Point(i, j), new Size(xfactor, yfactor)));
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- g.Flush();
- completed = true;
- return res;
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- }
- public class HSBRegulator : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- [DefaultValue(0f), LabelText("Shift brighness value"), DecimalLimits(Minimum = -100, Maximum = 100)]
- public decimal ShiftBrighnessValue { get; set; }
- [DefaultValue(0f), LabelText("ShiftHue value"), DecimalLimits(Minimum = -100, Maximum = 100)]
- public decimal ShiftHueValue { get; set; }
- [DefaultValue(0f), LabelText("Shift saturation value"), DecimalLimits(Minimum = -100, Maximum = 100)]
- public decimal ShiftSaturationValue { get; set; }
- public string Name
- {
- get { return "HSB regulator"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Adjustments; }
- }
- public string Description
- {
- get { return "Configure HSB image values"; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- public Image ApplyEffect(Image image)
- {
- Bitmap img = (Bitmap)image;
- Bitmap res = new Bitmap(img.Width, img.Height);
- int max = img.Width * img.Height;
- int it = 0;
- for (int i = 0; i < img.Width; ++i)
- for (int j = 0; j < img.Height; ++j)
- {
- Color c = img.GetPixel(i, j);
- c = HSBColor.ShiftBrighness(c, (float)ShiftBrighnessValue);
- c = HSBColor.ShiftHue(c, (float)ShiftHueValue);
- c = HSBColor.ShiftSaturation(c, (float)ShiftSaturationValue);
- res.SetPixel(i, j, c);
- progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
- }
- return res;
- }
- }
- public class Histogramm : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- public string Name
- {
- get { return "Histogramm builder"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Analistic; }
- }
- public string Description
- {
- get { return "This plugin build histogramm for image"; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version(1, 0, 0, 0); }
- }
- public int ProgressValue
- {
- get { return progress; }
- }
- public bool Completed
- {
- get { return completed; }
- }
- private int max(int r, int g, int b)
- {
- if (r >= g && r >= b) return r;
- if (g >= r && g >= b) return g;
- if (b >= r && b >= g) return b;
- return -1;
- }
- public Tuple<int[], int[], int[]> getHistogramData(Image image)
- {
- int[] redHisto = new int[0x100];
- int[] greenHisto = new int[0x100];
- int[] blueHisto = new int[0x100];
- using (Bitmap img = (Bitmap)image.Clone())
- {
- int pmax = img.Width * img.Height;
- int it = 0;
- int maxCount = 0;
- for (int i = 0; i < img.Width; ++i)
- for (int j = 0; j < img.Height; ++j)
- {
- Color col = img.GetPixel(i, j);
- redHisto[col.R]++;
- greenHisto[col.G]++;
- blueHisto[col.B]++;
- int cmax = max(col.R, col.G, col.B);
- if (cmax > maxCount) maxCount = cmax;
- progress = (int)LibHelper.Display(++it, 0f, pmax, 0f, 100f);
- }
- }
- return Tuple.Create(redHisto, greenHisto, blueHisto);
- }
- public Image ApplyEffect(Image image)
- {
- var t = getHistogramData(image);
- int[] redHisto = t.Item1;
- int[] greenHisto = t.Item2;
- int[] blueHisto = t.Item3;
- int maxCount = Math.Max(redHisto.Length, Math.Max(greenHisto.Length, blueHisto.Length)); ;
- int ih = 3 * maxCount;
- int ihd = ih - maxCount;
- Bitmap histogramm = new Bitmap(0x100 * 3, ih);
- Graphics gr = Graphics.FromImage(histogramm);
- gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- Point[] rhpts = new Point[0x100 + 2];
- Point[] ghpts = new Point[0x100 + 2];
- Point[] bhpts = new Point[0x100 + 2];
- rhpts[0] = new Point(0, ih);
- ghpts[0] = new Point(0x100, ih);
- bhpts[0] = new Point(0x100 * 2, ih);
- for (int i = 0; i < 0x100; ++i)
- {
- rhpts[i + 1] = new Point(i, ih - redHisto[i] *250);
- ghpts[i + 1] = new Point(i + 0x100, ih - greenHisto[i] *250);
- bhpts[i + 1] = new Point(i + 0x100 * 2, ih - blueHisto[i] *250);
- }
- rhpts[0x100 + 1] = new Point(0x100, ih);
- ghpts[0x100 + 1] = new Point(0x100 * 2, ih);
- bhpts[0x100 + 1] = new Point(0x100 * 3, ih);
- gr.DrawImage(image, new Rectangle(Point.Empty, histogramm.Size));
- gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 255, 0, 0)), rhpts);
- gr.DrawPolygon(new Pen(Color.Red, 1.5f), rhpts);
- gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 0, 255, 0)), ghpts);
- gr.DrawPolygon(new Pen(Color.Green, 1.5f), ghpts);
- gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 0, 0, 255)), bhpts);
- gr.DrawPolygon(new Pen(Color.Blue, 1.5f), bhpts);
- Form frm = new Form();
- frm.BackgroundImageLayout = ImageLayout.Stretch;
- frm.BackgroundImage = histogramm;
- frm.ShowDialog();
- this.completed = true;
- return image;
- }
- }
- public class BuildColorMap : IEffectPlugin
- {
- private int progress = 0;
- private bool completed = true;
- public string Name
- {
- get { return "Color map builder"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Analistic; }
- }
- public string Description
- {
- get { return "Build color map for image."; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version("1.0.0.0"); }
- }
- public int ProgressValue
- {
- get { return this.progress; }
- }
- public bool Completed
- {
- get { return this.completed; }
- }
- private float sqr(float x)
- {
- return x * x;
- }
- public Color mostSimColor(Color ci)
- {
- Color co = ci;
- int fmin = int.MaxValue;
- List<Color> table = new List<Color>();
- List<PropertyInfo> cols = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public).ToList();
- cols.ForEach(pic => { if (pic.PropertyType == typeof(Color)) table.Add((Color)pic.GetValue(null, null)); });
- foreach (var cc in table)
- {
- int fi = 30 * (int)sqr(cc.R - ci.R) +
- 59 * (int)sqr(cc.G - ci.G) +
- 11 * (int)sqr(cc.B - ci.B);
- if (fi < fmin)
- {
- co = cc;
- fmin = fi;
- }
- }
- return co;
- }
- public Image ApplyEffect(Image image)
- {
- Dictionary<Color, int> dict = new Dictionary<Color, int>();
- int pmax = image.Width * image.Height;
- int it = 0;
- for (int x = 0; x < image.Width; ++x)
- for (int y = 0; y < image.Height; ++y)
- {
- Color sim = mostSimColor((image as Bitmap).GetPixel(x, y));
- if (dict.ContainsKey(sim)) dict[sim] = dict[sim] + 1;
- else dict.Add(sim, 0);
- progress = (int)LibHelper.Display(++it, 0f, pmax, 0f, 100f);
- }
- Color[] arr = dict.OrderBy(kv => kv.Value).Where((_, i) => i < 5).Select(kv => kv.Key).ToArray();
- Bitmap res = new Bitmap(image.Width, image.Height + 50);
- using (Graphics gr0 = Graphics.FromImage(res))
- {
- using (Bitmap tmp = new Bitmap(image.Width, 50))
- {
- using (Graphics gr = Graphics.FromImage(tmp))
- {
- float w0 = image.Width / (float)arr.Length;
- for (int i = 0; i < arr.Length; ++i)
- gr.FillRectangle(new SolidBrush(arr[i]), w0 * i, 0f, w0, 50f);
- }
- gr0.DrawImage(tmp, 0, image.Height);
- }
- gr0.DrawImage(image, Point.Empty);
- }
- Form resfrm = new Form();
- resfrm.BackgroundImage = res;
- resfrm.Show();
- this.completed = true;
- return image;
- }
- }
- [Obsolete]
- public class RGBShift : IEffectPlugin
- {
- public enum Direction
- {
- Left, Rigth,
- Up, Down
- }
- private int progress = 0;
- private bool completed = true;
- public Direction RedDirection { get; set; }
- public int RedLength { get; set; }
- public Direction GreenDirection { get; set; }
- public int GreenLength { get; set; }
- public Direction BlueDirection { get; set; }
- public int BlueLength { get; set; }
- public string Name
- {
- get { return "RGB Shift"; }
- }
- public string SubMenuName
- {
- get { return DefaultSubMenuNames.Distort; }
- }
- public string Description
- {
- get { return ""; }
- }
- public string Author
- {
- get { return "Alex Sabaka"; }
- }
- public Version Version
- {
- get { return new Version(1, 0, 0, 0); }
- }
- public int ProgressValue
- {
- get { return this.progress; }
- }
- public bool Completed
- {
- get { return this.completed; }
- }
- public Image ApplyEffect(Image image)
- {
- Image redBmp = new Bitmap(image.Width, image.Height);
- Graphics gr = Graphics.FromImage(redBmp);
- gr.Clear(Color.Red);
- Image greenBmp = new Bitmap(image.Width, image.Height);
- gr = Graphics.FromImage(redBmp);
- gr.Clear(Color.Green);
- Image blueBmp = new Bitmap(image.Width, image.Height);
- gr = Graphics.FromImage(redBmp);
- gr.Clear(Color.Blue);
- //------------------------------------------------------
- Image redCh = LibHelper.Multiply(image, redBmp);
- Image greenCh = LibHelper.Multiply(image, greenBmp);
- Image blueCh = LibHelper.Multiply(image, blueBmp);
- //------------------------------------------------------
- Image rRedCh = new Bitmap(image.Width, image.Height);
- gr = Graphics.FromImage(rRedCh);
- int dx = RedDirection == Direction.Left ? -RedLength : RedDirection == Direction.Rigth ? RedLength : 0;
- int dy = RedDirection == Direction.Up ? -RedLength : RedDirection == Direction.Down ? RedLength : 0;
- gr.DrawImage(redCh, dx, dy);
- Image rGreenCh = new Bitmap(image.Width, image.Height);
- gr = Graphics.FromImage(rGreenCh);
- dx = GreenDirection == Direction.Left ? -GreenLength : GreenDirection == Direction.Rigth ? GreenLength : 0;
- dy = GreenDirection == Direction.Up ? -GreenLength : GreenDirection == Direction.Down ? GreenLength : 0;
- gr.DrawImage(redCh, dx, dy);
- Image rBlueCh = new Bitmap(image.Width, image.Height);
- gr = Graphics.FromImage(rBlueCh);
- dx = BlueDirection == Direction.Left ? -BlueLength : BlueDirection == Direction.Rigth ? BlueLength : 0;
- dy = BlueDirection == Direction.Up ? -BlueLength : BlueDirection == Direction.Down ? BlueLength : 0;
- gr.DrawImage(redCh, dx, dy);
- //------------------------------------------------------
- Image tmpImg = LibHelper.Additive(rRedCh, rGreenCh);
- Image result = LibHelper.Additive(tmpImg, rBlueCh);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement