//using ColorMine.ColorSpaces;
//using System;
//using System.Collections.Generic;
//using System.Drawing;
//using System.IO;
//using System.Linq;
//using System.Runtime.Serialization;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Text;
//using System.Threading.Tasks;
//using System.Web.Script.Serialization;

//namespace C64VideoConverter
//{
//    class Program
//    {

//        static object[] allColorsArray = new object[256];
//        static Lab[] c64Colors = new Lab[16];

//        static void Main(string[] args)
//        {

//            Console.Out.WriteLine("Deserializing C64 colors");

//            string jsonC64 = File.ReadAllText("Color\\C64Colors.lab");
//            c64Colors = new JavaScriptSerializer().Deserialize<Lab[]>(jsonC64);

//            Console.Out.WriteLine("C64 colors deserialized");


//            Console.Out.WriteLine("Deserializing RGB->LAB information");

//            for (int R = 0; R < 256; R++)
//            {
//                IFormatter formatter = new BinaryFormatter();
//                Stream stream = new FileStream(String.Format("Color\\MyFile{0}.bin", R), FileMode.Open, FileAccess.Read, FileShare.Read);
//                Lab[] obj = (Lab[])formatter.Deserialize(stream);
//                allColorsArray[R] = obj;
//                stream.Close();

//                Console.Out.WriteLine(String.Format("{0} finished", R));
//            }

//            Console.Out.WriteLine("RGB->LAB information deserialized");




//            Console.In.ReadLine();

//            string filePath = args[0];

//            if (args[0] == "a")
//            {
//                filePath = args[1];
//                Bins2Images(filePath);
//            }
//            else if (args[0] == "d")
//            {
//                filePath = args[1];
//                string outputFilePath = args[2];
//                int count = Convert.ToInt32(args[3]);
//                ProcessDupImages(filePath, outputFilePath, count);
//            }
//            else if (args[0] == "g")
//            {
//                filePath = args[1];
//                GeneratePattern(filePath);
//            }
//            else
//            {
//                if (args.Length > 2)
//                {
//                    string outputFile = args[1];
//                    int skip = Convert.ToInt32(args[2]);
//                    ProcessImages(filePath, "Scene", outputFile, skip);
//                }
//                else if (args.Length > 1)
//                {
//                    string outputFile = args[1];
//                    ProcessImage(filePath, outputFile);
//                }
//                else
//                {
//                    Bin2Image(filePath);
//                }

//            }
//        }

//        private static void GeneratePattern(string filePath)
//        {
//            FileStream fileStream = File.OpenWrite(filePath);

//            ulong f = 0;
//            ulong bPrev = 0;
//            ulong b = 1;
//            int i = 0;
//            while (i < 240)
//            {
//                f = b + bPrev;
//                bPrev = b;
//                b = f;

//                fileStream.WriteByte(Convert.ToByte((f >> 56) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 48) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 40) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 32) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 24) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 16) & 255));
//                fileStream.WriteByte(Convert.ToByte((f >> 8) & 255));
//                fileStream.WriteByte(Convert.ToByte(f & 255));
//                i = i + 1;
//            }

//            fileStream.Close();
//        }

//        /*
//        static void Bin2Image(string filePath)
//        {
//            byte[] img = File.ReadAllBytes(filePath);
//            int currentByte = 0;
//            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
//            int[] shiftValue = { 6, 4, 2, 0 };

//            Bitmap flag = new Bitmap(160, 96);
//            for (int y = 0; y < flag.Height; y++)
//            {
//                for (int x = 0; x < flag.Width; x++)
//                {
//                    int mask = pixelMask[x % 4];
//                    int shift = shiftValue[x % 4];
//                    int color = (img[currentByte] & mask) >> shift;
//                    if (color == 0) flag.SetPixel(x, y, Color.Black);
//                    if (color == 1) flag.SetPixel(x, y, Color.DarkGray);
//                    if (color == 2) flag.SetPixel(x, y, Color.LightGray);
//                    if (color == 3) flag.SetPixel(x, y, Color.White);
//                    if (x % 4 == 3) currentByte++;
//                }                    
//            }

//            flag.Save(filePath + ".bmp");
//        }

//        */

