Esempio n. 1
0
        public static bool Median(ref Bitmap bmp, int area)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((area < 1) | (area > 5)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int num = (2 * area + 1) * (2 * area + 1);
            int mid = num / 2; // mid-index of zero starting array

            byte[] rr = new byte[num];
            byte[] gg = new byte[num];
            byte[] bb = new byte[num];

            int indx;

            Bitmap tmp = (Bitmap)bmp.Clone();

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    indx = 0;

                    for (int iy = y - area; iy <= y + area; iy++)
                        for (int ix = x - area; ix <= x + area; ix++)
                        {
                            if ((ix < 0) | (ix > w - 1)) continue;
                            if ((iy < 0) | (iy > h - 1)) continue;

                            src.SetXY(ix, iy);
                            rr[indx] = src.R;
                            gg[indx] = src.G;
                            bb[indx] = src.B;

                            indx++;
                        }

                    Array.Sort(rr, 0, indx);
                    Array.Sort(gg, 0, indx);
                    Array.Sort(bb, 0, indx);

                    if (indx == num) indx = mid; else indx = indx / 2;

                    dst.SetXY(x, y);
                    dst.R = rr[indx];
                    dst.G = gg[indx];
                    dst.B = bb[indx];
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 2
0
        public static bool Wave(ref Bitmap bmp, WaveMode wm, double factor,
                                                                double frequency)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int mg = 1; // margin for interpolations

            double f = factor;
            double c = frequency;
            double deg = Math.PI / 180d;

            RectangleF rct = new RectangleF(0, 0, w - 1, h - 1);

            Bitmap tmp = new Bitmap(w + mg * 2, h + mg * 2, bmp.PixelFormat);

            Graphics g = Graphics.FromImage(tmp);
            g.Clear(Color.Gray);
            g.DrawImageUnscaled(bmp, mg, mg);
            g.Dispose();

            double xx, yy;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    xx = x;
                    yy = y;

                    switch (wm)
                    {
                        case WaveMode.wmHorizontal:
                            yy = y + f * Math.Sin(c * deg * x);
                            break;

                        case WaveMode.wmVertical:
                            xx = x + f * Math.Sin(c * deg * y);
                            break;

                        case WaveMode.wmBoth:
                            xx = x + f * Math.Sin(c * deg * y);
                            yy = y + f * Math.Sin(c * deg * x);
                            break;
                    }

                    if (rct.Contains(new PointF((float)xx, (float)yy)))
                    {
                        xx = xx + mg;
                        yy = yy + mg;

                        intBilinear(dst, src, x, y, xx, yy);
                    }
                    else
                    {
                        if (xx < 0) xx = 0;
                        if (xx > w - 1) xx = w - 1;
                        if (yy < 0) yy = 0;
                        if (yy > h - 1) yy = h - 1;

                        xx = xx + mg;
                        yy = yy + mg;

                        intBilinear(dst, src, x, y, xx, yy);
                    }

                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 3
0
        public static bool BathroomWindow1(ref Bitmap bmp,
                                    int zone, BathroomWindowMode wm)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 2) | (zone > 100)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int rnd = zone * 2 + 1;

            int xx, yy;

            Bitmap tmp = bmp.Clone() as Bitmap;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    xx = x; yy = y;

                    switch (wm)
                    {
                        case BathroomWindowMode.bwVertical:
                            xx = x + (x % rnd) - zone;
                            break;
                        case BathroomWindowMode.bwHorizontal:
                            yy = y + (y % rnd) - zone;
                            break;
                        case BathroomWindowMode.bwBoth:
                            xx = x + (x % rnd) - zone;
                            yy = y + (y % rnd) - zone;
                            break;
                    }

                    if ((xx > 0) & (xx < w) & (yy > 0) & (yy < h))
                    {
                        dst.SetXY(x, y);
                        src.SetXY(xx, yy);
                        dst.R = src.R;
                        dst.G = src.G;
                        dst.B = src.B;
                    }
                }
            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 4
0
        public static bool TwoColorGrayScale(ref Bitmap bmp, Color Dark, Color Bright)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp = new Bitmap(w, h, PixelFormat.Format8bppIndexed);

            if (!SetTwoColorGrayPalette(tmp, Dark, Bright))
            {
                tmp.Dispose();
                return false;
            }

            BmpProc24 src = new BmpProc24(bmp);
            BmpProc8 dst = new BmpProc8(tmp);

            int ir;

            for (int y = 0; y < h; ++y)
                for (int x = 0; x < w; ++x)
                {
                    ir = src.IndexR(x, y);

                    dst[x, y] = (byte)(src[ir] * 0.299 +
                                         src[ir - 1] * 0.587 + src[ir - 2] * 0.114);

                }
            CallDispose(dst, src, bmp);

            bmp = tmp;
            return true;
        }
Esempio n. 5
0
        public static bool TwoValued2(Bitmap bmp24, Color BKcolor, out Bitmap bmp1)
        {
            bmp1 = null;

            if (bmp24.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp24.Width;
            int h = bmp24.Height;

            byte r = BKcolor.R;
            byte g = BKcolor.G;
            byte b = BKcolor.B;

            bmp1 = new Bitmap(w, h, PixelFormat.Format1bppIndexed);

            int ir;

            BmpProc24 src = new BmpProc24(bmp24);
            BmpProc1 dst = new BmpProc1(bmp1);

            for (int y = 0; y < h; ++y)
                for (int x = 0; x < w; ++x)
                {
                    ir = src.IndexR(x, y);       // current

                    dst[x, y] = ((src[ir] == r) && (src[ir - 1] == g) && (src[ir - 2] == b));

                }

            CallDispose(dst, src);

            return true;
        }
Esempio n. 6
0
        public static bool Sharpen(ref Bitmap bmp, int percent)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp = (Bitmap)(bmp.Clone());

            Edge(ref tmp, false);

            double f = (double)(percent) / 100;

            int ird, irs;

            BmpProc24 dst = new BmpProc24(bmp);
            BmpProc24 src = new BmpProc24(tmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    ird = dst.IndexR(x, y);
                    irs = src.IndexR(x, y);

                    dst[ird] = AdjustByte(dst[ird] + (src[irs] - 127) * f);
                    dst[ird - 1] = AdjustByte(dst[ird - 1] + (src[irs - 1] - 127) * f);
                    dst[ird - 2] = AdjustByte(dst[ird - 2] + (src[irs - 2] - 127) * f);
                }
            CallDispose(src, dst, tmp);

            return true;
        }
