2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Brother PES Image Format %
20 % Copyright 1999-2010 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 "magick/studio.h"
46 #include "magick/property.h"
47 #include "magick/blob.h"
48 #include "magick/blob-private.h"
49 #include "magick/cache.h"
50 #include "magick/client.h"
51 #include "magick/colorspace.h"
52 #include "magick/constitute.h"
53 #include "magick/decorate.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/gem.h"
57 #include "magick/geometry.h"
58 #include "magick/image.h"
59 #include "magick/image-private.h"
60 #include "magick/list.h"
61 #include "magick/magick.h"
62 #include "magick/memory_.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/montage.h"
66 #include "magick/resize.h"
67 #include "magick/shear.h"
68 #include "magick/quantum-private.h"
69 #include "magick/static.h"
70 #include "magick/string_.h"
71 #include "magick/module.h"
72 #include "magick/resource_.h"
73 #include "magick/transform.h"
74 #include "magick/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[MaxTextExtent];
474 assert(image_info != (const ImageInfo *) NULL);
475 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
481 image=AcquireImage(image_info);
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=(ssize_t) ReadBlobLSBLong(image);
496 for (i=0; i < (offset+36); i++)
497 if (ReadBlobByte(image) == EOF)
499 if (EOFBlob(image) != MagickFalse)
500 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
504 number_colors=(unsigned long) ReadBlobByte(image)+1;
505 for (i=0; i < (long) number_colors; i++)
507 j=(int) ReadBlobByte(image);
508 blocks[i].color=PESColor+j;
511 for ( ; i < 256L; i++)
513 for (i=0; i < (long) (532L-number_colors-21); i++)
514 if (ReadBlobByte(image) == EOF)
516 if (EOFBlob(image) != MagickFalse)
517 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
522 stitches=(PointInfo *) AcquireQuantumMemory(number_stitches,
524 if (stitches == (PointInfo *) NULL)
525 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
528 bounds.x2=(-65535.0);
529 bounds.y2=(-65535.0);
534 while (EOFBlob(image) != EOF)
536 x=(int) ReadBlobByte(image);
537 y=(int) ReadBlobByte(image);
538 if ((x == 0xff) && (y == 0))
540 if ((x == 254) && (y == 176))
543 Start a new stitch block.
546 blocks[j].offset=(ssize_t) i;
548 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
549 (void) ReadBlobByte(image);
565 x=((x & 0x0f) << 8)+y;
566 if ((x & 0x800) != 0)
568 y=ReadBlobByte(image);
583 y=((y & 0x0f) << 8)+ReadBlobByte(image);
584 if ((y & 0x800) != 0)
594 stitches[i].x=(double) x;
595 stitches[i].y=(double) y;
596 if ((double) x < bounds.x1)
597 bounds.x1=(double) x;
598 if ((double) x > bounds.x2)
599 bounds.x2=(double) x;
600 if ((double) y < bounds.y1)
601 bounds.y1=(double) y;
602 if ((double) y > bounds.y2)
603 bounds.y2=(double) y;
605 if (i >= (long) number_stitches)
608 Make room for more stitches.
611 stitches=(PointInfo *) ResizeQuantumMemory(stitches,(size_t)
612 number_stitches,sizeof(*stitches));
613 if (stitches == (PointInfo *) NULL)
614 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
618 blocks[j].offset=(ssize_t) i;
619 number_blocks=(unsigned long) j;
621 Write stitches as SVG file.
624 unique_file=AcquireUniqueFileResource(filename);
625 if (unique_file != -1)
626 file=fdopen(unique_file,"wb");
627 if ((unique_file == -1) || (file == (FILE *) NULL))
628 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
629 (void) fprintf(file,"<?xml version=\"1.0\"?>\n");
630 (void) fprintf(file,"<svg xmlns=\"http://www.w3.org/2000/svg\" "
631 "xlink=\"http://www.w3.org/1999/xlink\" "
632 "ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" "
633 "baseProfile=\"full\" width=\"%g\" height=\"%g\">\n",bounds.x2-bounds.x1,
634 bounds.y2-bounds.y1);
635 for (i=0; i < (long) number_blocks; i++)
637 offset=blocks[i].offset;
638 (void) fprintf(file," <path stroke=\"#%02x%02x%02x\" fill=\"none\" "
639 "d=\"M %g %g",blocks[i].color->red,blocks[i].color->green,
640 blocks[i].color->blue,stitches[offset].x-bounds.x1,
641 stitches[offset].y-bounds.y1);
642 for (j=1; j < (long) (blocks[i+1].offset-offset); j++)
643 (void) fprintf(file," L %g %g",stitches[offset+j].x-bounds.x1,
644 stitches[offset+j].y-bounds.y1);
645 (void) fprintf(file,"\"/>\n");
647 (void) fprintf(file,"</svg>\n");
649 (void) CloseBlob(image);
650 image=DestroyImage(image);
654 read_info=CloneImageInfo(image_info);
655 SetImageInfoBlob(read_info,(void *) NULL,0);
656 (void) FormatMagickString(read_info->filename,MaxTextExtent,"svg:%.1024s",
658 image=ReadImage(read_info,exception);
659 if (image != (Image *) NULL)
661 (void) CopyMagickString(image->filename,image_info->filename,
663 (void) CopyMagickString(image->magick_filename,image_info->filename,
665 (void) CopyMagickString(image->magick,"PES",MaxTextExtent);
667 read_info=DestroyImageInfo(read_info);
668 (void) RelinquishUniqueFileResource(filename);
669 return(GetFirstImageInList(image));
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % R e g i s t e r P E S I m a g e %
681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % RegisterPESImage() adds attributes for the PES image format to
684 % the list of supported formats. The attributes include the image format
685 % tag, a method to read and/or write the format, whether the format
686 % supports the saving of more than one frame to the same file or blob,
687 % whether the format supports native in-memory I/O, and a brief
688 % description of the format.
690 % The format of the RegisterPESImage method is:
692 % unsigned long RegisterPESImage(void)
695 ModuleExport unsigned long RegisterPESImage(void)
700 entry=SetMagickInfo("PES");
701 entry->decoder=(DecodeImageHandler *) ReadPESImage;
702 entry->magick=(IsImageFormatHandler *) IsPES;
703 entry->description=ConstantString("Embrid Embroidery Format");
704 entry->module=ConstantString("PES");
705 (void) RegisterMagickInfo(entry);
706 return(MagickImageCoderSignature);
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714 % U n r e g i s t e r P E S I m a g e %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % UnregisterPESImage() removes format registrations made by the
721 % PES module from the list of supported formats.
723 % The format of the UnregisterPESImage method is:
725 % UnregisterPESImage(void)
728 ModuleExport void UnregisterPESImage(void)
730 (void) UnregisterMagickInfo("PES");