Skip to content

Commit

Permalink
Attempt to optimize design drawing code a little, capture more info a…
Browse files Browse the repository at this point in the history
…bout each stitch as it is read from the file, increment project versions in prep for a beta release
  • Loading branch information
njcrawford committed Mar 31, 2016
1 parent 39cdbbd commit ea38b44
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 21 deletions.
138 changes: 124 additions & 14 deletions PesFile/PesFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ private void OpenFile(string filename)
int colorNum = -1;
int colorIndex = 0;
List<Stitch> tempStitches = new List<Stitch>();
Stitch prevStitch = null;
bool firstStitchOfBlock = true;
while (!thisPartIsDone)
{
byte val1;
Expand Down Expand Up @@ -215,20 +217,23 @@ private void OpenFile(string filename)
curBlock.colorIndex = colorIndex;
curBlock.color = getColorFromIndex(colorIndex);
//read useless(?) byte
// The value of this 'useless' byte seems to alternate
// between 2 and 1 for every other block. The only
// exception I've noted is the last block which appears
// to always be 0.
// The value of this 'useless' byte seems to start with 2 for the first block and
// alternate between 2 and 1 for every other block after that. The only exception
// I've noted is the last block which appears to always be 0.
curBlock.unknownStartByte = fileIn.ReadByte();
blocks.Add(curBlock);

firstStitchOfBlock = true;

tempStitches = new List<Stitch>();
}
else
{
int deltaX = 0;
int deltaY = 0;
int extraBits1 = 0x00;
Stitch.MoveBitSize xMoveBits;
Stitch.MoveBitSize yMoveBits;
if ((val1 & 0x80) == 0x80)
{
// Save the top 4 bits to output with debug info
Expand All @@ -241,6 +246,8 @@ private void OpenFile(string filename)
// of up to +2047 or -2048.
deltaX = get12Bit2sComplement(val1, val2);

xMoveBits = Stitch.MoveBitSize.TwelveBits;

// The X value used both bytes, so read next byte
// for Y value.
val1 = fileIn.ReadByte();
Expand All @@ -251,6 +258,8 @@ private void OpenFile(string filename)
// of up to +63 or -64.
deltaX = get7Bit2sComplement(val1);

xMoveBits = Stitch.MoveBitSize.SevenBits;

// The X value only used 1 byte, so copy the second
// to to the first for Y value.
val1 = val2;
Expand All @@ -269,24 +278,71 @@ private void OpenFile(string filename)
// Read in the next byte to get the full value
val2 = fileIn.ReadByte();
deltaY = get12Bit2sComplement(val1, val2);

yMoveBits = Stitch.MoveBitSize.TwelveBits;
}
else
{
// This is a 7-bit int. Allows for needle movement
// of up to +63 or -64.
deltaY = get7Bit2sComplement(val1);

yMoveBits = Stitch.MoveBitSize.SevenBits;
// Finished reading data for this stitch, no more
// bytes needed.
}

Stitch.StitchType stitchType;
if (deltaX == 0 && deltaY == 0)
{
// A stitch with zero movement seems to indicate the current and next stitches are movement only.
// Almost always occurs at the end of a block.
stitchType = Stitch.StitchType.MovementBeginAnchor;
}
else if (extraBits1 == 0x20 && extraBits2 == 0x20)
{
// A stitch with extrabits 0x20 seems to indicate the current and next stitches are movement only.
// Almost always occurs within a block, not the beginning or end.
stitchType = Stitch.StitchType.MovementOnly;
}
else if (extraBits1 == 0x10 && extraBits2 == 0x10)
{
// A stitch with extrabits 0x10 seems to indicate the current stitch is movement only.
// Almost always occurs at the beginning of a block.
stitchType = Stitch.StitchType.MovementEndAnchor;
}
else if (prevStitch != null &&
(prevStitch.stitchType == Stitch.StitchType.MovementOnly ||
prevStitch.stitchType == Stitch.StitchType.MovementBeginAnchor))
{
stitchType = Stitch.StitchType.MovementEndAnchor;
}
else
{
if (firstStitchOfBlock)
{
// First stitch of block is usually a movement to position the needle in the block
firstStitchOfBlock = false;
stitchType = Stitch.StitchType.MovementEndAnchor;
}
else
{
// Assume everything else is a normal stitch
stitchType = Stitch.StitchType.NormalStitch;
}
}

// Add stitch to list
tempStitches.Add(
new Stitch(
prevStitch = new Stitch(
new Point(prevX, prevY),
new Point(prevX + deltaX, prevY + deltaY),
extraBits1,
extraBits2
)
);
extraBits2,
xMoveBits,
yMoveBits,
stitchType
);
tempStitches.Add(prevStitch);

// Calculate new "previous" position
prevX = prevX + deltaX;
Expand Down Expand Up @@ -346,15 +402,18 @@ public string GetFileName()
public string saveDebugInfo()
{
string retval = System.IO.Path.ChangeExtension(_filename, ".txt");
System.IO.StreamWriter outfile = new System.IO.StreamWriter(retval);
outfile.Write(getDebugInfo());
outfile.Close();
using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(retval))
{
outfile.Write(getDebugInfo());
outfile.Close();
}
return retval;
}

