aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Pulse/TwnFileReader.cs
blob: 2efa94936d95bfe0440f1f2b243ffa684159379a (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
using System;
using System.Drawing;
using System.IO;
using System.Text;

namespace Tango.Pulse
{
    /// <summary>
    /// Represents a Pulse, twn file reader.
    /// </summary>
    public class TwnFileReader
    {
        /// <summary>
        /// Reads the specified TWN stream.
        /// </summary>
        /// <param name="twnStream">The TWN stream.</param>
        /// <returns></returns>
        public TwnFile Read(Stream twnStream)
        {
            TwnFile twnFile = new TwnFile();

            BinaryReader reader = new BinaryReader(twnStream);

            //Format Version (Default is 1.0).
            twnFile.Version = reader.ReadSingle();


            //The name of the design or other labeling information as UTF - 8 text padded with white spaces and limited to 50 bytes.
            twnFile.Name = Encoding.UTF8.GetString(reader.ReadBytes(50)).Trim();


            //The embedded preview image size in byte length as a 32 bit unsigned integer.
            UInt32 thumbnail_size = reader.ReadUInt32();


            //The total number of brush segments as a 32 bit unsigned integer.
            UInt32 segments_count = reader.ReadUInt32();


            //Number of copies. (The number 0 will be treated as 1, so the default is 0)
            twnFile.NumberOfCopies = (int)reader.ReadUInt32();


            //0 - Spool per segment. (default)
            //1 - Spool per copy.
            //2 - Single spool for all segments.
            twnFile.SpoolingMethod = (SpoolingMethods)(int)reader.ReadByte();


            //A unique code representing an optional media ID from Twine’s recommended media list.
            twnFile.MediaID = (int)reader.ReadUInt32();


            //3 ASCII characters representing the embedded embroidery file extension (e.g dst, pes).
            twnFile.EmbroideryFileFormat = Encoding.ASCII.GetString(reader.ReadBytes(3));


            //The byte array length of the embedded embroidery file.
            UInt32 emb_file_size = reader.ReadUInt32();


            //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.
            MemoryStream ms = new MemoryStream(reader.ReadBytes((int)thumbnail_size));
            twnFile.Thumbnail = new Bitmap(ms);


            //Array of Brush Segment. The number of brush segments must be defined in the header.
            for (int i = 0; i < segments_count; i++)
            {
                TwnSegment segment = new TwnSegment();

                //Required thread length in centimeters.
                segment.Length = reader.ReadSingle();

                //Number of brush stops.
                UInt32 stop_count = reader.ReadUInt32();

                //Array of brush stops.
                for (int j = 0; j < stop_count; j++)
                {
                    TwnStop stop = new TwnStop();

                    //The RGB red component.
                    stop.R = reader.ReadByte();

                    //The RGB green component.
                    stop.G = reader.ReadByte();

                    //The RGB blue component.
                    stop.B = reader.ReadByte();

                    //The Brush stop offset position within the parent brush length in percentage (0-1).
                    stop.Offset = reader.ReadSingle();

                    segment.BrushStops.Add(stop);
                }

                twnFile.Segments.Add(segment);
            }

            //Byte array representing the standard embroidery file which can be extracted and inserted into an embroidery machine.
            twnFile.EmbroideryFile = reader.ReadBytes((int)emb_file_size);

            return twnFile;
        }
    }
}