Пример #1
0
        private void RoundColors(Bitmap image)
        {
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());

            //Color[] colorTable = ciq.CalculatePalette((Bitmap)UI_PictureViewer.Image, 16);

            UI_PictureViewer.Image = ciq.ReduceColors(image, colorReductionAmount);
        }
Пример #2
0
        public static Bitmap MedianCut(Bitmap bmp, int intensity)
        {
            ColorImageQuantizer ciq       = new ColorImageQuantizer(new MedianCutQuantizer());
            IColorQuantizer     quantizer = new MedianCutQuantizer();
            Bitmap newImage = ciq.ReduceColors(bmp, intensity);

            return(newImage);
        }
Пример #3
0
        //default 8-pallete
        public static Bitmap BurkesColorDithering(Bitmap sourceImage, int paletteSize)
        {
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());

            Color[] colorTable          = ciq.CalculatePalette(sourceImage, paletteSize);
            BurkesColorDithering filter = new BurkesColorDithering();

            filter.ColorTable = colorTable;
            return(filter.Apply(sourceImage));
        }
Пример #4
0
        static byte[] ConvertIcon(string filename)
        {
            List <byte> result = new List <byte>();

            //Get palette
            ushort[] colors_short = new ushort[16];
            Color[]  colors       = new Color[16];
            Bitmap   bitmap       = new Bitmap(filename);

            if (bitmap.PixelFormat != PixelFormat.Format4bppIndexed)
            {
                Console.WriteLine("Icon is {0}, converting to {1} (loss of quality possible)", bitmap.PixelFormat.ToString(), PixelFormat.Format4bppIndexed.ToString());
                ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
                bitmap = ciq.ReduceColors(bitmap, 16);
            }
            for (int u = 0; u < 16; u++)
            {
                colors[u] = bitmap.Palette.Entries[u];
                int a = colors[u].A / 16;
                int r = colors[u].R / 16;
                int g = colors[u].G / 16;
                int b = colors[u].B / 16;
                colors_short[u] = (ushort)(a << 12 | r << 8 | g << 4 | b);
                //Console.WriteLine("Color to binary {0} ({1}) : {2} comp A:{3} R:{4} G:{5} B:{6}", u, colors[u].ToString(), colors_short[u].ToString("X"), a.ToString("X"),r.ToString("X"), g.ToString("X"), b.ToString("X"));
                result.AddRange(BitConverter.GetBytes(colors_short[u]));
                //Console.WriteLine("Color {0}:{1}", u, colors[u].ToString());
            }
            //Get colors
            byte[] image = new byte[1024];
            for (int y = 0; y < 32; y++)
            {
                for (int x = 0; x < 32; x++)
                {
                    for (byte c = 0; c < 16; c++)
                    {
                        if (bitmap.GetPixel(x, y) == colors[c])
                        {
                            image[y * 32 + x] = c;
                            break;
                        }
                    }
                }
            }
            byte[] squeeze = new byte[512];
            for (int u = 0; u < 1024; u++)
            {
                if (u % 2 == 0)
                {
                    squeeze[u / 2] = (byte)(image[u] << 4 | image[u + 1] & 0xF);
                    //Console.WriteLine("Byte {0}: {1} / {2}", image[u], image[u] << 4, image[u + 1] & 0xF);
                }
            }
            result.AddRange(squeeze);
            return(result.ToArray());
        }
Пример #5
0
        private Bitmap reducedColor(Bitmap image, int numColors)
        {
            // create color image quantization routine
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());

            // create 16 colors table
            Color[] colorTable = ciq.CalculatePalette(image, numColors);
            // create dithering routine
            FloydSteinbergColorDithering dithering = new FloydSteinbergColorDithering();

            dithering.ColorTable = colorTable;
            // apply the dithering routine
            Bitmap newImage = dithering.Apply(image);

            return(newImage);
        }
Пример #6
0
        public static Bitmap OrderedColorDithering(Bitmap bmp, int value)
        {
            // create color image quantization routine
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());

            // create 256 colors table
            Color[] colorTable = ciq.CalculatePalette(bmp, value);
            // create dithering routine
            OrderedColorDithering dithering = new OrderedColorDithering();

            dithering.ColorTable = colorTable;
            // apply the dithering routine
            Bitmap newImage = dithering.Apply(bmp);

            return(newImage);
        }
