2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Photo CD Image Format %
20 % Copyright 1999-2014 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/colorspace-private.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/gem.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/monitor.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/montage.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/resize.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/transform.h"
72 #include "MagickCore/utility.h"
77 static MagickBooleanType
78 WritePCDImage(const ImageInfo *,Image *,ExceptionInfo *);
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % D e c o d e I m a g e %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % DecodeImage recovers the Huffman encoded luminance and chrominance
94 % The format of the DecodeImage method is:
96 % MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
97 % unsigned char *chroma1,unsigned char *chroma2)
99 % A description of each parameter follows:
101 % o image: the address of a structure of type Image.
103 % o luma: the address of a character buffer that contains the
104 % luminance information.
106 % o chroma1: the address of a character buffer that contains the
107 % chrominance information.
109 % o chroma2: the address of a character buffer that contains the
110 % chrominance information.
113 static MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
114 unsigned char *chroma1,unsigned char *chroma2,ExceptionInfo *exception)
116 #define IsSync(sum) ((sum & 0xffffff00UL) == 0xfffffe00UL)
117 #define PCDGetBits(n) \
119 sum=(sum << n) & 0xffffffff; \
123 if (p >= (buffer+0x800)) \
125 count=ReadBlob(image,0x800,buffer); \
128 sum|=((unsigned int) (*p) << (24-bits)); \
134 typedef struct PCDTable
157 register unsigned char
177 Initialize Huffman tables.
179 assert(image != (const Image *) NULL);
180 assert(image->signature == MagickSignature);
181 if (image->debug != MagickFalse)
182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
183 assert(luma != (unsigned char *) NULL);
184 assert(chroma1 != (unsigned char *) NULL);
185 assert(chroma2 != (unsigned char *) NULL);
186 buffer=(unsigned char *) AcquireQuantumMemory(0x800,sizeof(*buffer));
187 if (buffer == (unsigned char *) NULL)
188 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
193 for (i=0; i < 3; i++)
195 pcd_table[i]=(PCDTable *) NULL;
198 for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
201 length=(sum & 0xff)+1;
202 pcd_table[i]=(PCDTable *) AcquireQuantumMemory(length,
203 sizeof(*pcd_table[i]));
204 if (pcd_table[i] == (PCDTable *) NULL)
206 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
207 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
211 for (j=0; j < (ssize_t) length; j++)
214 r->length=(unsigned int) (sum & 0xff)+1;
217 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
221 r->sequence=(unsigned int) (sum & 0xffff) << 16;
223 r->key=(unsigned char) (sum & 0xff);
224 r->mask=(~((1U << (32-r->length))-1));
227 pcd_length[i]=(size_t) length;
230 Search for Sync byte.
232 for (i=0; i < 1; i++)
234 for (i=0; i < 1; i++)
236 while ((sum & 0x00fff000UL) != 0x00fff000UL)
238 while (IsSync(sum) == 0)
241 Recover the Huffman encoded luminance and chrominance deltas.
250 if (IsSync(sum) != 0)
253 Determine plane and row number.
256 row=((sum >> 9) & 0x1fff);
257 if (row == image->rows)
266 q=luma+row*image->columns;
267 count=(ssize_t) image->columns;
272 q=chroma1+(row >> 1)*image->columns;
273 count=(ssize_t) (image->columns >> 1);
279 q=chroma2+(row >> 1)*image->columns;
280 count=(ssize_t) (image->columns >> 1);
286 ThrowBinaryException(CorruptImageError,"CorruptImage",
290 length=pcd_length[plane];
294 Decode luminance or chrominance deltas.
297 for (i=0; ((i < (ssize_t) length) && ((sum & r->mask) != r->sequence)); i++)
299 if ((row > image->rows) || (r == (PCDTable *) NULL))
301 (void) ThrowMagickException(exception,GetMagickModule(),
302 CorruptImageWarning,"SkipToSyncByte","`%s'",image->filename);
303 while ((sum & 0x00fff000) != 0x00fff000)
305 while (IsSync(sum) == 0)
310 quantum=(ssize_t) (*q)+r->key;
312 quantum=(ssize_t) (*q)+r->key-256;
313 *q=(unsigned char) ((quantum < 0) ? 0 : (quantum > 255) ? 255 : quantum);
315 PCDGetBits(r->length);
319 Relinquish resources.
321 for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
322 pcd_table[i]=(PCDTable *) RelinquishMagickMemory(pcd_table[i]);
323 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 % IsPCD() returns MagickTrue if the image format type, identified by the
339 % magick string, is PCD.
341 % The format of the IsPCD method is:
343 % MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
345 % A description of each parameter follows:
347 % o magick: compare image format pattern against these bytes.
349 % o length: Specifies the length of the magick string.
352 static MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
356 if (LocaleNCompare((const char *) magick+2048,"PCD_",4) == 0)
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 % R e a d P C D I m a g e %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % ReadPCDImage() reads a Photo CD image file and returns it. It
373 % allocates the memory necessary for the new Image structure and returns a
374 % pointer to the new image. Much of the PCD decoder was derived from
375 % the program hpcdtoppm(1) by Hadmut Danisch.
377 % The format of the ReadPCDImage method is:
379 % image=ReadPCDImage(image_info)
381 % A description of each parameter follows:
383 % o image_info: the image info.
385 % o exception: return any errors or warnings in this structure.
389 static inline size_t MagickMin(const size_t x,const size_t y)
396 static Image *OverviewImage(const ImageInfo *image_info,Image *image,
397 ExceptionInfo *exception)
409 Create the PCD Overview image.
411 for (p=image; p != (Image *) NULL; p=p->next)
413 (void) DeleteImageProperty(p,"label");
414 (void) SetImageProperty(p,"label",DefaultTileLabel,exception);
416 montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
417 (void) CopyMagickString(montage_info->filename,image_info->filename,
419 montage_image=MontageImageList(image_info,montage_info,image,exception);
420 montage_info=DestroyMontageInfo(montage_info);
421 if (montage_image == (Image *) NULL)
422 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
423 image=DestroyImage(image);
424 return(montage_image);
427 static void Upsample(const size_t width,const size_t height,
428 const size_t scaled_width,unsigned char *pixels)
434 register unsigned char
440 Create a new image that is a integral size greater than an existing one.
442 assert(pixels != (unsigned char *) NULL);
443 for (y=0; y < (ssize_t) height; y++)
445 p=pixels+(height-1-y)*scaled_width+(width-1);
446 q=pixels+((height-1-y) << 1)*scaled_width+((width-1) << 1);
449 for (x=1; x < (ssize_t) width; x++)
454 *(q+1)=(unsigned char) ((((size_t) *p)+((size_t) *(p+1))+1) >> 1);
457 for (y=0; y < (ssize_t) (height-1); y++)
459 p=pixels+((size_t) y << 1)*scaled_width;
462 for (x=0; x < (ssize_t) (width-1); x++)
464 *q=(unsigned char) ((((size_t) *p)+((size_t) *r)+1) >> 1);
465 *(q+1)=(unsigned char) ((((size_t) *p)+((size_t) *(p+2))+
466 ((size_t) *r)+((size_t) *(r+2))+2) >> 2);
471 *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
472 *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
474 p=pixels+(2*height-2)*scaled_width;
475 q=pixels+(2*height-1)*scaled_width;
476 (void) CopyMagickMemory(q,p,(size_t) (2*width));
479 static Image *ReadPCDImage(const ImageInfo *image_info,ExceptionInfo *exception)
500 register unsigned char
528 assert(image_info != (const ImageInfo *) NULL);
529 assert(image_info->signature == MagickSignature);
530 if (image_info->debug != MagickFalse)
531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
532 image_info->filename);
533 assert(exception != (ExceptionInfo *) NULL);
534 assert(exception->signature == MagickSignature);
535 image=AcquireImage(image_info,exception);
536 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
537 if (status == MagickFalse)
539 image=DestroyImageList(image);
540 return((Image *) NULL);
543 Determine if this a PCD file.
545 header=(unsigned char *) AcquireQuantumMemory(0x800,3UL*sizeof(*header));
546 if (header == (unsigned char *) NULL)
547 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
548 count=ReadBlob(image,3*0x800,header);
549 overview=LocaleNCompare((char *) header,"PCD_OPA",7) == 0;
551 ((LocaleNCompare((char *) header+0x800,"PCD",3) != 0) && (overview ==0)))
552 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
553 rotate=header[0x0e02] & 0x03;
554 number_images=(header[10] << 8) | header[11];
555 if (number_images > 65535)
556 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
557 header=(unsigned char *) RelinquishMagickMemory(header);
559 Determine resolution by scene specification.
561 if ((image->columns == 0) || (image->rows == 0))
567 for (scene=1; scene < 6; scene++)
569 if ((width >= image->columns) && (height >= image->rows))
575 if (image_info->number_scenes != 0)
576 scene=(size_t) MagickMin(image_info->scene,6);
580 Initialize image structure.
584 for (i=1; i < (ssize_t) MagickMin(scene,3); i++)
589 image->columns=width;
592 for ( ; i < (ssize_t) scene; i++)
598 Allocate luma and chroma memory.
600 number_pixels=(MagickSizeType) image->columns*image->rows;
601 if (number_pixels != (size_t) number_pixels)
602 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
603 chroma1=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
604 10*sizeof(*chroma1));
605 chroma2=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
606 10*sizeof(*chroma2));
607 luma=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
609 if ((chroma1 == (unsigned char *) NULL) ||
610 (chroma2 == (unsigned char *) NULL) || (luma == (unsigned char *) NULL))
611 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
613 Advance to image data.
624 for (i=0; i < (ssize_t) (offset*0x800); i++)
625 (void) ReadBlobByte(image);
631 MagickProgressMonitor
638 Read thumbnails from overview image.
640 for (j=1; j <= (ssize_t) number_images; j++)
642 progress_monitor=SetImageProgressMonitor(image,
643 (MagickProgressMonitor) NULL,image->client_data);
644 (void) FormatLocaleString(image->filename,MaxTextExtent,
645 "images/img%04ld.pcd",(long) j);
646 (void) FormatLocaleString(image->magick_filename,MaxTextExtent,
647 "images/img%04ld.pcd",(long) j);
648 image->scene=(size_t) j;
649 image->columns=width;
655 for (y=0; y < (ssize_t) height; y+=2)
657 count=ReadBlob(image,width,yy);
659 count=ReadBlob(image,width,yy);
661 count=ReadBlob(image,width >> 1,c1);
663 count=ReadBlob(image,width >> 1,c2);
666 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
667 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
669 Transfer luminance and chrominance channels.
674 for (y=0; y < (ssize_t) image->rows; y++)
676 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
677 if (q == (Quantum *) NULL)
679 for (x=0; x < (ssize_t) image->columns; x++)
681 SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
682 SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
683 SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
684 q+=GetPixelChannels(image);
686 if (SyncAuthenticPixels(image,exception) == MagickFalse)
689 image->colorspace=YCCColorspace;
690 if (LocaleCompare(image_info->magick,"PCDS") == 0)
691 SetImageColorspace(image,sRGBColorspace,exception);
692 if (j < (ssize_t) number_images)
695 Allocate next image structure.
697 AcquireNextImage(image_info,image,exception);
698 if (GetNextImageInList(image) == (Image *) NULL)
700 image=DestroyImageList(image);
701 return((Image *) NULL);
703 image=SyncNextImageInList(image);
705 (void) SetImageProgressMonitor(image,progress_monitor,
707 if (image->previous == (Image *) NULL)
709 status=SetImageProgress(image,LoadImageTag,j-1,number_images);
710 if (status == MagickFalse)
714 chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
715 chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
716 luma=(unsigned char *) RelinquishMagickMemory(luma);
717 image=GetFirstImageInList(image);
718 overview_image=OverviewImage(image_info,image,exception);
719 return(overview_image);
722 Read interleaved image.
727 for (y=0; y < (ssize_t) height; y+=2)
729 count=ReadBlob(image,width,yy);
731 count=ReadBlob(image,width,yy);
733 count=ReadBlob(image,width >> 1,c1);
735 count=ReadBlob(image,width >> 1,c2);
741 Recover luminance deltas for 1536x1024 image.
743 Upsample(768,512,image->columns,luma);
744 Upsample(384,256,image->columns,chroma1);
745 Upsample(384,256,image->columns,chroma2);
747 for (i=0; i < (4*0x800); i++)
748 (void) ReadBlobByte(image);
749 status=DecodeImage(image,luma,chroma1,chroma2,exception);
750 if ((scene >= 5) && status)
753 Recover luminance deltas for 3072x2048 image.
755 Upsample(1536,1024,image->columns,luma);
756 Upsample(768,512,image->columns,chroma1);
757 Upsample(768,512,image->columns,chroma2);
759 offset=TellBlob(image)/0x800+12;
760 offset=SeekBlob(image,offset*0x800,SEEK_SET);
761 status=DecodeImage(image,luma,chroma1,chroma2,exception);
762 if ((scene >= 6) && (status != MagickFalse))
765 Recover luminance deltas for 6144x4096 image (vaporware).
767 Upsample(3072,2048,image->columns,luma);
768 Upsample(1536,1024,image->columns,chroma1);
769 Upsample(1536,1024,image->columns,chroma2);
774 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
775 Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
777 Transfer luminance and chrominance channels.
782 for (y=0; y < (ssize_t) image->rows; y++)
784 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
785 if (q == (Quantum *) NULL)
787 for (x=0; x < (ssize_t) image->columns; x++)
789 SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
790 SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
791 SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
792 q+=GetPixelChannels(image);
794 if (SyncAuthenticPixels(image,exception) == MagickFalse)
796 if (image->previous == (Image *) NULL)
798 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
800 if (status == MagickFalse)
804 chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
805 chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
806 luma=(unsigned char *) RelinquishMagickMemory(luma);
807 if (EOFBlob(image) != MagickFalse)
808 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
810 (void) CloseBlob(image);
811 if (image_info->ping == MagickFalse)
812 if ((rotate == 1) || (rotate == 3))
823 degrees=rotate == 1 ? -90.0 : 90.0;
824 rotate_image=RotateImage(image,degrees,exception);
825 if (rotate_image != (Image *) NULL)
827 image=DestroyImage(image);
832 Set CCIR 709 primaries with a D65 white point.
834 image->chromaticity.red_primary.x=0.6400f;
835 image->chromaticity.red_primary.y=0.3300f;
836 image->chromaticity.green_primary.x=0.3000f;
837 image->chromaticity.green_primary.y=0.6000f;
838 image->chromaticity.blue_primary.x=0.1500f;
839 image->chromaticity.blue_primary.y=0.0600f;
840 image->chromaticity.white_point.x=0.3127f;
841 image->chromaticity.white_point.y=0.3290f;
842 image->gamma=1.000f/2.200f;
843 image->colorspace=YCCColorspace;
844 if (LocaleCompare(image_info->magick,"PCDS") == 0)
845 SetImageColorspace(image,sRGBColorspace,exception);
846 return(GetFirstImageInList(image));
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % R e g i s t e r P C D I m a g e %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % RegisterPCDImage() adds attributes for the PCD image format to
861 % the list of supported formats. The attributes include the image format
862 % tag, a method to read and/or write the format, whether the format
863 % supports the saving of more than one frame to the same file or blob,
864 % whether the format supports native in-memory I/O, and a brief
865 % description of the format.
867 % The format of the RegisterPCDImage method is:
869 % size_t RegisterPCDImage(void)
872 ModuleExport size_t RegisterPCDImage(void)
877 entry=SetMagickInfo("PCD");
878 entry->decoder=(DecodeImageHandler *) ReadPCDImage;
879 entry->encoder=(EncodeImageHandler *) WritePCDImage;
880 entry->magick=(IsImageFormatHandler *) IsPCD;
881 entry->adjoin=MagickFalse;
882 entry->seekable_stream=MagickTrue;
883 entry->description=ConstantString("Photo CD");
884 entry->module=ConstantString("PCD");
885 (void) RegisterMagickInfo(entry);
886 entry=SetMagickInfo("PCDS");
887 entry->decoder=(DecodeImageHandler *) ReadPCDImage;
888 entry->encoder=(EncodeImageHandler *) WritePCDImage;
889 entry->adjoin=MagickFalse;
890 entry->seekable_stream=MagickTrue;
891 entry->description=ConstantString("Photo CD");
892 entry->module=ConstantString("PCD");
893 (void) RegisterMagickInfo(entry);
894 return(MagickImageCoderSignature);
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 % U n r e g i s t e r P C D I m a g e %
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 % UnregisterPCDImage() removes format registrations made by the
909 % PCD module from the list of supported formats.
911 % The format of the UnregisterPCDImage method is:
913 % UnregisterPCDImage(void)
916 ModuleExport void UnregisterPCDImage(void)
918 (void) UnregisterMagickInfo("PCD");
919 (void) UnregisterMagickInfo("PCDS");
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % W r i t e P C D I m a g e %
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 % WritePCDImage() writes an image in the Photo CD encoded image format.
935 % The format of the WritePCDImage method is:
937 % MagickBooleanType WritePCDImage(const ImageInfo *image_info,
938 % Image *image,ExceptionInfo *exception)
940 % A description of each parameter follows.
942 % o image_info: the image info.
944 % o image: The image.
946 % o exception: return any errors or warnings in this structure.
950 static MagickBooleanType WritePCDTile(Image *image,const char *page_geometry,
951 const char *tile_geometry,ExceptionInfo *exception)
969 register const Quantum
981 Scale image to tile size.
983 SetGeometry(image,&geometry);
984 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
985 &geometry.width,&geometry.height);
986 if ((geometry.width % 2) != 0)
988 if ((geometry.height % 2) != 0)
990 tile_image=ResizeImage(image,geometry.width,geometry.height,TriangleFilter,
992 if (tile_image == (Image *) NULL)
994 flags=ParseGeometry(page_geometry,&geometry_info);
995 geometry.width=(size_t) geometry_info.rho;
996 geometry.height=(size_t) geometry_info.sigma;
997 if ((flags & SigmaValue) == 0)
998 geometry.height=geometry.width;
999 if ((tile_image->columns != geometry.width) ||
1000 (tile_image->rows != geometry.height))
1009 Put a border around the image.
1011 border_info.width=(geometry.width-tile_image->columns+1) >> 1;
1012 border_info.height=(geometry.height-tile_image->rows+1) >> 1;
1013 bordered_image=BorderImage(tile_image,&border_info,image->compose,
1015 if (bordered_image == (Image *) NULL)
1016 return(MagickFalse);
1017 tile_image=DestroyImage(tile_image);
1018 tile_image=bordered_image;
1020 (void) TransformImage(&tile_image,(char *) NULL,tile_geometry,exception);
1021 (void) TransformImageColorspace(tile_image,YCCColorspace,exception);
1022 downsample_image=ResizeImage(tile_image,tile_image->columns/2,
1023 tile_image->rows/2,TriangleFilter,exception);
1024 if (downsample_image == (Image *) NULL)
1025 return(MagickFalse);
1027 Write tile to PCD file.
1029 for (y=0; y < (ssize_t) tile_image->rows; y+=2)
1031 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,2,exception);
1032 if (p == (const Quantum *) NULL)
1034 for (x=0; x < (ssize_t) (tile_image->columns << 1); x++)
1036 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(tile_image,p)));
1037 p+=GetPixelChannels(tile_image);
1039 q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,1,
1041 if (q == (Quantum *) NULL)
1043 for (x=0; x < (ssize_t) downsample_image->columns; x++)
1045 (void) WriteBlobByte(image,ScaleQuantumToChar(
1046 GetPixelGreen(tile_image,q)));
1049 q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,1,
1051 if (q == (Quantum *) NULL)
1053 for (x=0; x < (ssize_t) downsample_image->columns; x++)
1055 (void) WriteBlobByte(image,ScaleQuantumToChar(
1056 GetPixelBlue(tile_image,q)));
1059 status=SetImageProgress(image,SaveImageTag,y,tile_image->rows);
1060 if (status == MagickFalse)
1063 for (i=0; i < 0x800; i++)
1064 (void) WriteBlobByte(image,'\0');
1065 downsample_image=DestroyImage(downsample_image);
1066 tile_image=DestroyImage(tile_image);
1070 static MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image,
1071 ExceptionInfo *exception)
1082 assert(image_info != (const ImageInfo *) NULL);
1083 assert(image_info->signature == MagickSignature);
1084 assert(image != (Image *) NULL);
1085 assert(image->signature == MagickSignature);
1086 if (image->debug != MagickFalse)
1087 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1089 if (image->columns < image->rows)
1095 Rotate portrait to landscape.
1097 rotate_image=RotateImage(image,90.0,exception);
1098 if (rotate_image == (Image *) NULL)
1099 return(MagickFalse);
1100 pcd_image=rotate_image;
1103 Open output image file.
1105 status=OpenBlob(image_info,pcd_image,WriteBinaryBlobMode,exception);
1106 if (status == MagickFalse)
1108 if (IssRGBCompatibleColorspace(pcd_image->colorspace) == MagickFalse)
1109 (void) TransformImageColorspace(pcd_image,sRGBColorspace,exception);
1111 Write PCD image header.
1113 for (i=0; i < 32; i++)
1114 (void) WriteBlobByte(pcd_image,0xff);
1115 for (i=0; i < 4; i++)
1116 (void) WriteBlobByte(pcd_image,0x0e);
1117 for (i=0; i < 8; i++)
1118 (void) WriteBlobByte(pcd_image,'\0');
1119 for (i=0; i < 4; i++)
1120 (void) WriteBlobByte(pcd_image,0x01);
1121 for (i=0; i < 4; i++)
1122 (void) WriteBlobByte(pcd_image,0x05);
1123 for (i=0; i < 8; i++)
1124 (void) WriteBlobByte(pcd_image,'\0');
1125 for (i=0; i < 4; i++)
1126 (void) WriteBlobByte(pcd_image,0x0A);
1127 for (i=0; i < 36; i++)
1128 (void) WriteBlobByte(pcd_image,'\0');
1129 for (i=0; i < 4; i++)
1130 (void) WriteBlobByte(pcd_image,0x01);
1131 for (i=0; i < 1944; i++)
1132 (void) WriteBlobByte(pcd_image,'\0');
1133 (void) WriteBlob(pcd_image,7,(const unsigned char *) "PCD_IPI");
1134 (void) WriteBlobByte(pcd_image,0x06);
1135 for (i=0; i < 1530; i++)
1136 (void) WriteBlobByte(pcd_image,'\0');
1137 if (image->columns < image->rows)
1138 (void) WriteBlobByte(pcd_image,'\1');
1140 (void) WriteBlobByte(pcd_image,'\0');
1141 for (i=0; i < (3*0x800-1539); i++)
1142 (void) WriteBlobByte(pcd_image,'\0');
1146 status=WritePCDTile(pcd_image,"768x512>","192x128",exception);
1147 status=WritePCDTile(pcd_image,"768x512>","384x256",exception);
1148 status=WritePCDTile(pcd_image,"768x512>","768x512",exception);
1149 (void) CloseBlob(pcd_image);
1150 if (pcd_image != image)
1151 pcd_image=DestroyImage(pcd_image);