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