Skip to content
tatarize edited this page Nov 12, 2017 · 6 revisions

Writing PEC

Once the PES is written the PEC block is needed. It is the same for all versions, including the colors which use the PECThreads.

In versions of PES after 4 with the advent of thread descriptions in the header, these color codes are overloaded the loading routine in most software. Most all loaders will use a hybrid of the PES header for the colors, and the PEC block for the color values. This makes some technically acceptable actions problematic.

Consideration of the Wilcom Bug

A bug in Wilcom's reading of how to make this valid leads to two different interpretations.

Properly in PE-Design, if you reuse the same thread it doesn't add another thread entry to the thread table at the start. Rather it lists the unique threads. So if you used black, white, black, white, red, black, white. Brother would say you used 3 threads and define them in the header. In the PEC file it would list these 14 1D 14 1D 05 14 1D. Assigning the first thread value to 14, the second to 1D, and the third finally to 05.

Wilcom however, loads the thread table as three threads black, white, red, then completely ignores the order listed in the PEC table and uses them in a cycle coloring them black, red, white, black, red, white, black. This bug is easily duplicated.

When saving PE-Design will save the unique threads. When saving Wilcom will save all threads, in a list, repeating them as needed. And it will pigeon hole colors into being the nearest relevant color, when writing the PEC. However, Wilcom writes the PES file with the table colors referring to the header, so PE-Design loads these correctly. However they are previewed by PE-Design as wrong when you check the Open Dialog.

So when these colors get pigeon-holed into the same PECThread index value but refer to different colors, PE-Design will interpret them incorrectly, in the rare preview of the stitches. However, if we only provide the unique colors table as PES properly requires, Wilcom will read these in error if there a duplicate color, prior to exhausting all unique colors.

The embroidery machine will use those PEC colors for the preview screens, and accept that when a color change happens that it moves to the next needle. If #17 is the closest color to the first thread, so it calls that 17. And #17 is closest color to the second thread, it should not use #17 again. But, properly PE-Design takes #17 to always refer to the same thread no matter what. And Wilcom won't care, and just cycle to the next thread in it's chart.

The problem here is a bit of a catch-22. If you read/write the files properly, there will be a rather notable incompatibility with Wilcom. And it'll work fine with PE-Design. If you read/write the files in Wilcom's way, PE-Design will load the PES blocks fine and seem to work without issue outside of preview on load, and Wilcom will also work correctly for the most part, even though it's technically still wrong and will suffer in a few minor ways.

For max compatibility export the correct PES file with unique threads in the header, and detect the error when the number of threads is equal to the number of colors listed in the pec section. If the threads in the header is equal to the colors indexed in the PEC section, then use 1 to 1, if there are fewer colors in the header, properly index them as PE-Design expects.

Write PEC Elements

  • Write "LA:"
  • Write 20 bytes, name, this is typically truncated at 8 characters, padded with space (0x20)
  • Write 1 byte, 0x0D
  • Write 12 bytes, 12 copies of 0x20
  • Write 1 byte, 0xFF
  • Write 1 byte, 0x00
  • Write 1 byte, 0x06, Graphics size information.
  • Write 1 byte, 0x26, Graphics size information.
  • Write 12 bytes, 0x20, 12 spaces.
  • Write 1 byte, #Color_Changes, (n) number of color changes.
  • Write n bytes, #Index Color each change.
  • Write (463-n) spaces, that is to say pad color changes to 463 in total. (This is 512 bytes from the start of the PEC FILE)
  • Write 2 bytes, 0x00 0x00, two bytes of zeros.
  • Write 3 bytes, (NOTE: 3 bytes) position of graphics offset value. (20 + PEC_ENCODE)
  • Write 1 byte: 0x31
  • Write 1 byte: 0xFF
  • Write 1 byte: 0xF0
  • Write 2 bytes, Width.
  • Write 2 bytes, Height.
  • Write 2 bytes, 0x01E0
  • Write 2 bytes, 0x01B0
  • Write 2 bytes BIGENDIAN 0x9000 - minX
  • Write 2 bytes BIGENDIAN 0x9000 - minY
  • Write Pec Encoded stitches.
  • Write Graphics Objects.

Write Pec Encoded Stitches.

Though it might be technically right and there are documents with mixed length stitches, if the X or Y is long form they both should be, just as both X and Y get flagged for jumps and trims. It's unknown which of these are technically valid and on which machines.

Stitch

Pec stitches are encoded in short form and long form. Short form stitches are 2 bytes. X, Y. These are 7 bit signed integers. 0b0???????, 0b0???????

If the topmost bit is set, the next byte is also read as part of the coordinate: 0b1---????_???????? the topmost nibble is command data, and the bottom 12 bits are coordinate data.

Jump

Jumps are long form stitches with the command bit for Jump set. 0b0001????_????????

Trim

Trim commands are long form stitches with the command bit for Trim set. 0b0010????_????????

Color Change:

Write 1 byte: 0xfe Write 1 byte: 0xb0 Write 1 byte: 2, 1, 2, 1, 2, 1... alternating back and forth.

END

Write 1 byte: 0xFF

Write Graphics

  • 1 copy of blank.
  • N copies of blank. (where N is the number of colors used).

Blank Graphics:

(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xF0, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x0F, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0xF0, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x0F, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00