Esempio n. 7
0
        public static bool Swirl(ref Bitmap bmp, double factor,
                                       int cx, int cy, Color bkColor)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int mg = 2; // margin for interpolations

            double c = factor;
            if ((cx == 0) & (cy == 0)) { cx = w / 2; cy = h / 2; }

            Bitmap tmp = new Bitmap(w + mg * 2, h + mg * 2, bmp.PixelFormat);

            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, mg, mg);
            g.Dispose();

            g = Graphics.FromImage(bmp);
            g.Clear(bkColor);
            g.Dispose();

            RectangleF rct = new RectangleF(-1, -1, w + 1, h + 1);

            double r, a, xx, yy;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
                    a = Math.Atan2(y - cy, x - cx);                // radian
                    a = a + c * r;
                    xx = r * Math.Cos(a) + cx;
                    yy = r * Math.Sin(a) + cy;

                    if (rct.Contains(new PointF((float)xx, (float)yy)))
                    {
                        xx = xx + mg;
                        yy = yy + mg;

                        intBilinear(dst, src, x, y, xx, yy);
                    }
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 8
0
        public static bool PhotoBlur(ref Bitmap bmp, int zone, int baseOffset)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 1) | (zone > 30)) return false;

            if ((baseOffset < 0) | (baseOffset > 20)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            double baseB = 1.010d + (double)baseOffset / 1000d;
            double[] paw = new double[256];

            for (int i = 0; i < 256; i++)
                paw[i] = Math.Pow(baseB, (double)i);

            int[,] mask = new int[zone * 2 + 1, zone * 2 + 1];
            int r = zone * zone;

            for (int i = 0; i < zone * 2 + 1; i++)
                for (int j = 0; j < zone * 2 + 1; j++)
                    if ((i - zone) * (i - zone) + (j - zone) * (j - zone) <= r)
                        mask[i, j] = 1;
                    else
                        mask[i, j] = 0;

            double rr, gg, bb;
            int count;

            Bitmap tmp = bmp.Clone() as Bitmap;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    count = 0;
                    rr = gg = bb = 0d;

                    for (int iy = y - zone; iy < y + zone + 1; iy++)
                        for (int ix = x - zone; ix < x + zone + 1; ix++)
                        {
                            if ((iy < 0) | (iy > h - 1)) continue;
                            if ((ix < 0) | (ix > w - 1)) continue;

                            src.SetXY(ix, iy);
                            rr += paw[src.R] * mask[ix - x + zone, iy - y + zone];
                            gg += paw[src.G] * mask[ix - x + zone, iy - y + zone];
                            bb += paw[src.B] * mask[ix - x + zone, iy - y + zone];
                            count += mask[ix - x + zone, iy - y + zone];
                        }

                    dst.SetXY(x, y);
                    dst.R = AdjustByte(Math.Log(rr / count, baseB));
                    dst.G = AdjustByte(Math.Log(gg / count, baseB));
                    dst.B = AdjustByte(Math.Log(bb / count, baseB));
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 9
0
        public static bool Pixelate(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 2) | (zone > 30)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int ir, count, sumr, sumg, sumb;

            BmpProc24 src = new BmpProc24(bmp);

            for (int y = 0; y < h; y += zone)
                for (int x = 0; x < w; x += zone)
                {
                    count = sumr = sumg = sumb = 0;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            count++;

                            ir = src.IndexR(ix, iy);
                            sumr += src[ir];
                            sumg += src[ir - 1];
                            sumb += src[ir - 2];
                        }

                    sumr = sumr / count;
                    sumg = sumg / count;
                    sumb = sumb / count;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            ir = src.IndexR(ix, iy);
                            src[ir] = (byte)sumr;
                            src[ir - 1] = (byte)sumg;
                            src[ir - 2] = (byte)sumb;
                        }
                }

            src.Dispose();

            return true;
        }
Esempio n. 10
0
        public static bool P2PCore(ref Bitmap bmp, P2PFunc func, params double[] param)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            byte r, g, b;
            int ir;

            BmpProc24 bd = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    ir = bd.IndexR(x, y);

                    r = bd[ir]; g = bd[ir - 1]; b = bd[ir - 2];

                    func(ref r, ref g, ref b, param);

                    bd[ir] = r; bd[ir - 1] = g; bd[ir - 2] = b;
                }

            bd.Dispose();

            return true;
        }
