2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % M M M AAAAA SSS KKK %
20 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/enhance.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/magick.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/monitor.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/pixel-accessor.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
64 static MagickBooleanType
65 WriteMASKImage(const ImageInfo *,Image *,ExceptionInfo *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d M A S K I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadMASKImage returns the image mask associated with the image.
80 % The format of the ReadMASKImage method is:
82 % Image *ReadMASKImage(const ImageInfo *image_info,
83 % ExceptionInfo *exception)
85 % A description of each parameter follows:
87 % o image_info: the image info.
89 % o exception: return any errors or warnings in this structure.
92 static Image *ReadMASKImage(const ImageInfo *image_info,
93 ExceptionInfo *exception)
102 Initialize Image structure.
104 assert(image_info != (const ImageInfo *) NULL);
105 assert(image_info->signature == MagickCoreSignature);
106 if (image_info->debug != MagickFalse)
107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
108 image_info->filename);
109 assert(exception != (ExceptionInfo *) NULL);
110 assert(exception->signature == MagickCoreSignature);
111 read_info=CloneImageInfo(image_info);
112 SetImageInfoBlob(read_info,(void *) NULL,0);
113 (void) CopyMagickString(read_info->magick,"MIFF",MagickPathExtent);
114 image=ReadImage(read_info,exception);
115 read_info=DestroyImageInfo(read_info);
116 if (image != (Image *) NULL)
121 status=GrayscaleImage(image,image->intensity,exception);
122 if (status == MagickFalse)
123 image=DestroyImage(image);
125 return(GetFirstImageInList(image));
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % R e g i s t e r M A S K I m a g e %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % RegisterMASKImage() adds attributes for the MASK image format to
140 % the list of supported formats. The attributes include the image format
141 % tag, a method to read and/or write the format, whether the format
142 % supports the saving of more than one frame to the same file or blob,
143 % whether the format supports native in-memory I/O, and a brief
144 % description of the format.
146 % The format of the RegisterMASKImage method is:
148 % size_t RegisterMASKImage(void)
151 ModuleExport size_t RegisterMASKImage(void)
156 entry=AcquireMagickInfo("MASK","MASK","Image Clip Mask");
157 entry->decoder=(DecodeImageHandler *) ReadMASKImage;
158 entry->encoder=(EncodeImageHandler *) WriteMASKImage;
159 (void) RegisterMagickInfo(entry);
160 return(MagickImageCoderSignature);
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 % U n r e g i s t e r M A S K I m a g e %
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 % UnregisterMASKImage() removes format registrations made by the
175 % MASK module from the list of supported formats.
177 % The format of the UnregisterMASKImage method is:
179 % UnregisterMASKImage(void)
182 ModuleExport void UnregisterMASKImage(void)
184 (void) UnregisterMagickInfo("MASK");
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % W r i t e M A S K I m a g e %
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 % WriteMASKImage() writes an image mask to a file.
200 % The format of the WriteMASKImage method is:
202 % MagickBooleanType WriteMASKImage(const ImageInfo *image_info,
203 % Image *image,ExceptionInfo *exception)
205 % A description of each parameter follows.
207 % o image_info: the image info.
209 % o image: The image.
211 % o exception: return any errors or warnings in this structure.
215 static Image *MaskImage(const Image *image,ExceptionInfo *exception)
230 mask_image=CloneImage(image,image->columns,image->rows,MagickTrue,
232 if (mask_image == (Image *) NULL)
233 return((Image *) NULL);
234 if (SetImageStorageClass(mask_image,DirectClass,exception) == MagickFalse)
236 mask_image=DestroyImage(mask_image);
237 return((Image *) NULL);
239 mask_image->alpha_trait=UndefinedPixelTrait;
240 (void) SetImageColorspace(mask_image,GRAYColorspace,exception);
245 image_view=AcquireVirtualCacheView(image,exception);
246 mask_view=AcquireAuthenticCacheView(mask_image,exception);
247 for (y=0; y < (ssize_t) image->rows; y++)
249 register const Quantum
258 if (status == MagickFalse)
260 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
261 q=QueueCacheViewAuthenticPixels(mask_view,0,y,mask_image->columns,1,
263 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
268 for (x=0; x < (ssize_t) image->columns; x++)
270 SetPixelChannel(mask_image,GrayPixelChannel,0,q);
271 SetPixelChannel(mask_image,GrayPixelChannel,GetPixelReadMask(image,p),q);
272 p+=GetPixelChannels(image);
273 q+=GetPixelChannels(mask_image);
275 if (SyncCacheViewAuthenticPixels(mask_view,exception) == MagickFalse)
278 mask_view=DestroyCacheView(mask_view);
279 image_view=DestroyCacheView(image_view);
280 if (status == MagickFalse)
281 mask_image=DestroyImage(mask_image);
285 static MagickBooleanType WriteMASKImage(const ImageInfo *image_info,
286 Image *image,ExceptionInfo *exception)
297 mask_image=MaskImage(image,exception);
298 if (mask_image == (Image *) NULL)
300 (void) CopyMagickString(mask_image->filename,image->filename,MagickPathExtent);
301 write_info=CloneImageInfo(image_info);
302 (void) SetImageInfo(write_info,1,exception);
303 if (LocaleCompare(write_info->magick,"MASK") == 0)
304 (void) FormatLocaleString(mask_image->filename,MagickPathExtent,"miff:%s",
305 write_info->filename);
306 status=WriteImage(write_info,mask_image,exception);
307 mask_image=DestroyImage(mask_image);
308 write_info=DestroyImageInfo(write_info);