public bool WriteAnir(Palette pal, string strExportPath, string strAnimName) { Color clrTransparent = Color.FromArgb(0xff, 0, 0xff); SolidBrush brTransparent = new SolidBrush(clrTransparent); ASCIIEncoding enc = new ASCIIEncoding(); FileStream stm = new FileStream(strExportPath + Path.DirectorySeparatorChar + strAnimName + ".anir", FileMode.Create, FileAccess.Write); BinaryWriter stmw = new BinaryWriter(stm); // Count the number of Strips ushort cstpd = (ushort)StripSet.Count; // Write AnimationFileHeader.cstpd stmw.Write(Misc.SwapUShort(cstpd)); // Write array of offsets to StripDatas (AnimationFileHeader.aoffStpd) ushort offStpd = (ushort)(2 + (2 * cstpd)); ArrayList albm = new ArrayList(); foreach (Strip stp in StripSet) { stmw.Write(Misc.SwapUShort(offStpd)); // Advance offset to where the next StripData will be offStpd += (ushort)((26 + 1 + 1 + 2) /* sizeof(StripData) - sizeof(FrameData) */ + ((1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) /* sizeof(FrameData) */ * stp.Count)); // Force word alignment of StripDatas if ((offStpd & 1) == 1) { offStpd++; } } // Write array of StripDatas foreach (Strip stp in StripSet) { // Write StripData.Name byte[] abT = new byte[26]; enc.GetBytes(stp.Name, 0, Math.Min(stp.Name.Length, 25), abT, 0); abT[25] = 0; stmw.Write(abT); // Write StripData.cHold stmw.Write((byte)stp.DefHoldCount); // Write StripData.bfFlags stmw.Write((byte)0); // Write StripData.cfrmd ushort cfrmd = (ushort)stp.Count; stmw.Write(Misc.SwapUShort(cfrmd)); // Write array of FrameDatas foreach (Frame fr in stp) { // Add the Frame's Bitmap for output int ibm = -1; Bitmap bm; if (fr.BitmapPlacers.Count > 0) { bm = fr.BitmapPlacers[0].XBitmap.Bitmap; ibm = albm.IndexOf(bm); if (ibm == -1) { ibm = albm.Add(bm); } } // Write FrameData.ibm (the index of the Bitmap as it will be in the Bitmap array) stmw.Write((byte)ibm); ibm = -1; if (fr.BitmapPlacers.Count > 1) { // Add the Frame's Bitmap for output bm = fr.BitmapPlacers[1].XBitmap.Bitmap; ibm = albm.IndexOf(bm); if (ibm == -1) { ibm = albm.Add(bm); } } // Write FrameData.ibm2 (the index of the Bitmap as it will be in the Bitmap array) stmw.Write((byte)ibm); // Write FrameData.cHold stmw.Write((byte)fr.HoldCount); // Write FrameData.xOrigin, FrameData.yOrigin if (fr.BitmapPlacers.Count > 0) { stmw.Write((byte)fr.BitmapPlacers[0].X); stmw.Write((byte)fr.BitmapPlacers[0].Y); } else { stmw.Write((byte)0); stmw.Write((byte)0); } if (fr.BitmapPlacers.Count > 1) { stmw.Write((byte)fr.BitmapPlacers[1].X); stmw.Write((byte)fr.BitmapPlacers[1].Y); } else { stmw.Write((byte)0); stmw.Write((byte)0); } // Write FrameData.bCustomData1, FrameData.bCustomData2 stmw.Write((byte)fr.SpecialPoint.X); stmw.Write((byte)fr.SpecialPoint.Y); #if false // Write FrameData.bCustomData3 stmw.Write((byte)0); #endif } // Force word alignment of StripDatas given that FrameDatas are an odd // number of bytes long and there may be an odd number of frames. if ((cfrmd & 1) == 1) { stmw.Write((byte)0); } } stmw.Close(); // Write out .tbm if (albm.Count != 0) { string strFileName = strExportPath + Path.DirectorySeparatorChar + strAnimName + ".tbm"; // if (gfSuperVerbose) // Console.WriteLine("Crunching and writing " + strFileName); TBitmap.Save((Bitmap[])albm.ToArray(typeof(Bitmap)), pal, strFileName); } return(true); }
// Write the .anir file and .bmps or .tbms, depending on fCrunch public static bool Export(AnimSet anis, string strExportPath, Palette pal, bool fCrunch, bool fValidateColors) { Color clrTransparent = Color.FromArgb(0xff, 0, 0xff); SolidBrush brTransparent = new SolidBrush(clrTransparent); if (strExportPath == null) { strExportPath = "."; } ASCIIEncoding enc = new ASCIIEncoding(); FileStream stm = new FileStream(strExportPath + @"\" + anis.Name + ".anir", FileMode.Create, FileAccess.Write); BinaryWriter stmw = new BinaryWriter(stm); // Count the number of FrameSets (aka Strips) ushort cstpd = 0; foreach (DictionaryEntry deAnimSet in anis.Items) { Anim ani = (Anim)deAnimSet.Value; cstpd += (ushort)ani.Items.Count; } // Write AnimationFileHeader.cstpd stmw.Write(Misc.SwapUShort(cstpd)); // Write array of offsets to StripDatas (AnimationFileHeader.aoffStpd) ushort offStpd = (ushort)(2 + (2 * cstpd)); byte ibm = 0; ArrayList albm = new ArrayList(); foreach (DictionaryEntry deAnimSet in anis.Items) { Anim ani = (Anim)deAnimSet.Value; foreach (DictionaryEntry deAnim in ani.Items) { FrameSet frms = (FrameSet)deAnim.Value; stmw.Write(Misc.SwapUShort(offStpd)); // Advance offset to where the next StripData will be offStpd += (ushort)((26 + 1 + 1 + 2) /* sizeof(StripData) - sizeof(FrameData) */ + ((1 + 1 + 1 + 1 + 1 + 1) /* sizeof(FrameData) */ * frms.Count)); } } // Write array of StripDatas foreach (DictionaryEntry deAnimSet in anis.Items) { Anim ani = (Anim)deAnimSet.Value; foreach (DictionaryEntry deAnim in ani.Items) { FrameSet frms = (FrameSet)deAnim.Value; // Write StripData.Name string strName = ani.Name + " " + frms.Name; byte[] abT = new byte[26]; enc.GetBytes(strName, 0, Math.Min(strName.Length, 25), abT, 0); abT[25] = 0; stmw.Write(abT); // Write StripData.cDelay stmw.Write((byte)0); // Write StripData.bfFlags stmw.Write((byte)0); // Write StripData.cfrmd ushort cfrmd = (ushort)frms.Count; stmw.Write(Misc.SwapUShort(cfrmd)); // Write array of FrameDatas foreach (Frame frm in frms) { // Write FrameData.ibm (the index of the Bitmap as it will be in the Bitmap array) stmw.Write((byte)ibm); ibm++; // Add the Frame's Bitmap for output Bitmap bm = frm.Bitmap; if (fCrunch) { albm.Add(bm); } else { // If not crunching then we need to go through some special work to preserve // the origin point. Since the origin point is determined by finding the center // of the imported bitmap we preserve it at export by creating a new bitmap // sized so that he origin will be in its center. // The '+2' at the end gives us a pixel of transparent space padding the left // and right sides of the bitmap. We don't require this for any particular // reason but it comes in handy. int cxNew = (Math.Max(frm.OriginX, bm.Width - frm.OriginX) * 2) + 2; // The '+2' at the end is to ensure a single blank scanline at the top // which Import relies on to determine the transparent color. int cyNew = (Math.Max(frm.OriginY, bm.Height - frm.OriginY) * 2) + 2; using (Bitmap bmNew = new Bitmap(cxNew, cyNew, bm.PixelFormat)) { using (Graphics g = Graphics.FromImage(bmNew)) { g.FillRectangle(brTransparent, 0, 0, cxNew, cyNew); g.DrawImage(bm, cxNew / 2 - frm.OriginX, cyNew / 2 - frm.OriginY); } strName = anis.Name + "_" + ani.Name + "_" + frms.Name + "_" + frm.Index.ToString(); bmNew.Save(strExportPath + @"\" + strName + ".png", ImageFormat.Png); } } // Write FrameData.xOrigin, FrameData.yOrigin stmw.Write((byte)frm.OriginX); stmw.Write((byte)frm.OriginY); // Write FrameData.bCustomData1, FrameData.bCustomData2, FrameData.bCustomData3 stmw.Write((byte)0); stmw.Write((byte)0); stmw.Write((byte)0); } } } stmw.Close(); // Write out .tbm if (albm.Count != 0) { string strFileName = strExportPath + @"\" + anis.Name + ".tbm"; if (gfSuperVerbose) { Console.WriteLine("Crunching and writing " + strFileName); } TBitmap.Save((Bitmap[])albm.ToArray(typeof(Bitmap)), pal, strFileName); } return(true); }
static void Main(string[] astr) { // Special cases if (astr.Length == 0) { Usage(); return; } bool fRaw = false; bool fFont = false; double nScale = 1.0; bool fSaveScaled = false; int istr = 0; for (; istr < astr.Length; istr++) { if (astr[istr][0] != '-') { break; } switch (astr[istr]) { case "-raw": fRaw = true; break; case "-font": fFont = true; break; case "-scale": istr++; nScale = double.Parse(astr[istr]); break; case "-savescaled": fSaveScaled = true; break; } } // Params: palette outputdir filespecs // Get palette Palette pal = new Palette(astr[istr++]); // Get directory string strDir = astr[istr++]; // Expand filespecs ArrayList alsFiles = new ArrayList(); for (; istr < astr.Length; istr++) { string strFileT = Path.GetFileName(astr[istr]); string strDirT = Path.GetDirectoryName(astr[istr]); if (strDirT == "") { strDirT = "."; } string[] astrFiles = Directory.GetFiles(strDirT, strFileT); alsFiles.AddRange(astrFiles); } // Output if (fFont) { foreach (string strFile in alsFiles) { string strFileFnt = strDir + Path.DirectorySeparatorChar + Path.GetFileName(strFile.Substring(0, strFile.Length - 4) + ".fnt"); string strFileAscii = Path.GetDirectoryName(strFile) + Path.DirectorySeparatorChar + Path.GetFileName(strFile.Substring(0, strFile.Length - 4) + ".txt"); Console.WriteLine(strFile + " -> " + strFileFnt); TBitmap.SaveFont(strFile, pal, strFileAscii, strFileFnt); } } else { if (fRaw) { foreach (string strFile in alsFiles) { string strFileRaw = strDir + Path.DirectorySeparatorChar + Path.GetFileName(strFile.Substring(0, strFile.Length - 4) + ".rbm"); BitmapRaw.Save(strFile, pal, strFileRaw); } } else { foreach (string strFile in alsFiles) { string strFileTbm = strDir + Path.DirectorySeparatorChar + Path.GetFileName(strFile.Substring(0, strFile.Length - 4) + ".tbm"); Console.WriteLine(strFile + " -> " + strFileTbm); Bitmap[] abm = new Bitmap[1]; abm[0] = new Bitmap(strFile); if (nScale != 1.0) { abm[0] = TBitmapTools.ScaleBitmap(abm[0], nScale, pal); } if (fSaveScaled) { abm[0].Save(Path.Combine(strDir, Path.GetFileName(strFile)), ImageFormat.Bmp); } else { TBitmap.Save(abm, pal, strFileTbm); } } } } }