Exemplo n.º 1
0
    public static Bitmap DitteringImage(IErrorDiffusion diffusion, Bitmap bitmap)
    {
        List <ArgbColor> colorList = new List <ArgbColor>();
        int height = bitmap.Height;
        int width  = bitmap.Width;

        for (int h = 0; h < height; h++)
        {
            for (int w = 0; w < width; w++)
            {
                var pixel = bitmap.GetPixel(w, h);
                colorList.Add(new ArgbColor(pixel.A, pixel.R, pixel.G, pixel.B));
            }
        }

        ArgbColor[] colorArray = colorList.ToArray();

        for (int h = 0; h < height; h++)
        {
            for (int w = 0; w < width; w++)
            {
                var argb = colorArray[h * width + w];
                var bw   = ToBWPixel(argb);
                colorArray[h * width + w] = bw;

                diffusion?.Diffuse(colorArray, argb, bw, w, h, width, height);
            }
        }

        Bitmap newbm = new Bitmap(bitmap);

        for (int h = 0; h < height; h++)
        {
            for (int w = 0; w < width; w++)
            {
                var   argb  = colorArray[h * width + w];
                Color pixel = Color.FromArgb(argb.A, argb.R, argb.G, argb.B);
                newbm.SetPixel(w, h, pixel);
            }
        }

        return(newbm);
    }
Exemplo n.º 2
0
        public static Bitmap RequestImageTransform(Bitmap image, IPixelTransform transform, IErrorDiffusion ditherer)
        {
            WorkerData workerData;

            if (image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            {
                using (Bitmap tmp = image)
                {
                    image = tmp.Copy();
                }
            }

            workerData = new WorkerData
            {
                Image     = image,
                Transform = transform,
                Dither    = ditherer
            };

            return(backgroundWorker_RunWorkerCompleted(null, new RunWorkerCompletedEventArgs(GetTransformedImage(workerData), null, false)));
        }
Exemplo n.º 3
0
        public static void ProcessPixels(ArgbColor[] pixelData, Size size, IPixelTransform pixelTransform, IErrorDiffusion dither)
        {
            ArgbColor current;
            ArgbColor transformed;
            int       index;

            for (int row = 0; row < size.Height; row++)
            {
                for (int col = 0; col < size.Width; col++)
                {
                    index   = row * size.Width + col;
                    current = pixelData[index];

                    // transform the pixel
                    if (pixelTransform != null)
                    {
                        transformed      = pixelTransform.Transform(pixelData, current, col, row, size.Width, size.Height);
                        pixelData[index] = transformed;
                    }
                    else
                    {
                        transformed = current;
                    }

                    // apply a dither algorithm to this pixel
                    // assuming it wasn't done before
                    dither?.Diffuse(pixelData, current, transformed, col, row, size.Width, size.Height);
                }
            }
        }
Exemplo n.º 4
0
        public static void ProcessPixels(Color[] pixelData, Size size, IPixelTransform pixelTransform, IErrorDiffusion dither, WorkerData bw = null)
        {
            Color current;
            Color transformed;
            int   index = 0;

            for (int row = 0; row < size.Height; row++)
            {
                for (int col = 0; col < size.Width; col++)
                {
                    if (bw != null && bw.Worker.CancellationPending == true)
                    {
                        bw.Args.Cancel = true;
                        return;
                    }

                    current = pixelData[index];

                    if (pixelTransform != null)
                    {
                        transformed      = pixelTransform.Transform(current);
                        pixelData[index] = transformed;
                    }
                    else
                    {
                        transformed = current;
                    }
                    index++;

                    // apply a dither algorithm to this pixel
                    // assuming it wasn't done before
                    dither?.Diffuse(pixelData, current, transformed, col, row, size.Width, size.Height);
                }
            }
        }