]> granicus.if.org Git - imagemagick/blob - coders/fpx.c
e3b25507c5a897f2c2c1d75fee6a062d6bc75b99
[imagemagick] / coders / fpx.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            FFFFF  PPPP   X   X                              %
7 %                            F      P   P   X X                               %
8 %                            FFF    PPPP     X                                %
9 %                            F      P       X X                               %
10 %                            F      P      X   X                              %
11 %                                                                             %
12 %                                                                             %
13 %                     Read/Write FlashPIX 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/attribute.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/monitor.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/pixel.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/property.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #if defined(MAGICKCORE_FPX_DELEGATE)
72 #if !defined(vms) && !defined(macintosh) && !defined(MAGICKCORE_WINDOWS_SUPPORT)
73 #include <fpxlib.h>
74 #else
75 #include "Fpxlib.h"
76 #endif
77 #endif
78 \f
79 #if defined(MAGICKCORE_FPX_DELEGATE)
80 /*
81   Forward declarations.
82 */
83 static MagickBooleanType
84   WriteFPXImage(const ImageInfo *,Image *,ExceptionInfo *);
85 #endif
86 \f
87 #if defined(MAGICKCORE_FPX_DELEGATE)
88 /*
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %                                                                             %
91 %                                                                             %
92 %                                                                             %
93 %   R e a d F P X I m a g e                                                   %
94 %                                                                             %
95 %                                                                             %
96 %                                                                             %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %
99 %  ReadFPXImage() reads a FlashPix image file and returns it.  It
100 %  allocates the memory necessary for the new Image structure and returns a
101 %  pointer to the new image.  This method was contributed by BillR@corbis.com.
102 %
103 %  The format of the ReadFPXImage method is:
104 %
105 %      Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
106 %
107 %  A description of each parameter follows:
108 %
109 %    o image_info: the image info.
110 %
111 %    o exception: return any errors or warnings in this structure.
112 %
113 */
114 static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
115 {
116   FPXColorspace
117     colorspace;
118
119   FPXImageComponentDesc
120     *alpha_component,
121     *blue_component,
122     *green_component,
123     *red_component;
124
125   FPXImageDesc
126     fpx_info;
127
128   FPXImageHandle
129     *flashpix;
130
131   FPXStatus
132     fpx_status;
133
134   FPXSummaryInformation
135     summary_info;
136
137   Image
138     *image;
139
140   MagickBooleanType
141     status;
142
143   Quantum
144     index;
145
146   register ssize_t
147     i,
148     x;
149
150   register Quantum
151     *q;
152
153   register unsigned char
154     *a,
155     *b,
156     *g,
157     *r;
158
159   size_t
160     memory_limit;
161
162   ssize_t
163     y;
164
165   unsigned char
166     *pixels;
167
168   unsigned int
169     height,
170     tile_width,
171     tile_height,
172     width;
173
174   size_t
175     scene;
176
177   /*
178     Open image.
179   */
180   assert(image_info != (const ImageInfo *) NULL);
181   assert(image_info->signature == MagickSignature);
182   if (image_info->debug != MagickFalse)
183     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
184       image_info->filename);
185   assert(exception != (ExceptionInfo *) NULL);
186   assert(exception->signature == MagickSignature);
187   image=AcquireImage(image_info,exception);
188   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
189   if (status == MagickFalse)
190     {
191       image=DestroyImageList(image);
192       return((Image *) NULL);
193     }
194   (void) CloseBlob(image);
195   /*
196     Initialize FPX toolkit.
197   */
198   fpx_status=FPX_InitSystem();
199   if (fpx_status != FPX_OK)
200     ThrowReaderException(CoderError,"UnableToInitializeFPXLibrary");
201   memory_limit=20000000;
202   fpx_status=FPX_SetToolkitMemoryLimit(&memory_limit);
203   if (fpx_status != FPX_OK)
204     {
205       FPX_ClearSystem();
206       ThrowReaderException(CoderError,"UnableToInitializeFPXLibrary");
207     }
208   tile_width=64;
209   tile_height=64;
210   flashpix=(FPXImageHandle *) NULL;
211   {
212 #if defined(macintosh)
213     FSSpec
214       fsspec;
215
216     FilenameToFSSpec(image->filename,&fsspec);
217     fpx_status=FPX_OpenImageByFilename((const FSSpec &) fsspec,(char *) NULL,
218 #else
219     fpx_status=FPX_OpenImageByFilename(image->filename,(char *) NULL,
220 #endif
221       &width,&height,&tile_width,&tile_height,&colorspace,&flashpix);
222   }
223   if (fpx_status == FPX_LOW_MEMORY_ERROR)
224     {
225       FPX_ClearSystem();
226       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
227     }
228   if (fpx_status != FPX_OK)
229     {
230       FPX_ClearSystem();
231       ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
232         image->filename);
233       image=DestroyImageList(image);
234       return((Image *) NULL);
235     }
236   if (colorspace.numberOfComponents == 0)
237     {
238       FPX_ClearSystem();
239       ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
240     }
241   if (image_info->view == (char *) NULL)
242     {
243       float
244         aspect_ratio;
245
246       /*
247         Get the aspect ratio.
248       */
249       aspect_ratio=(float) width/height;
250       fpx_status=FPX_GetImageResultAspectRatio(flashpix,&aspect_ratio);
251       if (fpx_status != FPX_OK)
252         ThrowReaderException(DelegateError,"UnableToReadAspectRatio");
253       if (width != (size_t) floor((aspect_ratio*height)+0.5))
254         Swap(width,height);
255     }
256   fpx_status=FPX_GetSummaryInformation(flashpix,&summary_info);
257   if (fpx_status != FPX_OK)
258     {
259       FPX_ClearSystem();
260       ThrowReaderException(DelegateError,"UnableToReadSummaryInfo");
261     }
262   if (summary_info.title_valid)
263     if ((summary_info.title.length != 0) &&
264         (summary_info.title.ptr != (unsigned char *) NULL))
265       {
266         char
267           *label;
268
269         /*
270           Note image label.
271         */
272         label=(char *) NULL;
273         if (~summary_info.title.length >= (MaxTextExtent-1))
274           label=(char *) AcquireQuantumMemory(summary_info.title.length+
275             MaxTextExtent,sizeof(*label));
276         if (label == (char *) NULL)
277           {
278             FPX_ClearSystem();
279             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280           }
281         (void) CopyMagickString(label,(char *) summary_info.title.ptr,
282           summary_info.title.length+1);
283         (void) SetImageProperty(image,"label",label,exception);
284         label=DestroyString(label);
285       }
286   if (summary_info.comments_valid)
287     if ((summary_info.comments.length != 0) &&
288         (summary_info.comments.ptr != (unsigned char *) NULL))
289       {
290         char
291           *comments;
292
293         /*
294           Note image comment.
295         */
296         comments=(char *) NULL;
297         if (~summary_info.comments.length >= (MaxTextExtent-1))
298           comments=(char *) AcquireQuantumMemory(summary_info.comments.length+
299             MaxTextExtent,sizeof(*comments));
300         if (comments == (char *) NULL)
301           {
302             FPX_ClearSystem();
303             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
304           }
305         (void) CopyMagickString(comments,(char *) summary_info.comments.ptr,
306           summary_info.comments.length+1);
307         (void) SetImageProperty(image,"comment",comments,exception);
308         comments=DestroyString(comments);
309       }
310   /*
311     Determine resolution by scene specification.
312   */
313   for (i=1; ; i++)
314     if (((width >> i) < tile_width) || ((height >> i) < tile_height))
315       break;
316   scene=i;
317   if (image_info->number_scenes != 0)
318     while (scene > image_info->scene)
319     {
320       width>>=1;
321       height>>=1;
322       scene--;
323     }
324   if (image_info->size != (char *) NULL)
325     while ((width > image->columns) || (height > image->rows))
326     {
327       width>>=1;
328       height>>=1;
329       scene--;
330     }
331   image->depth=8;
332   image->columns=width;
333   image->rows=height;
334   if ((colorspace.numberOfComponents % 2) == 0)
335     image->alpha_trait=BlendPixelTrait;
336   if (colorspace.numberOfComponents == 1)
337     {
338       /*
339         Create linear colormap.
340       */
341       if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse)
342         {
343           FPX_ClearSystem();
344           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
345         }
346     }
347   if (image_info->ping != MagickFalse)
348     {
349       (void) FPX_CloseImage(flashpix);
350       FPX_ClearSystem();
351       return(GetFirstImageInList(image));
352     }
353   /*
354     Allocate memory for the image and pixel buffer.
355   */
356   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,(tile_height+
357     1UL)*colorspace.numberOfComponents*sizeof(*pixels));
358   if (pixels == (unsigned char *) NULL)
359     {
360       FPX_ClearSystem();
361       (void) FPX_CloseImage(flashpix);
362       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
363     }
364   /*
365     Initialize FlashPix image description.
366   */
367   fpx_info.numberOfComponents=colorspace.numberOfComponents;
368   for (i=0; i < 4; i++)
369   {
370     fpx_info.components[i].myColorType.myDataType=DATA_TYPE_UNSIGNED_BYTE;
371     fpx_info.components[i].horzSubSampFactor=1;
372     fpx_info.components[i].vertSubSampFactor=1;
373     fpx_info.components[i].columnStride=fpx_info.numberOfComponents;
374     fpx_info.components[i].lineStride=image->columns*
375       fpx_info.components[i].columnStride;
376     fpx_info.components[i].theData=pixels+i;
377   }
378   fpx_info.components[0].myColorType.myColor=fpx_info.numberOfComponents > 2 ?
379     NIFRGB_R : MONOCHROME;
380   red_component=(&fpx_info.components[0]);
381   fpx_info.components[1].myColorType.myColor=fpx_info.numberOfComponents > 2 ?
382     NIFRGB_G : ALPHA;
383   green_component=(&fpx_info.components[1]);
384   fpx_info.components[2].myColorType.myColor=NIFRGB_B;
385   blue_component=(&fpx_info.components[2]);
386   fpx_info.components[3].myColorType.myColor=ALPHA;
387   alpha_component=(&fpx_info.components[fpx_info.numberOfComponents-1]);
388   FPX_SetResampleMethod(FPX_LINEAR_INTERPOLATION);
389   /*
390     Initialize image pixels.
391   */
392   for (y=0; y < (ssize_t) image->rows; y++)
393   {
394     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
395     if (q == (Quantum *) NULL)
396       break;
397     if ((y % tile_height) == 0)
398       {
399         /*
400           Read FPX image tile (with or without viewing affine)..
401         */
402         if (image_info->view != (char *) NULL)
403           fpx_status=FPX_ReadImageRectangle(flashpix,0,y,image->columns,y+
404             tile_height-1,scene,&fpx_info);
405         else
406           fpx_status=FPX_ReadImageTransformRectangle(flashpix,0.0F,
407             (float) y/image->rows,(float) image->columns/image->rows,
408             (float) (y+tile_height-1)/image->rows,(ssize_t) image->columns,
409             (ssize_t) tile_height,&fpx_info);
410         if (fpx_status == FPX_LOW_MEMORY_ERROR)
411           {
412             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
413             (void) FPX_CloseImage(flashpix);
414             FPX_ClearSystem();
415             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
416           }
417       }
418     /*
419       Transfer a FPX pixels.
420     */
421     r=red_component->theData+(y % tile_height)*red_component->lineStride;
422     g=green_component->theData+(y % tile_height)*green_component->lineStride;
423     b=blue_component->theData+(y % tile_height)*blue_component->lineStride;
424     a=alpha_component->theData+(y % tile_height)*alpha_component->lineStride;
425     for (x=0; x < (ssize_t) image->columns; x++)
426     {
427       if (fpx_info.numberOfComponents > 2)
428         {
429           SetPixelRed(image,ScaleCharToQuantum(*r),q);
430           SetPixelGreen(image,ScaleCharToQuantum(*g),q);
431           SetPixelBlue(image,ScaleCharToQuantum(*b),q);
432         }
433       else
434         {
435           index=ScaleCharToQuantum(*r);
436           SetPixelBlack(image,index,q);
437           SetPixelRed(image,index,q);
438           SetPixelGreen(image,index,q);
439           SetPixelBlue(image,index,q);
440         }
441       SetPixelAlpha(image,OpaqueAlpha,q);
442       if (image->alpha_trait == BlendPixelTrait)
443         SetPixelAlpha(image,ScaleCharToQuantum(*a),q);
444       q+=GetPixelChannels(image);
445       r+=red_component->columnStride;
446       g+=green_component->columnStride;
447       b+=blue_component->columnStride;
448       a+=alpha_component->columnStride;
449     }
450     if (SyncAuthenticPixels(image,exception) == MagickFalse)
451       break;
452     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
453       image->rows);
454     if (status == MagickFalse)
455       break;
456   }
457   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
458   (void) FPX_CloseImage(flashpix);
459   FPX_ClearSystem();
460   return(GetFirstImageInList(image));
461 }
462 #endif
463 \f
464 /*
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 %                                                                             %
467 %                                                                             %
468 %                                                                             %
469 %   R e g i s t e r F P X I m a g e                                           %
470 %                                                                             %
471 %                                                                             %
472 %                                                                             %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 %
475 %  RegisterFPXImage() adds attributes for the FPX image format to
476 %  the list of supported formats.  The attributes include the image format
477 %  tag, a method to read and/or write the format, whether the format
478 %  supports the saving of more than one frame to the same file or blob,
479 %  whether the format supports native in-memory I/O, and a brief
480 %  description of the format.
481 %
482 %  The format of the RegisterFPXImage method is:
483 %
484 %      size_t RegisterFPXImage(void)
485 %
486 */
487 ModuleExport size_t RegisterFPXImage(void)
488 {
489   MagickInfo
490     *entry;
491
492   entry=SetMagickInfo("FPX");
493 #if defined(MAGICKCORE_FPX_DELEGATE)
494   entry->decoder=(DecodeImageHandler *) ReadFPXImage;
495   entry->encoder=(EncodeImageHandler *) WriteFPXImage;
496 #endif
497   entry->adjoin=MagickFalse;
498   entry->seekable_stream=MagickTrue;
499   entry->blob_support=MagickFalse;
500   entry->description=ConstantString("FlashPix Format");
501   entry->module=ConstantString("FPX");
502   (void) RegisterMagickInfo(entry);
503   return(MagickImageCoderSignature);
504 }
505 \f
506 /*
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 %                                                                             %
509 %                                                                             %
510 %                                                                             %
511 %   U n r e g i s t e r F P X I m a g e                                       %
512 %                                                                             %
513 %                                                                             %
514 %                                                                             %
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 %
517 %  UnregisterFPXImage() removes format registrations made by the
518 %  FPX module from the list of supported formats.
519 %
520 %  The format of the UnregisterFPXImage method is:
521 %
522 %      UnregisterFPXImage(void)
523 %
524 */
525 ModuleExport void UnregisterFPXImage(void)
526 {
527   (void) UnregisterMagickInfo("FPX");
528 }
529 \f
530 #if defined(MAGICKCORE_FPX_DELEGATE)
531 /*
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 %                                                                             %
534 %                                                                             %
535 %                                                                             %
536 %   W r i t e F P X I m a g e                                                 %
537 %                                                                             %
538 %                                                                             %
539 %                                                                             %
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 %
542 %  WriteFPXImage() writes an image in the FlashPix image format.  This
543 %  method was contributed by BillR@corbis.com.
544 %
545 %  The format of the WriteFPXImage method is:
546 %
547 %      MagickBooleanType WriteFPXImage(const ImageInfo *image_info,
548 %        Image *image,ExceptionInfo *exception)
549 %
550 %  A description of each parameter follows.
551 %
552 %    o image_info: the image info.
553 %
554 %    o image:  The image.
555 %
556 %    o exception: return any errors or warnings in this structure.
557 %
558 */
559
560 static void ColorTwistMultiply(FPXColorTwistMatrix first,
561   FPXColorTwistMatrix second,FPXColorTwistMatrix *color_twist)
562 {
563   /*
564     Matrix multiply.
565   */
566   assert(color_twist != (FPXColorTwistMatrix *) NULL);
567   color_twist->byy=(first.byy*second.byy)+(first.byc1*second.bc1y)+
568     (first.byc2*second.bc2y)+(first.dummy1_zero*second.dummy4_zero);
569   color_twist->byc1=(first.byy*second.byc1)+(first.byc1*second.bc1c1)+
570     (first.byc2*second.bc2c1)+(first.dummy1_zero*second.dummy5_zero);
571   color_twist->byc2=(first.byy*second.byc2)+(first.byc1*second.bc1c2)+
572     (first.byc2*second.bc2c2)+(first.dummy1_zero*second.dummy6_zero);
573   color_twist->dummy1_zero=(first.byy*second.dummy1_zero)+
574     (first.byc1*second.dummy2_zero)+(first.byc2*second.dummy3_zero)+
575     (first.dummy1_zero*second.dummy7_one);
576   color_twist->bc1y=(first.bc1y*second.byy)+(first.bc1c1*second.bc1y)+
577     (first.bc1c2*second.bc2y)+(first.dummy2_zero*second.dummy4_zero);
578   color_twist->bc1c1=(first.bc1y*second.byc1)+(first.bc1c1*second.bc1c1)+
579     (first.bc1c2*second.bc2c1)+(first.dummy2_zero*second.dummy5_zero);
580   color_twist->bc1c2=(first.bc1y*second.byc2)+(first.bc1c1*second.bc1c2)+
581     (first.bc1c2*second.bc2c2)+(first.dummy2_zero*second.dummy6_zero);
582   color_twist->dummy2_zero=(first.bc1y*second.dummy1_zero)+
583     (first.bc1c1*second.dummy2_zero)+(first.bc1c2*second.dummy3_zero)+
584     (first.dummy2_zero*second.dummy7_one);
585   color_twist->bc2y=(first.bc2y*second.byy)+(first.bc2c1*second.bc1y)+
586     (first.bc2c2*second.bc2y)+(first.dummy3_zero*second.dummy4_zero);
587   color_twist->bc2c1=(first.bc2y*second.byc1)+(first.bc2c1*second.bc1c1)+
588     (first.bc2c2*second.bc2c1)+(first.dummy3_zero*second.dummy5_zero);
589   color_twist->bc2c2=(first.bc2y*second.byc2)+(first.bc2c1*second.bc1c2)+
590     (first.bc2c2*second.bc2c2)+(first.dummy3_zero*second.dummy6_zero);
591   color_twist->dummy3_zero=(first.bc2y*second.dummy1_zero)+
592     (first.bc2c1*second.dummy2_zero)+(first.bc2c2*second.dummy3_zero)+
593     (first.dummy3_zero*second.dummy7_one);
594   color_twist->dummy4_zero=(first.dummy4_zero*second.byy)+
595     (first.dummy5_zero*second.bc1y)+(first.dummy6_zero*second.bc2y)+
596     (first.dummy7_one*second.dummy4_zero);
597   color_twist->dummy5_zero=(first.dummy4_zero*second.byc1)+
598     (first.dummy5_zero*second.bc1c1)+(first.dummy6_zero*second.bc2c1)+
599     (first.dummy7_one*second.dummy5_zero);
600   color_twist->dummy6_zero=(first.dummy4_zero*second.byc2)+
601     (first.dummy5_zero*second.bc1c2)+(first.dummy6_zero*second.bc2c2)+
602     (first.dummy7_one*second.dummy6_zero);
603   color_twist->dummy7_one=(first.dummy4_zero*second.dummy1_zero)+
604     (first.dummy5_zero*second.dummy2_zero)+
605     (first.dummy6_zero*second.dummy3_zero)+(first.dummy7_one*second.dummy7_one);
606 }
607
608 static void SetBrightness(double brightness,FPXColorTwistMatrix *color_twist)
609 {
610   FPXColorTwistMatrix
611     effect,
612     result;
613
614   /*
615     Set image brightness in color twist matrix.
616   */
617   assert(color_twist != (FPXColorTwistMatrix *) NULL);
618   brightness=sqrt((double) brightness);
619   effect.byy=brightness;
620   effect.byc1=0.0;
621   effect.byc2=0.0;
622   effect.dummy1_zero=0.0;
623   effect.bc1y=0.0;
624   effect.bc1c1=brightness;
625   effect.bc1c2=0.0;
626   effect.dummy2_zero=0.0;
627   effect.bc2y=0.0;
628   effect.bc2c1=0.0;
629   effect.bc2c2=brightness;
630   effect.dummy3_zero=0.0;
631   effect.dummy4_zero=0.0;
632   effect.dummy5_zero=0.0;
633   effect.dummy6_zero=0.0;
634   effect.dummy7_one=1.0;
635   ColorTwistMultiply(*color_twist,effect,&result);
636   *color_twist=result;
637 }
638
639 static void SetColorBalance(double red,double green,double blue,
640   FPXColorTwistMatrix *color_twist)
641 {
642   FPXColorTwistMatrix
643     blue_effect,
644     green_effect,
645     result,
646     rgb_effect,
647     rg_effect,
648     red_effect;
649
650   /*
651     Set image color balance in color twist matrix.
652   */
653   assert(color_twist != (FPXColorTwistMatrix *) NULL);
654   red=sqrt((double) red)-1.0;
655   green=sqrt((double) green)-1.0;
656   blue=sqrt((double) blue)-1.0;
657   red_effect.byy=1.0;
658   red_effect.byc1=0.0;
659   red_effect.byc2=0.299*red;
660   red_effect.dummy1_zero=0.0;
661   red_effect.bc1y=(-0.299)*red;
662   red_effect.bc1c1=1.0-0.299*red;
663   red_effect.bc1c2=(-0.299)*red;
664   red_effect.dummy2_zero=0.0;
665   red_effect.bc2y=0.701*red;
666   red_effect.bc2c1=0.0;
667   red_effect.bc2c2=1.0+0.402*red;
668   red_effect.dummy3_zero=0.0;
669   red_effect.dummy4_zero=0.0;
670   red_effect.dummy5_zero=0.0;
671   red_effect.dummy6_zero=0.0;
672   red_effect.dummy7_one=1.0;
673   green_effect.byy=1.0;
674   green_effect.byc1=(-0.114)*green;
675   green_effect.byc2=(-0.299)*green;
676   green_effect.dummy1_zero=0.0;
677   green_effect.bc1y=(-0.587)*green;
678   green_effect.bc1c1=1.0-0.473*green;
679   green_effect.bc1c2=0.299*green;
680   green_effect.dummy2_zero=0.0;
681   green_effect.bc2y=(-0.587)*green;
682   green_effect.bc2c1=0.114*green;
683   green_effect.bc2c2=1.0-0.288*green;
684   green_effect.dummy3_zero=0.0;
685   green_effect.dummy4_zero=0.0;
686   green_effect.dummy5_zero=0.0;
687   green_effect.dummy6_zero=0.0;
688   green_effect.dummy7_one=1.0;
689   blue_effect.byy=1.0;
690   blue_effect.byc1=0.114*blue;
691   blue_effect.byc2=0.0;
692   blue_effect.dummy1_zero=0.0;
693   blue_effect.bc1y=0.886*blue;
694   blue_effect.bc1c1=1.0+0.772*blue;
695   blue_effect.bc1c2=0.0;
696   blue_effect.dummy2_zero=0.0;
697   blue_effect.bc2y=(-0.114)*blue;
698   blue_effect.bc2c1=(-0.114)*blue;
699   blue_effect.bc2c2=1.0-0.114*blue;
700   blue_effect.dummy3_zero=0.0;
701   blue_effect.dummy4_zero=0.0;
702   blue_effect.dummy5_zero=0.0;
703   blue_effect.dummy6_zero=0.0;
704   blue_effect.dummy7_one=1.0;
705   ColorTwistMultiply(red_effect,green_effect,&rg_effect);
706   ColorTwistMultiply(rg_effect,blue_effect,&rgb_effect);
707   ColorTwistMultiply(*color_twist,rgb_effect,&result);
708   *color_twist=result;
709 }
710
711 static void SetSaturation(double saturation,FPXColorTwistMatrix *color_twist)
712 {
713   FPXColorTwistMatrix
714     effect,
715     result;
716
717   /*
718     Set image saturation in color twist matrix.
719   */
720   assert(color_twist != (FPXColorTwistMatrix *) NULL);
721   effect.byy=1.0;
722   effect.byc1=0.0;
723   effect.byc2=0.0;
724   effect.dummy1_zero=0.0;
725   effect.bc1y=0.0;
726   effect.bc1c1=saturation;
727   effect.bc1c2=0.0;
728   effect.dummy2_zero=0.0;
729   effect.bc2y=0.0;
730   effect.bc2c1=0.0;
731   effect.bc2c2=saturation;
732   effect.dummy3_zero=0.0;
733   effect.dummy4_zero=0.0;
734   effect.dummy5_zero=0.0;
735   effect.dummy6_zero=0.0;
736   effect.dummy7_one=1.0;
737   ColorTwistMultiply(*color_twist,effect,&result);
738   *color_twist=result;
739 }
740
741 static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
742   ExceptionInfo *exception)
743 {
744   FPXBackground
745     background_color;
746
747   FPXColorspace
748     colorspace =
749     {
750       TRUE, 4,
751       {
752         { NIFRGB_R, DATA_TYPE_UNSIGNED_BYTE },
753         { NIFRGB_G, DATA_TYPE_UNSIGNED_BYTE },
754         { NIFRGB_B, DATA_TYPE_UNSIGNED_BYTE },
755         { ALPHA, DATA_TYPE_UNSIGNED_BYTE }
756       }
757     };
758
759   const char
760     *comment,
761     *label;
762
763   FPXCompressionOption
764     compression;
765
766   FPXImageDesc
767     fpx_info;
768
769   FPXImageHandle
770     *flashpix;
771
772   FPXStatus
773     fpx_status;
774
775   FPXSummaryInformation
776     summary_info;
777
778   MagickBooleanType
779     status;
780
781   QuantumInfo
782     *quantum_info;
783
784   QuantumType
785     quantum_type;
786
787   register const Quantum
788     *p;
789
790   register ssize_t
791     i;
792
793   size_t
794     length,
795     memory_limit;
796
797   ssize_t
798     y;
799
800   unsigned char
801     *pixels;
802
803   unsigned int
804     tile_height,
805     tile_width;
806
807   /*
808     Open input file.
809   */
810   assert(image_info != (const ImageInfo *) NULL);
811   assert(image_info->signature == MagickSignature);
812   assert(image != (Image *) NULL);
813   assert(image->signature == MagickSignature);
814   if (image->debug != MagickFalse)
815     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
816   assert(exception != (ExceptionInfo *) NULL);
817   assert(exception->signature == MagickSignature);
818   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
819   if (status == MagickFalse)
820     return(status);
821   (void) TransformImageColorspace(image,sRGBColorspace,exception);
822   (void) CloseBlob(image);
823   /*
824     Initialize FPX toolkit.
825   */
826   image->depth=8;
827   memory_limit=20000000;
828   fpx_status=FPX_SetToolkitMemoryLimit(&memory_limit);
829   if (fpx_status != FPX_OK)
830     ThrowWriterException(DelegateError,"UnableToInitializeFPXLibrary");
831   tile_width=64;
832   tile_height=64;
833   colorspace.numberOfComponents=3;
834   if (image->alpha_trait == BlendPixelTrait)
835     colorspace.numberOfComponents=4;
836   if ((image_info->type != TrueColorType) &&
837       (IsImageGray(image,exception) != MagickFalse))
838     {
839       colorspace.numberOfComponents=1;
840       colorspace.theComponents[0].myColor=MONOCHROME;
841     }
842   background_color.color1_value=0;
843   background_color.color2_value=0;
844   background_color.color3_value=0;
845   background_color.color4_value=0;
846   compression=NONE;
847   if (image->compression == JPEGCompression)
848     compression=JPEG_UNSPECIFIED;
849   if (image_info->compression == JPEGCompression)
850     compression=JPEG_UNSPECIFIED;
851   {
852 #if defined(macintosh)
853     FSSpec
854       fsspec;
855
856     FilenameToFSSpec(filename,&fsspec);
857     fpx_status=FPX_CreateImageByFilename((const FSSpec &) fsspec,image->columns,
858 #else
859     fpx_status=FPX_CreateImageByFilename(image->filename,image->columns,
860 #endif
861       image->rows,tile_width,tile_height,colorspace,background_color,
862       compression,&flashpix);
863   }
864   if (fpx_status != FPX_OK)
865     return(status);
866   if (compression == JPEG_UNSPECIFIED)
867     {
868       /*
869         Initialize the compression by quality for the entire image.
870       */
871       fpx_status=FPX_SetJPEGCompression(flashpix,(unsigned short)
872         image->quality == UndefinedCompressionQuality ? 75 : image->quality);
873       if (fpx_status != FPX_OK)
874         ThrowWriterException(DelegateError,"UnableToSetJPEGLevel");
875     }
876   /*
877     Set image summary info.
878   */
879   summary_info.title_valid=MagickFalse;
880   summary_info.subject_valid=MagickFalse;
881   summary_info.author_valid=MagickFalse;
882   summary_info.comments_valid=MagickFalse;
883   summary_info.keywords_valid=MagickFalse;
884   summary_info.OLEtemplate_valid=MagickFalse;
885   summary_info.last_author_valid=MagickFalse;
886   summary_info.rev_number_valid=MagickFalse;
887   summary_info.edit_time_valid=MagickFalse;
888   summary_info.last_printed_valid=MagickFalse;
889   summary_info.create_dtm_valid=MagickFalse;
890   summary_info.last_save_dtm_valid=MagickFalse;
891   summary_info.page_count_valid=MagickFalse;
892   summary_info.word_count_valid=MagickFalse;
893   summary_info.char_count_valid=MagickFalse;
894   summary_info.thumbnail_valid=MagickFalse;
895   summary_info.appname_valid=MagickFalse;
896   summary_info.security_valid=MagickFalse;
897   summary_info.title.ptr=(unsigned char *) NULL;
898   label=GetImageProperty(image,"label",exception);
899   if (label != (const char *) NULL)
900     {
901       /*
902         Note image label.
903       */
904       summary_info.title_valid=MagickTrue;
905       length=strlen(label);
906       summary_info.title.length=length;
907       if (~length >= (MaxTextExtent-1))
908         summary_info.title.ptr=(unsigned char *) AcquireQuantumMemory(
909           length+MaxTextExtent,sizeof(*summary_info.title.ptr));
910       if (summary_info.title.ptr == (unsigned char *) NULL)
911         ThrowWriterException(DelegateError,"UnableToSetImageTitle");
912       (void) CopyMagickString((char *) summary_info.title.ptr,label,
913         MaxTextExtent);
914     }
915   comment=GetImageProperty(image,"comment",exception);
916   if (comment != (const char *) NULL)
917     {
918       /*
919         Note image comment.
920       */
921       summary_info.comments_valid=MagickTrue;
922       summary_info.comments.ptr=(unsigned char *) AcquireString(comment);
923       summary_info.comments.length=strlen(comment);
924     }
925   fpx_status=FPX_SetSummaryInformation(flashpix,&summary_info);
926   if (fpx_status != FPX_OK)
927     ThrowWriterException(DelegateError,"UnableToSetSummaryInfo");
928   /*
929     Initialize FlashPix image description.
930   */
931   quantum_info=AcquireQuantumInfo(image_info,image);
932   if (quantum_info == (QuantumInfo *) NULL)
933     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
934   pixels=GetQuantumPixels(quantum_info);
935   fpx_info.numberOfComponents=colorspace.numberOfComponents;
936   for (i=0; i < (ssize_t) fpx_info.numberOfComponents; i++)
937   {
938     fpx_info.components[i].myColorType.myDataType=DATA_TYPE_UNSIGNED_BYTE;
939     fpx_info.components[i].horzSubSampFactor=1;
940     fpx_info.components[i].vertSubSampFactor=1;
941     fpx_info.components[i].columnStride=fpx_info.numberOfComponents;
942     fpx_info.components[i].lineStride=
943       image->columns*fpx_info.components[i].columnStride;
944     fpx_info.components[i].theData=pixels+i;
945   }
946   fpx_info.components[0].myColorType.myColor=fpx_info.numberOfComponents != 1
947     ? NIFRGB_R : MONOCHROME;
948   fpx_info.components[1].myColorType.myColor=NIFRGB_G;
949   fpx_info.components[2].myColorType.myColor=NIFRGB_B;
950   fpx_info.components[3].myColorType.myColor=ALPHA;
951   /*
952     Write image pixelss.
953   */
954   quantum_type=RGBQuantum;
955   if (image->alpha_trait == BlendPixelTrait)
956     quantum_type=RGBAQuantum;
957   if (fpx_info.numberOfComponents == 1)
958     quantum_type=GrayQuantum;
959   for (y=0; y < (ssize_t) image->rows; y++)
960   {
961     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
962     if (p == (const Quantum *) NULL)
963       break;
964     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
965       quantum_type,pixels,exception);
966     fpx_status=FPX_WriteImageLine(flashpix,&fpx_info);
967     if (fpx_status != FPX_OK)
968       break;
969     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
970       image->rows);
971     if (status == MagickFalse)
972       break;
973   }
974   quantum_info=DestroyQuantumInfo(quantum_info);
975   if (image_info->view != (char *) NULL)
976     {
977       FPXAffineMatrix
978         affine;
979
980       FPXColorTwistMatrix
981         color_twist;
982
983       FPXContrastAdjustment
984         contrast;
985
986       FPXFilteringValue
987         sharpen;
988
989       FPXResultAspectRatio
990         aspect_ratio;
991
992       FPXROI
993         view_rect;
994
995       MagickBooleanType
996         affine_valid,
997         aspect_ratio_valid,
998         color_twist_valid,
999         contrast_valid,
1000         sharpen_valid,
1001         view_rect_valid;
1002
1003       /*
1004         Initialize default viewing parameters.
1005       */
1006       contrast=1.0;
1007       contrast_valid=MagickTrue;
1008       color_twist.byy=1.0;
1009       color_twist.byc1=0.0;
1010       color_twist.byc2=0.0;
1011       color_twist.dummy1_zero=0.0;
1012       color_twist.bc1y=0.0;
1013       color_twist.bc1c1=1.0;
1014       color_twist.bc1c2=0.0;
1015       color_twist.dummy2_zero=0.0;
1016       color_twist.bc2y=0.0;
1017       color_twist.bc2c1=0.0;
1018       color_twist.bc2c2=1.0;
1019       color_twist.dummy3_zero=0.0;
1020       color_twist.dummy4_zero=0.0;
1021       color_twist.dummy5_zero=0.0;
1022       color_twist.dummy6_zero=0.0;
1023       color_twist.dummy7_one=1.0;
1024       color_twist_valid=MagickTrue;
1025       sharpen=0.0;
1026       sharpen_valid=MagickTrue;
1027       aspect_ratio=(double) image->columns/image->rows;
1028       aspect_ratio_valid=MagickTrue;
1029       view_rect.left=(float) 0.1;
1030       view_rect.width=aspect_ratio-0.2;
1031       view_rect.top=(float) 0.1;
1032       view_rect.height=(float) 0.8; /* 1.0-0.2 */
1033       view_rect_valid=MagickTrue;
1034       affine.a11=1.0;
1035       affine.a12=0.0;
1036       affine.a13=0.0;
1037       affine.a14=0.0;
1038       affine.a21=0.0;
1039       affine.a22=1.0;
1040       affine.a23=0.0;
1041       affine.a24=0.0;
1042       affine.a31=0.0;
1043       affine.a32=0.0;
1044       affine.a33=1.0;
1045       affine.a34=0.0;
1046       affine.a41=0.0;
1047       affine.a42=0.0;
1048       affine.a43=0.0;
1049       affine.a44=1.0;
1050       affine_valid=MagickTrue;
1051       if (0)
1052         {
1053           /*
1054             Color color twist.
1055           */
1056           SetBrightness(0.5,&color_twist);
1057           SetSaturation(0.5,&color_twist);
1058           SetColorBalance(0.5,1.0,1.0,&color_twist);
1059           color_twist_valid=MagickTrue;
1060         }
1061       if (affine_valid != MagickFalse)
1062         {
1063           fpx_status=FPX_SetImageAffineMatrix(flashpix,&affine);
1064           if (fpx_status != FPX_OK)
1065             ThrowWriterException(DelegateError,"UnableToSetAffineMatrix");
1066         }
1067       if (aspect_ratio_valid != MagickFalse)
1068         {
1069           fpx_status=FPX_SetImageResultAspectRatio(flashpix,&aspect_ratio);
1070           if (fpx_status != FPX_OK)
1071             ThrowWriterException(DelegateError,"UnableToSetAspectRatio");
1072         }
1073       if (color_twist_valid != MagickFalse)
1074         {
1075           fpx_status=FPX_SetImageColorTwistMatrix(flashpix,&color_twist);
1076           if (fpx_status != FPX_OK)
1077             ThrowWriterException(DelegateError,"UnableToSetColorTwist");
1078         }
1079       if (contrast_valid != MagickFalse)
1080         {
1081           fpx_status=FPX_SetImageContrastAdjustment(flashpix,&contrast);
1082           if (fpx_status != FPX_OK)
1083             ThrowWriterException(DelegateError,"UnableToSetContrast");
1084         }
1085       if (sharpen_valid != MagickFalse)
1086         {
1087           fpx_status=FPX_SetImageFilteringValue(flashpix,&sharpen);
1088           if (fpx_status != FPX_OK)
1089             ThrowWriterException(DelegateError,"UnableToSetFilteringValue");
1090         }
1091       if (view_rect_valid != MagickFalse)
1092         {
1093           fpx_status=FPX_SetImageROI(flashpix,&view_rect);
1094           if (fpx_status != FPX_OK)
1095             ThrowWriterException(DelegateError,"UnableToSetRegionOfInterest");
1096         }
1097     }
1098   (void) FPX_CloseImage(flashpix);
1099   FPX_ClearSystem();
1100   return(MagickTrue);
1101 }
1102 #endif