]> granicus.if.org Git - imagemagick/blob - coders/gradient.c
(no commit message)
[imagemagick] / coders / gradient.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %            GGGG  RRRR    AAA   DDDD   IIIII  EEEEE  N   N  TTTTT            %
7 %           G      R   R  A   A  D   D    I    E      NN  N    T              %
8 %           G  GG  RRRR   AAAAA  D   D    I    EEE    N N N    T              %
9 %           G   G  R R    A   A  D   D    I    E      N  NN    T              %
10 %            GGG   R  R   A   A  DDDD   IIIII  EEEEE  N   N    T              %
11 %                                                                             %
12 %                                                                             %
13 %                   Read An Image Filled Using Gradient.                      %
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/color.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colorspace-private.h"
49 #include "MagickCore/draw.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/paint.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/studio.h"
64 \f
65 /*
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %   R e a d G R A D I E N T I m a g e                                         %
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %
76 %  ReadGRADIENTImage creates a gradient image and initializes it to
77 %  the color range as specified by the filename.  It allocates the memory
78 %  necessary for the new Image structure and returns a pointer to the new
79 %  image.
80 %
81 %  The format of the ReadGRADIENTImage method is:
82 %
83 %      Image *ReadGRADIENTImage(const ImageInfo *image_info,
84 %        ExceptionInfo *exception)
85 %
86 %  A description of each parameter follows:
87 %
88 %    o image_info: the image info.
89 %
90 %    o exception: return any errors or warnings in this structure.
91 %
92 */
93 static Image *ReadGRADIENTImage(const ImageInfo *image_info,
94   ExceptionInfo *exception)
95 {
96   char
97     colorname[MaxTextExtent];
98
99   Image
100     *image;
101
102   MagickBooleanType
103     icc_color,
104     status;
105
106   PixelInfo
107     start_color,
108     stop_color;
109
110   /*
111     Initialize Image structure.
112   */
113   assert(image_info != (const ImageInfo *) NULL);
114   assert(image_info->signature == MagickSignature);
115   if (image_info->debug != MagickFalse)
116     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
117       image_info->filename);
118   assert(exception != (ExceptionInfo *) NULL);
119   assert(exception->signature == MagickSignature);
120   image=AcquireImage(image_info,exception);
121   if ((image->columns == 0) || (image->rows == 0))
122     ThrowReaderException(OptionError,"MustSpecifyImageSize");
123   (void) SetImageAlpha(image,(Quantum) TransparentAlpha,exception);
124   (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
125   (void) CopyMagickString(colorname,image_info->filename,MaxTextExtent);
126   (void) sscanf(image_info->filename,"%[^-]",colorname);
127   icc_color=MagickFalse;
128   if (LocaleCompare(colorname,"icc") == 0)
129     {
130       (void) ConcatenateMagickString(colorname,"-",MaxTextExtent);
131       (void) sscanf(image_info->filename,"%*[^-]-%[^-]",colorname+4);
132       icc_color=MagickTrue;
133     }
134   status=QueryColorCompliance(colorname,AllCompliance,&start_color,exception);
135   if (status == MagickFalse)
136     {
137       image=DestroyImage(image);
138       return((Image *) NULL);
139     }
140   (void) CopyMagickString(colorname,"white",MaxTextExtent);
141   if (GetPixelInfoIntensity(&start_color) > (Quantum) (QuantumRange/2))
142     (void) CopyMagickString(colorname,"black",MaxTextExtent);
143   if (icc_color == MagickFalse)
144     (void) sscanf(image_info->filename,"%*[^-]-%s",colorname);
145   else
146     (void) sscanf(image_info->filename,"%*[^-]-%*[^-]-%s",colorname);
147   status=QueryColorCompliance(colorname,AllCompliance,&stop_color,exception);
148   if (status == MagickFalse)
149     {
150       image=DestroyImage(image);
151       return((Image *) NULL);
152     }
153   status=GradientImage(image,LocaleCompare(image_info->magick,"GRADIENT") == 0 ?
154     LinearGradient : RadialGradient,PadSpread,&start_color,&stop_color,
155     exception);
156   if (status == MagickFalse)
157     {
158       image=DestroyImageList(image);
159       return((Image *) NULL);
160     }
161   if ((start_color.matte == MagickFalse) && (stop_color.matte == MagickFalse))
162     (void) SetImageAlphaChannel(image,DeactivateAlphaChannel,exception);
163   return(GetFirstImageInList(image));
164 }
165 \f
166 /*
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 %                                                                             %
169 %                                                                             %
170 %                                                                             %
171 %   R e g i s t e r G R A D I E N T I m a g e                                 %
172 %                                                                             %
173 %                                                                             %
174 %                                                                             %
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 %
177 %  RegisterGRADIENTImage() adds attributes for the GRADIENT image format
178 %  to the list of supported formats.  The attributes include the image format
179 %  tag, a method to read and/or write the format, whether the format
180 %  supports the saving of more than one frame to the same file or blob,
181 %  whether the format supports native in-memory I/O, and a brief
182 %  description of the format.
183 %
184 %  The format of the RegisterGRADIENTImage method is:
185 %
186 %      size_t RegisterGRADIENTImage(void)
187 %
188 */
189 ModuleExport size_t RegisterGRADIENTImage(void)
190 {
191   MagickInfo
192     *entry;
193
194   entry=SetMagickInfo("GRADIENT");
195   entry->decoder=(DecodeImageHandler *) ReadGRADIENTImage;
196   entry->adjoin=MagickFalse;
197   entry->raw=MagickTrue;
198   entry->format_type=ImplicitFormatType;
199   entry->description=ConstantString("Gradual linear passing from one shade to "
200     "another");
201   entry->module=ConstantString("GRADIENT");
202   (void) RegisterMagickInfo(entry);
203   entry=SetMagickInfo("RADIAL-GRADIENT");
204   entry->decoder=(DecodeImageHandler *) ReadGRADIENTImage;
205   entry->adjoin=MagickFalse;
206   entry->raw=MagickTrue;
207   entry->format_type=ImplicitFormatType;
208   entry->description=ConstantString("Gradual radial passing from one shade to "
209     "another");
210   entry->module=ConstantString("GRADIENT");
211   (void) RegisterMagickInfo(entry);
212   return(MagickImageCoderSignature);
213 }
214 \f
215 /*
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %                                                                             %
218 %                                                                             %
219 %                                                                             %
220 %   U n r e g i s t e r G R A D I E N T I m a g e                             %
221 %                                                                             %
222 %                                                                             %
223 %                                                                             %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 %
226 %  UnregisterGRADIENTImage() removes format registrations made by the
227 %  GRADIENT module from the list of supported formats.
228 %
229 %  The format of the UnregisterGRADIENTImage method is:
230 %
231 %      UnregisterGRADIENTImage(void)
232 %
233 */
234 ModuleExport void UnregisterGRADIENTImage(void)
235 {
236   (void) UnregisterMagickInfo("RADIAL-GRADIENT");
237   (void) UnregisterMagickInfo("GRADIENT");
238 }