Пример #7
0
 private void buttonLoadIcon_Click(object sender, EventArgs e)
 {
     using (OpenFileDialog od = new OpenFileDialog()
     {
         DefaultExt = "bmp", Filter = "Bitmap Files|*.bmp;*.png;*.jpg;*.gif|All Files|*.*"
     })
         if (od.ShowDialog(this) == DialogResult.OK)
         {
             Bitmap bitmap = new Bitmap(od.FileName);
             if (bitmap.PixelFormat != PixelFormat.Format4bppIndexed)
             {
                 ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
                 bitmap = ciq.ReduceColors(bitmap, 16);
             }
             meta.Icon = bitmap;
             pictureBoxDLCicon.Image = ScalePreview(meta.Icon, pictureBoxDLCicon, checkBoxZoom.Checked);
         }
 }
 private void ReduceImage()
 {
     if (pictureBoxOriginal.Image != null)
     {
         Image img = new Bitmap(pictureBoxOriginal.Image, new Size((int)numericUpDownWidth.Value, (int)numericUpDownHeight.Value));
         ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
         img = ciq.ReduceColors((Bitmap)img, colorPalette);
         HashSet <Color> colorsUsed = new HashSet <Color>();
         for (int y = 0; y < img.Height; y++)
         {
             for (int x = 0; x < img.Width; x++)
             {
                 colorsUsed.Add(((Bitmap)img).GetPixel(x, y));
             }
         }
         labelColorsCount.Text   = String.Format("Using {0}/{1}/{2} colors", colorsUsed.Count(), colorPalette.Count(), 100);
         pictureBoxReduced.Image = img;
         pictureBoxReduced.Refresh();
     }
 }
