Example #1
0
        static void Main(string[] args)
        {
            //. NULL パターン位置
            //. パレット位置
            //. 固定パターン位置

            //. マップ位置
            //. ディザ有無
            //. 色距離 RGB YCbCr 色相優先

            //. パターンファイル名
            //. ネームファイル名

            //. 単純変換 bmp2bmp


            Rectangle        rectNull      = new Rectangle();
            Rectangle        rectPalette   = new Rectangle();
            Rectangle        rectPatterns  = new Rectangle();
            Rectangle        rectNames     = new Rectangle();
            List <Rectangle> listObjects   = new List <Rectangle>();
            string           strDither     = "1x1";
            string           strColorSpace = "RGB";

            string strPatternFileName = null;
            string strNameFileName    = null;
            string strBitmapFileName  = null;

            string strInputFileName = null;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-null":
                    rectNull = new Rectangle(int.Parse(args[i + 1]), int.Parse(args[i + 2]), int.Parse(args[i + 3]), int.Parse(args[i + 4]));
                    i       += 4;
                    break;

                case "-palette":
                    rectPalette = new Rectangle(int.Parse(args[i + 1]), int.Parse(args[i + 2]), int.Parse(args[i + 3]), int.Parse(args[i + 4]));
                    i          += 4;
                    break;

                case "-pattern":
                    rectPatterns = new Rectangle(int.Parse(args[i + 1]), int.Parse(args[i + 2]), int.Parse(args[i + 3]), int.Parse(args[i + 4]));
                    i           += 4;
                    break;

                case "-name":
                    rectNames = new Rectangle(int.Parse(args[i + 1]), int.Parse(args[i + 2]), int.Parse(args[i + 3]), int.Parse(args[i + 4]));
                    i        += 4;
                    break;

                case "-dither":
                    strDither = args[i + 1];
                    i++;
                    break;

                case "-color":
                    strColorSpace = args[i + 1];
                    i++;
                    break;

                case "-patternout":
                    strPatternFileName = args[i + 1];
                    i++;
                    break;

                case "-nameout":
                    strNameFileName = args[i + 1];
                    i++;
                    break;

                case "-bitmapout":
                    strBitmapFileName = args[i + 1];
                    i++;
                    break;

                default:
                    strInputFileName = args[i];
                    break;
                }
            }

            Bitmap bitmapSource = new Bitmap(strInputFileName);

            //. パンチスルーイメージ.
            Bitmap bitmapNull = null;

            if (rectNull.Width != 0 && rectNull.Height != 0)
            {
                bitmapNull = new Bitmap(8, 8);
                for (int y = 0; y < 8; y++)
                {
                    for (int x = 0; x < 8; x++)
                    {
                        bitmapNull.SetPixel(x, y, bitmapSource.GetPixel(rectNull.X + x, rectNull.Y + y));
                    }
                }
            }

            //. パレット作成.
            Color[] colors = s_colors;
            if (rectPalette.Width != 0 && rectPalette.Height != 0)
            {
                List <Color> listColors = new List <Color>();
                for (int y = 0; y < rectPalette.Height; y += 8)
                {
                    for (int x = 0; x < rectPalette.Width; x += 8)
                    {
                        listColors.Add(bitmapSource.GetPixel(rectPalette.X + x, rectPalette.Y + y));
                    }
                }

                colors = listColors.ToArray();
            }


            ColorList colorList = new ColorList(colors);

            ColorSpace colorSpace = new ColorSpaceRgb();

            if (strColorSpace == "RGB")
            {
                colorSpace = new ColorSpaceRgb();
            }
            else if (strColorSpace == "YCbCr")
            {
                colorSpace = new ColorSpaceYCbCr();
            }

            Dither dither = new Dither1x1();

            if (strDither == "1x1")
            {
                dither = new Dither1x1();
            }
            else if (strDither == "2x2")
            {
                dither = new Dither2x2();
            }
            else if (strDither == "4x4")
            {
                dither = new Dither4x4();
            }

            IndexedImage indexedImage = new IndexedImage(colorList, bitmapSource, colorSpace, dither);
            Bitmap       bitmap       = indexedImage.CreateBitmap();

            bitmap.Save("temp.png");

            ColorSpaceRgb colorSpaceRgb = new ColorSpaceRgb();
            Graphic2Image graphic2Image = new Graphic2Image(indexedImage, colorSpaceRgb);

            Bitmap bitmapGraphic2 = graphic2Image.CreateBitmap();

            if (strBitmapFileName != null)
            {
                bitmapGraphic2.Save(strBitmapFileName);
            }


            if (strNameFileName != null && strPatternFileName != null)
            {
                CellGraphic2Image cellImage  = new CellGraphic2Image(bitmapGraphic2);
                Bitmap            bitmapCell = cellImage.CreateBitmap();
                bitmapCell.Save("cell.png");                //, System.Drawing.Imaging.ImageFormat.Png);
            }

            if (strPatternFileName != null)
            {
                if (rectPatterns.Width <= 0 || rectPatterns.Height <= 0)
                {
                    rectPatterns = new Rectangle(0, 0, graphic2Image.Width, graphic2Image.Height);
                }

                graphic2Image.OutputPatterns(strPatternFileName, rectPatterns);
            }
        }
