]> 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 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2015 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 "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"
73 \f
74 /*
75   Forward declarations.
76 */
77 static MagickBooleanType
78   WritePCDImage(const ImageInfo *,Image *,ExceptionInfo *);
79 \f
80 /*
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %                                                                             %
83 %                                                                             %
84 %                                                                             %
85 %   D e c o d e I m a g e                                                     %
86 %                                                                             %
87 %                                                                             %
88 %                                                                             %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %
91 %  DecodeImage recovers the Huffman encoded luminance and chrominance
92 %  deltas.
93 %
94 %  The format of the DecodeImage method is:
95 %
96 %      MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
97 %        unsigned char *chroma1,unsigned char *chroma2)
98 %
99 %  A description of each parameter follows:
100 %
101 %    o image: the address of a structure of type Image.
102 %
103 %    o luma: the address of a character buffer that contains the
104 %      luminance information.
105 %
106 %    o chroma1: the address of a character buffer that contains the
107 %      chrominance information.
108 %
109 %    o chroma2: the address of a character buffer that contains the
110 %      chrominance information.
111 %
112 */
113 static MagickBooleanType DecodeImage(Image *image,unsigned char *luma,
114   unsigned char *chroma1,unsigned char *chroma2,ExceptionInfo *exception)
115 {
116 #define IsSync(sum)  ((sum & 0xffffff00UL) == 0xfffffe00UL)
117 #define PCDGetBits(n) \
118 {  \
119   sum=(sum << n) & 0xffffffff; \
120   bits-=n; \
121   while (bits <= 24) \
122   { \
123     if (p >= (buffer+0x800)) \
124       { \
125         count=ReadBlob(image,0x800,buffer); \
126         p=buffer; \
127       } \
128     sum|=((unsigned int) (*p) << (24-bits)); \
129     bits+=8; \
130     p++; \
131   } \
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   PCDTable
148     *pcd_table[3];
149
150   register ssize_t
151     i,
152     j;
153
154   register PCDTable
155     *r;
156
157   register unsigned char
158     *p,
159     *q;
160
161   size_t
162     bits,
163     length,
164     plane,
165     pcd_length[3],
166     row,
167     sum;
168
169   ssize_t
170     count,
171     quantum;
172
173   unsigned char
174     *buffer;
175
176   /*
177     Initialize Huffman tables.
178   */
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",
189       image->filename);
190   sum=0;
191   bits=32;
192   p=buffer+0x800;
193   for (i=0; i < 3; i++)
194   {
195     pcd_table[i]=(PCDTable *) NULL;
196     pcd_length[i]=0;
197   }
198   for (i=0; i < (image->columns > 1536 ? 3 : 1); i++)
199   {
200     PCDGetBits(8);
201     length=(sum & 0xff)+1;
202     pcd_table[i]=(PCDTable *) AcquireQuantumMemory(length,
203       sizeof(*pcd_table[i]));
204     if (pcd_table[i] == (PCDTable *) NULL)
205       {
206         buffer=(unsigned char *) RelinquishMagickMemory(buffer);
207         ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
208           image->filename);
209       }
210     r=pcd_table[i];
211     for (j=0; j < (ssize_t) length; j++)
212     {
213       PCDGetBits(8);
214       r->length=(unsigned int) (sum & 0xff)+1;
215       if (r->length > 16)
216         {
217           buffer=(unsigned char *) RelinquishMagickMemory(buffer);
218           return(MagickFalse);
219         }
220       PCDGetBits(16);
221       r->sequence=(unsigned int) (sum & 0xffff) << 16;
222       PCDGetBits(8);
223       r->key=(unsigned char) (sum & 0xff);
224       r->mask=(~((1U << (32-r->length))-1));
225       r++;
226     }
227     pcd_length[i]=(size_t) length;
228   }
229   /*
230     Search for Sync byte.
231   */
232   for (i=0; i < 1; i++)
233     PCDGetBits(16);
234   for (i=0; i < 1; i++)
235     PCDGetBits(16);
236   while ((sum & 0x00fff000UL) != 0x00fff000UL)
237     PCDGetBits(8);
238   while (IsSync(sum) == 0)
239     PCDGetBits(1);
240   /*
241     Recover the Huffman encoded luminance and chrominance deltas.
242   */
243   count=0;
244   length=0;
245   plane=0;
246   row=0;
247   q=luma;
248   for ( ; ; )
249   {
250     if (IsSync(sum) != 0)
251       {
252         /*
253           Determine plane and row number.
254         */
255         PCDGetBits(16);
256         row=((sum >> 9) & 0x1fff);
257         if (row == image->rows)
258           break;
259         PCDGetBits(8);
260         plane=sum >> 30;
261         PCDGetBits(16);
262         switch (plane)
263         {
264           case 0:
265           {
266             q=luma+row*image->columns;
267             count=(ssize_t) image->columns;
268             break;
269           }
270           case 2:
271           {
272             q=chroma1+(row >> 1)*image->columns;
273             count=(ssize_t) (image->columns >> 1);
274             plane--;
275             break;
276           }
277           case 3:
278           {
279             q=chroma2+(row >> 1)*image->columns;
280             count=(ssize_t) (image->columns >> 1);
281             plane--;
282             break;
283           }
284           default:
285           {
286             ThrowBinaryException(CorruptImageError,"CorruptImage",
287               image->filename);
288           }
289         }
290         length=pcd_length[plane];
291         continue;
292       }
293     /*
294       Decode luminance or chrominance deltas.
295     */
296     r=pcd_table[plane];
297     for (i=0; ((i < (ssize_t) length) && ((sum & r->mask) != r->sequence)); i++)
298       r++;
299     if ((row > image->rows) || (r == (PCDTable *) NULL))
300       {
301         (void) ThrowMagickException(exception,GetMagickModule(),
302           CorruptImageWarning,"SkipToSyncByte","`%s'",image->filename);
303         while ((sum & 0x00fff000) != 0x00fff000)
304           PCDGetBits(8);
305         while (IsSync(sum) == 0)
306           PCDGetBits(1);
307         continue;
308       }
309     if (r->key < 128)
310       quantum=(ssize_t) (*q)+r->key;
311     else
312       quantum=(ssize_t) (*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,exception);
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 size_t width,const size_t height,
428   const size_t scaled_width,unsigned char *pixels)
429 {
430   register ssize_t
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 < (ssize_t) 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 < (ssize_t) width; x++)
450     {
451       p--;
452       q-=2;
453       *q=(*p);
454       *(q+1)=(unsigned char) ((((size_t) *p)+((size_t) *(p+1))+1) >> 1);
455     }
456   }
457   for (y=0; y < (ssize_t) (height-1); y++)
458   {
459     p=pixels+((size_t) y << 1)*scaled_width;
460     q=p+scaled_width;
461     r=q+scaled_width;
462     for (x=0; x < (ssize_t) (width-1); x++)
463     {
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);
467       q+=2;
468       p+=2;
469       r+=2;
470     }
471     *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
472     *q++=(unsigned char) ((((size_t) *p++)+((size_t) *r++)+1) >> 1);
473   }
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));
477 }
478
479 static Image *ReadPCDImage(const ImageInfo *image_info,ExceptionInfo *exception)
480 {
481   Image
482     *image;
483
484   MagickBooleanType
485     status;
486
487   MagickOffsetType
488     offset;
489
490   MagickSizeType
491     number_pixels;
492
493   register ssize_t
494     i,
495     y;
496
497   register Quantum
498     *q;
499
500   register unsigned char
501     *c1,
502     *c2,
503     *yy;
504
505   size_t
506     height,
507     number_images,
508     rotate,
509     scene,
510     width;
511
512   ssize_t
513     count,
514     x;
515
516   unsigned char
517     *chroma1,
518     *chroma2,
519     *header,
520     *luma;
521
522   unsigned int
523     overview;
524
525   /*
526     Open image file.
527   */
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)
538     {
539       image=DestroyImageList(image);
540       return((Image *) NULL);
541     }
542   /*
543     Determine if this a PCD file.
544   */
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;
550   if ((count == 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);
558   /*
559     Determine resolution by scene specification.
560   */
561   if ((image->columns == 0) || (image->rows == 0))
562     scene=3;
563   else
564     {
565       width=192;
566       height=128;
567       for (scene=1; scene < 6; scene++)
568       {
569         if ((width >= image->columns) && (height >= image->rows))
570           break;
571         width<<=1;
572         height<<=1;
573       }
574     }
575   if (image_info->number_scenes != 0)
576     scene=(size_t) MagickMin(image_info->scene,6);
577   if (overview != 0)
578     scene=1;
579   /*
580     Initialize image structure.
581   */
582   width=192;
583   height=128;
584   for (i=1; i < (ssize_t) MagickMin(scene,3); i++)
585   {
586     width<<=1;
587     height<<=1;
588   }
589   image->columns=width;
590   image->rows=height;
591   image->depth=8;
592   for ( ; i < (ssize_t) scene; i++)
593   {
594     image->columns<<=1;
595     image->rows<<=1;
596   }
597   status=SetImageExtent(image,image->columns,image->rows,exception);
598   if (status == MagickFalse)
599     return(DestroyImageList(image));
600   /*
601     Allocate luma and 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     10*sizeof(*chroma1));
608   chroma2=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
609     10*sizeof(*chroma2));
610   luma=(unsigned char *) AcquireQuantumMemory(image->columns+1UL,image->rows*
611     10*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 != 0)
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 < (ssize_t) (offset*0x800); i++)
628     (void) ReadBlobByte(image);
629   if (overview != 0)
630     {
631       Image
632         *overview_image;
633
634       MagickProgressMonitor
635         progress_monitor;
636
637       register ssize_t
638         j;
639
640       /*
641         Read thumbnails from overview image.
642       */
643       for (j=1; j <= (ssize_t) number_images; j++)
644       {
645         progress_monitor=SetImageProgressMonitor(image,
646           (MagickProgressMonitor) NULL,image->client_data);
647         (void) FormatLocaleString(image->filename,MaxTextExtent,
648           "images/img%04ld.pcd",(long) j);
649         (void) FormatLocaleString(image->magick_filename,MaxTextExtent,
650           "images/img%04ld.pcd",(long) j);
651         image->scene=(size_t) 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 < (ssize_t) 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 < (ssize_t) image->rows; y++)
678         {
679           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
680           if (q == (Quantum *) NULL)
681             break;
682           for (x=0; x < (ssize_t) image->columns; x++)
683           {
684             SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
685             SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
686             SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
687             q+=GetPixelChannels(image);
688           }
689           if (SyncAuthenticPixels(image,exception) == MagickFalse)
690             break;
691         }
692         image->colorspace=YCCColorspace;
693         if (LocaleCompare(image_info->magick,"PCDS") == 0)
694           SetImageColorspace(image,sRGBColorspace,exception);
695         if (j < (ssize_t) number_images)
696           {
697             /*
698               Allocate next image structure.
699             */
700             AcquireNextImage(image_info,image,exception);
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 < (ssize_t) 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,exception);
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,exception);
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 < (ssize_t) image->rows; y++)
786   {
787     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
788     if (q == (Quantum *) NULL)
789       break;
790     for (x=0; x < (ssize_t) image->columns; x++)
791     {
792       SetPixelRed(image,ScaleCharToQuantum(*yy++),q);
793       SetPixelGreen(image,ScaleCharToQuantum(*c1++),q);
794       SetPixelBlue(image,ScaleCharToQuantum(*c2++),q);
795       q+=GetPixelChannels(image);
796     }
797     if (SyncAuthenticPixels(image,exception) == MagickFalse)
798       break;
799     if (image->previous == (Image *) NULL)
800       {
801         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
802                 image->rows);
803         if (status == MagickFalse)
804           break;
805       }
806   }
807   chroma2=(unsigned char *) RelinquishMagickMemory(chroma2);
808   chroma1=(unsigned char *) RelinquishMagickMemory(chroma1);
809   luma=(unsigned char *) RelinquishMagickMemory(luma);
810   if (EOFBlob(image) != MagickFalse)
811     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
812       image->filename);
813   (void) CloseBlob(image);
814   if (image_info->ping == MagickFalse)
815     if ((rotate == 1) || (rotate == 3))
816       {
817         double
818           degrees;
819
820         Image
821           *rotate_image;
822
823         /*
824           Rotate image.
825         */
826         degrees=rotate == 1 ? -90.0 : 90.0;
827         rotate_image=RotateImage(image,degrees,exception);
828         if (rotate_image != (Image *) NULL)
829           {
830             image=DestroyImage(image);
831             image=rotate_image;
832           }
833       }
834   /*
835     Set CCIR 709 primaries with a D65 white point.
836   */
837   image->chromaticity.red_primary.x=0.6400f;
838   image->chromaticity.red_primary.y=0.3300f;
839   image->chromaticity.green_primary.x=0.3000f;
840   image->chromaticity.green_primary.y=0.6000f;
841   image->chromaticity.blue_primary.x=0.1500f;
842   image->chromaticity.blue_primary.y=0.0600f;
843   image->chromaticity.white_point.x=0.3127f;
844   image->chromaticity.white_point.y=0.3290f;
845   image->gamma=1.000f/2.200f;
846   image->colorspace=YCCColorspace;
847   if (LocaleCompare(image_info->magick,"PCDS") == 0)
848     SetImageColorspace(image,sRGBColorspace,exception);
849   return(GetFirstImageInList(image));
850 }
851 \f
852 /*
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 %                                                                             %
855 %                                                                             %
856 %                                                                             %
857 %   R e g i s t e r P C D I m a g e                                           %
858 %                                                                             %
859 %                                                                             %
860 %                                                                             %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 %
863 %  RegisterPCDImage() adds attributes for the PCD image format to
864 %  the list of supported formats.  The attributes include the image format
865 %  tag, a method to read and/or write the format, whether the format
866 %  supports the saving of more than one frame to the same file or blob,
867 %  whether the format supports native in-memory I/O, and a brief
868 %  description of the format.
869 %
870 %  The format of the RegisterPCDImage method is:
871 %
872 %      size_t RegisterPCDImage(void)
873 %
874 */
875 ModuleExport size_t RegisterPCDImage(void)
876 {
877   MagickInfo
878     *entry;
879
880   entry=SetMagickInfo("PCD");
881   entry->decoder=(DecodeImageHandler *) ReadPCDImage;
882   entry->encoder=(EncodeImageHandler *) WritePCDImage;
883   entry->magick=(IsImageFormatHandler *) IsPCD;
884   entry->adjoin=MagickFalse;
885   entry->seekable_stream=MagickTrue;
886   entry->description=ConstantString("Photo CD");
887   entry->module=ConstantString("PCD");
888   (void) RegisterMagickInfo(entry);
889   entry=SetMagickInfo("PCDS");
890   entry->decoder=(DecodeImageHandler *) ReadPCDImage;
891   entry->encoder=(EncodeImageHandler *) WritePCDImage;
892   entry->adjoin=MagickFalse;
893   entry->seekable_stream=MagickTrue;
894   entry->description=ConstantString("Photo CD");
895   entry->module=ConstantString("PCD");
896   (void) RegisterMagickInfo(entry);
897   return(MagickImageCoderSignature);
898 }
899 \f
900 /*
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 %                                                                             %
903 %                                                                             %
904 %                                                                             %
905 %   U n r e g i s t e r P C D I m a g e                                       %
906 %                                                                             %
907 %                                                                             %
908 %                                                                             %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 %
911 %  UnregisterPCDImage() removes format registrations made by the
912 %  PCD module from the list of supported formats.
913 %
914 %  The format of the UnregisterPCDImage method is:
915 %
916 %      UnregisterPCDImage(void)
917 %
918 */
919 ModuleExport void UnregisterPCDImage(void)
920 {
921   (void) UnregisterMagickInfo("PCD");
922   (void) UnregisterMagickInfo("PCDS");
923 }
924 \f
925 /*
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 %                                                                             %
928 %                                                                             %
929 %                                                                             %
930 %   W r i t e P C D I m a g e                                                 %
931 %                                                                             %
932 %                                                                             %
933 %                                                                             %
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 %
936 %  WritePCDImage() writes an image in the Photo CD encoded image format.
937 %
938 %  The format of the WritePCDImage method is:
939 %
940 %      MagickBooleanType WritePCDImage(const ImageInfo *image_info,
941 %        Image *image,ExceptionInfo *exception)
942 %
943 %  A description of each parameter follows.
944 %
945 %    o image_info: the image info.
946 %
947 %    o image:  The image.
948 %
949 %    o exception: return any errors or warnings in this structure.
950 %
951 */
952
953 static MagickBooleanType WritePCDTile(Image *image,const char *page_geometry,
954   const char *tile_geometry,ExceptionInfo *exception)
955 {
956   GeometryInfo
957     geometry_info;
958
959   Image
960     *downsample_image,
961     *tile_image;
962
963   MagickBooleanType
964     status;
965
966   MagickStatusType
967     flags;
968
969   RectangleInfo
970     geometry;
971
972   register const Quantum
973     *p,
974     *q;
975
976   register ssize_t
977     i,
978     x;
979
980   ssize_t
981     y;
982
983   /*
984     Scale image to tile size.
985   */
986   SetGeometry(image,&geometry);
987   (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
988     &geometry.width,&geometry.height);
989   if ((geometry.width % 2) != 0)
990     geometry.width--;
991   if ((geometry.height % 2) != 0)
992     geometry.height--;
993   tile_image=ResizeImage(image,geometry.width,geometry.height,TriangleFilter,
994     exception);
995   if (tile_image == (Image *) NULL)
996     return(MagickFalse);
997   flags=ParseGeometry(page_geometry,&geometry_info);
998   geometry.width=(size_t) geometry_info.rho;
999   geometry.height=(size_t) geometry_info.sigma;
1000   if ((flags & SigmaValue) == 0)
1001     geometry.height=geometry.width;
1002   if ((tile_image->columns != geometry.width) ||
1003       (tile_image->rows != geometry.height))
1004     {
1005       Image
1006         *bordered_image;
1007
1008       RectangleInfo
1009         border_info;
1010
1011       /*
1012         Put a border around the image.
1013       */
1014       border_info.width=(geometry.width-tile_image->columns+1) >> 1;
1015       border_info.height=(geometry.height-tile_image->rows+1) >> 1;
1016       bordered_image=BorderImage(tile_image,&border_info,image->compose,
1017         exception);
1018       if (bordered_image == (Image *) NULL)
1019         return(MagickFalse);
1020       tile_image=DestroyImage(tile_image);
1021       tile_image=bordered_image;
1022     }
1023   (void) TransformImage(&tile_image,(char *) NULL,tile_geometry,exception);
1024   (void) TransformImageColorspace(tile_image,YCCColorspace,exception);
1025   downsample_image=ResizeImage(tile_image,tile_image->columns/2,
1026     tile_image->rows/2,TriangleFilter,exception);
1027   if (downsample_image == (Image *) NULL)
1028     return(MagickFalse);
1029   /*
1030     Write tile to PCD file.
1031   */
1032   for (y=0; y < (ssize_t) tile_image->rows; y+=2)
1033   {
1034     p=GetVirtualPixels(tile_image,0,y,tile_image->columns,2,exception);
1035     if (p == (const Quantum *) NULL)
1036       break;
1037     for (x=0; x < (ssize_t) (tile_image->columns << 1); x++)
1038     {
1039       (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(tile_image,p)));
1040       p+=GetPixelChannels(tile_image);
1041     }
1042     q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,1,
1043       exception);
1044     if (q == (Quantum *) NULL)
1045       break;
1046     for (x=0; x < (ssize_t) downsample_image->columns; x++)
1047     {
1048       (void) WriteBlobByte(image,ScaleQuantumToChar(
1049         GetPixelGreen(tile_image,q)));
1050       q++;
1051     }
1052     q=GetVirtualPixels(downsample_image,0,y >> 1,downsample_image->columns,1,
1053       exception);
1054     if (q == (Quantum *) NULL)
1055       break;
1056     for (x=0; x < (ssize_t) downsample_image->columns; x++)
1057     {
1058       (void) WriteBlobByte(image,ScaleQuantumToChar(
1059         GetPixelBlue(tile_image,q)));
1060       q++;
1061     }
1062     status=SetImageProgress(image,SaveImageTag,y,tile_image->rows);
1063     if (status == MagickFalse)
1064       break;
1065   }
1066   for (i=0; i < 0x800; i++)
1067     (void) WriteBlobByte(image,'\0');
1068   downsample_image=DestroyImage(downsample_image);
1069   tile_image=DestroyImage(tile_image);
1070   return(MagickTrue);
1071 }
1072
1073 static MagickBooleanType WritePCDImage(const ImageInfo *image_info,Image *image,
1074   ExceptionInfo *exception)
1075 {
1076   Image
1077     *pcd_image;
1078
1079   MagickBooleanType
1080     status;
1081
1082   register ssize_t
1083     i;
1084
1085   assert(image_info != (const ImageInfo *) NULL);
1086   assert(image_info->signature == MagickSignature);
1087   assert(image != (Image *) NULL);
1088   assert(image->signature == MagickSignature);
1089   if (image->debug != MagickFalse)
1090     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1091   pcd_image=image;
1092   if (image->columns < image->rows)
1093     {
1094       Image
1095         *rotate_image;
1096
1097       /*
1098         Rotate portrait to landscape.
1099       */
1100       rotate_image=RotateImage(image,90.0,exception);
1101       if (rotate_image == (Image *) NULL)
1102         return(MagickFalse);
1103       pcd_image=rotate_image;
1104     }
1105   /*
1106     Open output image file.
1107   */
1108   status=OpenBlob(image_info,pcd_image,WriteBinaryBlobMode,exception);
1109   if (status == MagickFalse)
1110     return(status);
1111   if (IssRGBCompatibleColorspace(pcd_image->colorspace) == MagickFalse)
1112     (void) TransformImageColorspace(pcd_image,sRGBColorspace,exception);
1113   /*
1114     Write PCD image header.
1115   */
1116   for (i=0; i < 32; i++)
1117     (void) WriteBlobByte(pcd_image,0xff);
1118   for (i=0; i < 4; i++)
1119     (void) WriteBlobByte(pcd_image,0x0e);
1120   for (i=0; i < 8; i++)
1121     (void) WriteBlobByte(pcd_image,'\0');
1122   for (i=0; i < 4; i++)
1123     (void) WriteBlobByte(pcd_image,0x01);
1124   for (i=0; i < 4; i++)
1125     (void) WriteBlobByte(pcd_image,0x05);
1126   for (i=0; i < 8; i++)
1127     (void) WriteBlobByte(pcd_image,'\0');
1128   for (i=0; i < 4; i++)
1129     (void) WriteBlobByte(pcd_image,0x0A);
1130   for (i=0; i < 36; i++)
1131     (void) WriteBlobByte(pcd_image,'\0');
1132   for (i=0; i < 4; i++)
1133     (void) WriteBlobByte(pcd_image,0x01);
1134   for (i=0; i < 1944; i++)
1135     (void) WriteBlobByte(pcd_image,'\0');
1136   (void) WriteBlob(pcd_image,7,(const unsigned char *) "PCD_IPI");
1137   (void) WriteBlobByte(pcd_image,0x06);
1138   for (i=0; i < 1530; i++)
1139     (void) WriteBlobByte(pcd_image,'\0');
1140   if (image->columns < image->rows)
1141     (void) WriteBlobByte(pcd_image,'\1');
1142   else
1143     (void) WriteBlobByte(pcd_image,'\0');
1144   for (i=0; i < (3*0x800-1539); i++)
1145     (void) WriteBlobByte(pcd_image,'\0');
1146   /*
1147     Write PCD tiles.
1148   */
1149   status=WritePCDTile(pcd_image,"768x512>","192x128",exception);
1150   status=WritePCDTile(pcd_image,"768x512>","384x256",exception);
1151   status=WritePCDTile(pcd_image,"768x512>","768x512",exception);
1152   (void) CloseBlob(pcd_image);
1153   if (pcd_image != image)
1154     pcd_image=DestroyImage(pcd_image);
1155   return(status);
1156 }