Пример #9
0
        public Bitmap Convert(Bitmap source, Size size)
        {
            if (source == null)
            {
                return(null);
            }

            Bitmap result = source.Clone(System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            if (result.Size != size)
            {
                if (size.Width >= result.Width && size.Height >= result.Height)
                {
                    size = result.Size; // no change
                }
                else
                {
                    var ratioWidth  = (double)size.Width / result.Width;
                    var ratioHeight = (double)size.Height / result.Height;

                    var ratio = Math.Min(ratioWidth, ratioHeight);
                    size = new Size((int)(result.Width * ratio), (int)(result.Height * ratio));

                    ResizeBicubic filter = new ResizeBicubic(size.Width, size.Height);
                    result = filter.Apply(result);
                }
            }

            ColorImageQuantizer  colorImageQuantizer = new ColorImageQuantizer(new MedianCutQuantizer());
            BurkesColorDithering dither = new BurkesColorDithering
            {
                ColorTable = palette
            };

            result = dither.Apply(result);

            return(result);
        }
Пример #10
0
        private Boolean colorIsPresent(Bitmap carImg, String color)
        {
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
            ResizeBilinear      rsF = new ResizeBilinear(Convert.ToInt32(carImg.Width * 0.3), Convert.ToInt32(carImg.Height * 0.3));

            pictureBox2.Image = rsF.Apply(carImg);
            Bitmap img = ciq.ReduceColors(rsF.Apply(carImg), 8);

            pictureBox3.Image = img;

            int     lowVal  = 0;
            int     highVal = 360;
            double  s       = 0.15;
            double  lowL    = 0.05;
            double  highL   = 0.95;
            Boolean flag    = false;

            if (!(String.Equals(color, "white", StringComparison.OrdinalIgnoreCase)) && !(String.Equals(color, "black", StringComparison.OrdinalIgnoreCase)) && !(String.Equals(color, "gray", StringComparison.OrdinalIgnoreCase)))
            {
                if (String.Equals(color, "red", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 0;
                    highVal = 20;
                }
                if (String.Equals(color, "orange", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 11;
                    highVal = 50;
                }
                if (String.Equals(color, "brown", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 21;
                    highVal = 40;
                }
                if (String.Equals(color, "yellow", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 41;
                    highVal = 80;
                }
                if (String.Equals(color, "green", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 61;
                    highVal = 169;
                }
                if (String.Equals(color, "cyan", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 141;
                    highVal = 220;
                }
                if (String.Equals(color, "blue", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 201;
                    highVal = 280;
                }
                if (String.Equals(color, "purple", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 241;
                    highVal = 330;
                }
                if (String.Equals(color, "pink", StringComparison.OrdinalIgnoreCase))
                {
                    lowVal  = 321;
                    highVal = 355;
                }

                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        if (img.GetPixel(i, j).GetHue() >= lowVal && img.GetPixel(i, j).GetHue() <= highVal && img.GetPixel(i, j).GetSaturation() > s && img.GetPixel(i, j).GetBrightness() >= lowL && img.GetPixel(i, j).GetBrightness() <= highL)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true)
                    {
                        break;
                    }
                }
            }
            else if (String.Equals(color, "black", StringComparison.OrdinalIgnoreCase))
            {
                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        if (img.GetPixel(i, j).GetBrightness() <= 0.15)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true)
                    {
                        break;
                    }
                }
            }
            else if (String.Equals(color, "white", StringComparison.OrdinalIgnoreCase))
            {
                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        if (img.GetPixel(i, j).GetBrightness() >= 0.85)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true)
                    {
                        break;
                    }
                }
            }
            else if (String.Equals(color, "gray", StringComparison.OrdinalIgnoreCase))
            {
                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        if (img.GetPixel(i, j).R.Equals((img.GetPixel(i, j).G).Equals(img.GetPixel(i, j).B)))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true)
                    {
                        break;
                    }
                }
            }


            return(flag);
        }
Пример #11
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("There was no file specified.");
                filename = "i_eat_ass.png";
            }
            else
            {
                filename = args[0];
            }

            Image  imageFile = Image.FromFile(filename);
            Bitmap bmp       = new Bitmap(imageFile);

            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());

            Color[] colorTable = ciq.CalculatePalette(bmp, 16);
            Dictionary <String, int> colorMap = new Dictionary <String, int>();

            for (int i = 0; i < colorTable.Length; i++)
            {
                try
                {
                    colorMap.Add(colorTable[i].ToString(), i);
                }
                catch (System.ArgumentException e) {
                }
            }


            Bitmap RImage = ciq.ReduceColors(bmp, colorTable);



            int[] flatImage = new int[RImage.Width * RImage.Height];
            for (int x = 0; x < RImage.Width; x++)
            {
                for (int y = 0; y < RImage.Height; y++)
                {
                    flatImage[x + RImage.Width * y] = colorMap[RImage.GetPixel(x, y).ToString()];
                }
            }
            output.AddRange(Encoding.UTF8.GetBytes("YAIF"));
            Add16BitInt(2);
            Add16BitInt(bmp.Width);
            Add16BitInt(bmp.Height);
            Add8BitInt(colorTable.Length * 3);
            Add32BitInt(flatImage.Length / 2);
            output.AddRange(Encoding.UTF8.GetBytes("F**K"));

            for (int i = 0; i < colorTable.Length; i++)
            {
                Add8BitInt(colorTable[i].R);
                Add8BitInt(colorTable[i].G);
                Add8BitInt(colorTable[i].B);
            }

            for (int i = 0; i < flatImage.Length; i += 2)
            {
                AddNibbles(flatImage[i], flatImage[i + 1]);
            }

            byte[] outputArray = output.ToArray();
            File.WriteAllBytes(args[0].Split('.')[0] + ".yaif", outputArray);
        }
