]> granicus.if.org Git - imagemagick/blob - coders/cin.c
(no commit message)
[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-2010 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 "magick/studio.h"
50 #include "magick/blob.h"
51 #include "magick/blob-private.h"
52 #include "magick/cache.h"
53 #include "magick/colorspace.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/image.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/magick.h"
60 #include "magick/memory_.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/option.h"
64 #include "magick/profile.h"
65 #include "magick/property.h"
66 #include "magick/quantum-private.h"
67 #include "magick/quantum-private.h"
68 #include "magick/static.h"
69 #include "magick/string_.h"
70 #include "magick/string-private.h"
71 #include "magick/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 *);
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     size_t
365       unsigned_value;
366
367     double
368       float_value;
369   } quantum;
370
371   quantum.unsigned_value=0UL;
372   quantum.float_value=value;
373   if (quantum.unsigned_value == 0UL)
374     return(MagickFalse);
375   return(MagickTrue);
376 }
377
378 static Image *ReadCINImage(const ImageInfo *image_info,
379   ExceptionInfo *exception)
380 {
381 #define MonoColorType  1
382 #define RGBColorType  3
383
384   char
385     magick[4];
386
387   CINInfo
388     cin;
389
390   Image
391     *image;
392
393   ssize_t
394     y;
395
396   MagickBooleanType
397     status;
398
399   MagickOffsetType
400     offset;
401
402   QuantumInfo
403     *quantum_info;
404
405   QuantumType
406     quantum_type;
407
408   register ssize_t
409     i;
410
411   register PixelPacket
412     *q;
413
414   size_t
415     length;
416
417   ssize_t
418     count;
419
420   unsigned char
421     *pixels;
422
423   size_t
424     lsb_first;
425
426   /*
427     Open image file.
428   */
429   assert(image_info != (const ImageInfo *) NULL);
430   assert(image_info->signature == MagickSignature);
431   if (image_info->debug != MagickFalse)
432     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
433       image_info->filename);
434   assert(exception != (ExceptionInfo *) NULL);
435   assert(exception->signature == MagickSignature);
436   image=AcquireImage(image_info);
437   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
438   if (status == MagickFalse)
439     {
440       image=DestroyImageList(image);
441       return((Image *) NULL);
442     }
443   /*
444     File information.
445   */
446   offset=0;
447   count=ReadBlob(image,4,(unsigned char *) magick);
448   offset+=count;
449   if ((count != 4) ||
450       ((LocaleNCompare((char *) magick,"\200\052\137\327",4) != 0)))
451     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
452   image->endian=LSBEndian;
453   lsb_first=1;
454   if ((int) (*(char *) &lsb_first) != 0)
455     image->endian=MSBEndian;
456   cin.file.image_offset=ReadBlobLong(image);
457   offset+=4;
458   cin.file.generic_length=ReadBlobLong(image);
459   offset+=4;
460   cin.file.industry_length=ReadBlobLong(image);
461   offset+=4;
462   cin.file.user_length=ReadBlobLong(image);
463   offset+=4;
464   cin.file.file_size=ReadBlobLong(image);
465   offset+=4;
466   offset+=ReadBlob(image,sizeof(cin.file.version),(unsigned char *)
467     cin.file.version);
468   (void) SetImageProperty(image,"cin:file.version",cin.file.version);
469   offset+=ReadBlob(image,sizeof(cin.file.filename),(unsigned char *)
470     cin.file.filename);
471   (void) SetImageProperty(image,"cin:file.filename",cin.file.filename);
472   offset+=ReadBlob(image,sizeof(cin.file.create_date),(unsigned char *)
473     cin.file.create_date);
474   (void) SetImageProperty(image,"cin:file.create_date",cin.file.create_date);
475   offset+=ReadBlob(image,sizeof(cin.file.create_time),(unsigned char *)
476     cin.file.create_time);
477   (void) SetImageProperty(image,"cin:file.create_time",cin.file.create_time);
478   offset+=ReadBlob(image,sizeof(cin.file.reserve),(unsigned char *)
479     cin.file.reserve);
480   /*
481     Image information.
482   */
483   cin.image.orientation=(unsigned char) ReadBlobByte(image);
484   offset++;
485   if (cin.image.orientation != (unsigned char) (~0U))
486     (void) FormatImageProperty(image,"cin:image.orientation","%d",
487       cin.image.orientation);
488   switch (cin.image.orientation)
489   {
490     default:
491     case 0:  image->orientation=TopLeftOrientation; break;
492     case 1:  image->orientation=TopRightOrientation; break;
493     case 2:  image->orientation=BottomLeftOrientation; break;
494     case 3:  image->orientation=BottomRightOrientation; break;
495     case 4:  image->orientation=LeftTopOrientation; break;
496     case 5:  image->orientation=RightTopOrientation; break;
497     case 6:  image->orientation=LeftBottomOrientation; break;
498     case 7:  image->orientation=RightBottomOrientation; break;
499   }
500   cin.image.number_channels=(unsigned char) ReadBlobByte(image);
501   offset++;
502   offset+=ReadBlob(image,sizeof(cin.image.reserve1),(unsigned char *)
503     cin.image.reserve1);
504   for (i=0; i < 8; i++)
505   {
506     cin.image.channel[i].designator[0]=(unsigned char) ReadBlobByte(image);
507     offset++;
508     cin.image.channel[i].designator[1]=(unsigned char) ReadBlobByte(image);
509     offset++;
510     cin.image.channel[i].bits_per_pixel=(unsigned char) ReadBlobByte(image);
511     offset++;
512     cin.image.channel[i].reserve=(unsigned char) ReadBlobByte(image);
513     offset++;
514     cin.image.channel[i].pixels_per_line=ReadBlobLong(image);
515     offset+=4;
516     cin.image.channel[i].lines_per_image=ReadBlobLong(image);
517     offset+=4;
518     cin.image.channel[i].min_data=ReadBlobFloat(image);
519     offset+=4;
520     cin.image.channel[i].min_quantity=ReadBlobFloat(image);
521     offset+=4;
522     cin.image.channel[i].max_data=ReadBlobFloat(image);
523     offset+=4;
524     cin.image.channel[i].max_quantity=ReadBlobFloat(image);
525     offset+=4;
526   }
527   cin.image.white_point[0]=ReadBlobFloat(image);
528   offset+=4;
529   if (IsFloatDefined(cin.image.white_point[0]) != MagickFalse)
530     image->chromaticity.white_point.x=cin.image.white_point[0];
531   cin.image.white_point[1]=ReadBlobFloat(image);
532   offset+=4;
533   if (IsFloatDefined(cin.image.white_point[1]) != MagickFalse)
534     image->chromaticity.white_point.y=cin.image.white_point[1];
535   cin.image.red_primary_chromaticity[0]=ReadBlobFloat(image);
536   offset+=4;
537   if (IsFloatDefined(cin.image.red_primary_chromaticity[0]) != MagickFalse)
538     image->chromaticity.red_primary.x=cin.image.red_primary_chromaticity[0];
539   cin.image.red_primary_chromaticity[1]=ReadBlobFloat(image);
540   offset+=4;
541   if (IsFloatDefined(cin.image.red_primary_chromaticity[1]) != MagickFalse)
542     image->chromaticity.red_primary.y=cin.image.red_primary_chromaticity[1];
543   cin.image.green_primary_chromaticity[0]=ReadBlobFloat(image);
544   offset+=4;
545   if (IsFloatDefined(cin.image.green_primary_chromaticity[0]) != MagickFalse)
546     image->chromaticity.red_primary.x=cin.image.green_primary_chromaticity[0];
547   cin.image.green_primary_chromaticity[1]=ReadBlobFloat(image);
548   offset+=4;
549   if (IsFloatDefined(cin.image.green_primary_chromaticity[1]) != MagickFalse)
550     image->chromaticity.green_primary.y=cin.image.green_primary_chromaticity[1];
551   cin.image.blue_primary_chromaticity[0]=ReadBlobFloat(image);
552   offset+=4;
553   if (IsFloatDefined(cin.image.blue_primary_chromaticity[0]) != MagickFalse)
554     image->chromaticity.blue_primary.x=cin.image.blue_primary_chromaticity[0];
555   cin.image.blue_primary_chromaticity[1]=ReadBlobFloat(image);
556   offset+=4;
557   if (IsFloatDefined(cin.image.blue_primary_chromaticity[1]) != MagickFalse)
558     image->chromaticity.blue_primary.y=cin.image.blue_primary_chromaticity[1];
559   offset+=ReadBlob(image,sizeof(cin.image.label),(unsigned char *)
560     cin.image.label);
561   (void) SetImageProperty(image,"cin:image.label",cin.image.label);
562   offset+=ReadBlob(image,sizeof(cin.image.reserve),(unsigned char *)
563     cin.image.reserve);
564   /*
565     Image data format information.
566   */
567   cin.data_format.interleave=(unsigned char) ReadBlobByte(image);
568   offset++;
569   cin.data_format.packing=(unsigned char) ReadBlobByte(image);
570   offset++;
571   cin.data_format.sign=(unsigned char) ReadBlobByte(image);
572   offset++;
573   cin.data_format.sense=(unsigned char) ReadBlobByte(image);
574   offset++;
575   cin.data_format.line_pad=ReadBlobLong(image);
576   offset+=4;
577   cin.data_format.channel_pad=ReadBlobLong(image);
578   offset+=4;
579   offset+=ReadBlob(image,sizeof(cin.data_format.reserve),(unsigned char *)
580     cin.data_format.reserve);
581   /*
582     Image origination information.
583   */
584   cin.origination.x_offset=(int) ReadBlobLong(image);
585   offset+=4;
586   if ((size_t) cin.origination.x_offset != ~0UL)
587     (void) FormatImageProperty(image,"cin:origination.x_offset","%.20g",
588       (double) cin.origination.x_offset);
589   cin.origination.y_offset=(int) ReadBlobLong(image);
590   offset+=4;
591   if ((size_t) cin.origination.y_offset != ~0UL)
592     (void) FormatImageProperty(image,"cin:origination.y_offset","%.20g",
593       (double) cin.origination.y_offset);
594   offset+=ReadBlob(image,sizeof(cin.origination.filename),(unsigned char *)
595     cin.origination.filename);
596   (void) SetImageProperty(image,"cin:origination.filename",
597     cin.origination.filename);
598   offset+=ReadBlob(image,sizeof(cin.origination.create_date),(unsigned char *)
599     cin.origination.create_date);
600   (void) SetImageProperty(image,"cin:origination.create_date",
601     cin.origination.create_date);
602   offset+=ReadBlob(image,sizeof(cin.origination.create_time),(unsigned char *)
603     cin.origination.create_time);
604   (void) SetImageProperty(image,"cin:origination.create_time",
605     cin.origination.create_time);
606   offset+=ReadBlob(image,sizeof(cin.origination.device),(unsigned char *)
607     cin.origination.device);
608   (void) SetImageProperty(image,"cin:origination.device",
609     cin.origination.device);
610   offset+=ReadBlob(image,sizeof(cin.origination.model),(unsigned char *)
611     cin.origination.model);
612   (void) SetImageProperty(image,"cin:origination.model",cin.origination.model);
613   offset+=ReadBlob(image,sizeof(cin.origination.serial),(unsigned char *)
614     cin.origination.serial);
615   (void) SetImageProperty(image,"cin:origination.serial",
616     cin.origination.serial);
617   cin.origination.x_pitch=ReadBlobFloat(image);
618   offset+=4;
619   cin.origination.y_pitch=ReadBlobFloat(image);
620   offset+=4;
621   cin.origination.gamma=ReadBlobFloat(image);
622   offset+=4;
623   if (IsFloatDefined(cin.origination.gamma) != MagickFalse)
624     image->gamma=cin.origination.gamma;
625   offset+=ReadBlob(image,sizeof(cin.origination.reserve),(unsigned char *)
626     cin.origination.reserve);
627   if ((cin.file.image_offset > 2048) && (cin.file.user_length != 0))
628     {
629       /*
630         Image film information.
631       */
632       cin.film.id=ReadBlobByte(image);
633       offset++;
634       if (((size_t) cin.film.id) != ~0UL)
635         (void) FormatImageProperty(image,"cin:film.id","%d",cin.film.id);
636       cin.film.type=ReadBlobByte(image);
637       offset++;
638       if (((size_t) cin.film.type) != ~0UL)
639         (void) FormatImageProperty(image,"cin:film.type","%d",cin.film.type);
640       cin.film.offset=ReadBlobByte(image);
641       offset++;
642       if (((size_t) cin.film.offset) != ~0UL)
643         (void) FormatImageProperty(image,"cin:film.offset","%d",
644           cin.film.offset);
645       cin.film.reserve1=ReadBlobByte(image);
646       offset++;
647       cin.film.prefix=ReadBlobLong(image);
648       offset+=4;
649       if (cin.film.prefix != ~0UL)
650         (void) FormatImageProperty(image,"cin:film.prefix","%.20g",(double)
651           cin.film.prefix);
652       cin.film.count=ReadBlobLong(image);
653       offset+=4;
654       offset+=ReadBlob(image,sizeof(cin.film.format),(unsigned char *)
655         cin.film.format);
656       (void) SetImageProperty(image,"cin:film.format",cin.film.format);
657       cin.film.frame_position=ReadBlobLong(image);
658       offset+=4;
659       if (cin.film.frame_position != ~0UL)
660         (void) FormatImageProperty(image,"cin:film.frame_position","%.20g",
661           (double) cin.film.frame_position);
662       cin.film.frame_rate=ReadBlobFloat(image);
663       offset+=4;
664       if (IsFloatDefined(cin.film.frame_rate) != MagickFalse)
665         (void) FormatImageProperty(image,"cin:film.frame_rate","%g",
666           cin.film.frame_rate);
667       offset+=ReadBlob(image,sizeof(cin.film.frame_id),(unsigned char *)
668         cin.film.frame_id);
669       (void) SetImageProperty(image,"cin:film.frame_id",cin.film.frame_id);
670       offset+=ReadBlob(image,sizeof(cin.film.slate_info),(unsigned char *)
671         cin.film.slate_info);
672       (void) SetImageProperty(image,"cin:film.slate_info",cin.film.slate_info);
673       offset+=ReadBlob(image,sizeof(cin.film.reserve),(unsigned char *)
674         cin.film.reserve);
675     }
676   if ((cin.file.image_offset > 2048) && (cin.file.user_length != 0))
677     {
678       StringInfo
679         *profile;
680
681       /*
682         User defined data.
683       */
684       profile=AcquireStringInfo(cin.file.user_length);
685       offset+=ReadBlob(image,GetStringInfoLength(profile),
686         GetStringInfoDatum(profile));
687       (void) SetImageProfile(image,"cin:user.data",profile);
688       profile=DestroyStringInfo(profile);
689     }
690   for ( ; offset < (ssize_t) cin.file.image_offset; offset++)
691     (void) ReadBlobByte(image);
692   image->depth=cin.image.channel[0].bits_per_pixel;
693   image->columns=cin.image.channel[0].pixels_per_line;
694   image->rows=cin.image.channel[0].lines_per_image;
695   if (image_info->ping)
696     {
697       (void) CloseBlob(image);
698       return(image);
699     }
700   /*
701     Convert CIN raster image to pixel packets.
702   */
703   quantum_info=AcquireQuantumInfo(image_info,image);
704   if (quantum_info == (QuantumInfo *) NULL)
705     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
706   quantum_info->quantum=32;
707   quantum_info->pack=MagickFalse;
708   quantum_type=RGBQuantum;
709   pixels=GetQuantumPixels(quantum_info);
710   length=GetQuantumExtent(image,quantum_info,quantum_type);
711   length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
712   if (cin.image.number_channels == 1)
713     {
714       quantum_type=GrayQuantum;
715       length=GetBytesPerRow(image->columns,1,image->depth,MagickTrue);
716     }
717   for (y=0; y < (ssize_t) image->rows; y++)
718   {
719     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
720     if (q == (PixelPacket *) NULL)
721       break;
722     count=ReadBlob(image,length,pixels);
723     if ((size_t) count != length)
724       break;
725     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
726       quantum_type,pixels,exception);
727     if (SyncAuthenticPixels(image,exception) == MagickFalse)
728       break;
729     if (image->previous == (Image *) NULL)
730       {
731         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
732                 image->rows);
733         if (status == MagickFalse)
734           break;
735       }
736   }
737   SetQuantumImageType(image,quantum_type);
738   quantum_info=DestroyQuantumInfo(quantum_info);
739   if (EOFBlob(image) != MagickFalse)
740     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
741       image->filename);
742   image->colorspace=LogColorspace;
743   (void) CloseBlob(image);
744   return(GetFirstImageInList(image));
745 }
746 \f
747 /*
748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 %                                                                             %
750 %                                                                             %
751 %                                                                             %
752 %   R e g i s t e r C I N E O N I m a g e                                     %
753 %                                                                             %
754 %                                                                             %
755 %                                                                             %
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 %
758 %  RegisterCINImage() adds attributes for the CIN image format to the list of
759 %  of supported formats.  The attributes include the image format tag, a method
760 %  to read and/or write the format, whether the format supports the saving of
761 %  more than one frame to the same file or blob, whether the format supports
762 %  native in-memory I/O, and a brief description of the format.
763 %
764 %  The format of the RegisterCINImage method is:
765 %
766 %      size_t RegisterCINImage(void)
767 %
768 */
769 ModuleExport size_t RegisterCINImage(void)
770 {
771   MagickInfo
772     *entry;
773
774   entry=SetMagickInfo("CIN");
775   entry->decoder=(DecodeImageHandler *) ReadCINImage;
776   entry->encoder=(EncodeImageHandler *) WriteCINImage;
777   entry->magick=(IsImageFormatHandler *) IsCIN;
778   entry->description=ConstantString("Cineon Image File");
779   entry->module=ConstantString("CIN");
780   (void) RegisterMagickInfo(entry);
781   return(MagickImageCoderSignature);
782 }
783 \f
784 /*
785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 %                                                                             %
787 %                                                                             %
788 %                                                                             %
789 %   U n r e g i s t e r C I N E O N I m a g e                                 %
790 %                                                                             %
791 %                                                                             %
792 %                                                                             %
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 %
795 %  UnregisterCINImage() removes format registrations made by the CIN module
796 %  from the list of supported formats.
797 %
798 %  The format of the UnregisterCINImage method is:
799 %
800 %      UnregisterCINImage(void)
801 %
802 */
803 ModuleExport void UnregisterCINImage(void)
804 {
805   (void) UnregisterMagickInfo("CINEON");
806 }
807 \f
808 /*
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 %                                                                             %
811 %                                                                             %
812 %                                                                             %
813 %   W r i t e C I N E O N I m a g e                                           %
814 %                                                                             %
815 %                                                                             %
816 %                                                                             %
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 %
819 %  WriteCINImage() writes an image in CIN encoded image format.
820 %
821 %  The format of the WriteCINImage method is:
822 %
823 %      MagickBooleanType WriteCINImage(const ImageInfo *image_info,Image *image)
824 %
825 %  A description of each parameter follows.
826 %
827 %    o image_info: the image info.
828 %
829 %    o image:  The image.
830 %
831 */
832
833 static inline const char *GetCINProperty(const ImageInfo *image_info,
834   const Image *image,const char *property)
835 {
836   const char
837     *value;
838
839   value=GetImageOption(image_info,property);
840   if (value != (const char *) NULL)
841     return(value);
842   return(GetImageProperty(image,property));
843 }
844
845 static MagickBooleanType WriteCINImage(const ImageInfo *image_info,Image *image)
846 {
847   const char
848     *value;
849
850   CINInfo
851     cin;
852
853   const StringInfo
854     *profile;
855
856   ssize_t
857     y;
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 PixelPacket
872     *p;
873
874   register ssize_t
875     i;
876
877   size_t
878     length;
879
880   ssize_t
881     count;
882
883   struct tm
884     local_time;
885
886   time_t
887     seconds;
888
889   unsigned char
890     *pixels;
891
892   /*
893     Open output image file.
894   */
895   assert(image_info != (const ImageInfo *) NULL);
896   assert(image_info->signature == MagickSignature);
897   assert(image != (Image *) NULL);
898   assert(image->signature == MagickSignature);
899   if (image->debug != MagickFalse)
900     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
901   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
902   if (status == MagickFalse)
903     return(status);
904   if (image->colorspace != LogColorspace)
905     (void) TransformImageColorspace(image,LogColorspace);
906   /*
907     Write image information.
908   */
909   (void) ResetMagickMemory(&cin,0,sizeof(cin));
910   offset=0;
911   cin.file.magic=0x802A5FD7UL;
912   offset+=WriteBlobLong(image,(unsigned int) cin.file.magic);
913   cin.file.image_offset=0x800;
914   profile=GetImageProfile(image,"cin:user.data");
915   if (profile != (StringInfo *) NULL)
916     {
917       cin.file.image_offset+=(size_t) GetStringInfoLength(profile);
918       cin.file.image_offset=(((cin.file.image_offset+0x2000-1)/0x2000)*0x2000);
919     }
920   offset+=WriteBlobLong(image,(unsigned int) cin.file.image_offset);
921   cin.file.generic_length=0x400;
922   offset+=WriteBlobLong(image,(unsigned int) cin.file.generic_length);
923   cin.file.industry_length=0x400;
924   offset+=WriteBlobLong(image,(unsigned int) cin.file.industry_length);
925   cin.file.user_length=0x00;
926   if (profile != (StringInfo *) NULL)
927     {
928       cin.file.user_length+=(size_t) GetStringInfoLength(profile);
929       cin.file.user_length=(((cin.file.user_length+0x2000-1)/0x2000)*0x2000);
930     }
931   offset+=WriteBlobLong(image,(unsigned int) cin.file.user_length);
932   cin.file.file_size=4*image->columns*image->rows+0x2000;
933   offset+=WriteBlobLong(image,(unsigned int) cin.file.file_size);
934   (void) CopyMagickString(cin.file.version,"V4.5",sizeof(cin.file.version));
935   offset+=WriteBlob(image,sizeof(cin.file.version),(unsigned char *)
936     cin.file.version);
937   value=GetCINProperty(image_info,image,"cin:file.filename");
938   if (value != (const char *) NULL)
939     (void) CopyMagickString(cin.file.filename,value,sizeof(cin.file.filename));
940   else
941     (void) CopyMagickString(cin.file.filename,image->filename,
942       sizeof(cin.file.filename));
943   offset+=WriteBlob(image,sizeof(cin.file.filename),(unsigned char *)
944     cin.file.filename);
945   seconds=time((time_t *) NULL);
946 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
947   (void) localtime_r(&seconds,&local_time);
948 #else
949   (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
950 #endif
951   (void) strftime(cin.file.create_date,sizeof(cin.file.create_date),"%Y:%m:%d",
952     &local_time);
953   offset+=WriteBlob(image,sizeof(cin.file.create_date),(unsigned char *)
954     cin.file.create_date);
955   (void) strftime(cin.file.create_time,sizeof(cin.file.create_time),
956     "%H:%M:%S%Z",&local_time);
957   offset+=WriteBlob(image,sizeof(cin.file.create_time),(unsigned char *)
958     cin.file.create_time);
959   offset+=WriteBlob(image,sizeof(cin.file.reserve),(unsigned char *)
960     cin.file.reserve);
961   cin.image.orientation=0x00;
962   offset+=WriteBlobByte(image,cin.image.orientation);
963   cin.image.number_channels=3;
964   offset+=WriteBlobByte(image,cin.image.number_channels);
965   offset+=WriteBlob(image,sizeof(cin.image.reserve1),(unsigned char *)
966     cin.image.reserve1);
967   for (i=0; i < 8; i++)
968   {
969     cin.image.channel[i].designator[0]=0; /* universal metric */
970     offset+=WriteBlobByte(image,cin.image.channel[0].designator[0]);
971     cin.image.channel[i].designator[1]=(unsigned char) (i > 3 ? 0 : i+1); /* channel color */;
972     offset+=WriteBlobByte(image,cin.image.channel[1].designator[0]);
973     cin.image.channel[i].bits_per_pixel=(unsigned char) image->depth;
974     offset+=WriteBlobByte(image,cin.image.channel[0].bits_per_pixel);
975     offset+=WriteBlobByte(image,cin.image.channel[0].reserve);
976     cin.image.channel[i].pixels_per_line=image->columns;
977     offset+=WriteBlobLong(image,(unsigned int)
978       cin.image.channel[0].pixels_per_line);
979     cin.image.channel[i].lines_per_image=image->rows;
980     offset+=WriteBlobLong(image,(unsigned int)
981       cin.image.channel[0].lines_per_image);
982     cin.image.channel[i].min_data=0;
983     offset+=WriteBlobFloat(image,cin.image.channel[0].min_data);
984     cin.image.channel[i].min_quantity=0.0;
985     offset+=WriteBlobFloat(image,cin.image.channel[0].min_quantity);
986     cin.image.channel[i].max_data=(float) ((MagickOffsetType)
987       GetQuantumRange(image->depth));
988     offset+=WriteBlobFloat(image,cin.image.channel[0].max_data);
989     cin.image.channel[i].max_quantity=2.048f;
990     offset+=WriteBlobFloat(image,cin.image.channel[0].max_quantity);
991   }
992   offset+=WriteBlobFloat(image,image->chromaticity.white_point.x);
993   offset+=WriteBlobFloat(image,image->chromaticity.white_point.y);
994   offset+=WriteBlobFloat(image,image->chromaticity.red_primary.x);
995   offset+=WriteBlobFloat(image,image->chromaticity.red_primary.y);
996   offset+=WriteBlobFloat(image,image->chromaticity.green_primary.x);
997   offset+=WriteBlobFloat(image,image->chromaticity.green_primary.y);
998   offset+=WriteBlobFloat(image,image->chromaticity.blue_primary.x);
999   offset+=WriteBlobFloat(image,image->chromaticity.blue_primary.y);
1000   value=GetCINProperty(image_info,image,"cin:image.label");
1001   if (value != (const char *) NULL)
1002     (void) CopyMagickString(cin.image.label,value,sizeof(cin.image.label));
1003   offset+=WriteBlob(image,sizeof(cin.image.label),(unsigned char *)
1004     cin.image.label);
1005   offset+=WriteBlob(image,sizeof(cin.image.reserve),(unsigned char *)
1006     cin.image.reserve);
1007   /*
1008     Write data format information.
1009   */
1010   cin.data_format.interleave=0; /* pixel interleave (rgbrgbr...) */
1011   offset+=WriteBlobByte(image,cin.data_format.interleave);
1012   cin.data_format.packing=5; /* packing ssize_tword (32bit) boundaries */
1013   offset+=WriteBlobByte(image,cin.data_format.packing);
1014   cin.data_format.sign=0; /* unsigned data */
1015   offset+=WriteBlobByte(image,cin.data_format.sign);
1016   cin.data_format.sense=0; /* image sense: positive image */
1017   offset+=WriteBlobByte(image,cin.data_format.sense);
1018   cin.data_format.line_pad=0;
1019   offset+=WriteBlobLong(image,(unsigned int) cin.data_format.line_pad);
1020   cin.data_format.channel_pad=0;
1021   offset+=WriteBlobLong(image,(unsigned int) cin.data_format.channel_pad);
1022   offset+=WriteBlob(image,sizeof(cin.data_format.reserve),(unsigned char *)
1023     cin.data_format.reserve);
1024   /*
1025     Write origination information.
1026   */
1027   cin.origination.x_offset=0UL;
1028   value=GetCINProperty(image_info,image,"cin:origination.x_offset");
1029   if (value != (const char *) NULL)
1030     cin.origination.x_offset=StringToLong(value);
1031   offset+=WriteBlobLong(image,(unsigned int) cin.origination.x_offset);
1032   cin.origination.y_offset=0UL;
1033   value=GetCINProperty(image_info,image,"cin:origination.y_offset");
1034   if (value != (const char *) NULL)
1035     cin.origination.y_offset=StringToLong(value);
1036   offset+=WriteBlobLong(image,(unsigned int) cin.origination.y_offset);
1037   value=GetCINProperty(image_info,image,"cin:origination.filename");
1038   if (value != (const char *) NULL)
1039     (void) CopyMagickString(cin.origination.filename,value,
1040       sizeof(cin.origination.filename));
1041   else
1042     (void) CopyMagickString(cin.origination.filename,image->filename,
1043       sizeof(cin.origination.filename));
1044   offset+=WriteBlob(image,sizeof(cin.origination.filename),(unsigned char *)
1045     cin.origination.filename);
1046   seconds=time((time_t *) NULL);
1047   (void) strftime(cin.origination.create_date,
1048     sizeof(cin.origination.create_date),"%Y:%m:%d",&local_time);
1049   offset+=WriteBlob(image,sizeof(cin.origination.create_date),(unsigned char *)
1050     cin.origination.create_date);
1051   (void) strftime(cin.origination.create_time,
1052     sizeof(cin.origination.create_time),"%H:%M:%S%Z",&local_time);
1053   offset+=WriteBlob(image,sizeof(cin.origination.create_time),(unsigned char *)
1054     cin.origination.create_time);
1055   value=GetCINProperty(image_info,image,"cin:origination.device");
1056   if (value != (const char *) NULL)
1057     (void) CopyMagickString(cin.origination.device,value,
1058       sizeof(cin.origination.device));
1059   offset+=WriteBlob(image,sizeof(cin.origination.device),(unsigned char *)
1060     cin.origination.device);
1061   value=GetCINProperty(image_info,image,"cin:origination.model");
1062   if (value != (const char *) NULL)
1063     (void) CopyMagickString(cin.origination.model,value,
1064       sizeof(cin.origination.model));
1065   offset+=WriteBlob(image,sizeof(cin.origination.model),(unsigned char *)
1066     cin.origination.model);
1067   value=GetCINProperty(image_info,image,"cin:origination.serial");
1068   if (value != (const char *) NULL)
1069     (void) CopyMagickString(cin.origination.serial,value,
1070       sizeof(cin.origination.serial));
1071   offset+=WriteBlob(image,sizeof(cin.origination.serial),(unsigned char *)
1072     cin.origination.serial);
1073   cin.origination.x_pitch=0.0f;
1074   value=GetCINProperty(image_info,image,"cin:origination.x_pitch");
1075   if (value != (const char *) NULL)
1076     cin.origination.x_pitch=StringToDouble(value);
1077   offset+=WriteBlobFloat(image,cin.origination.x_pitch);
1078   cin.origination.y_pitch=0.0f;
1079   value=GetCINProperty(image_info,image,"cin:origination.y_pitch");
1080   if (value != (const char *) NULL)
1081     cin.origination.y_pitch=StringToDouble(value);
1082   offset+=WriteBlobFloat(image,cin.origination.y_pitch);
1083   cin.origination.gamma=image->gamma;
1084   offset+=WriteBlobFloat(image,cin.origination.gamma);
1085   offset+=WriteBlob(image,sizeof(cin.origination.reserve),(unsigned char *)
1086     cin.origination.reserve);
1087   /*
1088     Image film information.
1089   */
1090   cin.film.id=0;
1091   value=GetCINProperty(image_info,image,"cin:film.id");
1092   if (value != (const char *) NULL)
1093     cin.film.id=(char) StringToLong(value);
1094   offset+=WriteBlobByte(image,(unsigned char) cin.film.id);
1095   cin.film.type=0;
1096   value=GetCINProperty(image_info,image,"cin:film.type");
1097   if (value != (const char *) NULL)
1098     cin.film.type=(char) StringToLong(value);
1099   offset+=WriteBlobByte(image,(unsigned char) cin.film.type);
1100   cin.film.offset=0;
1101   value=GetCINProperty(image_info,image,"cin:film.offset");
1102   if (value != (const char *) NULL)
1103     cin.film.offset=(char) StringToLong(value);
1104   offset+=WriteBlobByte(image,(unsigned char) cin.film.offset);
1105   offset+=WriteBlobByte(image,(unsigned char) cin.film.reserve1);
1106   cin.film.prefix=0UL;
1107   value=GetCINProperty(image_info,image,"cin:film.prefix");
1108   if (value != (const char *) NULL)
1109     cin.film.prefix=StringToUnsignedLong(value);
1110   offset+=WriteBlobLong(image,(unsigned int) cin.film.prefix);
1111   cin.film.count=0UL;
1112   value=GetCINProperty(image_info,image,"cin:film.count");
1113   if (value != (const char *) NULL)
1114     cin.film.count=StringToUnsignedLong(value);
1115   offset+=WriteBlobLong(image,(unsigned int) cin.film.count);
1116   value=GetCINProperty(image_info,image,"cin:film.format");
1117   if (value != (const char *) NULL)
1118     (void) CopyMagickString(cin.film.format,value,sizeof(cin.film.format));
1119   offset+=WriteBlob(image,sizeof(cin.film.format),(unsigned char *)
1120     cin.film.format);
1121   cin.film.frame_position=0UL;
1122   value=GetCINProperty(image_info,image,"cin:film.frame_position");
1123   if (value != (const char *) NULL)
1124     cin.film.frame_position=StringToUnsignedLong(value);
1125   offset+=WriteBlobLong(image,(unsigned int) cin.film.frame_position);
1126   cin.film.frame_rate=0.0f;
1127   value=GetCINProperty(image_info,image,"cin:film.frame_rate");
1128   if (value != (const char *) NULL)
1129     cin.film.frame_rate=StringToDouble(value);
1130   offset+=WriteBlobFloat(image,cin.film.frame_rate);
1131   value=GetCINProperty(image_info,image,"cin:film.frame_id");
1132   if (value != (const char *) NULL)
1133     (void) CopyMagickString(cin.film.frame_id,value,sizeof(cin.film.frame_id));
1134   offset+=WriteBlob(image,sizeof(cin.film.frame_id),(unsigned char *)
1135     cin.film.frame_id);
1136   value=GetCINProperty(image_info,image,"cin:film.slate_info");
1137   if (value != (const char *) NULL)
1138     (void) CopyMagickString(cin.film.slate_info,value,
1139       sizeof(cin.film.slate_info));
1140   offset+=WriteBlob(image,sizeof(cin.film.slate_info),(unsigned char *)
1141     cin.film.slate_info);
1142   offset+=WriteBlob(image,sizeof(cin.film.reserve),(unsigned char *)
1143     cin.film.reserve);
1144   if (profile != (StringInfo *) NULL)
1145     offset+=WriteBlob(image,GetStringInfoLength(profile),
1146       GetStringInfoDatum(profile));
1147   while (offset < (MagickOffsetType) cin.file.image_offset)
1148     offset+=WriteBlobByte(image,0x00);
1149   /*
1150     Convert pixel packets to CIN raster image.
1151   */
1152   quantum_info=AcquireQuantumInfo(image_info,image);
1153   if (quantum_info == (QuantumInfo *) NULL)
1154     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1155   quantum_info->quantum=32;
1156   quantum_info->pack=MagickFalse;
1157   quantum_type=RGBQuantum;
1158   pixels=GetQuantumPixels(quantum_info);
1159   length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
1160   if (0)
1161     {
1162       quantum_type=GrayQuantum;
1163       length=GetBytesPerRow(image->columns,3,image->depth,MagickTrue);
1164     }
1165   for (y=0; y < (ssize_t) image->rows; y++)
1166   {
1167     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1168     if (p == (const PixelPacket *) NULL)
1169       break;
1170     (void) ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
1171       quantum_type,pixels,&image->exception);
1172     count=WriteBlob(image,length,pixels);
1173     if (count != (ssize_t) length)
1174       break;
1175     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1176                 image->rows);
1177     if (status == MagickFalse)
1178       break;
1179   }
1180   quantum_info=DestroyQuantumInfo(quantum_info);
1181   (void) CloseBlob(image);
1182   return(status);
1183 }