2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Brother PES Image Format %
20 % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % The PES format was derived from Robert Heel's PHP script (see
37 % http://bobosch.dyndns.org/embroidery/showFile.php?pes.php) and pesconvert
38 % (see http://torvalds-family.blogspot.com/2010/01/embroidery-gaah.html).
45 #include "MagickCore/studio.h"
46 #include "MagickCore/property.h"
47 #include "MagickCore/blob.h"
48 #include "MagickCore/blob-private.h"
49 #include "MagickCore/cache.h"
50 #include "MagickCore/client.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/gem.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/montage.h"
66 #include "MagickCore/resize.h"
67 #include "MagickCore/shear.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/module.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/transform.h"
74 #include "MagickCore/utility.h"
79 typedef struct _PESColorInfo
88 typedef struct _PESBlockInfo
100 static const PESColorInfo
111 { 228, 154, 203, 1 },
113 { 157, 214, 125, 1 },
119 { 168, 168, 168, 1 },
121 { 255, 255, 179, 1 },
130 { 252, 187, 196, 1 },
132 { 240, 240, 240, 1 },
134 { 168, 221, 196, 1 },
137 { 255, 240, 141, 1 },
142 { 135, 135, 135, 1 },
143 { 216, 202, 198, 1 },
145 { 254, 227, 197, 1 },
146 { 249, 147, 188, 1 },
148 { 178, 175, 212, 1 },
149 { 104, 106, 176, 1 },
150 { 239, 227, 185, 1 },
156 { 168, 222, 235, 1 },
160 { 253, 217, 222, 1 },
163 { 240, 249, 112, 1 },
165 { 255, 200, 100, 1 },
166 { 255, 200, 150, 1 },
167 { 255, 200, 200, 1 },
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % IsPES() returns MagickTrue if the image format type, identified by the
373 % magick string, is PES.
375 % The format of the IsPES method is:
377 % MagickBooleanType IsPES(const unsigned char *magick,const size_t length)
379 % A description of each parameter follows:
381 % o magick: compare image format pattern against these bytes.
383 % o length: Specifies the length of the magick string.
386 static MagickBooleanType IsPES(const unsigned char *magick,const size_t length)
390 if (LocaleNCompare((const char *) magick,"#PES",4) == 0)
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % R e a d P E S I m a g e %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % ReadPESImage() reads a Brother PES image file and returns it. It allocates
407 % the memory necessary for the new Image structure and returns a pointer to
410 % The format of the ReadPESImage method is:
412 % image=ReadPESImage(image_info)
414 % A description of each parameter follows:
416 % o image_info: the image info.
418 % o exception: return any errors or warnings in this structure.
421 static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
424 filename[MagickPathExtent];
474 assert(image_info != (const ImageInfo *) NULL);
475 assert(image_info->signature == MagickCoreSignature);
476 if (image_info->debug != MagickFalse)
477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
478 image_info->filename);
479 assert(exception != (ExceptionInfo *) NULL);
480 assert(exception->signature == MagickCoreSignature);
481 image=AcquireImage(image_info,exception);
482 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
483 if (status == MagickFalse)
485 image=DestroyImageList(image);
486 return((Image *) NULL);
489 Verify PES identifier.
491 count=ReadBlob(image,4,magick);
492 if ((count != 4) || (LocaleNCompare((char *) magick,"#PES",4) != 0))
493 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
494 count=ReadBlob(image,4,version);
495 offset=ReadBlobLSBSignedLong(image);
496 if (DiscardBlobBytes(image,offset+36) == MagickFalse)
497 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
499 if (EOFBlob(image) != MagickFalse)
500 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
504 number_colors=(size_t) ReadBlobByte(image)+1;
505 for (i=0; i < (ssize_t) number_colors; i++)
507 j=ReadBlobByte(image);
508 blocks[i].color=PESColor+(j < 0 ? 0 : j);
511 for ( ; i < 256L; i++)
514 blocks[i].color=PESColor;
516 if (DiscardBlobBytes(image,532L-number_colors-21) == MagickFalse)
517 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
519 if (EOFBlob(image) != MagickFalse)
520 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
525 stitches=(PointInfo *) AcquireQuantumMemory(number_stitches,
527 if (stitches == (PointInfo *) NULL)
528 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
531 bounds.x2=(-65535.0);
532 bounds.y2=(-65535.0);
537 while (EOFBlob(image) != EOF)
539 x=ReadBlobByte(image);
540 y=ReadBlobByte(image);
541 if ((x == 0xff) && (y == 0))
543 if ((x == 254) && (y == 176))
546 Start a new stitch block.
549 blocks[j].offset=(ssize_t) i;
551 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
552 (void) ReadBlobByte(image);
568 x=((x & 0x0f) << 8)+y;
569 if ((x & 0x800) != 0)
571 y=ReadBlobByte(image);
586 y=((y & 0x0f) << 8)+ReadBlobByte(image);
587 if ((y & 0x800) != 0)
597 stitches[i].x=(double) x;
598 stitches[i].y=(double) y;
599 if ((double) x < bounds.x1)
600 bounds.x1=(double) x;
601 if ((double) x > bounds.x2)
602 bounds.x2=(double) x;
603 if ((double) y < bounds.y1)
604 bounds.y1=(double) y;
605 if ((double) y > bounds.y2)
606 bounds.y2=(double) y;
608 if (i >= (ssize_t) number_stitches)
611 Make room for more stitches.
614 stitches=(PointInfo *) ResizeQuantumMemory(stitches,(size_t)
615 number_stitches,sizeof(*stitches));
616 if (stitches == (PointInfo *) NULL)
617 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
621 blocks[j].offset=(ssize_t) i;
622 number_blocks=(size_t) j;
624 Write stitches as SVG file.
627 unique_file=AcquireUniqueFileResource(filename);
628 if (unique_file != -1)
629 file=fdopen(unique_file,"wb");
630 if ((unique_file == -1) || (file == (FILE *) NULL))
631 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
632 (void) FormatLocaleFile(file,"<?xml version=\"1.0\"?>\n");
633 (void) FormatLocaleFile(file,"<svg xmlns=\"http://www.w3.org/2000/svg\" "
634 "xlink=\"http://www.w3.org/1999/xlink\" "
635 "ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" "
636 "baseProfile=\"full\" width=\"%g\" height=\"%g\">\n",bounds.x2-bounds.x1,
637 bounds.y2-bounds.y1);
638 for (i=0; i < (ssize_t) number_blocks; i++)
640 offset=blocks[i].offset;
641 (void) FormatLocaleFile(file," <path stroke=\"#%02x%02x%02x\" "
642 "fill=\"none\" d=\"M %g %g",blocks[i].color->red,blocks[i].color->green,
643 blocks[i].color->blue,stitches[offset].x-bounds.x1,
644 stitches[offset].y-bounds.y1);
645 for (j=1; j < (ssize_t) (blocks[i+1].offset-offset); j++)
646 (void) FormatLocaleFile(file," L %g %g",stitches[offset+j].x-bounds.x1,
647 stitches[offset+j].y-bounds.y1);
648 (void) FormatLocaleFile(file,"\"/>\n");
650 (void) FormatLocaleFile(file,"</svg>\n");
652 (void) CloseBlob(image);
653 image=DestroyImage(image);
657 read_info=CloneImageInfo(image_info);
658 SetImageInfoBlob(read_info,(void *) NULL,0);
659 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"svg:%s",
661 image=ReadImage(read_info,exception);
662 if (image != (Image *) NULL)
664 (void) CopyMagickString(image->filename,image_info->filename,
666 (void) CopyMagickString(image->magick_filename,image_info->filename,
668 (void) CopyMagickString(image->magick,"PES",MagickPathExtent);
670 read_info=DestroyImageInfo(read_info);
671 (void) RelinquishUniqueFileResource(filename);
672 return(GetFirstImageInList(image));
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % R e g i s t e r P E S I m a g e %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % RegisterPESImage() adds attributes for the PES image format to
687 % the list of supported formats. The attributes include the image format
688 % tag, a method to read and/or write the format, whether the format
689 % supports the saving of more than one frame to the same file or blob,
690 % whether the format supports native in-memory I/O, and a brief
691 % description of the format.
693 % The format of the RegisterPESImage method is:
695 % size_t RegisterPESImage(void)
698 ModuleExport size_t RegisterPESImage(void)
703 entry=AcquireMagickInfo("PES","PES","Embrid Embroidery Format");
704 entry->decoder=(DecodeImageHandler *) ReadPESImage;
705 entry->magick=(IsImageFormatHandler *) IsPES;
706 (void) RegisterMagickInfo(entry);
707 return(MagickImageCoderSignature);
711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 % U n r e g i s t e r P E S I m a g e %
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 % UnregisterPESImage() removes format registrations made by the
722 % PES module from the list of supported formats.
724 % The format of the UnregisterPESImage method is:
726 % UnregisterPESImage(void)
729 ModuleExport void UnregisterPESImage(void)
731 (void) UnregisterMagickInfo("PES");