root / ImageMagick / trunk / coders / plasma.c

Revision 11748, 10.3 kB (checked in by cristy, 4 weeks ago)
Line 
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-2008 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
39/*
40  Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/constitute.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/image.h"
49#include "magick/image-private.h"
50#include "magick/list.h"
51#include "magick/magick.h"
52#include "magick/memory_.h"
53#include "magick/monitor.h"
54#include "magick/monitor-private.h"
55#include "magick/random_.h"
56#include "magick/signature-private.h"
57#include "magick/quantum-private.h"
58#include "magick/static.h"
59#include "magick/string_.h"
60#include "magick/module.h"
61
62/*
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64%                                                                             %
65%                                                                             %
66%                                                                             %
67%   R e a d P L A S M A I m a g e                                             %
68%                                                                             %
69%                                                                             %
70%                                                                             %
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72%
73%  ReadPlasmaImage creates a plasma fractal image.  The image is
74%  initialized to the X server color as specified by the filename.
75%
76%  The format of the ReadPlasmaImage method is:
77%
78%      Image *ReadPlasmaImage(const ImageInfo *image_info,
79%        ExceptionInfo *exception)
80%
81%  A description of each parameter follows:
82%
83%    o image_info: the image info.
84%
85%    o exception: return any errors or warnings in this structure.
86%
87*/
88
89static inline size_t MagickMax(const size_t x,const size_t y)
90{
91  if (x > y)
92    return(x);
93  return(y);
94}
95
96static inline void PlasmaPixel(Image *image,double x,double y)
97{
98  QuantumAny
99    scale;
100
101  register PixelPacket
102    *q;
103
104  q=GetImagePixels(image,(long) ceil(x-0.5),(long) ceil(y-0.5),1,1);
105  if (q == (PixelPacket *) NULL)
106    return;
107  scale=GetQuantumScale(16UL);
108  q->red=ScaleAnyToQuantum((unsigned long) (65535.0*GetPseudoRandomValue()+
109    0.5),16UL,scale);
110  q->green=ScaleAnyToQuantum((unsigned long) (65535.0*GetPseudoRandomValue()+
111    0.5),16UL,scale);
112  q->blue=ScaleAnyToQuantum((unsigned long) (65535.0*GetPseudoRandomValue()+
113    0.5),16UL,scale);
114  (void) SyncImagePixels(image);
115}
116
117static Image *ReadPlasmaImage(const ImageInfo *image_info,
118  ExceptionInfo *exception)
119{
120  Image
121    *image;
122
123  ImageInfo
124    *read_info;
125
126  long
127    y;
128
129  MagickBooleanType
130    status;
131
132  register long
133    x;
134
135  register PixelPacket
136    *q;
137
138  register unsigned long
139    i;
140
141  SegmentInfo
142    segment_info;
143
144  unsigned long
145    depth,
146    max_depth;
147
148  /*
149    Recursively apply plasma to the image.
150  */
151  read_info=CloneImageInfo(image_info);
152  SetImageInfoBlob(read_info,(void *) NULL,0);
153  (void) FormatMagickString(read_info->filename,MaxTextExtent,
154    "gradient:%s",image_info->filename);
155  image=ReadImage(read_info,exception);
156  read_info=DestroyImageInfo(read_info);
157  if (image == (Image *) NULL)
158    return((Image *) NULL);
159  image->storage_class=DirectClass;
160  for (y=0; y < (long) image->rows; y++)
161  {
162    q=GetImagePixels(image,0,y,image->columns,1);
163    if (q == (PixelPacket *) NULL)
164      break;
165    for (x=0; x < (long) image->columns; x++)
166    {
167      q->opacity=(Quantum) (QuantumRange/2);
168      q++;
169    }
170    if (SyncImagePixels(image) == 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      /*
180        Seed pixels before recursion.
181      */
182      PlasmaPixel(image,segment_info.x1,segment_info.y1);
183      PlasmaPixel(image,segment_info.x1,(segment_info.y1+segment_info.y2)/2);
184      PlasmaPixel(image,segment_info.x1,segment_info.y2);
185      PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2,segment_info.y1);
186      PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2,
187        (segment_info.y1+segment_info.y2)/2);
188      PlasmaPixel(image,(segment_info.x1+segment_info.x2)/2,segment_info.y2);
189      PlasmaPixel(image,segment_info.x2,segment_info.y1);
190      PlasmaPixel(image,segment_info.x2,(segment_info.y1+segment_info.y2)/2);
191      PlasmaPixel(image,segment_info.x2,segment_info.y2);
192    }
193  i=(unsigned long) MagickMax(image->columns,image->rows)/2;
194  for (max_depth=0; i != 0; max_depth++)
195    i>>=1;
196  for (depth=1; ; depth++)
197  {
198    if (PlasmaImage(image,&segment_info,0,depth) != MagickFalse)
199      break;
200    status=SetImageProgress(image,LoadImageTag,depth,max_depth);
201    if (status == MagickFalse)
202      break;
203  }
204  (void) SetImageAlphaChannel(image,DeactivateAlphaChannel);
205  return(GetFirstImageInList(image));
206}
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210%                                                                             %
211%                                                                             %
212%                                                                             %
213%   R e g i s t e r P L A S M A I m a g e                                     %
214%                                                                             %
215%                                                                             %
216%                                                                             %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
219%  RegisterPLASMAImage() adds attributes for the Plasma image format to
220%  the list of supported formats.  The attributes include the image format
221%  tag, a method to read and/or write the format, whether the format
222%  supports the saving of more than one frame to the same file or blob,
223%  whether the format supports native in-memory I/O, and a brief
224%  description of the format.
225%
226%  The format of the RegisterPLASMAImage method is:
227%
228%      unsigned long RegisterPLASMAImage(void)
229%
230*/
231ModuleExport unsigned long RegisterPLASMAImage(void)
232{
233  MagickInfo
234    *entry;
235
236  entry=SetMagickInfo("PLASMA");
237  entry->decoder=(DecodeImageHandler *) ReadPlasmaImage;
238  entry->adjoin=MagickFalse;
239  entry->format_type=ImplicitFormatType;
240  entry->description=ConstantString("Plasma fractal image");
241  entry->module=ConstantString("PLASMA");
242  (void) RegisterMagickInfo(entry);
243  entry=SetMagickInfo("FRACTAL");
244  entry->decoder=(DecodeImageHandler *) ReadPlasmaImage;
245  entry->adjoin=MagickFalse;
246  entry->format_type=ImplicitFormatType;
247  entry->description=ConstantString("Plasma fractal image");
248  entry->module=ConstantString("PLASMA");
249  (void) RegisterMagickInfo(entry);
250  return(MagickImageCoderSignature);
251}
252
253/*
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255%                                                                             %
256%                                                                             %
257%                                                                             %
258%   U n r e g i s t e r P L A S M A I m a g e                                 %
259%                                                                             %
260%                                                                             %
261%                                                                             %
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263%
264%  UnregisterPLASMAImage() removes format registrations made by the
265%  PLASMA module from the list of supported formats.
266%
267%  The format of the UnregisterPLASMAImage method is:
268%
269%      UnregisterPLASMAImage(void)
270%
271*/
272ModuleExport void UnregisterPLASMAImage(void)
273{
274  (void) UnregisterMagickInfo("FRACTAL");
275  (void) UnregisterMagickInfo("PLASMA");
276}
Note: See TracBrowser for help on using the browser.