spyrogimp.scm 12.2 KB
Newer Older
1
;; spyrogimp.scm -*-scheme-*-
2
;; Draws Spirographs, Epitrochoids and Lissajous Curves.
Jakub Friedl's avatar
Jakub Friedl committed
3
;; More info at http://www.wisdom.weizmann.ac.il/~elad/spyrogimp/
4 5 6
;; Version 1.2
;;
;; Copyright (C) 2003 by Elad Shahar <elad@wisdom.weizmann.ac.il>
7
;;
8 9 10 11
;; 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 2
;; of the License, or (at your option) any later version.
12
;;
13 14 15 16
;; 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.
17
;;
18 19 20 21 22 23 24
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


25 26
; This routine is invoked by a dialog.
; It is the main routine in this file.
27
(define (script-fu-spyrogimp img drw
28 29 30 31 32
                             type shape
                             oteeth iteeth
                             margin hole-ratio start-angle
                             tool brush
                             color-method color grad)
33

34
  ; Internal function to draw the spyro.
35
  (define (script-fu-spyrogimp-internal img drw
36 37 38 39 40 41 42 43 44 45 46 47 48
               x1 y1 x2 y2   ; Bounding box.
               type          ; = 0 (Spirograph), 1 (Epitrochoid), 2(Lissajous) .
               shape         ; = 0 (Circle), 1 (Frame), >2 (Polygons) .
               oteeth iteeth ; Outer and inner teeth.
               margin hole-ratio
               start-angle   ; 0 <= start-angle < 360 .
               tool          ; = 0 (Pencil), 1 (Brush), 2 (Airbrush) .
               brush
               color-method  ; = 0 (Single color), 1 (Grad. Loop Sawtooth),
                             ;   2 (Grad. Loop triangle) .
               color         ; Used when color-method = Single color .
               grad          ; Gradient used in Gradient color methods.
          )
49 50


51 52 53 54 55 56
    ; This function returns a list of samples according to the gradient.
    (define (get-gradient steps color-method grad)
      (if (= color-method 1)
          ; option 1
          ; Just return the gradient
          (gimp-gradient-get-uniform-samples grad (min steps 50) FALSE)
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          ; option 2
          ; The returned list is such that the gradient appears two times, once
          ; in the normal order and once in reverse. This way there are no color
          ; jumps if we go beyond the edge
          (let* (
                ; Sample the gradient into array "gr".
                (gr (gimp-gradient-get-uniform-samples grad
                                                       (/ (min steps 50) 2)
                                                       FALSE))

                (grn (car gr))  ; length of sample array.
                (gra (cadr gr)) ; array of color samples (R1,G1,B1,A1, R2,....)

                ; Allocate array gra-new of size  (2 * grn) - 8,
                ; but since each 4 items is actually one (RGBA) tuple,
                ; it contains 2x - 2 entries.
                (grn-new (+ grn grn -8))
                (gra-new (cons-array grn-new 'double))

                (gr-index 0)
                (gr-index2 0)
                )
80

81 82 83 84 85
            ; Copy original array gra to gra_new.
            (while (< gr-index grn)
               (aset gra-new gr-index (aref gra gr-index))
               (set! gr-index (+ 1 gr-index))
            )
86

87 88 89 90 91 92
            ; Copy second time, but in reverse
            (set! gr-index2 (- gr-index 8))
            (while (< gr-index grn-new)
               (aset gra-new gr-index (aref gra gr-index2))
               (set! gr-index (+ 1 gr-index))
               (set! gr-index2 (+ 1 gr-index2))
93

94 95 96 97
               (if (= (fmod gr-index 4) 0)
                 (set! gr-index2 (- gr-index2 8))
               )
            )
98

99 100
            ; Return list.
            (list grn-new gra-new)
101
          )
102 103
      )
    )
104 105


106 107 108
    (let* (
          (steps (+ 1 (lcm oteeth iteeth)))
          (*points* (cons-array (* steps 2) 'double))
109

110 111 112
          (ot 0)                         ; current outer tooth
          (cx 0)                         ; Current x,y
          (cy 0)
113

114 115 116
          ; If its a polygon or frame, how many sides does it have.
          (poly (if (= shape 1) 4   ; A frame has four sides.
                                (if (> shape 1) (+ shape 1) 0)))
117

118
          (2pi (* 2 *pi*))
119

120 121 122 123 124 125
          (drw-width (- x2 x1))
          (drw-height (- y2 y1))
          (half-width (/ drw-width 2))
          (half-height (/ drw-height 2))
          (midx (+ x1 half-width))
          (midy (+ y1 half-height))
126

127 128 129 130 131
          (hole (* hole-ratio
                   (- (/ (min drw-width drw-height) 2) margin)
                )
          )
          (irad (+ hole margin))
132

133 134
          (radx (- half-width irad))  ;
          (rady (- half-height irad)) ;
135

136 137 138
          (gradt (get-gradient steps color-method grad))
          (grada (cadr gradt)) ; Gradient array.
          (gradn (car gradt))  ; Number of entries of gradients.
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
          ; Indexes
          (grad-index 0)  ; for array: grada
          (point-index 0) ; for array: *points*
          (index 0)
          )

      ; Do one step of the loop.
      (define (calc-and-step!)
        (let* (
              (oangle (* 2pi (/ ot oteeth)) )
              (shifted-oangle (+ oangle (* 2pi (/ start-angle 360))) )
              (xfactor (cos shifted-oangle))
              (yfactor (sin shifted-oangle))
              (lenfactor 1)
              (ofactor (/ (+ oteeth iteeth) iteeth))

              ; The direction of the factor changes according
              ; to whether the type is a sypro or an epitcorhoid.
              (mfactor (if (= type 0) (- ofactor) ofactor))
              )

          ; If we are drawing a polygon then compute a contortion
          ; factor "lenfactor" which deforms the standard circle.
          (if (> poly 2)
            (let* (
165 166 167
                  (pi4 (/ *pi* poly))
                  (pi2 (* pi4 2))

168
                  (oanglemodpi2 (fmod (+ oangle
169 170
                                        (if (= 1 (fmod poly 2))
                                           0 ;(/ pi4 2)
171
                                           0
172 173
                                        )
                                      )
174 175 176 177 178 179 180 181 182
                                      pi2))
                  )

                  (set! lenfactor (/ ( if (= shape 1) 1 (cos pi4) )
                                     (cos
                                       (if (< oanglemodpi2 pi4)
                                         oanglemodpi2
                                         (- pi2 oanglemodpi2)
                                       )
183
                                     )
184 185 186
                                  )
                  )
            )
187 188
          )

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
          (if (= type 2)
            (begin  ; Lissajous
              (set! cx (+ midx
                          (* half-width (cos shifted-oangle)) ))
              (set! cy (+ midy
                          (* half-height (cos (* mfactor oangle))) ))
            )
            (begin  ; Spyrograph or Epitrochoid
             (set! cx (+ midx
                         (* radx xfactor lenfactor)
                         (* hole (cos (* mfactor oangle) ) ) ))
             (set! cy (+ midy
                         (* rady yfactor lenfactor)
                         (* hole (sin (* mfactor oangle) ) ) ))
            )
204 205
          )

206 207
        ;; Advance teeth
        (set! ot (+ ot 1))
208
        )
Kevin Cozens's avatar
Kevin Cozens committed
209
      )
210 211


212 213 214 215 216 217 218 219 220
      ;; Draw all the points in *points* with appropriate tool.
      (define (flush-points len)
        (if (= tool 0)
          (gimp-pencil drw len *points*)              ; Use pencil
          (if (= tool 1)
            (gimp-paintbrush-default drw len *points*); use paintbrush
            (gimp-airbrush-default drw len *points*)  ; use airbrush
          )
        )
221

222 223 224 225 226 227
        ; Reset points array, but copy last point to first
        ; position so it will connect the next time.
        (aset *points* 0 (aref *points* (- point-index 2)))
        (aset *points* 1 (aref *points* (- point-index 1)))
        (set! point-index 2)
      )
228

229 230 231 232 233
   ;;
   ;; Execution starts here.
   ;;

      (gimp-context-push)
Kevin Cozens's avatar
Kevin Cozens committed
234

235 236 237 238 239 240 241 242
      (gimp-image-undo-group-start img)

      ; Set new color, brush, opacity, paint mode.
      (gimp-context-set-foreground color)
      (gimp-context-set-brush (car brush))
      (gimp-context-set-opacity (* 100 (car (cdr brush))))
      (gimp-context-set-paint-mode (car (cdr (cdr (cdr brush)))))

243 244
      (gimp-progress-set-text _"Rendering Spyro")

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
      (while (< index steps)

          (calc-and-step!)

          (aset *points* point-index cx)
          (aset *points* (+ point-index 1) cy)
          (set! point-index (+ point-index 2))

          ; Change color and draw points if using gradient.
          (if (< 0 color-method)  ; use gradient.
             (if (< (/ (+ grad-index 4) gradn) (/ index steps))
               (begin
                (gimp-context-set-foreground
                  (list
                    (* 255 (aref grada grad-index))
                    (* 255 (aref grada (+ 1 grad-index)) )
                    (* 255 (aref grada (+ 2 grad-index)) )
                  )
263
                )
264 265
                (gimp-context-set-opacity (* 100 (aref grada (+ 3 grad-index) ) )  )
                (set! grad-index (+ 4 grad-index))
266

267 268 269
                ; Draw points
                (flush-points point-index)
               )
270
             )
271
          )
272

273
          (set! index (+ index 1))
274

275 276 277 278
	  (if (= 0 (modulo index 16))
	      (gimp-progress-update (/ index steps))
	  )
      )
279

280 281
      ; Draw remaining points.
      (flush-points point-index)
282

283 284
      (gimp-progress-update 1.0)

285 286
      (gimp-image-undo-group-end img)
      (gimp-displays-flush)
Kevin Cozens's avatar
Kevin Cozens committed
287 288

      (gimp-context-pop)
289
    )
290 291 292
  )

  (let* (
293
        ; Get current selection to determine where to draw.
294 295 296 297 298 299 300
        (bounds (cdr (gimp-selection-bounds img)))
        (x1 (car bounds))
        (y1 (cadr bounds))
        (x2 (caddr bounds))
        (y2 (car (cdddr bounds)))
        )

Kevin Cozens's avatar
Kevin Cozens committed
301 302 303
    (set! oteeth (trunc (+ oteeth 0.5)))
    (set! iteeth (trunc (+ iteeth 0.5)))

304
    (script-fu-spyrogimp-internal img drw
305 306
             x1 y1 x2 y2
             type shape
307
             oteeth iteeth
308 309 310 311 312 313 314 315
             margin hole-ratio start-angle
             tool brush
             color-method color grad)
  )
)



316
(script-fu-register "script-fu-spyrogimp"
Kevin Cozens's avatar
Kevin Cozens committed
317 318 319 320 321 322 323 324 325 326 327 328 329
  _"_Spyrogimp..."
  _"Add Spirographs, Epitrochoids, and Lissajous Curves to the current layer"
  "Elad Shahar <elad@wisdom.weizmann.ac.il>"
  "Elad Shahar"
  "June 2003"
  "RGB*, INDEXED*, GRAY*"
  SF-IMAGE       "Image"         0
  SF-DRAWABLE    "Drawable"      0

  SF-OPTION     _"Type"          '(_"Spyrograph"
                                   _"Epitrochoid"
                                   _"Lissajous")
  SF-OPTION     _"Shape"         '(_"Circle"
330
                                    _"Frame"
Kevin Cozens's avatar
Kevin Cozens committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
                                   _"Triangle"
                                   _"Square"
                                   _"Pentagon"
                                   _"Hexagon"
                                   _"Polygon: 7 sides"
                                   _"Polygon: 8 sides"
                                   _"Polygon: 9 sides"
                                   _"Polygon: 10 sides")
  SF-ADJUSTMENT _"Outer teeth"   '(86 1 120 1 10 0 0)
  SF-ADJUSTMENT _"Inner teeth"   '(70 1 120 1 10 0 0)
  SF-ADJUSTMENT _"Margin (pixels)" '(0 -10000 10000 1 10 0 1)
  SF-ADJUSTMENT _"Hole ratio"    '(0.4 0.0 1.0 0.01 0.1 2 0)
  SF-ADJUSTMENT _"Start angle"   '(0 0 359 1 10 0 0)

  SF-OPTION     _"Tool"          '(_"Pencil"
                                   _"Brush"
                                   _"Airbrush")
  SF-BRUSH      _"Brush"         '("Circle (01)" 1.0 -1 0)

  SF-OPTION     _"Color method"  '(_"Solid Color"
                                   _"Gradient: Loop Sawtooth"
                                   _"Gradient: Loop Triangle")
353
  SF-COLOR      _"Color"          "black"
Kevin Cozens's avatar
Kevin Cozens committed
354
  SF-GRADIENT   _"Gradient"       "Deep Sea"
355 356
)

357
(script-fu-menu-register "script-fu-spyrogimp"
Kevin Cozens's avatar
Kevin Cozens committed
358
                         "<Image>/Filters/Render")