Home | History | Annotate | Download | only in coders
      1 /*
      2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      3 %                                                                             %
      4 %                                                                             %
      5 %                                                                             %
      6 %                     M   M   AAA   TTTTT  TTTTT  EEEEE                       %
      7 %                     MM MM  A   A    T      T    E                           %
      8 %                     M M M  AAAAA    T      T    EEE                         %
      9 %                     M   M  A   A    T      T    E                           %
     10 %                     M   M  A   A    T      T    EEEEE                       %
     11 %                                                                             %
     12 %                                                                             %
     13 %                     Write Matte Channel To MIFF File.                       %
     14 %                                                                             %
     15 %                              Software Design                                %
     16 %                                   Cristy                                    %
     17 %                                 July 1992                                   %
     18 %                                                                             %
     19 %                                                                             %
     20 %  Copyright 1999-2016 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 
     39 /*
     41   Include declarations.
     42 */
     43 #include "MagickCore/studio.h"
     44 #include "MagickCore/attribute.h"
     45 #include "MagickCore/blob.h"
     46 #include "MagickCore/blob-private.h"
     47 #include "MagickCore/cache.h"
     48 #include "MagickCore/constitute.h"
     49 #include "MagickCore/exception.h"
     50 #include "MagickCore/exception-private.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 /*
     65   Forward declarations.
     66 */
     67 static MagickBooleanType
     68   WriteMATTEImage(const ImageInfo *,Image *,ExceptionInfo *);
     69 
     70 /*
     72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     73 %                                                                             %
     74 %                                                                             %
     75 %                                                                             %
     76 %   R e g i s t e r M A T T E I m a g e                                       %
     77 %                                                                             %
     78 %                                                                             %
     79 %                                                                             %
     80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     81 %
     82 %  RegisterMATTEImage() adds attributes for the MATTE image format to
     83 %  the list of supported formats.  The attributes include the image format
     84 %  tag, a method to read and/or write the format, whether the format
     85 %  supports the saving of more than one frame to the same file or blob,
     86 %  whether the format supports native in-memory I/O, and a brief
     87 %  description of the format.
     88 %
     89 %  The format of the RegisterMATTEImage method is:
     90 %
     91 %      size_t RegisterMATTEImage(void)
     92 %
     93 */
     94 ModuleExport size_t RegisterMATTEImage(void)
     95 {
     96   MagickInfo
     97     *entry;
     98 
     99   entry=AcquireMagickInfo("MATTE","MATTE","MATTE format");
    100   entry->encoder=(EncodeImageHandler *) WriteMATTEImage;
    101   entry->format_type=ExplicitFormatType;
    102   (void) RegisterMagickInfo(entry);
    103   return(MagickImageCoderSignature);
    104 }
    105 
    106 /*
    108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    109 %                                                                             %
    110 %                                                                             %
    111 %                                                                             %
    112 %   U n r e g i s t e r M A T T E I m a g e                                   %
    113 %                                                                             %
    114 %                                                                             %
    115 %                                                                             %
    116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    117 %
    118 %  UnregisterMATTEImage() removes format registrations made by the
    119 %  MATTE module from the list of supported formats.
    120 %
    121 %  The format of the UnregisterMATTEImage method is:
    122 %
    123 %      UnregisterMATTEImage(void)
    124 %
    125 */
    126 ModuleExport void UnregisterMATTEImage(void)
    127 {
    128   (void) UnregisterMagickInfo("MATTE");
    129 }
    130 
    131 /*
    133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    134 %                                                                             %
    135 %                                                                             %
    136 %                                                                             %
    137 %   W r i t e M A T T E I m a g e                                             %
    138 %                                                                             %
    139 %                                                                             %
    140 %                                                                             %
    141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    142 %
    143 %  WriteMATTEImage() writes an image of matte bytes to a file.  It consists of
    144 %  data from the matte component of the image [0..255].
    145 %
    146 %  The format of the WriteMATTEImage method is:
    147 %
    148 %      MagickBooleanType WriteMATTEImage(const ImageInfo *image_info,
    149 %        Image *image,ExceptionInfo *exception)
    150 %
    151 %  A description of each parameter follows.
    152 %
    153 %    o image_info: the image info.
    154 %
    155 %    o image:  The image.
    156 %
    157 %    o exception: return any errors or warnings in this structure.
    158 %
    159 */
    160 static MagickBooleanType WriteMATTEImage(const ImageInfo *image_info,
    161   Image *image,ExceptionInfo *exception)
    162 {
    163   Image
    164     *matte_image;
    165 
    166   ImageInfo
    167     *write_info;
    168 
    169   MagickBooleanType
    170     status;
    171 
    172   register const Quantum
    173     *p;
    174 
    175   register ssize_t
    176     x;
    177 
    178   register Quantum
    179     *q;
    180 
    181   ssize_t
    182     y;
    183 
    184   if (image->alpha_trait == UndefinedPixelTrait)
    185     ThrowWriterException(CoderError,"ImageDoesNotHaveAnAlphaChannel");
    186   matte_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
    187   if (matte_image == (Image *) NULL)
    188     return(MagickFalse);
    189   (void) SetImageType(matte_image,TrueColorAlphaType,exception);
    190   matte_image->alpha_trait=UndefinedPixelTrait;
    191   /*
    192     Convert image to matte pixels.
    193   */
    194   for (y=0; y < (ssize_t) image->rows; y++)
    195   {
    196     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
    197     q=QueueAuthenticPixels(matte_image,0,y,matte_image->columns,1,exception);
    198     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
    199       break;
    200     for (x=0; x < (ssize_t) image->columns; x++)
    201     {
    202       SetPixelRed(matte_image,GetPixelAlpha(image,p),q);
    203       SetPixelGreen(matte_image,GetPixelAlpha(image,p),q);
    204       SetPixelBlue(matte_image,GetPixelAlpha(image,p),q);
    205       SetPixelAlpha(matte_image,OpaqueAlpha,q);
    206       p+=GetPixelChannels(image);
    207       q+=GetPixelChannels(matte_image);
    208     }
    209     if (SyncAuthenticPixels(matte_image,exception) == MagickFalse)
    210       break;
    211     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
    212       image->rows);
    213     if (status == MagickFalse)
    214       break;
    215   }
    216   write_info=CloneImageInfo(image_info);
    217   if ((*write_info->magick == '\0') ||
    218       (LocaleCompare(write_info->magick,"MATTE") == 0))
    219     (void) FormatLocaleString(matte_image->filename,MagickPathExtent,
    220       "MIFF:%s",image->filename);
    221   status=WriteImage(write_info,matte_image,exception);
    222   write_info=DestroyImageInfo(write_info);
    223   matte_image=DestroyImage(matte_image);
    224   return(status);
    225 }
    226