GIMP Script-fu

Changes On Branch schemey-fu
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch schemey-fu Excluding Merge-Ins

This is equivalent to a diff from cf23bd898b to 1d3eeebbcb

2015-07-09
12:03
Added a new mode to get-layers which includes fundaments for nested groups. Leaf check-in: db8f2ac5bf user: saul tags: trunk
2015-05-26
06:40
Updated docs. Leaf check-in: 1d3eeebbcb user: saul tags: schemey-fu
2015-05-24
16:29
Update get-layers to support modes 0, 1, and 2. check-in: fd3793f503 user: saul tags: schemey-fu
2015-05-22
21:34
Create new branch named "sg-layer-bake" check-in: c4e75ba86e user: saul tags: sg-layer-bake
2015-03-27
01:38
Create new branch named "sg-group-linked-layers" check-in: a8849c0376 user: saul tags: sg-group-linked-layers
2015-03-25
17:33
Create new branch named "sg-decompose-text-layer" check-in: a1515524c2 user: saul tags: sg-decompose-text-layer
2015-03-17
18:49
Create new branch named "sg-render-palette-preview" check-in: 4f1cbefb4c user: saul tags: sg-render-palette-preview
2015-02-25
08:28
Create new branch named "schemey-fu" check-in: 5bbe976611 user: saul tags: schemey-fu
2015-02-24
07:14
Create new branch named "sg-schemy-fu" check-in: 07ab821547 user: saul tags: sg-schemy-fu
2015-02-08
14:29
Create new branch named "sg-ungroup-layer" check-in: 5efa4c1218 user: saul tags: sg-ungroup-layer
12:53
Create new branch named "sg-splice-grouped-layer" Closed-Leaf check-in: 5e982d637f user: saul
12:35
Create new branch named "sg-splice-group" Closed-Leaf check-in: 1c8774511c user: saul
2015-02-04
08:25
Create new branch named "sg-add-group-mask" check-in: 0d5aa5c54f user: saul tags: sg-add-group-mask
2015-01-24
20:37
Create new branch named "sg-text-sphere" check-in: 836e1da14d user: saul tags: sg-text-sphere
2014-12-19
23:46
Create new branch named "sg-dump-path-colors" check-in: c5f6d76f4c user: saul tags: sg-dump-path-colors
2014-12-06
17:40
Create new branch named "sg-float-layer" check-in: 9e12bc0967 user: saul tags: sg-float-layer
2014-11-08
23:30
Create new branch named "sg-save-all-layers-to-directory" check-in: f711177647 user: saul tags: sg-save-all-layers-to-directory
23:28
Create new branch named "sg-save-all-layers-to-directory.scm" Leaf check-in: 1745a103d0 user: saul tags: sg-save-all-layers-to-directory.scm
2014-11-04
05:56
Create new branch named "sg-save-pdf-multi" check-in: f1259633d4 user: saul tags: sg-save-pdf-multi
2014-10-19
18:33
Create new branch named "sg-split-strokes" check-in: db1b5362c8 user: saul tags: sg-split-strokes
2014-10-14
10:06
Create new branch named "srfi26-cute-cut" check-in: e6d98719ae user: saul tags: srfi26-cute-cut
2014-10-05
02:21
Create new branch named "sg-fvful" check-in: d8b478ea27 user: saul tags: sg-fvful
2014-09-13
08:36
Create new branch named "sg-merge-retain-rgb" check-in: e37665ce07 user: saul tags: sg-merge-retain-rgb
2014-07-13
11:16
Create new branch named "sg-grid-layer" check-in: 9c302f4f80 user: saul tags: sg-grid-layer
2014-06-04
14:51
Create new branch named "sg-de-mirror" check-in: 506e951f8f user: saul tags: sg-de-mirror
2014-04-04
23:24
Create new branch named "sg-STW-brochures" check-in: 1339ddf45c user: saul tags: sg-STW-brochures
2014-03-23
18:37
Create new branch named "sg-process-tga-files" check-in: aa1fa9108d user: saul tags: sg-process-tga-files
2014-03-22
00:16
Create new branch named "sg-with-files" check-in: d7e5d36797 user: saul tags: sg-with-files
2014-03-17
12:12
Create new branch named "sg-sequential-edit" check-in: 68950ba1e6 user: saul tags: sg-sequential-edit
2014-03-04
22:49
Create new branch named "sg-save-all-projections" check-in: 3750033250 user: saul tags: sg-save-all-projections
2014-03-01
17:28
Create new branch named "sg-stash-guides" check-in: 3135a9f5a4 user: saul tags: sg-stash-guides
2014-02-03
18:57
Create new branch named "fu-lib" check-in: 6703e22f7b user: saul tags: fu-lib
18:57
Ignore this branch named "fu-lib.scm" Closed-Leaf check-in: 38aac1784f user: saul
2014-01-09
01:36
Create new branch named "sg-alpha-to-svg-file" check-in: 68637c0508 user: saul tags: sg-alpha-to-svg-file
2014-01-02
19:10
Create new branch named "sg-process-layers" check-in: 23b79b0996 user: saul tags: sg-process-layers
2013-12-14
18:20
Create new branch named "fireanim" check-in: 165d30ddd5 user: saul tags: fireanim
2013-12-02
17:16
Create new branch named "sg-save-path-as-poly.scm" check-in: ac5ab91cc5 user: saul tags: sg-save-path-as-poly.scm
2013-11-24
16:12
Create new branch named "sg-3d-ify" check-in: 4a079c03bc user: saul tags: sg-3d-ify
2013-11-13
07:06
Create new branch named "sg-luminosity-masks" check-in: 1df9a40cf0 user: saul tags: sg-luminosity-masks
2013-09-17
16:43
Create new branch named "sg-kumar-kovuru" check-in: e5d1d3f8b3 user: saul tags: sg-kumar-kovuru
2013-07-16
04:21
Create new branch named "sg-pseudo-depth-map" check-in: fd9ac68f7e user: saul tags: sg-pseudo-depth-map
2013-07-06
23:12
Create new branch named "sg-dissolve-group" check-in: ed925e7d9c user: saul tags: sg-dissolve-group
2013-07-04
01:32
Create new branch named "sg-shadow-all-layers" check-in: 4a0fed20b2 user: saul tags: sg-shadow-all-layers
2013-06-24
17:06
Create new branch named "sg-copyright-symbol" check-in: 245b0ed848 user: saul tags: sg-copyright-symbol
2013-06-22
23:52
Create new branch named "sg-tone-map" check-in: 4a1b7f0f16 user: saul tags: sg-tone-map
2013-06-20
04:30
Create new branch named "sg-arrow-stroke-path" check-in: bbd0c9ad2b user: saul tags: sg-arrow-stroke-path
2013-06-06
12:04
Create new branch named "sg-snakes-and-ladders" check-in: 9b7689dfcd user: saul tags: sg-snakes-and-ladders
2013-05-23
16:58
Create new branch named "sg-merge-divide" check-in: 22b7b71c02 user: saul tags: sg-merge-divide
2013-05-17
08:55
Create new branch named "sg-borders" check-in: d9a8e61268 user: saul tags: sg-borders
2013-04-14
03:27
Create new branch named "sg-decomp-to-layers" Leaf check-in: 2b68df9d38 user: saul tags: sg-decomp-to-layers
2013-04-05
01:36
Create new branch named "sg-viktoria" check-in: 7c55465e6b user: saul tags: sg-viktoria
2013-02-26
03:24
Create new branch named "sg-hypmotized" check-in: 1d9e876332 user: saul tags: sg-hypmotized
2013-02-15
04:34
Create new branch named "sg-mtg-slate" check-in: 79331851cc user: saul tags: sg-mtg-slate
2013-01-06
04:07
Create new branch named "sg-save-histogram-info" Leaf check-in: 1db7b4bee5 user: saul tags: sg-save-histogram-info
2012-11-27
05:01
Create new branch named "sg-export-image-tree" check-in: d4928d0303 user: saul tags: sg-export-image-tree
2012-10-28
22:39
Create new branch named "sg-activity-log" Leaf check-in: 73552bb511 user: saul tags: sg-activity-log
22:30
Create new branch named "sg-logging" Leaf check-in: 996eb4f812 user: saul tags: sg-logging
2012-10-21
15:23
Create new branch named "sg-2d-packing" check-in: 4a7b7372f3 user: saul tags: sg-2d-packing
2012-10-14
12:14
Create new branch named "sg-hootyhoo" check-in: 080c78cad9 user: saul tags: sg-hootyhoo
2012-09-19
17:29
Create new branch named "sg-mgwalk-save" check-in: fbca1a5253 user: saul tags: sg-mgwalk-save
2012-09-12
22:25
Create new branch named "sg-photocomics" check-in: 8bc833a7f3 user: saul tags: sg-photocomics
2012-09-06
02:58
Create new branch named "sg-decompose" check-in: 5d6d26b662 user: saul tags: sg-decompose
2012-09-04
09:25
Create new branch named "sg-copy-mask" check-in: 6c15fb4db8 user: saul tags: sg-copy-mask
2012-09-03
17:40
Create new branch named "sg-apply-masks" check-in: 0066ab04c7 user: saul tags: sg-apply-masks
2012-08-22
11:15
Create new branch named "sg-multifill" check-in: e653d29bec user: saul tags: sg-multifill
2012-08-12
05:25
Create new branch named "sg-save-all-layers" check-in: cb9f750763 user: saul tags: sg-save-all-layers
2012-06-13
05:28
Create new branch named "sg-goobie" check-in: ff12df0c63 user: saul tags: sg-goobie
05:24
Create new branch named "sg-create-badges" check-in: b2cbf306cc user: saul tags: sg-create-badges
2012-05-29
05:27
Create new branch named "sg-process-jpegs-in-folder" check-in: 876d582ad4 user: saul tags: sg-process-jpegs-in-folder
2012-05-21
23:01
Create new branch named "sg-dejw-paste" check-in: 8ea62d1137 user: saul tags: sg-dejw-paste
2012-04-15
16:04
Create new branch named "sg-numbered-frames.scm" check-in: 797f07925b user: saul tags: sg-numbered-frames.scm
2012-04-07
07:17
Create new branch named "sg-select-within-layer" check-in: 5ac83fffdf user: saul tags: sg-select-within-layer
2012-03-27
10:57
Create new branch named "sg-anim-settings" check-in: f244e32dc9 user: saul tags: sg-anim-settings
2012-02-28
02:31
Create new branch named "sg-copy-mask-from-above" check-in: c9985a274d user: saul tags: sg-copy-mask-from-above
2012-02-15
17:44
Create new branch named "sg-contour-fill" check-in: 008532b72c user: saul tags: sg-contour-fill
2012-02-06
20:45
Create new branch named "sg-zibbafy" check-in: f7ca57d139 user: saul tags: sg-zibbafy
2012-01-13
13:39
Create new branch named "sg-select-similar-colors" check-in: 37b920c537 user: saul tags: sg-select-similar-colors
2011-11-27
17:48
Create new branch named "sg-ornament-simple" check-in: a742ec8e81 user: saul tags: sg-ornament-simple
2011-11-06
13:32
Create new branch named "sg-channel-blend" check-in: c1ac254420 user: saul tags: sg-channel-blend
2011-10-16
04:28
Create new branch named "sg-expand-strip" check-in: 1f353fce5c user: saul tags: sg-expand-strip
2011-10-12
01:20
Create new branch named "sg-candlelit" check-in: 347f3d34ed user: saul tags: sg-candlelit
2011-09-27
09:23
Create new branch named "sg-tooninator" check-in: a6ce201c97 user: saul tags: sg-tooninator
2011-08-09
20:19
Create new branch named "sg-snapshot" check-in: 090f94d913 user: saul tags: sg-snapshot
2011-07-17
05:17
Create new branch named "sg-mystic-rose" check-in: 64f406315f user: saul tags: sg-mystic-rose
2011-07-12
05:01
Create new branch named "sg-panzoom" check-in: 285b349c70 user: saul tags: sg-panzoom
2011-07-11
18:12
Create new branch named "sg-incandescence". Leaf check-in: 11196fd42b user: saul tags: sg-incandescence
2011-05-10
20:28
Create new branch named "sg-quantize" check-in: 464add1ba1 user: saul tags: sg-quantize
2011-04-26
17:44
Create new branch named "sg-pattern-border" check-in: ead511acd2 user: saul tags: sg-pattern-border
2011-04-22
08:01
Create new branch named "sg-technicolor-3" check-in: a2013fa115 user: saul tags: sg-technicolor-3
2011-02-28
01:54
Create new branch named "sg-rotate-tab-labels" check-in: 1b53b0735f user: saul tags: sg-rotate-tab-labels
2011-02-25
22:34
Create new branch named "sg-mosaicize" check-in: 1a8a12053e user: saul tags: sg-mosaicize
2011-02-10
05:34
Create new branch named "sg-letter-drop" check-in: 51d381624d user: saul tags: sg-letter-drop
2011-02-01
03:07
Create new branch named "sg-projection-to-dodecahedron" check-in: 07340bd80e user: saul tags: sg-projection-to-dodecahedron
2011-01-27
20:43
Create new branch named "sg-extend-layer" check-in: 74915931aa user: saul tags: sg-extend-layer
2011-01-18
18:21
Create new branch named "sg-merge-linked" check-in: 938990b590 user: saul tags: sg-merge-linked
2011-01-09
16:49
Create new branch named "sg-dodecalendar" Closed-Leaf check-in: 69860bfec9 user: saul
2010-12-28
16:58
Create new branch named "sg-calendar-year" check-in: cc9615ab14 user: saul tags: sg-calendar-year
00:24
Create new branch named "sg-calendar" check-in: 74bb511193 user: saul tags: sg-calendar
2010-12-14
23:48
Create new branch named "sg-combine-bg" check-in: 4d56aac507 user: saul tags: sg-combine-bg
2010-12-04
14:57
Create new branch named "sg-fit-face" check-in: 366a3d12e8 user: saul tags: sg-fit-face
2010-11-14
22:16
Create new branch named "sg-colorcube" check-in: 08198c0bd5 user: saul tags: sg-colorcube
2010-11-13
15:17
Create new branch named "sg-damien" check-in: 47fd9604dd user: saul tags: sg-damien
2010-11-12
05:38
Create new branch named "sg-histogram-hue" check-in: 1be57e8b83 user: saul tags: sg-histogram-hue
2010-11-02
11:17
Create new branch named "sg-mean-fill" check-in: 0248a1f958 user: saul tags: sg-mean-fill
2010-10-25
10:39
Create new branch named "sg-layer-fit-to-selection" check-in: 08b6529ec0 user: saul tags: sg-layer-fit-to-selection
2010-10-02
14:26
Create new branch named "sg-gradient-survey" check-in: 587a076fe0 user: saul tags: sg-gradient-survey
2010-09-18
09:36
Create new branch named "sg-mirror-dup" check-in: b6ac506cba user: saul tags: sg-mirror-dup
2010-08-16
05:08
Create new branch named "sg-sudoku" check-in: 4a90bcc680 user: saul tags: sg-sudoku
2010-08-05
04:47
Create new branch named "sg-align-down" check-in: 4de83f17ac user: saul tags: sg-align-down
02:25
Create new branch named "sg-warptext" check-in: e2e125da6a user: saul tags: sg-warptext
2010-07-28
23:11
Create new branch named "sg-scale-to-selection" check-in: 3c68259dfc user: saul tags: sg-scale-to-selection
23:08
Create new branch named "sg-isolate" check-in: 9685296eac user: saul tags: sg-isolate
22:52
Create new branch named "sg-anti-crop" check-in: d8e79744c5 user: saul tags: sg-anti-crop
22:48
Added empty file. check-in: b2436f229b user: saul tags: trunk
13:29
initial empty check-in check-in: cf23bd898b user: saulgoode tags: trunk, root

