/src/imagemagick/coders/uyvy.c
Line | Count | Source (jump to first uncovered line) |
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 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 | | % https://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 "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 | | |
63 | | /* |
64 | | Forward declarations. |
65 | | */ |
66 | | static MagickBooleanType |
67 | | WriteUYVYImage(const ImageInfo *,Image *,ExceptionInfo *); |
68 | | |
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 | 579 | { |
99 | 579 | Image |
100 | 579 | *image; |
101 | | |
102 | 579 | MagickBooleanType |
103 | 579 | status; |
104 | | |
105 | 579 | ssize_t |
106 | 579 | x; |
107 | | |
108 | 579 | Quantum |
109 | 579 | *q; |
110 | | |
111 | 579 | ssize_t |
112 | 579 | y; |
113 | | |
114 | 579 | unsigned char |
115 | 579 | u, |
116 | 579 | v, |
117 | 579 | y1, |
118 | 579 | y2; |
119 | | |
120 | | /* |
121 | | Open image file. |
122 | | */ |
123 | 579 | assert(image_info != (const ImageInfo *) NULL); |
124 | 579 | assert(image_info->signature == MagickCoreSignature); |
125 | 579 | assert(exception != (ExceptionInfo *) NULL); |
126 | 579 | assert(exception->signature == MagickCoreSignature); |
127 | 579 | if (IsEventLogging() != MagickFalse) |
128 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
129 | 0 | image_info->filename); |
130 | 579 | image=AcquireImage(image_info,exception); |
131 | 579 | if ((image->columns == 0) || (image->rows == 0)) |
132 | 537 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
133 | 42 | if ((image->columns % 2) != 0) |
134 | 12 | image->columns++; |
135 | 42 | (void) CopyMagickString(image->filename,image_info->filename, |
136 | 42 | MagickPathExtent); |
137 | 42 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
138 | 42 | if (status == MagickFalse) |
139 | 0 | return(DestroyImage(image)); |
140 | 42 | if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse) |
141 | 0 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
142 | 42 | image->filename); |
143 | 42 | image->depth=8; |
144 | 42 | if (image_info->ping != MagickFalse) |
145 | 0 | { |
146 | 0 | (void) CloseBlob(image); |
147 | 0 | return(GetFirstImageInList(image)); |
148 | 0 | } |
149 | 42 | status=SetImageExtent(image,image->columns,image->rows,exception); |
150 | 42 | if (status == MagickFalse) |
151 | 26 | return(DestroyImageList(image)); |
152 | | /* |
153 | | Accumulate UYVY, then unpack into two pixels. |
154 | | */ |
155 | 2.64k | for (y=0; y < (ssize_t) image->rows; y++) |
156 | 2.62k | { |
157 | 2.62k | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
158 | 2.62k | if (q == (Quantum *) NULL) |
159 | 0 | break; |
160 | 1.04M | for (x=0; x < (ssize_t) (image->columns >> 1); x++) |
161 | 1.03M | { |
162 | 1.03M | u=(unsigned char) ReadBlobByte(image); |
163 | 1.03M | y1=(unsigned char) ReadBlobByte(image); |
164 | 1.03M | v=(unsigned char) ReadBlobByte(image); |
165 | 1.03M | y2=(unsigned char) ReadBlobByte(image); |
166 | 1.03M | SetPixelRed(image,ScaleCharToQuantum(y1),q); |
167 | 1.03M | SetPixelGreen(image,ScaleCharToQuantum(u),q); |
168 | 1.03M | SetPixelBlue(image,ScaleCharToQuantum(v),q); |
169 | 1.03M | q+=(ptrdiff_t) GetPixelChannels(image); |
170 | 1.03M | SetPixelRed(image,ScaleCharToQuantum(y2),q); |
171 | 1.03M | SetPixelGreen(image,ScaleCharToQuantum(u),q); |
172 | 1.03M | SetPixelBlue(image,ScaleCharToQuantum(v),q); |
173 | 1.03M | q+=(ptrdiff_t) GetPixelChannels(image); |
174 | 1.03M | } |
175 | 2.62k | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
176 | 0 | break; |
177 | 2.62k | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
178 | 2.62k | image->rows); |
179 | 2.62k | if (status == MagickFalse) |
180 | 0 | break; |
181 | 2.62k | } |
182 | 16 | SetImageColorspace(image,YCbCrColorspace,exception); |
183 | 16 | if (EOFBlob(image) != MagickFalse) |
184 | 15 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
185 | 16 | image->filename); |
186 | 16 | if (CloseBlob(image) == MagickFalse) |
187 | 0 | status=MagickFalse; |
188 | 16 | if (status == MagickFalse) |
189 | 0 | return(DestroyImageList(image)); |
190 | 16 | return(GetFirstImageInList(image)); |
191 | 16 | } |
192 | | |
193 | | /* |
194 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
195 | | % % |
196 | | % % |
197 | | % % |
198 | | % R e g i s t e r U Y V Y I m a g e % |
199 | | % % |
200 | | % % |
201 | | % % |
202 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
203 | | % |
204 | | % RegisterUYVYImage() adds attributes for the UYVY image format to |
205 | | % the list of supported formats. The attributes include the image format |
206 | | % tag, a method to read and/or write the format, whether the format |
207 | | % supports the saving of more than one frame to the same file or blob, |
208 | | % whether the format supports native in-memory I/O, and a brief |
209 | | % description of the format. |
210 | | % |
211 | | % The format of the RegisterUYVYImage method is: |
212 | | % |
213 | | % size_t RegisterUYVYImage(void) |
214 | | % |
215 | | */ |
216 | | ModuleExport size_t RegisterUYVYImage(void) |
217 | 9 | { |
218 | 9 | MagickInfo |
219 | 9 | *entry; |
220 | | |
221 | 9 | entry=AcquireMagickInfo("UYVY","PAL","16bit/pixel interleaved YUV"); |
222 | 9 | entry->decoder=(DecodeImageHandler *) ReadUYVYImage; |
223 | 9 | entry->encoder=(EncodeImageHandler *) WriteUYVYImage; |
224 | 9 | entry->flags^=CoderAdjoinFlag; |
225 | 9 | entry->flags|=CoderRawSupportFlag; |
226 | 9 | entry->flags|=CoderEndianSupportFlag; |
227 | 9 | (void) RegisterMagickInfo(entry); |
228 | 9 | entry=AcquireMagickInfo("UYVY","UYVY","16bit/pixel interleaved YUV"); |
229 | 9 | entry->decoder=(DecodeImageHandler *) ReadUYVYImage; |
230 | 9 | entry->encoder=(EncodeImageHandler *) WriteUYVYImage; |
231 | 9 | entry->flags^=CoderAdjoinFlag; |
232 | 9 | entry->flags|=CoderRawSupportFlag; |
233 | 9 | entry->flags|=CoderEndianSupportFlag; |
234 | 9 | (void) RegisterMagickInfo(entry); |
235 | 9 | return(MagickImageCoderSignature); |
236 | 9 | } |
237 | | |
238 | | /* |
239 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
240 | | % % |
241 | | % % |
242 | | % % |
243 | | % U n r e g i s t e r U Y V Y I m a g e % |
244 | | % % |
245 | | % % |
246 | | % % |
247 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
248 | | % |
249 | | % UnregisterUYVYImage() removes format registrations made by the |
250 | | % UYVY module from the list of supported formats. |
251 | | % |
252 | | % The format of the UnregisterUYVYImage method is: |
253 | | % |
254 | | % UnregisterUYVYImage(void) |
255 | | % |
256 | | */ |
257 | | ModuleExport void UnregisterUYVYImage(void) |
258 | 0 | { |
259 | 0 | (void) UnregisterMagickInfo("PAL"); |
260 | 0 | (void) UnregisterMagickInfo("UYVY"); |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
265 | | % % |
266 | | % % |
267 | | % % |
268 | | % W r i t e U Y V Y I m a g e % |
269 | | % % |
270 | | % % |
271 | | % % |
272 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
273 | | % |
274 | | % WriteUYVYImage() writes an image to a file in the digital UYVY |
275 | | % format. This format, used by AccomWSD, is not dramatically higher quality |
276 | | % than the 12bit/pixel YUV format, but has better locality. |
277 | | % |
278 | | % The format of the WriteUYVYImage method is: |
279 | | % |
280 | | % MagickBooleanType WriteUYVYImage(const ImageInfo *image_info, |
281 | | % Image *image,ExceptionInfo *exception) |
282 | | % |
283 | | % A description of each parameter follows. |
284 | | % |
285 | | % o image_info: the image info. |
286 | | % |
287 | | % o image: The image. Implicit assumption: number of columns is even. |
288 | | % |
289 | | % o exception: return any errors or warnings in this structure. |
290 | | % |
291 | | */ |
292 | | static MagickBooleanType WriteUYVYImage(const ImageInfo *image_info, |
293 | | Image *image,ExceptionInfo *exception) |
294 | 0 | { |
295 | 0 | PixelInfo |
296 | 0 | pixel; |
297 | |
|
298 | 0 | Image |
299 | 0 | *uyvy_image; |
300 | |
|
301 | 0 | MagickBooleanType |
302 | 0 | full, |
303 | 0 | status; |
304 | |
|
305 | 0 | const Quantum |
306 | 0 | *p; |
307 | |
|
308 | 0 | ssize_t |
309 | 0 | x; |
310 | |
|
311 | 0 | ssize_t |
312 | 0 | y; |
313 | | |
314 | | /* |
315 | | Open output image file. |
316 | | */ |
317 | 0 | assert(image_info != (const ImageInfo *) NULL); |
318 | 0 | assert(image_info->signature == MagickCoreSignature); |
319 | 0 | assert(image != (Image *) NULL); |
320 | 0 | assert(image->signature == MagickCoreSignature); |
321 | 0 | if (IsEventLogging() != MagickFalse) |
322 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
323 | 0 | if ((image->columns % 2) != 0) |
324 | 0 | image->columns++; |
325 | 0 | assert(exception != (ExceptionInfo *) NULL); |
326 | 0 | assert(exception->signature == MagickCoreSignature); |
327 | 0 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
328 | 0 | if (status == MagickFalse) |
329 | 0 | return(status); |
330 | | /* |
331 | | Accumulate two pixels, then output. |
332 | | */ |
333 | 0 | uyvy_image=CloneImage(image,0,0,MagickTrue,exception); |
334 | 0 | if (uyvy_image == (Image *) NULL) |
335 | 0 | return(MagickFalse); |
336 | 0 | (void) TransformImageColorspace(uyvy_image,YCbCrColorspace,exception); |
337 | 0 | full=MagickFalse; |
338 | 0 | (void) memset(&pixel,0,sizeof(PixelInfo)); |
339 | 0 | for (y=0; y < (ssize_t) image->rows; y++) |
340 | 0 | { |
341 | 0 | p=GetVirtualPixels(uyvy_image,0,y,image->columns,1,exception); |
342 | 0 | if (p == (const Quantum *) NULL) |
343 | 0 | break; |
344 | 0 | for (x=0; x < (ssize_t) image->columns; x++) |
345 | 0 | { |
346 | 0 | if (full != MagickFalse) |
347 | 0 | { |
348 | 0 | pixel.green=(pixel.green+(double) GetPixelGreen(uyvy_image,p))/2; |
349 | 0 | pixel.blue=(pixel.blue+(double) GetPixelBlue(uyvy_image,p))/2; |
350 | 0 | (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.green)); |
351 | 0 | (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.red)); |
352 | 0 | (void) WriteBlobByte(image,ScaleQuantumToChar((Quantum) pixel.blue)); |
353 | 0 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
354 | 0 | GetPixelRed(uyvy_image,p))); |
355 | 0 | } |
356 | 0 | pixel.red=(double) GetPixelRed(uyvy_image,p); |
357 | 0 | pixel.green=(double) GetPixelGreen(uyvy_image,p); |
358 | 0 | pixel.blue=(double) GetPixelBlue(uyvy_image,p); |
359 | 0 | full=full == MagickFalse ? MagickTrue : MagickFalse; |
360 | 0 | p+=(ptrdiff_t) GetPixelChannels(uyvy_image); |
361 | 0 | } |
362 | 0 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
363 | 0 | image->rows); |
364 | 0 | if (status == MagickFalse) |
365 | 0 | break; |
366 | 0 | } |
367 | 0 | uyvy_image=DestroyImage(uyvy_image); |
368 | 0 | if (CloseBlob(image) == MagickFalse) |
369 | 0 | status=MagickFalse; |
370 | 0 | return(status); |
371 | 0 | } |