blob: b751d7dbfafd773ba7cb192f3a277f58c1e7a5e5 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% U U Y Y V V Y Y %
7% U U Y Y V V Y Y %
8% U U Y V V Y %
9% U U Y V V Y %
10% UUU Y V Y %
11% %
12% %
13% Read/Write 16bit/pixel Interleaved YUV 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% %
Cristyd8420112021-01-01 14:52:00 -050020% Copyright 1999-2021 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% %
Cristy83d74de2018-10-13 10:17:25 -040026% https://imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
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/color.h"
47#include "MagickCore/colorspace.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/monitor.h"
56#include "MagickCore/monitor-private.h"
57#include "MagickCore/pixel-accessor.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/static.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Forward declarations.
65*/
66static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000067 WriteUYVYImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000068
69/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71% %
72% %
73% %
74% R e a d U Y V Y I m a g e %
75% %
76% %
77% %
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79%
80% ReadUYVYImage() reads an image in the UYVY format and returns it. It
81% allocates the memory necessary for the new Image structure and returns a
82% pointer to the new image.
83%
84% The format of the ReadUYVYImage method is:
85%
86% Image *ReadUYVYImage(const ImageInfo *image_info,
87% ExceptionInfo *exception)
88%
89% A description of each parameter follows:
90%
91% o image_info: the image info.
92%
93% o exception: return any errors or warnings in this structure.
94%
cristy3ed852e2009-09-05 21:47:34 +000095*/
96static Image *ReadUYVYImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
98{
99 Image
100 *image;
101
cristy3ed852e2009-09-05 21:47:34 +0000102 MagickBooleanType
103 status;
104
Cristyf2dc1dd2020-12-28 13:59:26 -0500105 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000106 x;
107
Cristyf2dc1dd2020-12-28 13:59:26 -0500108 Quantum
cristy3ed852e2009-09-05 21:47:34 +0000109 *q;
110
cristyc6da28e2011-04-28 01:41:35 +0000111 ssize_t
112 y;
113
cristy3ed852e2009-09-05 21:47:34 +0000114 unsigned char
115 u,
116 v,
117 y1,
118 y2;
119
120 /*
121 Open image file.
122 */
123 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000124 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000125 if (image_info->debug != MagickFalse)
126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
127 image_info->filename);
128 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000129 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000130 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000131 if ((image->columns == 0) || (image->rows == 0))
132 ThrowReaderException(OptionError,"MustSpecifyImageSize");
133 if ((image->columns % 2) != 0)
134 image->columns++;
Cristy096bddc2018-06-07 18:24:26 -0400135 (void) CopyMagickString(image->filename,image_info->filename,
136 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000137 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
138 if (status == MagickFalse)
Cristy096bddc2018-06-07 18:24:26 -0400139 return(DestroyImage(image));
cristyd4297022010-09-16 22:59:09 +0000140 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
141 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
142 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000143 image->depth=8;
144 if (image_info->ping != MagickFalse)
145 {
146 (void) CloseBlob(image);
147 return(GetFirstImageInList(image));
148 }
cristyacabb842014-12-14 23:36:33 +0000149 status=SetImageExtent(image,image->columns,image->rows,exception);
150 if (status == MagickFalse)
151 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000152 /*
153 Accumulate UYVY, then unpack into two pixels.
154 */
cristybb503372010-05-27 20:51:26 +0000155 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000156 {
157 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000158 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000159 break;
cristybb503372010-05-27 20:51:26 +0000160 for (x=0; x < (ssize_t) (image->columns >> 1); x++)
cristy3ed852e2009-09-05 21:47:34 +0000161 {
162 u=(unsigned char) ReadBlobByte(image);
163 y1=(unsigned char) ReadBlobByte(image);
164 v=(unsigned char) ReadBlobByte(image);
165 y2=(unsigned char) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000166 SetPixelRed(image,ScaleCharToQuantum(y1),q);
167 SetPixelGreen(image,ScaleCharToQuantum(u),q);
168 SetPixelBlue(image,ScaleCharToQuantum(v),q);
cristyed231572011-07-14 02:18:59 +0000169 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000170 SetPixelRed(image,ScaleCharToQuantum(y2),q);
171 SetPixelGreen(image,ScaleCharToQuantum(u),q);
172 SetPixelBlue(image,ScaleCharToQuantum(v),q);
cristyed231572011-07-14 02:18:59 +0000173 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000174 }
175 if (SyncAuthenticPixels(image,exception) == MagickFalse)
176 break;
cristycee97112010-05-28 00:44:52 +0000177 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000178 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000179 if (status == MagickFalse)
180 break;
181 }
cristye2c4f182012-05-12 14:11:53 +0000182 SetImageColorspace(image,YCbCrColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000183 if (EOFBlob(image) != MagickFalse)
184 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
185 image->filename);
186 (void) CloseBlob(image);
187 return(GetFirstImageInList(image));
188}
189
190/*
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192% %
193% %
194% %
195% R e g i s t e r U Y V Y I m a g e %
196% %
197% %
198% %
199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200%
201% RegisterUYVYImage() adds attributes for the UYVY image format to
202% the list of supported formats. The attributes include the image format
203% tag, a method to read and/or write the format, whether the format
204% supports the saving of more than one frame to the same file or blob,
205% whether the format supports native in-memory I/O, and a brief
206% description of the format.
207%
208% The format of the RegisterUYVYImage method is:
209%
cristybb503372010-05-27 20:51:26 +0000210% size_t RegisterUYVYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000211%
212*/
cristybb503372010-05-27 20:51:26 +0000213ModuleExport size_t RegisterUYVYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000214{
215 MagickInfo
216 *entry;
217
dirk06b627a2015-04-06 18:59:17 +0000218 entry=AcquireMagickInfo("UYVY","PAL","16bit/pixel interleaved YUV");
cristy3ed852e2009-09-05 21:47:34 +0000219 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
220 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
dirk08e9a112015-02-22 01:51:41 +0000221 entry->flags^=CoderAdjoinFlag;
222 entry->flags|=CoderRawSupportFlag;
223 entry->flags|=CoderEndianSupportFlag;
cristy3ed852e2009-09-05 21:47:34 +0000224 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000225 entry=AcquireMagickInfo("UYVY","UYVY","16bit/pixel interleaved YUV");
cristy3ed852e2009-09-05 21:47:34 +0000226 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
227 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
dirk08e9a112015-02-22 01:51:41 +0000228 entry->flags^=CoderAdjoinFlag;
229 entry->flags|=CoderRawSupportFlag;
230 entry->flags|=CoderEndianSupportFlag;
cristy3ed852e2009-09-05 21:47:34 +0000231 (void) RegisterMagickInfo(entry);
232 return(MagickImageCoderSignature);
233}
234
235/*
236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237% %
238% %
239% %
240% U n r e g i s t e r U Y V Y I m a g e %
241% %
242% %
243% %
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245%
246% UnregisterUYVYImage() removes format registrations made by the
247% UYVY module from the list of supported formats.
248%
249% The format of the UnregisterUYVYImage method is:
250%
251% UnregisterUYVYImage(void)
252%
253*/
254ModuleExport void UnregisterUYVYImage(void)
255{
256 (void) UnregisterMagickInfo("PAL");
257 (void) UnregisterMagickInfo("UYVY");
258}
259
260/*
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262% %
263% %
264% %
265% W r i t e U Y V Y I m a g e %
266% %
267% %
268% %
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270%
271% WriteUYVYImage() writes an image to a file in the digital UYVY
272% format. This format, used by AccomWSD, is not dramatically higher quality
273% than the 12bit/pixel YUV format, but has better locality.
274%
275% The format of the WriteUYVYImage method is:
276%
277% MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000278% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000279%
280% A description of each parameter follows.
281%
282% o image_info: the image info.
283%
cristy3a37efd2011-08-28 20:31:03 +0000284% o image: The image. Implicit assumption: number of columns is even.
285%
286% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +0000287%
288*/
289static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000290 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000291{
cristy4c08aed2011-07-01 19:47:50 +0000292 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000293 pixel;
294
295 Image
296 *uyvy_image;
297
cristy3ed852e2009-09-05 21:47:34 +0000298 MagickBooleanType
299 full,
300 status;
301
Cristyf2dc1dd2020-12-28 13:59:26 -0500302 const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000303 *p;
304
Cristyf2dc1dd2020-12-28 13:59:26 -0500305 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000306 x;
307
cristyc6da28e2011-04-28 01:41:35 +0000308 ssize_t
309 y;
310
cristy3ed852e2009-09-05 21:47:34 +0000311 /*
312 Open output image file.
313 */
314 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000315 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000316 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000317 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000318 if (image->debug != MagickFalse)
319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
320 if ((image->columns % 2) != 0)
321 image->columns++;
cristy3a37efd2011-08-28 20:31:03 +0000322 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000323 assert(exception->signature == MagickCoreSignature);
cristy3a37efd2011-08-28 20:31:03 +0000324 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000325 if (status == MagickFalse)
326 return(status);
327 /*
328 Accumulate two pixels, then output.
329 */
cristy3a37efd2011-08-28 20:31:03 +0000330 uyvy_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000331 if (uyvy_image == (Image *) NULL)
cristy3a37efd2011-08-28 20:31:03 +0000332 return(MagickFalse);
cristye941a752011-10-15 01:52:48 +0000333 (void) TransformImageColorspace(uyvy_image,YCbCrColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000334 full=MagickFalse;
Cristy81bfff22018-03-10 07:58:31 -0500335 (void) memset(&pixel,0,sizeof(PixelInfo));
cristybb503372010-05-27 20:51:26 +0000336 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000337 {
cristy3a37efd2011-08-28 20:31:03 +0000338 p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000339 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000340 break;
cristybb503372010-05-27 20:51:26 +0000341 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000342 {
343 if (full != MagickFalse)
344 {
cristy4c08aed2011-07-01 19:47:50 +0000345 pixel.green=(pixel.green+GetPixelGreen(uyvy_image,p))/2;
346 pixel.blue=(pixel.blue+GetPixelBlue(uyvy_image,p))/2;
cristy3ed852e2009-09-05 21:47:34 +0000347 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.green));
348 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.red));
349 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.blue));
cristyc6da28e2011-04-28 01:41:35 +0000350 (void) WriteBlobByte(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000351 GetPixelRed(uyvy_image,p)));
cristy3ed852e2009-09-05 21:47:34 +0000352 }
cristy4c08aed2011-07-01 19:47:50 +0000353 pixel.red=(double) GetPixelRed(uyvy_image,p);
354 pixel.green=(double) GetPixelGreen(uyvy_image,p);
355 pixel.blue=(double) GetPixelBlue(uyvy_image,p);
cristy3ed852e2009-09-05 21:47:34 +0000356 full=full == MagickFalse ? MagickTrue : MagickFalse;
cristyed231572011-07-14 02:18:59 +0000357 p+=GetPixelChannels(uyvy_image);
cristy3ed852e2009-09-05 21:47:34 +0000358 }
cristycee97112010-05-28 00:44:52 +0000359 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000360 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000361 if (status == MagickFalse)
362 break;
363 }
364 uyvy_image=DestroyImage(uyvy_image);
365 (void) CloseBlob(image);
366 return(MagickTrue);
367}