Added doc/schemey-fu-devnotes.txt.



















































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
This document is intended for developers who might wish to make changes to Schemey-fu, or those who just wish to understand how it works. It is not necessary to understand the contents of this document to use Schemey-fu.

WHAT IS SCHEMEY-FU?

Schemey-fu is wrapper for the GIMP Procedural DataBase (PDB) that is intended to allow coding of Script-fu scripts in a more Scheme-like manner. GIMP's PDB is intended to interface with a wide range of programming languages (C, Scheme, Python, Perl, Ruby, to name a few) and thus the arguments passed to PDB procedures, and the values returned by the procedures, are specified using low level data objects such as simple numbers, strings, and uncounted arrays. All plug-in languages --except C itself -- provide a wrapper interface that will convert arguments from the native formats to the format expected by the PDB; likewise converting the return values of the procedure into the corresponding native format.

In some cases, the conversion between PDB format and the native language format may be quite simple. While the PDB uses an integer value of "1" for logical true, and "0" for logical false, Python uses special boolean objects True and False, respectively. 

As a more complicated example, GIMP's Python plug-in is largely object-oriented and so an 'image' is an object with certain values (e.g., width, height, list of layers) and methods (e.g., duplicate, rotate, get width) associated with it, while to the PDB an 'image' is just a unique integer identifier. Python-fu has an extensive wrapper that converts such objects while interacting with the PDB.

Script-fu's interface to the PDB is performed at a lower level. Boolean arguments passed to the PDB must be converted (by the script) to the numbers "1" and "0", while images, paths, and drawables are passed as simple integer identifiers. Lists and vectors need to be converted to counted vectors -- in other words, while the length of a Scheme list or vector is inherently known, that length has to be passed to the PDB as a separate argument.

In addition, Script-fu procedures only return a single value, though that value may be a list (of values). Multiple assignment such as Python's "x,y = pdb.gimp_layer_get_offset(layer)" is not possible in Script-fu. Whenever a PDB procedure returns more than one value, it is necessary that those returns be provided as a list. 

Furthermore, to simplify maintainability of Script-fu and provide consistency between GIMP-supplied and third-party procedures, even PDB procedures that return only a single value have that value placed inside a list. This is why it is typically necessary to use 'car' on the return value of PDB procedures such as 'gimp-image-get-active-layer'.

Schemey-fu attempts to address some of these issues and allow script coding to benefit from the conventions of the Scheme language. 

IS SCHEMEY-FU BETTER THAN SCRIPT-FU?

No, it is not. 

SCHEMEY-FU INTERNALS

The PDB procedures are divided up into different namespaces, with the namespace supplied as a prefix in the procedure name. The main namespaces are gimp, file, script-fu, and plug-in. Schemey-fu is mostly limited to the procedures within the gimp namespace. The gimp namespace generally follows the naming convention of gimp-object-action, where the object might be an image, a layer, a tool, or somesuch.

In order to maintain a consistent mapping with the PDB names, Schemey-fu procedures are generally divided into packages based upon the object being accessed. For example, the Schemey-fu call for gimp-layer-add-alpha becomes Layer::add-alpha, the Schemey-fu call for gimp-image-select-rectangle becomes Image::select-rectangle. gimp-selection-invert becomes Selection::invert, gimp-brush-set-radius becomes Brush::set-radius, et cetera. Schemey-fu package names begin with a capital letter.

The trick to implementing this mapping has to do with how Script-fu handles variable names containing two parts separate by double colons. Given an expression such as prefix::function, Script-fu converts that into (eval function prefix), where the 'prefix' specifies the environment in which the expression is to be evaluated. This allows us to define a function (or variable) within a localized environment and then easily access that function from a different environment. 

A local environment is created whenever a new lambda is introduced. To define our functions locally (so that they are not found when invoked from a different environment), we do the following:

(lambda ()
  (define (base-type image)
    (car (gimp-image-base-type image)))
  (define (clean-all image)
    (gimp-image-clean-all image))
  <body>)
  
We can then access the two functions 'base-type' and 'clean-all' from within <body>, but they are not accessible (by those names) from outside the lambda. In order to have access from outside the lambda, we return the local environment by having the last (in fact, only) thing <body> does is call the function 'current-environment'.

Of course, we have to actually execute the lambda to create the definitions and return the environment. We do that by applying the lambda (to no arguments). We then just need to assign that returned environment to a variable (that will become our 'prefix'). 
    
(define Image
  (apply (lambda ()
           (define (base-type image)
             (car (gimp-image-base-type image)))
           (define (clean-all image)
             (gimp-image-clean-all image))
           ; more definitions can go here
           (current-environment))))

After this is done, it is possible to call 'base-type' using eval and passing the environment:

