aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Pulse/TwnFileWriter.cs
blob: 1b173851aeb7c167dbb061993740b5e20d987adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

namespace Tango.Pulse
{
    /// <summary>
    /// Represents a Pulse, twn file writer.
    /// </summary>
    public class TwnFileWriter
    {
        /// <summary>
        /// Writes the specified TWN file.
        /// </summary>
        /// <param name="twnFile">The TWN file.</param>
        /// <param name="output">The output.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">Invalid design name length.</exception>
        /// <exception cref="System.NullReferenceException">
        /// Thumbnail image is null.
        /// or
        /// Could not serialize a null embroidery file.
        /// </exception>
        /// <exception cref="System.ArgumentException">The embroidery format must contain exactly 3 characters.</exception>
        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);
        }
    }
}