; The GIMP -- an 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 2 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, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;
; rasterize-selection.scm   version 0.90   12/07/97
;
; Copyright (C) 1997 Simon Budig (Simon.Budig@unix-ag.org)
; 
; Makes the active selection look "blocky".

(define (script-fu-rasterize-selection image
                                  drawable
                                  blocksize
                                  blurfactor)

   (gimp-undo-push-group-start image)
   (let* ((select-bounds (gimp-selection-bounds image))
         (has-selection (car select-bounds))
         (x1 (cadr select-bounds))
         (y1 (caddr select-bounds))
         (x2 (cadr (cddr select-bounds)))
         (y2 (caddr (cddr select-bounds)))
         (xrun (- x1 (modulo x1 blocksize)))
         (yrun y1)
         (width (- x2 x1))
         (height (- y2 y1))
         (blurradius (* blocksize blurfactor))
         (saved-selection (car (gimp-selection-save image)))
         (mode (if (= TRUE has-selection) INTERSECT REPLACE))
      )
      (set! blocksize (max blocksize 1))
      (set! blurradius (max blurradius 1))
      (while (<= xrun x2 )
         (set! yrun (- y1 (modulo y1 blocksize)))
         (while (<= yrun y2)
            (if (= TRUE has-selection) 
               (gimp-selection-load image saved-selection) ())
            (gimp-rect-select image xrun yrun blocksize blocksize mode 0 0)
            (if (= (car (gimp-selection-is-empty image)) FALSE)
               (plug-in-gauss-iir 1 image drawable blurradius TRUE TRUE) ())
            (set! yrun (+ yrun blocksize))
         )
         (set! xrun (+ xrun blocksize))
      )
      (gimp-selection-load image saved-selection)
      (gimp-image-remove-channel image saved-selection)
      (gimp-displays-flush)
   )
   (gimp-undo-push-group-end image)
)

(script-fu-register "script-fu-rasterize-selection" 
                    "<Image>/Script-Fu/Decor/Rasterize"
                    "Makes the active selection look \"blocky\"." 
                    "Simon Budig (Simon.Budig@unix-ag.org)"
                    "Simon Budig"
                    "12/07/97"
                    "RGB RGBA GRAY GRAYA"
                    SF-IMAGE "Image" 0
                    SF-DRAWABLE "Drawable" 0
                    SF-VALUE "Size of blocks" "20"
                    SF-VALUE "Blurfactor" "2")