Example #2
0
        public Graphic2Image(IndexedImage indexedImage, ColorSpace colorSpace)
        {
            m_colorList = indexedImage.ColorList;
            Width       = indexedImage.Width;
            Height      = indexedImage.Height;

            Pixels           = new int[Width * Height];
            ForegroundColors = new int[Width * Height];
            BackgroundColors = new int[Width * Height];

            for (int y = 0; y < Height; y++)
            {
                if (y % 10 == 0)
                {
                    Console.WriteLine("converting to graphic2 ... {0}/{1}", y, Height);
                }

                for (int x = 0; x < Width; x += 8)
                {
                    //. ユニークカラーの取得.
                    List <int> listIndices = new List <int>();
                    for (int i = 0; i < 8; i++)
                    {
                        if (x + i >= Width)
                        {
                            continue;
                        }

                        int index = indexedImage.GetIndex(x + i, y);
                        if (listIndices.Contains(index) == false)
                        {
                            listIndices.Add(index);
                        }
                    }

                    if (listIndices.Count <= 1)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (x + i >= Width)
                            {
                                continue;
                            }
                            SetIndex(x + i, y, listIndices[0], listIndices[0], 0);
                        }
                    }
                    else
                    {
                        List <ColorDistance> listColorDistance = new List <ColorDistance>();
                        ColorSpaceRgb        colorSpaceRgb = new ColorSpaceRgb();
                        Color color0, color1;
                        for (int i = 0; i < listIndices.Count - 1; i++)
                        {
                            for (int j = i + 1; j < listIndices.Count; j++)
                            {
                                color0 = indexedImage.ColorList.Colors[listIndices[i]];
                                color1 = indexedImage.ColorList.Colors[listIndices[j]];
                                listColorDistance.Add(new ColorDistance(listIndices[i], listIndices[j], colorSpaceRgb.Distance(color0, color1)));
                            }
                        }

                        ColorDistance[] arrayColorDistance = listColorDistance.ToArray();
                        Array.Sort(arrayColorDistance);

                        int index0 = arrayColorDistance[0].Index0;
                        int index1 = arrayColorDistance[0].Index1;
                        color0 = indexedImage.ColorList.Colors[index0];
                        color1 = indexedImage.ColorList.Colors[index1];

                        for (int i = 0; i < 8; i++)
                        {
                            if (x + i >= Width)
                            {
                                continue;
                            }

                            Color color = indexedImage.GetPixel(x + i, y);
                            float d0    = colorSpaceRgb.Distance(color0, color);
                            float d1    = colorSpaceRgb.Distance(color1, color);

                            SetIndex(x + i, y, d0 < d1 ? index0 : index1, index0, index1);
                        }
                    }



                    /*
                     * //. 多い色2つ取ってくる.
                     * Dictionary<int, int> dicColors = new Dictionary<int, int>();
                     *
                     * for (int i = 0; i < 8; i++)
                     * {
                     *      if ((x + i) >= Width)
                     *      {
                     *              continue;
                     *      }
                     *
                     *      int color = indexedImage.GetIndex(x + i, y);
                     *      if (dicColors.ContainsKey(color))
                     *      {
                     *              dicColors[color] += 1;
                     *      }
                     *      else
                     *      {
                     *              dicColors.Add(color, 1);
                     *      }
                     * }
                     *
                     * if (dicColors.Keys.Count == 1)
                     * {
                     *      for (int i = 0; i < 8; i++)
                     *      {
                     *              if (x + i >= Width)
                     *              {
                     *                      continue;
                     *              }
                     *
                     *              SetIndex( x+ i , y, dicColors.Keys.ElementAt(0), dicColors.Keys.ElementAt(0), 0 );
                     *      }
                     * }
                     * else
                     * {
                     *      ColorCount[] colors = new ColorCount[dicColors.Keys.Count];
                     *      for (int i = 0; i < colors.Length; i++)
                     *      {
                     *              colors[i] = new ColorCount(dicColors.Keys.ElementAt(i), dicColors.Values.ElementAt(i));
                     *      }
                     *
                     *      Array.Sort(colors);
                     *
                     *      for (int i = 0; i < 8; i++)
                     *      {
                     *              if (x + i >= Width)
                     *              {
                     *                      continue;
                     *              }
                     *
                     *              Color color = indexedImage.GetPixel(x + i, y);
                     *              float d0 = colorSpace.Distance( m_colorList.GetColor(colors[0].Color), color);
                     *              float d1 = colorSpace.Distance( m_colorList.GetColor(colors[1].Color), color);
                     *
                     *              SetIndex(x + i, y, d0 < d1 ? colors[0].Color : colors[1].Color, colors[0].Color, colors[1].Color);
                     *      }
                     * }
                     */
                }
            }
        }