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 % Cristy %
17 % July 1992 %
18 % %
19 % %
20 % Copyright 1999-2021 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 % https://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 /*
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/channel.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/paint.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/studio.h"
66
67 /*
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % %
70 % %
71 % %
72 % R e a d G R A D I E N T I m a g e %
73 % %
74 % %
75 % %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %
78 % ReadGRADIENTImage creates a gradient image and initializes it to
79 % the color range as specified by the filename. It allocates the memory
80 % necessary for the new Image structure and returns a pointer to the new
81 % image.
82 %
83 % The format of the ReadGRADIENTImage method is:
84 %
85 % Image *ReadGRADIENTImage(const ImageInfo *image_info,
86 % ExceptionInfo *exception)
87 %
88 % A description of each parameter follows:
89 %
90 % o image_info: the image info.
91 %
92 % o exception: return any errors or warnings in this structure.
93 %
94 */
95
ReadXCImage(const ImageInfo * image_info,ExceptionInfo * exception)96 static Image *ReadXCImage(const ImageInfo *image_info,ExceptionInfo *exception)
97 {
98 Image
99 *image;
100
101 MagickBooleanType
102 status;
103
104 PixelInfo
105 pixel;
106
107 ssize_t
108 x;
109
110 Quantum
111 *q;
112
113 ssize_t
114 y;
115
116 /*
117 Initialize Image structure.
118 */
119 assert(image_info != (const ImageInfo *) NULL);
120 assert(image_info->signature == MagickCoreSignature);
121 if (image_info->debug != MagickFalse)
122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
123 image_info->filename);
124 assert(exception != (ExceptionInfo *) NULL);
125 assert(exception->signature == MagickCoreSignature);
126 image=AcquireImage(image_info,exception);
127 if (image->columns == 0)
128 image->columns=1;
129 if (image->rows == 0)
130 image->rows=1;
131 status=SetImageExtent(image,image->columns,image->rows,exception);
132 if (status == MagickFalse)
133 return(DestroyImageList(image));
134 (void) CopyMagickString(image->filename,image_info->filename,
135 MagickPathExtent);
136 if (*image_info->filename == '\0')
137 pixel=image->background_color;
138 else
139 {
140 status=QueryColorCompliance((char *) image_info->filename,AllCompliance,
141 &pixel,exception);
142 if (status == MagickFalse)
143 {
144 image=DestroyImage(image);
145 return((Image *) NULL);
146 }
147 }
148 (void) SetImageColorspace(image,pixel.colorspace,exception);
149 image->alpha_trait=pixel.alpha_trait;
150 for (y=0; y < (ssize_t) image->rows; y++)
151 {
152 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
153 if (q == (Quantum *) NULL)
154 break;
155 for (x=0; x < (ssize_t) image->columns; x++)
156 {
157 SetPixelViaPixelInfo(image,&pixel,q);
158 q+=GetPixelChannels(image);
159 }
160 if (SyncAuthenticPixels(image,exception) == MagickFalse)
161 break;
162 }
163 return(GetFirstImageInList(image));
164 }
165
ReadGRADIENTImage(const ImageInfo * image_info,ExceptionInfo * exception)166 static Image *ReadGRADIENTImage(const ImageInfo *image_info,
167 ExceptionInfo *exception)
168 {
169 char
170 start_color[MagickPathExtent],
171 stop_color[MagickPathExtent];
172
173 Image
174 *image;
175
176 ImageInfo
177 *read_info;
178
179 MagickBooleanType
180 status;
181
182 StopInfo
183 *stops;
184
185 /*
186 Identify start and stop gradient colors.
187 */
188 assert(image_info != (const ImageInfo *) NULL);
189 assert(image_info->signature == MagickCoreSignature);
190 if (image_info->debug != MagickFalse)
191 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
192 image_info->filename);
193 assert(exception != (ExceptionInfo *) NULL);
194 assert(exception->signature == MagickCoreSignature);
195 (void) CopyMagickString(start_color,"white",MagickPathExtent);
196 (void) CopyMagickString(stop_color,"black",MagickPathExtent);
197 if (*image_info->filename != '\0')
198 {
199 char
200 *p;
201
202 (void) CopyMagickString(start_color,image_info->filename,
203 MagickPathExtent);
204 for (p=start_color; (*p != '-') && (*p != '\0'); p++)
205 if (*p == '(')
206 {
207 for (p++; (*p != ')') && (*p != '\0'); p++);
208 if (*p == '\0')
209 break;
210 }
211 if (*p == '-')
212 (void) CopyMagickString(stop_color,p+1,MagickPathExtent);
213 *p='\0';
214 }
215 /*
216 Create base gradient image from start color.
217 */
218 read_info=CloneImageInfo(image_info);
219 SetImageInfoBlob(read_info,(void *) NULL,0);
220 (void) CopyMagickString(read_info->filename,start_color,MagickPathExtent);
221 image=ReadXCImage(read_info,exception);
222 read_info=DestroyImageInfo(read_info);
223 if (image == (Image *) NULL)
224 return((Image *) NULL);
225 /*
226 Create gradient stops.
227 */
228 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
229 if (stops == (StopInfo *) NULL)
230 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
231 stops[0].offset=0.0;
232 stops[1].offset=1.0;
233 status=QueryColorCompliance(start_color,AllCompliance,&stops[0].color,
234 exception);
235 if (status != MagickFalse)
236 status=QueryColorCompliance(stop_color,AllCompliance,&stops[1].color,
237 exception);
238 if (status == MagickFalse)
239 {
240 stops=(StopInfo *) RelinquishMagickMemory(stops);
241 image=DestroyImage(image);
242 return((Image *) NULL);
243 }
244 (void) SetImageColorspace(image,stops[0].color.colorspace,exception);
245 if ((stops[0].color.alpha_trait != UndefinedPixelTrait) ||
246 (stops[1].color.alpha_trait != UndefinedPixelTrait))
247 SetImageAlpha(image,TransparentAlpha,exception);
248 /*
249 Paint gradient.
250 */
251 status=GradientImage(image,LocaleCompare(image_info->magick,"GRADIENT") == 0 ?
252 LinearGradient : RadialGradient,PadSpread,stops,2,exception);
253 stops=(StopInfo *) RelinquishMagickMemory(stops);
254 if (status == MagickFalse)
255 {
256 image=DestroyImageList(image);
257 return((Image *) NULL);
258 }
259 return(GetFirstImageInList(image));
260 }
261
262 /*
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 % %
265 % %
266 % %
267 % R e g i s t e r G R A D I E N T I m a g e %
268 % %
269 % %
270 % %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %
273 % RegisterGRADIENTImage() adds attributes for the GRADIENT image format
274 % to the list of supported formats. The attributes include the image format
275 % tag, a method to read and/or write the format, whether the format
276 % supports the saving of more than one frame to the same file or blob,
277 % whether the format supports native in-memory I/O, and a brief
278 % description of the format.
279 %
280 % The format of the RegisterGRADIENTImage method is:
281 %
282 % size_t RegisterGRADIENTImage(void)
283 %
284 */
RegisterGRADIENTImage(void)285 ModuleExport size_t RegisterGRADIENTImage(void)
286 {
287 MagickInfo
288 *entry;
289
290 entry=AcquireMagickInfo("GRADIENT","GRADIENT",
291 "Gradual linear passing from one shade to another");
292 entry->decoder=(DecodeImageHandler *) ReadGRADIENTImage;
293 entry->flags^=CoderAdjoinFlag;
294 entry->flags|=CoderRawSupportFlag;
295 entry->format_type=ImplicitFormatType;
296 (void) RegisterMagickInfo(entry);
297 entry=AcquireMagickInfo("GRADIENT","RADIAL-GRADIENT",
298 "Gradual radial passing from one shade to another");
299 entry->decoder=(DecodeImageHandler *) ReadGRADIENTImage;
300 entry->flags^=CoderAdjoinFlag;
301 entry->flags|=CoderRawSupportFlag;
302 entry->format_type=ImplicitFormatType;
303 (void) RegisterMagickInfo(entry);
304 return(MagickImageCoderSignature);
305 }
306
307 /*
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % %
310 % %
311 % %
312 % U n r e g i s t e r G R A D I E N T I m a g e %
313 % %
314 % %
315 % %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 %
318 % UnregisterGRADIENTImage() removes format registrations made by the
319 % GRADIENT module from the list of supported formats.
320 %
321 % The format of the UnregisterGRADIENTImage method is:
322 %
323 % UnregisterGRADIENTImage(void)
324 %
325 */
UnregisterGRADIENTImage(void)326 ModuleExport void UnregisterGRADIENTImage(void)
327 {
328 (void) UnregisterMagickInfo("RADIAL-GRADIENT");
329 (void) UnregisterMagickInfo("GRADIENT");
330 }
331