Remove comment cruft
[conformal.git] / conformal.py
blobde2154e3f6e71326c77fbae8e5458e6ded854628
1 #!/usr/bin/env python
3 # Gimp-Python - allows the writing of Gimp plugins in Python.
4 # Copyright (C) 1997 James Henstridge <james@daa.com.au>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 import math, cmath
21 from math import *
22 from cmath import *
23 # allow access through module and without
24 from array import array
25 from gimpfu import *
26 try:
27 from fpconst import *
28 import scipy.special
29 except ImportError:
30 pass
32 #import psyco
33 #psyco.full()
35 def python_conformal(image, indrawable, code, xl, xr, yt, yb, grid):
36 width = image.width
37 height = image.height
38 drawables = [ gimp.Layer(image, "Argument", width, height, RGBA_IMAGE, 100, NORMAL_MODE),
39 gimp.Layer(image, "Log. modulus", width, height, RGBA_IMAGE, 35, DARKEN_ONLY_MODE),
40 gimp.Layer(image, "Grid", width, height, RGBA_IMAGE, 10, DARKEN_ONLY_MODE)]
41 image.disable_undo()
42 l = 1
43 for drawable in drawables:
44 image.add_layer(drawable, l)
45 l = -1
46 image.remove_layer(indrawable)
48 bpp = drawables[0].bpp
50 gimp.tile_cache_ntiles(2 * (width + 63) / 64)
52 dest_rgns = [ drawable.get_pixel_rgn(0, 0, width, height, True, False) for drawable in drawables ]
53 gradient = gimp.context_get_gradient()
54 progress = 0
55 max_progress = width * height
56 gimp.progress_init("Conformally Mapping...")
57 sx = (width-1.0)/(xr-xl)
58 sy = (height-1.0)/(yt-yb)
59 w = complex(0.0)
60 z = complex(0.0)
61 cx, cy = 0, 0
62 ml2 = 2.0*math.log(2) # no need to do this 500*500 times...
63 compiled=compile(code, "compiled code", "exec", 0, 1)
65 for row in range(0, height):
66 args = ()
67 mods = ()
68 sqrs = ()
69 for col in range(0, width):
70 z = col/sx + xl + 1j*( yt - row/sy)
71 exec(compiled)
72 arg = math.atan2(w.imag, w.real)
73 if arg<0.0:
74 arg = arg + 2*math.pi
75 args = args + ( arg/(2*math.pi) ,)
76 try:
77 mod = ( math.log(w.imag**2+w.real**2)/ml2 ) % 1.0
78 except OverflowError:
79 mod=0.0
80 mods = mods + ( mod ,)
81 sqrs = sqrs + (bpp-1)*( 255*(((int)(w.imag/grid % 2.0) + (int)(w.real/grid % 2.0)) % 2) ,) + (255, )
83 samples = gimp.gradient_get_custom_samples(gradient, args)
84 top_p = array("B", [ ((int)(255*samples[col][i]+0.5)) for col in range(0, width) for i in range(bpp) ] )
86 dest_rgns[0][0:width, row] = top_p.tostring()
88 samples = gimp.gradient_get_custom_samples("Default", mods)
89 top_p = array("B", [ ((int)(255*samples[col][i]+0.5)) for col in range(0, width) for i in range(bpp) ] )
90 dest_rgns[1][0:width, row] = top_p.tostring()
92 top_p = array("B", sqrs )
93 dest_rgns[2][0:width, row] = top_p.tostring()
95 progress = progress + width
96 gimp.progress_update(float(progress) / max_progress)
98 for drawable in drawables:
99 drawable.flush()
100 drawable.update(0,0,width,height)
101 pdb.plug_in_edge(image,drawables[2], 10, 0, 0) # amount, WRAP, SOBEL
102 pdb.plug_in_vinvert(image,drawables[2])
104 image.enable_undo()
108 register(
109 "python_fu_conformal",
110 "Colour representation of a conformal map",
111 "Colour representation of a conformal map",
112 "Michael J Gruber",
113 "Michael J Gruber",
114 "2005",
115 "<Image>/Filters/Render/_Conformal ...",
116 "RGB*, GRAY*, INDEXED*",
118 (PF_TEXT, "code", "code", "w=z"),
119 (PF_FLOAT, "xl", "x left", -1.0),
120 (PF_FLOAT, "xr", "x right", 1.0),
121 (PF_FLOAT, "yt", "y top", 1.0),
122 (PF_FLOAT, "yb", "y bottom", -1.0),
123 (PF_FLOAT, "grid", "grid", 1.0),
126 python_conformal)
128 main()