Esempio n. 11
0
        public static bool PatternMosaic(ref Bitmap bmp, PatternShape ps,
                                                     int width, int height)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((width < 5) | (width > 20)) return false;
            if ((height < 5) | (height > 60)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            double ww = (double)width;
            double hh = (double)height;

            double[] wd = new double[height];
            int[] iwd = new int[height];

            switch (ps)
            {
                case PatternShape.Brick:
                    for (int i = 0; i < height; i++)
                    {
                        if ((double)i < (hh / 2d))
                            wd[i] = ww;
                        else
                            wd[i] = 0;
                        iwd[i] = (int)(wd[i] + 0.5);
                    }
                    break;

                case PatternShape.Diamond:
                    for (int i = 0; i < height; i++)
                    {
                        if ((double)i < (hh / 2d))
                            wd[i] = ww * (double)i * 2d / hh;
                        else
                            wd[i] = ww * 2d - ww * (double)i * 2d / hh;
                        iwd[i] = (int)(wd[i] + 0.5);
                    }
                    break;

                case PatternShape.Hexagon:
                    for (int i = 0; i < height; i++)
                    {
                        if ((double)i < (hh / 6d))
                            wd[i] = ww * (double)i * 6d / hh;
                        else
                        {
                            if ((double)i < (hh / 2d))
                                wd[i] = ww;
                            else
                            {
                                if ((double)i < (hh * 2d / 3d))
                                    wd[i] = ww * 4d - ww * (double)i * 6d / hh;
                                else
                                    wd[i] = 0;
                            }
                        }

                        iwd[i] = (int)(wd[i] + 0.5);
                    }
                    break;

                case PatternShape.Circle:
                    for (int i = 0; i < height; i++)
                    {
                        if ((double)i < (hh / 2d))
                            wd[i] = ww * (double)i * 2d / hh
                                - (ww / 8d) * Math.Sin((double)i * 4d / hh * Math.PI);
                        else
                            wd[i] = ww * 2d - ww * (double)i * 2d / hh
                                + (ww / 8d) * Math.Sin((double)(i - hh / 2d) *
                                                                    4d / hh * Math.PI);
                        iwd[i] = (int)(wd[i] + 0.5);
                    }
                    break;
            }

            bool eflag = false;

            int r, g, b, count, d, im, yinit;
            double rm;

            Bitmap tmp = bmp.Clone() as Bitmap;

            Graphics gr = Graphics.FromImage(bmp);
            gr.SmoothingMode = SmoothingMode.AntiAlias;

            Pen pn = new Pen(Color.Black, 1.0f);

            BmpProc24 src = new BmpProc24(tmp);

            for (int x = 0; x < w - 1 + width; x += width)
            {
                eflag = !eflag;

                if (eflag) yinit = 0; else yinit = -height / 2;

                for (int y = yinit; y < h - 1; y += height)
                {
                    r = g = b = count = 0;

                    for (int iy = y; iy < y + height; iy++)
                    {
                        if ((iy > h - 1) | (iy < 0)) continue;

                        d = iy % height;

                        if (eflag)
                        {
                            im = iwd[d];
                            rm = wd[d];
                        }
                        else
                        {
                            im = width - iwd[d];
                            rm = ww - wd[d];
                        }

                        for (int ix = x - im; ix <= x + im; ix++)
                        {
                            if ((ix < 0) | (ix > w - 1)) continue;

                            src.SetXY(ix, iy);
                            r += src.R;
                            g += src.G;
                            b += src.B;
                            count++;
                        }
                    }

                    if (count == 0) continue;

                    r /= count;
                    g /= count;
                    b /= count;

                    pn.Color = Color.FromArgb(r, g, b);

                    for (int iy = y; iy < y + height; iy++)
                    {
                        if ((iy > h - 1) | (iy < 0)) continue;

                        d = iy % height;

                        if (eflag)
                        {
                            im = iwd[d];
                            rm = wd[d];
                        }
                        else
                        {
                            im = width - iwd[d];
                            rm = (double)width - wd[d];
                        }

                        gr.DrawLine(pn, (float)(x - rm), (float)iy,
                                                 (float)(x + rm), (float)iy);

                    }
                }
            }

            CallDispose(src, gr, tmp);

            return true;
        }
Esempio n. 12
0
        public static bool OilPaint(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int[] rh = new int[16];
            int[] gh = new int[16];
            int[] bh = new int[16];

            int rmax, gmax, bmax, rindx, gindx, bindx;
            int r, g, b, countr, countg, countb;

            Bitmap tmp = (Bitmap)bmp.Clone();

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    rmax = gmax = bmax = -1;
                    rindx = gindx = bindx = 0;
                    for (int i = 0; i <= 15; i++)
                    { rh[i] = gh[i] = bh[i] = 0; }

                    for (int iy = y - zone; iy <= y + zone; iy++)
                        for (int ix = x - zone; ix <= x + zone; ix++)
                        {
                            if ((iy < 0) | (iy > h - 1)) continue;
                            if ((ix < 0) | (ix > w - 1)) continue;

                            src.SetXY(ix, iy);
                            rh[src.R >> 4]++;
                            gh[src.G >> 4]++;
                            bh[src.B >> 4]++;
                        }

                    for (int i = 0; i <= 15; i++)
                    {
                        if (rmax < rh[i]) { rmax = rh[i]; rindx = i; }
                        if (gmax < gh[i]) { gmax = gh[i]; gindx = i; }
                        if (bmax < bh[i]) { bmax = bh[i]; bindx = i; }
                    }

                    r = g = b = countr = countg = countb = 0;

                    for (int iy = y - zone; iy <= y + zone; iy++)
                        for (int ix = x - zone; ix <= x + zone; ix++)
                        {
                            if ((iy < 0) | (iy > h - 1)) continue;
                            if ((ix < 0) | (ix > w - 1)) continue;

                            src.SetXY(ix, iy);
                            if ((src.R >> 4) == rindx) { r += src.R; countr++; }
                            if ((src.G >> 4) == gindx) { g += src.G; countg++; }
                            if ((src.B >> 4) == bindx) { b += src.B; countb++; }
                        }

                    dst.SetXY(x, y);
                    dst.R = (byte)(r / countr);
                    dst.G = (byte)(g / countg);
                    dst.B = (byte)(b / countb);
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 13
0
        public static bool MotionBlur(ref Bitmap bmp, int range, double angle)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if (range < 2) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            angle += 180;

            double sn = Math.Sin(angle * Math.PI / 180d);
            double cs = Math.Cos(angle * Math.PI / 180d);

            int[] dx = new int[range];
            int[] dy = new int[range];

            for (int i = 0; i < range; i++)
            {
                dx[i] = (int)(cs * (i + 1) + 0.5d);
                dy[i] = (int)(sn * (i + 1) + 0.5d);
            }

            int xx, yy, rr, gg, bb, count;

            Bitmap tmp = bmp.Clone() as Bitmap;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    src.SetXY(x, y);

                    rr = src.R;
                    gg = src.G;
                    bb = src.B;

                    count = 1;

                    for (int i = 1; i <= range; i++)
                    {
                        xx = x + dx[i - 1];
                        yy = y + dy[i - 1];

                        if ((xx < 0) | (xx > w - 1) | (yy < 0) | (yy > h - 1))
                            continue;

                        src.SetXY(xx, yy);
                        rr += src.R;
                        gg += src.G;
                        bb += src.B;
                        count++;
                    }

                    dst.SetXY(x, y);

                    dst.R = (byte)(rr / count);
                    dst.G = (byte)(gg / count);
                    dst.B = (byte)(bb / count);
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 14
0
        public static bool Mirror(ref Bitmap bmp, double cx, double cy,
                                        double angle, Color bkColor)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((angle < 0) | (angle > 360)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            if ((cx < 0) | (cx > w - 1) | (cy < 0) | (cy > h - 1))
                return false;

            int mg = 3; // margin for interpolations
            RectangleF rct = new RectangleF(-1, -1, w + 1, h + 1);

            double deg = Math.PI / 180d;

            double a, b, sn, cs;

            if (Math.Abs(Math.Cos(angle * deg)) < 1.0E-20)
                a = 1.0E20;
            else
                a = Math.Sin(angle * deg) / Math.Cos(angle * deg);

            bool fUp = ((90 < angle) & (angle <= 270));

            b = cy - a * cx;

            if (fUp)
            {
                sn = Math.Sin((angle - 90) * deg);
                cs = Math.Cos((angle - 90) * deg);
            }
            else
            {
                sn = Math.Sin((angle + 90) * deg);
                cs = Math.Cos((angle + 90) * deg);
            }

            Bitmap tmp = new Bitmap(w + mg * 2, h + mg * 2, bmp.PixelFormat);

            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, mg, mg);
            g.Dispose();

            double xx, yy, dd;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    if ((fUp & (a * x + b > y)) | (!fUp & (a * x + b < y)))
                    {

                        dd = Math.Abs(y - a * x - b) / Math.Sqrt(1 + a * a);

                        if (y < a * x + b)
                        {
                            xx = x + 2 * dd * cs;
                            yy = y + 2 * dd * sn;
                        }
                        else
                        {
                            xx = x - 2 * dd * cs;
                            yy = y - 2 * dd * sn;
                        }

                        if (rct.Contains((float)xx, (float)yy))
                        {
                            xx = xx + mg;
                            yy = yy + mg;

                            ImageUtils.intBicubic(dst, src, x, y, xx, yy);
                        }
                        else
                        {
                            dst[x, y, eRGB.r] = bkColor.R;
                            dst[x, y, eRGB.g] = bkColor.G;
                            dst[x, y, eRGB.b] = bkColor.B;
                        }

                    }
                }

            ImageUtils.CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 15
