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.
23 # allow access through module and without
24 from array
import array
35 def python_conformal(image
, indrawable
, code
, xl
, xr
, yt
, yb
, grid
):
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
)]
43 for drawable
in drawables
:
44 image
.add_layer(drawable
, l
)
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()
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
)
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
):
69 for col
in range(0, width
):
70 z
= col
/sx
+ xl
+ 1j
*( yt
- row
/sy
)
72 arg
= math
.atan2(w
.imag
, w
.real
)
75 args
= args
+ ( arg
/(2*math
.pi
) ,)
77 mod
= ( math
.log(w
.imag
**2+w
.real
**2)/ml2
) % 1.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
:
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])
109 "python_fu_conformal",
110 "Colour representation of a conformal map",
111 "Colour representation of a conformal map",
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),