1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                 PPPP   L       AAA   SSSSS  M   M   AAA                     %
7 %                 P   P  L      A   A  SS     MM MM  A   A                    %
8 %                 PPPP   L      AAAAA   SSS   M M M  AAAAA                    %
9 %                 P      L      A   A     SS  M   M  A   A                    %
10 %                 P      LLLLL  A   A  SSSSS  M   M  A   A                    %
11 %                                                                             %
12 %                                                                             %
13 %                          Read a Plasma Image.                               %
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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/channel.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/constitute.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/module.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/random_.h"
61 #include "MagickCore/random-private.h"
62 #include "MagickCore/signature-private.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/visual-effects.h"
67 
68 /*
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %                                                                             %
71 %                                                                             %
72 %                                                                             %
73 %   R e a d P L A S M A I m a g e                                             %
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %
79 %  ReadPlasmaImage creates a plasma fractal image.  The image is
80 %  initialized to the X server color as specified by the filename.
81 %
82 %  The format of the ReadPlasmaImage method is:
83 %
84 %      Image *ReadPlasmaImage(const ImageInfo *image_info,
85 %        ExceptionInfo *exception)
86 %
87 %  A description of each parameter follows:
88 %
89 %    o image_info: the image info.
90 %
91 %    o exception: return any errors or warnings in this structure.
92 %
93 */
94 
PlasmaPixel(Image * image,RandomInfo * magick_restrict random_info,const double x,const double y,ExceptionInfo * exception)95 static inline MagickBooleanType PlasmaPixel(Image *image,
96   RandomInfo *magick_restrict random_info,const double x,const double y,
97   ExceptionInfo *exception)
98 {
99   Quantum
100     *q;
101 
102   q=GetAuthenticPixels(image,(ssize_t) (x+0.5),(ssize_t) (y+0.5),1,1,
103     exception);
104   if (q == (Quantum *) NULL)
105     return(MagickFalse);
106   SetPixelRed(image,(Quantum) (QuantumRange*
107     GetPseudoRandomValue(random_info)+0.5),q);
108   SetPixelGreen(image,(Quantum) (QuantumRange*
109     GetPseudoRandomValue(random_info)+0.5),q);
110   SetPixelBlue(image,(Quantum) (QuantumRange*
111     GetPseudoRandomValue(random_info)+0.5),q);
112   return(SyncAuthenticPixels(image,exception));
113 }
114 
ReadPlasmaImage(const ImageInfo * image_info,ExceptionInfo * exception)115 static Image *ReadPlasmaImage(const ImageInfo *image_info,
116   ExceptionInfo *exception)
117 {
118   Image
119     *image;
120 
121   ImageInfo
122     *read_info;
123 
124   MagickStatusType
125     status;
126 
127   ssize_t
128     x;
129 
130   Quantum
131     *q;
132 
133   size_t
134     i;
135 
136   SegmentInfo
137     segment_info;
138 
139   size_t
140     depth,
141     max_depth;
142 
143   ssize_t
144     y;
145 
146   /*
147     Recursively apply plasma to the image.
148   */
149   read_info=CloneImageInfo(image_info);
150   SetImageInfoBlob(read_info,(void *) NULL,0);
151   (void) FormatLocaleString(read_info->filename,MagickPathExtent,
152     "gradient:%s",image_info->filename);
153   image=ReadImage(read_info,exception);
154   read_info=DestroyImageInfo(read_info);
155   if (image == (Image *) NULL)
156     return((Image *) NULL);
157   (void) SetImageStorageClass(image,DirectClass,exception);
158   if (IsGrayColorspace(image->colorspace) != MagickFalse)
159     (void) SetImageColorspace(image,sRGBColorspace,exception);
160   for (y=0; y < (ssize_t) image->rows; y++)
161   {
162     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
163     if (q == (Quantum *) NULL)
164       break;
165     for (x=0; x < (ssize_t) image->columns; x++)
166     {
167       SetPixelAlpha(image,QuantumRange/2,q);
168       q+=GetPixelChannels(image);
169     }
170     if (SyncAuthenticPixels(image,exception) == MagickFalse)
171       break;
172   }
173   segment_info.x1=0;
174   segment_info.y1=0;
175   segment_info.x2=(double) image->columns-1;
176   segment_info.y2=(double) image->rows-1;
177   if (LocaleCompare(image_info->filename,"fractal") == 0)
178     {
179       RandomInfo
180         *random_info;
181 
182       /*
183         Seed pixels before recursion.
184       */
185       (void) SetImageColorspace(image,sRGBColorspace,exception);
186       random_info=AcquireRandomInfo();
187       status=PlasmaPixel(image,random_info,segment_info.x1,segment_info.y1,
188         exception);
189       status&=PlasmaPixel(image,random_info,segment_info.x1,(segment_info.y1+
190         segment_info.y2)/2,exception);
191       status&=PlasmaPixel(image,random_info,segment_info.x1,segment_info.y2,
192         exception);
193       status&=PlasmaPixel(image,random_info,(segment_info.x1+segment_info.x2)/2,
194         segment_info.y1,exception);
195       status&=PlasmaPixel(image,random_info,(segment_info.x1+segment_info.x2)/2,
196         (segment_info.y1+segment_info.y2)/2,exception);
197       status&=PlasmaPixel(image,random_info,(segment_info.x1+segment_info.x2)/2,
198         segment_info.y2,exception);
199       status&=PlasmaPixel(image,random_info,segment_info.x2,segment_info.y1,
200         exception);
201       status&=PlasmaPixel(image,random_info,segment_info.x2,(segment_info.y1+
202         segment_info.y2)/2,exception);
203       status&=PlasmaPixel(image,random_info,segment_info.x2,segment_info.y2,
204         exception);
205       random_info=DestroyRandomInfo(random_info);
206       if (status == MagickFalse)
207         return(image);
208     }
209   i=(size_t) MagickMax(image->columns,image->rows)/2;
210   for (max_depth=0; i != 0; max_depth++)
211     i>>=1;
212   for (depth=1; ; depth++)
213   {
214     if (PlasmaImage(image,&segment_info,0,depth,exception) != MagickFalse)
215       break;
216     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) depth,
217       max_depth);
218     if (status == MagickFalse)
219       break;
220   }
221   return(GetFirstImageInList(image));
222 }
223 
224 /*
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 %                                                                             %
227 %                                                                             %
228 %                                                                             %
229 %   R e g i s t e r P L A S M A I m a g e                                     %
230 %                                                                             %
231 %                                                                             %
232 %                                                                             %
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 %
235 %  RegisterPLASMAImage() adds attributes for the Plasma image format to
236 %  the list of supported formats.  The attributes include the image format
237 %  tag, a method to read and/or write the format, whether the format
238 %  supports the saving of more than one frame to the same file or blob,
239 %  whether the format supports native in-memory I/O, and a brief
240 %  description of the format.
241 %
242 %  The format of the RegisterPLASMAImage method is:
243 %
244 %      size_t RegisterPLASMAImage(void)
245 %
246 */
RegisterPLASMAImage(void)247 ModuleExport size_t RegisterPLASMAImage(void)
248 {
249   MagickInfo
250     *entry;
251 
252   entry=AcquireMagickInfo("PLASMA","PLASMA","Plasma fractal image");
253   entry->decoder=(DecodeImageHandler *) ReadPlasmaImage;
254   entry->flags^=CoderAdjoinFlag;
255   entry->format_type=ImplicitFormatType;
256   (void) RegisterMagickInfo(entry);
257   entry=AcquireMagickInfo("PLASMA","FRACTAL","Plasma fractal image");
258   entry->decoder=(DecodeImageHandler *) ReadPlasmaImage;
259   entry->flags^=CoderAdjoinFlag;
260   entry->format_type=ImplicitFormatType;
261   (void) RegisterMagickInfo(entry);
262   return(MagickImageCoderSignature);
263 }
264 
265 /*
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 %                                                                             %
268 %                                                                             %
269 %                                                                             %
270 %   U n r e g i s t e r P L A S M A I m a g e                                 %
271 %                                                                             %
272 %                                                                             %
273 %                                                                             %
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 %
276 %  UnregisterPLASMAImage() removes format registrations made by the
277 %  PLASMA module from the list of supported formats.
278 %
279 %  The format of the UnregisterPLASMAImage method is:
280 %
281 %      UnregisterPLASMAImage(void)
282 %
283 */
UnregisterPLASMAImage(void)284 ModuleExport void UnregisterPLASMAImage(void)
285 {
286   (void) UnregisterMagickInfo("FRACTAL");
287   (void) UnregisterMagickInfo("PLASMA");
288 }
289