2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write 16bit/pixel Interleaved YUV Image Format %
20 % Copyright 1999-2018 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 % https://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/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"
66 static MagickBooleanType
67 WriteUYVYImage(const ImageInfo *,Image *,ExceptionInfo *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d U Y V Y I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
84 % The format of the ReadUYVYImage method is:
86 % Image *ReadUYVYImage(const ImageInfo *image_info,
87 % ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
96 static Image *ReadUYVYImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
123 assert(image_info != (const ImageInfo *) NULL);
124 assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
130 image=AcquireImage(image_info,exception);
131 if ((image->columns == 0) || (image->rows == 0))
132 ThrowReaderException(OptionError,"MustSpecifyImageSize");
133 if ((image->columns % 2) != 0)
135 (void) CopyMagickString(image->filename,image_info->filename,
137 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
138 if (status == MagickFalse)
139 return(DestroyImage(image));
140 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
141 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
144 if (image_info->ping != MagickFalse)
146 (void) CloseBlob(image);
147 return(GetFirstImageInList(image));
149 status=SetImageExtent(image,image->columns,image->rows,exception);
150 if (status == MagickFalse)
151 return(DestroyImageList(image));
153 Accumulate UYVY, then unpack into two pixels.
155 for (y=0; y < (ssize_t) image->rows; y++)
157 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
158 if (q == (Quantum *) NULL)
160 for (x=0; x < (ssize_t) (image->columns >> 1); x++)
162 u=(unsigned char) ReadBlobByte(image);
163 y1=(unsigned char) ReadBlobByte(image);
164 v=(unsigned char) ReadBlobByte(image);
165 y2=(unsigned char) ReadBlobByte(image);
166 SetPixelRed(image,ScaleCharToQuantum(y1),q);
167 SetPixelGreen(image,ScaleCharToQuantum(u),q);
168 SetPixelBlue(image,ScaleCharToQuantum(v),q);
169 q+=GetPixelChannels(image);
170 SetPixelRed(image,ScaleCharToQuantum(y2),q);
171 SetPixelGreen(image,ScaleCharToQuantum(u),q);
172 SetPixelBlue(image,ScaleCharToQuantum(v),q);
173 q+=GetPixelChannels(image);
175 if (SyncAuthenticPixels(image,exception) == MagickFalse)
177 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
179 if (status == MagickFalse)
182 SetImageColorspace(image,YCbCrColorspace,exception);
183 if (EOFBlob(image) != MagickFalse)
184 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
186 (void) CloseBlob(image);
187 return(GetFirstImageInList(image));
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 % R e g i s t e r U Y V Y I m a g e %
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
208 % The format of the RegisterUYVYImage method is:
210 % size_t RegisterUYVYImage(void)
213 ModuleExport size_t RegisterUYVYImage(void)
218 entry=AcquireMagickInfo("UYVY","PAL","16bit/pixel interleaved YUV");
219 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
220 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
221 entry->flags^=CoderAdjoinFlag;
222 entry->flags|=CoderRawSupportFlag;
223 entry->flags|=CoderEndianSupportFlag;
224 (void) RegisterMagickInfo(entry);
225 entry=AcquireMagickInfo("UYVY","UYVY","16bit/pixel interleaved YUV");
226 entry->decoder=(DecodeImageHandler *) ReadUYVYImage;
227 entry->encoder=(EncodeImageHandler *) WriteUYVYImage;
228 entry->flags^=CoderAdjoinFlag;
229 entry->flags|=CoderRawSupportFlag;
230 entry->flags|=CoderEndianSupportFlag;
231 (void) RegisterMagickInfo(entry);
232 return(MagickImageCoderSignature);
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % U n r e g i s t e r U Y V Y I m a g e %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % UnregisterUYVYImage() removes format registrations made by the
247 % UYVY module from the list of supported formats.
249 % The format of the UnregisterUYVYImage method is:
251 % UnregisterUYVYImage(void)
254 ModuleExport void UnregisterUYVYImage(void)
256 (void) UnregisterMagickInfo("PAL");
257 (void) UnregisterMagickInfo("UYVY");
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % W r i t e U Y V Y I m a g e %
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
275 % The format of the WriteUYVYImage method is:
277 % MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
278 % Image *image,ExceptionInfo *exception)
280 % A description of each parameter follows.
282 % o image_info: the image info.
284 % o image: The image. Implicit assumption: number of columns is even.
286 % o exception: return any errors or warnings in this structure.
289 static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
290 Image *image,ExceptionInfo *exception)
302 register const Quantum
312 Open output image file.
314 assert(image_info != (const ImageInfo *) NULL);
315 assert(image_info->signature == MagickCoreSignature);
316 assert(image != (Image *) NULL);
317 assert(image->signature == MagickCoreSignature);
318 if (image->debug != MagickFalse)
319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
320 if ((image->columns % 2) != 0)
322 assert(exception != (ExceptionInfo *) NULL);
323 assert(exception->signature == MagickCoreSignature);
324 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
325 if (status == MagickFalse)
328 Accumulate two pixels, then output.
330 uyvy_image=CloneImage(image,0,0,MagickTrue,exception);
331 if (uyvy_image == (Image *) NULL)
333 (void) TransformImageColorspace(uyvy_image,YCbCrColorspace,exception);
335 (void) memset(&pixel,0,sizeof(PixelInfo));
336 for (y=0; y < (ssize_t) image->rows; y++)
338 p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,exception);
339 if (p == (const Quantum *) NULL)
341 for (x=0; x < (ssize_t) image->columns; x++)
343 if (full != MagickFalse)
345 pixel.green=(pixel.green+GetPixelGreen(uyvy_image,p))/2;
346 pixel.blue=(pixel.blue+GetPixelBlue(uyvy_image,p))/2;
347 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.green));
348 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.red));
349 (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.blue));
350 (void) WriteBlobByte(image,ScaleQuantumToChar(
351 GetPixelRed(uyvy_image,p)));
353 pixel.red=(double) GetPixelRed(uyvy_image,p);
354 pixel.green=(double) GetPixelGreen(uyvy_image,p);
355 pixel.blue=(double) GetPixelBlue(uyvy_image,p);
356 full=full == MagickFalse ? MagickTrue : MagickFalse;
357 p+=GetPixelChannels(uyvy_image);
359 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
361 if (status == MagickFalse)
364 uyvy_image=DestroyImage(uyvy_image);
365 (void) CloseBlob(image);