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