0
        public static bool Scramble(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 2) | (zone > 30)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int num = zone * zone;

            byte[] rr = new byte[num];
            byte[] gg = new byte[num];
            byte[] bb = new byte[num];
            int[] ra = new int[num];

            int indx;
            Random rnd = new Random();
            int j, t;

            BmpProc24 src = new BmpProc24(bmp);

            for (int y = 0; y < h - 1; y += zone)
                for (int x = 0; x < w - 1; x += zone)
                {

                    indx = 0;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((ix > w - 1) | (iy > h - 1)) continue;
                            src.SetXY(ix, iy);
                            rr[indx] = src.R;
                            gg[indx] = src.G;
                            bb[indx] = src.B;
                            indx++;
                        }

                    for (int i = 0; i < indx; i++) ra[i] = i;

                    for (int i = 0; i < indx - 1; i++)
                    {
                        j = rnd.Next(i, indx);
                        t = ra[i];
                        ra[i] = ra[j];
                        ra[j] = t;
                    }

                    indx = 0;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((ix > w - 1) | (iy > h - 1)) continue;
                            src.SetXY(ix, iy);
                            src.R = rr[ra[indx]];
                            src.G = gg[ra[indx]];
                            src.B = bb[ra[indx]];
                            indx++;
                        }
                }

            src.Dispose();

            return true;
        }
Esempio n. 16
0
        public static bool Pixelate2(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 2) | (zone > 30)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int ir, count, sumr, sumg, sumb;

            byte[,] rr = new byte[w / zone + 1, h / zone + 1];
            byte[,] gg = new byte[w / zone + 1, h / zone + 1];
            byte[,] bb = new byte[w / zone + 1, h / zone + 1];

            int countX, countY;

            byte rrr, ggg, bbb;

            countY = 0;

            BmpProc24 src = new BmpProc24(bmp);

            for (int y = 0; y < h; y += zone)
            {
                countX = 0;

                for (int x = 0; x < w; x += zone)
                {
                    count = sumr = sumg = sumb = 0;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            count++;

                            ir = src.IndexR(ix, iy);
                            sumr += src[ir];
                            sumg += src[ir - 1];
                            sumb += src[ir - 2];
                        }

                    sumr = sumr / count;
                    sumg = sumg / count;
                    sumb = sumb / count;

                    rr[countX, countY] = (byte)sumr;
                    gg[countX, countY] = (byte)sumg;
                    bb[countX, countY] = (byte)sumb;

                    if (sumr > 127)
                    { rrr = (byte)(sumr * 0.8); }
                    else
                    { rrr = (byte)(sumr * 1.2); }

                    if (sumg > 127)
                    { ggg = (byte)(sumg * 0.8); }
                    else
                    { ggg = (byte)(sumg * 1.2); }

                    if (sumb > 127)
                    { bbb = (byte)(sumb * 0.8); }
                    else
                    { bbb = (byte)(sumb * 1.2); }

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            ir = src.IndexR(ix, iy);
                            src[ir] = rrr;
                            src[ir - 1] = ggg;
                            src[ir - 2] = bbb;
                        }

                    countX++;

                }

                countY++;
            }

            src.Dispose();

            countY = 0;

            SolidBrush br = new SolidBrush(Color.Black);

            Rectangle rct = new Rectangle(0, 0, zone, zone);

            Graphics g = Graphics.FromImage(bmp);
            g.SmoothingMode = SmoothingMode.AntiAlias;

            for (int y = 0; y < h; y += zone)
            {
                countX = 0;

                for (int x = 0; x < w; x += zone)
                {
                    br.Color = Color.FromArgb(rr[countX, countY], gg[countX, countY],
                                                                   bb[countX, countY]);
                    rct.X = x; rct.Y = y;
                    g.FillEllipse(br, rct);

                    countX++;
                }

                countY++;
            }

            g.Dispose();
            br.Dispose();

            return true;
        }
