Advertisement
Guest User

Plugins Lib

a guest
Feb 1st, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 38.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using PlugAPI;
  8. using System.Reflection;
  9. using AutoGui;
  10. using System.Drawing.Imaging;
  11.  
  12. namespace PlugsLib
  13. {
  14.     public struct HSBColor
  15.     {
  16.         float h;
  17.         float s;
  18.         float b;
  19.         int a;
  20.  
  21.         public HSBColor(float h, float s, float b)
  22.         {
  23.             this.a = 0xff;
  24.             this.h = Math.Min(Math.Max(h, 0), 255);
  25.             this.s = Math.Min(Math.Max(s, 0), 255);
  26.             this.b = Math.Min(Math.Max(b, 0), 255);
  27.         }
  28.         public HSBColor(int a, float h, float s, float b)
  29.         {
  30.             this.a = a;
  31.             this.h = Math.Min(Math.Max(h, 0), 255);
  32.             this.s = Math.Min(Math.Max(s, 0), 255);
  33.             this.b = Math.Min(Math.Max(b, 0), 255);
  34.         }
  35.         public HSBColor(Color color)
  36.         {
  37.             HSBColor temp = FromColor(color);
  38.             this.a = temp.a;
  39.             this.h = temp.h;
  40.             this.s = temp.s;
  41.             this.b = temp.b;
  42.         }
  43.  
  44.         public float H
  45.         {
  46.             get { return h; }
  47.         }
  48.         public float S
  49.         {
  50.             get { return s; }
  51.         }
  52.         public float B
  53.         {
  54.             get { return b; }
  55.         }
  56.         public int A
  57.         {
  58.             get { return a; }
  59.         }
  60.         public Color Color
  61.         {
  62.             get
  63.             {
  64.                 return FromHSB(this);
  65.             }
  66.         }
  67.  
  68.         public static Color ShiftHue(Color c, float hueDelta)
  69.         {
  70.             HSBColor hsb = HSBColor.FromColor(c);
  71.             hsb.h += hueDelta;
  72.             hsb.h = Math.Min(Math.Max(hsb.h, 0), 255);
  73.             return FromHSB(hsb);
  74.         }
  75.         public static Color ShiftSaturation(Color c, float saturationDelta)
  76.         {
  77.             HSBColor hsb = HSBColor.FromColor(c);
  78.             hsb.s += saturationDelta;
  79.             hsb.s = Math.Min(Math.Max(hsb.s, 0), 255);
  80.             return FromHSB(hsb);
  81.         }
  82.         public static Color ShiftBrighness(Color c, float brightnessDelta)
  83.         {
  84.             HSBColor hsb = HSBColor.FromColor(c);
  85.             hsb.b += brightnessDelta;
  86.             hsb.b = Math.Min(Math.Max(hsb.b, 0), 255);
  87.             return FromHSB(hsb);
  88.         }
  89.  
  90.         public static Color FromHSB(HSBColor hsbColor)
  91.         {
  92.             float r = hsbColor.b;
  93.             float g = hsbColor.b;
  94.             float b = hsbColor.b;
  95.             if (hsbColor.s != 0)
  96.             {
  97.                 float max = hsbColor.b;
  98.                 float dif = hsbColor.b * hsbColor.s / 255f;
  99.                 float min = hsbColor.b - dif;
  100.  
  101.                 float h = hsbColor.h * 360f / 255f;
  102.  
  103.                 if (h < 60f)
  104.                 {
  105.                     r = max;
  106.                     g = h * dif / 60f + min;
  107.                     b = min;
  108.                 }
  109.                 else if (h < 120f)
  110.                 {
  111.                     r = -(h - 120f) * dif / 60f + min;
  112.                     g = max;
  113.                     b = min;
  114.                 }
  115.                 else if (h < 180f)
  116.                 {
  117.                     r = min;
  118.                     g = max;
  119.                     b = (h - 120f) * dif / 60f + min;
  120.                 }
  121.                 else if (h < 240f)
  122.                 {
  123.                     r = min;
  124.                     g = -(h - 240f) * dif / 60f + min;
  125.                     b = max;
  126.                 }
  127.                 else if (h < 300f)
  128.                 {
  129.                     r = (h - 240f) * dif / 60f + min;
  130.                     g = min;
  131.                     b = max;
  132.                 }
  133.                 else if (h <= 360f)
  134.                 {
  135.                     r = max;
  136.                     g = min;
  137.                     b = -(h - 360f) * dif / 60 + min;
  138.                 }
  139.                 else
  140.                 {
  141.                     r = 0;
  142.                     g = 0;
  143.                     b = 0;
  144.                 }
  145.             }
  146.  
  147.             return Color.FromArgb
  148.                 (
  149.                     hsbColor.a,
  150.                     (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
  151.                     (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
  152.                     (int)Math.Round(Math.Min(Math.Max(b, 0), 255))
  153.                     );
  154.         }
  155.         public static HSBColor FromColor(Color color)
  156.         {
  157.             HSBColor ret = new HSBColor(0f, 0f, 0f);
  158.             ret.a = color.A;
  159.  
  160.             float r = color.R;
  161.             float g = color.G;
  162.             float b = color.B;
  163.  
  164.             float max = Math.Max(r, Math.Max(g, b));
  165.  
  166.             if (max <= 0)
  167.             {
  168.                 return ret;
  169.             }
  170.  
  171.             float min = Math.Min(r, Math.Min(g, b));
  172.             float dif = max - min;
  173.  
  174.             if (max > min)
  175.             {
  176.                 if (g == max)
  177.                 {
  178.                     ret.h = (b - r) / dif * 60f + 120f;
  179.                 }
  180.                 else if (b == max)
  181.                 {
  182.                     ret.h = (r - g) / dif * 60f + 240f;
  183.                 }
  184.                 else if (b > g)
  185.                 {
  186.                     ret.h = (g - b) / dif * 60f + 360f;
  187.                 }
  188.                 else
  189.                 {
  190.                     ret.h = (g - b) / dif * 60f;
  191.                 }
  192.                 if (ret.h < 0)
  193.                 {
  194.                     ret.h = ret.h + 360f;
  195.                 }
  196.             }
  197.             else
  198.             {
  199.                 ret.h = 0;
  200.             }
  201.  
  202.             ret.h *= 255f / 360f;
  203.             ret.s = (dif / max) * 255f;
  204.             ret.b = max;
  205.  
  206.             return ret;
  207.         }
  208.     }
  209.  
  210.     public static class LibHelper
  211.     {
  212.         public static float Display(float x, float minA, float maxA, float minB, float maxB)
  213.         {
  214.             double lenA = Math.Abs(maxA - minA);
  215.             double lenB = Math.Abs(maxB - minB);
  216.             double y = lenB * (x - minA) / lenA + minB;
  217.             return (float)y;
  218.         }
  219.  
  220.         public static int Clamp(int x, int min, int max)
  221.         {
  222.             return (x < min ? min : (x > max ? max : x));
  223.         }
  224.  
  225.         public static BitmapData LockImage(Image img, Rectangle rect)
  226.         {
  227.             if (img == null) return null;
  228.             return (img as Bitmap).LockBits(rect, ImageLockMode.ReadWrite, img.PixelFormat);
  229.         }
  230.         public static void UnlockImage(Image img, BitmapData data)
  231.         {
  232.             if (img != null && data != null) (img as Bitmap).UnlockBits(data);
  233.         }
  234.  
  235.         public static Image Additive(Image srcA, Image srcB)
  236.         {
  237.             Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
  238.             for (int i = 0; i < res.Width; ++i)
  239.                 for (int j = 0; j < res.Height; ++j)
  240.                 {
  241.                     var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
  242.                     var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
  243.                     (res as Bitmap).SetPixel(i, j, pixA + pixB);
  244.                 }
  245.             return res;
  246.         }
  247.         public static Image Substractive(Image srcA, Image srcB)
  248.         {
  249.             Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
  250.             for (int i = 0; i < res.Width; ++i)
  251.                 for (int j = 0; j < res.Height; ++j)
  252.                 {
  253.                     var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
  254.                     var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
  255.                     (res as Bitmap).SetPixel(i, j, pixA - pixB);
  256.                 }
  257.             return res;
  258.         }
  259.         public static Image Multiply(Image srcA, Image srcB)
  260.         {
  261.             Image res = new Bitmap(Math.Min(srcA.Width, srcB.Width), Math.Min(srcA.Height, srcB.Height));
  262.             for (int i = 0; i < res.Width; ++i)
  263.                 for (int j = 0; j < res.Height; ++j)
  264.                 {
  265.                     var pixA = (DotGL.Color)(srcA as Bitmap).GetPixel(i, j);
  266.                     var pixB = (DotGL.Color)(srcB as Bitmap).GetPixel(i, j);
  267.                     (res as Bitmap).SetPixel(i, j, pixA * pixB);
  268.                 }
  269.             return res;
  270.         }
  271.     }
  272.  
  273.     public class ImageCircles : IEffectPlugin
  274.     {
  275.         private int progress = 0;
  276.         private bool completed = true;
  277.  
  278.         private struct circ
  279.         {
  280.             public PointF p;
  281.             public float r;
  282.             public Color c;
  283.  
  284.             public circ(PointF p, float r, Color c)
  285.                 : this()
  286.             {
  287.                 this.p = p;
  288.                 this.r = r;
  289.                 this.c = c;
  290.             }
  291.         }
  292.         private float rgb(Color c)
  293.         {
  294.             return (c.R + c.B + c.G) / (3f * 255f);
  295.         }
  296.         private float clamp(float value, float min, float max)
  297.         {
  298.             return value >= max ? max : value <= min ? min : value;
  299.         }
  300.  
  301.         [LabelText("Background color")]
  302.         public Color ClearColor { get; set; }
  303.  
  304.         public string Name
  305.         {
  306.             get { return "Circles image"; }
  307.         }
  308.         public string SubMenuName
  309.         {
  310.             get { return DefaultSubMenuNames.Artistic; }
  311.         }
  312.         public string Description
  313.         {
  314.             get { return "Make all pixels in source image as small circles."; }
  315.         }
  316.         public string Author
  317.         {
  318.             get { return "Alex Sabaka"; }
  319.         }
  320.         public Version Version
  321.         {
  322.             get { return new Version("1.0.0.0"); }
  323.         }
  324.  
  325.         public Image ApplyEffect(Image image)
  326.         {
  327.             completed = false;
  328.             Bitmap bmp = null;
  329.             lock(image)
  330.              bmp = new Bitmap(image, new Size(image.Width, image.Height));
  331.             float maxr = 5f, minr = 0.2f;
  332.             Bitmap res = new Bitmap((int)(bmp.Width * maxr), (int)(bmp.Height * maxr));
  333.             Graphics graphics = Graphics.FromImage((Image)res);
  334.             graphics.Clear(ClearColor);
  335.  
  336.             int max = image.Width * image.Height;
  337.             int it = 0;
  338.  
  339.             for (int i = 0; i < bmp.Width; i++)
  340.                 for (int j = 0; j < bmp.Height; j++)
  341.                 {
  342.                     Color pix = bmp.GetPixel(i, j);
  343.                     float r = clamp(rgb(pix) * maxr, minr, maxr - 2);
  344.                     PointF pos = new PointF(i * maxr - r, j * maxr - r);
  345.                     graphics.FillEllipse(new SolidBrush(pix),
  346.                         new RectangleF(pos, new SizeF(r * 2f, r * 2f)));
  347.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  348.                 }
  349.             graphics.Flush();
  350.             completed = true;
  351.             return res;
  352.         }
  353.  
  354.         public int ProgressValue
  355.         {
  356.             get { return progress; }
  357.         }
  358.         public bool Completed
  359.         {
  360.             get { return completed; }
  361.         }
  362.     }
  363.  
  364.     public class Grayscale : IEffectPlugin
  365.     {
  366.         private int progress = 0;
  367.         private bool completed = true;
  368.  
  369.         private float rgb(Color c)
  370.         {
  371.             return (c.R + c.B + c.G) / (3f * 255f);
  372.         }
  373.  
  374.         public string Name
  375.         {
  376.             get { return "Grayscale"; }
  377.         }
  378.         public string SubMenuName
  379.         {
  380.             get { throw new NotImplementedException(); }
  381.         }
  382.         public string Description
  383.         {
  384.             get { return "Make image grayscale."; }
  385.         }
  386.         public string Author
  387.         {
  388.             get { return "Alex Sabaka"; }
  389.         }
  390.         public Version Version
  391.         {
  392.             get { return new Version("1.0.0.0"); }
  393.         }
  394.  
  395.         public Image ApplyEffect(Image image)
  396.         {
  397.             completed = false;
  398.             Bitmap bmp = (Bitmap)image;
  399.             Bitmap res = new Bitmap(image.Width, image.Height);
  400.  
  401.             int max = image.Width * image.Height;
  402.             int it = 0;
  403.  
  404.             for (int i = 0; i < image.Width; i++)
  405.                 for (int j = 0; j < image.Height; j++)
  406.                 {
  407.                     int wbColor = (int)(rgb(bmp.GetPixel(i, j)) * 255);
  408.                     res.SetPixel(i, j, Color.FromArgb(255, wbColor, wbColor, wbColor));
  409.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  410.                 }
  411.             completed = true;
  412.             return res;
  413.         }
  414.  
  415.         public int ProgressValue
  416.         {
  417.             get { return progress; }
  418.         }
  419.         public bool Completed
  420.         {
  421.             get { return completed; }
  422.         }
  423.     }
  424.  
  425.     public class WhiteOrBlack : IEffectPlugin
  426.     {
  427.         private int progress = 0;
  428.         private bool completed = true;
  429.  
  430.         private float rgb(Color c)
  431.         {
  432.             return (c.R + c.B + c.G) / (3f * 255f);
  433.         }
  434.  
  435.         public string Name
  436.         {
  437.             get { return "White or Black"; }
  438.         }
  439.         public string SubMenuName
  440.         {
  441.             get { return DefaultSubMenuNames.Adjustments; }
  442.         }
  443.         public string Description
  444.         {
  445.             get { return "Replece all colors to white or black."; }
  446.         }
  447.         public string Author
  448.         {
  449.             get { return "Alex Sabaka"; }
  450.         }
  451.         public Version Version
  452.         {
  453.             get { return new Version("1.0.0.0"); }
  454.         }
  455.  
  456.         public Image ApplyEffect(Image image)
  457.         {
  458.             completed = false;
  459.             Bitmap img = (Bitmap)image;
  460.             Bitmap res = new Bitmap(img.Width, img.Height);
  461.  
  462.             int max = image.Width * image.Height;
  463.             int it = 0;
  464.  
  465.             for (int i = 0; i < img.Width; i++)
  466.                 for (int j = 0; j < img.Height; j++)
  467.                 {
  468.                     float pix = rgb(img.GetPixel(i, j));
  469.                     if (pix > .5f) res.SetPixel(i, j, Color.White);
  470.                     else res.SetPixel(i, j, Color.Black);
  471.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  472.                 }
  473.             completed = true;
  474.             return res;
  475.         }
  476.  
  477.         public int ProgressValue
  478.         {
  479.             get { return progress; }
  480.         }
  481.         public bool Completed
  482.         {
  483.             get { return completed; }
  484.         }
  485.     }
  486.  
  487.     public class WBRGBCMY : IEffectPlugin
  488.     {
  489.         private int progress = 0;
  490.         private bool completed = true;
  491.  
  492.         public string Name
  493.         {
  494.             get { return "WBRGBCMY"; }
  495.         }
  496.         public string SubMenuName
  497.         {
  498.             get { return DefaultSubMenuNames.Adjustments; }
  499.         }
  500.         public string Description
  501.         {
  502.             get { return "Effect same to WhiteOrBlack, but here are more colors: white, black, red, green, blue, cyan, magenta, yellow."; }
  503.         }
  504.         public string Author
  505.         {
  506.             get { return "Alex Sabaka"; }
  507.         }
  508.         public Version Version
  509.         {
  510.             get { return new Version("1.0.0.0"); }
  511.         }
  512.  
  513.         public Image ApplyEffect(Image image)
  514.         {
  515.             completed = false;
  516.             Bitmap img = (Bitmap)image;
  517.             Bitmap res = new Bitmap(img.Width, img.Height);
  518.  
  519.             int max = image.Width * image.Height;
  520.             int it = 0;
  521.  
  522.             for (int i = 0; i < img.Width; i++)
  523.                 for (int j = 0; j < img.Height; j++)
  524.                 {
  525.                     Color pix = img.GetPixel(i, j);
  526.                     Color resc = Color.Transparent;
  527.                     float hr = (float)pix.R / 255.0f, hb = (float)pix.B / 255.0f, hg = (float)pix.G / 255.0f;
  528.  
  529.                     if (hr >= .5f) resc = Color.FromArgb(255, resc.G, resc.B);
  530.                     else resc = Color.FromArgb(0, resc.G, resc.B);
  531.  
  532.                     if (hg >= .5f) resc = Color.FromArgb(resc.R, 255, resc.B);
  533.                     else resc = Color.FromArgb(resc.R, 0, resc.B);
  534.  
  535.                     if (hb >= .5f) resc = Color.FromArgb(resc.R, resc.G, 255);
  536.                     else resc = Color.FromArgb(resc.R, resc.G, 0);
  537.  
  538.                     res.SetPixel(i, j, resc);
  539.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  540.                 }
  541.             completed = true;
  542.             return res;
  543.         }
  544.  
  545.         public int ProgressValue
  546.         {
  547.             get { return progress; }
  548.         }
  549.         public bool Completed
  550.         {
  551.             get { return completed; }
  552.         }
  553.     }
  554.  
  555.     public class Negative : IEffectPlugin
  556.     {
  557.         private int progress = 0;
  558.         private bool completed = true;
  559.  
  560.         public string Name
  561.         {
  562.             get { return "Negative"; }
  563.         }
  564.         public string SubMenuName
  565.         {
  566.             get { return DefaultSubMenuNames.Adjustments; }
  567.         }
  568.         public string Description
  569.         {
  570.             get { return "Make image negative."; }
  571.         }
  572.         public string Author
  573.         {
  574.             get { return "Alex Sabaka"; }
  575.         }
  576.         public Version Version
  577.         {
  578.             get { return new Version("1.0.0.0"); }
  579.         }
  580.  
  581.         public Image ApplyEffect(Image image)
  582.         {
  583.             completed = false;
  584.             Bitmap img = (Bitmap)image;
  585.             Bitmap res = new Bitmap(img.Width, img.Height);
  586.  
  587.             int max = image.Width * image.Height;
  588.             int it = 0;
  589.  
  590.             for (int i = 0; i < img.Width; i++)
  591.                 for (int j = 0; j < img.Height; j++)
  592.                 {
  593.                     Color pix = img.GetPixel(i, j);
  594.                     res.SetPixel(i, j, Color.FromArgb(255 - pix.R, 255 - pix.G, 255 - pix.B));
  595.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  596.                 }
  597.             completed = true;
  598.             return res;
  599.         }
  600.  
  601.         public int ProgressValue
  602.         {
  603.             get { return progress; }
  604.         }
  605.         public bool Completed
  606.         {
  607.             get { return completed; }
  608.         }
  609.     }
  610.  
  611.     public class Outline : IEffectPlugin
  612.     {
  613.         private int progress = 0;
  614.         private bool completed = true;
  615.  
  616.         [DefaultValue(20), LabelText("Koeficient")]
  617.         public int Koef { get; set; }
  618.  
  619.         public string Name
  620.         {
  621.             get { return "Outline"; }
  622.         }
  623.         public string SubMenuName
  624.         {
  625.             get { return DefaultSubMenuNames.Stylize; }
  626.         }
  627.         public string Description
  628.         {
  629.             get { return "Mark in source image hot spots."; }
  630.         }
  631.         public string Author
  632.         {
  633.             get { return "Alex Sabaka"; }
  634.         }
  635.         public Version Version
  636.         {
  637.             get { return new Version("1.0.0.0"); }
  638.         }
  639.  
  640.         public Image ApplyEffect(Image image)
  641.         {
  642.             completed = false;
  643.             int factor = Koef;
  644.             Bitmap img = (Bitmap)image;
  645.             Bitmap res = new Bitmap(img.Width, img.Height);
  646.             // lines
  647.  
  648.             int max = image.Width * image.Height * 2 - 2;
  649.             int it = 0;
  650.  
  651.             for (int i = 1; i < img.Width; i++)
  652.                 for (int j = 0; j < img.Height; j++)
  653.                 {
  654.                     Color currPix = img.GetPixel(i, j);
  655.                     Color prewPix = img.GetPixel(i - 1, j);
  656.                     int rf = Math.Abs(currPix.R - prewPix.R),
  657.                         gf = Math.Abs(currPix.G - prewPix.G),
  658.                         bf = Math.Abs(currPix.B - prewPix.B);
  659.                     if (bf > factor || gf > factor || rf > factor) res.SetPixel(i, j, Color.White);
  660.                     else res.SetPixel(i, j, Color.Black);
  661.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  662.                 }
  663.             //colums
  664.             for (int i = 1; i < img.Height; i++)
  665.                 for (int j = 0; j < img.Width; j++)
  666.                 {
  667.                     Color currPix = img.GetPixel(j, i);
  668.                     Color prewPix = img.GetPixel(j, i - 1);
  669.                     int rf = Math.Abs(currPix.R - prewPix.R),
  670.                         gf = Math.Abs(currPix.G - prewPix.G),
  671.                         bf = Math.Abs(currPix.B - prewPix.B);
  672.                     if (bf > factor || gf > factor || rf > factor) res.SetPixel(j, i, Color.White);
  673.                     else res.SetPixel(j, i, Color.Black);
  674.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  675.                 }
  676.             //---------------------------------------------
  677.             completed = true;
  678.             return res;
  679.         }
  680.  
  681.         public int ProgressValue
  682.         {
  683.             get { return progress; }
  684.         }
  685.         public bool Completed
  686.         {
  687.             get { return completed; }
  688.         }
  689.     }
  690.  
  691.     public class GaussianBlur : IEffectPlugin
  692.     {
  693.         private int progress = 0;
  694.         private bool completed = true;
  695.  
  696.         [DefaultValue(3), LabelText("Horizontal length")]
  697.         public int XFactor { get; set; }
  698.         [DefaultValue(3), LabelText("Vertical length")]
  699.         public int YFactor { get; set; }
  700.  
  701.         public string Name
  702.         {
  703.             get { return "Gaussian Blur"; }
  704.         }
  705.         public string SubMenuName
  706.         {
  707.             get { return DefaultSubMenuNames.Blurs; }
  708.         }
  709.         public string Description
  710.         {
  711.             get { return "Gaussian blur."; }
  712.         }
  713.         public string Author
  714.         {
  715.             get { return "Alex Sabaka"; }
  716.         }
  717.         public Version Version
  718.         {
  719.             get { return new Version("1.0.0.0"); }
  720.         }
  721.  
  722.         public Image ApplyEffect(Image image)
  723.         {
  724.             completed = false;
  725.             Bitmap img = (Bitmap)image;
  726.             Bitmap res = new Bitmap(img.Width, img.Height);
  727.  
  728.             int max = image.Width * image.Height * 3 * 3;
  729.             int it = 0;
  730.  
  731.             int xf = XFactor / 2;
  732.             int yf = YFactor / 2;
  733.  
  734.             for (int i = 0; i < img.Width; i++)
  735.                 for (int j = 0; j < img.Height; j++)
  736.                 {
  737.                     int rr = 0, rg = 0, rb = 0;
  738.                     int count = 0;
  739.                     for (int i0 = -xf; i0 <= xf; i0++)
  740.                         for (int j0 = -yf; j0 <= yf; j0++)
  741.                         {
  742.                             int x = i + i0, y = j + j0;
  743.                             if (x >= 0 && x < img.Width &&
  744.                                 y >= 0 && y < img.Height)
  745.                             {
  746.                                 Color pix = img.GetPixel(x, y);
  747.                                 rr += pix.R;
  748.                                 rg += pix.G;
  749.                                 rb += pix.B;
  750.                                 count++;
  751.                                 progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  752.                             }
  753.                         }
  754.                     if (count > 0)
  755.                     {
  756.                         rr /= count;
  757.                         rg /= count;
  758.                         rb /= count;
  759.                     }
  760.                     res.SetPixel(i, j, Color.FromArgb(rr, rg, rb));
  761.                 }
  762.             completed = true;
  763.             return res;
  764.         }
  765.  
  766.         public int ProgressValue
  767.         {
  768.             get { return progress; }
  769.         }
  770.         public bool Completed
  771.         {
  772.             get { return completed; }
  773.         }
  774.     }
  775.  
  776.     public class Pixelezer : IEffectPlugin
  777.     {
  778.         private int progress = 0;
  779.         private bool completed = true;
  780.  
  781.         [DefaultValue(20), LabelText("Horizontal length")]
  782.         public int XFactor { get; set; }
  783.         [DefaultValue(20), LabelText("Vertical length")]
  784.         public int YFactor { get; set; }
  785.  
  786.         public string Name
  787.         {
  788.             get { return "Pixelezer"; }
  789.         }
  790.         public string SubMenuName
  791.         {
  792.             get { return DefaultSubMenuNames.Stylize; }
  793.         }
  794.         public string Description
  795.         {
  796.             get { return "Pixelezer"; }
  797.         }
  798.         public string Author
  799.         {
  800.             get { return "Alex Sabaka"; }
  801.         }
  802.         public Version Version
  803.         {
  804.             get { return new Version("1.0.0.0"); }
  805.         }
  806.  
  807.         public Image ApplyEffect(Image image)
  808.         {
  809.             completed = false;
  810.             int xfactor = XFactor, yfactor = YFactor;
  811.             Bitmap img = (Bitmap)image;
  812.             int hparts = img.Width / xfactor;
  813.             int vparts = img.Height / yfactor;
  814.             int divnum = xfactor * yfactor;
  815.             int cr = 0, cg = 0, cb = 0;
  816.             Bitmap res = new Bitmap(hparts * xfactor, vparts * yfactor);
  817.             Graphics g = Graphics.FromImage((Image)res);
  818.  
  819.             int max = hparts * vparts;
  820.             int it = 0;
  821.  
  822.             for (int i = 0; i < img.Width - xfactor; i += xfactor)
  823.                 for (int j = 0; j < img.Height - yfactor; j += yfactor)
  824.                 {
  825.                     for (int i0 = i; i0 < i + xfactor; i0++)
  826.                         for (int j0 = j; j0 < j + yfactor; j0++)
  827.                         {
  828.                             Color pix = img.GetPixel(i0, j0);
  829.                             cr += pix.R;
  830.                             cg += pix.G;
  831.                             cb += pix.B;
  832.                         }
  833.                     cr /= divnum;
  834.                     cg /= divnum;
  835.                     cb /= divnum;
  836.                     g.FillRectangle(new SolidBrush(Color.FromArgb(cr, cg, cb)), new Rectangle(new Point(i, j), new Size(xfactor, yfactor)));
  837.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  838.                 }
  839.             g.Flush();
  840.             completed = true;
  841.             return res;
  842.         }
  843.  
  844.         public int ProgressValue
  845.         {
  846.             get { return progress; }
  847.         }
  848.         public bool Completed
  849.         {
  850.             get { return completed; }
  851.         }
  852.     }
  853.  
  854.     public class HSBRegulator : IEffectPlugin
  855.     {
  856.         private int progress = 0;
  857.         private bool completed = true;
  858.  
  859.         [DefaultValue(0f), LabelText("Shift brighness value"), DecimalLimits(Minimum = -100, Maximum = 100)]
  860.         public decimal ShiftBrighnessValue { get; set; }
  861.         [DefaultValue(0f), LabelText("ShiftHue value"), DecimalLimits(Minimum = -100, Maximum = 100)]
  862.         public decimal ShiftHueValue { get; set; }
  863.         [DefaultValue(0f), LabelText("Shift saturation value"), DecimalLimits(Minimum = -100, Maximum = 100)]
  864.         public decimal ShiftSaturationValue { get; set; }
  865.  
  866.         public string Name
  867.         {
  868.             get { return "HSB regulator"; }
  869.         }
  870.         public string SubMenuName
  871.         {
  872.             get { return DefaultSubMenuNames.Adjustments; }
  873.         }
  874.         public string Description
  875.         {
  876.             get { return "Configure HSB image values"; }
  877.         }
  878.         public string Author
  879.         {
  880.             get { return "Alex Sabaka"; }
  881.         }
  882.         public Version Version
  883.         {
  884.             get { return new Version("1.0.0.0"); }
  885.         }
  886.  
  887.         public int ProgressValue
  888.         {
  889.             get { return progress; }
  890.         }
  891.         public bool Completed
  892.         {
  893.             get { return completed; }
  894.         }
  895.  
  896.         public Image ApplyEffect(Image image)
  897.         {
  898.             Bitmap img = (Bitmap)image;
  899.             Bitmap res = new Bitmap(img.Width, img.Height);
  900.  
  901.             int max = img.Width * img.Height;
  902.             int it = 0;
  903.  
  904.             for (int i = 0; i < img.Width; ++i)
  905.                 for (int j = 0; j < img.Height; ++j)
  906.                 {
  907.                     Color c = img.GetPixel(i, j);
  908.                     c = HSBColor.ShiftBrighness(c, (float)ShiftBrighnessValue);
  909.                     c = HSBColor.ShiftHue(c, (float)ShiftHueValue);
  910.                     c = HSBColor.ShiftSaturation(c, (float)ShiftSaturationValue);
  911.                     res.SetPixel(i, j, c);
  912.                     progress = (int)LibHelper.Display(++it, 0f, max, 0f, 100f);
  913.                 }
  914.             return res;
  915.         }
  916.     }
  917.  
  918.     public class Histogramm : IEffectPlugin
  919.     {
  920.         private int progress = 0;
  921.         private bool completed = true;
  922.  
  923.         public string Name
  924.         {
  925.             get { return "Histogramm builder"; }
  926.         }
  927.         public string SubMenuName
  928.         {
  929.             get { return DefaultSubMenuNames.Analistic; }
  930.         }
  931.         public string Description
  932.         {
  933.             get { return "This plugin build histogramm for image"; }
  934.         }
  935.         public string Author
  936.         {
  937.             get { return "Alex Sabaka"; }
  938.         }
  939.         public Version Version
  940.         {
  941.             get { return new Version(1, 0, 0, 0); }
  942.         }
  943.  
  944.         public int ProgressValue
  945.         {
  946.             get { return progress; }
  947.         }
  948.         public bool Completed
  949.         {
  950.             get { return completed; }
  951.         }
  952.  
  953.         private int max(int r, int g, int b)
  954.         {
  955.             if (r >= g && r >= b) return r;
  956.             if (g >= r && g >= b) return g;
  957.             if (b >= r && b >= g) return b;
  958.             return -1;
  959.         }
  960.  
  961.         public Tuple<int[], int[], int[]> getHistogramData(Image image)
  962.         {
  963.             int[] redHisto = new int[0x100];
  964.             int[] greenHisto = new int[0x100];
  965.             int[] blueHisto = new int[0x100];
  966.             using (Bitmap img = (Bitmap)image.Clone())
  967.             {
  968.                 int pmax = img.Width * img.Height;
  969.                 int it = 0;
  970.                 int maxCount = 0;
  971.                 for (int i = 0; i < img.Width; ++i)
  972.                     for (int j = 0; j < img.Height; ++j)
  973.                     {
  974.                         Color col = img.GetPixel(i, j);
  975.                         redHisto[col.R]++;
  976.                         greenHisto[col.G]++;
  977.                         blueHisto[col.B]++;
  978.                         int cmax = max(col.R, col.G, col.B);
  979.                         if (cmax > maxCount) maxCount = cmax;
  980.                         progress = (int)LibHelper.Display(++it, 0f, pmax, 0f, 100f);
  981.                     }
  982.             }
  983.             return Tuple.Create(redHisto, greenHisto, blueHisto);
  984.         }
  985.  
  986.         public Image ApplyEffect(Image image)
  987.         {
  988.             var t = getHistogramData(image);
  989.             int[] redHisto = t.Item1;
  990.             int[] greenHisto = t.Item2;
  991.             int[] blueHisto = t.Item3;
  992.             int maxCount = Math.Max(redHisto.Length, Math.Max(greenHisto.Length, blueHisto.Length)); ;
  993.  
  994.             int ih = 3 * maxCount;
  995.             int ihd = ih - maxCount;
  996.             Bitmap histogramm = new Bitmap(0x100 * 3, ih);
  997.             Graphics gr = Graphics.FromImage(histogramm);
  998.             gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  999.             Point[] rhpts = new Point[0x100 + 2];
  1000.             Point[] ghpts = new Point[0x100 + 2];
  1001.             Point[] bhpts = new Point[0x100 + 2];
  1002.             rhpts[0] = new Point(0, ih);
  1003.             ghpts[0] = new Point(0x100, ih);
  1004.             bhpts[0] = new Point(0x100 * 2, ih);
  1005.             for (int i = 0; i < 0x100; ++i)
  1006.             {
  1007.                 rhpts[i + 1] = new Point(i, ih - redHisto[i] *250);
  1008.                 ghpts[i + 1] = new Point(i + 0x100, ih -  greenHisto[i] *250);
  1009.                 bhpts[i + 1] = new Point(i + 0x100 * 2, ih -  blueHisto[i] *250);
  1010.             }
  1011.             rhpts[0x100 + 1] = new Point(0x100, ih);
  1012.             ghpts[0x100 + 1] = new Point(0x100 * 2, ih);
  1013.             bhpts[0x100 + 1] = new Point(0x100 * 3, ih);
  1014.  
  1015.             gr.DrawImage(image, new Rectangle(Point.Empty, histogramm.Size));
  1016.             gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 255, 0, 0)), rhpts);
  1017.             gr.DrawPolygon(new Pen(Color.Red, 1.5f), rhpts);
  1018.             gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 0, 255, 0)), ghpts);
  1019.             gr.DrawPolygon(new Pen(Color.Green, 1.5f), ghpts);
  1020.             gr.FillPolygon(new SolidBrush(Color.FromArgb(80, 0, 0, 255)), bhpts);
  1021.             gr.DrawPolygon(new Pen(Color.Blue, 1.5f), bhpts);
  1022.  
  1023.             Form frm = new Form();
  1024.             frm.BackgroundImageLayout = ImageLayout.Stretch;
  1025.             frm.BackgroundImage = histogramm;
  1026.             frm.ShowDialog();
  1027.             this.completed = true;
  1028.             return image;
  1029.         }
  1030.     }
  1031.  
  1032.     public class BuildColorMap : IEffectPlugin
  1033.     {
  1034.         private int progress = 0;
  1035.         private bool completed = true;
  1036.  
  1037.         public string Name
  1038.         {
  1039.             get { return "Color map builder"; }
  1040.         }
  1041.         public string SubMenuName
  1042.         {
  1043.             get { return DefaultSubMenuNames.Analistic; }
  1044.         }
  1045.         public string Description
  1046.         {
  1047.             get { return "Build color map for image."; }
  1048.         }
  1049.         public string Author
  1050.         {
  1051.             get { return "Alex Sabaka"; }
  1052.         }
  1053.         public Version Version
  1054.         {
  1055.             get { return new Version("1.0.0.0"); }
  1056.         }
  1057.  
  1058.         public int ProgressValue
  1059.         {
  1060.             get { return this.progress; }
  1061.         }
  1062.         public bool Completed
  1063.         {
  1064.             get { return this.completed; }
  1065.         }
  1066.  
  1067.         private float sqr(float x)
  1068.         {
  1069.             return x * x;
  1070.         }
  1071.  
  1072.         public Color mostSimColor(Color ci)
  1073.         {
  1074.             Color co = ci;
  1075.             int fmin = int.MaxValue;
  1076.             List<Color> table = new List<Color>();
  1077.             List<PropertyInfo> cols = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public).ToList();
  1078.             cols.ForEach(pic => { if (pic.PropertyType == typeof(Color)) table.Add((Color)pic.GetValue(null, null)); });
  1079.             foreach (var cc in table)
  1080.             {
  1081.                 int fi = 30 * (int)sqr(cc.R - ci.R) +
  1082.                          59 * (int)sqr(cc.G - ci.G) +
  1083.                          11 * (int)sqr(cc.B - ci.B);
  1084.                 if (fi < fmin)
  1085.                 {
  1086.                     co = cc;
  1087.                     fmin = fi;
  1088.                 }
  1089.             }
  1090.             return co;
  1091.         }
  1092.  
  1093.         public Image ApplyEffect(Image image)
  1094.         {
  1095.             Dictionary<Color, int> dict = new Dictionary<Color, int>();
  1096.             int pmax = image.Width * image.Height;
  1097.             int it = 0;
  1098.             for (int x = 0; x < image.Width; ++x)
  1099.                 for (int y = 0; y < image.Height; ++y)
  1100.                 {
  1101.                     Color sim = mostSimColor((image as Bitmap).GetPixel(x, y));
  1102.                     if (dict.ContainsKey(sim)) dict[sim] = dict[sim] + 1;
  1103.                     else dict.Add(sim, 0);
  1104.                     progress = (int)LibHelper.Display(++it, 0f, pmax, 0f, 100f);
  1105.                 }
  1106.             Color[] arr = dict.OrderBy(kv => kv.Value).Where((_, i) => i < 5).Select(kv => kv.Key).ToArray();
  1107.             Bitmap res = new Bitmap(image.Width, image.Height + 50);
  1108.             using (Graphics gr0 = Graphics.FromImage(res))
  1109.             {
  1110.                 using (Bitmap tmp = new Bitmap(image.Width, 50))
  1111.                 {
  1112.                     using (Graphics gr = Graphics.FromImage(tmp))
  1113.                     {
  1114.                         float w0 = image.Width / (float)arr.Length;
  1115.                         for (int i = 0; i < arr.Length; ++i)
  1116.                             gr.FillRectangle(new SolidBrush(arr[i]), w0 * i, 0f, w0, 50f);
  1117.                     }
  1118.                     gr0.DrawImage(tmp, 0, image.Height);
  1119.                 }
  1120.                 gr0.DrawImage(image, Point.Empty);
  1121.             }
  1122.             Form resfrm = new Form();
  1123.             resfrm.BackgroundImage = res;
  1124.             resfrm.Show();
  1125.             this.completed = true;
  1126.             return image;
  1127.         }
  1128.     }
  1129.  
  1130.     [Obsolete]
  1131.     public class RGBShift : IEffectPlugin
  1132.     {
  1133.         public enum Direction
  1134.         {
  1135.             Left, Rigth,
  1136.             Up, Down
  1137.         }
  1138.  
  1139.         private int progress = 0;
  1140.         private bool completed = true;
  1141.  
  1142.         public Direction RedDirection { get; set; }
  1143.         public int RedLength { get; set; }
  1144.         public Direction GreenDirection { get; set; }
  1145.         public int GreenLength { get; set; }
  1146.         public Direction BlueDirection { get; set; }
  1147.         public int BlueLength { get; set; }
  1148.  
  1149.         public string Name
  1150.         {
  1151.             get { return "RGB Shift"; }
  1152.         }
  1153.         public string SubMenuName
  1154.         {
  1155.             get { return DefaultSubMenuNames.Distort; }
  1156.         }
  1157.         public string Description
  1158.         {
  1159.             get { return ""; }
  1160.         }
  1161.         public string Author
  1162.         {
  1163.             get { return "Alex Sabaka"; }
  1164.         }
  1165.         public Version Version
  1166.         {
  1167.             get { return new Version(1, 0, 0, 0); }
  1168.         }
  1169.  
  1170.         public int ProgressValue
  1171.         {
  1172.             get { return this.progress; }
  1173.         }
  1174.         public bool Completed
  1175.         {
  1176.             get { return this.completed; }
  1177.         }
  1178.  
  1179.         public Image ApplyEffect(Image image)
  1180.         {
  1181.             Image redBmp = new Bitmap(image.Width, image.Height);
  1182.             Graphics gr = Graphics.FromImage(redBmp);
  1183.             gr.Clear(Color.Red);
  1184.             Image greenBmp = new Bitmap(image.Width, image.Height);
  1185.             gr = Graphics.FromImage(redBmp);
  1186.             gr.Clear(Color.Green);
  1187.             Image blueBmp = new Bitmap(image.Width, image.Height);
  1188.             gr = Graphics.FromImage(redBmp);
  1189.             gr.Clear(Color.Blue);
  1190.             //------------------------------------------------------
  1191.             Image redCh = LibHelper.Multiply(image, redBmp);
  1192.             Image greenCh = LibHelper.Multiply(image, greenBmp);
  1193.             Image blueCh = LibHelper.Multiply(image, blueBmp);
  1194.             //------------------------------------------------------
  1195.             Image rRedCh = new Bitmap(image.Width, image.Height);
  1196.             gr = Graphics.FromImage(rRedCh);
  1197.             int dx = RedDirection == Direction.Left ? -RedLength : RedDirection == Direction.Rigth ? RedLength : 0;
  1198.             int dy = RedDirection == Direction.Up ? -RedLength : RedDirection == Direction.Down ? RedLength : 0;
  1199.             gr.DrawImage(redCh, dx, dy);
  1200.  
  1201.             Image rGreenCh = new Bitmap(image.Width, image.Height);
  1202.             gr = Graphics.FromImage(rGreenCh);
  1203.             dx = GreenDirection == Direction.Left ? -GreenLength : GreenDirection == Direction.Rigth ? GreenLength : 0;
  1204.             dy = GreenDirection == Direction.Up ? -GreenLength : GreenDirection == Direction.Down ? GreenLength : 0;
  1205.             gr.DrawImage(redCh, dx, dy);
  1206.  
  1207.             Image rBlueCh = new Bitmap(image.Width, image.Height);
  1208.             gr = Graphics.FromImage(rBlueCh);
  1209.             dx = BlueDirection == Direction.Left ? -BlueLength : BlueDirection == Direction.Rigth ? BlueLength : 0;
  1210.             dy = BlueDirection == Direction.Up ? -BlueLength : BlueDirection == Direction.Down ? BlueLength : 0;
  1211.             gr.DrawImage(redCh, dx, dy);
  1212.             //------------------------------------------------------
  1213.             Image tmpImg = LibHelper.Additive(rRedCh, rGreenCh);
  1214.             Image result = LibHelper.Additive(tmpImg, rBlueCh);
  1215.             return result;
  1216.         }
  1217.     }
  1218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement