]> granicus.if.org Git - imagemagick/blob - coders/uyvy.c
0bd1ff4c28d5d0b87a2a7e2d51370c4ccf18c098
[imagemagick] / coders / uyvy.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
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 \f
62 /*
63   Forward declarations.
64 */
65 static MagickBooleanType
66   WriteUYVYImage(const ImageInfo *,Image *);
67 \f
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 static Image *ReadUYVYImage(const ImageInfo *image_info,
96   ExceptionInfo *exception)
97 {
98   Image
99     *image;
100
101   MagickBooleanType
102     status;
103
104   register ssize_t
105     x;
106
107   register PixelPacket
108     *q;
109
110   ssize_t
111     y;
112
113   unsigned char
114     u,
115     v,
116     y1,
117     y2;
118
119   /*
120     Open image file.
121   */
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)
133     image->columns++;
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",
140       image->filename);
141   image->depth=8;
142   if (image_info->ping != MagickFalse)
143     {
144       (void) CloseBlob(image);
145       return(GetFirstImageInList(image));
146     }
147   /*
148     Accumulate UYVY, then unpack into two pixels.
149   */
150   for (y=0; y < (ssize_t) image->rows; y++)
151   {
152     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
153     if (q == (PixelPacket *) NULL)
154       break;
155     for (x=0; x < (ssize_t) (image->columns >> 1); x++)
156     {
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));
164       q++;
165       SetRedPixelComponent(q,ScaleCharToQuantum(y2));
166       SetGreenPixelComponent(q,ScaleCharToQuantum(u));
167       SetBluePixelComponent(q,ScaleCharToQuantum(v));
168       q++;
169     }
170     if (SyncAuthenticPixels(image,exception) == MagickFalse)
171       break;
172     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
173       image->rows);
174     if (status == MagickFalse)
175       break;
176   }
177   image->colorspace=YCbCrColorspace;
178   if (EOFBlob(image) != MagickFalse)
179     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
180       image->filename);
181   (void) CloseBlob(image);
182   return(GetFirstImageInList(image));
183 }
184 \f
185 /*
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 %                                                                             %
188 %                                                                             %
189 %                                                                             %
190 %   R e g i s t e r U Y V Y I m a g e                                         %
191 %                                                                             %
192 %                                                                             %
193 %                                                                             %
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 %
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.
202 %
203 %  The format of the RegisterUYVYImage method is:
204 %
205 %      size_t RegisterUYVYImage(void)
206 %
207 */
208 ModuleExport size_t RegisterUYVYImage(void)
209 {
210   MagickInfo
211     *entry;
212
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);
232 }
233 \f
234 /*
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 %                                                                             %
237 %                                                                             %
238 %                                                                             %
239 %   U n r e g i s t e r U Y V Y I m a g e                                     %
240 %                                                                             %
241 %                                                                             %
242 %                                                                             %
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 %
245 %  UnregisterUYVYImage() removes format registrations made by the
246 %  UYVY module from the list of supported formats.
247 %
248 %  The format of the UnregisterUYVYImage method is:
249 %
250 %      UnregisterUYVYImage(void)
251 %
252 */
253 ModuleExport void UnregisterUYVYImage(void)
254 {
255   (void) UnregisterMagickInfo("PAL");
256   (void) UnregisterMagickInfo("UYVY");
257 }
258 \f
259 /*
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 %                                                                             %
262 %                                                                             %
263 %                                                                             %
264 %   W r i t e U Y V Y I m a g e                                               %
265 %                                                                             %
266 %                                                                             %
267 %                                                                             %
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 %
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.
273 %
274 %  The format of the WriteUYVYImage method is:
275 %
276 %      MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
277 %        Image *image)
278 %
279 %  A description of each parameter follows.
280 %
281 %    o image_info: the image info.
282 %
283 %    o image:  The image.
284 %      Implicit assumption: number of columns is even.
285 %
286 */
287 static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
288   Image *image)
289 {
290   MagickPixelPacket
291     pixel;
292
293   Image
294     *uyvy_image;
295
296   MagickBooleanType
297     full,
298     status;
299
300   register const PixelPacket
301     *p;
302
303   register ssize_t
304     x;
305
306   ssize_t
307     y;
308
309   /*
310     Open output image file.
311   */
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)
319     image->columns++;
320   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
321   if (status == MagickFalse)
322     return(status);
323   /*
324     Accumulate two pixels, then output.
325   */
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);
330   full=MagickFalse;
331   (void) ResetMagickMemory(&pixel,0,sizeof(MagickPixelPacket));
332   for (y=0; y < (ssize_t) image->rows; y++)
333   {
334     p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,&image->exception);
335     if (p == (const PixelPacket *) NULL)
336       break;
337     for (x=0; x < (ssize_t) image->columns; x++)
338     {
339       if (full != MagickFalse)
340         {
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)));
348         }
349       pixel.red=(double) GetRedPixelComponent(p);
350       pixel.green=(double) GetGreenPixelComponent(p);
351       pixel.blue=(double) GetBluePixelComponent(p);
352       full=full == MagickFalse ? MagickTrue : MagickFalse;
353       p++;
354     }
355     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
356       image->rows);
357     if (status == MagickFalse)
358       break;
359   }
360   uyvy_image=DestroyImage(uyvy_image);
361   (void) CloseBlob(image);
362   return(MagickTrue);
363 }