Esempio n. 17
0
        public static bool AdditiveColor(Bitmap bmpDst, Bitmap bmpSrc,
                                    RGBSelection rgb, int dstX, int dstY)
        {
            if (bmpDst.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if (bmpSrc.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            Rectangle dstRect = new Rectangle(0, 0,
                                            bmpDst.Width, bmpDst.Height);
            Rectangle srcRect = new Rectangle(dstX, dstY,
                                            bmpSrc.Width, bmpSrc.Height);

            if (!dstRect.IntersectsWith(srcRect)) return true;

            dstRect.Intersect(srcRect);

            int scanX = dstRect.X;
            int scanY = dstRect.Y;
            int scanEndX = scanX + dstRect.Width;
            int scanEndY = scanY + dstRect.Height;

            BmpProc24 src = new BmpProc24(bmpSrc);
            BmpProc24 dst = new BmpProc24(bmpDst);

            switch (rgb)
            {
                case RGBSelection.all:
                    for (int y = scanY; y < scanEndY; y++)
                        for (int x = scanX; x < scanEndX; x++)
                        {
                            src.SetXY(x - dstX, y - dstY);
                            dst.SetXY(x, y);

                            dst.R = AdjustByte(dst.R + src.R);
                            dst.G = AdjustByte(dst.G + src.G);
                            dst.B = AdjustByte(dst.B + src.B);
                        }
                    break;

                case RGBSelection.red:
                    for (int y = scanY; y < scanEndY; y++)
                        for (int x = scanX; x < scanEndX; x++)
                        {
                            src.SetXY(x - dstX, y - dstY);
                            dst.SetXY(x, y);

                            dst.R = AdjustByte(dst.R + src.R);
                        }
                    break;

                case RGBSelection.green:
                    for (int y = scanY; y < scanEndY; y++)
                        for (int x = scanX; x < scanEndX; x++)
                        {
                            src.SetXY(x - dstX, y - dstY);
                            dst.SetXY(x, y);

                            dst.G = AdjustByte(dst.G + src.G);
                        }
                    break;

                case RGBSelection.blue:
                    for (int y = scanY; y < scanEndY; y++)
                        for (int x = scanX; x < scanEndX; x++)
                        {
                            src.SetXY(x - dstX, y - dstY);
                            dst.SetXY(x, y);

                            dst.B = AdjustByte(dst.B + src.B);
                        }
                    break;
            }

            CallDispose(dst, src);

            return true;
        }
Esempio n. 18
0
        public static bool Pixelate3(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 2) | (zone > 30)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int ir, count, sumr, sumg, sumb;

            byte[,] rr = new byte[w / zone + 1, h / zone + 1];
            byte[,] gg = new byte[w / zone + 1, h / zone + 1];
            byte[,] bb = new byte[w / zone + 1, h / zone + 1];

            int countX, countY;

            byte rrr, ggg, bbb;

            countY = 0;

            BmpProc24 src = new BmpProc24(bmp);

            for (int y = 0; y < h; y += zone)
            {
                countX = 0;

                for (int x = 0; x < w; x += zone)
                {
                    count = sumr = sumg = sumb = 0;

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            count++;

                            ir = src.IndexR(ix, iy);
                            sumr += src[ir];
                            sumg += src[ir - 1];
                            sumb += src[ir - 2];
                        }

                    sumr = sumr / count;
                    sumg = sumg / count;
                    sumb = sumb / count;

                    rr[countX, countY] = (byte)sumr;
                    gg[countX, countY] = (byte)sumg;
                    bb[countX, countY] = (byte)sumb;

                    rrr = (byte)sumr;
                    ggg = (byte)sumg;
                    bbb = (byte)sumb;

                    if ((sumr < 230) & (sumr > 15)) { rrr = (byte)(sumr - 15); }
                    if ((sumg < 230) & (sumg > 15)) { ggg = (byte)(sumg - 15); }
                    if ((sumb < 230) & (sumb > 15)) { bbb = (byte)(sumb - 15); }

                    for (int iy = y; iy < y + zone; iy++)
                        for (int ix = x; ix < x + zone; ix++)
                        {
                            if ((iy > h - 1) | (ix > w - 1)) continue;

                            ir = src.IndexR(ix, iy);
                            src[ir] = rrr;
                            src[ir - 1] = ggg;
                            src[ir - 2] = bbb;
                        }

                    countX++;

                }

                countY++;
            }

            src.Dispose();

            countY = 0;

            SolidBrush br = new SolidBrush(Color.Black);

            Point[] pt = new Point[3];

            Graphics g = Graphics.FromImage(bmp);
            g.SmoothingMode = SmoothingMode.AntiAlias;

            for (int y = 0; y < h; y += zone)
            {
                countX = 0;

                for (int x = 0; x < w; x += zone)
                {
                    rrr = rr[countX, countY]; ggg = gg[countX, countY];
                    bbb = bb[countX, countY];
                    if ((rrr < 230) & (rrr > 15)) { rrr = (byte)(rrr + 15); }
                    if ((ggg < 230) & (ggg > 15)) { ggg = (byte)(ggg + 15); }
                    if ((bbb < 230) & (bbb > 15)) { bbb = (byte)(bbb + 15); }
                    br.Color = Color.FromArgb(rrr, ggg, bbb);
                    pt[0].X = x; pt[0].Y = y;
                    pt[1].X = x + zone; pt[1].Y = y;
                    pt[2].X = x; pt[2].Y = y + zone;
                    g.FillPolygon(br, pt);

                    countX++;
                }

                countY++;
            }

            g.Dispose();
            br.Dispose();

            return true;
        }
Esempio n. 19
0
        public static bool Sobel(ref Bitmap bmp, bool fGrayScale)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int[,] hmask = { {-1, -2, -1},
                             { 0,  0,  0},
                             { 1,  2,  1}};
            int[,] vmask = { {-1,  0,  1},
                             {-2,  0,  2},
                             {-1,  0,  1}};

            int xx, yy, mx, my;
            double rv, gv, bv, rh, gh, bh;

            Bitmap tmp = (Bitmap)bmp.Clone();

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    rv = gv = bv = rh = gh = bh = 0;

                    for (int iy = y - 1; iy <= y + 1; iy++)
                        for (int ix = x - 1; ix <= x + 1; ix++)
                        {
                            mx = ix - x + 1; my = iy - y + 1;
                            xx = ix; yy = iy;
                            if ((ix < 0) | (ix > w - 1)) xx = x;
                            if ((iy < 0) | (iy > h - 1)) yy = y;

                            src.SetXY(xx, yy);

                            rv += src.R * vmask[mx, my];
                            gv += src.G * vmask[mx, my];
                            bv += src.B * vmask[mx, my];

                            rh += src.R * hmask[mx, my];
                            gh += src.G * hmask[mx, my];
                            bh += src.B * hmask[mx, my];
                        }

                    dst.SetXY(x, y);

                    dst.R = AdjustByte(Math.Sqrt(rv * rv + rh * rh));
                    dst.G = AdjustByte(Math.Sqrt(gv * gv + gh * gh));
                    dst.B = AdjustByte(Math.Sqrt(bv * bv + bh * bh));
                }

            CallDispose(dst, src, tmp);

            if (fGrayScale)
                return GrayScale(ref bmp);
            else
                return true;
        }
