static public void Save(Bitmap[] abm, Palette pal, string strFile) { // Open file for writing BinaryWriter bwtr = new BinaryWriter(new FileStream(strFile, FileMode.Create, FileAccess.Write)); // Convert into tbm's TBitmap[] atbm = new TBitmap[abm.Length]; for (int ibm = 0; ibm < abm.Length; ibm++) { atbm[ibm] = new TBitmap(abm[ibm], pal); } // Serialize the whole thing bwtr.Write(Serialize(atbm)); // All done bwtr.Close(); }
static byte[] Serialize(TBitmap[] atbm) { //struct TBitmapHeader: // ctbm // TBitmapEntry[] atbme; // word cx; // word cy; // word yBaseline; // word ibsd; // word cbsd; // ScanData[] asd; // Write header info BinaryWriter bwtr = new BinaryWriter(new MemoryStream()); bwtr.Write(Misc.SwapUShort((ushort)atbm.Length)); // Serialize TBitmapEntry's ArrayList alsSdBytes = new ArrayList(); int ibCurrent = 2 + 10 * atbm.Length; for (int itbm = 0; itbm < atbm.Length; itbm++) { bwtr.Write(Misc.SwapUShort((ushort)atbm[itbm].m_cx)); bwtr.Write(Misc.SwapUShort((ushort)atbm[itbm].m_cy)); bwtr.Write(Misc.SwapUShort((ushort)atbm[itbm].m_yBaseline)); bwtr.Write(Misc.SwapUShort((ushort)ibCurrent)); byte[] ab = atbm[itbm].SerializeScanData(); alsSdBytes.AddRange(ab); bwtr.Write(Misc.SwapUShort((ushort)ab.Length)); ibCurrent += ab.Length; Debug.Assert(ibCurrent < ushort.MaxValue); } // Write sd bytes bwtr.Write((byte[])alsSdBytes.ToArray(typeof(byte))); // Done byte[] abT = new Byte[bwtr.BaseStream.Length]; bwtr.BaseStream.Seek(0, SeekOrigin.Begin); bwtr.BaseStream.Read(abT, 0, abT.Length); bwtr.Close(); return abT; }
public static void SaveFont(string strFileBitmap, Palette pal, string strFileAscii, string strFileSave) { // Get the character order TextReader tr = new StreamReader(strFileAscii); string strAscii = tr.ReadLine(); tr.Close(); // Get the character count int cch = strAscii.Length; // Load the image, lose scaling factor Bitmap bmFile = new Bitmap(strFileBitmap); Bitmap bm = Misc.NormalizeBitmap(bmFile); bmFile.Dispose(); // Turn this on to see the character -> glyph mapping as it happens (help for // finding 'font bugs'). Set a breakpoint below on frm.Dispose(). #if SHOWFONT Form frm = new Form(); frm.Height = 1000; frm.Show(); Graphics gT = frm.CreateGraphics(); gT.InterpolationMode = InterpolationMode.NearestNeighbor; int yDst = 0; int xDst = 0; #endif // Scan the bitmap for widths int xLast = 0; int ich = 0; byte[] acxChar = new byte[256]; for (int x = 0; x < bm.Width; x++) { if (bm.GetPixel(x, 0) != Color.FromArgb(255, 0, 255)) { Debug.Assert(ich < cch); acxChar[strAscii[ich]] = (byte)(x - xLast); #if SHOWFONT gT.DrawString(strAscii[ich].ToString(), frm.Font, new SolidBrush(frm.ForeColor), new PointF(xDst, yDst)); Rectangle rcDst = new Rectangle(xDst + 20, yDst + 2, (x - xLast), bm.Height); Rectangle rcSrc = new Rectangle(xLast, 1, x - xLast, bm.Height); gT.DrawImage(bm, rcDst, rcSrc, GraphicsUnit.Pixel); yDst += Math.Max(bm.Height, frm.Font.Height); if (yDst > frm.ClientRectangle.Height) { xDst += 50; yDst = 0; } gT.Flush(); Application.DoEvents(); #endif ich++; xLast = x; } } #if SHOWFONT gT.Dispose(); frm.Dispose(); #endif if (ich != cch) { MessageBox.Show(String.Format("Expecting {0} characters but found {2}{1}.", cch, ich, ich < cch ? "only " : ""), "bcr2 - Font Compilation Error"); Debug.Assert(ich == cch - 1); } int cy = bm.Height - 1; // Save serialization ArrayList alsSdEven = new ArrayList(); int xT = 0; int ichDefault = -1; for (ich = 0; ich < cch; ich++) { // ? is the default "no glyph" character if (strAscii[ich] == '?') ichDefault = ich; // Get subimage int cx = acxChar[strAscii[ich]]; Rectangle rcT = new Rectangle(xT, 1, cx, cy); xT += cx; Bitmap bmT = new Bitmap(cx, cy, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmT); g.DrawImage(bm, 0, 0, rcT, GraphicsUnit.Pixel); g.Dispose(); // Compile scan data TBitmap tbm = new TBitmap(bmT, pal); bmT.Dispose(); // Save scan data serialization alsSdEven.Add(tbm.SerializeScanData()); } //FontHeader { // word cy; // byte acxChar[256]; // word mpchibsdEven[256]; // ScanData asd[1]; //}; // First serialize scan data ArrayList alsIbsdEven = new ArrayList(); ArrayList alsSd = new ArrayList(); foreach (byte[] absd in alsSdEven) { if ((alsSd.Count & 1) != 0) alsSd.Add((byte)0); alsIbsdEven.Add(alsSd.Count); alsSd.AddRange(absd); } // Write out to file BinaryWriter bwtr = new BinaryWriter(new FileStream(strFileSave, FileMode.Create, FileAccess.Write)); // Height bwtr.Write(Misc.SwapUShort((ushort)cy)); // Ascii ordered char widths in bytes. First init 0's to width of '?' if (ichDefault != -1) { for (ich = 0; ich < acxChar.Length; ich++) { if (acxChar[ich] == 0) { acxChar[ich] = acxChar[strAscii[ichDefault]]; } } } bwtr.Write(acxChar); // Ascii ordered offsets to even scan data (even) // Fill unused entries to entry for '?' int[] aibsdEven = new int[256]; for (int ibsd = 0; ibsd < aibsdEven.Length; ibsd++) aibsdEven[ibsd] = -1; for (int i = 0; i < cch; i++) aibsdEven[strAscii[i]] = (int)alsIbsdEven[i]; if (ichDefault != -1) { for (int ibsd = 0; ibsd < aibsdEven.Length; ibsd++) { if (aibsdEven[ibsd] == -1) { aibsdEven[ibsd] = (int)alsIbsdEven[ichDefault]; } } } // Write it out int cbHeader = 2 + 256 + 512; for (int i = 0; i < 256; i++) bwtr.Write(Misc.SwapUShort((ushort)(cbHeader + aibsdEven[i]))); // Now save scan data bwtr.Write((byte[])alsSd.ToArray(typeof(byte))); // Done bwtr.Close(); }
public static void Save(Bitmap[] abm, Palette pal, string strFile) { // Open file for writing BinaryWriter bwtr = new BinaryWriter(new FileStream(strFile, FileMode.Create, FileAccess.Write)); // Convert into tbm's TBitmap[] atbm = new TBitmap[abm.Length]; for (int ibm = 0; ibm < abm.Length; ibm++) atbm[ibm] = new TBitmap(abm[ibm], pal); // Serialize the whole thing bwtr.Write(Serialize(atbm)); // All done bwtr.Close(); }
static public void SaveFont(string strFileBitmap, Palette pal, string strFileAscii, string strFileSave) { // Get the character order TextReader tr = new StreamReader(strFileAscii); string strAscii = tr.ReadLine(); tr.Close(); // Get the character count int cch = strAscii.Length; // Load the image, lose scaling factor Bitmap bmFile = new Bitmap(strFileBitmap); Bitmap bm = Misc.NormalizeBitmap(bmFile); bmFile.Dispose(); // Turn this on to see the character -> glyph mapping as it happens (help for // finding 'font bugs'). Set a breakpoint below on frm.Dispose(). #if SHOWFONT Form frm = new Form(); frm.Height = 1000; frm.Show(); Graphics gT = frm.CreateGraphics(); gT.InterpolationMode = InterpolationMode.NearestNeighbor; int yDst = 0; int xDst = 0; #endif // Scan the bitmap for widths int xLast = 0; int ich = 0; byte[] acxChar = new byte[256]; for (int x = 0; x < bm.Width; x++) { if (bm.GetPixel(x, 0) != Color.FromArgb(255, 0, 255)) { Debug.Assert(ich < cch); acxChar[strAscii[ich]] = (byte)(x - xLast); #if SHOWFONT gT.DrawString(strAscii[ich].ToString(), frm.Font, new SolidBrush(frm.ForeColor), new PointF(xDst, yDst)); Rectangle rcDst = new Rectangle(xDst + 20, yDst + 2, (x - xLast), bm.Height); Rectangle rcSrc = new Rectangle(xLast, 1, x - xLast, bm.Height); gT.DrawImage(bm, rcDst, rcSrc, GraphicsUnit.Pixel); yDst += Math.Max(bm.Height, frm.Font.Height); if (yDst > frm.ClientRectangle.Height) { xDst += 50; yDst = 0; } gT.Flush(); Application.DoEvents(); #endif ich++; xLast = x; } } #if SHOWFONT gT.Dispose(); frm.Dispose(); #endif if (ich != cch) { MessageBox.Show(String.Format("Expecting {0} characters but found {2}{1}.", cch, ich, ich < cch ? "only " : ""), "bcr2 - Font Compilation Error"); Debug.Assert(ich == cch - 1); } int cy = bm.Height - 1; // Save serialization ArrayList alsSdEven = new ArrayList(); int xT = 0; int ichDefault = -1; for (ich = 0; ich < cch; ich++) { // ? is the default "no glyph" character if (strAscii[ich] == '?') { ichDefault = ich; } // Get subimage int cx = acxChar[strAscii[ich]]; Rectangle rcT = new Rectangle(xT, 1, cx, cy); xT += cx; Bitmap bmT = new Bitmap(cx, cy, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmT); g.DrawImage(bm, 0, 0, rcT, GraphicsUnit.Pixel); g.Dispose(); // Compile scan data TBitmap tbm = new TBitmap(bmT, pal); bmT.Dispose(); // Save scan data serialization alsSdEven.Add(tbm.SerializeScanData()); } //FontHeader { // word cy; // byte acxChar[256]; // word mpchibsdEven[256]; // ScanData asd[1]; //}; // First serialize scan data ArrayList alsIbsdEven = new ArrayList(); ArrayList alsSd = new ArrayList(); foreach (byte[] absd in alsSdEven) { if ((alsSd.Count & 1) != 0) { alsSd.Add((byte)0); } alsIbsdEven.Add(alsSd.Count); alsSd.AddRange(absd); } // Write out to file BinaryWriter bwtr = new BinaryWriter(new FileStream(strFileSave, FileMode.Create, FileAccess.Write)); // Height bwtr.Write(Misc.SwapUShort((ushort)cy)); // Ascii ordered char widths in bytes. First init 0's to width of '?' if (ichDefault != -1) { for (ich = 0; ich < acxChar.Length; ich++) { if (acxChar[ich] == 0) { acxChar[ich] = acxChar[strAscii[ichDefault]]; } } } bwtr.Write(acxChar); // Ascii ordered offsets to even scan data (even) // Fill unused entries to entry for '?' int[] aibsdEven = new int[256]; for (int ibsd = 0; ibsd < aibsdEven.Length; ibsd++) { aibsdEven[ibsd] = -1; } for (int i = 0; i < cch; i++) { aibsdEven[strAscii[i]] = (int)alsIbsdEven[i]; } if (ichDefault != -1) { for (int ibsd = 0; ibsd < aibsdEven.Length; ibsd++) { if (aibsdEven[ibsd] == -1) { aibsdEven[ibsd] = (int)alsIbsdEven[ichDefault]; } } } // Write it out int cbHeader = 2 + 256 + 512; for (int i = 0; i < 256; i++) { bwtr.Write(Misc.SwapUShort((ushort)(cbHeader + aibsdEven[i]))); } // Now save scan data bwtr.Write((byte[])alsSd.ToArray(typeof(byte))); // Done bwtr.Close(); }