]> granicus.if.org Git - imagemagick/blob - coders/matte.c
(no commit message)
[imagemagick] / coders / matte.c
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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2012 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/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/constitute.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 \f
62 /*
63   Forward declarations.
64 */
65 static MagickBooleanType
66   WriteMATTEImage(const ImageInfo *,Image *,ExceptionInfo *);
67 \f
68 /*
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %                                                                             %
71 %                                                                             %
72 %                                                                             %
73 %   R e g i s t e r M A T T E I m a g e                                       %
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %
79 %  RegisterMATTEImage() adds attributes for the MATTE image format to
80 %  the list of supported formats.  The attributes include the image format
81 %  tag, a method to read and/or write the format, whether the format
82 %  supports the saving of more than one frame to the same file or blob,
83 %  whether the format supports native in-memory I/O, and a brief
84 %  description of the format.
85 %
86 %  The format of the RegisterMATTEImage method is:
87 %
88 %      size_t RegisterMATTEImage(void)
89 %
90 */
91 ModuleExport size_t RegisterMATTEImage(void)
92 {
93   MagickInfo
94     *entry;
95
96   entry=SetMagickInfo("MATTE");
97   entry->encoder=(EncodeImageHandler *) WriteMATTEImage;
98   entry->format_type=ExplicitFormatType;
99   entry->description=ConstantString("MATTE format");
100   entry->module=ConstantString("MATTE");
101   (void) RegisterMagickInfo(entry);
102   return(MagickImageCoderSignature);
103 }
104 \f
105 /*
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %                                                                             %
108 %                                                                             %
109 %                                                                             %
110 %   U n r e g i s t e r M A T T E I m a g e                                   %
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 %  UnregisterMATTEImage() removes format registrations made by the
117 %  MATTE module from the list of supported formats.
118 %
119 %  The format of the UnregisterMATTEImage method is:
120 %
121 %      UnregisterMATTEImage(void)
122 %
123 */
124 ModuleExport void UnregisterMATTEImage(void)
125 {
126   (void) UnregisterMagickInfo("MATTE");
127 }
128 \f
129 /*
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 %                                                                             %
132 %                                                                             %
133 %                                                                             %
134 %   W r i t e M A T T E I m a g e                                             %
135 %                                                                             %
136 %                                                                             %
137 %                                                                             %
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %
140 %  WriteMATTEImage() writes an image of matte bytes to a file.  It consists of
141 %  data from the matte component of the image [0..255].
142 %
143 %  The format of the WriteMATTEImage method is:
144 %
145 %      MagickBooleanType WriteMATTEImage(const ImageInfo *image_info,
146 %        Image *image,ExceptionInfo *exception)
147 %
148 %  A description of each parameter follows.
149 %
150 %    o image_info: the image info.
151 %
152 %    o image:  The image.
153 %
154 %    o exception: return any errors or warnings in this structure.
155 %
156 */
157 static MagickBooleanType WriteMATTEImage(const ImageInfo *image_info,
158   Image *image,ExceptionInfo *exception)
159 {
160   Image
161     *matte_image;
162
163   MagickBooleanType
164     status;
165
166   register const Quantum
167     *p;
168
169   register ssize_t
170     x;
171
172   register Quantum
173     *q;
174
175   ssize_t
176     y;
177
178   if (image->matte == MagickFalse)
179     ThrowWriterException(CoderError,"ImageDoesNotHaveAAlphaChannel");
180   matte_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
181   if (matte_image == (Image *) NULL)
182     return(MagickFalse);
183   (void) SetImageType(matte_image,TrueColorMatteType,exception);
184   matte_image->matte=MagickFalse;
185   /*
186     Convert image to matte pixels.
187   */
188   for (y=0; y < (ssize_t) image->rows; y++)
189   {
190     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
191     q=QueueAuthenticPixels(matte_image,0,y,matte_image->columns,1,exception);
192     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
193       break;
194     for (x=0; x < (ssize_t) image->columns; x++)
195     {
196       SetPixelRed(matte_image,GetPixelAlpha(image,p),q);
197       SetPixelGreen(matte_image,GetPixelAlpha(image,p),q);
198       SetPixelBlue(matte_image,GetPixelAlpha(image,p),q);
199       SetPixelAlpha(matte_image,OpaqueAlpha,q);
200       p+=GetPixelChannels(image);
201       q+=GetPixelChannels(matte_image);
202     }
203     if (SyncAuthenticPixels(matte_image,exception) == MagickFalse)
204       break;
205     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
206       image->rows);
207     if (status == MagickFalse)
208       break;
209   }
210   (void) FormatLocaleString(matte_image->filename,MaxTextExtent,
211     "MIFF:%s",image->filename);
212   status=WriteImage(image_info,matte_image,exception);
213   matte_image=DestroyImage(matte_image);
214   return(status);
215 }