Exemplo n.º 1
0
        private void CodeToImage(object sender, DoWorkEventArgs e)
        {
            TransformColorFormats format = (TransformColorFormats)e.Argument;

            string Code = "";
            int    Width = 0, Height = 0;
            int    x = 0, y = 0;

            this.Invoke(new MethodInvoker(delegate {
                Width  = (int)num_Width.Value;
                Height = (int)num_Height.Value;
                Code   = GeneratedCode.Text;
            }));

            if (Width <= 0 || Height <= 0)
            {
                MessageBox.Show("Error:\nThe size of image can't be zero!");
                return;
            }

            // Some additional checks
            if (format == TransformColorFormats.Mono_1bpp_H && Width % 8 != 0)
            {
                MessageBox.Show("Error:\nWidth has to be multiple 8!");
                return;
            }

            if (format == TransformColorFormats.Mono_1bpp_V && Height % 8 != 0)
            {
                MessageBox.Show("Error:\nHeight has to be multiple 8!");
                return;
            }

            Bitmap result = new Bitmap(Width, Height);

            int pixelsCurrent = 0;
            int pixelsTotal   = Width * Height;
            int pixelColor;

            // Two ways re symbol-by-symbol, or use regex. What faster?
            // Read from symbos "{", ignore tabs and spaces, read to "}". Use "," as separator.
            // Use regex and search pattern "0x([0-9a-fA-F]{2}),"
            string regex = "";

            switch (format)
            {
            case TransformColorFormats.Mono_1bpp_H:
            case TransformColorFormats.Mono_1bpp_V:
            case TransformColorFormats.RGB_332:
                regex = @"0x([0-9a-fA-F]{2}),";     // 0xAB (one byte)
                break;

            case TransformColorFormats.RGB_444:
            case TransformColorFormats.RGB_565:
                regex = @"0x([0-9a-fA-F]{4}),";     // 0xABCD (two bytes)
                break;
            }

            foreach (Match m in Regex.Matches(Code, regex))
            {
                pixelColor = int.Parse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);

                switch (format)
                {
                case TransformColorFormats.Mono_1bpp_H:
                    for (int offset = 7; offset >= 0; offset--)
                    {
                        result.SetPixel(x, y, ((pixelColor & (1 << offset)) > 0) ? Color.Black : Color.White);
                        x++;
                    }
                    x--;
                    break;

                case TransformColorFormats.Mono_1bpp_V:
                    for (int offset = 7; offset >= 0; offset--)
                    {
                        result.SetPixel(x, y + offset, ((pixelColor & (1 << offset)) > 0) ? Color.Black : Color.White);
                    }
                    break;

                case TransformColorFormats.RGB_332:      // RRRG GGBB
                    result.SetPixel(x, y, color_from332(pixelColor));
                    break;

                case TransformColorFormats.RGB_444:      // 0000 RRRR GGGG BBBB
                    result.SetPixel(x, y, color_from444(pixelColor));
                    break;

                case TransformColorFormats.RGB_565:      // RRRR RGGG GGGB BBBB
                    result.SetPixel(x, y, color_from556(pixelColor));
                    break;
                }

                x++;
                pixelsCurrent++;

                if (x == Width)
                {
                    x = 0;
                    if (format == TransformColorFormats.Mono_1bpp_V)
                    {
                        y += 8;
                    }
                    else
                    {
                        y += 1;
                    }

                    bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));

                    if (y == Height)
                    {
                        break;  // All done. Stop conversion.
                    }
                }
            }
            e.Result = result;
        }
Exemplo n.º 2
0
        private string ImageToCode(TransformColorFormats format)
        {
            StringBuilder result = new StringBuilder();
            string        codeFormat = "";
            int           Width = 0, Height = 0;
            Image         image = null;

            this.Invoke(new MethodInvoker(delegate {
                image  = imageBox.Image;
                Width  = (int)num_Width.Value;
                Height = (int)num_Height.Value;
            }));

            switch (format)
            {
            case TransformColorFormats.RGB_332:
                result.Append("uint8_t");
                codeFormat = "0x{0:x2}, ";
                break;

            case TransformColorFormats.RGB_444:
            case TransformColorFormats.RGB_565:
                result.Append("uint16_t");
                codeFormat = "0x{0:x4}, ";
                break;
            }
            result.Append(" image = {" + Environment.NewLine + "\t");

            using (Bitmap bmp = new Bitmap(image, Width, Height))
            {
                int rowPos = 0;

                int pixelsTotal   = bmp.Width * bmp.Height;
                int pixelsCurrent = 0;
                int ColorByte     = 0;

                // ===========================================================
                // Optimisation: Upto 5x faster than GetPixel();
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                 bmp.PixelFormat);

                IntPtr ptr       = bmpData.Scan0;
                int    bytes     = bmpData.Stride * bmp.Height; // ARGB: Width * Height * 4 (Stride = Width * 4)
                byte[] rgbValues = new byte[bytes];

                // Format BGRA (GRB+Alpha, inverted). Example: BBBBBBBB GGGGGGGG RRRRRRRR AAAAAAAA
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                int pixelByte = 0;
                // ===========================================================

                for (int y = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        pixelByte = (y * bmp.Width + x) * 4;

                        switch (format)
                        {
                        case TransformColorFormats.RGB_332:
                            ColorByte =
                                (rgbValues[pixelByte + 2] & 0xE0) |             // 0xE0 = 1110 0000
                                (rgbValues[pixelByte + 1] & 0xE0) >> 3 |
                                (rgbValues[pixelByte + 0] & 0xC0) >> 6;         // 0xC0 = 1100 0000
                            break;

                        case TransformColorFormats.RGB_444:
                            // byte = 16 bit per color
                            // RRRR RGGG GGGB BBBB
                            ColorByte =
                                (rgbValues[pixelByte + 2] & 0xF0) << 4 |        // 0xF0 = 1111 0000
                                    (rgbValues[pixelByte + 1] & 0xF0) |
                                    (rgbValues[pixelByte + 0] & 0xF0) >> 4;
                            break;

                        case TransformColorFormats.RGB_565:
                            // byte = 16 bit per color
                            // RRRR RGGG GGGB BBBB
                            ColorByte =
                                (rgbValues[pixelByte + 2] & 0xF8) << 8 |        // 0xF8 = 1111 1000
                                    (rgbValues[pixelByte + 1] & 0xFC) << 3 |    // 0xFC = 1111 1100
                                    (rgbValues[pixelByte + 0] & 0xF8) >> 3;
                            break;
                        }

                        result.AppendFormat(codeFormat, ColorByte);
                        pixelsCurrent++;

                        rowPos++;
                        if (rowPos == 16)
                        {
                            rowPos = 0;
                            result.Append(Environment.NewLine + "\t");
                        }
                    }
                    bgWork.ReportProgress((int)((double)pixelsCurrent / (double)pixelsTotal * 100));
                }
            }

            result.Append(Environment.NewLine + "};");
            return(result.ToString());
        }