Esempio n. 20
0
        public static bool AdditiveColor(Bitmap bmpDst, Bitmap bmpSrc,
            RGBSelection rgb)
        {
            if (bmpDst.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if (bmpSrc.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmpDst.Width;
            int h = bmpDst.Height;

            if ((bmpSrc.Width != w) | (bmpSrc.Height != h)) return false;

            BmpProc24 src = new BmpProc24(bmpSrc);
            BmpProc24 dst = new BmpProc24(bmpDst);

            switch (rgb)
            {
                case RGBSelection.all:
                    for (int y = 0; y < h; y++)
                        for (int x = 0; x < w; x++)
                        {
                            src.SetXY(x, y);
                            dst.SetXY(x, y);

                            dst.R = AdjustByte(dst.R + src.R);
                            dst.G = AdjustByte(dst.G + src.G);
                            dst.B = AdjustByte(dst.B + src.B);
                        }
                    break;

                case RGBSelection.red:
                    for (int y = 0; y < h; y++)
                        for (int x = 0; x < w; x++)
                        {
                            src.SetXY(x, y);
                            dst.SetXY(x, y);

                            dst.R = AdjustByte(dst.R + src.R);
                        }
                    break;

                case RGBSelection.green:
                    for (int y = 0; y < h; y++)
                        for (int x = 0; x < w; x++)
                        {
                            src.SetXY(x, y);
                            dst.SetXY(x, y);

                            dst.G = AdjustByte(dst.G + src.G);
                        }
                    break;

                case RGBSelection.blue:
                    for (int y = 0; y < h; y++)
                        for (int x = 0; x < w; x++)
                        {
                            src.SetXY(x, y);
                            dst.SetXY(x, y);

                            dst.B = AdjustByte(dst.B + src.B);
                        }
                    break;
            }

            CallDispose(dst, src);

            return true;
        }
Esempio n. 21
0
        public static bool Twist(ref Bitmap bmp, double factor, int frequency,
                                                double radius, int cx, int cy)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if (radius < 0) return false;

            if ((frequency < 1) | (frequency > 16)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int q = frequency;

            int mg = 2; // margin for interpolations

            double c = factor;
            if ((cx == 0) & (cy == 0)) { cx = w / 2; cy = h / 2; }

            Bitmap tmp = new Bitmap(w + mg * 2, h + mg * 2, bmp.PixelFormat);

            Graphics g = Graphics.FromImage(tmp);
            g.Clear(Color.LightGray);
            g.DrawImageUnscaled(bmp, mg, mg);
            g.Dispose();

            RectangleF rct = new RectangleF(0, 0, w - 1, h - 1);

            double r, a, xx, yy;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));

                    if ((r > radius) | (r < 1)) continue;

                    a = Math.Atan2(y - cy, x - cx);                // radian
                    a = a + c * Math.Sin(2d * q * r / radius * Math.PI) / r;

                    xx = r * Math.Cos(a) + cx;
                    yy = r * Math.Sin(a) + cy;

                    if (rct.Contains(new PointF((float)xx, (float)yy)))
                    {
                        xx = xx + mg;
                        yy = yy + mg;

                        intBicubic(dst, src, x, y, xx, yy);
                    }
                    else
                    {
                        if (xx < 0) xx = 0;
                        if (xx > w - 1) xx = w - 1;
                        if (yy < 0) yy = 0;
                        if (yy > h - 1) yy = h - 1;

                        xx = xx + mg;
                        yy = yy + mg;

                        intBicubic(dst, src, x, y, xx, yy);

                    }
                }
            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 22
0
        public static bool Radiance(ref Bitmap bmp, double factor, int cx, int cy)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            double r, r1, a;
            int f, rr, gg, bb, xx, yy, count;

            Bitmap tmp = bmp.Clone() as Bitmap;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
                    a = Math.Atan2(y - cy, x - cx);                // radian
                    f = (int)(factor * r * 0.1);

                    if (f < 1) continue;

                    rr = gg = bb = count = 0;

                    for (int i = -f; i <= 0; i++)
                    {
                        r1 = r + i;
                        xx = (int)(r1 * Math.Cos(a) + cx);
                        yy = (int)(r1 * Math.Sin(a) + cy);

                        if ((xx < 0) | (xx > w - 1)) continue;
                        if ((yy < 0) | (yy > h - 1)) continue;

                        src.SetXY(xx, yy);

                        rr += src.R;
                        gg += src.G;
                        bb += src.B;
                        count++;
                    }

                    dst.SetXY(x, y);

                    dst.R = (byte)(rr / count);
                    dst.G = (byte)(gg / count);
                    dst.B = (byte)(bb / count);
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 23
0
        public static bool TwoValued1(Bitmap bmp24, int threshold, out Bitmap bmp1)
        {
            bmp1 = null;

            if (bmp24.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp24.Width;
            int h = bmp24.Height;

            bmp1 = new Bitmap(w, h, PixelFormat.Format1bppIndexed);

            int srcR;

            BmpProc24 src = new BmpProc24(bmp24);
            BmpProc1 dst = new BmpProc1(bmp1);

            for (int y = 0; y < h; ++y)
                for (int x = 0; x < w; ++x)
                {
                    srcR = src.IndexR(x, y);       // current

                    dst[x, y] = ((src[srcR] * 0.299 +
                          src[srcR - 1] * 0.587 + src[srcR - 2] * 0.114) > threshold);

                }

            CallDispose(dst, src);

            return true;
        }
Esempio n. 24
0
        public static bool Rank(ref Bitmap bmp, int area, RankOp ro)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((area < 1) | (area > 10)) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int num = (2 * area + 1) * (2 * area + 1);
            byte rmax, gmax, bmax, rmin, gmin, bmin;

            Bitmap tmp = (Bitmap)bmp.Clone();

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    rmax = gmax = bmax = 0;
                    rmin = gmin = bmin = 255;

                    for (int iy = y - area; iy <= y + area; iy++)
                        for (int ix = x - area; ix <= x + area; ix++)
                        {
                            if ((ix < 0) | (ix > w - 1)) continue;
                            if ((iy < 0) | (iy > h - 1)) continue;

                            src.SetXY(ix, iy);

                            switch (ro)
                            {
                                case RankOp.Max:
                                    rmax = Math.Max(rmax, src.R);
                                    gmax = Math.Max(gmax, src.G);
                                    bmax = Math.Max(bmax, src.B);
                                    break;

                                case RankOp.Mid:
                                    rmax = Math.Max(rmax, src.R);
                                    gmax = Math.Max(gmax, src.G);
                                    bmax = Math.Max(bmax, src.B);
                                    rmin = Math.Min(rmin, src.R);
                                    gmin = Math.Min(gmin, src.G);
                                    bmin = Math.Min(bmin, src.B);
                                    break;

                                case RankOp.Min:
                                    rmin = Math.Min(rmin, src.R);
                                    gmin = Math.Min(gmin, src.G);
                                    bmin = Math.Min(bmin, src.B);
                                    break;
                            }
                        }

                    dst.SetXY(x, y);

                    switch (ro)
                    {
                        case RankOp.Max:
                            dst.R = rmax;
                            dst.G = gmax;
                            dst.B = bmax;
                            break;

                        case RankOp.Mid:
                            dst.R = ImageUtils.AdjustByte((rmax + rmin) / 2);
                            dst.G = ImageUtils.AdjustByte((gmax + gmin) / 2);
                            dst.B = ImageUtils.AdjustByte((bmax + bmin) / 2);
                            break;

                        case RankOp.Min:
                            dst.R = rmin;
                            dst.G = gmin;
                            dst.B = bmin;
                            break;
                    }
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 25
0
        public static bool UnsharpMask(ref Bitmap bmp, int zone, int percent)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((zone < 0) | (zone > 15)) return false;
            if (percent < 0) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            double f = (double)percent / 100d;

            Bitmap tmp = bmp.Clone() as Bitmap;

            GaussianBlur(ref tmp, zone);

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(bmp);

            for (int y = 0; y < h - 1; y++)
                for (int x = 0; x < w - 1; x++)
                {
                    src.SetXY(x, y);
                    dst.SetXY(x, y);
                    dst.R = AdjustByte(dst.R + f * (dst.R - src.R));
                    dst.G = AdjustByte(dst.G + f * (dst.G - src.G));
                    dst.B = AdjustByte(dst.B + f * (dst.B - src.B));
                }

            CallDispose(dst, src, tmp);

            return true;
        }
