]> granicus.if.org Git - imagemagick/blob - coders/pcd.c
(no commit message)
[imagemagick] / coders / pcd.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP    CCCC  DDDD                               %
7 %                            P   P  C      D   D                              %
8 %                            PPPP   C      D   D                              %
9 %                            P      C      D   D                              %
10 %                            P       CCCC  DDDD                               %
11 %                                                                             %
12 %                                                                             %
13 %                     Read/Write Photo CD Image Format                        %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/property.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/client.h"
48 #include "magick/colorspace.h"
49 #include "magick/constitute.h"
50 #include "magick/decorate.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/gem.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/montage.h"
63 #include "magick/resize.h"
64 #include "magick/shear.h"
65 #include "magick/quantum-private.h"
66 #include "magick/static.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
69 #include "magick/transform.h"
70 #include "magick/utility.h"
71 \f
72 /*
73   Forward declarations.
74 */
75 static MagickBooleanType
76   WritePCDImage(const ImageInfo *,Image *);
77 \f
78 /*
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %   D e c o d e I m a g e                                                     %
84 %                                                                             %
85 %                                                                             %
86 %                                                                             %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 %  DecodeImage recovers the Huffman encoded luminance and chrominance
90 %  deltas.
91 %
92 %  The format of the DecodeImage method is:
93 %
94 %      MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
95 %        unsigned char *chroma1,unsigned char *chroma2)
96 %
97 %  A description of each parameter follows:
98 %
99 %    o image: the address of a structure of type Image.
100 %
101 %    o luma: the address of a character buffer that contains the
102 %      luminance information.
103 %
104 %    o chroma1: the address of a character buffer that contains the
105 %      chrominance information.
106 %
107 %    o chroma2: the address of a character buffer that contains the
108 %      chrominance information.
109 %
110 */
111 static MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
112   unsigned char *chroma1,unsigned char *chroma2)
113 {
114 #define IsSync  ((sum & 0xffffff00UL) == 0xfffffe00UL)
115 #define PCDGetBits(n) \
116 {  \
117   sum=(sum << n) & 0xffffffff; \
118   bits-=n; \
119   while (bits <= 24) \
120   { \
121     if (p >= (buffer+0x800)) \
122       { \
123         count=ReadBlob(image,0x800,buffer); \
124         p=buffer; \
125       } \
126     sum|=((unsigned int) (*p) << (24-bits)); \
127     bits+=8; \
128     p++; \
129   } \
130   if (EOFBlob(image) != MagickFalse) \
131     break; \
132 }
133
134   typedef struct PCDTable
135   {
136     unsigned int
137       length,
138       sequence;
139
140     MagickStatusType
141       mask;
142
143     unsigned char
144       key;
145   } PCDTable;
146
147   long
148     quantum;
149
150   PCDTable
151     *pcd_table[3];
152
153   register long
154     i,
155     j;
156
157   register PCDTable
158     *r;
159
160   register unsigned char
161     *p,
162     *q;
163
164   size_t
165     length;
166
167   ssize_t
168     count;
169
170   unsigned char
171     *buffer;
172
173   unsigned long
174     bits,
175     plane,
176     pcd_length[3],
177     row,
178     sum;
179
180   /*
181     Initialize Huffman tables.
182   */
183   assert(image != (const Image *) NULL);
184   assert(image->signature == MagickSignature);
185   if (image->debug != MagickFalse)
186     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
187   assert(luma != (unsigned char *) NULL);
188   assert(chroma1 != (unsigned char *) NULL);
189   assert(chroma2 != (unsigned char *) NULL);
190   buffer=(unsigned char *) AcquireQuantumMemory(0x800,sizeof(*buffer));
191   if (buffer == (unsigned char *) NULL)
192     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
193       image->filename);
194   sum=0;
195   bits=32;
196   p=buffer+0x800;
197   for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
198   {
199     PCDGetBits(8);
200     length=(sum & 0xff)+1;
201     pcd_table[i]=(PCDTable *) AcquireQuantumMemory(length,
202       sizeof(*pcd_table[i]));
203     if (pcd_table[i] == (PCDTable *) NULL)
204       {
205         buffer=(unsigned char *) RelinquishMagickMemory(buffer);
206         ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
207           image->filename);
208       }
209     r=pcd_table[i];
210     for (j=0; j < (long) length; j++)
211     {
212       PCDGetBits(8);
213       r->length=(unsigned int) (sum & 0xff)+1;
214       if (r->length > 16)
215         {
216           buffer=(unsigned char *) RelinquishMagickMemory(buffer);
217           return(MagickFalse);
218         }
219       PCDGetBits(16);
220       r->sequence=(unsigned int) (sum & 0xffff) << 16;
221       PCDGetBits(8);
222       r->key=(unsigned char) (sum & 0xff);
223       r->mask=(~((1U << (32-r->length))-1));
224       r++;
225     }
226     pcd_length[i]=(unsigned long) length;
227   }
228   /*
229     Search for Sync byte.
230   */
231   for (i=0; i < 1; i++)
232     PCDGetBits(16);
233   for (i=0; i < 1; i++)
234     PCDGetBits(16);
235   while ((sum & 0x00fff000UL) != 0x00fff000UL)
236     PCDGetBits(8);
237   while (IsSync == 0)
238     PCDGetBits(1);
239   /*
240     Recover the Huffman encoded luminance and chrominance deltas.
241   */
242   count=0;
243   length=0;
244   plane=0;
245   row=0;
246   q=luma;
247   for ( ; ; )
248   {
249     if (IsSync != 0)
250       {
251         /*
252           Determine plane and row number.
253         */
254         PCDGetBits(16);
255         row=((sum >> 9) & 0x1fff);
256         if (row == image->rows)
257           break;
258         PCDGetBits(8);
259         plane=sum >> 30;
260         PCDGetBits(16);
261         switch (plane)
262         {
263           case 0:
264           {
265             q=luma+row*image->columns;
266             count=(ssize_t) image->columns;
267             break;
268           }
269           case 2:
270           {
271             q=chroma1+(row >> 1)*image->columns;
272             count=(ssize_t) (image->columns >> 1);
273             plane--;
274             break;
275           }
276           case 3:
277           {
278             q=chroma2+(row >> 1)*image->columns;
279             count=(ssize_t) (image->columns >> 1);
280             plane--;
281             break;
282           }
283           default:
284           {
285             ThrowBinaryException(CorruptImageError,"CorruptImage",
286               image->filename);
287           }
288         }
289         length=pcd_length[plane];
290         continue;
291       }
292     /*
293       Decode luminance or chrominance deltas.
294     */
295     r=pcd_table[plane];
296     for (i=0; ((i < (long) length) && ((sum & r->mask) != r->sequence)); i++)
297       r++;
298     if ((row > image->rows) || (r == (PCDTable *) NULL) ||
299         ((size_t) (q-luma) > (image->columns*image->rows)))
300       {
301         (void) ThrowMagickException(&image->exception,GetMagickModule(),
302           CorruptImageWarning,"SkipToSyncByte","`%s'",image->filename);
303         while ((sum & 0x00fff000) != 0x00fff000)
304           PCDGetBits(8);
305         while (IsSync == 0)
306           PCDGetBits(1);
307         continue;
308       }
309     if (r->key < 128)
310       quantum=(long) (*q)+r->key;
311     else
312       quantum=(long) (*q)+r->key-256;
313     *q=(unsigned char) ((quantum < 0) ? 0 : (quantum > 255) ? 255 : quantum);
314     q++;
315     PCDGetBits(r->length);
316     count--;
317   }
318   /*
319     Relinquish resources.
320   */
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);
324   return(MagickTrue);
325 }
326 \f
327 /*
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 %                                                                             %
330 %                                                                             %
331 %                                                                             %
332 %   I s P C D                                                                 %
333 %                                                                             %
334 %                                                                             %
335 %                                                                             %
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 %
338 %  IsPCD() returns MagickTrue if the image format type, identified by the
339 %  magick string, is PCD.
340 %
341 %  The format of the IsPCD method is:
342 %
343 %      MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
344 %
345 %  A description of each parameter follows:
346 %
347 %    o magick: compare image format pattern against these bytes.
348 %
349 %    o length: Specifies the length of the magick string.
350 %
351 */
352 static MagickBooleanType IsPCD(const unsigned char *magick,const size_t length)
353 {
354   if (length < 2052)
355     return(MagickFalse);
356   if (LocaleNCompare((const char *) magick+2048,"PCD_",4) == 0)
357     return(MagickTrue);
358   return(MagickFalse);
359 }
360 \f
361 /*
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363 %                                                                             %
364 %                                                                             %
365 %                                                                             %
366 %   R e a d P C D I m a g e                                                   %
367 %                                                                             %
368 %                                                                             %
369 %                                                                             %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 %
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.
376 %
377 %  The format of the ReadPCDImage method is:
378 %
379 %      image=ReadPCDImage(image_info)
380 %
381 %  A description of each parameter follows:
382 %
383 %    o image_info: the image info.
384 %
385 %    o exception: return any errors or warnings in this structure.
386 %
387 */
388
389 static inline size_t MagickMin(const size_t x,const size_t y)
390 {
391   if (x < y)
392     return(x);
393   return(y);
394 }
395
396 static Image *OverviewImage(const ImageInfo *image_info,Image *image,
397   ExceptionInfo *exception)
398 {
399   Image
400     *montage_image;
401
402   MontageInfo
403     *montage_info;
404
405   register Image
406     *p;
407
408   /*
409     Create the PCD Overview image.
410   */
411   for (p=image; p != (Image *) NULL; p=p->next)
412   {
413     (void) DeleteImageProperty(p,"label");
414     (void) SetImageProperty(p,"label",DefaultTileLabel);
415   }
416   montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
417   (void) CopyMagickString(montage_info->filename,image_info->filename,
418     MaxTextExtent);
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);
425 }
426
427 static void Upsample(const unsigned long width,const unsigned long height,
428   const unsigned long scaled_width,unsigned char *pixels)
429 {
430   register long
431     x,
432     y;
433
434   register unsigned char
435     *p,
436     *q,
437     *r;
438
439   /*
440     Create a new image that is a integral size greater than an existing one.
441   */
442   assert(pixels != (unsigned char *) NULL);
443   for (y=0; y < (long) height; y++)
444   {
445     p=pixels+(height-1-y)*scaled_width+(width-1);
446     q=pixels+((height-1-y) << 1)*scaled_width+((width-1) << 1);
447     *q=(*p);
448     *(q+1)=(*(p));
449     for (x=1; x < (long) width; x++)
450     {
451       p--;
452       q-=2;
453       *q=(*p);
454       *(q+1)=(unsigned char) ((((unsigned long) *p)+
455         ((unsigned long) *(p+1))+1) >> 1);
456     }
457   }
458   for (y=0; y < (long) (height-1); y++)
459   {
460     p=pixels+((unsigned long) y << 1)*scaled_width;
461     q=p+scaled_width;
462     r=q+scaled_width;
463     for (x=0; x < (long) (width-1); x++)
464     {
465       *q=(unsigned char) ((((unsigned long) *p)+((unsigned long) *r)+1) >> 1);
466       *(q+1)=(unsigned char) ((((unsigned long) *p)+((unsigned long) *(p+2))+
467         ((unsigned long) *r)+((unsigned long) *(r+2))+2) >> 2);
468       q+=2;
469       p+=2;
470       r+=2;
471     }
472     *q++=(unsigned char) ((((unsigned long) *p++)+
473       ((unsigned long) *r++)+1) >> 1);
474     *q++=(unsigned char) ((((unsigned long) *p++)+
475       ((unsigned long) *r++)+1) >> 1);
476   }
477   p=pixels+(2*height-2)*scaled_width;
478   q=pixels+(2*height-1)*scaled_width;
479   (void) CopyMagickMemory(q,p,(size_t) (2*width));
480 }
481
482 static Image *ReadPCDImage(const ImageInfo *image_info,ExceptionInfo *exception)
483 {
484   Image
485     *image;
486
487   long
488     x;
489
490   MagickBooleanType
491     status;
492
493   MagickOffsetType
494     offset;
495
496   MagickSizeType
497     number_pixels;
498
499   register long
500     i,
501     y;
502
503   register PixelPacket
504     *q;
505
506   register unsigned char
507     *c1,
508     *c2,
509     *yy;
510
511   ssize_t
512     count;
513
514   unsigned char
515     *chroma1,
516     *chroma2,
517     *header,
518     *luma;
519
520   unsigned int
521     overview;
522
523   unsigned long
524     height,
525     number_images,
526     rotate,
527     scene,
528     width;
529
530   /*
531     Open image file.
532   */
533   assert(image_info != (const ImageInfo *) NULL);
534   assert(image_info->signature == MagickSignature);
535   if (image_info->debug != MagickFalse)
536     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
537       image_info->filename);
538   assert(exception != (ExceptionInfo *) NULL);
539   assert(exception->signature == MagickSignature);
540   image=AcquireImage(image_info);
541   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
542   if (status == MagickFalse)
543     {
544       image=DestroyImageList(image);
545       return((Image *) NULL);
546     }
547   /*
548     Determine if this a PCD file.
549   */
550   header=(unsigned char *) AcquireQuantumMemory(0x800,3UL*sizeof(*header));
551   if (header == (unsigned char *) NULL)
552     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
553   count=ReadBlob(image,3*0x800,header);
554   overview=LocaleNCompare((char *) header,"PCD_OPA",7) == 0;
555   if ((count == 0) ||
556       ((LocaleNCompare((char *) header+0x800,"PCD",3) != 0) && !overview))
557     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
558   rotate=header[0x0e02] & 0x03;
559   number_images=(header[10] << 8) | header[11];
560   header=(unsigned char *) RelinquishMagickMemory(header);
561   /*
562     Determine resolution by scene specification.
563   */
564   if ((image->columns == 0) || (image->rows == 0))
565     scene=3;
566   else
567     {
568       width=192;
569       height=128;
570       for (scene=1; scene < 6; scene++)
571       {
572         if ((width >= image->columns) && (height >= image->rows))
573           break;
574         width<<=1;
575         height<<=1;
576       }
577     }
578   if (image_info->number_scenes != 0)
579     scene=(unsigned long) MagickMin(image_info->scene,6);
580   if (overview)
581     scene=1;
582   /*
583     Initialize image structure.
584   */
585   width=192;
586   height=128;
587   for (i=1; i < (long) MagickMin(scene,3); i++)
588   {
589     width<<=1;
590     height<<=1;
591   }
592   image->columns=width;
593   image->rows=height;
594   image->depth=8;
595   for ( ; i < (long) scene; i++)
596   {
597     image->columns<<=1;
598     image->rows<<=1;
599   }
600   /*
601     Allocate lumand chroma memory.
602   */
603   number_pixels=(MagickSizeType) image->columns*image->rows;
604   if (number_pixels != (size_t) number_pixels)
605     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
606   chroma1=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
607     sizeof(*chroma1));
608   chroma2=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
609     sizeof(*chroma2));
610   luma=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
611     sizeof(*luma));
612   if ((chroma1 == (unsigned char *) NULL) ||
613       (chroma2 == (unsigned char *) NULL) || (luma == (unsigned char *) NULL))
614     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
615   /*
616     Advance to image data.
617   */
618   offset=93;
619   if (overview)
620     offset=2;
621   else
622     if (scene == 2)
623       offset=20;
624     else
625       if (scene <= 1)
626         offset=1;
627   for (i=0; i < (long) (offset*0x800); i++)
628     (void) ReadBlobByte(image);
629   if (overview)
630     {
631       Image
632         *overview_image;
633
634       MagickProgressMonitor
635         progress_monitor;
636
637       register long
638         j;
639
640       /*
641         Read thumbnails from overview image.
642       */
643       for (j=1; j <= (long) number_images; j++)
644       {
645         progress_monitor=SetImageProgressMonitor(image,
646           (MagickProgressMonitor) NULL,image->client_data);
647         (void) FormatMagickString(image->filename,MaxTextExtent,
648           "images/img%04ld.pcd",j);
649         (void) FormatMagickString(image->magick_filename,MaxTextExtent,
650           "images/img%04ld.pcd",j);
651         image->scene=(unsigned long) j;
652         image->columns=width;
653         image->rows=height;
654         image->depth=8;
655         yy=luma;
656         c1=chroma1;
657         c2=chroma2;
658         for (y=0; y < (long) height; y+=2)
659         {
660           count=ReadBlob(image,width,yy);
661           yy+=image->columns;
662           count=ReadBlob(image,width,yy);
663           yy+=image->columns;
664           count=ReadBlob(image,width >> 1,c1);
665           c1+=image->columns;
666           count=ReadBlob(image,width >> 1,c2);
667           c2+=image->columns;
668         }
669         Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
670         Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
671         /*
672           Transfer luminance and chrominance channels.
673         */
674         yy=luma;
675         c1=chroma1;
676         c2=chroma2;
677         for (y=0; y < (long) image->rows; y++)
678         {
679           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
680           if (q == (PixelPacket *) NULL)
681             break;
682           for (x=0; x < (long) image->columns; x++)
683           {
684             q->red=ScaleCharToQuantum(*yy++);
685             q->green=ScaleCharToQuantum(*c1++);
686             q->blue=ScaleCharToQuantum(*c2++);
687             q++;
688           }
689           if (SyncAuthenticPixels(image,exception) == MagickFalse)
690             break;
691         }
692         image->colorspace=YCCColorspace;
693         if (LocaleCompare(image_info->magick,"PCDS") == 0)
694           image->colorspace=sRGBColorspace;
695         if (j < (long) number_images)
696           {
697             /*
698               Allocate next image structure.
699             */
700             AcquireNextImage(image_info,image);
701             if (GetNextImageInList(image) == (Image *) NULL)
702               {
703                 image=DestroyImageList(image);
704                 return((Image *) NULL);
705               }
706             image=SyncNextImageInList(image);
707           }
708         (void) SetImageProgressMonitor(image,progress_monitor,
709           image->client_data);
710         if (image->previous == (Image *) NULL)
711           {
712             status=SetImageProgress(image,LoadImageTag,j-1,number_images);
713             if (status == MagickFalse)
714               break;
715           }
716       }
717       chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
718       chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
719       luma=(unsigned char *) RelinquishMagickMemory(luma);
720       image=GetFirstImageInList(image);
721       overview_image=OverviewImage(image_info,image,exception);
722       return(overview_image);
723     }
724   /*
725     Read interleaved image.
726   */
727   yy=luma;
728   c1=chroma1;
729   c2=chroma2;
730   for (y=0; y < (long) height; y+=2)
731   {
732     count=ReadBlob(image,width,yy);
733     yy+=image->columns;
734     count=ReadBlob(image,width,yy);
735     yy+=image->columns;
736     count=ReadBlob(image,width >> 1,c1);
737     c1+=image->columns;
738     count=ReadBlob(image,width >> 1,c2);
739     c2+=image->columns;
740   }
741   if (scene >= 4)
742     {
743       /*
744         Recover luminance deltas for 1536x1024 image.
745       */
746       Upsample(768,512,image->columns,luma);
747       Upsample(384,256,image->columns,chroma1);
748       Upsample(384,256,image->columns,chroma2);
749       image->rows=1024;
750       for (i=0; i < (4*0x800); i++)
751         (void) ReadBlobByte(image);
752       status=DecodeImage(image,luma,chroma1,chroma2);
753       if ((scene >= 5) && status)
754         {
755           /*
756             Recover luminance deltas for 3072x2048 image.
757           */
758           Upsample(1536,1024,image->columns,luma);
759           Upsample(768,512,image->columns,chroma1);
760           Upsample(768,512,image->columns,chroma2);
761           image->rows=2048;
762           offset=TellBlob(image)/0x800+12;
763           offset=SeekBlob(image,offset*0x800,SEEK_SET);
764           status=DecodeImage(image,luma,chroma1,chroma2);
765           if ((scene >= 6) && (status != MagickFalse))
766             {
767               /*
768                 Recover luminance deltas for 6144x4096 image (vaporware).
769               */
770               Upsample(3072,2048,image->columns,luma);
771               Upsample(1536,1024,image->columns,chroma1);
772               Upsample(1536,1024,image->columns,chroma2);
773               image->rows=4096;
774             }
775         }
776     }
777   Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma1);
778   Upsample(image->columns >> 1,image->rows >> 1,image->columns,chroma2);
779   /*
780     Transfer luminance and chrominance channels.
781   */
782   yy=luma;
783   c1=chroma1;
784   c2=chroma2;
785   for (y=0; y < (long) image->rows; y++)
786   {
787     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
788     if (q == (PixelPacket *) NULL)
789       break;
790     for (x=0; x < (long) image->columns; x++)
791     {
792       q->red=ScaleCharToQuantum(*yy++);
793       q->green=ScaleCharToQuantum(*c1++);
794       q->blue=ScaleCharToQuantum(*c2++);
795       q++;
796     }
797     if (SyncAuthenticPixels(image,exception) == MagickFalse)
798       break;
799     if (image->previous == (Image *) NULL)
800       {
801         status=SetImageProgress(image,LoadImageTag,y,image->rows);
802         if (status == MagickFalse)
803           break;
804       }
805   }
806   chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
807   chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
808   luma=(unsigned char *) RelinquishMagickMemory(luma);
809   if (EOFBlob(image) != MagickFalse)
810     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
811       image->filename);
812   (void) CloseBlob(image);
813   if (image_info->ping == MagickFalse)
814     if ((rotate == 1) || (rotate == 3))
815       {
816         double
817           degrees;
818
819         Image
820           *rotate_image;
821
822         /*
823           Rotate image.
824         */
825         degrees=rotate == 1 ? -90.0 : 90.0;
826         rotate_image=RotateImage(image,degrees,exception);
827         if (rotate_image != (Image *) NULL)
828           {
829             image=DestroyImage(image);
830             image=rotate_image;
831           }
832       }
833   /*
834     Set CCIR 709 primaries with a D65 white point.
835   */
836   image->chromaticity.red_primary.x=0.6400f;
837   image->chromaticity.red_primary.y=0.3300f;
838   image->chromaticity.green_primary.x=0.3000f;
839   image->chromaticity.green_primary.y=0.6000f;
840   image->chromaticity.blue_primary.x=0.1500f;
841   image->chromaticity.blue_primary.y=0.0600f;
842   image->chromaticity.white_point.x=0.3127f;
843   image->chromaticity.white_point.y=0.3290f;
844   image->gamma=1.000f/2.200f;
845   image->colorspace=YCCColorspace;
846   if (LocaleCompare(image_info->magick,"PCDS") == 0)
847     image->colorspace=sRGBColorspace;
848   return(GetFirstImageInList(image));
849 }
850 \f
851 /*
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 %                                                                             %
854 %                                                                             %
855 %                                                                             %
856 %   R e g i s t e r P C D I m a g e                                           %
857 %                                                                             %
858 %                                                                             %
859 %                                                                             %
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 %
862 %  RegisterPCDImage() adds attributes for the PCD image format to
863 %  the list of supported formats.  The attributes include the image format
864 %  tag, a method to read and/or write the format, whether the format
865 %  supports the saving of more than one frame to the same file or blob,
866 %  whether the format supports native in-memory I/O, and a brief
867 %  description of the format.
868 %
869 %  The format of the RegisterPCDImage method is:
870 %
871 %      unsigned long RegisterPCDImage(void)
872 %
873 */
874 ModuleExport unsigned long RegisterPCDImage(void)
875 {
876   MagickInfo
877     *entry;
878
879   entry=SetMagickInfo("PCD");
880   entry->decoder=(DecodeImageHandler *) ReadPCDImage;
881   entry->encoder=(EncodeImageHandler *) WritePCDImage;
882   entry->magick=(IsImageFormatHandler *) IsPCD;
883   entry->adjoin=MagickFalse;
884   entry->description=ConstantString("Photo CD");
885   entry->module=ConstantString("PCD");
886   (void) RegisterMagickInfo(entry);
887   entry=SetMagickInfo("PCDS");
888   entry->decoder=(DecodeImageHandler *) ReadPCDImage;
889   entry->encoder=(EncodeImageHandler *) WritePCDImage;
890   entry->adjoin=MagickFalse;
891   entry->description=ConstantString("Photo CD");
892   entry->module=ConstantString("PCD");
893   (void) RegisterMagickInfo(entry);
894   return(MagickImageCoderSignature);
895 }
896 \f
897 /*
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 %                                                                             %
900 %                                                                             %
901 %                                                                             %
902 %   U n r e g i s t e r P C D I m a g e                                       %
903 %                                                                             %
904 %                                                                             %
905 %                                                                             %
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 %
908 %  UnregisterPCDImage() removes format registrations made by the
909 %  PCD module from the list of supported formats.
910 %
911 %  The format of the UnregisterPCDImage method is:
912 %
913 %      UnregisterPCDImage(void)
914 %
915 */
916 ModuleExport void UnregisterPCDImage(void)
917 {
918   (void) UnregisterMagickInfo("PCD");
919   (void) UnregisterMagickInfo("PCDS");
920 }
921 \f
922 /*
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 %                                                                             %
925 %                                                                             %
926 %                                                                             %
927 %   W r i t e P C D I m a g e                                                 %
928 %                                                                             %
929 %                                                                             %
930 %                                                                             %
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 %
933 %  WritePCDImage() writes an image in the Photo CD encoded image format.
934 %
935 %  The format of the WritePCDImage method is:
936 %
937 %      MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image)
938 %
939 %  A description of each parameter follows.
940 %
941 %    o image_info: the image info.
942 %
943 %    o image:  The image.
944 %
945 */
946
947 static MagickBooleanType WritePCDTile(Image *image,const char *page_geometry,
948   const char *tile_geometry)
949 {
950   GeometryInfo
951     geometry_info;
952
953   Image
954     *downsample_image,
955     *tile_image;
956
957   long
958     y;
959
960   MagickBooleanType
961     status;
962
963   MagickStatusType
964     flags;
965
966   RectangleInfo
967     geometry;
968
969   register const PixelPacket
970     *p,
971     *q;
972
973   register long
974     i,
975     x;
976
977   /*
978     Scale image to tile size.
979   */
980   SetGeometry(image,&geometry);
981   (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
982     &geometry.width,&geometry.height);
983   if ((geometry.width % 2) != 0)
984     geometry.width--;
985   if ((geometry.height % 2) != 0)
986     geometry.height--;
987   tile_image=ResizeImage(image,geometry.width,geometry.height,TriangleFilter,
988     1.0,&image->exception);
989   if (tile_image == (Image *) NULL)
990     return(MagickFalse);
991   flags=ParseGeometry(page_geometry,&geometry_info);
992   geometry.width=(unsigned long) geometry_info.rho;
993   geometry.height=(unsigned long) geometry_info.sigma;
994   if ((flags & SigmaValue) == 0)
995     geometry.height=geometry.width;
996   if ((tile_image->columns != geometry.width) ||
997       (tile_image->rows != geometry.height))
998     {
999       Image
1000         *bordered_image;
1001
1002       RectangleInfo
1003         border_info;
1004
1005       /*
1006         Put a border around the image.
1007       */
1008       border_info.width=(geometry.width-tile_image->columns+1) >> 1;
1009       border_info.height=(geometry.height-tile_image->rows+1) >> 1;
1010       bordered_image=BorderImage(tile_image,&border_info,&image->exception);
1011       if (bordered_image == (Image *) NULL)
1012         return(MagickFalse);
1013       tile_image=DestroyImage(tile_image);
1014       tile_image=bordered_image;
1015     }
1016   (void) TransformImage(&tile_image,(char *) NULL,tile_geometry);
1017   if (image->colorspace != RGBColorspace)
1018     (void) TransformImageColorspace(tile_image,YCCColorspace);
1019   downsample_image=ResizeImage(tile_image,tile_image->columns/2,
1020     tile_image->rows/2,TriangleFilter,1.0,&image->exception);
1021   if (downsample_image == (Image *) NULL)
1022     return(MagickFalse);
1023   /*
1024     Write tile to PCD file.
1025   */
1026   for (y=0; y < (long) tile_image->rows; y+=2)
1027   {
1028     p=GetVirtualPixels(tile_image,0,y,tile_image->columns,2,
1029       &tile_image->exception);
1030     if (p == (const PixelPacket *) NULL)
1031       break;
1032     for (x=0; x < (long) (tile_image->columns << 1); x++)
1033     {
1034       (void) WriteBlobByte(image,ScaleQuantumToChar(p->red));
1035       p++;
1036     }
1037     q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,
1038       1,&downsample_image->exception);
1039     if (q == (const PixelPacket *) NULL)
1040       break;
1041     for (x=0; x < (long) downsample_image->columns; x++)
1042     {
1043       (void) WriteBlobByte(image,ScaleQuantumToChar(q->green));
1044       q++;
1045     }
1046     q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,
1047       1,&downsample_image->exception);
1048     if (q == (const PixelPacket *) NULL)
1049       break;
1050     for (x=0; x < (long) downsample_image->columns; x++)
1051     {
1052       (void) WriteBlobByte(image,ScaleQuantumToChar(q->blue));
1053       q++;
1054     }
1055     status=SetImageProgress(image,SaveImageTag,y,tile_image->rows);
1056     if (status == MagickFalse)
1057       break;
1058   }
1059   for (i=0; i < 0x800; i++)
1060     (void) WriteBlobByte(image,'\0');
1061   downsample_image=DestroyImage(downsample_image);
1062   tile_image=DestroyImage(tile_image);
1063   return(MagickTrue);
1064 }
1065
1066 static MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image)
1067 {
1068   Image
1069     *pcd_image;
1070
1071   MagickBooleanType
1072     status;
1073
1074   register long
1075     i;
1076
1077   assert(image_info != (const ImageInfo *) NULL);
1078   assert(image_info->signature == MagickSignature);
1079   assert(image != (Image *) NULL);
1080   assert(image->signature == MagickSignature);
1081   if (image->debug != MagickFalse)
1082     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1083   pcd_image=image;
1084   if (image->columns < image->rows)
1085     {
1086       Image
1087         *rotate_image;
1088
1089       /*
1090         Rotate portrait to landscape.
1091       */
1092       rotate_image=RotateImage(image,90.0,&image->exception);
1093       if (rotate_image == (Image *) NULL)
1094         return(MagickFalse);
1095       pcd_image=rotate_image;
1096     }
1097   /*
1098     Open output image file.
1099   */
1100   status=OpenBlob(image_info,pcd_image,WriteBinaryBlobMode,&image->exception);
1101   if (status == MagickFalse)
1102     return(status);
1103   if (image->colorspace != RGBColorspace)
1104     (void) TransformImageColorspace(pcd_image,RGBColorspace);
1105   /*
1106     Write PCD image header.
1107   */
1108   for (i=0; i < 32; i++)
1109     (void) WriteBlobByte(pcd_image,0xff);
1110   for (i=0; i < 4; i++)
1111     (void) WriteBlobByte(pcd_image,0x0e);
1112   for (i=0; i < 8; i++)
1113     (void) WriteBlobByte(pcd_image,'\0');
1114   for (i=0; i < 4; i++)
1115     (void) WriteBlobByte(pcd_image,0x01);
1116   for (i=0; i < 4; i++)
1117     (void) WriteBlobByte(pcd_image,0x05);
1118   for (i=0; i < 8; i++)
1119     (void) WriteBlobByte(pcd_image,'\0');
1120   for (i=0; i < 4; i++)
1121     (void) WriteBlobByte(pcd_image,0x0A);
1122   for (i=0; i < 36; i++)
1123     (void) WriteBlobByte(pcd_image,'\0');
1124   for (i=0; i < 4; i++)
1125     (void) WriteBlobByte(pcd_image,0x01);
1126   for (i=0; i < 1944; i++)
1127     (void) WriteBlobByte(pcd_image,'\0');
1128   (void) WriteBlob(pcd_image,7,(const unsigned char *) "PCD_IPI");
1129   (void) WriteBlobByte(pcd_image,0x06);
1130   for (i=0; i < 1530; i++)
1131     (void) WriteBlobByte(pcd_image,'\0');
1132   if (image->columns < image->rows)
1133     (void) WriteBlobByte(pcd_image,'\1');
1134   else
1135     (void) WriteBlobByte(pcd_image,'\0');
1136   for (i=0; i < (3*0x800-1539); i++)
1137     (void) WriteBlobByte(pcd_image,'\0');
1138   /*
1139     Write PCD tiles.
1140   */
1141   status=WritePCDTile(pcd_image,"768x512>","192x128");
1142   status=WritePCDTile(pcd_image,"768x512>","384x256");
1143   status=WritePCDTile(pcd_image,"768x512>","768x512");
1144   (void) CloseBlob(pcd_image);
1145   if (pcd_image != image)
1146     pcd_image=DestroyImage(pcd_image);
1147   return(status);
1148 }