blob: 599c24e05adab2a2b5ad327abd676369aab2f06c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% Y Y U U V V %
7% Y Y U U V V %
8% Y U U V V %
9% Y U U V V %
10% Y UUU V %
11% %
12% %
13% Read/Write Raw CCIR 601 4:1:1 or 4:2:2 Image Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67 Forward declarations.
68*/
69static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000070 WriteYUVImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74% %
75% %
76% %
77% R e a d Y U V I m a g e %
78% %
79% %
80% %
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82%
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.
87%
88% The format of the ReadYUVImage method is:
89%
90% Image *ReadYUVImage(const ImageInfo *image_info,ExceptionInfo *exception)
91%
92% A description of each parameter follows:
93%
94% o image_info: the image info.
95%
96% o exception: return any errors or warnings in this structure.
97%
98*/
99static Image *ReadYUVImage(const ImageInfo *image_info,ExceptionInfo *exception)
100{
101 Image
102 *chroma_image,
103 *image,
104 *resize_image;
105
106 InterlaceType
107 interlace;
108
cristy3ed852e2009-09-05 21:47:34 +0000109 MagickBooleanType
110 status;
111
cristy4c08aed2011-07-01 19:47:50 +0000112 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000113 *chroma_pixels;
114
cristybb503372010-05-27 20:51:26 +0000115 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000116 x;
117
cristy4c08aed2011-07-01 19:47:50 +0000118 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000119 *q;
120
cristy3ed852e2009-09-05 21:47:34 +0000121 register unsigned char
122 *p;
123
124 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000125 count,
126 horizontal_factor,
127 quantum,
128 vertical_factor,
129 y;
cristy3ed852e2009-09-05 21:47:34 +0000130
131 unsigned char
132 *scanline;
133
134 /*
135 Allocate image structure.
136 */
137 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000138 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000139 if (image_info->debug != MagickFalse)
140 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
141 image_info->filename);
142 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000143 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000144 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000145 if ((image->columns == 0) || (image->rows == 0))
146 ThrowReaderException(OptionError,"MustSpecifyImageSize");
cristyacabb842014-12-14 23:36:33 +0000147 status=SetImageExtent(image,image->columns,image->rows,exception);
148 if (status == MagickFalse)
149 return(DestroyImageList(image));
cristy292ebf12011-02-08 20:21:31 +0000150 quantum=image->depth <= 8 ? 1 : 2;
cristy3ed852e2009-09-05 21:47:34 +0000151 interlace=image_info->interlace;
152 horizontal_factor=2;
153 vertical_factor=2;
154 if (image_info->sampling_factor != (char *) NULL)
155 {
156 GeometryInfo
157 geometry_info;
158
159 MagickStatusType
160 flags;
161
162 flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
cristybb503372010-05-27 20:51:26 +0000163 horizontal_factor=(ssize_t) geometry_info.rho;
164 vertical_factor=(ssize_t) geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000165 if ((flags & SigmaValue) == 0)
166 vertical_factor=horizontal_factor;
167 if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
168 (vertical_factor != 1) && (vertical_factor != 2))
169 ThrowReaderException(CorruptImageError,"UnexpectedSamplingFactor");
170 }
171 if ((interlace == UndefinedInterlace) ||
172 ((interlace == NoInterlace) && (vertical_factor == 2)))
173 {
174 interlace=NoInterlace; /* CCIR 4:2:2 */
175 if (vertical_factor == 2)
176 interlace=PlaneInterlace; /* CCIR 4:1:1 */
177 }
178 if (interlace != PartitionInterlace)
179 {
180 /*
181 Open image file.
182 */
183 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
184 if (status == MagickFalse)
185 {
186 image=DestroyImageList(image);
187 return((Image *) NULL);
188 }
cristyd4297022010-09-16 22:59:09 +0000189 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
190 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
191 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000192 }
193 /*
194 Allocate memory for a scanline.
195 */
196 if (interlace == NoInterlace)
197 scanline=(unsigned char *) AcquireQuantumMemory((size_t) 2UL*
cristy292ebf12011-02-08 20:21:31 +0000198 image->columns+2UL,quantum*sizeof(*scanline));
cristy3ed852e2009-09-05 21:47:34 +0000199 else
cristybc4a1d42014-12-15 21:47:00 +0000200 scanline=(unsigned char *) AcquireQuantumMemory(image->columns,
cristy292ebf12011-02-08 20:21:31 +0000201 quantum*sizeof(*scanline));
cristy3ed852e2009-09-05 21:47:34 +0000202 if (scanline == (unsigned char *) NULL)
203 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
204 do
205 {
dirkaf44efc2013-09-27 21:51:30 +0000206 chroma_image=CloneImage(image,(image->columns + horizontal_factor - 1) /
207 horizontal_factor, (image->rows + vertical_factor - 1) / vertical_factor,
208 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000209 if (chroma_image == (Image *) NULL)
210 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
211 /*
212 Convert raster image to pixel packets.
213 */
214 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
215 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
216 break;
cristyacabb842014-12-14 23:36:33 +0000217 status=SetImageExtent(image,image->columns,image->rows,exception);
218 if (status == MagickFalse)
219 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000220 if (interlace == PartitionInterlace)
221 {
222 AppendImageFormat("Y",image->filename);
223 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
224 if (status == MagickFalse)
225 {
226 image=DestroyImageList(image);
227 return((Image *) NULL);
228 }
229 }
cristybb503372010-05-27 20:51:26 +0000230 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000231 {
cristy4c08aed2011-07-01 19:47:50 +0000232 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000233 *chroma_pixels;
234
235 if (interlace == NoInterlace)
236 {
237 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
cristy292ebf12011-02-08 20:21:31 +0000238 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
cristy3ed852e2009-09-05 21:47:34 +0000239 p=scanline;
240 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000241 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000242 break;
243 chroma_pixels=QueueAuthenticPixels(chroma_image,0,y,
244 chroma_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000245 if (chroma_pixels == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
cristybb503372010-05-27 20:51:26 +0000247 for (x=0; x < (ssize_t) image->columns; x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000248 {
cristy4c08aed2011-07-01 19:47:50 +0000249 SetPixelRed(image,0,chroma_pixels);
cristy292ebf12011-02-08 20:21:31 +0000250 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000251 SetPixelGreen(image,ScaleCharToQuantum(*p++),chroma_pixels);
cristy292ebf12011-02-08 20:21:31 +0000252 else
253 {
cristy4c08aed2011-07-01 19:47:50 +0000254 SetPixelGreen(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
255 chroma_pixels);
cristy292ebf12011-02-08 20:21:31 +0000256 p+=2;
257 }
258 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000259 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy292ebf12011-02-08 20:21:31 +0000260 else
261 {
cristy4c08aed2011-07-01 19:47:50 +0000262 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
cristy292ebf12011-02-08 20:21:31 +0000263 p+=2;
264 }
cristy4c08aed2011-07-01 19:47:50 +0000265 SetPixelGreen(image,0,q);
266 SetPixelBlue(image,0,q);
cristyed231572011-07-14 02:18:59 +0000267 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000268 SetPixelGreen(image,0,q);
269 SetPixelBlue(image,0,q);
cristy292ebf12011-02-08 20:21:31 +0000270 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000271 SetPixelBlue(image,ScaleCharToQuantum(*p++),chroma_pixels);
cristy292ebf12011-02-08 20:21:31 +0000272 else
273 {
cristy4c08aed2011-07-01 19:47:50 +0000274 SetPixelBlue(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),
275 chroma_pixels);
cristy292ebf12011-02-08 20:21:31 +0000276 p+=2;
277 }
278 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000279 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy292ebf12011-02-08 20:21:31 +0000280 else
281 {
cristy4c08aed2011-07-01 19:47:50 +0000282 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
cristy292ebf12011-02-08 20:21:31 +0000283 p+=2;
284 }
cristy3ed852e2009-09-05 21:47:34 +0000285 chroma_pixels++;
cristyed231572011-07-14 02:18:59 +0000286 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000287 }
288 }
289 else
290 {
291 if ((y > 0) || (GetPreviousImageInList(image) == (Image *) NULL))
cristy292ebf12011-02-08 20:21:31 +0000292 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
cristy3ed852e2009-09-05 21:47:34 +0000293 p=scanline;
294 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000295 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000296 break;
cristybb503372010-05-27 20:51:26 +0000297 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000298 {
cristy292ebf12011-02-08 20:21:31 +0000299 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000300 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy292ebf12011-02-08 20:21:31 +0000301 else
302 {
cristy4c08aed2011-07-01 19:47:50 +0000303 SetPixelRed(image,ScaleShortToQuantum(((*p) << 8) | *(p+1)),q);
cristy292ebf12011-02-08 20:21:31 +0000304 p+=2;
305 }
cristy4c08aed2011-07-01 19:47:50 +0000306 SetPixelGreen(image,0,q);
307 SetPixelBlue(image,0,q);
cristyed231572011-07-14 02:18:59 +0000308 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000309 }
310 }
311 if (SyncAuthenticPixels(image,exception) == MagickFalse)
312 break;
313 if (interlace == NoInterlace)
314 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
315 break;
316 if (image->previous == (Image *) NULL)
317 {
cristycee97112010-05-28 00:44:52 +0000318 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy292ebf12011-02-08 20:21:31 +0000319 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000320 if (status == MagickFalse)
321 break;
322 }
323 }
324 if (interlace == PartitionInterlace)
325 {
326 (void) CloseBlob(image);
327 AppendImageFormat("U",image->filename);
328 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
329 if (status == MagickFalse)
330 {
331 image=DestroyImageList(image);
332 return((Image *) NULL);
333 }
334 }
335 if (interlace != NoInterlace)
336 {
cristybb503372010-05-27 20:51:26 +0000337 for (y=0; y < (ssize_t) chroma_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000338 {
cristy292ebf12011-02-08 20:21:31 +0000339 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
cristy3ed852e2009-09-05 21:47:34 +0000340 p=scanline;
341 q=QueueAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
342 exception);
cristyacd2ed22011-08-30 01:44:23 +0000343 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000344 break;
cristybb503372010-05-27 20:51:26 +0000345 for (x=0; x < (ssize_t) chroma_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000346 {
cristy4c08aed2011-07-01 19:47:50 +0000347 SetPixelRed(chroma_image,0,q);
cristy292ebf12011-02-08 20:21:31 +0000348 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000349 SetPixelGreen(chroma_image,ScaleCharToQuantum(*p++),q);
cristy292ebf12011-02-08 20:21:31 +0000350 else
351 {
cristy4c08aed2011-07-01 19:47:50 +0000352 SetPixelGreen(chroma_image,ScaleShortToQuantum(((*p) << 8) |
353 *(p+1)),q);
cristy292ebf12011-02-08 20:21:31 +0000354 p+=2;
355 }
cristy4c08aed2011-07-01 19:47:50 +0000356 SetPixelBlue(chroma_image,0,q);
cristyed231572011-07-14 02:18:59 +0000357 q+=GetPixelChannels(chroma_image);
cristy3ed852e2009-09-05 21:47:34 +0000358 }
359 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
360 break;
361 }
362 if (interlace == PartitionInterlace)
363 {
364 (void) CloseBlob(image);
365 AppendImageFormat("V",image->filename);
366 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
367 if (status == MagickFalse)
368 {
369 image=DestroyImageList(image);
370 return((Image *) NULL);
371 }
372 }
cristybb503372010-05-27 20:51:26 +0000373 for (y=0; y < (ssize_t) chroma_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000374 {
cristy292ebf12011-02-08 20:21:31 +0000375 count=ReadBlob(image,(size_t) quantum*chroma_image->columns,scanline);
cristy3ed852e2009-09-05 21:47:34 +0000376 p=scanline;
377 q=GetAuthenticPixels(chroma_image,0,y,chroma_image->columns,1,
378 exception);
cristyacd2ed22011-08-30 01:44:23 +0000379 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000380 break;
cristybb503372010-05-27 20:51:26 +0000381 for (x=0; x < (ssize_t) chroma_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000382 {
cristy292ebf12011-02-08 20:21:31 +0000383 if (quantum == 1)
cristy4c08aed2011-07-01 19:47:50 +0000384 SetPixelBlue(chroma_image,ScaleCharToQuantum(*p++),q);
cristy292ebf12011-02-08 20:21:31 +0000385 else
386 {
cristy4c08aed2011-07-01 19:47:50 +0000387 SetPixelBlue(chroma_image,ScaleShortToQuantum(((*p) << 8) |
388 *(p+1)),q);
cristy292ebf12011-02-08 20:21:31 +0000389 p+=2;
390 }
cristyed231572011-07-14 02:18:59 +0000391 q+=GetPixelChannels(chroma_image);
cristy3ed852e2009-09-05 21:47:34 +0000392 }
393 if (SyncAuthenticPixels(chroma_image,exception) == MagickFalse)
394 break;
395 }
396 }
397 /*
398 Scale image.
399 */
400 resize_image=ResizeImage(chroma_image,image->columns,image->rows,
cristyaa2c16c2012-03-25 22:21:35 +0000401 TriangleFilter,exception);
cristy3ed852e2009-09-05 21:47:34 +0000402 chroma_image=DestroyImage(chroma_image);
403 if (resize_image == (Image *) NULL)
404 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000405 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000406 {
407 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
408 chroma_pixels=GetVirtualPixels(resize_image,0,y,resize_image->columns,1,
cristyc82a27b2011-10-21 01:07:16 +0000409 exception);
cristyacd2ed22011-08-30 01:44:23 +0000410 if ((q == (Quantum *) NULL) ||
cristy4c08aed2011-07-01 19:47:50 +0000411 (chroma_pixels == (const Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000412 break;
cristybb503372010-05-27 20:51:26 +0000413 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000414 {
cristy4c08aed2011-07-01 19:47:50 +0000415 SetPixelGreen(image,GetPixelGreen(image,chroma_pixels),q);
416 SetPixelBlue(image,GetPixelBlue(image,chroma_pixels),q);
cristy3ed852e2009-09-05 21:47:34 +0000417 chroma_pixels++;
cristyed231572011-07-14 02:18:59 +0000418 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000419 }
420 if (SyncAuthenticPixels(image,exception) == MagickFalse)
421 break;
422 }
423 resize_image=DestroyImage(resize_image);
cristye2c4f182012-05-12 14:11:53 +0000424 SetImageColorspace(image,YCbCrColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000425 if (interlace == PartitionInterlace)
426 (void) CopyMagickString(image->filename,image_info->filename,
cristy151b66d2015-04-15 10:50:31 +0000427 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000428 if (EOFBlob(image) != MagickFalse)
429 {
430 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
431 image->filename);
432 break;
433 }
434 /*
435 Proceed to next image.
436 */
437 if (image_info->number_scenes != 0)
438 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
439 break;
440 if (interlace == NoInterlace)
cristy292ebf12011-02-08 20:21:31 +0000441 count=ReadBlob(image,(size_t) (2*quantum*image->columns),scanline);
cristy3ed852e2009-09-05 21:47:34 +0000442 else
cristy292ebf12011-02-08 20:21:31 +0000443 count=ReadBlob(image,(size_t) quantum*image->columns,scanline);
cristy3ed852e2009-09-05 21:47:34 +0000444 if (count != 0)
445 {
446 /*
447 Allocate next image structure.
448 */
cristy9950d572011-10-01 18:22:35 +0000449 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000450 if (GetNextImageInList(image) == (Image *) NULL)
451 {
452 image=DestroyImageList(image);
453 return((Image *) NULL);
454 }
455 image=SyncNextImageInList(image);
456 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
457 GetBlobSize(image));
458 if (status == MagickFalse)
459 break;
460 }
461 } while (count != 0);
462 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
463 (void) CloseBlob(image);
464 return(GetFirstImageInList(image));
465}
466
467/*
468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469% %
470% %
471% %
472% R e g i s t e r Y U V I m a g e %
473% %
474% %
475% %
476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477%
478% RegisterYUVImage() adds attributes for the YUV image format to
479% the list of supported formats. The attributes include the image format
480% tag, a method to read and/or write the format, whether the format
481% supports the saving of more than one frame to the same file or blob,
482% whether the format supports native in-memory I/O, and a brief
483% description of the format.
484%
485% The format of the RegisterYUVImage method is:
486%
cristybb503372010-05-27 20:51:26 +0000487% size_t RegisterYUVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000488%
489*/
cristybb503372010-05-27 20:51:26 +0000490ModuleExport size_t RegisterYUVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000491{
492 MagickInfo
493 *entry;
494
dirk06b627a2015-04-06 18:59:17 +0000495 entry=AcquireMagickInfo("YUV","YUV","CCIR 601 4:1:1 or 4:2:2");
cristy3ed852e2009-09-05 21:47:34 +0000496 entry->decoder=(DecodeImageHandler *) ReadYUVImage;
497 entry->encoder=(EncodeImageHandler *) WriteYUVImage;
dirk08e9a112015-02-22 01:51:41 +0000498 entry->flags^=CoderAdjoinFlag;
499 entry->flags|=CoderRawSupportFlag;
cristy3ed852e2009-09-05 21:47:34 +0000500 (void) RegisterMagickInfo(entry);
501 return(MagickImageCoderSignature);
502}
503
504/*
505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506% %
507% %
508% %
509% U n r e g i s t e r Y U V I m a g e %
510% %
511% %
512% %
513%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514%
515% UnregisterYUVImage() removes format registrations made by the
516% YUV module from the list of supported formats.
517%
518% The format of the UnregisterYUVImage method is:
519%
520% UnregisterYUVImage(void)
521%
522*/
523ModuleExport void UnregisterYUVImage(void)
524{
525 (void) UnregisterMagickInfo("YUV");
526}
527
528/*
529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530% %
531% %
532% %
533% W r i t e Y U V I m a g e %
534% %
535% %
536% %
537%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538%
539% WriteYUVImage() writes an image to a file in the digital YUV
540% (CCIR 601 4:1:1, plane or partition interlaced, or 4:2:2 plane, partition
541% interlaced or noninterlaced) bytes and returns it.
542%
543% The format of the WriteYUVImage method is:
544%
cristy3a37efd2011-08-28 20:31:03 +0000545% MagickBooleanType WriteYUVImage(const ImageInfo *image_info,
546% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000547%
548% A description of each parameter follows.
549%
550% o image_info: the image info.
551%
552% o image: The image.
553%
cristy3a37efd2011-08-28 20:31:03 +0000554% o exception: return any errors or warnings in this structure.
555%
cristy3ed852e2009-09-05 21:47:34 +0000556*/
cristy3a37efd2011-08-28 20:31:03 +0000557static MagickBooleanType WriteYUVImage(const ImageInfo *image_info,Image *image,
558 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000559{
560 Image
561 *chroma_image,
562 *yuv_image;
563
564 InterlaceType
565 interlace;
566
cristy3ed852e2009-09-05 21:47:34 +0000567 MagickBooleanType
568 status;
569
570 MagickOffsetType
571 scene;
572
cristy4c08aed2011-07-01 19:47:50 +0000573 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000574 *p,
575 *s;
576
cristybb503372010-05-27 20:51:26 +0000577 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000578 x;
579
cristybb503372010-05-27 20:51:26 +0000580 size_t
cristy3ed852e2009-09-05 21:47:34 +0000581 height,
cristy292ebf12011-02-08 20:21:31 +0000582 quantum,
cristy3ed852e2009-09-05 21:47:34 +0000583 width;
584
cristyc6da28e2011-04-28 01:41:35 +0000585 ssize_t
586 horizontal_factor,
587 vertical_factor,
588 y;
589
cristy3ed852e2009-09-05 21:47:34 +0000590 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000591 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000592 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000593 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000594 if (image->debug != MagickFalse)
595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristye42f6582012-02-11 17:59:50 +0000596 quantum=(size_t) (image->depth <= 8 ? 1 : 2);
cristy3ed852e2009-09-05 21:47:34 +0000597 interlace=image->interlace;
598 horizontal_factor=2;
599 vertical_factor=2;
600 if (image_info->sampling_factor != (char *) NULL)
601 {
602 GeometryInfo
603 geometry_info;
604
605 MagickStatusType
606 flags;
607
608 flags=ParseGeometry(image_info->sampling_factor,&geometry_info);
cristybb503372010-05-27 20:51:26 +0000609 horizontal_factor=(ssize_t) geometry_info.rho;
610 vertical_factor=(ssize_t) geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000611 if ((flags & SigmaValue) == 0)
612 vertical_factor=horizontal_factor;
613 if ((horizontal_factor != 1) && (horizontal_factor != 2) &&
614 (vertical_factor != 1) && (vertical_factor != 2))
615 ThrowWriterException(CorruptImageError,"UnexpectedSamplingFactor");
616 }
617 if ((interlace == UndefinedInterlace) ||
618 ((interlace == NoInterlace) && (vertical_factor == 2)))
619 {
620 interlace=NoInterlace; /* CCIR 4:2:2 */
621 if (vertical_factor == 2)
622 interlace=PlaneInterlace; /* CCIR 4:1:1 */
623 }
624 if (interlace != PartitionInterlace)
625 {
626 /*
627 Open output image file.
628 */
cristyedf03fa2011-08-30 12:44:39 +0000629 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000630 if (status == MagickFalse)
631 return(status);
632 }
633 else
634 {
635 AppendImageFormat("Y",image->filename);
cristyedf03fa2011-08-30 12:44:39 +0000636 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000637 if (status == MagickFalse)
638 return(status);
639 }
640 scene=0;
641 do
642 {
643 /*
644 Sample image to an even width and height, if necessary.
645 */
cristye42f6582012-02-11 17:59:50 +0000646 image->depth=(size_t) (quantum == 1 ? 8 : 16);
cristy3ed852e2009-09-05 21:47:34 +0000647 width=image->columns+(image->columns & (horizontal_factor-1));
648 height=image->rows+(image->rows & (vertical_factor-1));
cristyaa2c16c2012-03-25 22:21:35 +0000649 yuv_image=ResizeImage(image,width,height,TriangleFilter,exception);
cristy3ed852e2009-09-05 21:47:34 +0000650 if (yuv_image == (Image *) NULL)
cristy3a37efd2011-08-28 20:31:03 +0000651 {
652 (void) CloseBlob(image);
653 return(MagickFalse);
654 }
cristye941a752011-10-15 01:52:48 +0000655 (void) TransformImageColorspace(yuv_image,YCbCrColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000656 /*
657 Downsample image.
658 */
659 chroma_image=ResizeImage(image,width/horizontal_factor,
cristyaa2c16c2012-03-25 22:21:35 +0000660 height/vertical_factor,TriangleFilter,exception);
cristy3ed852e2009-09-05 21:47:34 +0000661 if (chroma_image == (Image *) NULL)
cristy3a37efd2011-08-28 20:31:03 +0000662 {
663 (void) CloseBlob(image);
664 return(MagickFalse);
665 }
cristye941a752011-10-15 01:52:48 +0000666 (void) TransformImageColorspace(chroma_image,YCbCrColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000667 if (interlace == NoInterlace)
668 {
669 /*
670 Write noninterlaced YUV.
671 */
cristybb503372010-05-27 20:51:26 +0000672 for (y=0; y < (ssize_t) yuv_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000673 {
cristy3a37efd2011-08-28 20:31:03 +0000674 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000675 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000676 break;
677 s=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
cristy3a37efd2011-08-28 20:31:03 +0000678 exception);
cristy4c08aed2011-07-01 19:47:50 +0000679 if (s == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000680 break;
cristybb503372010-05-27 20:51:26 +0000681 for (x=0; x < (ssize_t) yuv_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000682 {
cristy292ebf12011-02-08 20:21:31 +0000683 if (quantum == 1)
684 {
cristyc6da28e2011-04-28 01:41:35 +0000685 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000686 GetPixelGreen(yuv_image,s)));
cristy292ebf12011-02-08 20:21:31 +0000687 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000688 GetPixelRed(yuv_image,p)));
cristyed231572011-07-14 02:18:59 +0000689 p+=GetPixelChannels(yuv_image);
cristyc6da28e2011-04-28 01:41:35 +0000690 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000691 GetPixelBlue(yuv_image,s)));
cristy292ebf12011-02-08 20:21:31 +0000692 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000693 GetPixelRed(yuv_image,p)));
cristy292ebf12011-02-08 20:21:31 +0000694 }
695 else
696 {
cristyc6da28e2011-04-28 01:41:35 +0000697 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000698 GetPixelGreen(yuv_image,s)));
cristy292ebf12011-02-08 20:21:31 +0000699 (void) WriteBlobShort(image,ScaleQuantumToShort(
cristy4c08aed2011-07-01 19:47:50 +0000700 GetPixelRed(yuv_image,p)));
cristyed231572011-07-14 02:18:59 +0000701 p+=GetPixelChannels(yuv_image);
cristyc6da28e2011-04-28 01:41:35 +0000702 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000703 GetPixelBlue(yuv_image,s)));
cristy292ebf12011-02-08 20:21:31 +0000704 (void) WriteBlobShort(image,ScaleQuantumToShort(
cristy4c08aed2011-07-01 19:47:50 +0000705 GetPixelRed(yuv_image,p)));
cristy292ebf12011-02-08 20:21:31 +0000706 }
cristyed231572011-07-14 02:18:59 +0000707 p+=GetPixelChannels(yuv_image);
cristy3ed852e2009-09-05 21:47:34 +0000708 s++;
709 x++;
710 }
711 if (image->previous == (Image *) NULL)
712 {
cristycee97112010-05-28 00:44:52 +0000713 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
714 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000715 if (status == MagickFalse)
716 break;
717 }
718 }
719 yuv_image=DestroyImage(yuv_image);
720 }
721 else
722 {
723 /*
724 Initialize Y channel.
725 */
cristybb503372010-05-27 20:51:26 +0000726 for (y=0; y < (ssize_t) yuv_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000727 {
cristy3a37efd2011-08-28 20:31:03 +0000728 p=GetVirtualPixels(yuv_image,0,y,yuv_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000729 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000730 break;
cristybb503372010-05-27 20:51:26 +0000731 for (x=0; x < (ssize_t) yuv_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000732 {
cristy292ebf12011-02-08 20:21:31 +0000733 if (quantum == 1)
734 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000735 GetPixelRed(yuv_image,p)));
cristy292ebf12011-02-08 20:21:31 +0000736 else
737 (void) WriteBlobShort(image,ScaleQuantumToShort(
cristy4c08aed2011-07-01 19:47:50 +0000738 GetPixelRed(yuv_image,p)));
cristyed231572011-07-14 02:18:59 +0000739 p+=GetPixelChannels(yuv_image);
cristy3ed852e2009-09-05 21:47:34 +0000740 }
741 if (image->previous == (Image *) NULL)
742 {
cristycee97112010-05-28 00:44:52 +0000743 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
744 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000745 if (status == MagickFalse)
746 break;
747 }
748 }
749 yuv_image=DestroyImage(yuv_image);
750 if (image->previous == (Image *) NULL)
751 {
752 status=SetImageProgress(image,SaveImageTag,1,3);
753 if (status == MagickFalse)
754 break;
755 }
756 /*
757 Initialize U channel.
758 */
759 if (interlace == PartitionInterlace)
760 {
761 (void) CloseBlob(image);
762 AppendImageFormat("U",image->filename);
cristyedf03fa2011-08-30 12:44:39 +0000763 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000764 if (status == MagickFalse)
765 return(status);
766 }
cristybb503372010-05-27 20:51:26 +0000767 for (y=0; y < (ssize_t) chroma_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000768 {
769 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
cristy3a37efd2011-08-28 20:31:03 +0000770 exception);
cristy4c08aed2011-07-01 19:47:50 +0000771 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000772 break;
cristybb503372010-05-27 20:51:26 +0000773 for (x=0; x < (ssize_t) chroma_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000774 {
cristy292ebf12011-02-08 20:21:31 +0000775 if (quantum == 1)
776 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000777 GetPixelGreen(chroma_image,p)));
cristy292ebf12011-02-08 20:21:31 +0000778 else
779 (void) WriteBlobShort(image,ScaleQuantumToShort(
cristy4c08aed2011-07-01 19:47:50 +0000780 GetPixelGreen(chroma_image,p)));
cristyed231572011-07-14 02:18:59 +0000781 p+=GetPixelChannels(chroma_image);
cristy3ed852e2009-09-05 21:47:34 +0000782 }
783 }
784 if (image->previous == (Image *) NULL)
785 {
786 status=SetImageProgress(image,SaveImageTag,2,3);
787 if (status == MagickFalse)
788 break;
789 }
790 /*
791 Initialize V channel.
792 */
793 if (interlace == PartitionInterlace)
794 {
795 (void) CloseBlob(image);
796 AppendImageFormat("V",image->filename);
cristyedf03fa2011-08-30 12:44:39 +0000797 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000798 if (status == MagickFalse)
799 return(status);
800 }
cristybb503372010-05-27 20:51:26 +0000801 for (y=0; y < (ssize_t) chroma_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
803 p=GetVirtualPixels(chroma_image,0,y,chroma_image->columns,1,
cristy3a37efd2011-08-28 20:31:03 +0000804 exception);
cristy4c08aed2011-07-01 19:47:50 +0000805 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000806 break;
cristybb503372010-05-27 20:51:26 +0000807 for (x=0; x < (ssize_t) chroma_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000808 {
cristy292ebf12011-02-08 20:21:31 +0000809 if (quantum == 1)
810 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000811 GetPixelBlue(chroma_image,p)));
cristy292ebf12011-02-08 20:21:31 +0000812 else
813 (void) WriteBlobShort(image,ScaleQuantumToShort(
cristy4c08aed2011-07-01 19:47:50 +0000814 GetPixelBlue(chroma_image,p)));
cristyed231572011-07-14 02:18:59 +0000815 p+=GetPixelChannels(chroma_image);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
817 }
818 if (image->previous == (Image *) NULL)
819 {
820 status=SetImageProgress(image,SaveImageTag,2,3);
821 if (status == MagickFalse)
822 break;
823 }
824 }
825 chroma_image=DestroyImage(chroma_image);
826 if (interlace == PartitionInterlace)
827 (void) CopyMagickString(image->filename,image_info->filename,
cristy151b66d2015-04-15 10:50:31 +0000828 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000829 if (GetNextImageInList(image) == (Image *) NULL)
830 break;
831 image=SyncNextImageInList(image);
832 status=SetImageProgress(image,SaveImagesTag,scene++,
833 GetImageListLength(image));
834 if (status == MagickFalse)
835 break;
836 } while (image_info->adjoin != MagickFalse);
837 (void) CloseBlob(image);
838 return(MagickTrue);
839}