Esempio n. 26
0
        public static bool RGBHistogram(Bitmap bmp, out Bitmap bmpHist)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
            {
                bmpHist = null;
                return false;
            }

            int w = bmp.Width;
            int h = bmp.Height;

            int[] hist = new int[256];
            int[] rh = new int[256];
            int[] gh = new int[256];
            int[] bh = new int[256];

            int ir, indx;

            BmpProc24 bd = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    ir = bd.IndexR(x, y);
                    rh[bd[ir]]++;
                    gh[bd[ir - 1]]++;
                    bh[bd[ir - 2]]++;
                    indx = (int)(bd[ir] * 0.299 + bd[ir - 1] * 0.587 +
                                                           bd[ir - 2] * 0.114);
                    hist[indx]++;
                }

            bd.Dispose();

            int max = -1;
            for (int i = 0; i < 256; i++)
            {
                if (rh[i] > max) max = rh[i];
                if (gh[i] > max) max = gh[i];
                if (bh[i] > max) max = bh[i];
                if (hist[i] > max) max = hist[i];
            }

            PointF[] pt = new PointF[256];

            bmpHist = new Bitmap(275, 160, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(bmpHist);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.Clear(Color.AntiqueWhite);

            Pen pen = new Pen(Color.Red, 1.5F);
            for (int i = 0; i < 256; i++)
            {
                pt[i].X = 10 + i;
                pt[i].Y = (float)(150 - rh[i] * 140f / max);
            }

            g.DrawLines(pen, pt);

            pen.Color = Color.ForestGreen;
            for (int i = 0; i < 256; i++)
                pt[i].Y = (float)(150 - gh[i] * 140f / max);

            g.DrawLines(pen, pt);

            pen.Color = Color.Blue;
            for (int i = 0; i < 256; i++)
                pt[i].Y = (float)(150 - bh[i] * 140f / max);

            g.DrawLines(pen, pt);

            pen.Color = Color.Black;
            for (int i = 0; i < 256; i++)
                pt[i].Y = (float)(150 - hist[i] * 140f / max);

            g.DrawLines(pen, pt);

            pen.Color = Color.Black;

            g.DrawLine(pen, 8, 150, 8, 10);

            for (int i = 0; i <= 20; i++)
                if ((i % 2) == 0)
                    g.DrawLine(pen, 8, 150 - 7 * i, 4, 150 - 7 * i);
                else
                    g.DrawLine(pen, 8, 150 - 7 * i, 6, 150 - 7 * i);

            g.DrawLine(pen, 10, 150, 10 + 255, 150);

            for (int i = 0; i <= 51; i++)
                if ((i % 2) == 0)
                    g.DrawLine(pen, 10 + 5 * i, 150, 10 + 5 * i, 154);
                else
                    g.DrawLine(pen, 10 + 5 * i, 150, 10 + 5 * i, 152);

            g.Dispose();

            return true;
        }
Esempio n. 27
0
        public static bool AlphaGradient(ref Bitmap bmp, float[] factor,
                                              float[] position, GradientSide gs)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(tmp);
            g.DrawImageUnscaled(bmp, 0, 0);
            g.Dispose();

            Color startColor = Color.White;
            Color endColor = Color.Black;

            Point start = new Point(-1, 0);
            Point end = new Point(0, 0);

            switch (gs)
            {
                case GradientSide.Left:
                    end.X = w;
                    break;

                case GradientSide.Right:
                    start.X = w;
                    break;

                case GradientSide.Upper:
                    end.Y = h;
                    break;

                case GradientSide.Lower:
                    start.Y = h;
                    break;

                case GradientSide.UpperLeft:
                    end.X = w;
                    end.Y = h;
                    break;

                case GradientSide.UpperRight:
                    start.X = w;
                    end.Y = h;
                    break;

                case GradientSide.LowerLeft:
                    start.Y = h;
                    end.X = w;
                    break;

                case GradientSide.LowerRight:
                    start.X = w;
                    start.Y = h;
                    break;
            }

            Blend bl = new Blend();
            bl.Factors = factor;
            bl.Positions = position;

            LinearGradientBrush br = new LinearGradientBrush(
                                        start, end, startColor, endColor);
            br.Blend = bl;
            //br.GammaCorrection = true;

            Rectangle rct = new Rectangle(0, 0, w, h);

            g = Graphics.FromImage(bmp);
            g.FillRectangle(br, rct);
            g.Dispose();

            BmpProc24 src = new BmpProc24(bmp);
            BmpProc32 dst = new BmpProc32(tmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                    dst[x, y, eRGB.a] = src[x, y, eRGB.r];

            CallDispose(dst, src, bmp);

            bmp = tmp;

            return true;
        }
