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