//        static void Bins2Images(string filePath)
//        {
//            byte[] imgsAll = File.ReadAllBytes(filePath);
//            byte[] img = new byte[3840];
//            for (long index = 0; index < imgsAll.Length; index = index + 3840)
//            {
//                Array.Copy(imgsAll, index, img, 0, 960);
//                Bitmap bm = Bin2ImageFB(img);
//                bm.Save("out\\scene" + index + ".png");
//            }
//        }

//        static void Bin2Image(string filePath)
//        {
//            byte[] img = File.ReadAllBytes(filePath);

//            Bitmap flag = Bin2ImageFB(img);
//            flag.Save(filePath + ".png");
//        }

//        static Bitmap Bin2ImageFB(byte[] img)
//        {
//            Bitmap flag = new Bitmap(80, 48);
//            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
//            int[] shiftValue = { 6, 4, 2, 0 };

//            for (int y = 0; y < flag.Height; y++)
//            {
//                int row = (y / 8);
//                for (int x = 0; x < flag.Width; x++)
//                {
//                    int column = (x / 4);
//                    int currentByte = row * 320 + (y % 8) + column * 8;
//                    int mask = pixelMask[x % 4];
//                    int shift = shiftValue[x % 4];
//                    int color = (img[currentByte] & mask) >> shift;
//                    if (color == 0) flag.SetPixel(x, y, Color.Black);
//                    if (color == 1) flag.SetPixel(x, y, Color.DarkGray);
//                    if (color == 2) flag.SetPixel(x, y, Color.LightGray);
//                    if (color == 3) flag.SetPixel(x, y, Color.White);
//                }
//            }

//            return flag;
//        }

//        static void ProcessDupImages(string inputFile, string outputFile, int count)
//        {
//            FileStream fileStream = File.OpenWrite(outputFile);
//            Bitmap bitmap = new Bitmap(inputFile);
//            byte[] converted = ConvertBitmapActual(bitmap);

//            for (int i = 0; i < count; i++)
//            {
//                fileStream.Write(converted, 0, converted.Length);
//            }

//            fileStream.Close();
//        }


//        static void ProcessImages(string filePath, string filePrefix, string outputFile, int skip)
//        {
//            FileStream fileStream = File.OpenWrite(outputFile);

//            int i = 1;

//            while (true)
//            {
//                string currentFileName = String.Format("{0}{1}.bmp", filePrefix, i.ToString().PadLeft(5, '0'));
//                string fullFilePath = String.Format("{0}\\{1}", filePath, currentFileName);
//                Console.Out.WriteLine("Processing : " + fullFilePath);
//                try
//                {
//                    Bitmap bitmap = new Bitmap(fullFilePath);
//                    byte[] converted = ConvertBitmapActual(bitmap);
//                    fileStream.Write(converted, 0, converted.Length);
//                }
//                catch (Exception ex)
//                {
//                    break;
//                }
//                i = i + skip;
//            }
//        }

//        static void ProcessImage(string filePath, string outputFile)
//        {

//            Console.Out.WriteLine("Processing : " + filePath);
//            FileStream fileStream = File.OpenWrite(outputFile);

//            Bitmap bitmap = new Bitmap(filePath);
//            byte[] converted = ConvertBitmapActual(bitmap);
//            fileStream.Write(converted, 0, converted.Length);
//            fileStream.Close();
//        }

//        //const int FrameSize = (160 * 100) / 4;
//        //const int FrameSize = (320 * 12);
//        const int FrameSize = 960;

//        /*
//        const float black = 0;
//        const float dark_gray = 10/32;
//        const float light_gray = 20/32;
//        const float white = 32/32;
//        */


//        const float black = 0;
//        const float dark_gray = 8.0f / 32;
//        const float light_gray = 16.0f / 32;
//        const float white = 24.0f / 32;



//        static byte[] ConvertBitmapActual(Bitmap bitmap)
//        {
//            byte[] generated = new byte[FrameSize];
//            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
//            int[] shiftValue = { 6, 4, 2, 0 };


//            for (int y = 0; y < 48; y++)
//            {
//                int row = (y / 8);
//                for (int x = 0; x < 80; x++)
//                {
//                    int column = (x / 4);
//                    int color = 0;
//                    Color clr = bitmap.GetPixel(x, y);

//                    float brightness = clr.GetBrightness();

