blob: f77c1793365c0ed035b83fd53f6b24ffc4b76d6f [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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
46#include "magick/color.h"
47#include "magick/colorspace.h"
48#include "magick/exception.h"
49#include "magick/exception-private.h"
50#include "magick/image.h"
51#include "magick/image-private.h"
52#include "magick/list.h"
53#include "magick/magick.h"
54#include "magick/memory_.h"
55#include "magick/monitor.h"
56#include "magick/monitor-private.h"
57#include "magick/quantum-private.h"
58#include "magick/static.h"
59#include "magick/string_.h"
60#include "magick/module.h"
61
62/*
63 Forward declarations.
64*/
65static MagickBooleanType
66 WriteUYVYImage(const ImageInfo *,Image *);
67
68/*
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70% %
71% %
72% %
73% R e a d U Y V Y I m a g e %
74% %
75% %
76% %
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78%
79% ReadUYVYImage() reads an image in the UYVY format and returns it. It
80% allocates the memory necessary for the new Image structure and returns a
81% pointer to the new image.
82%
83% The format of the ReadUYVYImage method is:
84%
85% Image *ReadUYVYImage(const ImageInfo *image_info,
86% ExceptionInfo *exception)
87%
88% A description of each parameter follows:
89%
90% o image_info: the image info.
91%
92% o exception: return any errors or warnings in this structure.
93%
94%
95*/
96static Image *ReadUYVYImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
98{
99 Image
100 *image;
101
cristybb503372010-05-27 20:51:26 +0000102 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000103 y;
104
105 MagickBooleanType
106 status;
107
cristybb503372010-05-27 20:51:26 +0000108 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000109 x;
110
111 register PixelPacket
112 *q;
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);
124 assert(image_info->signature == MagickSignature);
125 if (image_info->debug != MagickFalse)
126 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
127 image_info->filename);
128 assert(exception != (ExceptionInfo *) NULL);
129 assert(exception->signature == MagickSignature);
130 image=AcquireImage(image_info);
131 if ((image->columns == 0) || (image->rows == 0))
132 ThrowReaderException(OptionError,"MustSpecifyImageSize");
133 if ((image->columns % 2) != 0)
134 image->columns++;
135 (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
136 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
137 if (status == MagickFalse)
138 return((Image *) NULL);
cristyd4297022010-09-16 22:59:09 +0000139 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
140 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
141 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000142 image->depth=8;
143 if (image_info->ping != MagickFalse)
144 {
145 (void) CloseBlob(image);
146 return(GetFirstImageInList(image));
147 }
148 /*
149 Accumulate UYVY, then unpack into two pixels.
150 */
cristybb503372010-05-27 20:51:26 +0000151 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000152 {
153 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
154 if (q == (PixelPacket *) NULL)
155 break;
cristybb503372010-05-27 20:51:26 +0000156 for (x=0; x < (ssize_t) (image->columns >> 1); x++)
cristy3ed852e2009-09-05 21:47:34 +0000157 {
158 u=(unsigned char) ReadBlobByte(image);
159 y1=(unsigned char) ReadBlobByte(image);
160 v=(unsigned char) ReadBlobByte(image);
161 y2=(unsigned char) ReadBlobByte(image);
162 q->red=ScaleCharToQuantum(y1);
163 q->green=ScaleCharToQuantum(u);
164 q->blue=ScaleCharToQuantum(v);
165 q++;
166 q->red=ScaleCharToQuantum(y2);
167 q->green=ScaleCharToQuantum(u);
168 q->blue=ScaleCharToQuantum(v);
169 q++;
170 }
171 if (SyncAuthenticPixels(image,exception) == MagickFalse)
172 break;
cristycee97112010-05-28 00:44:52 +0000173 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
174 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000175 if (status == MagickFalse)
176 break;
177 }
178 image->colorspace=YCbCrColorspace;
179 if (EOFBlob(image) != MagickFalse)
180 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
181 image->filename);
182 (void) CloseBlob(image);
183 return(GetFirstImageInList(image));
184}
185
186/*
187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188% %
189% %
190% %
191% R e g i s t e r U Y V Y I m a g e %
192% %
193% %
194% %
195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196%
197% RegisterUYVYImage() adds attributes for the UYVY image format to
198% the list of supported formats. The attributes include the image format
199% tag, a method to read and/or write the format, whether the format
200% supports the saving of more than one frame to the same file or blob,
201% whether the format supports native in-memory I/O, and a brief
202% description of the format.
203%
204% The format of the RegisterUYVYImage method is:
205%
cristybb503372010-05-27 20:51:26 +0000206% size_t RegisterUYVYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000207%
208*/
cristybb503372010-05-27 20:51:26 +0000209ModuleExport size_t RegisterUYVYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000210{
211 MagickInfo
212 *entry;
213
214 entry=SetMagickInfo("PAL");
215 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
216 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
217 entry->adjoin=MagickFalse;
218 entry->raw=MagickTrue;
219 entry->endian_support=MagickTrue;
220 entry->description=ConstantString("16bit/pixel interleaved YUV");
221 entry->module=ConstantString("UYVY");
222 (void) RegisterMagickInfo(entry);
223 entry=SetMagickInfo("UYVY");
224 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
225 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
226 entry->adjoin=MagickFalse;
227 entry->raw=MagickTrue;
228 entry->endian_support=MagickTrue;
229 entry->description=ConstantString("16bit/pixel interleaved YUV");
230 entry->module=ConstantString("UYVY");
231 (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,
278% Image *image)
279%
280% A description of each parameter follows.
281%
282% o image_info: the image info.
283%
284% o image: The image.
285% Implicit assumption: number of columns is even.
286%
287*/
288static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
289 Image *image)
290{
291 MagickPixelPacket
292 pixel;
293
294 Image
295 *uyvy_image;
296
cristybb503372010-05-27 20:51:26 +0000297 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000298 y;
299
300 MagickBooleanType
301 full,
302 status;
303
304 register const PixelPacket
305 *p;
306
cristybb503372010-05-27 20:51:26 +0000307 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000308 x;
309
310 /*
311 Open output image file.
312 */
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 assert(image != (Image *) NULL);
316 assert(image->signature == MagickSignature);
317 if (image->debug != MagickFalse)
318 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
319 if ((image->columns % 2) != 0)
320 image->columns++;
321 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
322 if (status == MagickFalse)
323 return(status);
324 /*
325 Accumulate two pixels, then output.
326 */
327 uyvy_image=CloneImage(image,0,0,MagickTrue,&image->exception);
328 if (uyvy_image == (Image *) NULL)
329 ThrowWriterException(ResourceLimitError,image->exception.reason);
330 (void) TransformImageColorspace(uyvy_image,YCbCrColorspace);
331 full=MagickFalse;
332 (void) ResetMagickMemory(&pixel,0,sizeof(MagickPixelPacket));
cristybb503372010-05-27 20:51:26 +0000333 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
335 p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,&image->exception);
336 if (p == (const PixelPacket *) NULL)
337 break;
cristybb503372010-05-27 20:51:26 +0000338 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000339 {
340 if (full != MagickFalse)
341 {
cristyce70c172010-01-07 17:15:30 +0000342 pixel.green=(pixel.green+GetGreenPixelComponent(p))/2;
343 pixel.blue=(pixel.blue+GetBluePixelComponent(p))/2;
cristy3ed852e2009-09-05 21:47:34 +0000344 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.green));
345 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.red));
346 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.blue));
cristyce70c172010-01-07 17:15:30 +0000347 (void) WriteBlobByte(image,ScaleQuantumToChar(GetRedPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +0000348 }
cristyce70c172010-01-07 17:15:30 +0000349 pixel.red=(double) GetRedPixelComponent(p);
350 pixel.green=(double) GetGreenPixelComponent(p);
351 pixel.blue=(double) GetBluePixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000352 full=full == MagickFalse ? MagickTrue : MagickFalse;
353 p++;
354 }
cristycee97112010-05-28 00:44:52 +0000355 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
356 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000357 if (status == MagickFalse)
358 break;
359 }
360 uyvy_image=DestroyImage(uyvy_image);
361 (void) CloseBlob(image);
362 return(MagickTrue);
363}