using System; using System.Drawing.Imaging; using System.IO; using System.Text; namespace Tango.Pulse { /// /// Represents a Pulse, twn file writer. /// public class TwnFileWriter { /// /// Writes the specified TWN file. /// /// The TWN file. /// The output. /// Invalid design name length. /// /// Thumbnail image is null. /// or /// Could not serialize a null embroidery file. /// /// The embroidery format must contain exactly 3 characters. public void Write(TwnFile twnFile, Stream output) { BinaryWriter writer = new BinaryWriter(output); //Format Version (Default is 1.0). writer.Write(twnFile.Version); //The name of the design or other labeling information as UTF - 8 text padded with white spaces and limited to 50 bytes. byte[] nameBytes = Encoding.UTF8.GetBytes(twnFile.Name.PadRight(50, ' ')); if (nameBytes.Length < 50) { throw new ArgumentOutOfRangeException("Invalid design name length."); } writer.Write(nameBytes, 0, 50); //The embedded preview image size in byte length as a 32 bit unsigned integer. if (twnFile.Thumbnail == null) { throw new NullReferenceException("Thumbnail image is null."); } byte[] thumbnailBytes = null; //For later use. using (MemoryStream ms = new MemoryStream()) { twnFile.Thumbnail.Save(ms, ImageFormat.Png); writer.Write((UInt32)ms.Length); thumbnailBytes = ms.ToArray(); } //The total number of brush segments as a 32 bit unsigned integer. writer.Write((UInt32)twnFile.Segments.Count); //Number of copies. (The number 0 will be treated as 1, so the default is 0) writer.Write((UInt32)twnFile.NumberOfCopies); //0 - Spool per segment. (default) //1 - Spool per copy. //2 - Single spool for all segments. writer.Write((byte)twnFile.SpoolingMethod); //A unique code representing an optional media ID from Twine’s recommended media list. writer.Write((UInt32)twnFile.MediaID); //3 ASCII characters representing the embedded embroidery file extension (e.g dst, pes). if (twnFile.EmbroideryFileFormat.Length != 3) { throw new ArgumentException("The embroidery format must contain exactly 3 characters."); } writer.Write(Encoding.ASCII.GetBytes(twnFile.EmbroideryFileFormat)); //The byte array length of the embedded embroidery file. if (twnFile.EmbroideryFile == null) { throw new NullReferenceException("Could not serialize a null embroidery file."); } writer.Write((UInt32)twnFile.EmbroideryFile.Length); //Array of bytes representing the design preview image in PNG format(transparent background). //The size of the PNG byte array must be defined in the header. Recommended image width is 1280 //pixels while maintaining aspect ratio. writer.Write(thumbnailBytes); //Array of Brush Segment. The number of brush segments must be defined in the header. foreach (var segment in twnFile.Segments) { //Required thread length in centimeters. writer.Write(segment.Length); //Number of brush stops. writer.Write((UInt32)segment.BrushStops.Count); //Array of brush stops. foreach (var stop in segment.BrushStops) { //The RGB red component. writer.Write(stop.R); //The RGB green component. writer.Write(stop.G); //The RGB blue component. writer.Write(stop.B); //The Brush stop offset position within the parent brush length in percentage (0-1). writer.Write(stop.Offset); } } //Byte array representing the standard embroidery file which can be extracted and inserted into an embroidery machine. writer.Write(twnFile.EmbroideryFile); } } }