summaryrefslogtreecommitdiffstats
path: root/plug-ins/script-fu/scripts/addborder.scm
blob: ed47febfa0833466082c3a51b54a80c6daf55391 (plain)
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
; GIMP - The GNU Image Manipulation Program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
;
; 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.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <https://www.gnu.org/licenses/>.
;
; Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
;
; Version 0.2 10.6.97 Changed to new script-fu interface in 0.99.10

; Delta the color by the given amount. Check for boundary conditions
; If < 0 set to zero
; If > 255 set to 255
; Return the new value

(define (script-fu-addborder aimg adraw xsize ysize color dvalue)

  (define (deltacolor col delta)
    (let* ((newcol (+ col delta)))
      (if (< newcol 0) (set! newcol 0))
      (if (> newcol 255) (set! newcol 255))
      newcol
    )
  )

  (define (adjcolor col delta)
    (mapcar (lambda (x) (deltacolor x delta)) col)
  )

  (define (gen_top_array xsize ysize owidth oheight width height)
    (let* ((n_array (cons-array 10 'double)))
      (aset n_array 0 0 )
      (aset n_array 1 0 )
      (aset n_array 2 xsize)
      (aset n_array 3 ysize)
      (aset n_array 4 (+ xsize owidth))
      (aset n_array 5 ysize)
      (aset n_array 6 width)
      (aset n_array 7 0 )
      (aset n_array 8 0 )
      (aset n_array 9 0 )
      n_array)
  )

  (define (gen_left_array xsize ysize owidth oheight width height)
    (let* ((n_array (cons-array 10 'double)))
      (aset n_array 0 0 )
      (aset n_array 1 0 )
      (aset n_array 2 xsize)
      (aset n_array 3 ysize)
      (aset n_array 4 xsize)
      (aset n_array 5 (+ ysize oheight))
      (aset n_array 6 0 )
      (aset n_array 7 height )
      (aset n_array 8 0 )
      (aset n_array 9 0 )
      n_array)
  )

  (define (gen_right_array xsize ysize owidth oheight width height)
    (let* ((n_array (cons-array 10 'double)))
      (aset n_array 0 width )
      (aset n_array 1 0 )
      (aset n_array 2 (+ xsize owidth))
      (aset n_array 3 ysize)
      (aset n_array 4 (+ xsize owidth))
      (aset n_array 5 (+ ysize oheight))
      (aset n_array 6 width)
      (aset n_array 7 height)
      (aset n_array 8 width )
      (aset n_array 9 0 )
      n_array)
  )

  (define (gen_bottom_array xsize ysize owidth oheight width height)
    (let* ((n_array (cons-array 10 'double)))
      (aset n_array 0 0 )
      (aset n_array 1 height)
      (aset n_array 2 xsize)
      (aset n_array 3 (+ ysize oheight))
      (aset n_array 4 (+ xsize owidth))
      (aset n_array 5 (+ ysize oheight))
      (aset n_array 6 width)
      (aset n_array 7 height)
      (aset n_array 8 0 )
      (aset n_array 9 height)
      n_array)
  )

  (let* ((img (car (gimp-item-get-image adraw)))
         (owidth (car (gimp-image-width img)))
         (oheight (car (gimp-image-height img)))
         (width (+ owidth (* 2 xsize)))
         (height (+ oheight (* 2 ysize)))
         (layer (car (gimp-layer-new img
                                     width height
                                     (car (gimp-drawable-type-with-alpha adraw))
                                     _"Border Layer" 100 LAYER-MODE-NORMAL))))

    (gimp-context-push)
    (gimp-context-set-paint-mode LAYER-MODE-NORMAL)
    (gimp-context-set-opacity 100.0)
    (gimp-context-set-antialias FALSE)
    (gimp-context-set-feather FALSE)

    (gimp-image-undo-group-start img)

    (gimp-image-resize img
                       width
                       height
                       xsize
                       ysize)

    (gimp-image-insert-layer img layer 0 0)
    (gimp-drawable-fill layer FILL-TRANSPARENT)

    (gimp-context-set-background (adjcolor color dvalue))
    (gimp-image-select-polygon img
                               CHANNEL-OP-REPLACE
                               10
                               (gen_top_array xsize ysize owidth oheight width height))
    (gimp-drawable-edit-fill layer FILL-BACKGROUND)
    (gimp-context-set-background (adjcolor color (/ dvalue 2)))
    (gimp-image-select-polygon img
                               CHANNEL-OP-REPLACE
                               10
                               (gen_left_array xsize ysize owidth oheight width height))
    (gimp-drawable-edit-fill layer FILL-BACKGROUND)
    (gimp-context-set-background (adjcolor color (- 0 (/ dvalue 2))))
    (gimp-image-select-polygon img
                               CHANNEL-OP-REPLACE
                               10
                               (gen_right_array xsize ysize owidth oheight width height))

    (gimp-drawable-edit-fill layer FILL-BACKGROUND)
    (gimp-context-set-background (adjcolor color (- 0 dvalue)))
    (gimp-image-select-polygon img
                               CHANNEL-OP-REPLACE
                               10
                               (gen_bottom_array xsize ysize owidth oheight width height))

    (gimp-drawable-edit-fill layer FILL-BACKGROUND)
    (gimp-selection-none img)
    (gimp-image-undo-group-end img)
    (gimp-displays-flush)

    (gimp-context-pop)
    )
)

(script-fu-register "script-fu-addborder"
  _"Add _Border..."
  _"Add a border around an image"
  "Andy Thomas <alt@picnic.demon.co.uk>"
  "Andy Thomas"
  "6/10/97"
  "*"
  SF-IMAGE       "Input image" 0
  SF-DRAWABLE    "Input drawable" 0
  SF-ADJUSTMENT _"Border X size" '(12 1 250 1 10 0 1)
  SF-ADJUSTMENT _"Border Y size" '(12 1 250 1 10 0 1)
  SF-COLOR      _"Border color" '(38 31 207)
  SF-ADJUSTMENT _"Delta value on color" '(25 1 255 1 10 0 1)
)

(script-fu-menu-register "script-fu-addborder"
                         "<Image>/Filters/Decor")