]> granicus.if.org Git - imagemagick/blob - coders/cin.c
0ec21d4f30d8c380f976b108c97b153bf19e7114
[imagemagick] / coders / cin.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             CCCC  IIIII  N   N                              %
7 %                            C        I    NN  N                              %
8 %                            C        I    N N N                              %
9 %                            C        I    N  NN                              %
10 %                             CCCC  IIIII  N   N                              %
11 %                                                                             %
12 %                                                                             %
13 %                    Read/Write Kodak Cineon Image Format                     %
14 %                Cineon Image Format is a subset of SMTPE CIN                 %
15 %                                                                             %
16 %                                                                             %
17 %                              Software Design                                %
18 %                                John Cristy                                  %
19 %                             Kelly Bergougnoux                               %
20 %                               October 2003                                  %
21 %                                                                             %
22 %                                                                             %
23 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
24 %  dedicated to making software imaging solutions freely available.           %
25 %                                                                             %
26 %  You may not use this file except in compliance with the License.  You may  %
27 %  obtain a copy of the License at                                            %
28 %                                                                             %
29 %    http://www.imagemagick.org/script/license.php                            %
30 %                                                                             %
31 %  Unless required by applicable law or agreed to in writing, software        %
32 %  distributed under the License is distributed on an "AS IS" BASIS,          %
33 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
34 %  See the License for the specific language governing permissions and        %
35 %  limitations under the License.                                             %
36 %                                                                             %
37 %                                                                             %
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %
40 %  Cineon image file format draft is available at
41 %  http://www.cineon.com/ff_draft.php.
42 %
43 %
44 */
45 \f
46 /*
47   Include declarations.
48 */
49 #include "MagickCore/studio.h"
50 #include "MagickCore/blob.h"
51 #include "MagickCore/blob-private.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/colorspace.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/property.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/string-private.h"
71 #include "MagickCore/module.h"
72 \f
73 /*
74   Typedef declaration.
75 */
76 typedef struct _CINDataFormatInfo
77 {
78   unsigned char
79     interleave,
80     packing,
81     sign,
82     sense;
83
84   size_t
85     line_pad,
86     channel_pad;
87
88   unsigned char
89     reserve[20];
90 } CINDataFormatInfo;
91
92 typedef struct _CINFileInfo
93 {
94   size_t
95     magic,
96     image_offset,
97     generic_length,
98     industry_length,
99     user_length,
100     file_size;
101
102   char
103     version[8],
104     filename[100],
105     create_date[12],
106     create_time[12],
107     reserve[36];
108 } CINFileInfo;
109
110 typedef struct _CINFilmInfo
111 {
112   char
113     id,
114     type,
115     offset,
116     reserve1;
117
118   size_t
119     prefix,
120     count;
121
122   char
123     format[32];
124
125   size_t
126     frame_position;
127
128   float
129     frame_rate;
130
131   char
132     frame_id[32],
133     slate_info[200],
134     reserve[740];
135 } CINFilmInfo;
136
137 typedef struct _CINImageChannel
138 {
139   unsigned char
140     designator[2],
141     bits_per_pixel,
142     reserve;
143
144   size_t
145     pixels_per_line,
146     lines_per_image;
147
148   float
149     min_data,
150     min_quantity,
151     max_data,
152     max_quantity;
153 } CINImageChannel;
154
155 typedef struct _CINImageInfo
156 {
157   unsigned char
158     orientation,
159     number_channels,
160     reserve1[2];
161
162   CINImageChannel
163     channel[8];
164
165   float
166     white_point[2],
167     red_primary_chromaticity[2],
168     green_primary_chromaticity[2],
169     blue_primary_chromaticity[2];
170
171   char
172     label[200],
173     reserve[28];
174 } CINImageInfo;
175
176 typedef struct _CINOriginationInfo
177 {
178   ssize_t
179     x_offset,
180     y_offset;
181
182   char
183     filename[100],
184     create_date[12],
185     create_time[12],
186     device[64],
187     model[32],
188     serial[32];
189
190   float
191     x_pitch,
192     y_pitch,
193     gamma;
194
195   char
196     reserve[40];
197 } CINOriginationInfo;
198
199 typedef struct _CINUserInfo
200 {
201   char
202     id[32];
203 } CINUserInfo;
204
205 typedef struct CINInfo
206 {
207   CINFileInfo
208     file;
209
210   CINImageInfo
211     image;
212
213   CINDataFormatInfo
214     data_format;
215
216   CINOriginationInfo
217     origination;
218
219   CINFilmInfo
220     film;
221
222   CINUserInfo
223     user;
224 } CINInfo;
225 \f
226 /*
227   Forward declaractions.
228 */
229 static MagickBooleanType
230   WriteCINImage(const ImageInfo *,Image *,ExceptionInfo *);
231 \f
232 /*
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 %                                                                             %
235 %                                                                             %
236 %                                                                             %
237 %   I s C I N E O N                                                           %
238 %                                                                             %
239 %                                                                             %
240 %                                                                             %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 %
243 %  IsCIN() returns MagickTrue if the image format type, identified by the magick
244 %  string, is CIN.
245 %
246 %  The format of the IsCIN method is:
247 %
248 %      MagickBooleanType IsCIN(const unsigned char *magick,const size_t length)
249 %
250 %  A description of each parameter follows:
251 %
252 %    o magick: compare image format pattern against these bytes.
253 %
254 %    o length: Specifies the length of the magick string.
255 %
256 */
257 static MagickBooleanType IsCIN(const unsigned char *magick,const size_t length)
258 {
259   if (length < 4)
260     return(MagickFalse);
261   if (memcmp(magick,"\200\052\137\327",4) == 0)
262     return(MagickTrue);
263   return(MagickFalse);
264 }
265 \f
266 /*
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 %                                                                             %
269 %                                                                             %
270 %                                                                             %
271 %   R e a d C I N E O N I m a g e                                             %
272 %                                                                             %
273 %                                                                             %
274 %                                                                             %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 %
277 %  ReadCINImage() reads an CIN X image file and returns it.  It allocates
278 %  the memory necessary for the new Image structure and returns a point to the
279 %  new image.
280 %
281 %  The format of the ReadCINImage method is:
282 %
283 %      Image *ReadCINImage(const ImageInfo *image_info,
284 %        ExceptionInfo *exception)
285 %
286 %  A description of each parameter follows:
287 %
288 %    o image_info: the image info.
289 %
290 %    o exception: return any errors or warnings in this structure.
291 %
292 */
293
294 static size_t GetBytesPerRow(size_t columns,
295   size_t samples_per_pixel,size_t bits_per_pixel,
296   MagickBooleanType pad)
297 {
298   size_t
299     bytes_per_row;
300
301   switch (bits_per_pixel)
302   {
303     case 1:
304     {
305       bytes_per_row=4*(((size_t) samples_per_pixel*columns*
306         bits_per_pixel+31)/32);
307       break;
308     }
309     case 8:
310     default:
311     {
312       bytes_per_row=4*(((size_t) samples_per_pixel*columns*
313         bits_per_pixel+31)/32);
314       break;
315     }
316     case 10:
317     {
318       if (pad == MagickFalse)
319         {
320           bytes_per_row=4*(((size_t) samples_per_pixel*columns*
321             bits_per_pixel+31)/32);
322           break;
323         }
324       bytes_per_row=4*(((size_t) (32*((samples_per_pixel*columns+2)/3))+31)/32);
325       break;
326     }
327     case 12:
328     {
329       if (pad == MagickFalse)
330         {
331           bytes_per_row=4*(((size_t) samples_per_pixel*columns*
332             bits_per_pixel+31)/32);
333           break;
334         }
335       bytes_per_row=2*(((size_t) (16*samples_per_pixel*columns)+15)/16);
336       break;
337     }
338     case 16:
339     {
340       bytes_per_row=2*(((size_t) samples_per_pixel*columns*
341         bits_per_pixel+8)/16);
342       break;
343     }
344     case 32:
345     {
346       bytes_per_row=4*(((size_t) samples_per_pixel*columns*
347         bits_per_pixel+31)/32);
348       break;
349     }
350     case 64:
351     {
352       bytes_per_row=8*(((size_t) samples_per_pixel*columns*
353         bits_per_pixel+63)/64);
354       break;
355     }
356   }
357   return(bytes_per_row);
358 }
359
360 static inline MagickBooleanType IsFloatDefined(const float value)
361 {
362   union
363   {
364     unsigned int
365       unsigned_value;
366
367     double
368       float_value;
369   } quantum;
370
371   quantum.unsigned_value=0U;
372   quantum.float_value=value;
373   if (quantum.unsigned_value == 0U)
374     return(MagickFalse);
375   return(MagickTrue);
376 }
377
378 static Image *ReadCINImage(const ImageInfo *image_info,ExceptionInfo *exception)
379 {
380 #define MonoColorType  1
381 #define RGBColorType  3
382
383   CINInfo
384     cin;
385
386   Image
387     *image;
388
389   MagickBooleanType
390     status;
391
392   MagickOffsetType
393     offset;
394
395   QuantumInfo
396     *quantum_info;
397
398   QuantumType
399     quantum_type;
400
401   register ssize_t
402     i;
403
404   register Quantum
405     *q;
406
407   size_t
408     length;
409
410   ssize_t
411     count,
412     y;
413
414   unsigned char
415     magick[4],
416     *pixels;
417
418   /*
419     Open image file.
420   */
421   assert(image_info != (const ImageInfo *) NULL);
422   assert(image_info->signature == MagickSignature);
423   if (image_info->debug != MagickFalse)
424     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
425       image_info->filename);
426   assert(exception != (ExceptionInfo *) NULL);
427   assert(exception->signature == MagickSignature);
428   image=AcquireImage(image_info);
429   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
430   if (status == MagickFalse)
431     {
432       image=DestroyImageList(image);
433       return((Image *) NULL);
434     }
435   /*
436     File information.
437   */
438   offset=0;
439   count=ReadBlob(image,4,magick);
440   offset+=count;
441   if ((count != 4) ||
442       ((LocaleNCompare((char *) magick,"\200\052\137\327",4) != 0)))
443     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
444   image->endian=(magick[0] == 0x80) && (magick[1] == 0x2a) &&
445     (magick[2] == 0x5f) && (magick[3] == 0xd7) ? MSBEndian : LSBEndian;
446   cin.file.image_offset=ReadBlobLong(image);
447   offset+=4;
448   cin.file.generic_length=ReadBlobLong(image);
449   offset+=4;
450   cin.file.industry_length=ReadBlobLong(image);
451   offset+=4;
452   cin.file.user_length=ReadBlobLong(image);
453   offset+=4;
454   cin.file.file_size=ReadBlobLong(image);
455   offset+=4;
456   offset+=ReadBlob(image,sizeof(cin.file.version),(unsigned char *)
457     cin.file.version);
458   (void) SetImageProperty(image,"dpx:file.version",cin.file.version);
459   offset+=ReadBlob(image,sizeof(cin.file.filename),(unsigned char *)
460     cin.file.filename);
461   (void) SetImageProperty(image,"dpx:file.filename",cin.file.filename);
462   offset+=ReadBlob(image,sizeof(cin.file.create_date),(unsigned char *)
463     cin.file.create_date);
464   (void) SetImageProperty(image,"dpx:file.create_date",cin.file.create_date);
465   offset+=ReadBlob(image,sizeof(cin.file.create_time),(unsigned char *)
466     cin.file.create_time);
467   (void) SetImageProperty(image,"dpx:file.create_time",cin.file.create_time);
468   offset+=ReadBlob(image,sizeof(cin.file.reserve),(unsigned char *)
469     cin.file.reserve);
470   /*
471     Image information.
472   */
473   cin.image.orientation=(unsigned char) ReadBlobByte(image);
474   offset++;
475   if (cin.image.orientation != (unsigned char) (~0U))
476     (void) FormatImageProperty(image,"dpx:image.orientation","%d",
477       cin.image.orientation);
478   switch (cin.image.orientation)
479   {
480     default:
481     case 0: image->orientation=TopLeftOrientation; break;
482     case 1: image->orientation=TopRightOrientation; break;
483     case 2: image->orientation=BottomLeftOrientation; break;
484     case 3: image->orientation=BottomRightOrientation; break;
485     case 4: image->orientation=LeftTopOrientation; break;
486     case 5: image->orientation=RightTopOrientation; break;
487     case 6: image->orientation=LeftBottomOrientation; break;
488     case 7: image->orientation=RightBottomOrientation; break;
489   }
490   cin.image.number_channels=(unsigned char) ReadBlobByte(image);
491   offset++;
492   offset+=ReadBlob(image,sizeof(cin.image.reserve1),(unsigned char *)
493     cin.image.reserve1);
494   for (i=0; i < 8; i++)
495   {
496     cin.image.channel[i].designator[0]=(unsigned char) ReadBlobByte(image);
497     offset++;
498     cin.image.channel[i].designator[1]=(unsigned char) ReadBlobByte(image);
499     offset++;
500     cin.image.channel[i].bits_per_pixel=(unsigned char) ReadBlobByte(image);
501     offset++;
502     cin.image.channel[i].reserve=(unsigned char) ReadBlobByte(image);
503     offset++;
504     cin.image.channel[i].pixels_per_line=ReadBlobLong(image);
505     offset+=4;
506     cin.image.channel[i].lines_per_image=ReadBlobLong(image);
507     offset+=4;
508     cin.image.channel[i].min_data=ReadBlobFloat(image);
509     offset+=4;
510     cin.image.channel[i].min_quantity=ReadBlobFloat(image);
511     offset+=4;
512     cin.image.channel[i].max_data=ReadBlobFloat(image);
513     offset+=4;
514     cin.image.channel[i].max_quantity=ReadBlobFloat(image);
515     offset+=4;
516   }
517   cin.image.white_point[0]=ReadBlobFloat(image);
518   offset+=4;
519   if (IsFloatDefined(cin.image.white_point[0]) != MagickFalse)
520     image->chromaticity.white_point.x=cin.image.white_point[0];
521   cin.image.white_point[1]=ReadBlobFloat(image);
522   offset+=4;
523   if (IsFloatDefined(cin.image.white_point[1]) != MagickFalse)
524     image->chromaticity.white_point.y=cin.image.white_point[1];
525   cin.image.red_primary_chromaticity[0]=ReadBlobFloat(image);
526   offset+=4;
527   if (IsFloatDefined(cin.image.red_primary_chromaticity[0]) != MagickFalse)
528     image->chromaticity.red_primary.x=cin.image.red_primary_chromaticity[0];
529   cin.image.red_primary_chromaticity[1]=ReadBlobFloat(image);
530   offset+=4;
531   if (IsFloatDefined(cin.image.red_primary_chromaticity[1]) != MagickFalse)
532     image->chromaticity.red_primary.y=cin.image.red_primary_chromaticity[1];
533   cin.image.green_primary_chromaticity[0]=ReadBlobFloat(image);
534   offset+=4;
535   if (IsFloatDefined(cin.image.green_primary_chromaticity[0]) != MagickFalse)
536     image->chromaticity.red_primary.x=cin.image.green_primary_chromaticity[0];
537   cin.image.green_primary_chromaticity[1]=ReadBlobFloat(image);
538   offset+=4;
539   if (IsFloatDefined(cin.image.green_primary_chromaticity[1]) != MagickFalse)
540     image->chromaticity.green_primary.y=cin.image.green_primary_chromaticity[1];
541   cin.image.blue_primary_chromaticity[0]=ReadBlobFloat(image);
542   offset+=4;
543   if (IsFloatDefined(cin.image.blue_primary_chromaticity[0]) != MagickFalse)
544     image->chromaticity.blue_primary.x=cin.image.blue_primary_chromaticity[0];
545   cin.image.blue_primary_chromaticity[1]=ReadBlobFloat(image);
546   offset+=4;
547   if (IsFloatDefined(cin.image.blue_primary_chromaticity[1]) != MagickFalse)
548     image->chromaticity.blue_primary.y=cin.image.blue_primary_chromaticity[1];
549   offset+=ReadBlob(image,sizeof(cin.image.label),(unsigned char *)
550     cin.image.label);
551   (void) SetImageProperty(image,"dpx:image.label",cin.image.label);
552   offset+=ReadBlob(image,sizeof(cin.image.reserve),(unsigned char *)
553     cin.image.reserve);
554   /*
555     Image data format information.
556   */
557   cin.data_format.interleave=(unsigned char) ReadBlobByte(image);
558   offset++;
559   cin.data_format.packing=(unsigned char) ReadBlobByte(image);
560   offset++;
561   cin.data_format.sign=(unsigned char) ReadBlobByte(image);
562   offset++;
563   cin.data_format.sense=(unsigned char) ReadBlobByte(image);
564   offset++;
565   cin.data_format.line_pad=ReadBlobLong(image);
566   offset+=4;
567   cin.data_format.channel_pad=ReadBlobLong(image);
568   offset+=4;
569   offset+=ReadBlob(image,sizeof(cin.data_format.reserve),(unsigned char *)
570     cin.data_format.reserve);
571   /*
572     Image origination information.
573   */
574   cin.origination.x_offset=(int) ReadBlobLong(image);
575   offset+=4;
576   if ((size_t) cin.origination.x_offset != ~0UL)
577     (void) FormatImageProperty(image,"dpx:origination.x_offset","%.20g",
578       (double) cin.origination.x_offset);
579   cin.origination.y_offset=(ssize_t) ReadBlobLong(image);
580   offset+=4;
581   if ((size_t) cin.origination.y_offset != ~0UL)
582     (void) FormatImageProperty(image,"dpx:origination.y_offset","%.20g",
583       (double) cin.origination.y_offset);
584   offset+=ReadBlob(image,sizeof(cin.origination.filename),(unsigned char *)
585     cin.origination.filename);
586   (void) SetImageProperty(image,"dpx:origination.filename",
587     cin.origination.filename);
588   offset+=ReadBlob(image,sizeof(cin.origination.create_date),(unsigned char *)
589     cin.origination.create_date);
590   (void) SetImageProperty(image,"dpx:origination.create_date",
591     cin.origination.create_date);
592   offset+=ReadBlob(image,sizeof(cin.origination.create_time),(unsigned char *)
593     cin.origination.create_time);
594   (void) SetImageProperty(image,"dpx:origination.create_time",
595     cin.origination.create_time);
596   offset+=ReadBlob(image,sizeof(cin.origination.device),(unsigned char *)
597     cin.origination.device);
598   (void) SetImageProperty(image,"dpx:origination.device",
599     cin.origination.device);
600   offset+=ReadBlob(image,sizeof(cin.origination.model),(unsigned char *)
601     cin.origination.model);
602   (void) SetImageProperty(image,"dpx:origination.model",cin.origination.model);
603   offset+=ReadBlob(image,sizeof(cin.origination.serial),(unsigned char *)
604     cin.origination.serial);
605   (void) SetImageProperty(image,"dpx:origination.serial",
606     cin.origination.serial);
607   cin.origination.x_pitch=ReadBlobFloat(image);
608   offset+=4;
609   cin.origination.y_pitch=ReadBlobFloat(image);
610   offset+=4;
611   cin.origination.gamma=ReadBlobFloat(image);
612   offset+=4;
613   if (IsFloatDefined(cin.origination.gamma) != MagickFalse)
614     image->gamma=cin.origination.gamma;
615   offset+=ReadBlob(image,sizeof(cin.origination.reserve),(unsigned char *)
616     cin.origination.reserve);
617   if ((cin.file.image_offset > 2048) && (cin.file.user_length != 0))
618     {
619       int
620         c;
621
622       /*
623         Image film information.
624       */
625       cin.film.id=ReadBlobByte(image);
626       offset++;
627       c=cin.film.id;
628       if (c != ~0)
629         (void) FormatImageProperty(image,"dpx:film.id","%d",cin.film.id);
630       cin.film.type=ReadBlobByte(image);
631       offset++;
632       c=cin.film.type;
633       if (c != ~0)
634         (void) FormatImageProperty(image,"dpx:film.type","%d",cin.film.type);
635       cin.film.offset=ReadBlobByte(image);
636       offset++;
637       c=cin.film.offset;
638       if (c != ~0)
639         (void) FormatImageProperty(image,"dpx:film.offset","%d",
640           cin.film.offset);
641       cin.film.reserve1=ReadBlobByte(image);
642       offset++;
643       cin.film.prefix=ReadBlobLong(image);
644       offset+=4;
645       if (cin.film.prefix != ~0UL)
646         (void) FormatImageProperty(image,"dpx:film.prefix","%.20g",(double)
647           cin.film.prefix);
648       cin.film.count=ReadBlobLong(image);
649       offset+=4;
650       offset+=ReadBlob(image,sizeof(cin.film.format),(unsigned char *)
651         cin.film.format);
652       (void) SetImageProperty(image,"dpx:film.format",cin.film.format);
653       cin.film.frame_position=ReadBlobLong(image);
654       offset+=4;
655       if (cin.film.frame_position != ~0UL)
656         (void) FormatImageProperty(image,"dpx:film.frame_position","%.20g",
657           (double) cin.film.frame_position);
658       cin.film.frame_rate=ReadBlobFloat(image);
659       offset+=4;
660       if (IsFloatDefined(cin.film.frame_rate) != MagickFalse)
661         (void) FormatImageProperty(image,"dpx:film.frame_rate","%g",
662           cin.film.frame_rate);
663       offset+=ReadBlob(image,sizeof(cin.film.frame_id),(unsigned char *)
664         cin.film.frame_id);
665       (void) SetImageProperty(image,"dpx:film.frame_id",cin.film.frame_id);
666       offset+=ReadBlob(image,sizeof(cin.film.slate_info),(unsigned char *)
667         cin.film.slate_info);
668       (void) SetImageProperty(image,"dpx:film.slate_info",cin.film.slate_info);
669       offset+=ReadBlob(image,sizeof(cin.film.reserve),(unsigned char *)
670         cin.film.reserve);
671     }
672   if ((cin.file.image_offset > 2048) && (cin.file.user_length != 0))
673     {
674       StringInfo
675         *profile;
676
677       /*
678         User defined data.
679       */
680       profile=BlobToStringInfo((const void *) NULL,cin.file.user_length);
681       if (profile == (StringInfo *) NULL)
682         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
683       offset+=ReadBlob(image,GetStringInfoLength(profile),
684         GetStringInfoDatum(profile));
685       (void) SetImageProfile(image,"dpx:user.data",profile);
686       profile=DestroyStringInfo(profile);
687     }
688   for ( ; offset < (MagickOffsetType) cin.file.image_offset; offset++)
689     (void) ReadBlobByte(image);
690   image->depth=cin.image.channel[0].bits_per_pixel;
691   image->columns=cin.image.channel[0].pixels_per_line;
692   image->rows=cin.image.channel[0].lines_per_image;
693   if (image_info->ping)
694     {
695       (void) CloseBlob(image);
696       return(image);
697     }
698   /*
699     Convert CIN raster image to pixel packets.
700   */
701   quantum_info=AcquireQuantumInfo(image_info,image);
702   if (quantum_info == (QuantumInfo *) NULL)
703     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
704   quantum_info->quantum=32;
705   quantum_info->pack=MagickFalse;
706   quantum_type=RGBQuantum;
707   pixels=GetQuantumPixels(quantum_info);
708   length=GetQuantumExtent(image,quantum_info,quantum_type);
709   length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
710   if (cin.image.number_channels == 1)
711     {
712       quantum_type=GrayQuantum;
713       length=GetBytesPerRow(image->columns,1,image->depth,MagickTrue);
714     }
715   for (y=0; y < (ssize_t) image->rows; y++)
716   {
717     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
718     if (q == (Quantum *) NULL)
719       break;
720     count=ReadBlob(image,length,pixels);
721     if ((size_t) count != length)
722       break;
723     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
724       quantum_type,pixels,exception);
725     if (SyncAuthenticPixels(image,exception) == MagickFalse)
726       break;
727     if (image->previous == (Image *) NULL)
728       {
729         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
730           image->rows);
731         if (status == MagickFalse)
732           break;
733       }
734   }
735   SetQuantumImageType(image,quantum_type);
736   quantum_info=DestroyQuantumInfo(quantum_info);
737   if (EOFBlob(image) != MagickFalse)
738     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
739       image->filename);
740   image->colorspace=LogColorspace;
741   (void) CloseBlob(image);
742   return(GetFirstImageInList(image));
743 }
744 \f
745 /*
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
747 %                                                                             %
748 %                                                                             %
749 %                                                                             %
750 %   R e g i s t e r C I N E O N I m a g e                                     %
751 %                                                                             %
752 %                                                                             %
753 %                                                                             %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 %
756 %  RegisterCINImage() adds attributes for the CIN image format to the list of
757 %  of supported formats.  The attributes include the image format tag, a method
758 %  to read and/or write the format, whether the format supports the saving of
759 %  more than one frame to the same file or blob, whether the format supports
760 %  native in-memory I/O, and a brief description of the format.
761 %
762 %  The format of the RegisterCINImage method is:
763 %
764 %      size_t RegisterCINImage(void)
765 %
766 */
767 ModuleExport size_t RegisterCINImage(void)
768 {
769   MagickInfo
770     *entry;
771
772   entry=SetMagickInfo("CIN");
773   entry->decoder=(DecodeImageHandler *) ReadCINImage;
774   entry->encoder=(EncodeImageHandler *) WriteCINImage;
775   entry->magick=(IsImageFormatHandler *) IsCIN;
776   entry->adjoin=MagickFalse;
777   entry->description=ConstantString("Cineon Image File");
778   entry->module=ConstantString("CIN");
779   (void) RegisterMagickInfo(entry);
780   return(MagickImageCoderSignature);
781 }
782 \f
783 /*
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 %                                                                             %
786 %                                                                             %
787 %                                                                             %
788 %   U n r e g i s t e r C I N E O N I m a g e                                 %
789 %                                                                             %
790 %                                                                             %
791 %                                                                             %
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 %
794 %  UnregisterCINImage() removes format registrations made by the CIN module
795 %  from the list of supported formats.
796 %
797 %  The format of the UnregisterCINImage method is:
798 %
799 %      UnregisterCINImage(void)
800 %
801 */
802 ModuleExport void UnregisterCINImage(void)
803 {
804   (void) UnregisterMagickInfo("CINEON");
805 }
806 \f
807 /*
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 %                                                                             %
810 %                                                                             %
811 %                                                                             %
812 %   W r i t e C I N E O N I m a g e                                           %
813 %                                                                             %
814 %                                                                             %
815 %                                                                             %
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 %
818 %  WriteCINImage() writes an image in CIN encoded image format.
819 %
820 %  The format of the WriteCINImage method is:
821 %
822 %      MagickBooleanType WriteCINImage(const ImageInfo *image_info,
823 %        Image *image,ExceptionInfo *exception)
824 %
825 %  A description of each parameter follows.
826 %
827 %    o image_info: the image info.
828 %
829 %    o image:  The image.
830 %
831 %    o exception: return any errors or warnings in this structure.
832 %
833 */
834
835 static inline const char *GetCINProperty(const ImageInfo *image_info,
836   const Image *image,const char *property)
837 {
838   const char
839     *value;
840
841   value=GetImageOption(image_info,property);
842   if (value != (const char *) NULL)
843     return(value);
844   return(GetImageProperty(image,property));
845 }
846
847 static MagickBooleanType WriteCINImage(const ImageInfo *image_info,Image *image,
848   ExceptionInfo *exception)
849 {
850   const char
851     *value;
852
853   CINInfo
854     cin;
855
856   const StringInfo
857     *profile;
858
859   MagickBooleanType
860     status;
861
862   MagickOffsetType
863     offset;
864
865   QuantumInfo
866     *quantum_info;
867
868   QuantumType
869     quantum_type;
870
871   register const Quantum
872     *p;
873
874   register ssize_t
875     i;
876
877   size_t
878     length;
879
880   ssize_t
881     count,
882     y;
883
884   struct tm
885     local_time;
886
887   time_t
888     seconds;
889
890   unsigned char
891     *pixels;
892
893   /*
894     Open output image file.
895   */
896   assert(image_info != (const ImageInfo *) NULL);
897   assert(image_info->signature == MagickSignature);
898   assert(image != (Image *) NULL);
899   assert(image->signature == MagickSignature);
900   if (image->debug != MagickFalse)
901     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
902   assert(exception != (ExceptionInfo *) NULL);
903   assert(exception->signature == MagickSignature);
904   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
905   if (status == MagickFalse)
906     return(status);
907   if (image->colorspace != LogColorspace)
908     (void) TransformImageColorspace(image,LogColorspace);
909   /*
910     Write image information.
911   */
912   (void) ResetMagickMemory(&cin,0,sizeof(cin));
913   offset=0;
914   cin.file.magic=0x802A5FD7UL;
915   offset+=WriteBlobLong(image,(unsigned int) cin.file.magic);
916   cin.file.image_offset=0x800;
917   offset+=WriteBlobLong(image,(unsigned int) cin.file.image_offset);
918   cin.file.generic_length=0x400;
919   offset+=WriteBlobLong(image,(unsigned int) cin.file.generic_length);
920   cin.file.industry_length=0x400;
921   offset+=WriteBlobLong(image,(unsigned int) cin.file.industry_length);
922   cin.file.user_length=0x00;
923   profile=GetImageProfile(image,"dpx:user.data");
924   if (profile != (StringInfo *) NULL)
925     {
926       cin.file.user_length+=(size_t) GetStringInfoLength(profile);
927       cin.file.user_length=(((cin.file.user_length+0x2000-1)/0x2000)*0x2000);
928     }
929   offset+=WriteBlobLong(image,(unsigned int) cin.file.user_length);
930   cin.file.file_size=4*image->columns*image->rows+0x2000;
931   offset+=WriteBlobLong(image,(unsigned int) cin.file.file_size);
932   (void) CopyMagickString(cin.file.version,"V4.5",sizeof(cin.file.version));
933   offset+=WriteBlob(image,sizeof(cin.file.version),(unsigned char *)
934     cin.file.version);
935   value=GetCINProperty(image_info,image,"dpx:file.filename");
936   if (value != (const char *) NULL)
937     (void) CopyMagickString(cin.file.filename,value,sizeof(cin.file.filename));
938   else
939     (void) CopyMagickString(cin.file.filename,image->filename,
940       sizeof(cin.file.filename));
941   offset+=WriteBlob(image,sizeof(cin.file.filename),(unsigned char *)
942     cin.file.filename);
943   seconds=time((time_t *) NULL);
944 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
945   (void) localtime_r(&seconds,&local_time);
946 #else
947   (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
948 #endif
949   (void) strftime(cin.file.create_date,sizeof(cin.file.create_date),"%Y:%m:%d",
950     &local_time);
951   offset+=WriteBlob(image,sizeof(cin.file.create_date),(unsigned char *)
952     cin.file.create_date);
953   (void) strftime(cin.file.create_time,sizeof(cin.file.create_time),
954     "%H:%M:%S%Z",&local_time);
955   offset+=WriteBlob(image,sizeof(cin.file.create_time),(unsigned char *)
956     cin.file.create_time);
957   offset+=WriteBlob(image,sizeof(cin.file.reserve),(unsigned char *)
958     cin.file.reserve);
959   cin.image.orientation=0x00;
960   offset+=WriteBlobByte(image,cin.image.orientation);
961   cin.image.number_channels=3;
962   offset+=WriteBlobByte(image,cin.image.number_channels);
963   offset+=WriteBlob(image,sizeof(cin.image.reserve1),(unsigned char *)
964     cin.image.reserve1);
965   for (i=0; i < 8; i++)
966   {
967     cin.image.channel[i].designator[0]=0; /* universal metric */
968     offset+=WriteBlobByte(image,cin.image.channel[0].designator[0]);
969     cin.image.channel[i].designator[1]=(unsigned char) (i > 3 ? 0 : i+1); /* channel color */;
970     offset+=WriteBlobByte(image,cin.image.channel[1].designator[0]);
971     cin.image.channel[i].bits_per_pixel=(unsigned char) image->depth;
972     offset+=WriteBlobByte(image,cin.image.channel[0].bits_per_pixel);
973     offset+=WriteBlobByte(image,cin.image.channel[0].reserve);
974     cin.image.channel[i].pixels_per_line=image->columns;
975     offset+=WriteBlobLong(image,(unsigned int)
976       cin.image.channel[0].pixels_per_line);
977     cin.image.channel[i].lines_per_image=image->rows;
978     offset+=WriteBlobLong(image,(unsigned int)
979       cin.image.channel[0].lines_per_image);
980     cin.image.channel[i].min_data=0;
981     offset+=WriteBlobFloat(image,cin.image.channel[0].min_data);
982     cin.image.channel[i].min_quantity=0.0;
983     offset+=WriteBlobFloat(image,cin.image.channel[0].min_quantity);
984     cin.image.channel[i].max_data=(float) ((MagickOffsetType)
985       GetQuantumRange(image->depth));
986     offset+=WriteBlobFloat(image,cin.image.channel[0].max_data);
987     cin.image.channel[i].max_quantity=2.048f;
988     offset+=WriteBlobFloat(image,cin.image.channel[0].max_quantity);
989   }
990   offset+=WriteBlobFloat(image,image->chromaticity.white_point.x);
991   offset+=WriteBlobFloat(image,image->chromaticity.white_point.y);
992   offset+=WriteBlobFloat(image,image->chromaticity.red_primary.x);
993   offset+=WriteBlobFloat(image,image->chromaticity.red_primary.y);
994   offset+=WriteBlobFloat(image,image->chromaticity.green_primary.x);
995   offset+=WriteBlobFloat(image,image->chromaticity.green_primary.y);
996   offset+=WriteBlobFloat(image,image->chromaticity.blue_primary.x);
997   offset+=WriteBlobFloat(image,image->chromaticity.blue_primary.y);
998   value=GetCINProperty(image_info,image,"dpx:image.label");
999   if (value != (const char *) NULL)
1000     (void) CopyMagickString(cin.image.label,value,sizeof(cin.image.label));
1001   offset+=WriteBlob(image,sizeof(cin.image.label),(unsigned char *)
1002     cin.image.label);
1003   offset+=WriteBlob(image,sizeof(cin.image.reserve),(unsigned char *)
1004     cin.image.reserve);
1005   /*
1006     Write data format information.
1007   */
1008   cin.data_format.interleave=0; /* pixel interleave (rgbrgbr...) */
1009   offset+=WriteBlobByte(image,cin.data_format.interleave);
1010   cin.data_format.packing=5; /* packing ssize_tword (32bit) boundaries */
1011   offset+=WriteBlobByte(image,cin.data_format.packing);
1012   cin.data_format.sign=0; /* unsigned data */
1013   offset+=WriteBlobByte(image,cin.data_format.sign);
1014   cin.data_format.sense=0; /* image sense: positive image */
1015   offset+=WriteBlobByte(image,cin.data_format.sense);
1016   cin.data_format.line_pad=0;
1017   offset+=WriteBlobLong(image,(unsigned int) cin.data_format.line_pad);
1018   cin.data_format.channel_pad=0;
1019   offset+=WriteBlobLong(image,(unsigned int) cin.data_format.channel_pad);
1020   offset+=WriteBlob(image,sizeof(cin.data_format.reserve),(unsigned char *)
1021     cin.data_format.reserve);
1022   /*
1023     Write origination information.
1024   */
1025   cin.origination.x_offset=0UL;
1026   value=GetCINProperty(image_info,image,"dpx:origination.x_offset");
1027   if (value != (const char *) NULL)
1028     cin.origination.x_offset=(ssize_t) StringToLong(value);
1029   offset+=WriteBlobLong(image,(unsigned int) cin.origination.x_offset);
1030   cin.origination.y_offset=0UL;
1031   value=GetCINProperty(image_info,image,"dpx:origination.y_offset");
1032   if (value != (const char *) NULL)
1033     cin.origination.y_offset=(ssize_t) StringToLong(value);
1034   offset+=WriteBlobLong(image,(unsigned int) cin.origination.y_offset);
1035   value=GetCINProperty(image_info,image,"dpx:origination.filename");
1036   if (value != (const char *) NULL)
1037     (void) CopyMagickString(cin.origination.filename,value,
1038       sizeof(cin.origination.filename));
1039   else
1040     (void) CopyMagickString(cin.origination.filename,image->filename,
1041       sizeof(cin.origination.filename));
1042   offset+=WriteBlob(image,sizeof(cin.origination.filename),(unsigned char *)
1043     cin.origination.filename);
1044   seconds=time((time_t *) NULL);
1045   (void) strftime(cin.origination.create_date,
1046     sizeof(cin.origination.create_date),"%Y:%m:%d",&local_time);
1047   offset+=WriteBlob(image,sizeof(cin.origination.create_date),(unsigned char *)
1048     cin.origination.create_date);
1049   (void) strftime(cin.origination.create_time,
1050     sizeof(cin.origination.create_time),"%H:%M:%S%Z",&local_time);
1051   offset+=WriteBlob(image,sizeof(cin.origination.create_time),(unsigned char *)
1052     cin.origination.create_time);
1053   value=GetCINProperty(image_info,image,"dpx:origination.device");
1054   if (value != (const char *) NULL)
1055     (void) CopyMagickString(cin.origination.device,value,
1056       sizeof(cin.origination.device));
1057   offset+=WriteBlob(image,sizeof(cin.origination.device),(unsigned char *)
1058     cin.origination.device);
1059   value=GetCINProperty(image_info,image,"dpx:origination.model");
1060   if (value != (const char *) NULL)
1061     (void) CopyMagickString(cin.origination.model,value,
1062       sizeof(cin.origination.model));
1063   offset+=WriteBlob(image,sizeof(cin.origination.model),(unsigned char *)
1064     cin.origination.model);
1065   value=GetCINProperty(image_info,image,"dpx:origination.serial");
1066   if (value != (const char *) NULL)
1067     (void) CopyMagickString(cin.origination.serial,value,
1068       sizeof(cin.origination.serial));
1069   offset+=WriteBlob(image,sizeof(cin.origination.serial),(unsigned char *)
1070     cin.origination.serial);
1071   cin.origination.x_pitch=0.0f;
1072   value=GetCINProperty(image_info,image,"dpx:origination.x_pitch");
1073   if (value != (const char *) NULL)
1074     cin.origination.x_pitch=InterpretLocaleValue(value,(char **) NULL);
1075   offset+=WriteBlobFloat(image,cin.origination.x_pitch);
1076   cin.origination.y_pitch=0.0f;
1077   value=GetCINProperty(image_info,image,"dpx:origination.y_pitch");
1078   if (value != (const char *) NULL)
1079     cin.origination.y_pitch=InterpretLocaleValue(value,(char **) NULL);
1080   offset+=WriteBlobFloat(image,cin.origination.y_pitch);
1081   cin.origination.gamma=image->gamma;
1082   offset+=WriteBlobFloat(image,cin.origination.gamma);
1083   offset+=WriteBlob(image,sizeof(cin.origination.reserve),(unsigned char *)
1084     cin.origination.reserve);
1085   /*
1086     Image film information.
1087   */
1088   cin.film.id=0;
1089   value=GetCINProperty(image_info,image,"dpx:film.id");
1090   if (value != (const char *) NULL)
1091     cin.film.id=(char) StringToLong(value);
1092   offset+=WriteBlobByte(image,(unsigned char) cin.film.id);
1093   cin.film.type=0;
1094   value=GetCINProperty(image_info,image,"dpx:film.type");
1095   if (value != (const char *) NULL)
1096     cin.film.type=(char) StringToLong(value);
1097   offset+=WriteBlobByte(image,(unsigned char) cin.film.type);
1098   cin.film.offset=0;
1099   value=GetCINProperty(image_info,image,"dpx:film.offset");
1100   if (value != (const char *) NULL)
1101     cin.film.offset=(char) StringToLong(value);
1102   offset+=WriteBlobByte(image,(unsigned char) cin.film.offset);
1103   offset+=WriteBlobByte(image,(unsigned char) cin.film.reserve1);
1104   cin.film.prefix=0UL;
1105   value=GetCINProperty(image_info,image,"dpx:film.prefix");
1106   if (value != (const char *) NULL)
1107     cin.film.prefix=StringToUnsignedLong(value);
1108   offset+=WriteBlobLong(image,(unsigned int) cin.film.prefix);
1109   cin.film.count=0UL;
1110   value=GetCINProperty(image_info,image,"dpx:film.count");
1111   if (value != (const char *) NULL)
1112     cin.film.count=StringToUnsignedLong(value);
1113   offset+=WriteBlobLong(image,(unsigned int) cin.film.count);
1114   value=GetCINProperty(image_info,image,"dpx:film.format");
1115   if (value != (const char *) NULL)
1116     (void) CopyMagickString(cin.film.format,value,sizeof(cin.film.format));
1117   offset+=WriteBlob(image,sizeof(cin.film.format),(unsigned char *)
1118     cin.film.format);
1119   cin.film.frame_position=0UL;
1120   value=GetCINProperty(image_info,image,"dpx:film.frame_position");
1121   if (value != (const char *) NULL)
1122     cin.film.frame_position=StringToUnsignedLong(value);
1123   offset+=WriteBlobLong(image,(unsigned int) cin.film.frame_position);
1124   cin.film.frame_rate=0.0f;
1125   value=GetCINProperty(image_info,image,"dpx:film.frame_rate");
1126   if (value != (const char *) NULL)
1127     cin.film.frame_rate=InterpretLocaleValue(value,(char **) NULL);
1128   offset+=WriteBlobFloat(image,cin.film.frame_rate);
1129   value=GetCINProperty(image_info,image,"dpx:film.frame_id");
1130   if (value != (const char *) NULL)
1131     (void) CopyMagickString(cin.film.frame_id,value,sizeof(cin.film.frame_id));
1132   offset+=WriteBlob(image,sizeof(cin.film.frame_id),(unsigned char *)
1133     cin.film.frame_id);
1134   value=GetCINProperty(image_info,image,"dpx:film.slate_info");
1135   if (value != (const char *) NULL)
1136     (void) CopyMagickString(cin.film.slate_info,value,
1137       sizeof(cin.film.slate_info));
1138   offset+=WriteBlob(image,sizeof(cin.film.slate_info),(unsigned char *)
1139     cin.film.slate_info);
1140   offset+=WriteBlob(image,sizeof(cin.film.reserve),(unsigned char *)
1141     cin.film.reserve);
1142   if (profile != (StringInfo *) NULL)
1143     offset+=WriteBlob(image,GetStringInfoLength(profile),
1144       GetStringInfoDatum(profile));
1145   while (offset < (MagickOffsetType) cin.file.image_offset)
1146     offset+=WriteBlobByte(image,0x00);
1147   /*
1148     Convert pixel packets to CIN raster image.
1149   */
1150   quantum_info=AcquireQuantumInfo(image_info,image);
1151   if (quantum_info == (QuantumInfo *) NULL)
1152     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1153   quantum_info->quantum=32;
1154   quantum_info->pack=MagickFalse;
1155   quantum_type=RGBQuantum;
1156   pixels=GetQuantumPixels(quantum_info);
1157   length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
1158   if (0)
1159     {
1160       quantum_type=GrayQuantum;
1161       length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
1162     }
1163   for (y=0; y < (ssize_t) image->rows; y++)
1164   {
1165     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1166     if (p == (const Quantum *) NULL)
1167       break;
1168     (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1169       quantum_type,pixels,exception);
1170     count=WriteBlob(image,length,pixels);
1171     if (count != (ssize_t) length)
1172       break;
1173     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1174       image->rows);
1175     if (status == MagickFalse)
1176       break;
1177   }
1178   quantum_info=DestroyQuantumInfo(quantum_info);
1179   (void) CloseBlob(image);
1180   return(status);
1181 }