public string getDebugInfo()
{
System.IO.StringWriter outfile = new System.IO.StringWriter();
outfile.WriteLine(_filename);
outfile.WriteLine("PES header");
outfile.WriteLine("PES version:\t" + pesVersion);

Expand All @@ -381,7 +440,7 @@ public string getDebugInfo()
outfile.WriteLine("unknown start byte: " + thisBlock.unknownStartByte.ToString("X2"));
foreach (Stitch thisStitch in thisBlock.stitches)
{
string tempLine = thisStitch.a.ToString() + " - " + thisStitch.b.ToString() + ", length " + thisStitch.calcLength();
string tempLine = thisStitch.a.ToString() + " - " + thisStitch.b.ToString() + ", length " + thisStitch.calcLength().ToString("F02");
if (thisStitch.extraBits1 != 0x00)
{
tempLine += " (extra bits 1: " + thisStitch.extraBits1.ToString("X2") + ")";
Expand All @@ -390,6 +449,27 @@ public string getDebugInfo()
{
tempLine += " (extra bits 2: " + thisStitch.extraBits2.ToString("X2") + ")";
}

if(thisStitch.XMoveBits == Stitch.MoveBitSize.SevenBits)
{
tempLine += " (7 bit X move)";
}
else if(thisStitch.XMoveBits == Stitch.MoveBitSize.TwelveBits)
{
tempLine += " (12 bit X move)";
}

if (thisStitch.YMoveBits == Stitch.MoveBitSize.SevenBits)
{
tempLine += " (7 bit Y move)";
}
else if (thisStitch.YMoveBits == Stitch.MoveBitSize.TwelveBits)
{
tempLine += " (12 bit Y move)";
}

tempLine += " (type " + thisStitch.stitchType + ")";

outfile.WriteLine(tempLine);
}
blockCount++;
Expand Down Expand Up @@ -458,9 +538,15 @@ public Bitmap designToBitmap(Single threadThickness, bool filterUglyStitches, do

foreach(StitchBlock thisBlock in blocks)
{
// Skip this block if it doesn't have stitches, for any reason
if(thisBlock.stitches.Length == 0)
{
continue;
}

// Set new color for this block
tempPen.Color = thisBlock.color;

foreach (Stitch thisStitch in thisBlock.stitches)
{
if (filterUglyStitches && // Check for filter ugly stitches option
Expand All @@ -470,6 +556,30 @@ public Bitmap designToBitmap(Single threadThickness, bool filterUglyStitches, do
// This stitch is too long, so skip it
continue;
}

// Style special stitches differently
// TODO: Finish figuring out the remaining stitch types
/*if (thisStitch.stitchType == Stitch.StitchType.MovementBeginAnchor)
{
tempPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
tempPen.Width = tempThreadThickness * 0.5f;
}
else if (thisStitch.stitchType == Stitch.StitchType.MovementOnly)
{
tempPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
tempPen.Width = tempThreadThickness * 0.5f;
}
else if(thisStitch.stitchType == Stitch.StitchType.MovementEndAnchor)
{
tempPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
tempPen.Width = tempThreadThickness * 0.5f;
}
else if (thisStitch.stitchType == Stitch.StitchType.NormalStitch)
{
tempPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
tempPen.Width = tempThreadThickness;
}*/

Point tempA = new Point((int)(thisStitch.a.X * scale), (int)(thisStitch.a.Y * scale));
Point tempB = new Point((int)(thisStitch.b.X * scale), (int)(thisStitch.b.Y * scale));
xGraph.DrawLine(tempPen, tempA, tempB);
Expand Down
4 changes: 2 additions & 2 deletions PesFile/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ A copy of the full GPL 2 license can be found in the docs directory.
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.0.0")]
25 changes: 24 additions & 1 deletion PesFile/Stitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,47 @@ namespace PesFile
{
public class Stitch
{
public enum MoveBitSize
{
Unknown,
SevenBits,
TwelveBits
};

public enum StitchType
{
Unknown,
NormalStitch,
MovementBeginAnchor,
MovementEndAnchor,
MovementOnly
}

public Point a;
public Point b;
// Extra bits to output with debug info
public int extraBits1;
public int extraBits2;
// Was this stitch represented as 7 bit move or 12 bit move in the file
public MoveBitSize XMoveBits;
public MoveBitSize YMoveBits;
public StitchType stitchType;

public Stitch(Point pointA, Point pointB)
{
this.a = pointA;
this.b = pointB;
}

public Stitch(Point pointA, Point pointB, int extraBits1, int extraBits2)
public Stitch(Point pointA, Point pointB, int extraBits1, int extraBits2, MoveBitSize XMoveBits, MoveBitSize YMoveBits, StitchType stitchType)
{
this.a = pointA;
this.b = pointB;
this.extraBits1 = extraBits1;
this.extraBits2 = extraBits2;
this.XMoveBits = XMoveBits;
this.YMoveBits = YMoveBits;
this.stitchType = stitchType;
}

public double calcLength()
Expand Down
4 changes: 2 additions & 2 deletions embroideryInfo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ A copy of the full GPL 2 license can be found in the docs directory.
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
4 changes: 2 additions & 2 deletions embroideryReader/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ A copy of the full GPL 2 license can be found in the docs directory.
// Build Number
// Revision
//
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]

0 comments on commit ea38b44

Please sign in to comment.