(eval '(base-type x) Image)

Or using the double-colon reader transformation:

(Image::base-type x)

Similarly for 'clean-all'. 

All that is left is to define a macro that simplifies all that. 

(macro (make-environment form)
   `(apply (lambda ()
             ,@(cdr form)
             (current-environment))))

Which allows stating the same thing as:

(define Image
  (make-environment
    (define (base-type image)
      (car (gimp-image-base-type image)))
    (define (clean-all image)
      (gimp-image-clean-all image))
    ; more definitions can go here
    ))

Note that the 'make-environment' macro is included in Script-fu's script-fu.init file.

HELPER MACROS

Schemey-fu defines a few helper routines that make defining packages a bit easier and more readable. First there is 'pdb-dehyphen' which will return a new symbol after stripping the prefixes. For example, (pdb-deyphen 2 'gimp-image-base-type) will return 'base-type (the first parameter is the number of hyphens to skip). 

There are also some macros that create definitions for the most common types of PDB functions automatically. For example, (pdb-pass 2 gimp-image-clean-all) will create the appropriate definition as described above. Likewise, (pdb-decar 2 gimp-image-base-type) will create the above defintion for 'base-type such that the car of the PDB return value is extracted.

There is also a macro for converting the PDB return values from boolean tests to Scheme's #t and #f, ready for testing with Scheme conditionals. A question mark is appended to the new name following the Scheme convention for boolean procedures. For example, (pdb-bool 3 gimp-item-get-visible) will provide a suitable definition for (Item::visible? x).

Note that not all PDB procedures are handled by the above three macros. The above macros only handle the most common transformations of return values; sometimes it is necessary to transform the arguments being passed to the procedure. Also, the data types that get transformed (booleans and counted arrays) are sometimes returned along with other values in a list (see Vectors::stroke-get-points for one such example). It would be a waste of effort to have a macro for such exceptional cases. 

TRANSFORMATIONS

In addition to "decar"ing the returns of procedures that return a single value and converting TRUE/FALSE returns to Scheme booleans, Schemey-fu converts counted PDB arrays to lists. For example, the PDB procedure, gimp-image-get-layers, returns the number of layers plus a vector of those layers; this gets converts to a simple list of the layers. 

(set! layers (Image::get-layers x))

This transformation of counted arrays applies also to parameters passed to procedures expecting counted arrays. The Schemey-fu binding will automatically insert the number of elements in the list and convert the list to the appropriate array.

(set! stroke-id (Vectors::stroke-new-from-points path 
                                                 VECTORS-STROKE-TYPE-BEZIER  
                                                 '(34 5 40 10 42 15 54 15 41 20 68 24)
                                                 1))

Note that when booleans parameters are expected by PDB procedures, they should be passed as TRUE/FALSE (1/0) rather than as Scheme's #f and #t. It is only when they are returned as values from PDB procedures that booleans get transformed. The reasoning behind adopting this inconsistency is two-fold. 

First, the majority of third-party plug-ins/scripts do not return meaningful values and hardly any return booleans. However, there are third-party scripts that accept TRUE/FALSE as parameters and using the same convention for both the Gimp namespace and third-party parameters simplifies things somewhat.

Second, if the script writer makes a mistake and passes #t or #f to a procedure that is expecting a TRUE or FALSE, an error will be produced about the wrong type being supplied. However, if Schemey-fu had chosen the opposite approach (and functions expected #t/#f parameters) then should a scripter pass, by mistake, a TRUE/FALSE to the procedure, no error would be generated and in the case of FALSE being passed, that FALSE would be treated as a TRUE. It would be much harder to track down the cause of such an unflagged error after it produces the faulty behavior.

Finally, there are basically two different forms of boolean procedures in the PDB: where the procedure name includes "is/has" (e.g., gimp-image-is-valid, gimp-drawable-has-alpha), and where the procedure name includes "get" and there is a corresponding "set" procedure (e.g., gimp-item-get-visible). For the first form, the Schemey-fu function starts with the "is-" (or "has-") while for the second form the "-get-" is dropped altogether. For both forms, a question mark is appended to the function name: e.g., Image::is-valid?, Drawable::has-alpha?, and Item::visible?.


APPENDIX

Useful means of querying the PDB while excluding deprecated procedures:
   (gimp-procedural-db-query ".*"
                             "^((?!deprecated).)*$" ; exclude deprecated
                             ".*" 
                             ".*" 
                             ".*" 
                             ".*" 
                             "Internal GIMP procedure")

The following function was used to separate the procedures based upon the number of values they return:
                      
   (define (filter-by-nb-values pdb-names nb-values)
     (let loop ((procs pdb-names)
                (result '()))
       (if (null? procs)
         (reverse result)
         (loop (cdr procs)
           (if (= (cadddr (cddddr (gimp-procedural-db-proc-info (car procs)))) nb-values)
             (cons (car procs) result)
             result)))))
  

Added doc/schemey-fu-manual.txt.















































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
This document describes general usage of Schemey-fu.

WHAT IS SCHEMEY-FU?

Schemey-fu is a wrapper for the GIMP Procedural DataBase (PDB) that is intended to allow coding of Script-fu scripts in a more Scheme-like manner. Schemey-fu provides substitute functions for all of the internally defined PDB procedures, with the substitute functions returning individual values, pure lists, and true Scheme booleans, and accepting lists as arguments when counted arrays are expected.

INSTALLATION

Copy the schemey-fu.scm file into your ~/.gimp-2.8/scripts directory.

SINGLE RETURN VALUES

When a PDB procedure returns a single (typically numeric) value, that value is always placed inside a list, and requires applying 'car' to that list to extract the value. The corresponding Schemey-fu function returns the desired value directly.

  Examples:
    (Image::width image) ==> width
    (Layer::new-from-drawable drawable dest-image) ==> layer-copy
    
BOOLEANS

When a PDB procedure returns a FALSE/TRUE, not only is that return placed inside a list, it follows the convention that zero indicates boolean false and "1" indicates boolean true. Schemey-fu's boolean functions directly return either #f of #t respectively.

Furthermore, the name of the Schemey-fu function has a question mark (?) appended, following the Scheme convention for predicate functions.

  Examples:
    (Image::is-valid? x) ==> #t/#f
    (Item::is-channel? x) ==> #t/#f
    (Drawable::has-alpha? x) ==> #t/#f

For boolean procedures that include "get" in the name and have a corresponding "set" procedure, Schemey-fu naming convention omits the "get" (and appends the question mark).

  Examples:
    (Layer::visible? x) ==> #t/#f
    (Context::antialias?) ==> #t/#f

Note that when booleans are passed as arguments, they should follow the PDB convention of TRUE=1/FALSE=0. It is only when they are returned as values from PDB procedures that booleans get transformed. The reasoning behind adopting this assymetry is two-fold. 

First, the majority of third-party plug-ins/scripts do not return meaningful values and hardly any return booleans. However, there are third-party scripts that accept TRUE/FALSE as parameters and using the same convention for both the Schemey-fu parameters and third-party parameters provides better consistency.

Second, if the script writer makes a mistake and passes #t or #f to a procedure that is expecting a TRUE or FALSE, an error will be produced about the wrong type being supplied. However, if Schemey-fu had chosen the opposite approach (and functions expected #t/#f parameters) then should a scripter mistakenly provide a TRUE/FALSE to the procedure, no error would be generated and, in the case of FALSE being passed, that FALSE would be treated as TRUE. It would be much harder to track down bugs caused by such an unflagged error.

COUNTED ARRAYS

In addition to "decar"ing the returns of procedures that return a single value and converting TRUE/FALSE returns to Scheme booleans, Schemey-fu converts counted PDB arrays to lists. For example, the PDB procedure, gimp-image-get-layers, returns the number of layers plus a vector of those layers; the Schemey-fu function returns a simple list of the layers. 

    (Image::get-layers x) ==> list of layers

This transformation of counted arrays applies also to parameters passed to procedures expecting counted arrays. The Schemey-fu binding will automatically insert the number of elements in the list and convert the list to the appropriate array type expected (i.e., either a list or a vector).

    (Vectors::stroke-new-from-points path 
                                     VECTORS-STROKE-TYPE-BEZIER  
                                     '(34 5 40 10 42 15 54 15 41 20 68 24)
                                     1)
      ==> stroke-id
      
Note that this transformation of counted arrays only applies to GIMP internal procedures; plug-ins with counted array parameters (such as 'plug-in-film') need to be handled in the traditional manner. Fortunately, there are very few instances where this is a concern.

NAMESPACES

By now you've probably realized that Schemey-fu functions follow the naming convention of "Namespace::function", which consistly maps to the corresponding PDB procedure's name.

Within the PDB, procedures are divided up into different namespaces with the namespace supplied as a prefix in the procedure name. The main top-level namespaces are 'gimp', 'file', 'script-fu', and 'plug-in'. Schemey-fu is mostly limited to the procedures within the 'gimp' namespace. The 'gimp' namespace itself generally follows the naming convention of gimp-object-action, where the object might be an image, a layer, a tool, or somesuch.

In order to maintain a consistent mapping with the PDB names, Schemey-fu functions are generally divided into namespaces based upon the object being accessed. For example, the Schemey-fu call for gimp-layer-add-alpha becomes Layer::add-alpha, the Schemey-fu call for gimp-image-select-rectangle becomes Image::select-rectangle. gimp-selection-invert becomes Selection::invert, gimp-brush-set-radius becomes Brush::set-radius, et cetera. 

Note that Schemey-fu namespaces all begin with a capital letter, while the remainder of the name is lowercase. Though this breaks with traditional Scheme naming convention, it permits use of variable names that might otherwise conflict with a Schemey-fu namespace name (e.g., image, layer, drawable). 

The Schemey-fu namespaces comprise the following (in alphabetical order):
    namespace     prefix
    ---------     ------
    Brush         gimp-brush-
    Brushes       gimp-brushes-
    Buffer        gimp-buffer-
    Buffers       gimp-buffers-
    Channel       gimp-channel-
    Context       gimp-context-
    Drawable      gimp-drawable-
    Edit          gimp-edit-
    Floating-sel  gimp-floating-sel-
    Gradient      gimp-gradient-
    Image         gimp-image-
    Item          gimp-item-
    Layer         gimp-layer-
    Palette       gimp-palette-
    Palettes      gimp-palettes-
    Plugin        gimp-plug-in-
    Pdb           gimp-procedural-db-
    Progress      gimp-progress-
    Selection     gimp-selection-
    Text          gimp-text-
    Unit          gimp-unit-
    Vectors       gimp-vectors-

In addition, there are a few gimp-* PDB procedures that do not fit into the above categories. First, there are the procedures that implement some of the tools found in the Toolbox and in the Colors menu; these are consolidated into the "Tool" namespace.

    namespace    prefix  PDB procedure (+ indicates there is an additional -default version)
    ---------    ------  -------------------------------------------------------------------
    Tool         gimp-   airbrush+, clone+, convolve+, curves-explicit, curves-spline,
                         dodgeburn+, eraser+, heal+, histogram, hue-saturation, invert,
                         paintbrush+, pencil+, posterize, smudge+

There are also a handful of procedures that address GIMP "sessions". These are incorporated into the Gimp namespace.

    namespace    prefix  PDB procedure
    ---------    ------  -------------------------------------------------------------------
    Gimp         gimp-   attach-parasite, gimp-detach-parasite, get-parasite-list, get-parasite name, quit

FU UTILITY PROCEDURES

Finally, there is a special Fu namespace, where I've placed some utility functions. 

    Fu::list-head (lis k) 
        return the first 'k' elements in the list
    Fu::list-tail (lis k) 
        return all element after the first 'k' items (Script-fu already provides 
        'list-tail' with the same functionality but it is replicated in the Fu 
        namespace for consistency with Fu::list-head)
    Fu::fold (op accum lis
        fold (left) per SRFI-1 (http://srfi.schemers.org/srfi-1/srfi-1.html)
        Note that there exists a 'foldr' in Script-fu, however, 'foldr'
        is inconsistent with traditional 'fold' (as the arguments expected 
        by 'op' are in reversed order).
    Fu::fold-right (op accum lis) 
        Per SRFI-1
    Fu::partition (pred? lis)
        Per SRFI-1. Split 'lis' into two lists: one containing the elements
        that satisfy the predicate function and one the elements that do not.
    Fu::remove (lis items)
        Remove elements from list that equal those in items. items can be
        either one item or a list of several items. Note that this remove
        is NOT per SRFI-1
    Fu::number->string (z . args) 
        Per R5RS
    Fu::string-reverse (str)
        Reverse the order of the characters in a string.
    Fu::string-trim (str [pred])
        Per SRFI-13 without optional start/end.
        Skip over initial characters matching the 'pred'. 
        'pred' can be either a procedure or a char; if 
        not supplied, pred defaults to #\space.
    Fu::string-pad (str n [char])
        Per SRFI-13 without start/end (also does not truncate str).
        Prepends enough 'char's to string to fill field of size 'n'
        If not supplied, 'char' is #\space.
    Fu::string-delete (pred str)
        Per SRFI-13 without optional start/end.
        Remove all chars from a string that match 'pred'
        'pred' can be either a procedure or a char.

    Fu::progression (start end [num-elements [growth]])
        Return a list of N elements from start to end with either
        algebraic/linear growth or geometric/exponential growth.
        If 'num-elements' is not specified then a simple integer sequence is
        generated.
        If 'growth' is not specified or is #f then a linear progression is
        generated, otherwise a geometric progression. For geometric
        progressions, 'start' must be greater than 0.
        Examples:
          (Fu::progression 1 5) ==> (1 2 3 4 5)
          (Fu::progression 1 5 5) ==> (1 2 3 4 5)
          (Fu::progression 1 5 6) ==> (1 1.8 2.6 3.4 4.2 5)
          (Fu::progression 1 1000 4 TRUE) ==> (1 10 100 1000)
    Fu::sort (compare? lis)
        Sort a list based on compare? Optimal for short lists (< 50 items)
        Example:
          (Fu::sort < '(30 20 50 40 10)) ==> (10 20 30 40 50)
    Fu::msort (compare? lis)
        Merge sort a list based on compare? Optimal for longer lists (> 50 items)
        Example:
          (Fu::msort < '(30 20 50 40 10)) ==> (10 20 30 40 50)
          
    Fu::get-bounds (drawables)
        Accepts either the ID of a single drawable or a list of drawable IDs. 
        If a list then the bounds of all drawables in the list is returned.

    Fu::grow-while (pred? . args)
        Returns the largest integer that satisfies the 'pred?' function.
        The 'pred?' function should accept a single "size" argument
        and return #t if the size fits the criteria. (Se Fu::calc-fontsize
        for an example of usage.)

    Fu::calc-fontsize (text font width height)
        Calculate the largest size of the given font that will fit within 
        the given dimensions.

USEFUL MACROS
   
   Schemey-fu also provides some useful macros. These do not follow the 
   double-colon namespace syntax.
   
   (with-context <body>)
   
     will automatically push the context before executing the code in
     <body> and pop it afterward.
   
   (with-undo image <body>)
     
     will automatically start an undo group before executing <body> and
     end it afterward.

   (with-selection image <body>)
     
     will save the current selection to a channel before executing <body>
     and restore it afterward. If you want to have access to the saved
     selection within <body> then you should provide a list for the 
     first argument, with the second item in the list being a channel 
     name that can be used from within the body.
     
   (with-xywh (name-x name-y name-w name-h) drawable <body>)

     will create bindings (whose scope are local to <body>). It accepts 
     as its first argument a list of four symbols respectively correspond 
     to the variable names for the x-offset, y-offset, width, and height 
     of the 'drawable'. If 'bindings' is empty then the default variable
     names are x, y, w, and h.

Added doc/sg-split-text-into-layers.scm.





























































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
; License: GPLv3
;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;    GNU General Public License for more details.
;
;    To view a copy of the GNU General Public License
;    visit: http://www.gnu.org/licenses/gpl.html

(define (schemey-fu-decompose-text-layer image layer granularity)
  (define (split-text-into-lines layer)
    (let ((image (Item::get-image layer))
          (text (Text::get-text layer)))
      (if (> (length (strbreakup text "\n")) 1)
        (let* ((lines (with-xywh () layer 
                 (let loop ((text-lines (strbreakup text "\n"))
                            (y-offset y)
                            (line-layers '()))
                   (if (null? text-lines)
                     line-layers
                     (let ((line-layer (Layer::copy layer TRUE)))
                       (Image::insert-layer image line-layer 0 -1)
                       (Text::set-text line-layer (car text-lines))
                       (Layer::set-offsets line-layer x y-offset)
                       (loop (cdr text-lines)
                             (+ y-offset 
                                (Text::get-line-spacing layer) 
                                (Drawable::height line-layer))
                             (cons line-layer line-layers)))))))
               (max-width (apply max (map Drawable::width lines))))
          (if (= (Text::get-justification layer) TEXT-JUSTIFY-CENTER)
            (map (lambda (layer) 
                   (with-xywh () layer 
                     (Layer::set-offsets layer
                                         (+ x (ceiling (/ (- max-width w) 2)))
                                         y))) 
                 lines))
          (Image::remove-layer image layer)
          lines)
        (list layer))))
  
  (define (split-line-into-words layer)
    (let* ((image (Item::get-image layer))
           (text (Text::get-text layer))
           (space (+ (* 2 (Text::get-letter-spacing layer))
                     (car (Text::get-extents-fontname " " 
                                                      (car (Text::get-font-size layer))
                                                      PIXELS
                                                      (Text::get-font layer))))))
      (if (> (length (strbreakup text " ")) 1)
        (let* ((words (with-xywh () layer
                 (let loop ((text-words (strbreakup text " "))
                            (x-offset x)
                            (word-layers '()))
                   (if (null? text-words)
                     word-layers
                     (let ((word-layer (Layer::copy layer TRUE)))
                       (Image::insert-layer image word-layer 0 -1)
                       (Text::set-text word-layer (car text-words))
                       (Layer::set-offsets word-layer x-offset y)
                       (loop (cdr text-words)
                             (+ x-offset 
                                space
                                (Drawable::width word-layer))
                       (cons word-layer word-layers))))))))
          (Image::remove-layer image layer)
          words)
        (list layer))))
                                       
  (define (split-word-into-letters layer)
    (let* ((image (Item::get-image layer))
           (text (Text::get-text layer))
           (space (Text::get-letter-spacing layer)))
      (if (> (string-length text) 1)
        (let* ((letters (with-xywh () layer
                 (let loop ((text-letters (string->list text))
                            (x-offset x)
                            (letter-layers '()))
                   (cond
                     ((null? text-letters)
                       letter-layers)
                     ((null? (cdr text-letters))
                       (let ((letter-layer (Layer::copy layer TRUE))
                             (letter (make-string 1 (car text-letters))))
                         (Image::insert-layer image letter-layer 0 -1)
                         (Text::set-text letter-layer letter)
                         (Layer::set-offsets letter-layer x-offset y)
                         (cons letter-layer letter-layers)))
                     (else
                       (let ((letter-layer (Layer::copy layer TRUE))
                             (letter (make-string 1 (car text-letters))))
                         (Image::insert-layer image letter-layer 0 -1)
                         (Text::set-text letter-layer letter)
                         (Layer::set-offsets letter-layer x-offset y)
                         (let* ((next (make-string 1 (cadr text-letters)))
                                (sub-width (car (Text::get-extents-fontname 
                                                  next
                                                  (car (Text::get-font-size layer))
                                                  PIXELS
                                                  (Text::get-font layer))))
                                (tot-width (car (Text::get-extents-fontname 
                                                  (string-append letter next)
                                                  (car (Text::get-font-size layer))
                                                  PIXELS
                                                  (Text::get-font layer)))))
                           (loop (cdr text-letters)
                                 (+ x-offset space (- tot-width sub-width))
                                 (cons letter-layer letter-layers))))))))))
          (Image::remove-layer image layer)
          letters)
        (list layer))))
        
  (when (Item::is-text-layer? layer)
    (with-undo image
      (let ((lines (split-text-into-lines layer)))
        (if (zero? granularity)
          (map split-word-into-letters
               (Fu::fold append '() (map split-line-into-words lines)))
          (map split-line-into-words lines)))))
  (gimp-displays-flush))
            

(script-fu-register "schemey-fu-decompose-text-layer"
		    "Decompose Text Layer..."
		    "Split a text layer into lines, words, or letters"
		    "saulgoode"
		    "saulgoode"
		    "March 2015"
		    "RGB*, GRAY*"
		    SF-IMAGE		"Image"			0
		    SF-DRAWABLE		"Drawable"		0
		    SF-OPTION		"Granularity" '("Letters" "Words" "Lines")
        )
(script-fu-menu-register "schemey-fu-decompose-text-layer"
		    "<Image>/Filters/Misc"
        )
        

Added schemey-fu.scm.





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
(define schemey-fu '(2 8)) ; to match GIMP version

; pdb-dehyphen accepts a symbol and returns the string that remains
; after everything up to and including 'cnt' hyphens have been 
; removed. For example, (pdb-dehyphen 'gimp-selection-none 2) ==> "none"
; 
(define (pdb-dehyphen sym cnt)
  (let loop ((lis (string->list (symbol->string sym)))
             (cnt cnt))
    (if (zero? cnt)
      (list->string lis)
      (if (equal? (car lis) #\-)
        (loop (cdr lis) (- cnt 1))
        (loop (cdr lis) cnt)))))

;; The following macros automatically convert PDB procedures
;; to their Schemey-fu equivalents. They work on the bulk of the 
;; PDB procedures, for example by taking the car of a single
;; returned value, but some procedures require special processing.

;; Note that 'pdb-pass ' does not incur the overhead of duplicating
;; the closure code, and so it is used for renaming PDB procedures
;; that return no values (but which actually return one value that 
;; should be ignored). This results in return values such as the
;; list (#t), rather than #t.
;; 'cons' is used to avoid using 'list' (which might conflict with
;; certain procedures such as 'gimp-image-list' being redefined and
;; replacing Scheme's standard 'list' operation for evaluation within
;; the local namespace).
;
(macro (pdb-pass form) ; for functions that return 0 values or a list
  (cons 'define 
        (cons (string->symbol (pdb-dehyphen (caddr form) (cadr form)))
              (cons (eval (caddr form) (current-environment))
                    '()))))
;; PDB procedures that return one value need to have that value
;; extracted from the returned list.
;
(macro (pdb-decar form) ; for 1 return value
  (cons 'define 
        (cons (string->symbol (pdb-dehyphen (caddr form) (cadr form)))
              (cons (eval (append '(lambda x) 
                                  (cons (cons 'car
                                              (cddr (get-closure-code (eval (caddr form) 
                                                                            (interaction-environment)))))
                                        '()))
                          (current-environment))
                    '()))))
                    
;; PDB procedures that return a boolean value need to have that value
;; extracted from the returned list and converted to #t or #f.
;
(macro (pdb-bool form) ; for PDB funcs that return TRUE/FALSE
  (cons 'define 
    (cons (string->symbol (string-append (pdb-dehyphen (caddr form) (cadr form))
                                         "?"))
      (cons (eval (append '(lambda x) 
                          (cons (cons 'not
                            (cons (cons 'zero?
                              (cons (cons 'car
                                (cddr (get-closure-code (eval (caddr form) 
                                                                    (interaction-environment)))))
                                '()))
                              '()))
                            '()))
                  (current-environment))
            '()))))

(define Gimp
  (make-environment  
    (define (attach-parasite name . rest)
      (gimp-attach-parasite (if (pair? rest)
                              (cons name rest)
                              name )))
    (define (convert-set-dither-matrix width height matrix)
      (gimp-image-convert-set-dither-matrix width 
                                            height 
                                            (length matrix)
                                            (list->vector matrix)))
    (pdb-pass  1 gimp-detach-parasite)
    (define (get-parasite-list)
      (cadr (gimp-get-parasite-list)))
    (define (get-parasite name)
      (catch #f
        (car (gimp-get-parasite name))))
    (pdb-pass  1 gimp-quit)
    ))

(define Image
  (make-environment 
    (pdb-decar 2 gimp-image-add-hguide)
    (pdb-decar 2 gimp-image-add-vguide)
    (pdb-pass  2 gimp-image-attach-parasite)
    (pdb-decar 2 gimp-image-base-type)
    (pdb-pass  2 gimp-image-clean-all)
    (pdb-pass  2 gimp-image-convert-grayscale)
    (pdb-pass  2 gimp-image-convert-indexed)
    (pdb-pass  2 gimp-image-convert-rgb)
    (pdb-pass  2 gimp-image-convert-set-dither-matrix)
    (pdb-pass  2 gimp-image-crop)
    (pdb-pass  2 gimp-image-delete)
    (pdb-pass  2 gimp-image-delete-guide)
    (pdb-pass  2 gimp-image-detach-parasite)
    (pdb-decar 2 gimp-image-duplicate)
    (pdb-decar 2 gimp-image-find-next-guide)
    (pdb-decar 2 gimp-image-flatten)
    (pdb-pass  2 gimp-image-flip)
    (pdb-decar 2 gimp-image-floating-sel-attached-to)
    (pdb-decar 2 gimp-image-get-active-channel)
    (pdb-decar 2 gimp-image-get-active-drawable)
    (pdb-decar 2 gimp-image-get-active-layer)
    (pdb-decar 2 gimp-image-get-active-vectors)
    (pdb-decar 2 gimp-image-get-channel-by-name)
    (pdb-decar 2 gimp-image-get-channel-by-tattoo)
    (define (get-channels image)  
      (vector->list (cadr (gimp-image-get-channels image))))
    (define (get-colormap image)  
      (vector->list (cadr (gimp-image-get-colormap image))))
    (pdb-decar 2 gimp-image-get-component-active)
    (pdb-decar 2 gimp-image-get-component-visible)
    (pdb-decar 2 gimp-image-get-exported-uri)
    (pdb-decar 2 gimp-image-get-filename)
    (pdb-decar 2 gimp-image-get-floating-sel)
    (pdb-decar 2 gimp-image-get-guide-orientation)
    (pdb-decar 2 gimp-image-get-guide-position)
    (pdb-decar 2 gimp-image-get-imported-uri)
    (pdb-decar 2 gimp-image-get-item-position)
    (pdb-decar 2 gimp-image-get-layer-by-name)
    (pdb-decar 2 gimp-image-get-layer-by-tattoo)
    (pdb-decar 2 gimp-image-get-name)
    (pdb-decar 2 gimp-image-get-parasite)
    (define (get-parasite-list image)
      (cadr (gimp-image-get-parasite-list image)))
    (pdb-pass  2 gimp-image-get-resolution)
    (pdb-decar 2 gimp-image-get-selection)
    (pdb-decar 2 gimp-image-get-tattoo-state)
    (pdb-decar 2 gimp-image-get-unit)
    (pdb-decar 2 gimp-image-get-uri)
    (define (get-vectors image)
      (vector->list (cadr (gimp-image-get-vectors image))))
    (pdb-decar 2 gimp-image-get-vectors-by-name)
    (pdb-decar 2 gimp-image-get-vectors-by-tattoo)
    (pdb-decar 2 gimp-image-get-xcf-uri)
    (pdb-decar 2 gimp-image-grid-get-background-color)
    (pdb-decar 2 gimp-image-grid-get-foreground-color)
    (pdb-pass  2 gimp-image-grid-get-offset)
    (pdb-pass  2 gimp-image-grid-get-spacing)
    (pdb-decar 2 gimp-image-grid-get-style)
    (pdb-pass  2 gimp-image-grid-set-background-color)
    (pdb-pass  2 gimp-image-grid-set-foreground-color)
    (pdb-pass  2 gimp-image-grid-set-offset)
    (pdb-pass  2 gimp-image-grid-set-spacing)
    (pdb-pass  2 gimp-image-grid-set-style)
    (pdb-decar 2 gimp-image-height)
    (pdb-pass  2 gimp-image-insert-channel)
    (pdb-pass  2 gimp-image-insert-layer)    
    (pdb-pass  2 gimp-image-insert-vectors)
    (pdb-bool  2 gimp-image-is-dirty)
    (pdb-bool  2 gimp-image-is-valid)
    ;; gimp-image-list is defined later to avoid possible
    ;; conflicts from re-defining list within this package
    (pdb-pass  2 gimp-image-lower-item)
    (pdb-pass  2 gimp-image-lower-item-to-bottom)
    (pdb-decar 2 gimp-image-merge-down)
    (pdb-decar 2 gimp-image-merge-visible-layers)
    (pdb-decar 2 gimp-image-new)
    (pdb-decar 2 gimp-image-pick-color)
    (pdb-decar 2 gimp-image-pick-correlate-layer)
    (pdb-pass  2 gimp-image-raise-item)
    (pdb-pass  2 gimp-image-raise-item-to-top)
    (pdb-pass  2 gimp-image-remove-channel)
    (pdb-pass  2 gimp-image-remove-layer)
    (pdb-pass  2 gimp-image-remove-vectors)
    (pdb-pass  2 gimp-image-reorder-item)
    (pdb-pass  2 gimp-image-resize)
    (pdb-pass  2 gimp-image-resize-to-layers)
    (pdb-pass  2 gimp-image-rotate)
    (pdb-pass  2 gimp-image-scale)
    (pdb-pass  2 gimp-image-select-color)
    (pdb-pass  2 gimp-image-select-contiguous-color)
    (pdb-pass  2 gimp-image-select-ellipse)
    (pdb-pass  2 gimp-image-select-item)
    (define (select-polygon image operation segs)
      (gimp-image-select-polygon image operation (length segs) (list->vector segs)))
    (pdb-pass  2 gimp-image-select-rectangle)
    (pdb-pass  2 gimp-image-select-round-rectangle)
    (pdb-pass  2 gimp-image-set-active-channel)
    (pdb-pass  2 gimp-image-set-active-layer)
    (pdb-pass  2 gimp-image-set-active-vectors)
    (define (set-colormap image colormap)
      (gimp-image-set-colormap image (length colormap) (list->vector colormap)))
    (pdb-pass  2 gimp-image-set-component-active)
    (pdb-pass  2 gimp-image-set-component-visible)
    (pdb-pass  2 gimp-image-set-filename)
    (pdb-pass  2 gimp-image-set-resolution)
    (pdb-pass  2 gimp-image-set-tattoo-state)
    (pdb-pass  2 gimp-image-set-unit)
    (define (thumbnail image width height)
      (let ((info (gimp-image-thumbnail image width height)))
        (list (car info)
              (cadr info)
              (caddr info)
              (vector->list (car (cdddd info))))))
    (pdb-decar 2 gimp-image-undo-disable)
    (pdb-decar 2 gimp-image-undo-enable)
    (pdb-decar 2 gimp-image-undo-freeze)
    (pdb-pass  2 gimp-image-undo-group-end)
    (pdb-pass  2 gimp-image-undo-group-start)
    (pdb-bool  2 gimp-image-undo-is-enabled)
    (pdb-decar 2 gimp-image-undo-thaw)
    (pdb-pass  2 gimp-image-unset-active-channel)
    (pdb-decar 2 gimp-image-width)

    ; Image::get-layers takes an option 'mode' argument (that is not
    ; available with the 'gimp-image-get-layers' procedure).
    ; if 'mode' is either 0 or omitted altogether return just the 
    ;   top-level layers and fundaments
    ; If 'mode' is 1 then a nested list of all real layers (no
    ;   fundaments) is returned.
    ; If 'mode' is 2 then a nested list is returned, where the first
    ;   item in each sub-list is the fundament of the group.
    ;
    (define (get-layers image . mode)
      (let ((top-layers (vector->list (cadr (gimp-image-get-layers image)))))
        (if (or (null? mode) (zero? (car mode)))
          top-layers
          (let loop ((top-layers top-layers)
                     (layers '()))
            (if (null? top-layers)
              (reverse layers)
              (loop (cdr top-layers)
                    (cons (if (zero? (car (gimp-item-is-group (car top-layers))))
                            (car top-layers)
                            (if (= (car mode) 2)
                              (cons (car top-layers)
                                    (Item::get-children (car top-layers) (car mode)))
                              (Item::get-children (car top-layers) (car mode))))
                          layers)))))))
    (define (list)
      (vector->list (cadr (gimp-image-list))))
    ))
    
(define Layer
  (make-environment 
    (pdb-pass  2 gimp-layer-add-alpha)
    (pdb-pass  2 gimp-layer-add-mask)
    (pdb-decar 2 gimp-layer-copy)
    (pdb-decar 2 gimp-layer-create-mask)
    (pdb-pass  2 gimp-layer-flatten)
    (pdb-decar 2 gimp-layer-from-mask)
    (pdb-bool  3 gimp-layer-get-apply-mask)
    (pdb-bool  3 gimp-layer-get-edit-mask)
    (pdb-bool  3 gimp-layer-get-lock-alpha)
    (pdb-decar 2 gimp-layer-get-mask)
    (pdb-decar 2 gimp-layer-get-mode)
    (pdb-decar 2 gimp-layer-get-opacity)
    (pdb-bool  3 gimp-layer-get-show-mask)
    (pdb-decar 2 gimp-layer-group-new)
    (pdb-bool  2 gimp-layer-is-floating-sel)
    (pdb-decar 2 gimp-layer-new)
    (pdb-decar 2 gimp-layer-new-from-drawable)
    (pdb-decar 2 gimp-layer-new-from-visible)
    (pdb-pass  2 gimp-layer-remove-mask)
    (pdb-pass  2 gimp-layer-resize)
    (pdb-pass  2 gimp-layer-resize-to-image-size)
    (pdb-pass  2 gimp-layer-scale)
    (pdb-pass  2 gimp-layer-set-apply-mask)
    (pdb-pass  2 gimp-layer-set-edit-mask)
    (pdb-pass  2 gimp-layer-set-lock-alpha)
    (pdb-pass  2 gimp-layer-set-mode)
    (pdb-pass  2 gimp-layer-set-offsets)
    (pdb-pass  2 gimp-layer-set-opacity)
    (pdb-pass  2 gimp-layer-set-show-mask)
    (pdb-pass  2 gimp-layer-translate)
    ))

(define Drawable
  (make-environment  
    (pdb-decar 2 gimp-drawable-bpp)
    (pdb-pass  2 gimp-drawable-fill)
    (pdb-pass  2 gimp-drawable-foreground-extract)
    (pdb-pass  2 gimp-drawable-free-shadow)
    (pdb-decar 2 gimp-drawable-get-pixel)
    (pdb-bool  2 gimp-drawable-has-alpha)
    (pdb-decar 2 gimp-drawable-height)
    (pdb-bool  2 gimp-drawable-is-gray)
    (pdb-bool  2 gimp-drawable-is-indexed)
    (pdb-bool  2 gimp-drawable-is-rgb)
    (pdb-pass  2 gimp-drawable-mask-bounds)
    (pdb-pass  2 gimp-drawable-mask-intersect)
    (pdb-pass  2 gimp-drawable-merge-shadow)
    (pdb-pass  2 gimp-drawable-offset)
    (pdb-pass  2 gimp-drawable-offsets)
    (pdb-pass  2 gimp-drawable-set-pixel)
    (define (sub-thumbnail drawable x y w h dest-w dest-h)
      (let ((info (gimp-drawable-sub-thumbnail drawable x y w h dest-w dest-h)))
        (list (car info)
              (cadr info)
              (caddr info)
              (vector->list (car (cddddr info))))))
    (define (thumbnail drawable dest-w dest-h)
      (let ((info (gimp-drawable-sub-thumbnail drawable dest-w dest-h)))
        (list (car info)
              (cadr info)
              (caddr info)
              (vector->list (car (cddddr info))))))
    (pdb-decar 2 gimp-drawable-type)
    (pdb-decar 2 gimp-drawable-type-with-alpha)
    (pdb-pass  2 gimp-drawable-update)
    (pdb-decar 2 gimp-drawable-width)

    ))
    
(define Item
  (make-environment 
    (pdb-pass  2 gimp-item-attach-parasite)
    (pdb-pass  2 gimp-item-delete)
    (pdb-pass  2 gimp-item-detach-parasite)
    (pdb-decar 2 gimp-item-get-image)
    (pdb-bool  3 gimp-item-get-linked)
    (pdb-bool  3 gimp-item-get-lock-content)
    (pdb-decar 2 gimp-item-get-name)
    (pdb-decar 2 gimp-item-get-parasite)
    (define (get-parasite-list item)
      (cadr (gimp-item-parasite-list item)))
    (pdb-decar 2 gimp-item-get-parent)
    (pdb-decar 2 gimp-item-get-tattoo)
    (pdb-bool  3 gimp-item-get-visible)
    (pdb-bool  2 gimp-item-is-channel)
    (pdb-bool  2 gimp-item-is-drawable)
    (pdb-bool  2 gimp-item-is-group)
    (pdb-bool  2 gimp-item-is-layer)
    (pdb-bool  2 gimp-item-is-layer-mask)
    (pdb-bool  2 gimp-item-is-selection)
    (pdb-bool  2 gimp-item-is-text-layer)
    (pdb-bool  2 gimp-item-is-valid)
    (pdb-bool  2 gimp-item-is-vectors)
    (pdb-pass  2 gimp-item-set-linked)
    (pdb-pass  2 gimp-item-set-lock-content)
    (pdb-pass  2 gimp-item-set-name)
    (pdb-pass  2 gimp-item-set-tattoo)
    (pdb-pass  2 gimp-item-set-visible)
    (pdb-decar 2 gimp-item-transform-2d)
    (pdb-decar 2 gimp-item-transform-flip)
    (pdb-decar 2 gimp-item-transform-flip-simple)
    (pdb-decar 2 gimp-item-transform-matrix)
    (pdb-decar 2 gimp-item-transform-perspective)
    (pdb-decar 2 gimp-item-transform-rotate)
    (pdb-decar 2 gimp-item-transform-rotate-simple)
    (pdb-decar 2 gimp-item-transform-scale)
    (pdb-decar 2 gimp-item-transform-shear)

    ; Item::get-children takes an option 'mode' argument (that is not
    ; available with the 'gimp-item-get-children' procedure).
    ; if 'mode' is either 0 or omitted altogether return just the 
    ;   top-level layers and fundaments
    ; If 'mode' is 1 then a nested list of all real layers (no
    ;   fundaments) is returned.
    ; If 'mode' is 2 then a nested list is returned, where the first
    ;   item in each sub-list is the fundament of the group.
    ;
    ; similar to 'get-layers' but for a group fundament
    ;
    (define (get-children fundament . mode)
      (let ((top-layers (vector->list (cadr (gimp-item-get-children fundament)))))
        (if (or (null? mode) (zero? (car mode)))
          top-layers
          (let loop ((top-layers top-layers)
                     (layers '()))
            (if (null? top-layers)
              (reverse layers)
              (loop (cdr top-layers)
                    (cons (if (zero? (car (gimp-item-is-group (car top-layers))))
                            (car top-layers)
                            (if (= (car mode) 2)
                              (cons (car top-layers)
                                    (get-children (car top-layers) (car mode)))
                              (get-children (car top-layers) (car mode))))
                          layers)))))))
    ))

(define Floating-sel
  (make-environment 
    (pdb-pass  3 gimp-floating-sel-anchor)
    (pdb-pass  3 gimp-floating-sel-attach)
    (define (attached-to image)
      (car (gimp-image-floating-sel-attached-to image)))
    (pdb-pass  3 gimp-floating-sel-relax)
    (pdb-pass  3 gimp-floating-sel-remove)
    (pdb-pass  3 gimp-floating-sel-rigor)
    (pdb-pass  3 gimp-floating-sel-to-layer)
    ))

(define Edit
  (make-environment 
    (pdb-pass  2 gimp-edit-blend)
    (pdb-pass  2 gimp-edit-bucket-fill)
    (pdb-pass  2 gimp-edit-bucket-fill-full)
    (pdb-pass  2 gimp-edit-clear)
    (define (copy drawable)
      (not (zero? (car (gimp-edit-copy drawable)))))
    (define (copy-visible drawable)
      (not (zero? (car (gimp-edit-copy-visible drawable)))))
    (define (cut drawable)
      (not (zero? (car (gimp-edit-cut drawable)))))
    (pdb-pass  2 gimp-edit-fill)
    (pdb-decar 2 gimp-edit-named-copy)
    (pdb-decar 2 gimp-edit-named-copy-visible)
    (pdb-decar 2 gimp-edit-named-cut)
    (pdb-decar 2 gimp-edit-named-paste)
    (pdb-decar 2 gimp-edit-named-paste-as-new)
    (pdb-decar 2 gimp-edit-paste)
    (pdb-decar 2 gimp-edit-paste-as-new)
    (pdb-pass  2 gimp-edit-stroke)
    (pdb-pass  2 gimp-edit-stroke-vectors)
    ))        

(define Context
  (make-environment 
    (pdb-bool  3 gimp-context-get-antialias)
    (pdb-decar 2 gimp-context-get-background)
    (pdb-decar 2 gimp-context-get-brush)
    (pdb-decar 2 gimp-context-get-brush-angle)
    (pdb-decar 2 gimp-context-get-brush-aspect-ratio)
    (pdb-decar 2 gimp-context-get-brush-size)
    (pdb-decar 2 gimp-context-get-dynamics)
    (pdb-decar 2 gimp-context-get-feather)
    (pdb-pass  2 gimp-context-get-feather-radius) 
    (pdb-decar 2 gimp-context-get-font)
    (pdb-decar 2 gimp-context-get-foreground)
    (pdb-decar 2 gimp-context-get-gradient)
    (pdb-decar 2 gimp-context-get-ink-angle)
    (pdb-decar 2 gimp-context-get-ink-blob-angle)
    (pdb-decar 2 gimp-context-get-ink-blob-aspect-ratio)
    (pdb-decar 2 gimp-context-get-ink-blob-type)
    (pdb-decar 2 gimp-context-get-ink-size)
    (pdb-decar 2 gimp-context-get-ink-size-sensitivity)
    (pdb-decar 2 gimp-context-get-ink-speed-sensitivity)
    (pdb-decar 2 gimp-context-get-ink-tilt-sensitivity)
    (pdb-decar 2 gimp-context-get-interpolation)
    (pdb-decar 2 gimp-context-get-opacity)
    (pdb-decar 2 gimp-context-get-paint-method)
    (pdb-decar 2 gimp-context-get-paint-mode)
    (pdb-decar 2 gimp-context-get-palette)
    (pdb-decar 2 gimp-context-get-pattern)
    (pdb-bool  3 gimp-context-get-sample-merged)
    (pdb-decar 2 gimp-context-get-sample-criterion)
    (pdb-decar 2 gimp-context-get-sample-threshold)
    (pdb-decar 2 gimp-context-get-sample-threshold-int)
    (pdb-bool  3 gimp-context-get-sample-transparent)
    (pdb-decar 2 gimp-context-get-transform-direction)
    (pdb-decar 2 gimp-context-get-transform-recursion)
    (pdb-decar 2 gimp-context-get-transform-resize)
    (define (list-paint-methods)
      (cadr (gimp-context-list-paint-methods)))
    (pdb-pass  2 gimp-context-pop)
    (pdb-pass  2 gimp-context-push)
    (pdb-pass  2 gimp-context-set-antialias)
    (pdb-pass  2 gimp-context-set-background)
    (pdb-pass  2 gimp-context-set-brush)
    (pdb-pass  2 gimp-context-set-brush-angle)
    (pdb-pass  2 gimp-context-set-brush-aspect-ratio)
    (pdb-pass  2 gimp-context-set-brush-default-size)
    (pdb-pass  2 gimp-context-set-brush-size)
    (pdb-pass  2 gimp-context-set-default-colors)
    (pdb-pass  2 gimp-context-set-defaults)
    (pdb-pass  2 gimp-context-set-dynamics)
    (pdb-pass  2 gimp-context-set-feather)
    (pdb-pass  2 gimp-context-set-feather-radius)
    (pdb-pass  2 gimp-context-set-font)
    (pdb-pass  2 gimp-context-set-foreground)
    (pdb-pass  2 gimp-context-set-gradient)
    (pdb-pass  2 gimp-context-set-ink-angle)
    (pdb-pass  2 gimp-context-set-ink-blob-angle)
    (pdb-pass  2 gimp-context-set-ink-blob-aspect-ratio)
    (pdb-pass  2 gimp-context-set-ink-blob-type)
    (pdb-pass  2 gimp-context-set-ink-size)
    (pdb-pass  2 gimp-context-set-ink-size-sensitivity)
    (pdb-pass  2 gimp-context-set-ink-speed-sensitivity)
    (pdb-pass  2 gimp-context-set-ink-tilt-sensitivity)
    (pdb-pass  2 gimp-context-set-interpolation)
    (pdb-pass  2 gimp-context-set-opacity)
    (pdb-pass  2 gimp-context-set-paint-method)
    (pdb-pass  2 gimp-context-set-paint-mode)
    (pdb-pass  2 gimp-context-set-palette)
    (pdb-pass  2 gimp-context-set-pattern)
    (pdb-pass  2 gimp-context-set-sample-criterion)
    (pdb-pass  2 gimp-context-set-sample-merged)
    (pdb-pass  2 gimp-context-set-sample-threshold)
    (pdb-pass  2 gimp-context-set-sample-threshold-int)
    (pdb-pass  2 gimp-context-set-sample-transparent)
    (pdb-pass  2 gimp-context-set-transform-direction)
    (pdb-pass  2 gimp-context-set-transform-recursion)
    (pdb-pass  2 gimp-context-set-transform-resize)
    (pdb-pass  2 gimp-context-swap-colors)
    ))
    
(define Channel
  (make-environment 
    (pdb-pass  2 gimp-channel-combine-masks)
    (pdb-decar 2 gimp-channel-copy)
    (pdb-decar 2 gimp-channel-get-color)
    (pdb-decar 2 gimp-channel-get-opacity)
    (pdb-bool  3 gimp-channel-get-show-masked)
    (pdb-decar 2 gimp-channel-new)    
    (pdb-decar 2 gimp-channel-new-from-component)
    (pdb-pass  2 gimp-channel-set-color)
    (pdb-pass  2 gimp-channel-set-opacity)
    (pdb-pass  2 gimp-channel-set-show-masked)
    ))

(define Brush
  (make-environment  
    (pdb-pass  2 gimp-brush-delete)
    (pdb-decar 2 gimp-brush-duplicate)
    (pdb-decar 2 gimp-brush-get-angle)
    (pdb-decar 2 gimp-brush-get-aspect-ratio)
    (pdb-decar 2 gimp-brush-get-hardness)
    (pdb-pass  2 gimp-brush-get-info)
    (define (get-pixels name)
      (let ((value (gimp-brush-get-pixels name)))
        (list (car value) ; width
              (cadr value) ;height
              (caddr value) ; mask bpp
              (vector->list (car (cddddr value))) ; mask-bytes
              (cadr (cddddr value)) ; color bpp
              (vector->list (cadddr (cddddr value))))))
    (pdb-decar 2 gimp-brush-get-radius)
    (pdb-decar 2 gimp-brush-get-shape)
    (pdb-decar 2 gimp-brush-get-spacing)
    (pdb-decar 2 gimp-brush-get-spikes)
    (pdb-bool  2 gimp-brush-is-editable)
    (pdb-bool  2 gimp-brush-is-generated)
    (pdb-decar 2 gimp-brush-new)
    (pdb-decar 2 gimp-brush-rename)
    (pdb-decar 2 gimp-brush-set-angle)
    (pdb-decar 2 gimp-brush-set-aspect-ratio)
    (pdb-decar 2 gimp-brush-set-hardness)
    (pdb-decar 2 gimp-brush-set-radius)
    (pdb-decar 2 gimp-brush-set-shape)
    (pdb-pass  2 gimp-brush-set-spacing)    
    (pdb-decar 2 gimp-brush-set-spikes)
    ))

(define Brushes
  (make-environment  
    (pdb-pass  2 gimp-brushes-close-popup)
    (define (get-list filter)
      (cadr (gimp-brushes-get-list filter)))
    (pdb-pass  2 gimp-brushes-popup)
    (pdb-pass  2 gimp-brushes-refresh)
    (pdb-pass  2 gimp-brushes-set-popup)
    ))
    
(define Buffer
  (make-environment  
    (pdb-pass  2 gimp-buffer-delete)
    (pdb-decar 2 gimp-buffer-get-bytes)
    (pdb-decar 2 gimp-buffer-get-height)
    (pdb-decar 2 gimp-buffer-get-image-type)
    (pdb-decar 2 gimp-buffer-get-width)
    (pdb-decar 2 gimp-buffer-rename)
    ))

(define Buffers
  (make-environment  
    (define (get-list filter)
      (cadr (gimp-buffers-get-list filter)))
    ))
 
(define Palette
  (make-environment 
    (pdb-decar 2 gimp-palette-add-entry)
    (pdb-pass  2 gimp-palette-delete)
    (pdb-pass  2 gimp-palette-delete-entry)
    (pdb-decar 2 gimp-palette-duplicate)
    (pdb-decar 2 gimp-palette-entry-get-color)
    (pdb-decar 2 gimp-palette-entry-get-name)
    (pdb-pass  2 gimp-palette-entry-set-color)
    (pdb-pass  2 gimp-palette-entry-set-name)
    (define (get-colors name)
      (vector->list (cadr (gimp-palette-get-colors name))))
    (pdb-decar 2 gimp-palette-get-columns)
    (pdb-decar 2 gimp-palette-get-info)
    (pdb-bool  2 gimp-palette-is-editable)
    (pdb-decar 2 gimp-palette-new)
    (pdb-decar 2 gimp-palette-rename)
    (pdb-pass  2 gimp-palette-set-columns)
    )) 
 
(define Palettes
  (make-environment 
    (pdb-pass  2 gimp-palettes-close-popup)
    (define (get-list filter)
      (cadr (gimp-palettes-get-list filter)))
    (pdb-pass  2 gimp-palettes-popup)
    (pdb-pass  2 gimp-palettes-refresh)
    (pdb-pass  2 gimp-palettes-set-popup)
    ))
    
(define Patterns
  (make-environment 
    (pdb-pass  2 gimp-patterns-close-popup)
    (define (get-list filter)
      (cadr (gimp-patterns-get-list filter)))
    (pdb-pass  2 gimp-patterns-popup)
    (pdb-pass  2 gimp-patterns-refresh)
    (pdb-pass  2 gimp-patterns-set-popup)
    ))
 
(define Pdb
  (make-environment 
    (pdb-pass  3 gimp-procedural-db-dump)
    (define (get-data identifier)
      (vector->list (cadr (gimp-procedural-db-get-data identifier))))
    (pdb-decar 3 gimp-procedural-db-get-data-size)
    (pdb-pass  3 gimp-procedural-db-proc-arg)
    (pdb-bool  3 gimp-procedural-db-proc-exists)
    (pdb-pass  3 gimp-procedural-db-proc-info)
    (pdb-pass  3 gimp-procedural-db-proc-val)
    (define (query name blurb help author copyright date type)
      (cadr (gimp-procedural-db-query name blurb help author copyright date type)))
    (define (set-data name lis)
      (gimp-procedural-db-set-data name (length lis) (list->vector lis)))
    (pdb-decar 3 gimp-procedural-db-temp-name)
    ))

(define Plugin
  (make-environment 
    (pdb-pass  2 gimp-plugin-domain-register)
    (pdb-pass  2 gimp-plugin-get-pdb-error-handler)
    (pdb-pass  2 gimp-plugin-help-register)
    (pdb-pass  2 gimp-plugin-icon-register)
    (pdb-pass  2 gimp-plugin-menu-branch-register)
    (pdb-pass  2 gimp-plugin-menu-register)
    (pdb-decar 2 gimp-plugin-set-pdb-error-handler)
    ))
    
(define Progress
  (make-environment 
    (pdb-pass  2 gimp-progress-cancel)
    (pdb-pass  2 gimp-progress-end)
    (pdb-decar 2 gimp-progress-get-window-handle)
    (pdb-pass  2 gimp-progress-init)
    (pdb-pass  2 gimp-progress-install)
    (pdb-pass  2 gimp-progress-pulse)
    (pdb-pass  2 gimp-progress-set-text)
    (pdb-pass  2 gimp-progress-uninstall)
    (pdb-pass  2 gimp-progress-update)
    ))

(define Selection
  (make-environment 
    (pdb-pass  2 gimp-selection-all)
    (pdb-pass  2 gimp-selection-border)
    (pdb-pass  2 gimp-selection-bounds)
    (pdb-pass  2 gimp-selection-feather)
    (pdb-decar 2 gimp-selection-float)
    (pdb-pass  2 gimp-selection-grow)
    (pdb-pass  2 gimp-selection-invert)
    (pdb-bool  2 gimp-selection-is-empty)    
    (pdb-pass  2 gimp-selection-none)    
    (pdb-decar 2 gimp-selection-save)
    (pdb-pass  2 gimp-selection-sharpen)
    (pdb-pass  2 gimp-selection-shrink)
    (pdb-pass  2 gimp-selection-translate)
    (pdb-decar 2 gimp-selection-value)
    ))

(define Text
  (make-environment 
    (pdb-decar 2 gimp-text-fontname) ; note the prefix
    (define (antialias? layer)
      (not (zero? (car (gimp-text-layer-get-antialias layer)))))
    (pdb-decar 3 gimp-text-layer-get-base-direction)
    (pdb-decar 3 gimp-text-layer-get-color)
    (pdb-pass  2 gimp-text-get-extents-fontname)
    (pdb-decar 3 gimp-text-layer-get-font)
    (pdb-pass  3 gimp-text-layer-get-font-size)
    (pdb-decar 3 gimp-text-layer-get-hint-style)
    (pdb-decar 3 gimp-text-layer-get-indent)
    (pdb-decar 3 gimp-text-layer-get-justification)
    (define (kerning? layer)
      (not (zero? (car (gimp-text-layer-get-kerning layer)))))
    (pdb-decar 3 gimp-text-layer-get-language)
    (pdb-decar 3 gimp-text-layer-get-letter-spacing)
    (pdb-decar 3 gimp-text-layer-get-line-spacing)
    (pdb-decar 3 gimp-text-layer-get-markup)
    (pdb-decar 3 gimp-text-layer-get-text)
    (pdb-decar 3 gimp-text-layer-new)
    (pdb-pass  3 gimp-text-layer-resize)
    (pdb-pass  3 gimp-text-layer-set-antialias)
    (pdb-pass  3 gimp-text-layer-set-base-direction)
    (pdb-pass  3 gimp-text-layer-set-color)
    (pdb-pass  3 gimp-text-layer-set-font)
    (pdb-pass  3 gimp-text-layer-set-font-size)
    (pdb-pass  3 gimp-text-layer-set-hint-style)
    (pdb-pass  3 gimp-text-layer-set-hinting)
    (pdb-pass  3 gimp-text-layer-set-indent)
    (pdb-pass  3 gimp-text-layer-set-justification)
    (pdb-pass  3 gimp-text-layer-set-kerning)
    (pdb-pass  3 gimp-text-layer-set-language)
    (pdb-pass  3 gimp-text-layer-set-letter-spacing)
    (pdb-pass  3 gimp-text-layer-set-line-spacing)
    (pdb-pass  3 gimp-text-layer-set-text)
    ))

(define Unit
  (make-environment 
    (pdb-decar 2 gimp-unit-get-abbreviation)
    (pdb-decar 2 gimp-unit-get-deletion-flag)
    (pdb-decar 2 gimp-unit-get-digits)
    (pdb-decar 2 gimp-unit-get-factor)
    (pdb-decar 2 gimp-unit-get-identifier)
    (pdb-decar 2 gimp-unit-get-number-of-built-in-units)
    (pdb-decar 2 gimp-unit-get-number-of-units)
    (pdb-decar 2 gimp-unit-get-plural)
    (pdb-decar 2 gimp-unit-get-singular)
    (pdb-decar 2 gimp-unit-get-symbol)
    (pdb-decar 2 gimp-unit-new)
    (pdb-pass  2 gimp-unit-set-deletion-flag)
    ))
    
(define Vectors
  (make-environment 
    (pdb-pass  2 gimp-vectors-bezier-stroke-conicto)
    (pdb-pass  2 gimp-vectors-bezier-stroke-cubicto)
    (pdb-pass  2 gimp-vectors-bezier-stroke-lineto)
    (pdb-decar 2 gimp-vectors-bezier-stroke-new-ellipse)
    (pdb-decar 2 gimp-vectors-bezier-stroke-new-moveto)
    (pdb-decar 2 gimp-vectors-copy)
    (pdb-pass  2 gimp-vectors-export-to-file)
    (pdb-pass  2 gimp-vectors-export-to-string)
    (define (get-strokes vectors)
      (vector->list (cadr (gimp-vectors-get-strokes vectors))))
    (define (import-from-file image filename merge scale)
      (vector->list (cadr (gimp-vectors-import-from-file image
                                                         filename
                                                         merge
                                                         scale))))
    (define (import-from-string image string merge scale)
      (vector->list (cadr (gimp-vectors-import-from-string image
                                                           string
                                                           (string-length string)
                                                           merge
                                                           scale))))
    (pdb-decar 2 gimp-vectors-new)
    (pdb-pass  2 gimp-vectors-new-from-text-layer)
    (pdb-pass  2 gimp-vectors-remove-stroke)
    (pdb-pass  2 gimp-vectors-stroke-close)
    (pdb-pass  2 gimp-vectors-stroke-flip)
    (pdb-pass  2 gimp-vectors-stroke-flip-free)
    (define (stroke-new-from-points vectors type points closed)
      (car (gimp-vectors-stroke-new-from-points vectors
                                                type
                                                (length points)
                                                (list->vector points)
                                                closed)))
    (pdb-decar 2 gimp-vectors-stroke-get-length)
    (pdb-pass  2 gimp-vectors-stroke-get-point-at-dist)
    (define (stroke-get-points vectors stroke-id)
      (let ((info (gimp-vectors-stroke-get-points vectors stroke-id)))
        (list (car info)
              (vector->list (caddr info))
              (not (zero? (cadddr info))))))
    (define (stroke-interpolate vectors stroke-id precision)
      (let ((info (gimp-vectors-stroke-interpolate vectors stroke-id precision)))
        (list (vector->list (cadr info))
              (not (zero? (caddr info))))))
    (pdb-pass  2 gimp-vectors-stroke-rotate)
    (pdb-pass  2 gimp-vectors-stroke-scale)
    (pdb-pass  2 gimp-vectors-stroke-translate)
    ))
    
(define Tool
  (make-environment 
    (define (airbrush drawable pressure strokes)
      (gimp-airbrush drawable pressure (length strokes) (list->vector strokes)))
    (define (airbrush-default drawable strokes)
      (gimp-airbrush drawable (length strokes) (list->vector strokes)))
    (pdb-pass  1 gimp-brightness-contrast)
    (define (clone drawable src-drawable type src-x src-y strokes)
      (gimp-clone drawable src-drawable type src-x src-y (length strokes) (list->vector strokes)))
    (define (clone-default drawable strokes)
      (gimp-clone-default drawable (length strokes) (list->vector strokes)))
    (pdb-pass  1 gimp-color-balance)
    (pdb-pass  1 gimp-colorize)
    (define (convolve drawable pressure type strokes)
      (gimp-convolve drawable pressure type (length strokes) (list->vector strokes)))
    (define (convolve-default drawable strokes)
      (gimp-convolve-default drawable (length strokes) (list->vector strokes)))
    (define (curves-explicit drawable channel curve)
      (gimp-curves-explicit drawable channel (length curve) (list->vector curve)))
    (define (curves-spline drawable channel control-pts)
      (gimp-curves-spline drawable channel (length control-pts) (list->vector control-pts)))
    (define (dodgeburn drawable exposure type mode strokes)
      (gimp-dodgeburn drawable exposure type mode (length strokes) (list->vector strokes)))
    (define (dodgeburn-default drawable strokes)
      (gimp-dodgeburn-default drawable (length strokes) (list->vector strokes)))
    (define (eraser drawable strokes hardness method)
      (gimp-eraser drawable (length strokes) (list->vector strokes) hardness method))
    (define (eraser-default drawable strokes)
      (gimp-eraser-default drawable (length strokes) (list->vector strokes)))
    (pdb-pass  1 gimp-equalize) ; Note: https://bugzilla.gnome.org/show_bug.cgi?id=745313
    (define (heal drawable src-drawable src-x src-y strokes)
      (gimp-heal drawable src-drawable src-x src-y (length strokes) (list->vector strokes)))
    (define (heal-default drawable strokes)
      (gimp-heal-default drawable (length strokes) (list->vector strokes)))
    (pdb-pass 1 gimp-histogram)
    (pdb-pass 1 gimp-hue-saturation)
    (pdb-pass 1 gimp-invert)
    (define (paintbrush drawable fade strokes method gradient-length)
      (gimp-paintbrush drawable fade (length strokes) (list->vector strokes) method gradient-length))
    (define (paintbrush-default drawable strokes)
      (gimp-paintbrush-default drawable (length strokes) (list->vector strokes)))
    (define (pencil drawable strokes)
      (gimp-pencil drawable (length strokes) (list->vector strokes)))
    (define (posterize levels)
      (gimp-posterize levels))
    (define (smudge drawable pressure strokes)
      (gimp-smudge drawable pressure (length strokes) (list->vector strokes)))
    (define (smudge-default drawable strokes)
      (gimp-smudge-default drawable (length strokes) (list->vector strokes)))
    ))

(define Gradient
  (make-environment 
    (pdb-pass  2 gimp-gradient-delete)
    (pdb-decar 2 gimp-gradient-duplicate)
    (define (get-custom-samples name positions reverse)
      (vector->list (cadr (gimp-gradient-get-custom-samples name
                                                            (length positions)
                                                            (list->vector positions)
                                                            reverse))))
    (define (get-list filter)
      (cadr (gimp-gradients-get-list filter)))
    (pdb-decar 2 gimp-gradient-get-number-of-segments)
    (define (get-uniform-samples name num-samples reverse)
      (vector->list (cadr (gimp-gradient-get-uniform-samples name
                                                             num-samples
                                                             reverse))))
    (pdb-bool  2 gimp-gradient-is-editable)
    (pdb-decar 2 gimp-gradient-new)
    (pdb-decar 2 gimp-gradient-rename)
    (pdb-decar 2 gimp-gradient-segment-get-blending-function)
    (pdb-decar 2 gimp-gradient-segment-get-coloring-type)
    (pdb-pass  2 gimp-gradient-segment-get-left-color)
    (pdb-decar 2 gimp-gradient-segment-get-left-pos)
    (pdb-decar 2 gimp-gradient-segment-get-middle-pos)
    (pdb-pass  2 gimp-gradient-segment-get-right-color)
    (pdb-decar 2 gimp-gradient-segment-get-right-pos)
    (pdb-pass  2 gimp-gradient-segment-range-blend-colors)
    (pdb-pass  2 gimp-gradient-segment-range-blend-opacity)
    (pdb-pass  2 gimp-gradient-segment-range-delete)
    (pdb-pass  2 gimp-gradient-segment-range-flip)
    (pdb-decar 2 gimp-gradient-segment-range-move)
    (pdb-pass  2 gimp-gradient-segment-range-redistribute-handles)
    (pdb-pass  2 gimp-gradient-segment-range-replicate)
    (pdb-pass  2 gimp-gradient-segment-range-set-blending-function)
    (pdb-pass  2 gimp-gradient-segment-range-set-coloring-type)
    (pdb-pass  2 gimp-gradient-segment-range-split-midpoint)
    (pdb-pass  2 gimp-gradient-segment-range-split-uniform)
    (pdb-pass  2 gimp-gradient-segment-set-left-color)
    (pdb-decar 2 gimp-gradient-segment-set-left-pos)
    (pdb-decar 2 gimp-gradient-segment-set-middle-pos)
    (pdb-decar 2 gimp-gradient-segment-set-right-pos)
    (pdb-pass  2 gimp-gradient-segment-set-right-color)
    (pdb-pass  2 gimp-gradients-close-popup)
    ))
(define Fu
  (make-environment
    (define (list-head lis k)
      (let recur ((lis lis) (k k))
        (if (zero? k) 
          '()
          (cons (car lis)
                (recur (cdr lis) (- k 1))))))
    (define list-tail list-tail)

    ;; Note that there exists a 'foldr' in Script-fu by default (defined in
    ;; the "script-fu-init.scm" file); however, this 'foldr' function is
    ;; inconsistent with either right or left folding per the traditional 
    ;; definitions of these structural transformations. ('foldr' is similar
    ;; to 'fold' except that the arguments expected by 'op' are in 
    ;; reversed order).
    
    ; per SRFI-1 (http://srfi.schemers.org/srfi-1/srfi-1.html)
    (define (fold op accum lis) 
      (if (null? lis)
        accum
        (fold op (op (car lis) accum) (cdr lis)) ))
    ; per SRFI-1 
    (define (fold-right op accum lis) 
      (if (null? lis)
        accum
        (op (fold-right op accum (cdr lis)) (car lis)) ))
    ; per SRFI-1 
    (define (partition pred? lis)
      (let loop ((lis lis)
                 (result '(() ())))
        (if (null? lis)
          (map reverse result)
          (loop (cdr lis)
                (if (pred? (car lis))
                  (list (cons (car lis) (car result))
                        (cadr result))
                  (list (car result)
                        (cons (car lis) (cadr result))))))))
    ; per R5RS (Script-fu's number->string only supports decimal radix                    
    (define (number->string z . args)
      (let ((radix (if (null? args)
                      10 
                      (car args))))
        (cond
          ((= radix 10)
            (anyatom->string z number?))
          ((not (member radix '(2 8 16)))
            (error "Unsupported numeric radix"))
          ((inexact? z)
            (error "Conversion of inexact number to non-decimal radix"))
          (else    
            (let ((digits (vector "0" "1" "2" "3" "4" "5" "6" "7"
                                  "8" "9" "a" "b" "c" "d" "e" "f"))
                  (val (abs z))
                  (sign (if (< z 0) "-" "")))
              (if (< val radix)
                (string-append sign (vector-ref digits val))
                (let loop ((num val)
                           (order (inexact->exact (truncate (/ (log val) 
                                                               (log radix)))))
                           (result sign))
                  (let* ((power (expt radix order))
                         (digit (quotient num power)))
                    (if (zero? order)
                      (string-append result (vector-ref digits digit))
                      (loop (- num (* digit power))
                            (pred order)
                            (string-append result (vector-ref digits digit))))))))))))                       
    ; per SRFI-1
    (define (filter pred lis)    
      (let loop ((lis lis))    
        (if (null? lis) 
          lis
          (let ((head (car lis))
                (tail (cdr lis)))
            (if (pred head)
              (let ((new-tail (loop tail)))  ; Replicate 'loop' so
                (if (eq? tail new-tail) 
                  lis
                  (cons head new-tail)))
              (loop tail))))))                  ; this one can be a tail call.                        
    ; per SRFI-13                      
    (define (string-reverse str)
      (let loop ((i (string-length str))
                 (result ""))
        (if (zero? i)
          result
          (loop (pred i)
                (string-append result (make-string 1 (string-ref str (pred i))))))))
    ; per SRFI-13 without optional start/end     
    ; skip over all characters matching the pred
    (define (string-trim str . args)
      (let ((pred (if (pair? args) (car args) #\space))
            (end (string-length str)))
        (cond 
          ((char? pred)
            (let loop ((i 0))
              (if (or (>= i end) (not (char=? pred (string-ref str i))))
                (substring str i end)
                (loop (succ i)))))
          (else 
            (let loop ((i 0))
              (if (or (>= i end) (not (pred (string-ref str i))))
                (substring str i end)
                (loop (succ i))))))))
    ; per SRFI-13 without start/end (also does not truncate str)
    (define (string-pad str n . args)
      (let* ((char (if (pair? args) (car args) #\space))
             (len (string-length str)))
        (if (<= n len)
          str
          (string-append (make-string (- n len) char)
                         str))))
    ; per SRFI-13 without start/end
    (define (string-delete pred str)
      (let ((end (string-length str)))
        (when (char? pred)
          (set! pred 
            (let ((char pred))
              (lambda (x) (char=? x char)))))
        (let loop ((i 0)
                   (result ""))
          (if (>= i end)
            result
            (loop (succ i)
                  (let ((char (string-ref str i)))
                    (if (pred char) ; for 'string-filter' swap consequents
                      result        
                      (string-append result (make-string 1 char)))))))))    
    ; NOT per SRFI-1
    (define (remove lis items)
      (if (not (pair? items))
        (set! items (list items)))
      (let loop ((items items)
                 (lis lis))
        (if (null? items)
          lis
          (loop (cdr items)
                (cadr (Fu::partition (lambda (x) (equal? (car items) x))
                                    lis))))))
    ; Return a list of every nth item (n specified by 'step') 
    ; For example to get a list of the x-coordinates of the anchors
    ; of a path stroke:
    ;   (Fu::nths points 6 2)
    ; The y-coordinates of the anchors would be:
    ;   (Fu::nths points 6 3)
    (define (nths lis step . offset)
      (define (butn lis n)
        (if (or (zero? n) (null? lis))
          lis
          (butn (cdr lis) (- n 1))))
      (if (pair? offset)
        (nths (butn lis (car offset)) step)
        (if (null? lis)
          lis
          (cons (car lis) (nths (butn lis step) step)))))
                                      
    ; 'Fu::get-bounds' accepts either the ID of a single drawable or a list
    ; of IDs. If a list then the bounds of all drawables in the list
    ; is returned.
    (define (get-bounds drawables)
      (unless (list? drawables)
        (set! drawables (list drawables)))
      (let* ((x1s (map car (map gimp-drawable-offsets drawables)))
             (y1s (map cadr (map gimp-drawable-offsets drawables)))
             (x2s (map + (map car (map gimp-drawable-width drawables))
                         x1s))
             (y2s (map + (map car (map gimp-drawable-height drawables))
                         y1s)))
        (list (apply min x1s)
              (apply min y1s)
              (apply max x2s)
              (apply max y2s))))

    ;; 'Fu::grow-while' returns the largest integer that satisfies 
    ;; the 'pred'. The pred function should accept a single "size"
    ;; argument and return #t if the size fits the criteria. (See
    ;; Fu::calc-fontsize for an example of usage.)
    ;
    (define (grow-while pred . args)
      (define (avg a b)
        (inexact->exact (truncate (/ (+ a b) 2))))
      (let ((start (if (null? args) 1 (car args))))
        (let loop ((size start)
                   (lower start)
                   (upper (* 2 start))
                   (last 0)
                   (unbounded? #t) )
          (if (= last size)
            size
            (if (pred size)
              (if unbounded?
                (loop (* 2 size)
                      size
                      (* size 2)
                      size
                      #t)
                (loop (avg size upper)
                      size
                      upper
                      size
                      #f))
              (loop (avg lower size)
                    lower
                    size
                    size
                    #f))))))
          
    ;; 'calc-fontsize' finds the largest size of the given font that 
    ;; will fit within the given dimensions.
    ;
    (define (calc-fontsize text font width height)
      (define (fits? fontsize)
        (let ((extents (gimp-text-get-extents-fontname text fontsize PIXELS font)))
          (and (<= (car extents) width) (<= (cadr extents) height))))
      (grow-while fits? 6))

  ; align layer with the image, the selection, a base layer, a pair (x . y), 
  ; or list (x y w h).
  ; 'base' specification:
  ;   0     image
  ;   1     selection
  ;  >1     a layer
  ; pair    (x . y)
  ; list    (x y w h)
  ;
  ; reference locations
  ; 1 2 3     1=top-left 2=top-center 3=top-right
  ;  \ /      
  ; 4-5-6     4=center-left 5=center 6 center-right
  ;  / \
  ; 7 8 9     7=bottom-left 8=bottom-center 9=bottom-right
  ;
  ; There are two reference location specifications and both are 
  ; optional. If neither the source reference or the target (base)
  ; references are provided, the source layer will be center aligned
  ; with the target center. If just one reference is provided, it is 
  ; used for both the source and the target (e.g., if the ref argument
  ; is "2" then the top-center of the layer will be aligned with the
  ; top-center of the base.
  ; If both references are provided then the first ref specifies the
  ; location on the source and the second specifies the location on the
  ; target. This allows more advanced alignments such as having the
  ; top-left of the source layer align with the bottom-right of the 
  ; target.
  ; Note:: it is possible (and sometimes useful) to align a layer
  ;        with itself. For example, to move a corner to the center:
  ;          (Fu::align layer layer 1 5)
  ;
  (define (align layer base . refs)
    (let* ((image (Item::get-image layer))
           (lx (car (Drawable::offsets layer)))
           (ly (cadr (Drawable::offsets layer)))
           (aref (if (pair? refs)
                   (car refs)
                   5))
           (bref (if (and (pair? refs) (pair? (cdr refs)))
                   (cadr refs)
                   aref))
           (bx (cond
                 ((list? base)
                   (list (car base)
                         (truncate (+ (car base) (/ (caddr base) 2)))
                         (+ (car base) (caddr base))))
                 ((pair? base)
                   (make-list 3 (car base)))
                 ((zero? base)
                   (let ((width (Image::width image)))
                     (list 0 (truncate (/ width 2)) width)))
                 ((= base 1)
                   (let ((bounds (cdr (Selection::bounds image))))
                     (list (car bounds) 
                           (truncate (/ (+ (car bounds) (caddr bounds)) 2))
                           (caddr bounds))))
                 (else
                   (let ((x (car (Drawable::offsets base)))
                         (w (Drawable::width base)))
                     (list x (+ x (truncate (/ w 2))) (+ x w))))))
           (by (cond
                 ((list? base)
                   (list (cadr base)
                         (truncate (+ (cadr base) (/ (cadddr base) 2)))
                         (+ (cadr base) (cadddr base))))
                 ((pair? base)
                   (make-list 3 (cdr base)))
                 ((zero? base)
                   (let ((height (Image::height image)))
                     (list 0 (truncate (/ height 2)) height)))
                 ((= base 1)
                   (let ((bounds (cdr (Selection::bounds image))))
                     (list (cadr bounds) 
                           (truncate (/ (+ (cadr bounds) (cadddr bounds)) 2))
                           (cadddr bounds))))
                 (else
                   (let ((y (cadr (Drawable::offsets base)))
                         (h (Drawable::height base)))
                     (list y (+ y (truncate (/ h 2))) (+ y h))))))
           (ax (case aref
                 ((1 4 7) 
                   lx)
                 ((2 5 8)
                   (let ((w (Drawable::width layer)))
                     (+ lx (truncate (/ w 2)))))
                 ((3 6 9)
                   (+ lx (Drawable::width layer)))))
           (ay (case aref
                 ((1 2 3) 
                   ly)
                 ((4 5 6)
                   (let ((h (Drawable::height layer)))
                     (+ ly (truncate (/ h 2)))))
                 ((7 8 9)
                   (+ ly (Drawable::height layer))))))
      (set! bx (case bref
                 ((1 4 7)
                   (car bx))               
                 ((2 5 8)
                   (cadr bx))               
                 ((3 6 9)
                   (caddr bx))))
      (set! by (case bref
                 ((1 2 3) 
                   (car by))
                 ((4 5 6)
                   (cadr by))
                 ((7 8 9)
                   (caddr by))))
      (gimp-layer-set-offsets layer 
                              (- bx (- ax lx))
                              (- by (- ay ly)))))

    ;; 'progression' returns a list of N elements from start to end with either
    ;; algebraic/linear growth or geometric/exponential growth
    ;;
    ;; (progression start end num-elements growth)
    ;; the num-elements and growth arguments are optional
    ;; If 'num-elements' is not specified then a simple integer sequence is
    ;; generated.
    ;; If 'growth' is not specified or is #f then a linear progression is
    ;; generated, otherwise a geometric progression. For geometric
    ;; progressions, 'start must be > 0.
    ;; Examples:
    ;;   (progression 1 5) ==> (1 2 3 4 5)
    ;;   (progression 1 5 5) ==> (1 2 3 4 5)
    ;;   (progression 1 5 6) ==> (1 1.8 2.6 3.4 4.2 5)
    ;;   (progression 1 1000 4 TRUE) ==> (1 10 100 1000)
    ;
    (define (progression start end . args) 
      (when (null? args) 
        (set! start (truncate start))
        (set! end (truncate end)) )
      (let ((num-elements (if (null? args) (+ (- end start) 1) (car args)))
            (growth (and (pair? args) 
                         (and (pair? (cdr args))
                              (cadr args) ))))
      (let ((adjustment (if growth ; #f=linear, else exponential
                          (pow (/ end start) (/ (pred num-elements))) ; nth root
                          (/ (- end start) (pred num-elements)) )) ; step size
            (op (if growth * +)) )
        (let loop ((count (- num-elements 2))
                   (result (list start)))
          (if (<= count 0)
            (let ((result (reverse (cons end result))))
              (if (null? args)
                (map inexact->exact result)
                result))
            (loop (pred count)
                  (cons (op adjustment (car result)) result) ))))))

    ; This sort is an optimized merge sort.
    ;
    (define (sort < lis)
      (define (dosort < lis n)
        (if (= n 1)
          (list (car lis))
          (let ((i (quotient n 2)))
            (domerge <
                     (dosort < lis i)
                     (dosort < (list-tail lis i) (- n i))))))
      (define (domerge < lis1 lis2)
        (cond
          ((null? lis1) 
            lis2)
          ((null? lis2) 
            lis1)
          ((< (car lis2) (car lis1))
            (cons (car lis2) (domerge < lis1 (cdr lis2) )))
          (else
            (cons (car lis1) (domerge < (cdr lis1) lis2)))))
      
      (if (null? lis) 
        lis 
        (dosort < lis (length lis))))
    ))

;; Schemey-fu also provides some useful macros
(define-macro (with-context . body)
  `(begin
     (gimp-context-push)
     ,@body
     (gimp-context-pop) ))

(define-macro (with-undo image . body)
  `(begin
      (gimp-image-undo-group-start ,image)
      ,@body
      (gimp-image-undo-group-end ,image) ))

; If provided a list for the first argument, 'with-selection' will
; use second item in the list as a channel name, which can be used
; from within the body to reload the selection.
; If provided an image-id for the first argument, then the saved
; selection will be unnamed.
(define-macro (with-selection image . body)
  (let ((channel (if (pair? image)
                   (cadr image)
                   (gensym))))
    (if (pair? image)
      (set! image (car image)))
    `(let ((,channel (car (gimp-selection-save ,image))))
      ,@body
      (gimp-selection-load ,channel)
      (gimp-image-remove-channel ,image ,channel) )))

;; 'with-xywh' creates bindings whose scope is local to the
;; 'body'. It accepts as its first argument a list of four symbols
;; that respectively correspond to the variable names for the
;; x-offset, y-offset, width, and height of the 'drawable'.
;; If 'bindings' is empty then the default variable names are
;; x, y, w, and h.
;
(define-macro (with-xywh bindings drawable . body)
  (if (null? bindings)
    (set! bindings '(x y w h)))
  `(let ((,(car bindings) (car (Drawable::offsets ,drawable)))
         (,(cadr bindings) (cadr (Drawable::offsets ,drawable)))
         (,(caddr bindings) (Drawable::width ,drawable))
         (,(cadddr bindings) (Drawable::height ,drawable)))
     ,@body))