//                    if (brightness < dark_gray)
//                    {
//                        color = 0;
//                    }
//                    else if (brightness >= dark_gray && brightness < light_gray)
//                    {
//                        color = 1;
//                    }
//                    else if (brightness >= light_gray && brightness < white)
//                    {
//                        color = 2;
//                    }
//                    else
//                    {
//                        color = 3;
//                    }

//                    int currentByte = row * 160 + (y % 8) + column * 8;
//                    int mask = pixelMask[x % 4];
//                    int shift = shiftValue[x % 4];
//                    generated[currentByte] = Convert.ToByte(generated[currentByte] | Convert.ToByte(mask & (color << shift)));
//                }
//            }

//            return generated;
//        }

//    }
//}



////using System;
////using System.Collections.Generic;
////using System.Drawing;
////using System.IO;
////using System.Linq;
////using System.Text;
////using System.Threading.Tasks;

////namespace C64VideoConverter
////{
////    class Program
////    {
////        static void Main(string[] args)
////        {
////            string filePath = args[0];

////            if (args[0] == "a")
////            {
////                filePath = args[1];
////                Bins2Images(filePath);
////            }
////            else if (args[0] == "d")
////            {
////                filePath = args[1];
////                string outputFilePath = args[2];
////                int count = Convert.ToInt32(args[3]);
////                ProcessDupImages(filePath, outputFilePath, count);
////            }
////            else if (args[0] == "g")
////            {
////                filePath = args[1];
////                GeneratePattern(filePath);
////            }
////            else
////            {
////                if (args.Length > 2)
////                {
////                    string outputFile = args[1];
////                    int skip = Convert.ToInt32(args[2]);
////                    ProcessImages(filePath, "Scene", outputFile, skip);
////                }
////                else if (args.Length > 1)
////                {
////                    string outputFile = args[1];
////                    ProcessImage(filePath, outputFile);
////                }
////                else
////                {
////                    Bin2Image(filePath);
////                }

////            }
////        }

////        private static void GeneratePattern(string filePath)
////        {
////            FileStream fileStream = File.OpenWrite(filePath);

////            ulong f = 0;
////            ulong bPrev = 0;
////            ulong b = 1;
////            int i = 0;
////            while (i < 240)
////            {
////                f = b + bPrev;
////                bPrev = b;
////                b = f;

////                fileStream.WriteByte(Convert.ToByte((f >> 56) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 48) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 40) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 32) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 24) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 16) & 255));
////                fileStream.WriteByte(Convert.ToByte((f >> 8) & 255));
////                fileStream.WriteByte(Convert.ToByte(f & 255));
////                i = i + 1;
////            }

////            fileStream.Close();
////        }

////        /*
////        static void Bin2Image(string filePath)
////        {
////            byte[] img = File.ReadAllBytes(filePath);
////            int currentByte = 0;
////            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
////            int[] shiftValue = { 6, 4, 2, 0 };

////            Bitmap flag = new Bitmap(160, 96);
////            for (int y = 0; y < flag.Height; y++)
////            {
////                for (int x = 0; x < flag.Width; x++)
////                {
////                    int mask = pixelMask[x % 4];
////                    int shift = shiftValue[x % 4];
////                    int color = (img[currentByte] & mask) >> shift;
////                    if (color == 0) flag.SetPixel(x, y, Color.Black);
////                    if (color == 1) flag.SetPixel(x, y, Color.DarkGray);
////                    if (color == 2) flag.SetPixel(x, y, Color.LightGray);
////                    if (color == 3) flag.SetPixel(x, y, Color.White);
////                    if (x % 4 == 3) currentByte++;
////                }                    
////            }

////            flag.Save(filePath + ".bmp");
////        }

////        */

////        static void Bins2Images(string filePath)
////        {
////            byte[] imgsAll = File.ReadAllBytes(filePath);
////            byte[] img = new byte[3840];
////            for (long index = 0; index < imgsAll.Length; index = index + 3840)
////            {
////                Array.Copy(imgsAll, index, img, 0, 3840);
////                Bitmap bm = Bin2ImageFB(img);
////                bm.Save("out\\scene" + index + ".png");
////            }
////        }

////        static void Bin2Image(string filePath)
////        {
////            byte[] img = File.ReadAllBytes(filePath);

////            Bitmap flag = Bin2ImageFB(img);
////            flag.Save(filePath + ".png");
////        }

