/// <summary> /// Get one char's font data by given bitmap /// </summary> /// <param name="bmp"></param> /// <param name="colorMode"></param> /// <param name="scanMode"></param> /// <returns></returns> public static List <byte> getCharFontData(Bitmap bmp, COLOR_MODE colorMode, SCAN_MODE scanMode) { List <byte> bytes = new List <byte>(); List <Color> colors = new List <Color>(); int outsideInitValue, insideInitValue; int outsideStopValue, insideStopValue; int outsideStep, insideStep; bool loopOutsideIsX = true; switch (scanMode) { case SCAN_MODE.ROW_SCAN: outsideInitValue = 0; outsideStopValue = bmp.Height; outsideStep = 1; insideInitValue = 0; insideStopValue = bmp.Width; insideStep = 1; break; case SCAN_MODE.COLUMN_SCAN: outsideInitValue = 0; outsideStopValue = bmp.Width; outsideStep = 1; insideInitValue = 0; insideStopValue = bmp.Height; insideStep = 1; break; default: throw new ArgumentException(Properties.Resources.Err_invalidateScanMode); } // scan font bitmap for (int i = outsideInitValue; i != outsideStopValue; i += outsideStep) { for (int j = insideInitValue; j != insideStopValue; j += insideStep) { if (loopOutsideIsX) { colors.Add(bmp.GetPixel(i, j)); } else { colors.Add(bmp.GetPixel(j, i)); } j++; } // convert to byte every row or column bytes.AddRange(color2byte(colors, colorMode)); colors.Clear(); i++; } return(bytes); }
// 色変更 public void ChangeColor(COLOR_MODE mode) { foreach (GameObject arrow in arrows) { if (mode == COLOR_MODE.GREEN) { arrow.GetComponent <Image>().color = modeGreen; } else if (mode == COLOR_MODE.RED) { arrow.GetComponent <Image>().color = modeRed; } } }
/// <summary> /// Convert color value to byte in given color mode. /// </summary> /// <remarks> /// if can't fill a byte then pad zero at last /// </remarks> /// <param name="colors">color datas</param> /// <param name="colorMode"></param> /// <returns></returns> public static List<byte> color2byte(List<Color> colors, COLOR_MODE colorMode) { List<byte> bytes = new List<byte>(); switch (colorMode) { case COLOR_MODE.MONO: { byte b = 0, mask = 0x80; foreach (Color color in colors) { if (Misc.isBlackLikely(color)) { b |= mask; } mask >>= 1; if (mask == 0) { bytes.Add(b); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), b)); b = 0; mask = 0x80; } } // if can't fill a byte, pad zero to right if (mask != 0) { bytes.Add(b); } } break; case COLOR_MODE.GRAY4BITS: { byte b = 0; bool isHighBits = true; foreach (Color color in colors) { if (isHighBits) { b = (byte)(Misc.getGrayScale(color) & 0xf0); } else { b |= (byte)(Misc.getGrayScale(color)); bytes.Add(b); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), b)); b = 0; } isHighBits = !isHighBits; } // if can't fill a byte. pad zero bytes.Add(b); } break; case COLOR_MODE.RGB565: foreach (Color color in colors) { UInt16 value = Misc.getRGB565(color); bytes.AddRange(BitConverter.GetBytes(value)); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), value)); } break; case COLOR_MODE.RGB565P: foreach (Color color in colors) { UInt16 value = Misc.getRGB565P(color); bytes.AddRange(BitConverter.GetBytes(value)); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), value)); } break; default: throw new ArgumentException(Properties.Resources.Err_invalidateColorMode); } //Debug.WriteLine("====one line scaned", "color2byte()"); return bytes; }
/// <summary> /// Get one char's font data by given bitmap /// </summary> /// <param name="bmp"></param> /// <param name="colorMode"></param> /// <param name="scanMode"></param> /// <returns></returns> public static List<byte> getCharFontData(Bitmap bmp, COLOR_MODE colorMode, SCAN_MODE scanMode) { List<byte> bytes = new List<byte>(); List<Color> colors = new List<Color>(); int outsideInitValue, insideInitValue; int outsideStopValue, insideStopValue; int outsideStep, insideStep; bool loopOutsideIsX = true; switch (scanMode) { case SCAN_MODE.ROW_SCAN: outsideInitValue = 0; outsideStopValue = bmp.Height; outsideStep = 1; insideInitValue = 0; insideStopValue = bmp.Width; insideStep = 1; break; case SCAN_MODE.COLUMN_SCAN: outsideInitValue = 0; outsideStopValue = bmp.Width; outsideStep = 1; insideInitValue = 0; insideStopValue = bmp.Height; insideStep = 1; break; default: throw new ArgumentException(Properties.Resources.Err_invalidateScanMode); } // scan font bitmap for(int i=outsideInitValue; i!=outsideStopValue; i+=outsideStep) { for(int j=insideInitValue; j!=insideStopValue; j+=insideStep) { if (loopOutsideIsX) { colors.Add(bmp.GetPixel(i, j)); } else { colors.Add(bmp.GetPixel(j, i)); } j++; } // convert to byte every row or column bytes.AddRange(color2byte(colors, colorMode)); colors.Clear(); i++; } return bytes; }
/// <summary> /// Export Font Data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSave_Click(object sender, EventArgs e) { // check output settings COLOR_MODE colorMode = radioButton8.Checked ? COLOR_MODE.MONO : radioButton9.Checked ? COLOR_MODE.GRAY4BITS : radioButton10.Checked ? COLOR_MODE.RGB565 : radioButton11.Checked ? COLOR_MODE.RGB565P : COLOR_MODE.UNKONOWN; if (colorMode == COLOR_MODE.UNKONOWN) { Misc.MsgInf(Properties.Resources.Inf_colorMode_required); return; } SCAN_MODE scanMode = radioButtonRow1.Checked ? SCAN_MODE.ROW_SCAN : radioButtonColumn1.Checked ? SCAN_MODE.COLUMN_SCAN : SCAN_MODE.UNKOWN; if (scanMode == SCAN_MODE.UNKOWN) { Misc.MsgInf(Properties.Resources.Inf_scanMode_required); return; } string arrayName = textBoxArrayName.Text; if (arrayName.Length == 0) { Misc.MsgInf(Properties.Resources.Inf_arrayName_required); return; } // if content never showed, check input parameters, and show it if (fontSettings == null) { this.toolStripButtonView_Click(null, null); if (fontSettings == null) { return; } } // user canceled if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } if (radioButton13.Checked) { saveAsBitmap(saveFileDialog1.FileName); return; } // get stream for write font data List <byte> bytes; int iNewLinePosition = Properties.Settings.Default.NewLinePositionOfExport; int pos = 1; Size size = new Size(); StringBuilder sb = new StringBuilder(); using (Stream ss = this.saveFileDialog1.OpenFile()) { using (StreamWriter sw = new System.IO.StreamWriter(ss)) { if (!fontSettings.AutoFixWidth) { sw.WriteLine(string.Format(@"const unsigned char {0} = {{", arrayName)); } using (Bitmap bmp = new Bitmap(fontSettings.BlockWidth, fontSettings.BlockHeight)) { using (Graphics g = Graphics.FromImage(bmp)) { // output chars data one by one foreach (char c in fontSettings.Chars) { FontDrawExtend.DrawChar(g, fontSettings, c); bytes = FontDrawExtend.getCharFontData(bmp, colorMode, scanMode, fontSettings, out size); if (fontSettings.AutoFixWidth) { sw.WriteLine(@" // char ""{0}"" const unsigned char {1}_{2:X2}_dat[] = {{", c, arrayName, (int)c); pos = 1; } foreach (byte b in bytes) { sw.Write("0x{0:X2},", b); if (iNewLinePosition > 0 && pos == iNewLinePosition) { sw.Write(Environment.NewLine); pos = 0; } pos++; } if (fontSettings.AutoFixWidth) { sw.WriteLine("};"); sw.WriteLine(@"mos_font_data_t {0}_{1:X2} = {{ {2},{3}, &{4}_{5:X2}_dat[0]}};", arrayName, (int)c, size.Width, size.Height, arrayName, (int)c); sb.Append(String.Format("&{0}_{1:X2},", arrayName, (int)c)); } } } } if (fontSettings.AutoFixWidth) { sw.WriteLine(@" // data list mos_font_data_t* {0}[] = {{{1}}};", arrayName, sb.ToString()); } else { sw.WriteLine("}"); } } } // ask for open export file if (MessageBox.Show(Properties.Resources.Inf_openFileConfirm, Properties.Resources.Caption_exportMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Process.Start(this.saveFileDialog1.FileName); } }
/// <summary> /// Convert color value to byte in given color mode. /// </summary> /// <remarks> /// if can't fill a byte then pad zero at last /// </remarks> /// <param name="colors">color datas</param> /// <param name="colorMode"></param> /// <returns></returns> public static List <byte> color2byte(List <Color> colors, COLOR_MODE colorMode, int threshold) { List <byte> bytes = new List <byte>(); switch (colorMode) { case COLOR_MODE.MONO: { byte b = 0, mask = 0x01; int padding_num = colors.Count % 8; if (padding_num != 0) { for (int i = 0; i < padding_num; i++) { colors.Add(Color.FromArgb(0, 0, 0)); } } foreach (Color color in colors) { if (Misc.isBlackLikely(color, threshold)) { b |= mask; } mask <<= 1; if (mask == 0) { bytes.Add(b); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), b)); b = 0; mask = 0x01; } } // if can't fill a byte, pad zero to right if (mask != 0x01) { bytes.Add(b); } } break; case COLOR_MODE.GRAY4BITS: { byte b = 0; bool isHighBits = true; foreach (Color color in colors) { if (isHighBits) { b = (byte)(Misc.getGrayScale(color) & 0xf0); } else { b |= (byte)(Misc.getGrayScale(color)); bytes.Add(b); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), b)); b = 0; } isHighBits = !isHighBits; } // if can't fill a byte. pad zero bytes.Add(b); } break; case COLOR_MODE.RGB565: foreach (Color color in colors) { UInt16 value = Misc.getRGB565(color); bytes.AddRange(BitConverter.GetBytes(value)); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), value)); } break; case COLOR_MODE.RGB565P: foreach (Color color in colors) { UInt16 value = Misc.getRGB565P(color); bytes.AddRange(BitConverter.GetBytes(value)); //Debug.Write(string.Format("c={0};={1:X2}\t\t", color.ToArgb().ToString("X8").Substring(2), value)); } break; default: throw new ArgumentException(Properties.Resources.Err_invalidateColorMode); } //Debug.WriteLine("====one line scaned", "color2byte()"); return(bytes); }
/// <summary> /// Get one char's font data by given bitmap /// </summary> /// <param name="bmp"></param> /// <param name="colorMode"></param> /// <param name="scanMode"></param> /// <returns></returns> public static List <byte> getCharFontData(Bitmap bmp, COLOR_MODE colorMode, SCAN_MODE scanMode, FontSettings settings, out Size size) { List <byte> bytes = new List <byte>(); StringBuilder sb = new StringBuilder(); List <Color> colors = new List <Color>(); int outsideInitValue, insideInitValue; int outsideStopValue, insideStopValue; int outsideStep, insideStep; bool loopOutsideIsX = true; Rectangle rect = new Rectangle(0, 0, settings.BlockWidth, settings.BlockHeight); if (settings.AutoFixWidth) { rect = GetFixedWidthRect(bmp, rect, settings.MinWidth, settings.BackColor); size = new Size(rect.Width, rect.Height); } else { size = new Size(); } switch (scanMode) { case SCAN_MODE.ROW_SCAN: outsideInitValue = rect.Top; outsideStopValue = rect.Bottom; outsideStep = 1; insideInitValue = rect.Left; insideStopValue = rect.Right; insideStep = 1; loopOutsideIsX = false; break; case SCAN_MODE.COLUMN_SCAN: outsideInitValue = rect.Left; outsideStopValue = rect.Right; outsideStep = 1; insideInitValue = rect.Top; insideStopValue = rect.Bottom; insideStep = 1; loopOutsideIsX = true; break; default: throw new ArgumentException(Properties.Resources.Err_invalidateScanMode); } // scan font bitmap for (int i = outsideInitValue; i != outsideStopValue; i += outsideStep) { for (int j = insideInitValue; j != insideStopValue; j += insideStep) { if (loopOutsideIsX) { colors.Add(bmp.GetPixel(i, j)); } else { colors.Add(bmp.GetPixel(j, i)); } } } bytes.AddRange(color2byte(colors, colorMode)); return(bytes); }