2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Photo CD Image Format %
20 % Copyright 1999-2011 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/client.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/decorate.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/gem.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/montage.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/resize.h"
65 #include "MagickCore/shear.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/transform.h"
71 #include "MagickCore/utility.h"
76 static MagickBooleanType
77 WritePCDImage(const ImageInfo *,Image *);
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % D e c o d e I m a g e %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 % DecodeImage recovers the Huffman encoded luminance and chrominance
93 % The format of the DecodeImage method is:
95 % MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
96 % unsigned char *chroma1,unsigned char *chroma2)
98 % A description of each parameter follows:
100 % o image: the address of a structure of type Image.
102 % o luma: the address of a character buffer that contains the
103 % luminance information.
105 % o chroma1: the address of a character buffer that contains the
106 % chrominance information.
108 % o chroma2: the address of a character buffer that contains the
109 % chrominance information.
112 static MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
113 unsigned char *chroma1,unsigned char *chroma2)
115 #define IsSync ((sum & 0xffffff00UL) == 0xfffffe00UL)
116 #define PCDGetBits(n) \
118 sum=(sum << n) & 0xffffffff; \
122 if (p >= (buffer+0x800)) \
124 count=ReadBlob(image,0x800,buffer); \
127 sum|=((unsigned int) (*p) << (24-bits)); \
131 if (EOFBlob(image) != MagickFalse) \
135 typedef struct PCDTable
158 register unsigned char
178 Initialize Huffman tables.
180 assert(image != (const Image *) NULL);
181 assert(image->signature == MagickSignature);
182 if (image->debug != MagickFalse)
183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
184 assert(luma != (unsigned char *) NULL);
185 assert(chroma1 != (unsigned char *) NULL);
186 assert(chroma2 != (unsigned char *) NULL);
187 buffer=(unsigned char *) AcquireQuantumMemory(0x800,sizeof(*buffer));
188 if (buffer == (unsigned char *) NULL)
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
194 for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
197 length=(sum & 0xff)+1;
198 pcd_table[i]=(PCDTable *) AcquireQuantumMemory(length,
199 sizeof(*pcd_table[i]));
200 if (pcd_table[i] == (PCDTable *) NULL)
202 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
203 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
207 for (j=0; j < (ssize_t) length; j++)
210 r->length=(unsigned int) (sum & 0xff)+1;
213 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
217 r->sequence=(unsigned int) (sum & 0xffff) << 16;
219 r->key=(unsigned char) (sum & 0xff);
220 r->mask=(~((1U << (32-r->length))-1));
223 pcd_length[i]=(size_t) length;
226 Search for Sync byte.
228 for (i=0; i < 1; i++)
230 for (i=0; i < 1; i++)
232 while ((sum & 0x00fff000UL) != 0x00fff000UL)
237 Recover the Huffman encoded luminance and chrominance deltas.
249 Determine plane and row number.
252 row=((sum >> 9) & 0x1fff);
253 if (row == image->rows)
262 q=luma+row*image->columns;
263 count=(ssize_t) image->columns;
268 q=chroma1+(row >> 1)*image->columns;
269 count=(ssize_t) (image->columns >> 1);
275 q=chroma2+(row >> 1)*image->columns;
276 count=(ssize_t) (image->columns >> 1);
282 ThrowBinaryException(CorruptImageError,"CorruptImage",
286 length=pcd_length[plane];
290 Decode luminance or chrominance deltas.
293 for (i=0; ((i < (ssize_t) length) && ((sum & r->mask) != r->sequence)); i++)
295 if ((row > image->rows) || (r == (PCDTable *) NULL))
297 (void) ThrowMagickException(&image->exception,GetMagickModule(),
298 CorruptImageWarning,"SkipToSyncByte","`%s'",image->filename);
299 while ((sum & 0x00fff000) != 0x00fff000)
306 quantum=(ssize_t) (*q)+r->key;
308 quantum=(ssize_t) (*q)+r->key-256;
309 *q=(unsigned char) ((quantum < 0) ? 0 : (quantum > 255) ? 255 : quantum);
311 PCDGetBits(r->length);
315 Relinquish resources.
317 for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
318 pcd_table[i]=(PCDTable *) RelinquishMagickMemory(pcd_table[i]);
319 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % IsPCD() returns MagickTrue if the image format type, identified by the
335 % magick string, is PCD.
337 % The format of the IsPCD method is:
339 % MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
341 % A description of each parameter follows:
343 % o magick: compare image format pattern against these bytes.
345 % o length: Specifies the length of the magick string.
348 static MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
352 if (LocaleNCompare((const char *) magick+2048,"PCD_",4) == 0)
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 % R e a d P C D I m a g e %
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 % ReadPCDImage() reads a Photo CD image file and returns it. It
369 % allocates the memory necessary for the new Image structure and returns a
370 % pointer to the new image. Much of the PCD decoder was derived from
371 % the program hpcdtoppm(1) by Hadmut Danisch.
373 % The format of the ReadPCDImage method is:
375 % image=ReadPCDImage(image_info)
377 % A description of each parameter follows:
379 % o image_info: the image info.
381 % o exception: return any errors or warnings in this structure.
385 static inline size_t MagickMin(const size_t x,const size_t y)
392 static Image *OverviewImage(const ImageInfo *image_info,Image *image,
393 ExceptionInfo *exception)
405 Create the PCD Overview image.
407 for (p=image; p != (Image *) NULL; p=p->next)
409 (void) DeleteImageProperty(p,"label");
410 (void) SetImageProperty(p,"label",DefaultTileLabel);
412 montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
413 (void) CopyMagickString(montage_info->filename,image_info->filename,
415 montage_image=MontageImageList(image_info,montage_info,image,exception);
416 montage_info=DestroyMontageInfo(montage_info);
417 if (montage_image == (Image *) NULL)
418 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
419 image=DestroyImage(image);
420 return(montage_image);
423 static void Upsample(const size_t width,const size_t height,
424 const size_t scaled_width,unsigned char *pixels)
430 register unsigned char
436 Create a new image that is a integral size greater than an existing one.
438 assert(pixels != (unsigned char *) NULL);
439 for (y=0; y < (ssize_t) height; y++)
441 p=pixels+(height-1-y)*scaled_width+(width-1);
442 q=pixels+((height-1-y) << 1)*scaled_width+((width-1) << 1);
445 for (x=1; x < (ssize_t) width; x++)
450 *(q+1)=(unsigned char) ((((size_t) *p)+((size_t) *(p+1))+1) >> 1);
453 for (y=0; y < (ssize_t) (height-1); y++)
455 p=pixels+((size_t) y << 1)*scaled_width;
458 for (x=0; x < (ssize_t) (width-1); x++)
460 *q=(unsigned char) ((((size_t) *p)+((size_t) *r)+1) >> 1);
461 *(q+1)=(unsigned char) ((((size_t) *p)+((size_t) *(p+2))+
462 ((size_t) *r)+((size_t) *(r+2))+2) >> 2);
467 *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
468 *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
470 p=pixels+(2*height-2)*scaled_width;
471 q=pixels+(2*height-1)*scaled_width;
472 (void) CopyMagickMemory(q,p,(size_t) (2*width));
475 static Image *ReadPCDImage(const ImageInfo *image_info,ExceptionInfo *exception)
496 register unsigned char
524 assert(image_info != (const ImageInfo *) NULL);
525 assert(image_info->signature == MagickSignature);
526 if (image_info->debug != MagickFalse)
527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
528 image_info->filename);
529 assert(exception != (ExceptionInfo *) NULL);
530 assert(exception->signature == MagickSignature);
531 image=AcquireImage(image_info);
532 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
533 if (status == MagickFalse)
535 image=DestroyImageList(image);
536 return((Image *) NULL);
539 Determine if this a PCD file.
541 header=(unsigned char *) AcquireQuantumMemory(0x800,3UL*sizeof(*header));
542 if (header == (unsigned char *) NULL)
543 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
544 count=ReadBlob(image,3*0x800,header);
545 overview=LocaleNCompare((char *) header,"PCD_OPA",7) == 0;
547 ((LocaleNCompare((char *) header+0x800,"PCD",3) != 0) && !overview))
548 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
549 rotate=header[0x0e02] & 0x03;
550 number_images=(header[10] << 8) | header[11];
551 header=(unsigned char *) RelinquishMagickMemory(header);
553 Determine resolution by scene specification.
555 if ((image->columns == 0) || (image->rows == 0))
561 for (scene=1; scene < 6; scene++)
563 if ((width >= image->columns) && (height >= image->rows))
569 if (image_info->number_scenes != 0)
570 scene=(size_t) MagickMin(image_info->scene,6);
574 Initialize image structure.
578 for (i=1; i < (ssize_t) MagickMin(scene,3); i++)
583 image->columns=width;
586 for ( ; i < (ssize_t) scene; i++)
592 Allocate luma and chroma memory.
594 number_pixels=(MagickSizeType) image->columns*image->rows;
595 if (number_pixels != (size_t) number_pixels)
596 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
597 chroma1=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
599 chroma2=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
601 luma=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
603 if ((chroma1 == (unsigned char *) NULL) ||
604 (chroma2 == (unsigned char *) NULL) || (luma == (unsigned char *) NULL))
605 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
607 Advance to image data.
618 for (i=0; i < (ssize_t) (offset*0x800); i++)
619 (void) ReadBlobByte(image);
625 MagickProgressMonitor
632 Read thumbnails from overview image.
634 for (j=1; j <= (ssize_t) number_images; j++)
636 progress_monitor=SetImageProgressMonitor(image,
637 (MagickProgressMonitor) NULL,image->client_data);
638 (void) FormatLocaleString(image->filename,MaxTextExtent,
639 "images/img%04ld.pcd",(long) j);
640 (void) FormatLocaleString(image->magick_filename,MaxTextExtent,
641 "images/img%04ld.pcd",(long) j);
642 image->scene=(size_t) j;
643 image->columns=width;
649 for (y=0; y < (ssize_t) height; y+=2)
651 count=ReadBlob(image,width,yy);
653 count=ReadBlob(image,width,yy);
655 count=ReadBlob(image,width >> 1,c1);
657 count=ReadBlob(image,width >> 1,c2);
660 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
661 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
663 Transfer luminance and chrominance channels.
668 for (y=0; y < (ssize_t) image->rows; y++)
670 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
671 if (q == (const Quantum *) NULL)
673 for (x=0; x < (ssize_t) image->columns; x++)
675 SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
676 SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
677 SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
678 q+=GetPixelChannels(image);
680 if (SyncAuthenticPixels(image,exception) == MagickFalse)
683 image->colorspace=YCCColorspace;
684 if (LocaleCompare(image_info->magick,"PCDS") == 0)
685 image->colorspace=sRGBColorspace;
686 if (j < (ssize_t) number_images)
689 Allocate next image structure.
691 AcquireNextImage(image_info,image);
692 if (GetNextImageInList(image) == (Image *) NULL)
694 image=DestroyImageList(image);
695 return((Image *) NULL);
697 image=SyncNextImageInList(image);
699 (void) SetImageProgressMonitor(image,progress_monitor,
701 if (image->previous == (Image *) NULL)
703 status=SetImageProgress(image,LoadImageTag,j-1,number_images);
704 if (status == MagickFalse)
708 chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
709 chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
710 luma=(unsigned char *) RelinquishMagickMemory(luma);
711 image=GetFirstImageInList(image);
712 overview_image=OverviewImage(image_info,image,exception);
713 return(overview_image);
716 Read interleaved image.
721 for (y=0; y < (ssize_t) height; y+=2)
723 count=ReadBlob(image,width,yy);
725 count=ReadBlob(image,width,yy);
727 count=ReadBlob(image,width >> 1,c1);
729 count=ReadBlob(image,width >> 1,c2);
735 Recover luminance deltas for 1536x1024 image.
737 Upsample(768,512,image->columns,luma);
738 Upsample(384,256,image->columns,chroma1);
739 Upsample(384,256,image->columns,chroma2);
741 for (i=0; i < (4*0x800); i++)
742 (void) ReadBlobByte(image);
743 status=DecodeImage(image,luma,chroma1,chroma2);
744 if ((scene >= 5) && status)
747 Recover luminance deltas for 3072x2048 image.
749 Upsample(1536,1024,image->columns,luma);
750 Upsample(768,512,image->columns,chroma1);
751 Upsample(768,512,image->columns,chroma2);
753 offset=TellBlob(image)/0x800+12;
754 offset=SeekBlob(image,offset*0x800,SEEK_SET);
755 status=DecodeImage(image,luma,chroma1,chroma2);
756 if ((scene >= 6) && (status != MagickFalse))
759 Recover luminance deltas for 6144x4096 image (vaporware).
761 Upsample(3072,2048,image->columns,luma);
762 Upsample(1536,1024,image->columns,chroma1);
763 Upsample(1536,1024,image->columns,chroma2);
768 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
769 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
771 Transfer luminance and chrominance channels.
776 for (y=0; y < (ssize_t) image->rows; y++)
778 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
779 if (q == (const Quantum *) NULL)
781 for (x=0; x < (ssize_t) image->columns; x++)
783 SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
784 SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
785 SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
786 q+=GetPixelChannels(image);
788 if (SyncAuthenticPixels(image,exception) == MagickFalse)
790 if (image->previous == (Image *) NULL)
792 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
794 if (status == MagickFalse)
798 chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
799 chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
800 luma=(unsigned char *) RelinquishMagickMemory(luma);
801 if (EOFBlob(image) != MagickFalse)
802 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
804 (void) CloseBlob(image);
805 if (image_info->ping == MagickFalse)
806 if ((rotate == 1) || (rotate == 3))
817 degrees=rotate == 1 ? -90.0 : 90.0;
818 rotate_image=RotateImage(image,degrees,exception);
819 if (rotate_image != (Image *) NULL)
821 image=DestroyImage(image);
826 Set CCIR 709 primaries with a D65 white point.
828 image->chromaticity.red_primary.x=0.6400f;
829 image->chromaticity.red_primary.y=0.3300f;
830 image->chromaticity.green_primary.x=0.3000f;
831 image->chromaticity.green_primary.y=0.6000f;
832 image->chromaticity.blue_primary.x=0.1500f;
833 image->chromaticity.blue_primary.y=0.0600f;
834 image->chromaticity.white_point.x=0.3127f;
835 image->chromaticity.white_point.y=0.3290f;
836 image->gamma=1.000f/2.200f;
837 image->colorspace=YCCColorspace;
838 if (LocaleCompare(image_info->magick,"PCDS") == 0)
839 image->colorspace=sRGBColorspace;
840 return(GetFirstImageInList(image));
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 % R e g i s t e r P C D I m a g e %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % RegisterPCDImage() adds attributes for the PCD image format to
855 % the list of supported formats. The attributes include the image format
856 % tag, a method to read and/or write the format, whether the format
857 % supports the saving of more than one frame to the same file or blob,
858 % whether the format supports native in-memory I/O, and a brief
859 % description of the format.
861 % The format of the RegisterPCDImage method is:
863 % size_t RegisterPCDImage(void)
866 ModuleExport size_t RegisterPCDImage(void)
871 entry=SetMagickInfo("PCD");
872 entry->decoder=(DecodeImageHandler *) ReadPCDImage;
873 entry->encoder=(EncodeImageHandler *) WritePCDImage;
874 entry->magick=(IsImageFormatHandler *) IsPCD;
875 entry->adjoin=MagickFalse;
876 entry->seekable_stream=MagickTrue;
877 entry->description=ConstantString("Photo CD");
878 entry->module=ConstantString("PCD");
879 (void) RegisterMagickInfo(entry);
880 entry=SetMagickInfo("PCDS");
881 entry->decoder=(DecodeImageHandler *) ReadPCDImage;
882 entry->encoder=(EncodeImageHandler *) WritePCDImage;
883 entry->adjoin=MagickFalse;
884 entry->seekable_stream=MagickTrue;
885 entry->description=ConstantString("Photo CD");
886 entry->module=ConstantString("PCD");
887 (void) RegisterMagickInfo(entry);
888 return(MagickImageCoderSignature);
892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 % U n r e g i s t e r P C D I m a g e %
900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 % UnregisterPCDImage() removes format registrations made by the
903 % PCD module from the list of supported formats.
905 % The format of the UnregisterPCDImage method is:
907 % UnregisterPCDImage(void)
910 ModuleExport void UnregisterPCDImage(void)
912 (void) UnregisterMagickInfo("PCD");
913 (void) UnregisterMagickInfo("PCDS");
917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % W r i t e P C D I m a g e %
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % WritePCDImage() writes an image in the Photo CD encoded image format.
929 % The format of the WritePCDImage method is:
931 % MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image)
933 % A description of each parameter follows.
935 % o image_info: the image info.
937 % o image: The image.
941 static MagickBooleanType WritePCDTile(Image *image,const char *page_geometry,
942 const char *tile_geometry)
960 register const Quantum
972 Scale image to tile size.
974 SetGeometry(image,&geometry);
975 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
976 &geometry.width,&geometry.height);
977 if ((geometry.width % 2) != 0)
979 if ((geometry.height % 2) != 0)
981 tile_image=ResizeImage(image,geometry.width,geometry.height,TriangleFilter,
982 1.0,&image->exception);
983 if (tile_image == (Image *) NULL)
985 flags=ParseGeometry(page_geometry,&geometry_info);
986 geometry.width=(size_t) geometry_info.rho;
987 geometry.height=(size_t) geometry_info.sigma;
988 if ((flags & SigmaValue) == 0)
989 geometry.height=geometry.width;
990 if ((tile_image->columns != geometry.width) ||
991 (tile_image->rows != geometry.height))
1000 Put a border around the image.
1002 border_info.width=(geometry.width-tile_image->columns+1) >> 1;
1003 border_info.height=(geometry.height-tile_image->rows+1) >> 1;
1004 bordered_image=BorderImage(tile_image,&border_info,&image->exception);
1005 if (bordered_image == (Image *) NULL)
1006 return(MagickFalse);
1007 tile_image=DestroyImage(tile_image);
1008 tile_image=bordered_image;
1010 (void) TransformImage(&tile_image,(char *) NULL,tile_geometry);
1011 if (image->colorspace != RGBColorspace)
1012 (void) TransformImageColorspace(tile_image,YCCColorspace);
1013 downsample_image=ResizeImage(tile_image,tile_image->columns/2,
1014 tile_image->rows/2,TriangleFilter,1.0,&image->exception);
1015 if (downsample_image == (Image *) NULL)
1016 return(MagickFalse);
1018 Write tile to PCD file.
1020 for (y=0; y < (ssize_t) tile_image->rows; y+=2)
1022 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,2,
1023 &tile_image->exception);
1024 if (p == (const Quantum *) NULL)
1026 for (x=0; x < (ssize_t) (tile_image->columns << 1); x++)
1028 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(tile_image,p)));
1029 p+=GetPixelChannels(tile_image);
1031 q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,
1032 1,&downsample_image->exception);
1033 if (q == (const Quantum *) NULL)
1035 for (x=0; x < (ssize_t) downsample_image->columns; x++)
1037 (void) WriteBlobByte(image,ScaleQuantumToChar(
1038 GetPixelGreen(tile_image,q)));
1041 q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,
1042 1,&downsample_image->exception);
1043 if (q == (const Quantum *) NULL)
1045 for (x=0; x < (ssize_t) downsample_image->columns; x++)
1047 (void) WriteBlobByte(image,ScaleQuantumToChar(
1048 GetPixelBlue(tile_image,q)));
1051 status=SetImageProgress(image,SaveImageTag,y,tile_image->rows);
1052 if (status == MagickFalse)
1055 for (i=0; i < 0x800; i++)
1056 (void) WriteBlobByte(image,'\0');
1057 downsample_image=DestroyImage(downsample_image);
1058 tile_image=DestroyImage(tile_image);
1062 static MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image)
1073 assert(image_info != (const ImageInfo *) NULL);
1074 assert(image_info->signature == MagickSignature);
1075 assert(image != (Image *) NULL);
1076 assert(image->signature == MagickSignature);
1077 if (image->debug != MagickFalse)
1078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1080 if (image->columns < image->rows)
1086 Rotate portrait to landscape.
1088 rotate_image=RotateImage(image,90.0,&image->exception);
1089 if (rotate_image == (Image *) NULL)
1090 return(MagickFalse);
1091 pcd_image=rotate_image;
1094 Open output image file.
1096 status=OpenBlob(image_info,pcd_image,WriteBinaryBlobMode,&image->exception);
1097 if (status == MagickFalse)
1099 if (image->colorspace != RGBColorspace)
1100 (void) TransformImageColorspace(pcd_image,RGBColorspace);
1102 Write PCD image header.
1104 for (i=0; i < 32; i++)
1105 (void) WriteBlobByte(pcd_image,0xff);
1106 for (i=0; i < 4; i++)
1107 (void) WriteBlobByte(pcd_image,0x0e);
1108 for (i=0; i < 8; i++)
1109 (void) WriteBlobByte(pcd_image,'\0');
1110 for (i=0; i < 4; i++)
1111 (void) WriteBlobByte(pcd_image,0x01);
1112 for (i=0; i < 4; i++)
1113 (void) WriteBlobByte(pcd_image,0x05);
1114 for (i=0; i < 8; i++)
1115 (void) WriteBlobByte(pcd_image,'\0');
1116 for (i=0; i < 4; i++)
1117 (void) WriteBlobByte(pcd_image,0x0A);
1118 for (i=0; i < 36; i++)
1119 (void) WriteBlobByte(pcd_image,'\0');
1120 for (i=0; i < 4; i++)
1121 (void) WriteBlobByte(pcd_image,0x01);
1122 for (i=0; i < 1944; i++)
1123 (void) WriteBlobByte(pcd_image,'\0');
1124 (void) WriteBlob(pcd_image,7,(const unsigned char *) "PCD_IPI");
1125 (void) WriteBlobByte(pcd_image,0x06);
1126 for (i=0; i < 1530; i++)
1127 (void) WriteBlobByte(pcd_image,'\0');
1128 if (image->columns < image->rows)
1129 (void) WriteBlobByte(pcd_image,'\1');
1131 (void) WriteBlobByte(pcd_image,'\0');
1132 for (i=0; i < (3*0x800-1539); i++)
1133 (void) WriteBlobByte(pcd_image,'\0');
1137 status=WritePCDTile(pcd_image,"768x512>","192x128");
1138 status=WritePCDTile(pcd_image,"768x512>","384x256");
1139 status=WritePCDTile(pcd_image,"768x512>","768x512");
1140 (void) CloseBlob(pcd_image);
1141 if (pcd_image != image)
1142 pcd_image=DestroyImage(pcd_image);