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