Пример #12
0
        public static Bitmap SmoothingFilter(this Bitmap sourceBitmap, MainForm form,
                                             SmoothingFilterType smoothFilter =
                                             SmoothingFilterType.Nenhum)
        {
            Bitmap inputBitmap = null;

            //Progress bar
            form.algorithmProgress = 0;

            switch (smoothFilter)
            {
            case SmoothingFilterType.Nenhum:
            {
                inputBitmap = sourceBitmap;
            } break;

            case SmoothingFilterType.Gaussiano3x3:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Gaussian3x3, 1.0 / 16.0, 0);
            } break;

            case SmoothingFilterType.Gaussiano5x5:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Gaussian5x5, 1.0 / 159.0, 0);
            } break;

            case SmoothingFilterType.Gaussiano7x7:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Gaussian7x7, 1.0 / 136.0, 0);
            } break;

            case SmoothingFilterType.Mediano3x3:
            {
                inputBitmap = sourceBitmap.MedianFilter(3);
            } break;

            case SmoothingFilterType.Mediano5x5:
            {
                inputBitmap = sourceBitmap.MedianFilter(5);
            } break;

            case SmoothingFilterType.Mediano7x7:
            {
                inputBitmap = sourceBitmap.MedianFilter(7);
            } break;

            case SmoothingFilterType.Mediano9x9:
            {
                inputBitmap = sourceBitmap.MedianFilter(9);
            } break;

            case SmoothingFilterType.Mean3x3:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Mean3x3, 1.0 / 9.0, 0);
            } break;

            case SmoothingFilterType.Mean5x5:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Mean5x5, 1.0 / 25.0, 0);
            } break;

            case SmoothingFilterType.LowPass3x3:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.LowPass3x3, 1.0 / 16.0, 0);
            } break;

            case SmoothingFilterType.LowPass5x5:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.LowPass5x5, 1.0 / 60.0, 0);
            } break;

            case SmoothingFilterType.Sharpen3x3:
            {
                inputBitmap = sourceBitmap.ConvolutionFilter(
                    Matrix.Sharpen3x3, 1.0 / 8.0, 0);
            } break;
            }

            //Progress bar
            form.algorithmProgress = 20;


            // START additional filters ADDED BY GABRIEL
            inputBitmap = AForge.Imaging.Image.Clone(inputBitmap, PixelFormat.Format24bppRgb); //Accepted format
            Bilateral           filterB = new Bilateral();
            Grayscale           filterG = new Grayscale(0.2125, 0.7154, 0.0721);               //arbitrary values
            CannyEdgeDetector   filterE = new CannyEdgeDetector();
            ColorImageQuantizer filterC = new ColorImageQuantizer(new MedianCutQuantizer());
            Dilatation          filterD = new Dilatation();

            //Bilateral filter as present in the article
            filterB.KernelSize    = form.kernelValue;
            filterB.SpatialFactor = form.spatialFactor;
            filterB.ColorFactor   = form.colorFactor;
            filterB.ColorPower    = form.colorPower;
            filterB.ApplyInPlace(inputBitmap);

            form.algorithmProgress = 40;

            /* GENERATING BORDERS */
            //Generate a grayscale version for edge detection
            Bitmap edges = filterG.Apply(inputBitmap);

            filterE.HighThreshold = form.highThreshold;
            filterE.LowThreshold  = form.lowThreshold;
            filterE.ApplyInPlace(edges); // a new image, edges, is created here defining the edges of inputBitmap
            //Dilatation filter
            edges = filterD.Apply(edges);
            generateBorder(edges);
            //Making bg transparent
            edges.MakeTransparent(Color.White);


            form.algorithmProgress = 70;


            // Color reduction as present in the article
            inputBitmap = filterC.ReduceColors(inputBitmap, form.colorReductionFactor);         // reduces to 24 variation

            inputBitmap = AForge.Imaging.Image.Clone(inputBitmap, PixelFormat.Format32bppArgb); //Accepted format

            // images merge
            Bitmap   bitmapResult = new Bitmap(inputBitmap.Width, inputBitmap.Height, inputBitmap.PixelFormat);
            Graphics g            = Graphics.FromImage(bitmapResult);

            g.DrawImage(inputBitmap, 0, 0, inputBitmap.Width, inputBitmap.Height);
            g.DrawImage(edges, 0, 0, inputBitmap.Width, inputBitmap.Height);
            // END additional filters ADDED BY GABRIEL

            form.algorithmProgress = 100;

            return(bitmapResult); // it was returning input Bitmap before
        }