Esempio n. 28
0
        public static bool SaturationHistogram(Bitmap bmp, out Bitmap bmpHist)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
            {
                bmpHist = null;
                return false;
            }

            int w = bmp.Width;
            int h = bmp.Height;

            int[] hist = new int[256];

            int hue, satu, lumi;
            byte rr, gg, bb;

            BmpProc24 src = new BmpProc24(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    src.SetXY(x, y);

                    rr = src.R; gg = src.G; bb = src.B;
                    ImageUtils.RGBToHSL(rr, gg, bb, out hue, out satu, out lumi);

                    hist[satu]++;
                }

            src.Dispose();

            int max = -1;
            for (int i = 0; i < 256; i++)
                if (hist[i] > max) max = hist[i];

            for (int i = 0; i < 256; i++)
                hist[i] = hist[i] * 140 / max;

            bmpHist = new Bitmap(275, 160, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(bmpHist);
            g.Clear(Color.AntiqueWhite);

            Pen pen = new Pen(Color.Gray, 1F);

            for (int i = 0; i < 256; i++)
                g.DrawLine(pen, 10 + i, 150, 10 + i, 150 - hist[i]);

            pen.Color = Color.Black;

            g.DrawLine(pen, 8, 150, 8, 10);

            for (int i = 0; i <= 20; i++)
                if ((i % 2) == 0)
                    g.DrawLine(pen, 8, 150 - 7 * i, 4, 150 - 7 * i);
                else
                    g.DrawLine(pen, 8, 150 - 7 * i, 6, 150 - 7 * i);

            g.DrawLine(pen, 10, 150, 10 + 255, 150);

            for (int i = 0; i <= 51; i++)
                if ((i % 10) == 0)
                    g.DrawLine(pen, 10 + 5 * i, 150, 10 + 5 * i, 156);
                else
                    if ((i % 5) == 0)
                        g.DrawLine(pen, 10 + 5 * i, 150, 10 + 5 * i, 154);
                    else
                        g.DrawLine(pen, 10 + 5 * i, 150, 10 + 5 * i, 152);

            g.Dispose();

            return true;
        }
Esempio n. 29
0
        public static bool Transform(ref Bitmap bmp, Color bkColor)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int mg = 3; // margin for interpolations

            int q = Math.Max(w, h);

            RectangleF rct = new RectangleF(-1, -1, q + 1, q + 1);

            Bitmap tmp1 = new Bitmap(q + mg * 2, q + mg * 2, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(tmp1);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.Clear(bkColor);
            g.DrawImage(bmp, mg, mg, q, q);
            g.Dispose();

            Bitmap tmp2 = new Bitmap(q, q, PixelFormat.Format24bppRgb);
            g = Graphics.FromImage(tmp2);
            g.Clear(bkColor);
            g.Dispose();

            double l, xx, yy;
            double r = (double)q / 2d;

            BmpProc24 src = new BmpProc24(tmp1);
            BmpProc24 dst = new BmpProc24(tmp2);

            for (int y = 1; y < q; y++)
                for (int x = 0; x < q; x++)
                {
                    l = Math.Sqrt(2d * r * y - y * y);
                    if (l == 0) xx = 0; else xx = r * (x - r) / l + r;
                    yy = y;

                    if (rct.Contains(new PointF((float)xx, (float)yy)))
                    {
                        xx = xx + mg;
                        yy = yy + mg;

                        ImageUtils.intBicubic(dst, src, x, y, xx, yy);
                    }
                }

            ImageUtils.CallDispose(dst, src, tmp1);

            g = Graphics.FromImage(bmp);
            g.Clear(bkColor);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(tmp2, 0, 0, w, h);
            g.Dispose();

            tmp2.Dispose();

            return true;
        }
Esempio n. 30
0
        public static bool Luca(ref Bitmap bmp, bool bInvert,
                            int outerRadius, int innerRadius, Color bkColor)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((outerRadius - innerRadius) < 30) return false;

            int w = bmp.Width;
            int h = bmp.Height;

            int radius = outerRadius - innerRadius;

            int mg = 3; // margin for interpolations

            Bitmap tmp = new Bitmap(w + mg * 2, h + mg * 2,
                                                PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, mg, mg);
            g.Dispose();

            int rr = (radius + innerRadius) * 2 + 2;

            Bitmap dbmp = new Bitmap(rr, rr, PixelFormat.Format24bppRgb);

            double cx = (double)(radius + innerRadius + 1);
            double cy = (double)(radius + innerRadius + 1);

            g = Graphics.FromImage(dbmp);
            g.Clear(bkColor);
            g.Dispose();

            RectangleF rct = new RectangleF(-1, -1, w + 1, h + 1);

            double r, a, xx, yy;
            double pi2 = Math.PI * 2d;

            BmpProc24 src = new BmpProc24(tmp);
            BmpProc24 dst = new BmpProc24(dbmp);

            for (int y = 0; y < rr; y++)
                for (int x = 0; x < rr; x++)
                {
                    r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
                    a = Math.Atan2(y - cy, x - cx);                // radian

                    r = r - innerRadius;

                    xx = (a + Math.PI) / pi2 * (w - 1);
                    if (bInvert)
                        yy = (1d - r / radius) * (h - 1);
                    else
                        yy = r / radius * (h - 1);

                    if (rct.Contains(new PointF((float)xx, (float)yy)))
                    {
                        xx = xx + mg;
                        yy = yy + mg;

                        intBicubic(dst, src, x, y, xx, yy);
                    }
                }

            CallDispose(dst, src, tmp, bmp);
            bmp = dbmp;

            if (bInvert)
                bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
            else
                bmp.RotateFlip(RotateFlipType.Rotate90FlipX);

            return true;
        }