Example #1
0
        public static AskGetDitherResult AskGetDithering()
        {
            string input;
            int    index = -2;

            AskGetDitherResult result = new AskGetDitherResult();

            while (true)
            {
                Console.WriteLine("\nwould you like to dither the image? (y/n)");
                input = Console.ReadLine().Trim().ToLower();

                if (input == "n")
                {
                    return(result);
                }

                if (input == "y")
                {
                    break;
                }
            }

            while (true)
            {
                Console.WriteLine("\nplease input the number of the pixel transform you'd like to use (-1 to cancel)");
                Console.WriteLine(
                    "\t0 : Monochrome\n\t" +
                    "1 : Color 232\n\t"
                    );
                input = Console.ReadLine().Trim().ToLower();

                if (int.TryParse(input, out index))
                {
                    if (index == -1)
                    {
                        return(result);
                    }

                    if (index == 0)
                    {
                        byte i = 128;
                        while (true)
                        {
                            Console.WriteLine("input the color threshold (0 - 255)");
                            input = Console.ReadLine().Trim().ToLower();

                            if (byte.TryParse(input, out i))
                            {
                                i = ((int)i).ToByte();
                                break;
                            }
                        }

                        result.Threshold = i;
                        result.Transform = GetPixelTransform(1, i);
                        break;
                    }

                    if (index > 0 && index < 4)
                    {
                        result.Transform = GetPixelTransform(index.ToByte(), 0);
                        break;
                    }
                }
            }

            while (true)
            {
                Console.WriteLine("\nplease input the number of the dithering algorithm you'd like to use (-1 to cancel)");
                Console.WriteLine(
                    "\t0 : Floyd Steinberg\n\t" +
                    "1 : Burks\n\t" +
                    "2 : Jarvis Judice Ninke\n\t" +
                    "3 : Stucki\n\t" +
                    "4 : Sierra 3\n\t" +
                    "5 : Sierra 2\n\t" +
                    "6 : Sierra Lite\n\t" +
                    "7 : Atkinson\n\t" +
                    "8 : Random\n\t" +
                    "9 : Bayer 2\n\t" +
                    "10 : Bayer 3\n\t" +
                    "11 : Bayer 4\n\t" +
                    "12 : Bayer 8\n"
                    );
                input = Console.ReadLine().ToLower().Trim();

                if (int.TryParse(input, out index))
                {
                    if (index == -1)
                    {
                        return(result);
                    }

                    if (index < 13 && index > -1)
                    {
                        result.DitherAlgorithm = GetDitheringInstance(index.ToByte());
                        break;
                    }
                }
            }

            return(result);
        }
Example #2
0
        static void Main(string[] args)
        {
            string             fileName, outName;
            bool               createScanFile   = false;
            Size               autoSizeOutImage = Size.Empty;
            AskGetDitherResult dither           = new AskGetDitherResult();

            if (args.Length > 0)
            {
                fileName = args[0];
                if (File.Exists(fileName))
                {
                    outName = Path.GetFileNameWithoutExtension(fileName) + "_convert.png";
                }
                else
                {
                    fileName = GetFileName();
                    outName  = Path.GetFileNameWithoutExtension(fileName) + "_convert.png";
                }

                switch (args.Length)
                {
                case 2:
                    autoSizeOutImage = ParseSize(args[1]);
                    break;

                case 3:
                    int w = -1, h = -1;

                    autoSizeOutImage = new Size(-1, -1);
                    if (int.TryParse(args[1], out w))
                    {
                        autoSizeOutImage.Width = w;
                    }
                    if (int.TryParse(args[2], out h))
                    {
                        autoSizeOutImage.Height = h;
                    }

                    if (autoSizeOutImage.Width == -1 || autoSizeOutImage.Height == -1)
                    {
                        autoSizeOutImage = Size.Empty;
                        break;
                    }
                    break;
                }
            }
            else
            {
                fileName = GetFileName();
                outName  = Path.GetFileNameWithoutExtension(fileName) + "_convert.png";
                Console.WriteLine("");
                autoSizeOutImage = AskGetSize();
                Console.WriteLine("");
                dither         = AskGetDithering();
                createScanFile = AskCreateScanFile();
            }

            Bitmap data = LoadImage(fileName);

            if (data == null)
            {
                Console.WriteLine("there was an error loading image, program will exit");
                Console.ReadLine();
                return;
            }

            if (autoSizeOutImage != Size.Empty)
            {
                Bitmap newScaled = new Bitmap(autoSizeOutImage.Width, autoSizeOutImage.Height);

                using (Graphics g = Graphics.FromImage(newScaled))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(data, new Rectangle(0, 0, autoSizeOutImage.Width, autoSizeOutImage.Height), new Rectangle(0, 0, data.Width, data.Height), GraphicsUnit.Pixel);
                }

                data.Dispose();
                data = newScaled;
            }



            Console.WriteLine("\nconverting...");

            // user wants dithering
            if (dither.DitherAlgorithm != null)
            {
                // request image tansform disposes of the input image so we don't need to
                // do anythign special to dispose of the unused data
                using (Bitmap bmp = DitherHelper.RequestImageTransform(data, dither.Transform, dither.DitherAlgorithm))
                {
                    if (createScanFile)
                    {
                        CreateScanFile(bmp, outName);
                    }
                    bmp.Save(outName, ImageFormat.Png);
                }
            }
            else
            {
                Color  color;
                Bitmap newBitmap = new Bitmap(data.Width, data.Height);

                using (Graphics g = Graphics.FromImage(newBitmap))
                {
                    for (int x = 0; x < data.Width; x++)
                    {
                        for (int y = 0; y < data.Height; y++)
                        {
                            color = data.GetPixel(x, y);
                            newBitmap.SetPixel(x, y, dye[closestColor2(dye, color)]);
                        }
                    }

                    g.DrawImage(newBitmap, new Point(0, 0));
                }

                if (createScanFile)
                {
                    CreateScanFile(newBitmap, outName);
                }
                newBitmap.Save(outName, ImageFormat.Png);
                data.Dispose();
            }

            Console.WriteLine("done.\n");
            Console.WriteLine(outName);
            //Console.ReadLine();
        }