public PesFile(string filePath) { if (filePath == "") { throw new ArgumentNullException("filePath"); } if (!File.Exists(filePath)) { throw new FileNotFoundException("File not found.", filePath); } _filePath = filePath; _colors = new List <int>(); _blocks = new List <StitchBlock>(); log.InfoFormat("Reading file: {0}", _filePath); BinaryReader pesData = new BinaryReader(File.OpenRead(_filePath)); string pesHeader = new string(pesData.ReadChars(4)); log.DebugFormat("Header string: {0}", pesHeader); if (pesHeader != "#PES") { throw new FileLoadException("The specified file is not a valid PES file.", _filePath); } _pesVersion = new string(pesData.ReadChars(4)); log.DebugFormat("Pes version: {0}", _pesVersion); uint pecStart = pesData.ReadUInt32(); log.DebugFormat("Pec start: {0}", pecStart); pesData.BaseStream.Position = pecStart + 3; _internalName = new string(pesData.ReadChars(16)); _internalName = _internalName.Trim(); log.DebugFormat("Internal name: {0}", _internalName); pesData.BaseStream.Position = pecStart + 48; int colorCount = pesData.ReadByte() + 1; log.DebugFormat("Color count: {0}", colorCount); log.Info("Reading color data..."); for (int i = 0; i < colorCount; i++) { _colors.Add(pesData.ReadByte()); } pesData.BaseStream.Position = pecStart + 514; uint pecLength = pesData.ReadUInt16(); uint pecEnd = pecStart + 514 + pecLength; log.DebugFormat("Pec Length: {0}", pecLength); log.DebugFormat("Pec End: {0}", pecEnd); pesData.BaseStream.Position = pecStart + 520; _width = pesData.ReadUInt16(); _height = pesData.ReadUInt16(); log.DebugFormat("Width: {0}cm", _width); log.DebugFormat("Height: {0}cm", _height); pesData.BaseStream.Position = pecStart + 532; int bx, by, dx, dy, x, y, mx, my, nx, ny; bx = by = dx = dy = x = y = mx = my = nx = ny = 0; //int x = Convert.ToInt16(_width) / 2; //int y = Convert.ToInt16(_height) / 2; int c = 0; bool jmp = false; StitchBlock sb = new StitchBlock(ThreadColor.FromIndex(_colors[c])); log.Info("Reading stitch data..."); while (pesData.BaseStream.Position < pecEnd) { bx = pesData.ReadByte(); by = pesData.ReadByte(); //log.DebugFormat("Bytes x={0}, y={1}", bx, by); if (bx == 255 && by == 0) { //log.Info("End of stitch marker"); _blocks.Add(sb); break; } else if (bx == 254 && by == 176) { //log.Info("End of color block."); c++; _blocks.Add(sb); sb = new StitchBlock(ThreadColor.FromIndex(_colors[c])); pesData.BaseStream.Position++; } else { //Regular stitch dx = dy = 0; if ((bx & 128) == 128)//$80 { //log.Info("Jump stitch on x"); dx = ((bx & 15) * 256) + by; dx = ((dx & 2048) == 2048) ? (int)(dx | 4294963200) : dx; by = pesData.ReadByte(); jmp = true; } else { dx = (bx > 63) ? bx - 128 : bx; } if ((by & 128) == 128)//$80 { //log.Info("Jump stitch on y"); bx = pesData.ReadByte(); dy = ((by & 15) * 256) + bx; dy = ((dy & 2048) == 2048) ? (int)(dy | 4294963200) : dy; jmp = true; } else { //normal stitch dy = (by > 63) ? by - 128 : by; } //log.DebugFormat("Stitch point: dx={0}, dy={1}", dx, dy); x += dx; y += dy; nx = (x < nx) ? x : nx; ny = (y < ny) ? y : ny; mx = (x > mx) ? x : mx; my = (y > my) ? y : my; StitchType type = (jmp) ? StitchType.Jump : StitchType.Normal; sb.AddStitch(new Stitch(new Point(x, y), type)); jmp = false; } _pixelWidth = mx - nx; _pixelHeight = my - ny; _xOffset = -nx; _yOffset = -ny; } log.Info("Closing the file."); pesData.Close(); }
public PesFile(string filePath) { if (filePath == "") throw new ArgumentNullException("filePath"); if (!File.Exists(filePath)) throw new FileNotFoundException("File not found.", filePath); _filePath = filePath; _colors = new List<int>(); _blocks = new List<StitchBlock>(); log.InfoFormat("Reading file: {0}", _filePath); BinaryReader pesData = new BinaryReader(File.OpenRead(_filePath)); string pesHeader = new string(pesData.ReadChars(4)); log.DebugFormat("Header string: {0}", pesHeader); if (pesHeader != "#PES") throw new FileLoadException("The specified file is not a valid PES file.", _filePath); _pesVersion = new string(pesData.ReadChars(4)); log.DebugFormat("Pes version: {0}", _pesVersion); uint pecStart = pesData.ReadUInt32(); log.DebugFormat("Pec start: {0}", pecStart); pesData.BaseStream.Position = pecStart + 3; _internalName = new string(pesData.ReadChars(16)); _internalName = _internalName.Trim(); log.DebugFormat("Internal name: {0}", _internalName); pesData.BaseStream.Position = pecStart + 48; int colorCount = pesData.ReadByte() + 1; log.DebugFormat("Color count: {0}", colorCount); log.Info("Reading color data..."); for (int i = 0; i < colorCount; i++) _colors.Add(pesData.ReadByte()); pesData.BaseStream.Position = pecStart + 514; uint pecLength = pesData.ReadUInt16(); uint pecEnd = pecStart + 514 + pecLength; log.DebugFormat("Pec Length: {0}", pecLength); log.DebugFormat("Pec End: {0}", pecEnd); pesData.BaseStream.Position = pecStart + 520; _width = pesData.ReadUInt16(); _height = pesData.ReadUInt16(); log.DebugFormat("Width: {0}cm", _width); log.DebugFormat("Height: {0}cm", _height); pesData.BaseStream.Position = pecStart + 532; int bx, by, dx, dy, x, y, mx, my, nx, ny; bx = by = dx = dy = x = y = mx = my = nx = ny = 0; //int x = Convert.ToInt16(_width) / 2; //int y = Convert.ToInt16(_height) / 2; int c = 0; bool jmp = false; StitchBlock sb = new StitchBlock( ThreadColor.FromIndex(_colors[c])); log.Info("Reading stitch data..."); while(pesData.BaseStream.Position < pecEnd) { bx = pesData.ReadByte(); by = pesData.ReadByte(); //log.DebugFormat("Bytes x={0}, y={1}", bx, by); if (bx == 255 && by == 0) { //log.Info("End of stitch marker"); _blocks.Add(sb); break; } else if (bx == 254 && by == 176) { //log.Info("End of color block."); c++; _blocks.Add(sb); sb = new StitchBlock(ThreadColor.FromIndex(_colors[c])); pesData.BaseStream.Position++; } else { //Regular stitch dx = dy = 0; if ((bx & 128) == 128)//$80 { //log.Info("Jump stitch on x"); dx = ((bx & 15) * 256) + by; dx = ((dx & 2048) == 2048) ? (int) (dx | 4294963200) : dx; by = pesData.ReadByte(); jmp = true; } else { dx = (bx > 63) ? bx - 128 : bx; } if ((by & 128) == 128)//$80 { //log.Info("Jump stitch on y"); bx = pesData.ReadByte(); dy = ((by & 15) * 256) + bx; dy = ((dy & 2048) == 2048) ? (int)(dy | 4294963200) : dy; jmp = true; } else { //normal stitch dy = (by > 63) ? by - 128 : by; } //log.DebugFormat("Stitch point: dx={0}, dy={1}", dx, dy); x += dx; y += dy; nx = (x < nx) ? x : nx; ny = (y < ny) ? y : ny; mx = (x > mx) ? x : mx; my = (y > my) ? y : my; StitchType type = (jmp) ? StitchType.Jump : StitchType.Normal; sb.AddStitch(new Stitch(new Point(x, y), type)); jmp = false; } _pixelWidth = mx - nx; _pixelHeight = my - ny; _xOffset = -nx; _yOffset = -ny; } log.Info("Closing the file."); pesData.Close(); }