////        static Bitmap Bin2ImageFB(byte[] img)
////        {
////            Bitmap flag = new Bitmap(160, 96);
////            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
////            int[] shiftValue = { 6, 4, 2, 0 };

////            for (int y = 0; y < flag.Height; y++)
////            {
////                int row = (y / 8);
////                for (int x = 0; x < flag.Width; x++)
////                {
////                    int column = (x / 4);
////                    int currentByte = row * 320 + (y % 8) + column * 8;
////                    int mask = pixelMask[x % 4];
////                    int shift = shiftValue[x % 4];
////                    int color = (img[currentByte] & mask) >> shift;
////                    if (color == 0) flag.SetPixel(x, y, Color.Black);
////                    if (color == 1) flag.SetPixel(x, y, Color.DarkGray);
////                    if (color == 2) flag.SetPixel(x, y, Color.LightGray);
////                    if (color == 3) flag.SetPixel(x, y, Color.White);
////                }
////            }

////            return flag;
////        }

////        static void ProcessDupImages(string inputFile, string outputFile, int count)
////        {
////            FileStream fileStream = File.OpenWrite(outputFile);
////            Bitmap bitmap = new Bitmap(inputFile);
////            byte[] converted = ConvertBitmapActual(bitmap);

////            for (int i = 0; i < count; i++)
////            {
////                fileStream.Write(converted, 0, converted.Length);
////            }

////            fileStream.Close();
////        }


////        static void ProcessImages(string filePath, string filePrefix, string outputFile, int skip)
////        {
////            FileStream fileStream = File.OpenWrite(outputFile);

////            int i = 1;

////            while (true)
////            {
////                string currentFileName = String.Format("{0}{1}.png", filePrefix, i.ToString().PadLeft(5, '0'));
////                string fullFilePath = String.Format("{0}\\{1}", filePath, currentFileName);
////                Console.Out.WriteLine("Processing : " + fullFilePath);
////                try
////                {
////                    Bitmap bitmap = new Bitmap(fullFilePath);
////                    byte[] converted = ConvertBitmapActual(bitmap);
////                    fileStream.Write(converted, 0, converted.Length);
////                }
////                catch (Exception ex)
////                {
////                    break;
////                }
////                i = i + skip;
////            }
////        }

////        static void ProcessImage(string filePath, string outputFile)
////        {

////            Console.Out.WriteLine("Processing : " + filePath);
////            FileStream fileStream = File.OpenWrite(outputFile);

////            Bitmap bitmap = new Bitmap(filePath);
////            byte[] converted = ConvertBitmapActual(bitmap);
////            fileStream.Write(converted, 0, converted.Length);
////            fileStream.Close();
////        }

////        //const int FrameSize = (160 * 100) / 4;
////        const int FrameSize = (320 * 12);

////        /*
////        const float black = 0;
////        const float dark_gray = 10/32;
////        const float light_gray = 20/32;
////        const float white = 32/32;
////        */


////        const float black = 0;
////        const float dark_gray = 8.0f / 32;
////        const float light_gray = 16.0f / 32;
////        const float white = 24.0f / 32;



////        static byte[] ConvertBitmapActual(Bitmap bitmap)
////        {
////            byte[] generated = new byte[FrameSize];
////            int[] pixelMask = { 0xC0, 0x30, 0x0C, 0x03 };
////            int[] shiftValue = { 6, 4, 2, 0 };


////            for (int y = 0; y < 96; y++)
////            {
////                int row = (y / 8);
////                for (int x = 0; x < 160; x++)
////                {
////                    int column = (x / 4);
////                    int color = 0;
////                    Color clr = bitmap.GetPixel(x, y);

////                    float brightness = clr.GetBrightness();

////                    if (brightness < dark_gray)
////                    {
////                        color = 0;
////                    }
////                    else if (brightness >= dark_gray && brightness < light_gray)
////                    {
////                        color = 1;
////                    }
////                    else if (brightness >= light_gray && brightness < white)
////                    {
////                        color = 2;
////                    }
////                    else
////                    {
////                        color = 3;
////                    }

////                    int currentByte = row * 320 + (y % 8) + column * 8;
////                    int mask = pixelMask[x % 4];
////                    int shift = shiftValue[x % 4];
////                    generated[currentByte] = Convert.ToByte(generated[currentByte] | Convert.ToByte(mask & (color << shift)));
////                }
////            }

////            return generated;
////        }

////    }
////}

