]> granicus.if.org Git - imagemagick/blob - coders/dpx.c
03b35cc2c8a234abf5f246fe7ac5e54e43df5cc5
[imagemagick] / coders / dpx.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            DDDD   PPPP   X   X                              %
7 %                            D   D  P   P   X X                               %
8 %                            D   D  PPPP    XXX                               %
9 %                            D   D  P       X X                               %
10 %                            DDDD   P      X   X                              %
11 %                                                                             %
12 %                                                                             %
13 %                     Read/Write SMTPE DPX Image Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                March 2001                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2019 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 %    https://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/attribute.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/geometry.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/module.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/option.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/profile.h"
63 #include "MagickCore/property.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
68 #include "MagickCore/timer-private.h"
69 \f
70 /*
71   Define declaration.
72 */
73 #define MaxNumberImageElements  8
74 \f
75 /*
76   Typedef declaration.
77 */
78 typedef enum
79 {
80   UserDefinedColorimetric = 0,
81   PrintingDensityColorimetric = 1,
82   LinearColorimetric = 2,
83   LogarithmicColorimetric = 3,
84   UnspecifiedVideoColorimetric = 4,
85   SMTPE_274MColorimetric = 5,
86   ITU_R709Colorimetric = 6,
87   ITU_R601_625LColorimetric = 7,
88   ITU_R601_525LColorimetric = 8,
89   NTSCCompositeVideoColorimetric = 9,
90   PALCompositeVideoColorimetric = 10,
91   ZDepthLinearColorimetric = 11,
92   DepthHomogeneousColorimetric = 12
93 } DPXColorimetric;
94
95 typedef enum
96 {
97   UndefinedComponentType = 0,
98   RedComponentType = 1,
99   GreenComponentType = 2,
100   BlueComponentType = 3,
101   AlphaComponentType = 4,
102   LumaComponentType = 6,
103   ColorDifferenceCbCrComponentType = 7,
104   DepthComponentType = 8,
105   CompositeVideoComponentType = 9,
106   RGBComponentType = 50,
107   RGBAComponentType = 51,
108   ABGRComponentType = 52,
109   CbYCrY422ComponentType = 100,
110   CbYACrYA4224ComponentType = 101,
111   CbYCr444ComponentType = 102,
112   CbYCrA4444ComponentType = 103,
113   UserDef2ElementComponentType = 150,
114   UserDef3ElementComponentType = 151,
115   UserDef4ElementComponentType = 152,
116   UserDef5ElementComponentType = 153,
117   UserDef6ElementComponentType = 154,
118   UserDef7ElementComponentType = 155,
119   UserDef8ElementComponentType = 156
120 } DPXComponentType;
121
122 typedef enum
123 {
124   TransferCharacteristicUserDefined = 0,
125   TransferCharacteristicPrintingDensity = 1,
126   TransferCharacteristicLinear = 2,
127   TransferCharacteristicLogarithmic = 3,
128   TransferCharacteristicUnspecifiedVideo = 4,
129   TransferCharacteristicSMTPE274M = 5,     /* 1920x1080 TV */
130   TransferCharacteristicITU_R709 = 6,      /* ITU R709 */
131   TransferCharacteristicITU_R601_625L = 7, /* 625 Line */
132   TransferCharacteristicITU_R601_525L = 8, /* 525 Line */
133   TransferCharacteristicNTSCCompositeVideo = 9,
134   TransferCharacteristicPALCompositeVideo = 10,
135   TransferCharacteristicZDepthLinear = 11,
136   TransferCharacteristicZDepthHomogeneous = 12
137 } DPXTransferCharacteristic;
138
139 typedef struct _DPXFileInfo
140 {
141   unsigned int
142     magic,
143     image_offset;
144
145   char
146     version[8];
147
148   unsigned int
149     file_size,
150     ditto_key,
151     generic_size,
152     industry_size,
153     user_size;
154
155   char
156     filename[100],
157     timestamp[24],
158     creator[100],
159     project[200],
160     copyright[200];
161
162   unsigned int
163     encrypt_key;
164
165   char
166     reserve[104];
167 } DPXFileInfo;
168
169 typedef struct _DPXFilmInfo
170 {
171   char
172     id[2],
173     type[2],
174     offset[2],
175     prefix[6],
176     count[4],
177     format[32];
178
179   unsigned int
180     frame_position,
181     sequence_extent,
182     held_count;
183
184   float
185     frame_rate,
186     shutter_angle;
187
188   char
189     frame_id[32],
190     slate[100],
191     reserve[56];
192 } DPXFilmInfo;
193
194 typedef struct _DPXImageElement
195 {
196   unsigned int
197     data_sign,
198     low_data;
199
200   float
201     low_quantity;
202
203   unsigned int
204     high_data;
205
206   float
207     high_quantity;
208
209   unsigned char
210     descriptor,
211     transfer_characteristic,
212     colorimetric,
213     bit_size;
214
215   unsigned short
216     packing,
217     encoding;
218
219   unsigned int
220     data_offset,
221     end_of_line_padding,
222     end_of_image_padding;
223
224   unsigned char
225     description[32];
226 } DPXImageElement;
227
228 typedef struct _DPXImageInfo
229 {
230   unsigned short
231     orientation,
232     number_elements;
233
234   unsigned int
235     pixels_per_line,
236     lines_per_element;
237
238   DPXImageElement
239     image_element[MaxNumberImageElements];
240
241   unsigned char
242     reserve[52];
243 } DPXImageInfo;
244
245 typedef struct _DPXOrientationInfo
246 {
247   unsigned int
248     x_offset,
249     y_offset;
250
251   float
252     x_center,
253     y_center;
254
255   unsigned int
256     x_size,
257     y_size;
258
259   char
260     filename[100],
261     timestamp[24],
262     device[32],
263     serial[32];
264
265   unsigned short
266     border[4];
267
268   unsigned int
269     aspect_ratio[2];
270
271   unsigned char
272     reserve[28];
273 } DPXOrientationInfo;
274
275 typedef struct _DPXTelevisionInfo
276 {
277   unsigned int
278     time_code,
279     user_bits;
280
281   unsigned char
282     interlace,
283     field_number,
284     video_signal,
285     padding;
286
287   float
288     horizontal_sample_rate,
289     vertical_sample_rate,
290     frame_rate,
291     time_offset,
292     gamma,
293     black_level,
294     black_gain,
295     break_point,
296     white_level,
297     integration_times;
298
299   char
300     reserve[76];
301 } DPXTelevisionInfo;
302
303 typedef struct _DPXUserInfo
304 {
305   char
306     id[32];
307 } DPXUserInfo;
308
309 typedef struct DPXInfo
310 {
311   DPXFileInfo
312     file;
313
314   DPXImageInfo
315     image;
316
317   DPXOrientationInfo
318     orientation;
319
320   DPXFilmInfo
321     film;
322
323   DPXTelevisionInfo
324     television;
325
326   DPXUserInfo
327     user;
328 } DPXInfo;
329 \f
330 /*
331   Forward declaractions.
332 */
333 static MagickBooleanType
334   WriteDPXImage(const ImageInfo *,Image *,ExceptionInfo *);
335 \f
336 /*
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 %                                                                             %
339 %                                                                             %
340 %                                                                             %
341 %   I s D P X                                                                 %
342 %                                                                             %
343 %                                                                             %
344 %                                                                             %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 %
347 %  IsDPX() returns MagickTrue if the image format type, identified by the
348 %  magick string, is DPX.
349 %
350 %  The format of the IsDPX method is:
351 %
352 %      MagickBooleanType IsDPX(const unsigned char *magick,const size_t extent)
353 %
354 %  A description of each parameter follows:
355 %
356 %    o magick: compare image format pattern against these bytes.
357 %
358 %    o extent: Specifies the extent of the magick string.
359 %
360 */
361 static MagickBooleanType IsDPX(const unsigned char *magick,const size_t extent)
362 {
363   if (extent < 4)
364     return(MagickFalse);
365   if (memcmp(magick,"SDPX",4) == 0)
366     return(MagickTrue);
367   if (memcmp(magick,"XPDS",4) == 0)
368     return(MagickTrue);
369   return(MagickFalse);
370 }
371 \f
372 /*
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 %                                                                             %
375 %                                                                             %
376 %                                                                             %
377 %   R e a d D P X I m a g e                                                   %
378 %                                                                             %
379 %                                                                             %
380 %                                                                             %
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 %
383 %  ReadDPXImage() reads an DPX X image file and returns it.  It
384 %  allocates the memory necessary for the new Image structure and returns a
385 %  pointer to the new image.
386 %
387 %  The format of the ReadDPXImage method is:
388 %
389 %      Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
390 %
391 %  A description of each parameter follows:
392 %
393 %    o image_info: the image info.
394 %
395 %    o exception: return any errors or warnings in this structure.
396 %
397 */
398
399 static size_t GetBytesPerRow(const size_t columns,
400   const size_t samples_per_pixel,const size_t bits_per_pixel,
401   const MagickBooleanType pad)
402 {
403   size_t
404     bytes_per_row;
405
406   switch (bits_per_pixel)
407   {
408     case 1:
409     {
410       bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+31)/
411         32);
412       break;
413     }
414     case 8:
415     default:
416     {
417       bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+31)/
418         32);
419       break;
420     }
421     case 10:
422     {
423       if (pad == MagickFalse)
424         {
425           bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+
426             31)/32);
427           break;
428         }
429       bytes_per_row=4*(((size_t) (32*((samples_per_pixel*columns+2)/3))+31)/32);
430       break;
431     }
432     case 12:
433     {
434       if (pad == MagickFalse)
435         {
436           bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+
437             31)/32);
438           break;
439         }
440       bytes_per_row=2*(((size_t) (16*samples_per_pixel*columns)+15)/16);
441       break;
442     }
443     case 16:
444     {
445       if (pad == MagickFalse)
446         {
447           bytes_per_row=2*(((size_t) samples_per_pixel*columns*bits_per_pixel+
448             15)/16);
449           break;
450         }
451       bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+31)/
452         32);
453       break;
454     }
455     case 32:
456     {
457       bytes_per_row=4*(((size_t) samples_per_pixel*columns*bits_per_pixel+31)/
458         32);
459       break;
460     }
461     case 64:
462     {
463       bytes_per_row=8*(((size_t) samples_per_pixel*columns*bits_per_pixel+63)/
464         64);
465       break;
466     }
467   }
468   return(bytes_per_row);
469 }
470
471 static const char *GetImageTransferCharacteristic(
472   const DPXTransferCharacteristic characteristic)
473 {
474   const char
475     *transfer;
476
477   /*
478     Get the element transfer characteristic.
479   */
480   switch(characteristic)
481   {
482     case TransferCharacteristicUserDefined:
483     {
484       transfer="UserDefined";
485       break;
486     }
487     case TransferCharacteristicPrintingDensity:
488     {
489       transfer="PrintingDensity";
490       break;
491     }
492     case TransferCharacteristicLinear:
493     {
494       transfer="Linear";
495       break;
496     }
497     case TransferCharacteristicLogarithmic:
498     {
499       transfer="Logarithmic";
500       break;
501     }
502     case TransferCharacteristicUnspecifiedVideo:
503     {
504       transfer="UnspecifiedVideo";
505       break;
506     }
507     case TransferCharacteristicSMTPE274M:
508     {
509       transfer="SMTPE274M";
510       break;
511     }
512     case TransferCharacteristicITU_R709:
513     {
514       transfer="ITU-R709";
515       break;
516     }
517     case TransferCharacteristicITU_R601_625L:
518     {
519       transfer="ITU-R601-625L";
520       break;
521     }
522     case TransferCharacteristicITU_R601_525L:
523     {
524       transfer="ITU-R601-525L";
525       break;
526     }
527     case TransferCharacteristicNTSCCompositeVideo:
528     {
529       transfer="NTSCCompositeVideo";
530       break;
531     }
532     case TransferCharacteristicPALCompositeVideo:
533     {
534       transfer="PALCompositeVideo";
535       break;
536     }
537     case TransferCharacteristicZDepthLinear:
538     {
539       transfer="ZDepthLinear";
540       break;
541     }
542     case TransferCharacteristicZDepthHomogeneous:
543     {
544       transfer="ZDepthHomogeneous";
545       break;
546     }
547     default:
548       transfer="Reserved";
549   }
550   return(transfer);
551 }
552
553 static inline MagickBooleanType IsFloatDefined(const float value)
554 {
555   union
556   {
557     unsigned int
558       unsigned_value;
559
560     float
561       float_value;
562   } quantum;
563
564   quantum.unsigned_value=0U;
565   quantum.float_value=(float) value;
566   if (quantum.unsigned_value == 0U)
567     return(MagickFalse);
568   return(MagickTrue);
569 }
570
571 static void SetPrimaryChromaticity(const DPXColorimetric colorimetric,
572   ChromaticityInfo *chromaticity_info)
573 {
574   switch(colorimetric)
575   {
576     case SMTPE_274MColorimetric:
577     case ITU_R709Colorimetric:
578     {
579       chromaticity_info->red_primary.x=0.640;
580       chromaticity_info->red_primary.y=0.330;
581       chromaticity_info->red_primary.z=0.030;
582       chromaticity_info->green_primary.x=0.300;
583       chromaticity_info->green_primary.y=0.600;
584       chromaticity_info->green_primary.z=0.100;
585       chromaticity_info->blue_primary.x=0.150;
586       chromaticity_info->blue_primary.y=0.060;
587       chromaticity_info->blue_primary.z=0.790;
588       chromaticity_info->white_point.x=0.3127;
589       chromaticity_info->white_point.y=0.3290;
590       chromaticity_info->white_point.z=0.3582;
591       break;
592     }
593     case NTSCCompositeVideoColorimetric:
594     {
595       chromaticity_info->red_primary.x=0.67;
596       chromaticity_info->red_primary.y=0.33;
597       chromaticity_info->red_primary.z=0.00;
598       chromaticity_info->green_primary.x=0.21;
599       chromaticity_info->green_primary.y=0.71;
600       chromaticity_info->green_primary.z=0.08;
601       chromaticity_info->blue_primary.x=0.14;
602       chromaticity_info->blue_primary.y=0.08;
603       chromaticity_info->blue_primary.z=0.78;
604       chromaticity_info->white_point.x=0.310;
605       chromaticity_info->white_point.y=0.316;
606       chromaticity_info->white_point.z=0.374;
607       break;
608     }
609     case PALCompositeVideoColorimetric:
610     {
611       chromaticity_info->red_primary.x=0.640;
612       chromaticity_info->red_primary.y=0.330;
613       chromaticity_info->red_primary.z=0.030;
614       chromaticity_info->green_primary.x=0.290;
615       chromaticity_info->green_primary.y=0.600;
616       chromaticity_info->green_primary.z=0.110;
617       chromaticity_info->blue_primary.x=0.150;
618       chromaticity_info->blue_primary.y=0.060;
619       chromaticity_info->blue_primary.z=0.790;
620       chromaticity_info->white_point.x=0.3127;
621       chromaticity_info->white_point.y=0.3290;
622       chromaticity_info->white_point.z=0.3582;
623       break;
624     }
625     default:
626       break;
627   }
628 }
629
630 static void TimeCodeToString(const size_t timestamp,char *code)
631 {
632 #define TimeFields  7
633
634   unsigned int
635     shift;
636
637   register ssize_t
638     i;
639
640   *code='\0';
641   shift=4*TimeFields;
642   for (i=0; i <= TimeFields; i++)
643   {
644     (void) FormatLocaleString(code,MagickPathExtent-strlen(code),"%x",
645       (unsigned int) ((timestamp >> shift) & 0x0fU));
646     code++;
647     if (((i % 2) != 0) && (i < TimeFields))
648       *code++=':';
649     shift-=4;
650     *code='\0';
651   }
652 }
653
654 static Image *ReadDPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
655 {
656   char
657     magick[4],
658     value[MagickPathExtent];
659
660   DPXInfo
661     dpx;
662
663   Image
664     *image;
665
666   MagickBooleanType
667     status;
668
669   MagickOffsetType
670     offset;
671
672   QuantumInfo
673     *quantum_info;
674
675   QuantumType
676     quantum_type;
677
678   register ssize_t
679     i;
680
681   size_t
682     extent,
683     samples_per_pixel;
684
685   ssize_t
686     count,
687     n,
688     row,
689     y;
690
691   unsigned char
692     component_type;
693
694   /*
695     Open image file.
696   */
697   assert(image_info != (const ImageInfo *) NULL);
698   assert(image_info->signature == MagickCoreSignature);
699   if (image_info->debug != MagickFalse)
700     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
701       image_info->filename);
702   assert(exception != (ExceptionInfo *) NULL);
703   assert(exception->signature == MagickCoreSignature);
704   image=AcquireImage(image_info,exception);
705   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
706   if (status == MagickFalse)
707     {
708       image=DestroyImageList(image);
709       return((Image *) NULL);
710     }
711   /*
712     Read DPX file header.
713   */
714   offset=0;
715   count=ReadBlob(image,4,(unsigned char *) magick);
716   offset+=count;
717   if ((count != 4) || ((LocaleNCompare(magick,"SDPX",4) != 0) &&
718       (LocaleNCompare((char *) magick,"XPDS",4) != 0)))
719     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
720   image->endian=LSBEndian;
721   if (LocaleNCompare(magick,"SDPX",4) == 0)
722     image->endian=MSBEndian;
723   (void) memset(&dpx,0,sizeof(dpx));
724   dpx.file.image_offset=ReadBlobLong(image);
725   offset+=4;
726   offset+=ReadBlob(image,sizeof(dpx.file.version),(unsigned char *)
727     dpx.file.version);
728   (void) FormatImageProperty(image,"dpx:file.version","%.8s",dpx.file.version);
729   dpx.file.file_size=ReadBlobLong(image);
730   if (0 && dpx.file.file_size > GetBlobSize(image))
731     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
732   offset+=4;
733   dpx.file.ditto_key=ReadBlobLong(image);
734   offset+=4;
735   if (dpx.file.ditto_key != ~0U)
736     (void) FormatImageProperty(image,"dpx:file.ditto.key","%u",
737       dpx.file.ditto_key);
738   dpx.file.generic_size=ReadBlobLong(image);
739   offset+=4;
740   dpx.file.industry_size=ReadBlobLong(image);
741   offset+=4;
742   dpx.file.user_size=ReadBlobLong(image);
743   offset+=4;
744   offset+=ReadBlob(image,sizeof(dpx.file.filename),(unsigned char *)
745     dpx.file.filename);
746   (void) FormatImageProperty(image,"dpx:file.filename","%.100s",
747     dpx.file.filename);
748   (void) FormatImageProperty(image,"document","%.100s",dpx.file.filename);
749   offset+=ReadBlob(image,sizeof(dpx.file.timestamp),(unsigned char *)
750     dpx.file.timestamp);
751   if (*dpx.file.timestamp != '\0')
752     (void) FormatImageProperty(image,"dpx:file.timestamp","%.24s",
753       dpx.file.timestamp);
754   offset+=ReadBlob(image,sizeof(dpx.file.creator),(unsigned char *)
755     dpx.file.creator);
756   if (*dpx.file.creator != '\0')
757     {
758       (void) FormatImageProperty(image,"dpx:file.creator","%.100s",
759         dpx.file.creator);
760       (void) FormatImageProperty(image,"software","%.100s",dpx.file.creator);
761     }
762   offset+=ReadBlob(image,sizeof(dpx.file.project),(unsigned char *)
763     dpx.file.project);
764   if (*dpx.file.project != '\0')
765     {
766       (void) FormatImageProperty(image,"dpx:file.project","%.200s",
767         dpx.file.project);
768       (void) FormatImageProperty(image,"comment","%.100s",dpx.file.project);
769     }
770   offset+=ReadBlob(image,sizeof(dpx.file.copyright),(unsigned char *)
771     dpx.file.copyright);
772   if (*dpx.file.copyright != '\0')
773     {
774       (void) FormatImageProperty(image,"dpx:file.copyright","%.200s",
775         dpx.file.copyright);
776       (void) FormatImageProperty(image,"copyright","%.100s",
777         dpx.file.copyright);
778     }
779   dpx.file.encrypt_key=ReadBlobLong(image);
780   offset+=4;
781   if (dpx.file.encrypt_key != ~0U)
782     (void) FormatImageProperty(image,"dpx:file.encrypt_key","%u",
783       dpx.file.encrypt_key);
784   offset+=ReadBlob(image,sizeof(dpx.file.reserve),(unsigned char *)
785     dpx.file.reserve);
786   /*
787     Read DPX image header.
788   */
789   dpx.image.orientation=ReadBlobShort(image);
790   if (dpx.image.orientation > 7)
791     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
792   offset+=2;
793   if (dpx.image.orientation != (unsigned short) ~0)
794     (void) FormatImageProperty(image,"dpx:image.orientation","%d",
795       dpx.image.orientation);
796   switch (dpx.image.orientation)
797   {
798     default:
799     case 0: image->orientation=TopLeftOrientation; break;
800     case 1: image->orientation=TopRightOrientation; break;
801     case 2: image->orientation=BottomLeftOrientation; break;
802     case 3: image->orientation=BottomRightOrientation; break;
803     case 4: image->orientation=LeftTopOrientation; break;
804     case 5: image->orientation=RightTopOrientation; break;
805     case 6: image->orientation=LeftBottomOrientation; break;
806     case 7: image->orientation=RightBottomOrientation; break;
807   }
808   dpx.image.number_elements=ReadBlobShort(image);
809   if ((dpx.image.number_elements < 1) ||
810       (dpx.image.number_elements > MaxNumberImageElements))
811     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
812   offset+=2;
813   dpx.image.pixels_per_line=ReadBlobLong(image);
814   offset+=4;
815   image->columns=dpx.image.pixels_per_line;
816   dpx.image.lines_per_element=ReadBlobLong(image);
817   offset+=4;
818   image->rows=dpx.image.lines_per_element;
819   for (i=0; i < 8; i++)
820   {
821     char
822       property[MagickPathExtent];
823
824     dpx.image.image_element[i].data_sign=ReadBlobLong(image);
825     offset+=4;
826     dpx.image.image_element[i].low_data=ReadBlobLong(image);
827     offset+=4;
828     dpx.image.image_element[i].low_quantity=ReadBlobFloat(image);
829     offset+=4;
830     dpx.image.image_element[i].high_data=ReadBlobLong(image);
831     offset+=4;
832     dpx.image.image_element[i].high_quantity=ReadBlobFloat(image);
833     offset+=4;
834     dpx.image.image_element[i].descriptor=(unsigned char) ReadBlobByte(image);
835     offset++;
836     dpx.image.image_element[i].transfer_characteristic=(unsigned char)
837       ReadBlobByte(image);
838     (void) FormatLocaleString(property,MagickPathExtent,
839       "dpx:image.element[%lu].transfer-characteristic",(long) i);
840     (void) FormatImageProperty(image,property,"%s",
841       GetImageTransferCharacteristic((DPXTransferCharacteristic)
842       dpx.image.image_element[i].transfer_characteristic));
843     offset++;
844     dpx.image.image_element[i].colorimetric=(unsigned char) ReadBlobByte(image);
845     offset++;
846     dpx.image.image_element[i].bit_size=(unsigned char) ReadBlobByte(image);
847     offset++;
848     dpx.image.image_element[i].packing=ReadBlobShort(image);
849     offset+=2;
850     dpx.image.image_element[i].encoding=ReadBlobShort(image);
851     offset+=2;
852     dpx.image.image_element[i].data_offset=ReadBlobLong(image);
853     offset+=4;
854     dpx.image.image_element[i].end_of_line_padding=ReadBlobLong(image);
855     offset+=4;
856     dpx.image.image_element[i].end_of_image_padding=ReadBlobLong(image);
857     offset+=4;
858     offset+=ReadBlob(image,sizeof(dpx.image.image_element[i].description),
859       (unsigned char *) dpx.image.image_element[i].description);
860   }
861   SetImageColorspace(image,RGBColorspace,exception);
862   offset+=ReadBlob(image,sizeof(dpx.image.reserve),(unsigned char *)
863     dpx.image.reserve);
864   if (dpx.file.image_offset >= 1664U)
865     {
866       /*
867         Read DPX orientation header.
868       */
869       dpx.orientation.x_offset=ReadBlobLong(image);
870       offset+=4;
871       if (dpx.orientation.x_offset != ~0U)
872         (void) FormatImageProperty(image,"dpx:orientation.x_offset","%u",
873           dpx.orientation.x_offset);
874       dpx.orientation.y_offset=ReadBlobLong(image);
875       offset+=4;
876       if (dpx.orientation.y_offset != ~0U)
877         (void) FormatImageProperty(image,"dpx:orientation.y_offset","%u",
878           dpx.orientation.y_offset);
879       dpx.orientation.x_center=ReadBlobFloat(image);
880       offset+=4;
881       if (IsFloatDefined(dpx.orientation.x_center) != MagickFalse)
882         (void) FormatImageProperty(image,"dpx:orientation.x_center","%g",
883           dpx.orientation.x_center);
884       dpx.orientation.y_center=ReadBlobFloat(image);
885       offset+=4;
886       if (IsFloatDefined(dpx.orientation.y_center) != MagickFalse)
887         (void) FormatImageProperty(image,"dpx:orientation.y_center","%g",
888           dpx.orientation.y_center);
889       dpx.orientation.x_size=ReadBlobLong(image);
890       offset+=4;
891       if (dpx.orientation.x_size != ~0U)
892         (void) FormatImageProperty(image,"dpx:orientation.x_size","%u",
893           dpx.orientation.x_size);
894       dpx.orientation.y_size=ReadBlobLong(image);
895       offset+=4;
896       if (dpx.orientation.y_size != ~0U)
897         (void) FormatImageProperty(image,"dpx:orientation.y_size","%u",
898           dpx.orientation.y_size);
899       offset+=ReadBlob(image,sizeof(dpx.orientation.filename),(unsigned char *)
900         dpx.orientation.filename);
901       if (*dpx.orientation.filename != '\0')
902         (void) FormatImageProperty(image,"dpx:orientation.filename","%.100s",
903           dpx.orientation.filename);
904       offset+=ReadBlob(image,sizeof(dpx.orientation.timestamp),(unsigned char *)
905         dpx.orientation.timestamp);
906       if (*dpx.orientation.timestamp != '\0')
907         (void) FormatImageProperty(image,"dpx:orientation.timestamp","%.24s",
908           dpx.orientation.timestamp);
909       offset+=ReadBlob(image,sizeof(dpx.orientation.device),(unsigned char *)
910         dpx.orientation.device);
911       if (*dpx.orientation.device != '\0')
912         (void) FormatImageProperty(image,"dpx:orientation.device","%.32s",
913           dpx.orientation.device);
914       offset+=ReadBlob(image,sizeof(dpx.orientation.serial),(unsigned char *)
915         dpx.orientation.serial);
916       if (*dpx.orientation.serial != '\0')
917         (void) FormatImageProperty(image,"dpx:orientation.serial","%.32s",
918           dpx.orientation.serial);
919       for (i=0; i < 4; i++)
920       {
921         dpx.orientation.border[i]=ReadBlobShort(image);
922         offset+=2;
923       }
924       if ((dpx.orientation.border[0] != (unsigned short) (~0)) &&
925           (dpx.orientation.border[1] != (unsigned short) (~0)))
926         (void) FormatImageProperty(image,"dpx:orientation.border","%dx%d%+d%+d",
927           dpx.orientation.border[0],dpx.orientation.border[1],
928           dpx.orientation.border[2],dpx.orientation.border[3]);
929       for (i=0; i < 2; i++)
930       {
931         dpx.orientation.aspect_ratio[i]=ReadBlobLong(image);
932         offset+=4;
933       }
934       if ((dpx.orientation.aspect_ratio[0] != ~0U) &&
935           (dpx.orientation.aspect_ratio[1] != ~0U))
936         (void) FormatImageProperty(image,"dpx:orientation.aspect_ratio",
937           "%ux%u",dpx.orientation.aspect_ratio[0],
938           dpx.orientation.aspect_ratio[1]);
939       offset+=ReadBlob(image,sizeof(dpx.orientation.reserve),(unsigned char *)
940         dpx.orientation.reserve);
941     }
942   if (dpx.file.image_offset >= 1920U)
943     {
944       /*
945         Read DPX film header.
946       */
947       offset+=ReadBlob(image,sizeof(dpx.film.id),(unsigned char *) dpx.film.id);
948       if (*dpx.film.id != '\0')
949         (void) FormatImageProperty(image,"dpx:film.id","%.2s",dpx.film.id);
950       offset+=ReadBlob(image,sizeof(dpx.film.type),(unsigned char *)
951         dpx.film.type);
952       if (*dpx.film.type != '\0')
953         (void) FormatImageProperty(image,"dpx:film.type","%.2s",dpx.film.type);
954       offset+=ReadBlob(image,sizeof(dpx.film.offset),(unsigned char *)
955         dpx.film.offset);
956       if (*dpx.film.offset != '\0')
957         (void) FormatImageProperty(image,"dpx:film.offset","%.2s",
958           dpx.film.offset);
959       offset+=ReadBlob(image,sizeof(dpx.film.prefix),(unsigned char *)
960         dpx.film.prefix);
961       if (*dpx.film.prefix != '\0')
962         (void) FormatImageProperty(image,"dpx:film.prefix","%.6s",
963           dpx.film.prefix);
964       offset+=ReadBlob(image,sizeof(dpx.film.count),(unsigned char *)
965         dpx.film.count);
966       if (*dpx.film.count != '\0')
967         (void) FormatImageProperty(image,"dpx:film.count","%.4s",
968           dpx.film.count);
969       offset+=ReadBlob(image,sizeof(dpx.film.format),(unsigned char *)
970         dpx.film.format);
971       if (*dpx.film.format != '\0')
972         (void) FormatImageProperty(image,"dpx:film.format","%.4s",
973           dpx.film.format);
974       dpx.film.frame_position=ReadBlobLong(image);
975       offset+=4;
976       if (dpx.film.frame_position != ~0U)
977         (void) FormatImageProperty(image,"dpx:film.frame_position","%u",
978           dpx.film.frame_position);
979       dpx.film.sequence_extent=ReadBlobLong(image);
980       offset+=4;
981       if (dpx.film.sequence_extent != ~0U)
982         (void) FormatImageProperty(image,"dpx:film.sequence_extent","%u",
983           dpx.film.sequence_extent);
984       dpx.film.held_count=ReadBlobLong(image);
985       offset+=4;
986       if (dpx.film.held_count != ~0U)
987         (void) FormatImageProperty(image,"dpx:film.held_count","%u",
988           dpx.film.held_count);
989       dpx.film.frame_rate=ReadBlobFloat(image);
990       offset+=4;
991       if (IsFloatDefined(dpx.film.frame_rate) != MagickFalse)
992         (void) FormatImageProperty(image,"dpx:film.frame_rate","%g",
993           dpx.film.frame_rate);
994       dpx.film.shutter_angle=ReadBlobFloat(image);
995       offset+=4;
996       if (IsFloatDefined(dpx.film.shutter_angle) != MagickFalse)
997         (void) FormatImageProperty(image,"dpx:film.shutter_angle","%g",
998           dpx.film.shutter_angle);
999       offset+=ReadBlob(image,sizeof(dpx.film.frame_id),(unsigned char *)
1000         dpx.film.frame_id);
1001       if (*dpx.film.frame_id != '\0')
1002         (void) FormatImageProperty(image,"dpx:film.frame_id","%.32s",
1003           dpx.film.frame_id);
1004       offset+=ReadBlob(image,sizeof(dpx.film.slate),(unsigned char *)
1005         dpx.film.slate);
1006       if (*dpx.film.slate != '\0')
1007         (void) FormatImageProperty(image,"dpx:film.slate","%.100s",
1008           dpx.film.slate);
1009       offset+=ReadBlob(image,sizeof(dpx.film.reserve),(unsigned char *)
1010         dpx.film.reserve);
1011     }
1012   if (dpx.file.image_offset >= 2048U)
1013     {
1014       /*
1015         Read DPX television header.
1016       */
1017       dpx.television.time_code=(unsigned int) ReadBlobLong(image);
1018       offset+=4;
1019       TimeCodeToString(dpx.television.time_code,value);
1020       (void) SetImageProperty(image,"dpx:television.time.code",value,exception);
1021       dpx.television.user_bits=(unsigned int) ReadBlobLong(image);
1022       offset+=4;
1023       TimeCodeToString(dpx.television.user_bits,value);
1024       (void) SetImageProperty(image,"dpx:television.user.bits",value,exception);
1025       dpx.television.interlace=(unsigned char) ReadBlobByte(image);
1026       offset++;
1027       if (dpx.television.interlace != 0)
1028         (void) FormatImageProperty(image,"dpx:television.interlace","%.20g",
1029           (double) dpx.television.interlace);
1030       dpx.television.field_number=(unsigned char) ReadBlobByte(image);
1031       offset++;
1032       if (dpx.television.field_number != 0)
1033         (void) FormatImageProperty(image,"dpx:television.field_number","%.20g",
1034           (double) dpx.television.field_number);
1035       dpx.television.video_signal=(unsigned char) ReadBlobByte(image);
1036       offset++;
1037       if (dpx.television.video_signal != 0)
1038         (void) FormatImageProperty(image,"dpx:television.video_signal","%.20g",
1039           (double) dpx.television.video_signal);
1040       dpx.television.padding=(unsigned char) ReadBlobByte(image);
1041       offset++;
1042       if (dpx.television.padding != 0)
1043         (void) FormatImageProperty(image,"dpx:television.padding","%d",
1044           dpx.television.padding);
1045       dpx.television.horizontal_sample_rate=ReadBlobFloat(image);
1046       offset+=4;
1047       if (IsFloatDefined(dpx.television.horizontal_sample_rate) != MagickFalse)
1048         (void) FormatImageProperty(image,
1049           "dpx:television.horizontal_sample_rate","%g",
1050           dpx.television.horizontal_sample_rate);
1051       dpx.television.vertical_sample_rate=ReadBlobFloat(image);
1052       offset+=4;
1053       if (IsFloatDefined(dpx.television.vertical_sample_rate) != MagickFalse)
1054         (void) FormatImageProperty(image,"dpx:television.vertical_sample_rate",
1055           "%g",dpx.television.vertical_sample_rate);
1056       dpx.television.frame_rate=ReadBlobFloat(image);
1057       offset+=4;
1058       if (IsFloatDefined(dpx.television.frame_rate) != MagickFalse)
1059         (void) FormatImageProperty(image,"dpx:television.frame_rate","%g",
1060           dpx.television.frame_rate);
1061       dpx.television.time_offset=ReadBlobFloat(image);
1062       offset+=4;
1063       if (IsFloatDefined(dpx.television.time_offset) != MagickFalse)
1064         (void) FormatImageProperty(image,"dpx:television.time_offset","%g",
1065           dpx.television.time_offset);
1066       dpx.television.gamma=ReadBlobFloat(image);
1067       offset+=4;
1068       if (IsFloatDefined(dpx.television.gamma) != MagickFalse)
1069         (void) FormatImageProperty(image,"dpx:television.gamma","%g",
1070           dpx.television.gamma);
1071       dpx.television.black_level=ReadBlobFloat(image);
1072       offset+=4;
1073       if (IsFloatDefined(dpx.television.black_level) != MagickFalse)
1074         (void) FormatImageProperty(image,"dpx:television.black_level","%g",
1075           dpx.television.black_level);
1076       dpx.television.black_gain=ReadBlobFloat(image);
1077       offset+=4;
1078       if (IsFloatDefined(dpx.television.black_gain) != MagickFalse)
1079         (void) FormatImageProperty(image,"dpx:television.black_gain","%g",
1080           dpx.television.black_gain);
1081       dpx.television.break_point=ReadBlobFloat(image);
1082       offset+=4;
1083       if (IsFloatDefined(dpx.television.break_point) != MagickFalse)
1084         (void) FormatImageProperty(image,"dpx:television.break_point","%g",
1085           dpx.television.break_point);
1086       dpx.television.white_level=ReadBlobFloat(image);
1087       offset+=4;
1088       if (IsFloatDefined(dpx.television.white_level) != MagickFalse)
1089         (void) FormatImageProperty(image,"dpx:television.white_level","%g",
1090           dpx.television.white_level);
1091       dpx.television.integration_times=ReadBlobFloat(image);
1092       offset+=4;
1093       if (IsFloatDefined(dpx.television.integration_times) != MagickFalse)
1094         (void) FormatImageProperty(image,"dpx:television.integration_times",
1095           "%g",dpx.television.integration_times);
1096       offset+=ReadBlob(image,sizeof(dpx.television.reserve),(unsigned char *)
1097         dpx.television.reserve);
1098     }
1099   if (dpx.file.image_offset > 2080U)
1100     {
1101       /*
1102         Read DPX user header.
1103       */
1104       offset+=ReadBlob(image,sizeof(dpx.user.id),(unsigned char *) dpx.user.id);
1105       if (*dpx.user.id != '\0')
1106         (void) FormatImageProperty(image,"dpx:user.id","%.32s",dpx.user.id);
1107       if ((dpx.file.user_size != ~0U) &&
1108           ((size_t) dpx.file.user_size > sizeof(dpx.user.id)))
1109         {
1110           StringInfo
1111             *profile;
1112
1113            if (dpx.file.user_size > GetBlobSize(image))
1114              ThrowReaderException(CorruptImageError,
1115                "InsufficientImageDataInFile");
1116            profile=BlobToStringInfo((const unsigned char *) NULL,
1117              dpx.file.user_size-sizeof(dpx.user.id));
1118            if (profile == (StringInfo *) NULL)
1119              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1120            offset+=ReadBlob(image,GetStringInfoLength(profile),
1121              GetStringInfoDatum(profile));
1122            if (EOFBlob(image) != MagickFalse)
1123              (void) SetImageProfile(image,"dpx:user-data",profile,exception);
1124            profile=DestroyStringInfo(profile);
1125         }
1126     }
1127   for ( ; offset < (MagickOffsetType) dpx.file.image_offset; offset++)
1128     if (ReadBlobByte(image) == EOF)
1129       break;
1130   if (EOFBlob(image) != MagickFalse)
1131     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1132       image->filename);
1133   if (image_info->ping != MagickFalse)
1134     {
1135       (void) CloseBlob(image);
1136       return(GetFirstImageInList(image));
1137     }
1138   status=SetImageExtent(image,image->columns,image->rows,exception);
1139   if (status == MagickFalse)
1140     return(DestroyImageList(image));
1141   status=ResetImagePixels(image,exception);
1142   if (status == MagickFalse)
1143     return(DestroyImageList(image));
1144   for (n=0; n < (ssize_t) dpx.image.number_elements; n++)
1145   {
1146     /*
1147       Convert DPX raster image to pixel packets.
1148     */
1149     if ((dpx.image.image_element[n].data_offset != ~0U) &&
1150         (dpx.image.image_element[n].data_offset != 0U))
1151       {
1152          MagickOffsetType
1153            data_offset;
1154
1155          data_offset=(MagickOffsetType) dpx.image.image_element[n].data_offset;
1156          if (data_offset < offset)
1157            offset=SeekBlob(image,data_offset,SEEK_SET);
1158          else
1159            for ( ; offset < data_offset; offset++)
1160              if (ReadBlobByte(image) == EOF)
1161                break;
1162           if (offset != data_offset)
1163             ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1164        }
1165     SetPrimaryChromaticity((DPXColorimetric)
1166       dpx.image.image_element[n].colorimetric,&image->chromaticity);
1167     image->depth=dpx.image.image_element[n].bit_size;
1168     if ((image->depth == 0) || (image->depth > 32))
1169       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1170     samples_per_pixel=1;
1171     quantum_type=GrayQuantum;
1172     component_type=dpx.image.image_element[n].descriptor;
1173     switch (component_type)
1174     {
1175       case CbYCrY422ComponentType:
1176       {
1177         samples_per_pixel=2;
1178         quantum_type=CbYCrYQuantum;
1179         break;
1180       }
1181       case CbYACrYA4224ComponentType:
1182       case CbYCr444ComponentType:
1183       {
1184         samples_per_pixel=3;
1185         quantum_type=CbYCrQuantum;
1186         break;
1187       }
1188       case RGBComponentType:
1189       {
1190         samples_per_pixel=3;
1191         quantum_type=RGBQuantum;
1192         break;
1193       }
1194       case ABGRComponentType:
1195       case RGBAComponentType:
1196       {
1197         image->alpha_trait=BlendPixelTrait;
1198         samples_per_pixel=4;
1199         quantum_type=RGBAQuantum;
1200         break;
1201       }
1202       default:
1203         break;
1204     }
1205     switch (component_type)
1206     {
1207       case CbYCrY422ComponentType:
1208       case CbYACrYA4224ComponentType:
1209       case CbYCr444ComponentType:
1210       {
1211         SetImageColorspace(image,Rec709YCbCrColorspace,exception);
1212         break;
1213       }
1214       case LumaComponentType:
1215       {
1216         SetImageColorspace(image,GRAYColorspace,exception);
1217         break;
1218       }
1219       default:
1220       {
1221         SetImageColorspace(image,sRGBColorspace,exception);
1222         if (dpx.image.image_element[n].transfer_characteristic == LogarithmicColorimetric)
1223           SetImageColorspace(image,LogColorspace,exception);
1224         if (dpx.image.image_element[n].transfer_characteristic == PrintingDensityColorimetric)
1225           SetImageColorspace(image,LogColorspace,exception);
1226         break;
1227       }
1228     }
1229     extent=GetBytesPerRow(image->columns,samples_per_pixel,image->depth,
1230       dpx.image.image_element[n].packing == 0 ? MagickFalse : MagickTrue);
1231     /*
1232       DPX any-bit pixel format.
1233     */
1234     row=0;
1235     quantum_info=AcquireQuantumInfo(image_info,image);
1236     if (quantum_info == (QuantumInfo *) NULL)
1237       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1238     SetQuantumQuantum(quantum_info,32);
1239     SetQuantumPack(quantum_info,dpx.image.image_element[n].packing == 0 ?
1240       MagickTrue : MagickFalse);
1241     for (y=0; y < (ssize_t) image->rows; y++)
1242     {
1243       const unsigned char
1244         *pixels;
1245
1246       MagickBooleanType
1247         sync;
1248
1249       register Quantum
1250         *q;
1251
1252       size_t
1253         length;
1254
1255       ssize_t
1256         offset;
1257
1258       pixels=(const unsigned char *) ReadBlobStream(image,extent,
1259         GetQuantumPixels(quantum_info),&count);
1260       if (count != (ssize_t) extent)
1261         break;
1262       if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1263           (image->previous == (Image *) NULL))
1264         {
1265           MagickBooleanType
1266             proceed;
1267
1268           proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) row,
1269             image->rows);
1270           if (proceed == MagickFalse)
1271             break;
1272         }
1273       offset=row++;
1274       q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1275       if (q == (Quantum *) NULL)
1276         break;
1277       length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1278         quantum_type,pixels,exception);
1279       (void) length;
1280       sync=SyncAuthenticPixels(image,exception);
1281       if (sync == MagickFalse)
1282         break;
1283     }
1284     quantum_info=DestroyQuantumInfo(quantum_info);
1285     if (y < (ssize_t) image->rows)
1286       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1287     SetQuantumImageType(image,quantum_type);
1288     if (EOFBlob(image) != MagickFalse)
1289       ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1290         image->filename);
1291     if ((i+1) < (ssize_t) dpx.image.number_elements)
1292       {
1293         /*
1294           Allocate next image structure.
1295         */
1296         AcquireNextImage(image_info,image,exception);
1297         if (GetNextImageInList(image) == (Image *) NULL)
1298           {
1299             status=MagickFalse;
1300             break;
1301           }
1302         image=SyncNextImageInList(image);
1303         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1304           GetBlobSize(image));
1305         if (status == MagickFalse)
1306           break;
1307       }
1308   }
1309   (void) CloseBlob(image);
1310   if (status == MagickFalse)
1311     return(DestroyImageList(image));
1312   return(GetFirstImageInList(image));
1313 }
1314 \f
1315 /*
1316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1317 %                                                                             %
1318 %                                                                             %
1319 %                                                                             %
1320 %   R e g i s t e r D P X I m a g e                                           %
1321 %                                                                             %
1322 %                                                                             %
1323 %                                                                             %
1324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1325 %
1326 %  RegisterDPXImage() adds properties for the DPX image format to
1327 %  the list of supported formats.  The properties include the image format
1328 %  tag, a method to read and/or write the format, whether the format
1329 %  supports the saving of more than one frame to the same file or blob,
1330 %  whether the format supports native in-memory I/O, and a brief
1331 %  description of the format.
1332 %
1333 %  The format of the RegisterDPXImage method is:
1334 %
1335 %      size_t RegisterDPXImage(void)
1336 %
1337 */
1338 ModuleExport size_t RegisterDPXImage(void)
1339 {
1340   MagickInfo
1341     *entry;
1342
1343   static const char
1344     *DPXNote =
1345     {
1346       "Digital Moving Picture Exchange Bitmap, Version 2.0.\n"
1347       "See SMPTE 268M-2003 specification at http://www.smtpe.org\n"
1348     };
1349
1350   entry=AcquireMagickInfo("DPX","DPX","SMPTE 268M-2003 (DPX 2.0)");
1351   entry->decoder=(DecodeImageHandler *) ReadDPXImage;
1352   entry->encoder=(EncodeImageHandler *) WriteDPXImage;
1353   entry->magick=(IsImageFormatHandler *) IsDPX;
1354   entry->flags^=CoderAdjoinFlag;
1355   entry->flags|=CoderDecoderSeekableStreamFlag;
1356   entry->note=ConstantString(DPXNote);
1357   (void) RegisterMagickInfo(entry);
1358   return(MagickImageCoderSignature);
1359 }
1360 \f
1361 /*
1362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363 %                                                                             %
1364 %                                                                             %
1365 %                                                                             %
1366 %   U n r e g i s t e r D P X I m a g e                                       %
1367 %                                                                             %
1368 %                                                                             %
1369 %                                                                             %
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 %
1372 %  UnregisterDPXImage() removes format registrations made by the
1373 %  DPX module from the list of supported formats.
1374 %
1375 %  The format of the UnregisterDPXImage method is:
1376 %
1377 %      UnregisterDPXImage(void)
1378 %
1379 */
1380 ModuleExport void UnregisterDPXImage(void)
1381 {
1382   (void) UnregisterMagickInfo("DPX");
1383 }
1384 \f
1385 /*
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 %                                                                             %
1388 %                                                                             %
1389 %                                                                             %
1390 %   W r i t e D P X I m a g e                                                 %
1391 %                                                                             %
1392 %                                                                             %
1393 %                                                                             %
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 %
1396 %  WriteDPXImage() writes an image in DPX encoded image format.
1397 %
1398 %  The format of the WriteDPXImage method is:
1399 %
1400 %      MagickBooleanType WriteDPXImage(const ImageInfo *image_info,
1401 %        Image *image,ExceptionInfo *exception)
1402 %
1403 %  A description of each parameter follows.
1404 %
1405 %    o image_info: the image info.
1406 %
1407 %    o image:  The image.
1408 %
1409 %    o exception: return any errors or warnings in this structure.
1410 %
1411 */
1412
1413 static unsigned int StringToTimeCode(const char *key)
1414 {
1415   char
1416     buffer[2];
1417
1418   register ssize_t
1419     i;
1420
1421   unsigned int
1422     shift,
1423     value;
1424
1425   value=0;
1426   shift=28;
1427   buffer[1]='\0';
1428   for (i=0; (*key != 0) && (i < 11); i++)
1429   {
1430     if (isxdigit((int) ((unsigned char) *key)) == 0)
1431       {
1432         key++;
1433         continue;
1434       }
1435     buffer[0]=(*key++);
1436     value|=(unsigned int) ((strtol(buffer,(char **) NULL,16)) << shift);
1437     shift-=4;
1438   }
1439   return(value);
1440 }
1441
1442 static inline const char *GetDPXProperty(const Image *image,
1443   const char *property,ExceptionInfo *exception)
1444 {
1445   const char
1446     *value;
1447
1448   value=GetImageArtifact(image,property);
1449   if (value != (const char *) NULL)
1450     return(value);
1451   return(GetImageProperty(image,property,exception));
1452 }
1453
1454 static MagickBooleanType WriteDPXImage(const ImageInfo *image_info,Image *image,
1455   ExceptionInfo *exception)
1456 {
1457   const char
1458     *value;
1459
1460   const StringInfo
1461     *profile;
1462
1463   DPXInfo
1464     dpx;
1465
1466   GeometryInfo
1467     geometry_info;
1468
1469   MagickBooleanType
1470     status;
1471
1472   MagickOffsetType
1473     offset;
1474
1475   MagickStatusType
1476     flags;
1477
1478   QuantumInfo
1479     *quantum_info;
1480
1481   QuantumType
1482     quantum_type;
1483
1484   register const Quantum
1485     *p;
1486
1487   register ssize_t
1488     i;
1489
1490   size_t
1491     channels,
1492     extent;
1493
1494   ssize_t
1495     count,
1496     horizontal_factor,
1497     vertical_factor,
1498     y;
1499
1500   time_t
1501     seconds;
1502
1503   unsigned char
1504     *pixels;
1505
1506   /*
1507     Open output image file.
1508   */
1509   assert(image_info != (const ImageInfo *) NULL);
1510   assert(image_info->signature == MagickCoreSignature);
1511   assert(image != (Image *) NULL);
1512   assert(image->signature == MagickCoreSignature);
1513   if (image->debug != MagickFalse)
1514     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1515   horizontal_factor=4;
1516   vertical_factor=4;
1517   if (image_info->sampling_factor != (char *) NULL)
1518     {
1519       flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
1520       horizontal_factor=(ssize_t) geometry_info.rho;
1521       vertical_factor=(ssize_t) geometry_info.sigma;
1522       if ((flags & SigmaValue) == 0)
1523         vertical_factor=horizontal_factor;
1524       if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
1525           (horizontal_factor != 4) && (vertical_factor != 1) &&
1526           (vertical_factor != 2) && (vertical_factor != 4))
1527         ThrowWriterException(CorruptImageError,"UnexpectedSamplingFactor");
1528     }
1529   if ((image->colorspace == YCbCrColorspace) &&
1530       ((horizontal_factor == 2) || (vertical_factor == 2)))
1531     if ((image->columns % 2) != 0)
1532       image->columns++;
1533   assert(exception != (ExceptionInfo *) NULL);
1534   assert(exception->signature == MagickCoreSignature);
1535   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1536   if (status == MagickFalse)
1537     return(status);
1538   /*
1539     Write file header.
1540   */
1541   (void) memset(&dpx,0,sizeof(dpx));
1542   offset=0;
1543   dpx.file.magic=0x53445058U;
1544   offset+=WriteBlobLong(image,dpx.file.magic);
1545   dpx.file.image_offset=0x2000U;
1546   profile=GetImageProfile(image,"dpx:user-data");
1547   if (profile != (StringInfo *) NULL)
1548     {
1549       if (GetStringInfoLength(profile) > 1048576)
1550         ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1551       dpx.file.image_offset+=(unsigned int) GetStringInfoLength(profile);
1552       dpx.file.image_offset=(((dpx.file.image_offset+0x2000-1)/0x2000)*0x2000);
1553     }
1554   offset+=WriteBlobLong(image,dpx.file.image_offset);
1555   (void) strncpy(dpx.file.version,"V2.0",sizeof(dpx.file.version)-1);
1556   offset+=WriteBlob(image,8,(unsigned char *) &dpx.file.version);
1557   channels=1;
1558   if (IsImageGray(image) == MagickFalse)
1559     channels=3;
1560   if (image->alpha_trait != UndefinedPixelTrait)
1561     channels++;
1562   dpx.file.file_size=(unsigned int) (channels*image->columns*image->rows+
1563     dpx.file.image_offset);
1564   offset+=WriteBlobLong(image,dpx.file.file_size);
1565   dpx.file.ditto_key=1U;  /* new frame */
1566   offset+=WriteBlobLong(image,dpx.file.ditto_key);
1567   dpx.file.generic_size=0x00000680U;
1568   offset+=WriteBlobLong(image,dpx.file.generic_size);
1569   dpx.file.industry_size=0x00000180U;
1570   offset+=WriteBlobLong(image,dpx.file.industry_size);
1571   dpx.file.user_size=0;
1572   if (profile != (StringInfo *) NULL)
1573     {
1574       dpx.file.user_size+=(unsigned int) GetStringInfoLength(profile);
1575       dpx.file.user_size=(((dpx.file.user_size+0x2000-1)/0x2000)*0x2000);
1576     }
1577   offset+=WriteBlobLong(image,dpx.file.user_size);
1578   value=GetDPXProperty(image,"dpx:file.filename",exception);
1579   if (value != (const char *) NULL)
1580     (void) strncpy(dpx.file.filename,value,sizeof(dpx.file.filename)-1);
1581   offset+=WriteBlob(image,sizeof(dpx.file.filename),(unsigned char *)
1582     dpx.file.filename);
1583   seconds=GetMagickTime();
1584   (void) FormatMagickTime(seconds,sizeof(dpx.file.timestamp),
1585     dpx.file.timestamp);
1586   offset+=WriteBlob(image,sizeof(dpx.file.timestamp),(unsigned char *)
1587     dpx.file.timestamp);
1588   (void) strncpy(dpx.file.creator,MagickAuthoritativeURL,
1589     sizeof(dpx.file.creator)-1);
1590   value=GetDPXProperty(image,"dpx:file.creator",exception);
1591   if (value != (const char *) NULL)
1592     (void) strncpy(dpx.file.creator,value,sizeof(dpx.file.creator)-1);
1593   offset+=WriteBlob(image,sizeof(dpx.file.creator),(unsigned char *)
1594     dpx.file.creator);
1595   value=GetDPXProperty(image,"dpx:file.project",exception);
1596   if (value != (const char *) NULL)
1597     (void) strncpy(dpx.file.project,value,sizeof(dpx.file.project)-1);
1598   offset+=WriteBlob(image,sizeof(dpx.file.project),(unsigned char *)
1599     dpx.file.project);
1600   value=GetDPXProperty(image,"dpx:file.copyright",exception);
1601   if (value != (const char *) NULL)
1602     (void) strncpy(dpx.file.copyright,value,sizeof(dpx.file.copyright)-1);
1603   offset+=WriteBlob(image,sizeof(dpx.file.copyright),(unsigned char *)
1604     dpx.file.copyright);
1605   dpx.file.encrypt_key=(~0U);
1606   offset+=WriteBlobLong(image,dpx.file.encrypt_key);
1607   offset+=WriteBlob(image,sizeof(dpx.file.reserve),(unsigned char *)
1608     dpx.file.reserve);
1609   /*
1610     Write image header.
1611   */
1612   switch (image->orientation)
1613   {
1614     default:
1615     case TopLeftOrientation: dpx.image.orientation=0; break;
1616     case TopRightOrientation: dpx.image.orientation=1; break;
1617     case BottomLeftOrientation: dpx.image.orientation=2; break;
1618     case BottomRightOrientation: dpx.image.orientation=3; break;
1619     case LeftTopOrientation: dpx.image.orientation=4; break;
1620     case RightTopOrientation: dpx.image.orientation=5; break;
1621     case LeftBottomOrientation: dpx.image.orientation=6; break;
1622     case RightBottomOrientation: dpx.image.orientation=7; break;
1623   }
1624   offset+=WriteBlobShort(image,dpx.image.orientation);
1625   dpx.image.number_elements=1;
1626   offset+=WriteBlobShort(image,dpx.image.number_elements);
1627   if ((image->columns != (unsigned int) image->columns) ||
1628       (image->rows != (unsigned int) image->rows))
1629     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1630   offset+=WriteBlobLong(image,(unsigned int) image->columns);
1631   offset+=WriteBlobLong(image,(unsigned int) image->rows);
1632   for (i=0; i < 8; i++)
1633   {
1634     dpx.image.image_element[i].data_sign=0U;
1635     offset+=WriteBlobLong(image,dpx.image.image_element[i].data_sign);
1636     dpx.image.image_element[i].low_data=0U;
1637     offset+=WriteBlobLong(image,dpx.image.image_element[i].low_data);
1638     dpx.image.image_element[i].low_quantity=0.0f;
1639     offset+=WriteBlobFloat(image,dpx.image.image_element[i].low_quantity);
1640     dpx.image.image_element[i].high_data=0U;
1641     offset+=WriteBlobLong(image,dpx.image.image_element[i].high_data);
1642     dpx.image.image_element[i].high_quantity=0.0f;
1643     offset+=WriteBlobFloat(image,dpx.image.image_element[i].high_quantity);
1644     dpx.image.image_element[i].descriptor=0;
1645     if (i == 0)
1646       switch (image->colorspace)
1647       {
1648         case Rec601YCbCrColorspace:
1649         case Rec709YCbCrColorspace:
1650         case YCbCrColorspace:
1651         {
1652           dpx.image.image_element[i].descriptor=CbYCr444ComponentType;
1653           if (image->alpha_trait != UndefinedPixelTrait)
1654             dpx.image.image_element[i].descriptor=CbYCrA4444ComponentType;
1655           break;
1656         }
1657         default:
1658         {
1659           dpx.image.image_element[i].descriptor=RGBComponentType;
1660           if (image->alpha_trait != UndefinedPixelTrait)
1661             dpx.image.image_element[i].descriptor=RGBAComponentType;
1662           if ((image_info->type != TrueColorType) &&
1663               (image->alpha_trait == UndefinedPixelTrait) &&
1664               (SetImageGray(image,exception) != MagickFalse))
1665             dpx.image.image_element[i].descriptor=LumaComponentType;
1666           break;
1667         }
1668       }
1669     offset+=WriteBlobByte(image,dpx.image.image_element[i].descriptor);
1670     dpx.image.image_element[i].transfer_characteristic=0;
1671     if (image->colorspace == LogColorspace)
1672       dpx.image.image_element[0].transfer_characteristic=
1673         PrintingDensityColorimetric;
1674     offset+=WriteBlobByte(image,
1675       dpx.image.image_element[i].transfer_characteristic);
1676     dpx.image.image_element[i].colorimetric=0;
1677     offset+=WriteBlobByte(image,dpx.image.image_element[i].colorimetric);
1678     dpx.image.image_element[i].bit_size=0;
1679     if (i == 0)
1680       dpx.image.image_element[i].bit_size=(unsigned char) image->depth;
1681     offset+=WriteBlobByte(image,dpx.image.image_element[i].bit_size);
1682     dpx.image.image_element[i].packing=0;
1683     if ((image->depth == 10) || (image->depth == 12))
1684       dpx.image.image_element[i].packing=1;
1685     offset+=WriteBlobShort(image,dpx.image.image_element[i].packing);
1686     dpx.image.image_element[i].encoding=0;
1687     offset+=WriteBlobShort(image,dpx.image.image_element[i].encoding);
1688     dpx.image.image_element[i].data_offset=0U;
1689     if (i == 0)
1690       dpx.image.image_element[i].data_offset=dpx.file.image_offset;
1691     offset+=WriteBlobLong(image,dpx.image.image_element[i].data_offset);
1692     dpx.image.image_element[i].end_of_line_padding=0U;
1693     offset+=WriteBlobLong(image,dpx.image.image_element[i].end_of_line_padding);
1694     offset+=WriteBlobLong(image,
1695       dpx.image.image_element[i].end_of_image_padding);
1696     offset+=WriteBlob(image,sizeof(dpx.image.image_element[i].description),
1697       (unsigned char *) dpx.image.image_element[i].description);
1698   }
1699   offset+=WriteBlob(image,sizeof(dpx.image.reserve),(unsigned char *)
1700     dpx.image.reserve);
1701   /*
1702     Write orientation header.
1703   */
1704   if ((image->rows != image->magick_rows) ||
1705       (image->columns != image->magick_columns))
1706     {
1707       /*
1708         These properties are not valid if image size changed.
1709       */
1710       (void) DeleteImageProperty(image,"dpx:orientation.x_offset");
1711       (void) DeleteImageProperty(image,"dpx:orientation.y_offset");
1712       (void) DeleteImageProperty(image,"dpx:orientation.x_center");
1713       (void) DeleteImageProperty(image,"dpx:orientation.y_center");
1714       (void) DeleteImageProperty(image,"dpx:orientation.x_size");
1715       (void) DeleteImageProperty(image,"dpx:orientation.y_size");
1716     }
1717   dpx.orientation.x_offset=0U;
1718   value=GetDPXProperty(image,"dpx:orientation.x_offset",exception);
1719   if (value != (const char *) NULL)
1720     dpx.orientation.x_offset=(unsigned int) StringToUnsignedLong(value);
1721   offset+=WriteBlobLong(image,dpx.orientation.x_offset);
1722   dpx.orientation.y_offset=0U;
1723   value=GetDPXProperty(image,"dpx:orientation.y_offset",exception);
1724   if (value != (const char *) NULL)
1725     dpx.orientation.y_offset=(unsigned int) StringToUnsignedLong(value);
1726   offset+=WriteBlobLong(image,dpx.orientation.y_offset);
1727   dpx.orientation.x_center=0.0f;
1728   value=GetDPXProperty(image,"dpx:orientation.x_center",exception);
1729   if (value != (const char *) NULL)
1730     dpx.orientation.x_center=StringToDouble(value,(char **) NULL);
1731   offset+=WriteBlobFloat(image,dpx.orientation.x_center);
1732   dpx.orientation.y_center=0.0f;
1733   value=GetDPXProperty(image,"dpx:orientation.y_center",exception);
1734   if (value != (const char *) NULL)
1735     dpx.orientation.y_center=StringToDouble(value,(char **) NULL);
1736   offset+=WriteBlobFloat(image,dpx.orientation.y_center);
1737   dpx.orientation.x_size=0U;
1738   value=GetDPXProperty(image,"dpx:orientation.x_size",exception);
1739   if (value != (const char *) NULL)
1740     dpx.orientation.x_size=(unsigned int) StringToUnsignedLong(value);
1741   offset+=WriteBlobLong(image,dpx.orientation.x_size);
1742   dpx.orientation.y_size=0U;
1743   value=GetDPXProperty(image,"dpx:orientation.y_size",exception);
1744   if (value != (const char *) NULL)
1745     dpx.orientation.y_size=(unsigned int) StringToUnsignedLong(value);
1746   offset+=WriteBlobLong(image,dpx.orientation.y_size);
1747   value=GetDPXProperty(image,"dpx:orientation.filename",exception);
1748   if (value != (const char *) NULL)
1749     (void) strncpy(dpx.orientation.filename,value,
1750       sizeof(dpx.orientation.filename)-1);
1751   offset+=WriteBlob(image,sizeof(dpx.orientation.filename),(unsigned char *)
1752     dpx.orientation.filename);
1753   offset+=WriteBlob(image,sizeof(dpx.orientation.timestamp),(unsigned char *)
1754     dpx.orientation.timestamp);
1755   value=GetDPXProperty(image,"dpx:orientation.device",exception);
1756   if (value != (const char *) NULL)
1757     (void) strncpy(dpx.orientation.device,value,
1758       sizeof(dpx.orientation.device)-1);
1759   offset+=WriteBlob(image,sizeof(dpx.orientation.device),(unsigned char *)
1760     dpx.orientation.device);
1761   value=GetDPXProperty(image,"dpx:orientation.serial",exception);
1762   if (value != (const char *) NULL)
1763     (void) strncpy(dpx.orientation.serial,value,
1764       sizeof(dpx.orientation.serial)-1);
1765   offset+=WriteBlob(image,sizeof(dpx.orientation.serial),(unsigned char *)
1766     dpx.orientation.serial);
1767   for (i=0; i < 4; i++)
1768     dpx.orientation.border[i]=0;
1769   value=GetDPXProperty(image,"dpx:orientation.border",exception);
1770   if (value != (const char *) NULL)
1771     {
1772       flags=ParseGeometry(value,&geometry_info);
1773       if ((flags & SigmaValue) == 0)
1774         geometry_info.sigma=geometry_info.rho;
1775       dpx.orientation.border[0]=(unsigned short) (geometry_info.rho+0.5);
1776       dpx.orientation.border[1]=(unsigned short) (geometry_info.sigma+0.5);
1777       dpx.orientation.border[2]=(unsigned short) (geometry_info.xi+0.5);
1778       dpx.orientation.border[3]=(unsigned short) (geometry_info.psi+0.5);
1779     }
1780   for (i=0; i < 4; i++)
1781     offset+=WriteBlobShort(image,dpx.orientation.border[i]);
1782   for (i=0; i < 2; i++)
1783     dpx.orientation.aspect_ratio[i]=0U;
1784   value=GetDPXProperty(image,"dpx:orientation.aspect_ratio",exception);
1785   if (value != (const char *) NULL)
1786     {
1787       flags=ParseGeometry(value,&geometry_info);
1788       if ((flags & SigmaValue) == 0)
1789         geometry_info.sigma=geometry_info.rho;
1790       dpx.orientation.aspect_ratio[0]=(unsigned int) (geometry_info.rho+0.5);
1791       dpx.orientation.aspect_ratio[1]=(unsigned int) (geometry_info.sigma+0.5);
1792     }
1793   for (i=0; i < 2; i++)
1794     offset+=WriteBlobLong(image,dpx.orientation.aspect_ratio[i]);
1795   offset+=WriteBlob(image,sizeof(dpx.orientation.reserve),(unsigned char *)
1796     dpx.orientation.reserve);
1797   /*
1798     Write film header.
1799   */
1800   (void) memset(dpx.film.id,0,sizeof(dpx.film.id));
1801   value=GetDPXProperty(image,"dpx:film.id",exception);
1802   if (value != (const char *) NULL)
1803     (void) strncpy(dpx.film.id,value,sizeof(dpx.film.id)-1);
1804   offset+=WriteBlob(image,sizeof(dpx.film.id),(unsigned char *) dpx.film.id);
1805   (void) memset(dpx.film.type,0,sizeof(dpx.film.type));
1806   value=GetDPXProperty(image,"dpx:film.type",exception);
1807   if (value != (const char *) NULL)
1808     (void) strncpy(dpx.film.type,value,sizeof(dpx.film.type)-1);
1809   offset+=WriteBlob(image,sizeof(dpx.film.type),(unsigned char *)
1810     dpx.film.type);
1811   (void) memset(dpx.film.offset,0,sizeof(dpx.film.offset));
1812   value=GetDPXProperty(image,"dpx:film.offset",exception);
1813   if (value != (const char *) NULL)
1814     (void) strncpy(dpx.film.offset,value,sizeof(dpx.film.offset)-1);
1815   offset+=WriteBlob(image,sizeof(dpx.film.offset),(unsigned char *)
1816     dpx.film.offset);
1817   (void) memset(dpx.film.prefix,0,sizeof(dpx.film.prefix));
1818   value=GetDPXProperty(image,"dpx:film.prefix",exception);
1819   if (value != (const char *) NULL)
1820     (void) strncpy(dpx.film.prefix,value,sizeof(dpx.film.prefix)-1);
1821   offset+=WriteBlob(image,sizeof(dpx.film.prefix),(unsigned char *)
1822     dpx.film.prefix);
1823   (void) memset(dpx.film.count,0,sizeof(dpx.film.count));
1824   value=GetDPXProperty(image,"dpx:film.count",exception);
1825   if (value != (const char *) NULL)
1826     (void) strncpy(dpx.film.count,value,sizeof(dpx.film.count)-1);
1827   offset+=WriteBlob(image,sizeof(dpx.film.count),(unsigned char *)
1828     dpx.film.count);
1829   (void) memset(dpx.film.format,0,sizeof(dpx.film.format));
1830   value=GetDPXProperty(image,"dpx:film.format",exception);
1831   if (value != (const char *) NULL)
1832     (void) strncpy(dpx.film.format,value,sizeof(dpx.film.format)-1);
1833   offset+=WriteBlob(image,sizeof(dpx.film.format),(unsigned char *)
1834     dpx.film.format);
1835   dpx.film.frame_position=0U;
1836   value=GetDPXProperty(image,"dpx:film.frame_position",exception);
1837   if (value != (const char *) NULL)
1838     dpx.film.frame_position=(unsigned int) StringToUnsignedLong(value);
1839   offset+=WriteBlobLong(image,dpx.film.frame_position);
1840   dpx.film.sequence_extent=0U;
1841   value=GetDPXProperty(image,"dpx:film.sequence_extent",exception);
1842   if (value != (const char *) NULL)
1843     dpx.film.sequence_extent=(unsigned int) StringToUnsignedLong(value);
1844   offset+=WriteBlobLong(image,dpx.film.sequence_extent);
1845   dpx.film.held_count=0U;
1846   value=GetDPXProperty(image,"dpx:film.held_count",exception);
1847   if (value != (const char *) NULL)
1848     dpx.film.held_count=(unsigned int) StringToUnsignedLong(value);
1849   offset+=WriteBlobLong(image,dpx.film.held_count);
1850   dpx.film.frame_rate=0.0f;
1851   value=GetDPXProperty(image,"dpx:film.frame_rate",exception);
1852   if (value != (const char *) NULL)
1853     dpx.film.frame_rate=StringToDouble(value,(char **) NULL);
1854   offset+=WriteBlobFloat(image,dpx.film.frame_rate);
1855   dpx.film.shutter_angle=0.0f;
1856   value=GetDPXProperty(image,"dpx:film.shutter_angle",exception);
1857   if (value != (const char *) NULL)
1858     dpx.film.shutter_angle=StringToDouble(value,(char **) NULL);
1859   offset+=WriteBlobFloat(image,dpx.film.shutter_angle);
1860   (void) memset(dpx.film.frame_id,0,sizeof(dpx.film.frame_id));
1861   value=GetDPXProperty(image,"dpx:film.frame_id",exception);
1862   if (value != (const char *) NULL)
1863     (void) strncpy(dpx.film.frame_id,value,sizeof(dpx.film.frame_id)-1);
1864   offset+=WriteBlob(image,sizeof(dpx.film.frame_id),(unsigned char *)
1865     dpx.film.frame_id);
1866   value=GetDPXProperty(image,"dpx:film.slate",exception);
1867   if (value != (const char *) NULL)
1868     (void) strncpy(dpx.film.slate,value,sizeof(dpx.film.slate)-1);
1869   offset+=WriteBlob(image,sizeof(dpx.film.slate),(unsigned char *)
1870     dpx.film.slate);
1871   offset+=WriteBlob(image,sizeof(dpx.film.reserve),(unsigned char *)
1872     dpx.film.reserve);
1873   /*
1874     Write television header.
1875   */
1876   value=GetDPXProperty(image,"dpx:television.time.code",exception);
1877   if (value != (const char *) NULL)
1878     dpx.television.time_code=StringToTimeCode(value);
1879   offset+=WriteBlobLong(image,dpx.television.time_code);
1880   value=GetDPXProperty(image,"dpx:television.user.bits",exception);
1881   if (value != (const char *) NULL)
1882     dpx.television.user_bits=StringToTimeCode(value);
1883   offset+=WriteBlobLong(image,dpx.television.user_bits);
1884   value=GetDPXProperty(image,"dpx:television.interlace",exception);
1885   if (value != (const char *) NULL)
1886     dpx.television.interlace=(unsigned char) StringToLong(value);
1887   offset+=WriteBlobByte(image,dpx.television.interlace);
1888   value=GetDPXProperty(image,"dpx:television.field_number",exception);
1889   if (value != (const char *) NULL)
1890     dpx.television.field_number=(unsigned char) StringToLong(value);
1891   offset+=WriteBlobByte(image,dpx.television.field_number);
1892   dpx.television.video_signal=0;
1893   value=GetDPXProperty(image,"dpx:television.video_signal",exception);
1894   if (value != (const char *) NULL)
1895     dpx.television.video_signal=(unsigned char) StringToLong(value);
1896   offset+=WriteBlobByte(image,dpx.television.video_signal);
1897   dpx.television.padding=0;
1898   value=GetDPXProperty(image,"dpx:television.padding",exception);
1899   if (value != (const char *) NULL)
1900     dpx.television.padding=(unsigned char) StringToLong(value);
1901   offset+=WriteBlobByte(image,dpx.television.padding);
1902   dpx.television.horizontal_sample_rate=0.0f;
1903   value=GetDPXProperty(image,"dpx:television.horizontal_sample_rate",
1904     exception);
1905   if (value != (const char *) NULL)
1906     dpx.television.horizontal_sample_rate=StringToDouble(value,(char **) NULL);
1907   offset+=WriteBlobFloat(image,dpx.television.horizontal_sample_rate);
1908   dpx.television.vertical_sample_rate=0.0f;
1909   value=GetDPXProperty(image,"dpx:television.vertical_sample_rate",exception);
1910   if (value != (const char *) NULL)
1911     dpx.television.vertical_sample_rate=StringToDouble(value,(char **) NULL);
1912   offset+=WriteBlobFloat(image,dpx.television.vertical_sample_rate);
1913   dpx.television.frame_rate=0.0f;
1914   value=GetDPXProperty(image,"dpx:television.frame_rate",exception);
1915   if (value != (const char *) NULL)
1916     dpx.television.frame_rate=StringToDouble(value,(char **) NULL);
1917   offset+=WriteBlobFloat(image,dpx.television.frame_rate);
1918   dpx.television.time_offset=0.0f;
1919   value=GetDPXProperty(image,"dpx:television.time_offset",exception);
1920   if (value != (const char *) NULL)
1921     dpx.television.time_offset=StringToDouble(value,(char **) NULL);
1922   offset+=WriteBlobFloat(image,dpx.television.time_offset);
1923   dpx.television.gamma=0.0f;
1924   value=GetDPXProperty(image,"dpx:television.gamma",exception);
1925   if (value != (const char *) NULL)
1926     dpx.television.gamma=StringToDouble(value,(char **) NULL);
1927   offset+=WriteBlobFloat(image,dpx.television.gamma);
1928   dpx.television.black_level=0.0f;
1929   value=GetDPXProperty(image,"dpx:television.black_level",exception);
1930   if (value != (const char *) NULL)
1931     dpx.television.black_level=StringToDouble(value,(char **) NULL);
1932   offset+=WriteBlobFloat(image,dpx.television.black_level);
1933   dpx.television.black_gain=0.0f;
1934   value=GetDPXProperty(image,"dpx:television.black_gain",exception);
1935   if (value != (const char *) NULL)
1936     dpx.television.black_gain=StringToDouble(value,(char **) NULL);
1937   offset+=WriteBlobFloat(image,dpx.television.black_gain);
1938   dpx.television.break_point=0.0f;
1939   value=GetDPXProperty(image,"dpx:television.break_point",exception);
1940   if (value != (const char *) NULL)
1941     dpx.television.break_point=StringToDouble(value,(char **) NULL);
1942   offset+=WriteBlobFloat(image,dpx.television.break_point);
1943   dpx.television.white_level=0.0f;
1944   value=GetDPXProperty(image,"dpx:television.white_level",exception);
1945   if (value != (const char *) NULL)
1946     dpx.television.white_level=StringToDouble(value,(char **) NULL);
1947   offset+=WriteBlobFloat(image,dpx.television.white_level);
1948   dpx.television.integration_times=0.0f;
1949   value=GetDPXProperty(image,"dpx:television.integration_times",exception);
1950   if (value != (const char *) NULL)
1951     dpx.television.integration_times=StringToDouble(value,(char **) NULL);
1952   offset+=WriteBlobFloat(image,dpx.television.integration_times);
1953   offset+=WriteBlob(image,sizeof(dpx.television.reserve),(unsigned char *)
1954     dpx.television.reserve);
1955   /*
1956     Write user header.
1957   */
1958   value=GetDPXProperty(image,"dpx:user.id",exception);
1959   if (value != (const char *) NULL)
1960     (void) strncpy(dpx.user.id,value,sizeof(dpx.user.id)-1);
1961   offset+=WriteBlob(image,sizeof(dpx.user.id),(unsigned char *) dpx.user.id);
1962   if (profile != (StringInfo *) NULL)
1963     offset+=WriteBlob(image,GetStringInfoLength(profile),
1964       GetStringInfoDatum(profile));
1965   while (offset < (MagickOffsetType) dpx.image.image_element[0].data_offset)
1966   {
1967     count=WriteBlobByte(image,0x00);
1968     if (count != 1)
1969       {
1970         ThrowFileException(exception,FileOpenError,"UnableToWriteFile",
1971           image->filename);
1972         break;
1973       }
1974     offset+=count;
1975   }
1976   /*
1977     Convert pixel packets to DPX raster image.
1978   */
1979   quantum_info=AcquireQuantumInfo(image_info,image);
1980   if (quantum_info == (QuantumInfo *) NULL)
1981     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1982   SetQuantumQuantum(quantum_info,32);
1983   SetQuantumPack(quantum_info,dpx.image.image_element[0].packing == 0 ?
1984     MagickTrue : MagickFalse);
1985   quantum_type=RGBQuantum;
1986   if (image->alpha_trait != UndefinedPixelTrait)
1987     quantum_type=RGBAQuantum;
1988   if (image->colorspace == YCbCrColorspace)
1989     {
1990       quantum_type=CbYCrQuantum;
1991       if (image->alpha_trait != UndefinedPixelTrait)
1992         quantum_type=CbYCrAQuantum;
1993       if ((horizontal_factor == 2) || (vertical_factor == 2))
1994         quantum_type=CbYCrYQuantum;
1995     }
1996   extent=GetBytesPerRow(image->columns,
1997     image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL,image->depth,
1998     dpx.image.image_element[0].packing == 0 ? MagickFalse : MagickTrue);
1999   if ((image_info->type != TrueColorType) &&
2000       (image->alpha_trait == UndefinedPixelTrait) &&
2001       (SetImageGray(image,exception) != MagickFalse))
2002     {
2003       quantum_type=GrayQuantum;
2004       extent=GetBytesPerRow(image->columns,1UL,image->depth,
2005         dpx.image.image_element[0].packing == 0 ? MagickFalse : MagickTrue);
2006     }
2007   pixels=(unsigned char *) GetQuantumPixels(quantum_info);
2008   for (y=0; y < (ssize_t) image->rows; y++)
2009   {
2010     size_t
2011       length;
2012
2013     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2014     if (p == (const Quantum *) NULL)
2015       break;
2016     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2017       quantum_type,pixels,exception);
2018     if (length == 0)
2019       break;
2020     count=WriteBlob(image,extent,pixels);
2021     if (count != (ssize_t) extent)
2022       break;
2023     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2024       image->rows);
2025     if (status == MagickFalse)
2026       break;
2027   }
2028   quantum_info=DestroyQuantumInfo(quantum_info);
2029   if (y < (ssize_t) image->rows)
2030     ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
2031   (void) CloseBlob(image);
2032   return(status);
2033 }