Beispiel #1
0
        private void updateBitmapPreview()
        {
            if (checkGroupBoxFontImage.Checked && m_currentLoadedBitmap != null)
            {
                int w            = (int)numericUpDownInputImageTileSizeX.Value;
                int h            = (int)numericUpDownInputImageTileSizeY.Value;
                int tilesPerLine = (int)numericUpDownInputImageTilesPerLine.Value;

                Point p = new Point(hScrollBarInputImageCharacterPos.Value % tilesPerLine * w, hScrollBarInputImageCharacterPos.Value / tilesPerLine * h);

                Rectangle r = new Rectangle(p, new Size(w, h));
                Color     c;
                if (colorList.ContainsValue(true))
                {
                    c = MyExtensions.GetEnabledKeys(colorList)[0];
                }
                else
                {
                    c = Color.Transparent;
                }
                Bitmap character = MyExtensions.Clone(m_currentLoadedBitmap, r, PixelFormat.Format32bppArgb, c);;

                Size newSize = r.Size;
                int  faktor  = 1;

                while (newSize.Height <= 32)
                {
                    newSize.Height *= 2;
                    newSize.Width  *= 2;
                    faktor         *= 2;
                }

                //convert to black white image
                character = MyExtensions.ToBitmap(MyExtensions.ToArgbArray(character).Select(argb =>
                {
                    return(colorList[Color.FromArgb(argb)] ? Color.Black.ToArgb() : Color.White.ToArgb());
                }).ToArray(), r.Size);

                pictureBoxInputImageFontCharacterPreview.Image = MyExtensions.ResizeImage(character, faktor);
                pictureBoxInputImageFontCharacterPreview.Size  = newSize;
            }
        }
Beispiel #2
0
        public static Border GetBorders(Bitmap bmp, Color[] borderColorList)
        {
            int[]  pixel = MyExtensions.ToArgbArray(bmp);
            Border b = new Border();
            int    width = bmp.Width, height = bmp.Height;

            int[] borderColorListInt = borderColorList.Select <Color, int>(p => p.ToArgb()).ToArray();

            Func <int, int, int> getPixel = delegate(int x, int y)
            {
                return(pixel[y * width + x]);
            };

            // returns whether a bitmap column is empty (empty means all is border color)
            Func <int, bool> columnIsEmpty = delegate(int column)
            {
                // for each row in the column
                for (int row = 0; row < height; ++row)
                {
                    // is the pixel black?
                    if (!borderColorListInt.Contains(getPixel(column, row)))
                    {
                        // found. column is not empty
                        return(false);
                    }
                }

                // column is empty
                return(true);
            };

            // returns whether a bitmap row is empty (empty means all is border color)
            Func <int, bool> rowIsEmpty = delegate(int row)
            {
                // for each column in the row
                for (int column = 0; column < width; ++column)
                {
                    // is the pixel black?
                    if (!borderColorListInt.Contains(getPixel(column, row)))
                    {
                        // found. row is not empty
                        return(false);
                    }
                }

                // row is empty
                return(true);
            };

            for (b.Left = 0; b.Left < width; ++b.Left)
            {
                if (!columnIsEmpty(b.Left))
                {
                    break;
                }
            }
            for (b.Right = width - 1; b.Right >= 0; --b.Right)
            {
                if (!columnIsEmpty(b.Right))
                {
                    break;
                }
            }
            for (b.Top = 0; b.Top < height; ++b.Top)
            {
                if (!rowIsEmpty(b.Top))
                {
                    break;
                }
            }
            for (b.Bottom = height - 1; b.Bottom >= 0; --b.Bottom)
            {
                if (!rowIsEmpty(b.Bottom))
                {
                    break;
                }
            }

            return(b);
        }
        // create the page array
        public void GeneratePageArray()
        {
            int[] pixels = MyExtensions.ToArgbArray(BitmapToGenerate);
            int   width = BitmapToGenerate.Width, height = BitmapToGenerate.Height;
            int   black = Color.Black.ToArgb(), white = Color.White.ToArgb();
            Dictionary <int, List <byte> > dpages           = new Dictionary <int, List <byte> >();
            Dictionary <int, bool>         backColorListInt = ColorList.ToDictionary <KeyValuePair <Color, bool>, int, bool>(kvp => kvp.Key.ToArgb(), kvp => kvp.Value);
            bool ColumnMajor = OutConfig.bitLayout == OutputConfiguration.BitLayout.ColumnMajor;

            // create pages
            Pages = new byte[0];

            Func <int, int, int> getPixel = delegate(int x, int y)
            {
                return(pixels[y * width + x]);
            };

            Action <int> ConvertRow = row =>
            {
                dpages.Add(row, new List <byte>());
                // current byte value
                byte currentValue = 0, bitsRead = 0;

                // for each column
                for (int column = 0; column < width; ++column)
                {
                    // is pixel set?
                    if (!backColorListInt[getPixel(column, row)])
                    {
                        // set the appropriate bit in the page
                        if (OutConfig.byteOrderMsbFirst)
                        {
                            currentValue |= (byte)(1 << (7 - bitsRead));
                        }
                        else
                        {
                            currentValue |= (byte)(1 << bitsRead);
                        }
                    }

                    // increment number of bits read
                    // have we filled a page?
                    if (++bitsRead == 8)
                    {
                        // add byte to page array
                        dpages[row].Add(currentValue);

                        // zero out current value
                        currentValue = 0;

                        // zero out bits read
                        bitsRead = 0;
                    }
                }
                // if we have bits left, add it as is
                if (bitsRead != 0)
                {
                    dpages[row].Add(currentValue);
                }
            };

            // for each row
            for (int row = 0; row < height; row++)
            {
                ConvertRow(row);
            }

            List <byte> tempPages = new List <byte>();

            for (int i = 0; i < dpages.Count; i++)
            {
                tempPages.AddRange(dpages[i]);
            }
            Pages = tempPages.ToArray();

            // transpose the pages if column major data is requested
            if (ColumnMajor)
            {
                Pages = Transpose(Pages, BitmapToGenerate.Width, BitmapToGenerate.Height, OutConfig.byteOrderMsbFirst);
            }
        }