2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write 16bit/pixel Interleaved YUV 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 "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"
65 static MagickBooleanType
66 WriteUYVYImage(const ImageInfo *,Image *);
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % R e a d U Y V Y I m a g e %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
83 % The format of the ReadUYVYImage method is:
85 % Image *ReadUYVYImage(const ImageInfo *image_info,
86 % ExceptionInfo *exception)
88 % A description of each parameter follows:
90 % o image_info: the image info.
92 % o exception: return any errors or warnings in this structure.
95 static Image *ReadUYVYImage(const ImageInfo *image_info,
96 ExceptionInfo *exception)
122 assert(image_info != (const ImageInfo *) NULL);
123 assert(image_info->signature == MagickSignature);
124 if (image_info->debug != MagickFalse)
125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
126 image_info->filename);
127 assert(exception != (ExceptionInfo *) NULL);
128 assert(exception->signature == MagickSignature);
129 image=AcquireImage(image_info);
130 if ((image->columns == 0) || (image->rows == 0))
131 ThrowReaderException(OptionError,"MustSpecifyImageSize");
132 if ((image->columns % 2) != 0)
134 (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
135 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136 if (status == MagickFalse)
137 return((Image *) NULL);
138 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
139 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
142 if (image_info->ping != MagickFalse)
144 (void) CloseBlob(image);
145 return(GetFirstImageInList(image));
148 Accumulate UYVY, then unpack into two pixels.
150 for (y=0; y < (ssize_t) image->rows; y++)
152 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
153 if (q == (PixelPacket *) NULL)
155 for (x=0; x < (ssize_t) (image->columns >> 1); x++)
157 u=(unsigned char) ReadBlobByte(image);
158 y1=(unsigned char) ReadBlobByte(image);
159 v=(unsigned char) ReadBlobByte(image);
160 y2=(unsigned char) ReadBlobByte(image);
161 SetRedPixelComponent(q,ScaleCharToQuantum(y1));
162 SetGreenPixelComponent(q,ScaleCharToQuantum(u));
163 SetBluePixelComponent(q,ScaleCharToQuantum(v));
165 SetRedPixelComponent(q,ScaleCharToQuantum(y2));
166 SetGreenPixelComponent(q,ScaleCharToQuantum(u));
167 SetBluePixelComponent(q,ScaleCharToQuantum(v));
170 if (SyncAuthenticPixels(image,exception) == MagickFalse)
172 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
174 if (status == MagickFalse)
177 image->colorspace=YCbCrColorspace;
178 if (EOFBlob(image) != MagickFalse)
179 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
181 (void) CloseBlob(image);
182 return(GetFirstImageInList(image));
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % R e g i s t e r U Y V Y I m a g e %
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 % RegisterUYVYImage() adds attributes for the UYVY image format to
197 % the list of supported formats. The attributes include the image format
198 % tag, a method to read and/or write the format, whether the format
199 % supports the saving of more than one frame to the same file or blob,
200 % whether the format supports native in-memory I/O, and a brief
201 % description of the format.
203 % The format of the RegisterUYVYImage method is:
205 % size_t RegisterUYVYImage(void)
208 ModuleExport size_t RegisterUYVYImage(void)
213 entry=SetMagickInfo("PAL");
214 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
215 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
216 entry->adjoin=MagickFalse;
217 entry->raw=MagickTrue;
218 entry->endian_support=MagickTrue;
219 entry->description=ConstantString("16bit/pixel interleaved YUV");
220 entry->module=ConstantString("UYVY");
221 (void) RegisterMagickInfo(entry);
222 entry=SetMagickInfo("UYVY");
223 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
224 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
225 entry->adjoin=MagickFalse;
226 entry->raw=MagickTrue;
227 entry->endian_support=MagickTrue;
228 entry->description=ConstantString("16bit/pixel interleaved YUV");
229 entry->module=ConstantString("UYVY");
230 (void) RegisterMagickInfo(entry);
231 return(MagickImageCoderSignature);
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % U n r e g i s t e r U Y V Y I m a g e %
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % UnregisterUYVYImage() removes format registrations made by the
246 % UYVY module from the list of supported formats.
248 % The format of the UnregisterUYVYImage method is:
250 % UnregisterUYVYImage(void)
253 ModuleExport void UnregisterUYVYImage(void)
255 (void) UnregisterMagickInfo("PAL");
256 (void) UnregisterMagickInfo("UYVY");
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 % W r i t e U Y V Y I m a g e %
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 % WriteUYVYImage() writes an image to a file in the digital UYVY
271 % format. This format, used by AccomWSD, is not dramatically higher quality
272 % than the 12bit/pixel YUV format, but has better locality.
274 % The format of the WriteUYVYImage method is:
276 % MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
279 % A description of each parameter follows.
281 % o image_info: the image info.
283 % o image: The image.
284 % Implicit assumption: number of columns is even.
287 static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
300 register const PixelPacket
310 Open output image file.
312 assert(image_info != (const ImageInfo *) NULL);
313 assert(image_info->signature == MagickSignature);
314 assert(image != (Image *) NULL);
315 assert(image->signature == MagickSignature);
316 if (image->debug != MagickFalse)
317 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
318 if ((image->columns % 2) != 0)
320 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
321 if (status == MagickFalse)
324 Accumulate two pixels, then output.
326 uyvy_image=CloneImage(image,0,0,MagickTrue,&image->exception);
327 if (uyvy_image == (Image *) NULL)
328 ThrowWriterException(ResourceLimitError,image->exception.reason);
329 (void) TransformImageColorspace(uyvy_image,YCbCrColorspace);
331 (void) ResetMagickMemory(&pixel,0,sizeof(MagickPixelPacket));
332 for (y=0; y < (ssize_t) image->rows; y++)
334 p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,&image->exception);
335 if (p == (const PixelPacket *) NULL)
337 for (x=0; x < (ssize_t) image->columns; x++)
339 if (full != MagickFalse)
341 pixel.green=(pixel.green+GetGreenPixelComponent(p))/2;
342 pixel.blue=(pixel.blue+GetBluePixelComponent(p))/2;
343 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.green));
344 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.red));
345 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.blue));
346 (void) WriteBlobByte(image,ScaleQuantumToChar(
347 GetRedPixelComponent(p)));
349 pixel.red=(double) GetRedPixelComponent(p);
350 pixel.green=(double) GetGreenPixelComponent(p);
351 pixel.blue=(double) GetBluePixelComponent(p);
352 full=full == MagickFalse ? MagickTrue : MagickFalse;
355 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
357 if (status == MagickFalse)
360 uyvy_image=DestroyImage(uyvy_image);
361 (void) CloseBlob(image);