2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Raw CCIR 601 4:1:1 or 4:2:2 Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/constitute.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/geometry.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/resize.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/utility.h"
69 static MagickBooleanType
70 WriteYUVImage(const ImageInfo *,Image *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d Y U V I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % ReadYUVImage() reads an image with digital YUV (CCIR 601 4:1:1, plane
84 % or partition interlaced, or 4:2:2 plane, partition interlaced or
85 % noninterlaced) bytes and returns it. It allocates the memory necessary
86 % for the new Image structure and returns a pointer to the new image.
88 % The format of the ReadYUVImage method is:
90 % Image *ReadYUVImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadYUVImage(const ImageInfo *image_info,ExceptionInfo *exception)
112 register const Quantum
121 register unsigned char
135 Allocate image structure.
137 assert(image_info != (const ImageInfo *) NULL);
138 assert(image_info->signature == MagickSignature);
139 if (image_info->debug != MagickFalse)
140 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
141 image_info->filename);
142 assert(exception != (ExceptionInfo *) NULL);
143 assert(exception->signature == MagickSignature);
144 image=AcquireImage(image_info);
145 if ((image->columns == 0) || (image->rows == 0))
146 ThrowReaderException(OptionError,"MustSpecifyImageSize");
147 quantum=image->depth <= 8 ? 1 : 2;
148 interlace=image_info->interlace;
151 if (image_info->sampling_factor != (char *) NULL)
159 flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
160 horizontal_factor=(ssize_t) geometry_info.rho;
161 vertical_factor=(ssize_t) geometry_info.sigma;
162 if ((flags & SigmaValue) == 0)
163 vertical_factor=horizontal_factor;
164 if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
165 (vertical_factor != 1) && (vertical_factor != 2))
166 ThrowReaderException(CorruptImageError,"UnexpectedSamplingFactor");
168 if ((interlace == UndefinedInterlace) ||
169 ((interlace == NoInterlace) && (vertical_factor == 2)))
171 interlace=NoInterlace; /* CCIR 4:2:2 */
172 if (vertical_factor == 2)
173 interlace=PlaneInterlace; /* CCIR 4:1:1 */
175 if (interlace != PartitionInterlace)
180 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
181 if (status == MagickFalse)
183 image=DestroyImageList(image);
184 return((Image *) NULL);
186 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
187 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
191 Allocate memory for a scanline.
193 if (interlace == NoInterlace)
194 scanline=(unsigned char *) AcquireQuantumMemory((size_t) 2UL*
195 image->columns+2UL,quantum*sizeof(*scanline));
197 scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
198 quantum*sizeof(*scanline));
199 if (scanline == (unsigned char *) NULL)
200 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
203 chroma_image=CloneImage(image,image->columns/horizontal_factor,
204 image->rows/vertical_factor,MagickTrue,exception);
205 if (chroma_image == (Image *) NULL)
206 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
208 Convert raster image to pixel packets.
210 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
211 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
213 if (interlace == PartitionInterlace)
215 AppendImageFormat("Y",image->filename);
216 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
217 if (status == MagickFalse)
219 image=DestroyImageList(image);
220 return((Image *) NULL);
223 for (y=0; y < (ssize_t) image->rows; y++)
228 if (interlace == NoInterlace)
230 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
231 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
233 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
234 if (q == (const Quantum *) NULL)
236 chroma_pixels=QueueAuthenticPixels(chroma_image,0,y,
237 chroma_image->columns,1,exception);
238 if (chroma_pixels == (Quantum *) NULL)
240 for (x=0; x < (ssize_t) image->columns; x+=2)
242 SetPixelRed(image,0,chroma_pixels);
244 SetPixelGreen(image,ScaleCharToQuantum(*p++),chroma_pixels);
247 SetPixelGreen(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
252 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
255 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
258 SetPixelGreen(image,0,q);
259 SetPixelBlue(image,0,q);
260 q+=GetPixelChannels(image);
261 SetPixelGreen(image,0,q);
262 SetPixelBlue(image,0,q);
264 SetPixelBlue(image,ScaleCharToQuantum(*p++),chroma_pixels);
267 SetPixelBlue(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
272 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
275 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
279 q+=GetPixelChannels(image);
284 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
285 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
287 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
288 if (q == (const Quantum *) NULL)
290 for (x=0; x < (ssize_t) image->columns; x++)
293 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
296 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
299 SetPixelGreen(image,0,q);
300 SetPixelBlue(image,0,q);
301 q+=GetPixelChannels(image);
304 if (SyncAuthenticPixels(image,exception) == MagickFalse)
306 if (interlace == NoInterlace)
307 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
309 if (image->previous == (Image *) NULL)
311 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
313 if (status == MagickFalse)
317 if (interlace == PartitionInterlace)
319 (void) CloseBlob(image);
320 AppendImageFormat("U",image->filename);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
324 image=DestroyImageList(image);
325 return((Image *) NULL);
328 if (interlace != NoInterlace)
330 for (y=0; y < (ssize_t) chroma_image->rows; y++)
332 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
334 q=QueueAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
336 if (q == (const Quantum *) NULL)
338 for (x=0; x < (ssize_t) chroma_image->columns; x++)
340 SetPixelRed(chroma_image,0,q);
342 SetPixelGreen(chroma_image,ScaleCharToQuantum(*p++),q);
345 SetPixelGreen(chroma_image,ScaleShortToQuantum(((*p) << 8) |
349 SetPixelBlue(chroma_image,0,q);
350 q+=GetPixelChannels(chroma_image);
352 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
355 if (interlace == PartitionInterlace)
357 (void) CloseBlob(image);
358 AppendImageFormat("V",image->filename);
359 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
360 if (status == MagickFalse)
362 image=DestroyImageList(image);
363 return((Image *) NULL);
366 for (y=0; y < (ssize_t) chroma_image->rows; y++)
368 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
370 q=GetAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
372 if (q == (const Quantum *) NULL)
374 for (x=0; x < (ssize_t) chroma_image->columns; x++)
377 SetPixelBlue(chroma_image,ScaleCharToQuantum(*p++),q);
380 SetPixelBlue(chroma_image,ScaleShortToQuantum(((*p) << 8) |
384 q+=GetPixelChannels(chroma_image);
386 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
393 resize_image=ResizeImage(chroma_image,image->columns,image->rows,
394 TriangleFilter,1.0,exception);
395 chroma_image=DestroyImage(chroma_image);
396 if (resize_image == (Image *) NULL)
397 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
398 for (y=0; y < (ssize_t) image->rows; y++)
400 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
401 chroma_pixels=GetVirtualPixels(resize_image,0,y,resize_image->columns,1,
402 &resize_image->exception);
403 if ((q == (const Quantum *) NULL) ||
404 (chroma_pixels == (const Quantum *) NULL))
406 for (x=0; x < (ssize_t) image->columns; x++)
408 SetPixelGreen(image,GetPixelGreen(image,chroma_pixels),q);
409 SetPixelBlue(image,GetPixelBlue(image,chroma_pixels),q);
411 q+=GetPixelChannels(image);
413 if (SyncAuthenticPixels(image,exception) == MagickFalse)
416 resize_image=DestroyImage(resize_image);
417 image->colorspace=YCbCrColorspace;
418 if (interlace == PartitionInterlace)
419 (void) CopyMagickString(image->filename,image_info->filename,
421 if (EOFBlob(image) != MagickFalse)
423 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
428 Proceed to next image.
430 if (image_info->number_scenes != 0)
431 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
433 if (interlace == NoInterlace)
434 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
436 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
440 Allocate next image structure.
442 AcquireNextImage(image_info,image);
443 if (GetNextImageInList(image) == (Image *) NULL)
445 image=DestroyImageList(image);
446 return((Image *) NULL);
448 image=SyncNextImageInList(image);
449 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
451 if (status == MagickFalse)
454 } while (count != 0);
455 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
456 (void) CloseBlob(image);
457 return(GetFirstImageInList(image));
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 % R e g i s t e r Y U V I m a g e %
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 % RegisterYUVImage() adds attributes for the YUV image format to
472 % the list of supported formats. The attributes include the image format
473 % tag, a method to read and/or write the format, whether the format
474 % supports the saving of more than one frame to the same file or blob,
475 % whether the format supports native in-memory I/O, and a brief
476 % description of the format.
478 % The format of the RegisterYUVImage method is:
480 % size_t RegisterYUVImage(void)
483 ModuleExport size_t RegisterYUVImage(void)
488 entry=SetMagickInfo("YUV");
489 entry->decoder=(DecodeImageHandler *) ReadYUVImage;
490 entry->encoder=(EncodeImageHandler *) WriteYUVImage;
491 entry->adjoin=MagickFalse;
492 entry->raw=MagickTrue;
493 entry->description=ConstantString("CCIR 601 4:1:1 or 4:2:2");
494 entry->module=ConstantString("YUV");
495 (void) RegisterMagickInfo(entry);
496 return(MagickImageCoderSignature);
500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % U n r e g i s t e r Y U V I m a g e %
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510 % UnregisterYUVImage() removes format registrations made by the
511 % YUV module from the list of supported formats.
513 % The format of the UnregisterYUVImage method is:
515 % UnregisterYUVImage(void)
518 ModuleExport void UnregisterYUVImage(void)
520 (void) UnregisterMagickInfo("YUV");
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % W r i t e Y U V I m a g e %
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % WriteYUVImage() writes an image to a file in the digital YUV
535 % (CCIR 601 4:1:1, plane or partition interlaced, or 4:2:2 plane, partition
536 % interlaced or noninterlaced) bytes and returns it.
538 % The format of the WriteYUVImage method is:
540 % MagickBooleanType WriteYUVImage(const ImageInfo *image_info,Image *image)
542 % A description of each parameter follows.
544 % o image_info: the image info.
546 % o image: The image.
549 static MagickBooleanType WriteYUVImage(const ImageInfo *image_info,Image *image)
564 register const Quantum
581 assert(image_info != (const ImageInfo *) NULL);
582 assert(image_info->signature == MagickSignature);
583 assert(image != (Image *) NULL);
584 assert(image->signature == MagickSignature);
585 if (image->debug != MagickFalse)
586 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
587 quantum=image->depth <= 8 ? 1 : 2;
588 interlace=image->interlace;
591 if (image_info->sampling_factor != (char *) NULL)
599 flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
600 horizontal_factor=(ssize_t) geometry_info.rho;
601 vertical_factor=(ssize_t) geometry_info.sigma;
602 if ((flags & SigmaValue) == 0)
603 vertical_factor=horizontal_factor;
604 if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
605 (vertical_factor != 1) && (vertical_factor != 2))
606 ThrowWriterException(CorruptImageError,"UnexpectedSamplingFactor");
608 if ((interlace == UndefinedInterlace) ||
609 ((interlace == NoInterlace) && (vertical_factor == 2)))
611 interlace=NoInterlace; /* CCIR 4:2:2 */
612 if (vertical_factor == 2)
613 interlace=PlaneInterlace; /* CCIR 4:1:1 */
615 if (interlace != PartitionInterlace)
618 Open output image file.
620 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
621 if (status == MagickFalse)
626 AppendImageFormat("Y",image->filename);
627 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
628 if (status == MagickFalse)
635 Sample image to an even width and height, if necessary.
637 image->depth=quantum == 1 ? 8 : 16;
638 width=image->columns+(image->columns & (horizontal_factor-1));
639 height=image->rows+(image->rows & (vertical_factor-1));
640 yuv_image=ResizeImage(image,width,height,TriangleFilter,1.0,
642 if (yuv_image == (Image *) NULL)
643 ThrowWriterException(ResourceLimitError,image->exception.reason);
644 (void) TransformImageColorspace(yuv_image,YCbCrColorspace);
648 chroma_image=ResizeImage(image,width/horizontal_factor,
649 height/vertical_factor,TriangleFilter,1.0,&image->exception);
650 if (chroma_image == (Image *) NULL)
651 ThrowWriterException(ResourceLimitError,image->exception.reason);
652 (void) TransformImageColorspace(chroma_image,YCbCrColorspace);
653 if (interlace == NoInterlace)
656 Write noninterlaced YUV.
658 for (y=0; y < (ssize_t) yuv_image->rows; y++)
660 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,
661 &yuv_image->exception);
662 if (p == (const Quantum *) NULL)
664 s=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
665 &chroma_image->exception);
666 if (s == (const Quantum *) NULL)
668 for (x=0; x < (ssize_t) yuv_image->columns; x++)
672 (void) WriteBlobByte(image,ScaleQuantumToChar(
673 GetPixelGreen(yuv_image,s)));
674 (void) WriteBlobByte(image,ScaleQuantumToChar(
675 GetPixelRed(yuv_image,p)));
676 p+=GetPixelChannels(yuv_image);
677 (void) WriteBlobByte(image,ScaleQuantumToChar(
678 GetPixelBlue(yuv_image,s)));
679 (void) WriteBlobByte(image,ScaleQuantumToChar(
680 GetPixelRed(yuv_image,p)));
684 (void) WriteBlobByte(image,ScaleQuantumToChar(
685 GetPixelGreen(yuv_image,s)));
686 (void) WriteBlobShort(image,ScaleQuantumToShort(
687 GetPixelRed(yuv_image,p)));
688 p+=GetPixelChannels(yuv_image);
689 (void) WriteBlobByte(image,ScaleQuantumToChar(
690 GetPixelBlue(yuv_image,s)));
691 (void) WriteBlobShort(image,ScaleQuantumToShort(
692 GetPixelRed(yuv_image,p)));
694 p+=GetPixelChannels(yuv_image);
698 if (image->previous == (Image *) NULL)
700 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
702 if (status == MagickFalse)
706 yuv_image=DestroyImage(yuv_image);
711 Initialize Y channel.
713 for (y=0; y < (ssize_t) yuv_image->rows; y++)
715 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,
716 &yuv_image->exception);
717 if (p == (const Quantum *) NULL)
719 for (x=0; x < (ssize_t) yuv_image->columns; x++)
722 (void) WriteBlobByte(image,ScaleQuantumToChar(
723 GetPixelRed(yuv_image,p)));
725 (void) WriteBlobShort(image,ScaleQuantumToShort(
726 GetPixelRed(yuv_image,p)));
727 p+=GetPixelChannels(yuv_image);
729 if (image->previous == (Image *) NULL)
731 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
733 if (status == MagickFalse)
737 yuv_image=DestroyImage(yuv_image);
738 if (image->previous == (Image *) NULL)
740 status=SetImageProgress(image,SaveImageTag,1,3);
741 if (status == MagickFalse)
745 Initialize U channel.
747 if (interlace == PartitionInterlace)
749 (void) CloseBlob(image);
750 AppendImageFormat("U",image->filename);
751 status=OpenBlob(image_info,image,WriteBinaryBlobMode,
753 if (status == MagickFalse)
756 for (y=0; y < (ssize_t) chroma_image->rows; y++)
758 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
759 &chroma_image->exception);
760 if (p == (const Quantum *) NULL)
762 for (x=0; x < (ssize_t) chroma_image->columns; x++)
765 (void) WriteBlobByte(image,ScaleQuantumToChar(
766 GetPixelGreen(chroma_image,p)));
768 (void) WriteBlobShort(image,ScaleQuantumToShort(
769 GetPixelGreen(chroma_image,p)));
770 p+=GetPixelChannels(chroma_image);
773 if (image->previous == (Image *) NULL)
775 status=SetImageProgress(image,SaveImageTag,2,3);
776 if (status == MagickFalse)
780 Initialize V channel.
782 if (interlace == PartitionInterlace)
784 (void) CloseBlob(image);
785 AppendImageFormat("V",image->filename);
786 status=OpenBlob(image_info,image,WriteBinaryBlobMode,
788 if (status == MagickFalse)
791 for (y=0; y < (ssize_t) chroma_image->rows; y++)
793 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
794 &chroma_image->exception);
795 if (p == (const Quantum *) NULL)
797 for (x=0; x < (ssize_t) chroma_image->columns; x++)
800 (void) WriteBlobByte(image,ScaleQuantumToChar(
801 GetPixelBlue(chroma_image,p)));
803 (void) WriteBlobShort(image,ScaleQuantumToShort(
804 GetPixelBlue(chroma_image,p)));
805 p+=GetPixelChannels(chroma_image);
808 if (image->previous == (Image *) NULL)
810 status=SetImageProgress(image,SaveImageTag,2,3);
811 if (status == MagickFalse)
815 chroma_image=DestroyImage(chroma_image);
816 if (interlace == PartitionInterlace)
817 (void) CopyMagickString(image->filename,image_info->filename,
819 if (GetNextImageInList(image) == (Image *) NULL)
821 image=SyncNextImageInList(image);
822 status=SetImageProgress(image,SaveImagesTag,scene++,
823 GetImageListLength(image));
824 if (status == MagickFalse)
826 } while (image_info->adjoin != MagickFalse);
827 (void) CloseBlob(image);