2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Raw CCIR 601 4:1:1 or 4:2:2 Image Format %
20 % Copyright 1999-2014 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 *,ExceptionInfo *);
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,exception);
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 - 1) /
204 horizontal_factor, (image->rows + vertical_factor - 1) / vertical_factor,
205 MagickTrue,exception);
206 if (chroma_image == (Image *) NULL)
207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
209 Convert raster image to pixel packets.
211 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
212 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
214 if (interlace == PartitionInterlace)
216 AppendImageFormat("Y",image->filename);
217 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
218 if (status == MagickFalse)
220 image=DestroyImageList(image);
221 return((Image *) NULL);
224 for (y=0; y < (ssize_t) image->rows; y++)
229 if (interlace == NoInterlace)
231 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
232 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
234 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
235 if (q == (Quantum *) NULL)
237 chroma_pixels=QueueAuthenticPixels(chroma_image,0,y,
238 chroma_image->columns,1,exception);
239 if (chroma_pixels == (Quantum *) NULL)
241 for (x=0; x < (ssize_t) image->columns; x+=2)
243 SetPixelRed(image,0,chroma_pixels);
245 SetPixelGreen(image,ScaleCharToQuantum(*p++),chroma_pixels);
248 SetPixelGreen(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
253 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
256 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
259 SetPixelGreen(image,0,q);
260 SetPixelBlue(image,0,q);
261 q+=GetPixelChannels(image);
262 SetPixelGreen(image,0,q);
263 SetPixelBlue(image,0,q);
265 SetPixelBlue(image,ScaleCharToQuantum(*p++),chroma_pixels);
268 SetPixelBlue(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
273 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
276 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
280 q+=GetPixelChannels(image);
285 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
286 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
288 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
289 if (q == (Quantum *) NULL)
291 for (x=0; x < (ssize_t) image->columns; x++)
294 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
297 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
300 SetPixelGreen(image,0,q);
301 SetPixelBlue(image,0,q);
302 q+=GetPixelChannels(image);
305 if (SyncAuthenticPixels(image,exception) == MagickFalse)
307 if (interlace == NoInterlace)
308 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
310 if (image->previous == (Image *) NULL)
312 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
314 if (status == MagickFalse)
318 if (interlace == PartitionInterlace)
320 (void) CloseBlob(image);
321 AppendImageFormat("U",image->filename);
322 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
323 if (status == MagickFalse)
325 image=DestroyImageList(image);
326 return((Image *) NULL);
329 if (interlace != NoInterlace)
331 for (y=0; y < (ssize_t) chroma_image->rows; y++)
333 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
335 q=QueueAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
337 if (q == (Quantum *) NULL)
339 for (x=0; x < (ssize_t) chroma_image->columns; x++)
341 SetPixelRed(chroma_image,0,q);
343 SetPixelGreen(chroma_image,ScaleCharToQuantum(*p++),q);
346 SetPixelGreen(chroma_image,ScaleShortToQuantum(((*p) << 8) |
350 SetPixelBlue(chroma_image,0,q);
351 q+=GetPixelChannels(chroma_image);
353 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
356 if (interlace == PartitionInterlace)
358 (void) CloseBlob(image);
359 AppendImageFormat("V",image->filename);
360 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
361 if (status == MagickFalse)
363 image=DestroyImageList(image);
364 return((Image *) NULL);
367 for (y=0; y < (ssize_t) chroma_image->rows; y++)
369 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
371 q=GetAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
373 if (q == (Quantum *) NULL)
375 for (x=0; x < (ssize_t) chroma_image->columns; x++)
378 SetPixelBlue(chroma_image,ScaleCharToQuantum(*p++),q);
381 SetPixelBlue(chroma_image,ScaleShortToQuantum(((*p) << 8) |
385 q+=GetPixelChannels(chroma_image);
387 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
394 resize_image=ResizeImage(chroma_image,image->columns,image->rows,
395 TriangleFilter,exception);
396 chroma_image=DestroyImage(chroma_image);
397 if (resize_image == (Image *) NULL)
398 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
399 for (y=0; y < (ssize_t) image->rows; y++)
401 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
402 chroma_pixels=GetVirtualPixels(resize_image,0,y,resize_image->columns,1,
404 if ((q == (Quantum *) NULL) ||
405 (chroma_pixels == (const Quantum *) NULL))
407 for (x=0; x < (ssize_t) image->columns; x++)
409 SetPixelGreen(image,GetPixelGreen(image,chroma_pixels),q);
410 SetPixelBlue(image,GetPixelBlue(image,chroma_pixels),q);
412 q+=GetPixelChannels(image);
414 if (SyncAuthenticPixels(image,exception) == MagickFalse)
417 resize_image=DestroyImage(resize_image);
418 SetImageColorspace(image,YCbCrColorspace,exception);
419 if (interlace == PartitionInterlace)
420 (void) CopyMagickString(image->filename,image_info->filename,
422 if (EOFBlob(image) != MagickFalse)
424 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
429 Proceed to next image.
431 if (image_info->number_scenes != 0)
432 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
434 if (interlace == NoInterlace)
435 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
437 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
441 Allocate next image structure.
443 AcquireNextImage(image_info,image,exception);
444 if (GetNextImageInList(image) == (Image *) NULL)
446 image=DestroyImageList(image);
447 return((Image *) NULL);
449 image=SyncNextImageInList(image);
450 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
452 if (status == MagickFalse)
455 } while (count != 0);
456 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
457 (void) CloseBlob(image);
458 return(GetFirstImageInList(image));
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % R e g i s t e r Y U V I m a g e %
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % RegisterYUVImage() adds attributes for the YUV image format to
473 % the list of supported formats. The attributes include the image format
474 % tag, a method to read and/or write the format, whether the format
475 % supports the saving of more than one frame to the same file or blob,
476 % whether the format supports native in-memory I/O, and a brief
477 % description of the format.
479 % The format of the RegisterYUVImage method is:
481 % size_t RegisterYUVImage(void)
484 ModuleExport size_t RegisterYUVImage(void)
489 entry=SetMagickInfo("YUV");
490 entry->decoder=(DecodeImageHandler *) ReadYUVImage;
491 entry->encoder=(EncodeImageHandler *) WriteYUVImage;
492 entry->adjoin=MagickFalse;
493 entry->raw=MagickTrue;
494 entry->description=ConstantString("CCIR 601 4:1:1 or 4:2:2");
495 entry->module=ConstantString("YUV");
496 (void) RegisterMagickInfo(entry);
497 return(MagickImageCoderSignature);
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % U n r e g i s t e r Y U V I m a g e %
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511 % UnregisterYUVImage() removes format registrations made by the
512 % YUV module from the list of supported formats.
514 % The format of the UnregisterYUVImage method is:
516 % UnregisterYUVImage(void)
519 ModuleExport void UnregisterYUVImage(void)
521 (void) UnregisterMagickInfo("YUV");
525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % W r i t e Y U V I m a g e %
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % WriteYUVImage() writes an image to a file in the digital YUV
536 % (CCIR 601 4:1:1, plane or partition interlaced, or 4:2:2 plane, partition
537 % interlaced or noninterlaced) bytes and returns it.
539 % The format of the WriteYUVImage method is:
541 % MagickBooleanType WriteYUVImage(const ImageInfo *image_info,
542 % Image *image,ExceptionInfo *exception)
544 % A description of each parameter follows.
546 % o image_info: the image info.
548 % o image: The image.
550 % o exception: return any errors or warnings in this structure.
553 static MagickBooleanType WriteYUVImage(const ImageInfo *image_info,Image *image,
554 ExceptionInfo *exception)
569 register const Quantum
586 assert(image_info != (const ImageInfo *) NULL);
587 assert(image_info->signature == MagickSignature);
588 assert(image != (Image *) NULL);
589 assert(image->signature == MagickSignature);
590 if (image->debug != MagickFalse)
591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
592 quantum=(size_t) (image->depth <= 8 ? 1 : 2);
593 interlace=image->interlace;
596 if (image_info->sampling_factor != (char *) NULL)
604 flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
605 horizontal_factor=(ssize_t) geometry_info.rho;
606 vertical_factor=(ssize_t) geometry_info.sigma;
607 if ((flags & SigmaValue) == 0)
608 vertical_factor=horizontal_factor;
609 if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
610 (vertical_factor != 1) && (vertical_factor != 2))
611 ThrowWriterException(CorruptImageError,"UnexpectedSamplingFactor");
613 if ((interlace == UndefinedInterlace) ||
614 ((interlace == NoInterlace) && (vertical_factor == 2)))
616 interlace=NoInterlace; /* CCIR 4:2:2 */
617 if (vertical_factor == 2)
618 interlace=PlaneInterlace; /* CCIR 4:1:1 */
620 if (interlace != PartitionInterlace)
623 Open output image file.
625 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
626 if (status == MagickFalse)
631 AppendImageFormat("Y",image->filename);
632 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
633 if (status == MagickFalse)
640 Sample image to an even width and height, if necessary.
642 image->depth=(size_t) (quantum == 1 ? 8 : 16);
643 width=image->columns+(image->columns & (horizontal_factor-1));
644 height=image->rows+(image->rows & (vertical_factor-1));
645 yuv_image=ResizeImage(image,width,height,TriangleFilter,exception);
646 if (yuv_image == (Image *) NULL)
648 (void) CloseBlob(image);
651 (void) TransformImageColorspace(yuv_image,YCbCrColorspace,exception);
655 chroma_image=ResizeImage(image,width/horizontal_factor,
656 height/vertical_factor,TriangleFilter,exception);
657 if (chroma_image == (Image *) NULL)
659 (void) CloseBlob(image);
662 (void) TransformImageColorspace(chroma_image,YCbCrColorspace,exception);
663 if (interlace == NoInterlace)
666 Write noninterlaced YUV.
668 for (y=0; y < (ssize_t) yuv_image->rows; y++)
670 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,exception);
671 if (p == (const Quantum *) NULL)
673 s=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
675 if (s == (const Quantum *) NULL)
677 for (x=0; x < (ssize_t) yuv_image->columns; x++)
681 (void) WriteBlobByte(image,ScaleQuantumToChar(
682 GetPixelGreen(yuv_image,s)));
683 (void) WriteBlobByte(image,ScaleQuantumToChar(
684 GetPixelRed(yuv_image,p)));
685 p+=GetPixelChannels(yuv_image);
686 (void) WriteBlobByte(image,ScaleQuantumToChar(
687 GetPixelBlue(yuv_image,s)));
688 (void) WriteBlobByte(image,ScaleQuantumToChar(
689 GetPixelRed(yuv_image,p)));
693 (void) WriteBlobByte(image,ScaleQuantumToChar(
694 GetPixelGreen(yuv_image,s)));
695 (void) WriteBlobShort(image,ScaleQuantumToShort(
696 GetPixelRed(yuv_image,p)));
697 p+=GetPixelChannels(yuv_image);
698 (void) WriteBlobByte(image,ScaleQuantumToChar(
699 GetPixelBlue(yuv_image,s)));
700 (void) WriteBlobShort(image,ScaleQuantumToShort(
701 GetPixelRed(yuv_image,p)));
703 p+=GetPixelChannels(yuv_image);
707 if (image->previous == (Image *) NULL)
709 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
711 if (status == MagickFalse)
715 yuv_image=DestroyImage(yuv_image);
720 Initialize Y channel.
722 for (y=0; y < (ssize_t) yuv_image->rows; y++)
724 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,exception);
725 if (p == (const Quantum *) NULL)
727 for (x=0; x < (ssize_t) yuv_image->columns; x++)
730 (void) WriteBlobByte(image,ScaleQuantumToChar(
731 GetPixelRed(yuv_image,p)));
733 (void) WriteBlobShort(image,ScaleQuantumToShort(
734 GetPixelRed(yuv_image,p)));
735 p+=GetPixelChannels(yuv_image);
737 if (image->previous == (Image *) NULL)
739 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
741 if (status == MagickFalse)
745 yuv_image=DestroyImage(yuv_image);
746 if (image->previous == (Image *) NULL)
748 status=SetImageProgress(image,SaveImageTag,1,3);
749 if (status == MagickFalse)
753 Initialize U channel.
755 if (interlace == PartitionInterlace)
757 (void) CloseBlob(image);
758 AppendImageFormat("U",image->filename);
759 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
760 if (status == MagickFalse)
763 for (y=0; y < (ssize_t) chroma_image->rows; y++)
765 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
767 if (p == (const Quantum *) NULL)
769 for (x=0; x < (ssize_t) chroma_image->columns; x++)
772 (void) WriteBlobByte(image,ScaleQuantumToChar(
773 GetPixelGreen(chroma_image,p)));
775 (void) WriteBlobShort(image,ScaleQuantumToShort(
776 GetPixelGreen(chroma_image,p)));
777 p+=GetPixelChannels(chroma_image);
780 if (image->previous == (Image *) NULL)
782 status=SetImageProgress(image,SaveImageTag,2,3);
783 if (status == MagickFalse)
787 Initialize V channel.
789 if (interlace == PartitionInterlace)
791 (void) CloseBlob(image);
792 AppendImageFormat("V",image->filename);
793 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
794 if (status == MagickFalse)
797 for (y=0; y < (ssize_t) chroma_image->rows; y++)
799 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
801 if (p == (const Quantum *) NULL)
803 for (x=0; x < (ssize_t) chroma_image->columns; x++)
806 (void) WriteBlobByte(image,ScaleQuantumToChar(
807 GetPixelBlue(chroma_image,p)));
809 (void) WriteBlobShort(image,ScaleQuantumToShort(
810 GetPixelBlue(chroma_image,p)));
811 p+=GetPixelChannels(chroma_image);
814 if (image->previous == (Image *) NULL)
816 status=SetImageProgress(image,SaveImageTag,2,3);
817 if (status == MagickFalse)
821 chroma_image=DestroyImage(chroma_image);
822 if (interlace == PartitionInterlace)
823 (void) CopyMagickString(image->filename,image_info->filename,
825 if (GetNextImageInList(image) == (Image *) NULL)
827 image=SyncNextImageInList(image);
828 status=SetImageProgress(image,SaveImagesTag,scene++,
829 GetImageListLength(image));
830 if (status == MagickFalse)
832 } while (image_info->adjoin != MagickFalse);
833 (void) CloseBlob(image);