Debian Codegraph @

coders/flif.c

raw symbols SCIP
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % FFFFF L IIIII FFFFF %
7 % F L I F %
8 % FFF L I FFF %
9 % F L I F %
10 % F LLLLL IIIII F %
11 % %
12 % %
13 % Read/Write Free Lossless Image Format %
14 % %
15 % Software Design %
16 % Jon Sneyers %
17 % April 2016 %
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/license/ %
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/artifact.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/client.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/display.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/option.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/resource_.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
65 #include "MagickCore/module.h"
66 #include "MagickCore/utility.h"
67 #include "MagickCore/xwindow.h"
68 #include "MagickCore/xwindow-private.h"
69 #if defined(MAGICKCORE_FLIF_DELEGATE)
70 #include <flif.h>
71 #endif
72
73 /*
74 Forward declarations.
75 */
76 #if defined(MAGICKCORE_FLIF_DELEGATE)
77 static MagickBooleanType
78 WriteFLIFImage(const ImageInfo *,Image *,ExceptionInfo *);
79 #endif
80
81 #if defined(MAGICKCORE_FLIF_DELEGATE)
82 /*
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % %
85 % %
86 % %
87 % R e a d F L I F I m a g e %
88 % %
89 % %
90 % %
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %
93 % ReadFLIFImage() reads an image in the FLIF image format.
94 %
95 % The format of the ReadFLIFImage method is:
96 %
97 % Image *ReadFLIFImage(const ImageInfo *image_info,
98 % ExceptionInfo *exception)
99 %
100 % A description of each parameter follows:
101 %
102 % o image_info: the image info.
103 %
104 % o exception: return any errors or warnings in this structure.
105 %
106 */
107 static Image *ReadFLIFImage(const ImageInfo *image_info,
108 ExceptionInfo *exception)
109 {
110 FLIF_DECODER
111 *flifdec;
112
113 FLIF_IMAGE
114 *flifimage;
115
116 Image
117 *image;
118
119 MagickBooleanType
120 status;
121
122 Quantum
123 *q;
124
125 ssize_t
126 x;
127
128 unsigned short
129 *p;
130
131 size_t
132 count,
133 image_count,
134 length;
135
136 ssize_t
137 y;
138
139 unsigned char
140 *stream;
141
142 unsigned short
143 *pixels;
144
145 /*
146 Open image file.
147 */
148 assert(image_info != (const ImageInfo *) NULL);
149 assert(image_info->signature == MagickCoreSignature);
150 assert(exception != (ExceptionInfo *) NULL);
151 assert(exception->signature == MagickCoreSignature);
152 if (IsEventLogging() != MagickFalse)
153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
154 image_info->filename);
155 image=AcquireImage(image_info,exception);
156 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
157 if (status == MagickFalse)
158 {
159 image=DestroyImageList(image);
160 return((Image *) NULL);
161 }
162 length=(size_t) GetBlobSize(image);
163 stream=(unsigned char *) AcquireQuantumMemory(length,sizeof(*stream));
164 if (stream == (unsigned char *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 count=ReadBlob(image,length,stream);
167 if (count != length)
168 {
169 stream=(unsigned char *) RelinquishMagickMemory(stream);
170 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
171 }
172 flifdec=flif_create_decoder();
173 if (image_info->quality != UndefinedCompressionQuality)
174 flif_decoder_set_quality(flifdec,(int32_t) image_info->quality);
175 if (!flif_decoder_decode_memory(flifdec,stream,length))
176 {
177 flif_destroy_decoder(flifdec);
178 ThrowReaderException(CorruptImageError,"CorruptImage");
179 }
180 image_count=flif_decoder_num_images(flifdec);
181 if (AcquireMagickResource(ListLengthResource,image_count) == MagickFalse)
182 ThrowFileException(exception,ResourceLimitError,"ListLengthExceedsLimit",
183 image->filename);
184 flifimage=flif_decoder_get_image(flifdec,0);
185 length=sizeof(unsigned short)*4*flif_image_get_width(flifimage);
186 pixels=(unsigned short *) AcquireQuantumMemory(1,length);
187 if (pixels == (unsigned short *) NULL)
188 {
189 flif_destroy_decoder(flifdec);
190 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
191 }
192
193 for (count=0; count < image_count; count++)
194 {
195 if (count > 0)
196 {
197 /*
198 Allocate next image structure.
199 */
200 AcquireNextImage(image_info,image,exception);
201 if (GetNextImageInList(image) == (Image *) NULL)
202 {
203 status=MagickFalse;
204 break;
205 }
206 image=SyncNextImageInList(image);
207 }
208 flifimage=flif_decoder_get_image(flifdec,count);
209 image->columns=(size_t) flif_image_get_width(flifimage);
210 image->rows=(size_t) flif_image_get_height(flifimage);
211 image->depth=flif_image_get_depth(flifimage);
212 image->alpha_trait=(flif_image_get_nb_channels(flifimage) > 3 ?
213 BlendPixelTrait : UndefinedPixelTrait);
214 image->delay=flif_image_get_frame_delay(flifimage);
215 image->ticks_per_second=1000;
216 image->scene=count;
217 image->dispose=BackgroundDispose;
218 for (y=0; y < (ssize_t) image->rows; y++)
219 {
220 flif_image_read_row_RGBA16(flifimage,(uint32_t) y,pixels,length);
221 p=pixels;
222 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
223 if (q == (Quantum *) NULL)
224 break;
225 for (x=0; x < (ssize_t) image->columns; x++)
226 {
227 SetPixelRed(image,ScaleShortToQuantum(*p++),q);
228 SetPixelGreen(image,ScaleShortToQuantum(*p++),q);
229 SetPixelBlue(image,ScaleShortToQuantum(*p++),q);
230 SetPixelAlpha(image,ScaleShortToQuantum(*p++),q);
231 q+=(ptrdiff_t) GetPixelChannels(image);
232 }
233 if (SyncAuthenticPixels(image,exception) == MagickFalse)
234 break;
235 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
236 image->rows);
237 if (status == MagickFalse)
238 break;
239 }
240 }
241 flif_destroy_decoder(flifdec);
242 pixels=(unsigned short *) RelinquishMagickMemory(pixels);
243 if (status == MagickFalse)
244 return(DestroyImageList(image));
245 return(image);
246 }
247 #endif
248
249 /*
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % %
252 % %
253 % %
254 % I s F L I F %
255 % %
256 % %
257 % %
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 %
260 % IsFLIF() returns MagickTrue if the image format type, identified by the
261 % magick string, is FLIF.
262 %
263 % The format of the IsFLIF method is:
264 %
265 % MagickBooleanType IsFLIF(const unsigned char *magick,
266 % const size_t length)
267 %
268 % A description of each parameter follows:
269 %
270 % o magick: compare image format pattern against these bytes.
271 %
272 % o length: Specifies the length of the magick string.
273 %
274 */
275 static MagickBooleanType IsFLIF(const unsigned char *magick,
276 const size_t length)
277 {
278 if (length < 4)
279 return(MagickFalse);
280 if (LocaleNCompare((char *) magick,"FLIF",4) == 0)
281 return(MagickTrue);
282 return(MagickFalse);
283 }
284
285 /*
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % %
288 % %
289 % %
290 % R e g i s t e r F L I F I m a g e %
291 % %
292 % %
293 % %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 %
296 % RegisterFLIFImage() adds attributes for the FLIF image format to
297 % the list of supported formats. The attributes include the image format
298 % tag, a method to read and/or write the format, whether the format
299 % supports the saving of more than one frame to the same file or blob,
300 % whether the format supports native in-memory I/O, and a brief
301 % description of the format.
302 %
303 % The format of the RegisterFLIFImage method is:
304 %
305 % size_t RegisterFLIFImage(void)
306 %
307 */
308 ModuleExport size_t RegisterFLIFImage(void)
309 {
310 char
311 version[MagickPathExtent];
312
313 MagickInfo
314 *entry;
315
316 *version='\0';
317 entry=AcquireMagickInfo("FLIF","FLIF","Free Lossless Image Format");
318 #if defined(MAGICKCORE_FLIF_DELEGATE)
319 entry->decoder=(DecodeImageHandler *) ReadFLIFImage;
320 entry->encoder=(EncodeImageHandler *) WriteFLIFImage;
321 (void) FormatLocaleString(version,MagickPathExtent,"libflif %d.%d.%d [%04X]",
322 (FLIF_VERSION >> 16) & 0xff,
323 (FLIF_VERSION >> 8) & 0xff,
324 (FLIF_VERSION >> 0) & 0xff,FLIF_ABI_VERSION);
325 #endif
326 entry->mime_type=ConstantString("image/flif");
327 entry->magick=(IsImageFormatHandler *) IsFLIF;
328 if (*version != '\0')
329 entry->version=ConstantString(version);
330 (void) RegisterMagickInfo(entry);
331 return(MagickImageCoderSignature);
332 }
333
334 /*
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % %
337 % %
338 % %
339 % U n r e g i s t e r F L I F I m a g e %
340 % %
341 % %
342 % %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 %
345 % UnregisterFLIFImage() removes format registrations made by the FLIF module
346 % from the list of supported formats.
347 %
348 % The format of the UnregisterFLIFImage method is:
349 %
350 % UnregisterFLIFImage(void)
351 %
352 */
353 ModuleExport void UnregisterFLIFImage(void)
354 {
355 (void) UnregisterMagickInfo("FLIF");
356 }
357
358 #if defined(MAGICKCORE_FLIF_DELEGATE)
359 /*
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 % %
362 % %
363 % %
364 % W r i t e F L I F I m a g e %
365 % %
366 % %
367 % %
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 %
370 % WriteFLIFImage() writes an image in the FLIF image format.
371 %
372 % The format of the WriteFLIFImage method is:
373 %
374 % MagickBooleanType WriteFLIFImage(const ImageInfo *image_info,
375 % Image *image)
376 %
377 % A description of each parameter follows.
378 %
379 % o image_info: the image info.
380 %
381 % o image: The image.
382 %
383 */
384 static MagickBooleanType WriteFLIFImage(const ImageInfo *image_info,
385 Image *image, ExceptionInfo *exception)
386 {
387 const Quantum
388 *magick_restrict p;
389
390 FLIF_ENCODER
391 *flifenc;
392
393 FLIF_IMAGE
394 *flifimage;
395
396 int
397 flif_status;
398
399 MagickBooleanType
400 status;
401
402 MagickOffsetType
403 scene;
404
405 unsigned char
406 *magick_restrict qc;
407
408 unsigned short
409 *magick_restrict qs;
410
411 size_t
412 columns,
413 length,
414 number_scenes,
415 rows;
416
417 ssize_t
418 x,
419 y;
420
421 void
422 *buffer,
423 *pixels;
424
425 assert(image_info != (const ImageInfo *) NULL);
426 assert(image_info->signature == MagickCoreSignature);
427 assert(image != (Image *) NULL);
428 assert(image->signature == MagickCoreSignature);
429 if (IsEventLogging() != MagickFalse)
430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
431 if ((image->columns > 0xFFFF) || (image->rows > 0xFFFF))
432 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
433 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
434 if (status == MagickFalse)
435 return(status);
436 flifenc=flif_create_encoder();
437 if (image_info->quality != UndefinedCompressionQuality)
438 flif_encoder_set_lossy(flifenc,3*(100-(int32_t) image_info->quality));
439
440 /* relatively fast encoding */
441 flif_encoder_set_learn_repeat(flifenc,1);
442 flif_encoder_set_split_threshold(flifenc,5461*8*5);
443
444 columns=image->columns;
445 rows=image->rows;
446
447 /* Convert image to FLIFIMAGE */
448 if (image->depth > 8)
449 {
450 flifimage=flif_create_image_HDR((uint32_t) image->columns,
451 (uint32_t) image->rows);
452 length=sizeof(unsigned short)*4*image->columns;
453 }
454 else
455 {
456 flifimage=flif_create_image((uint32_t) image->columns,
457 (uint32_t) image->rows);
458 length=sizeof(unsigned char)*4*image->columns;
459 }
460 if (flifimage == (FLIF_IMAGE *) NULL)
461 {
462 flif_destroy_encoder(flifenc);
463 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
464 }
465 pixels=AcquireMagickMemory(length);
466 if (pixels == (void *) NULL)
467 {
468 flif_destroy_image(flifimage);
469 flif_destroy_encoder(flifenc);
470 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
471 }
472 scene=0;
473 number_scenes=GetImageListLength(image);
474 do
475 {
476 for (y=0; y < (ssize_t) image->rows; y++)
477 {
478 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
479 if (p == (Quantum *) NULL)
480 break;
481
482 if (image->depth > 8)
483 {
484 qs=(unsigned short *) pixels;
485 for (x=0; x < (ssize_t) image->columns; x++)
486 {
487 *qs++=ScaleQuantumToShort(GetPixelRed(image,p));
488 *qs++=ScaleQuantumToShort(GetPixelGreen(image,p));
489 *qs++=ScaleQuantumToShort(GetPixelBlue(image,p));
490 if (image->alpha_trait != UndefinedPixelTrait)
491 *qs++=ScaleQuantumToShort(GetPixelAlpha(image,p));
492 else
493 *qs++=0xFFFF;
494 p+=(ptrdiff_t) GetPixelChannels(image);
495 }
496 flif_image_write_row_RGBA16(flifimage,(uint32_t) y,pixels,length);
497 }
498 else
499 {
500 qc=(unsigned char *) pixels;
501 for (x=0; x < (ssize_t) image->columns; x++)
502 {
503 *qc++=ScaleQuantumToChar(GetPixelRed(image,p));
504 *qc++=ScaleQuantumToChar(GetPixelGreen(image,p));
505 *qc++=ScaleQuantumToChar(GetPixelBlue(image,p));
506 if (image->alpha_trait != UndefinedPixelTrait)
507 *qc++=ScaleQuantumToChar(GetPixelAlpha(image,p));
508 else
509 *qc++=0xFF;
510 p+=(ptrdiff_t) GetPixelChannels(image);
511 }
512 flif_image_write_row_RGBA8(flifimage,(uint32_t) y,pixels,length);
513 }
514 }
515 flif_image_set_frame_delay(flifimage,(uint32_t) (image->delay*100/
516 image->ticks_per_second));
517 flif_encoder_add_image(flifenc,flifimage);
518 if (GetNextImageInList(image) == (Image *) NULL)
519 break;
520 image=SyncNextImageInList(image);
521 if ((columns != image->columns) || (rows != image->rows))
522 {
523 flif_destroy_image(flifimage);
524 flif_destroy_encoder(flifenc);
525 pixels=RelinquishMagickMemory(pixels);
526 ThrowWriterException(ImageError,"FramesNotSameDimensions");
527 }
528 scene++;
529 status=SetImageProgress(image,SaveImagesTag,scene,number_scenes);
530 if (status == MagickFalse)
531 break;
532 } while (image_info->adjoin != MagickFalse);
533 flif_destroy_image(flifimage);
534 pixels=RelinquishMagickMemory(pixels);
535 flif_status=flif_encoder_encode_memory(flifenc,&buffer,&length);
536 if (flif_status)
537 WriteBlob(image,length,buffer);
538 CloseBlob(image);
539 flif_destroy_encoder(flifenc);
540 buffer=RelinquishMagickMemory(buffer);
541 return(flif_status == 0 ? MagickFalse : MagickTrue);
542 }
543 #endif