]> granicus.if.org Git - imagemagick/blob - coders/uyvy.c
(no commit message)
[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 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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 "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"
62 \f
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteUYVYImage(const ImageInfo *,Image *,ExceptionInfo *);
68 \f
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 %
95 */
96 static Image *ReadUYVYImage(const ImageInfo *image_info,
97   ExceptionInfo *exception)
98 {
99   Image
100     *image;
101
102   MagickBooleanType
103     status;
104
105   register ssize_t
106     x;
107
108   register Quantum
109     *q;
110
111   ssize_t
112     y;
113
114   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,exception);
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);
139   if (DiscardBlobBytes(image,image->offset) == MagickFalse)
140     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
141       image->filename);
142   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   */
151   for (y=0; y < (ssize_t) image->rows; y++)
152   {
153     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
154     if (q == (Quantum *) NULL)
155       break;
156     for (x=0; x < (ssize_t) (image->columns >> 1); x++)
157     {
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       SetPixelRed(image,ScaleCharToQuantum(y1),q);
163       SetPixelGreen(image,ScaleCharToQuantum(u),q);
164       SetPixelBlue(image,ScaleCharToQuantum(v),q);
165       q+=GetPixelChannels(image);
166       SetPixelRed(image,ScaleCharToQuantum(y2),q);
167       SetPixelGreen(image,ScaleCharToQuantum(u),q);
168       SetPixelBlue(image,ScaleCharToQuantum(v),q);
169       q+=GetPixelChannels(image);
170     }
171     if (SyncAuthenticPixels(image,exception) == MagickFalse)
172       break;
173     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
174       image->rows);
175     if (status == MagickFalse)
176       break;
177   }
178   SetImageColorspace(image,YCbCrColorspace,exception);
179   if (EOFBlob(image) != MagickFalse)
180     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
181       image->filename);
182   (void) CloseBlob(image);
183   return(GetFirstImageInList(image));
184 }
185 \f
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 %
206 %      size_t RegisterUYVYImage(void)
207 %
208 */
209 ModuleExport size_t RegisterUYVYImage(void)
210 {
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 \f
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 */
254 ModuleExport void UnregisterUYVYImage(void)
255 {
256   (void) UnregisterMagickInfo("PAL");
257   (void) UnregisterMagickInfo("UYVY");
258 }
259 \f
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,ExceptionInfo *exception)
279 %
280 %  A description of each parameter follows.
281 %
282 %    o image_info: the image info.
283 %
284 %    o image:  The image.  Implicit assumption: number of columns is even.
285 %
286 %    o exception: return any errors or warnings in this structure.
287 %
288 */
289 static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info,
290   Image *image,ExceptionInfo *exception)
291 {
292   PixelInfo
293     pixel;
294
295   Image
296     *uyvy_image;
297
298   MagickBooleanType
299     full,
300     status;
301
302   register const Quantum
303     *p;
304
305   register ssize_t
306     x;
307
308   ssize_t
309     y;
310
311   /*
312     Open output image file.
313   */
314   assert(image_info != (const ImageInfo *) NULL);
315   assert(image_info->signature == MagickSignature);
316   assert(image != (Image *) NULL);
317   assert(image->signature == MagickSignature);
318   if (image->debug != MagickFalse)
319     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
320   if ((image->columns % 2) != 0)
321     image->columns++;
322   assert(exception != (ExceptionInfo *) NULL);
323   assert(exception->signature == MagickSignature);
324   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
325   if (status == MagickFalse)
326     return(status);
327   /*
328     Accumulate two pixels, then output.
329   */
330   uyvy_image=CloneImage(image,0,0,MagickTrue,exception);
331   if (uyvy_image == (Image *) NULL)
332     return(MagickFalse);
333   (void) TransformImageColorspace(uyvy_image,YCbCrColorspace,exception);
334   full=MagickFalse;
335   (void) ResetMagickMemory(&pixel,0,sizeof(PixelInfo));
336   for (y=0; y < (ssize_t) image->rows; y++)
337   {
338     p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,exception);
339     if (p == (const Quantum *) NULL)
340       break;
341     for (x=0; x < (ssize_t) image->columns; x++)
342     {
343       if (full != MagickFalse)
344         {
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)));
352         }
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);
358     }
359     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
360       image->rows);
361     if (status == MagickFalse)
362       break;
363   }
364   uyvy_image=DestroyImage(uyvy_image);
365   (void) CloseBlob(image);
366   return(MagickTrue);
367 }