2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Brother PES Image Format %
20 % Copyright 1999-2012 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[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,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=(int) ReadBlobLSBLong(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=(int) ReadBlobByte(image);
508 blocks[i].color=PESColor+j;
511 for ( ; i < 256L; i++)
513 if (DiscardBlobBytes(image,532L-number_colors-21) == MagickFalse)
514 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
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 >= (ssize_t) 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=(size_t) 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) FormatLocaleFile(file,"<?xml version=\"1.0\"?>\n");
630 (void) FormatLocaleFile(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 < (ssize_t) number_blocks; i++)
637 offset=blocks[i].offset;
638 (void) FormatLocaleFile(file," <path stroke=\"#%02x%02x%02x\" "
639 "fill=\"none\" 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 < (ssize_t) (blocks[i+1].offset-offset); j++)
643 (void) FormatLocaleFile(file," L %g %g",stitches[offset+j].x-bounds.x1,
644 stitches[offset+j].y-bounds.y1);
645 (void) FormatLocaleFile(file,"\"/>\n");
647 (void) FormatLocaleFile(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) FormatLocaleString(read_info->filename,MaxTextExtent,"svg:%s",
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 % size_t RegisterPESImage(void)
695 ModuleExport size_t 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");