where put files
[mop-gimp-scripts.git] / mop_im_explode_via_trasp_lay.scm
1 ; The GIMP -- an image manipulation program
2 ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
4 ; Explode via Transparent Layer NG
5 ; Copyright (c) 2000-2002 Matteo Nastasi aka MatOfPenguins aka mop
6 ; nastasi@alternativeoutput.it
7 ; matteo.nastasi@milug.org
8 ;
9 ; Take all layers with particular prefix, put disable everyone of this,
10 ; and to loop for enable one to one, copy the image, crop and save it
11 ; with the name of layer.
12 ;
13 ;
14 ; This program is free software; you can redistribute it and/or modify
15 ; it under the terms of the GNU General Public License as published by
16 ; the Free Software Foundation; either version 2 of the License, or
17 ; (at your option) any later version.
18
19 ; This program is distributed in the hope that it will be useful,
20 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ; GNU General Public License for more details.
23
24 ; You should have received a copy of the GNU General Public License
25 ; along with this program; if not, write to the Free Software
26 ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28 (define (script-fu-mop-im-explode-via-trasplay-batch filename
29                               pref
30                               ext
31                               path)
32    (let* ((image 0) (drawable 0))
33      (set! image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
34      (set! drawable (car (gimp-image-get-active-layer image)))
35      (script-fu-mop-im-explode-via-transplay image drawable pref ext path)
36      (gimp-image-delete image)
37      (gimp-quit 0) ))
38
39 (define (script-fu-mop-im-explode-via-transplay img drw pref type path)
40 ; disable undo
41   (let* ((char-num 0) (layarray 0) (nlay 0) (lay-arr 0) (ct 0) (i 0)
42          (curlay 0) (namelay 0) (namelaylen 0) (name 0) (namelen 0)
43          (nw-img 0) (nw-layarray 0) (nw-lay-arr 0) (nw-ct 0) (nw-i 0)
44          (nw-curlay 0) (nw-namelay 0) (nw-w 0) (nw-h 0) (nw-x 0) (nw-y 0)
45          (offsets 0) (fla 0) (namefile 0)) 
46     (gimp-image-undo-freeze img)
47                                         ; number of char in prefix
48     ; (gimp-message (string-append "pref: " pref))
49     
50     (set! char-num (string-length pref))
51 ; extract number and array of layer
52     (set! layarray (gimp-image-get-layers img))
53     (set! nlay (car layarray))
54     (set! lay-arr (cadr layarray))
55     (set! ct 0)
56     (set! i 0)
57
58     (while (< i nlay) 
59                                         ;         (gimp-message "in while")
60            (set! curlay (aref lay-arr i))
61            (set! namelay (car (gimp-layer-get-name curlay)))
62            
63            (if (and (>= (string-length namelay) char-num)
64                     (equal? (substring namelay 0 char-num) pref))
65                (begin
66                                         ;               (gimp-message (string-append "save layer: " namelay))
67                  (set! namelaylen (string-length namelay))
68                  (set! name (substring namelay char-num namelaylen))
69                  (set! namelen (string-length name))
70                  
71                                         ; if layer start with pref dup image and save only this active
72                  (set! nw-img (car (gimp-image-duplicate img)))
73                  
74                                         ;              (set! nw-img2 (car (gimp-image-duplicate img)))
75                                         ;              (set! fla2 (car (gimp-image-merge-visible-layers nw-img2 0)))
76                                         ;              (set! namefile (string-append path "/" name "_orig." type))
77                                         ;              (gimp-file-save 1 nw-img2 fla2 namefile namefile)
78                  
79                  (set! nw-layarray (gimp-image-get-layers nw-img))
80                  (set! nw-lay-arr (cadr nw-layarray))
81                  (set! nw-ct 0)
82                  (set! nw-i 0)
83                  
84                  (while (< nw-i nlay)
85                                         ;                      (gimp-message "in while2")
86                         
87                         (set! nw-curlay (aref nw-lay-arr nw-i))
88                         (if (= nw-i i)
89                             (begin
90                                         ;                           (gimp-image-set-active-layer nw-img nw-curlay)
91                                         ;                            (gimp-message "begin if")
92                               (set! nw-namelay (car (gimp-layer-get-name nw-curlay)))
93                                         ;                            (gimp-message (string-append "layer activated = nw-i: " nw-namelay (number->string nw-i)))
94                               (gimp-layer-set-visible nw-curlay TRUE)
95                               (set! nw-w (car (gimp-drawable-width nw-curlay)))
96                               (set! nw-h (car (gimp-drawable-height nw-curlay)))
97                               (set! offsets (gimp-drawable-offsets nw-curlay))
98                               (set! nw-x (car  offsets))
99                               (set! nw-y (cadr offsets))
100                               )
101                             (begin
102                                         ;    (gimp-image-set-active-layer nw-img nw-curlay)
103                               (set! nw-namelay (car (gimp-layer-get-name nw-curlay)))
104                                         ;                            (gimp-message (string-append "begin else xx " nw-namelay))
105                               (if (and (>= (string-length nw-namelay) char-num)
106                                        (equal? (substring nw-namelay 0 char-num) pref))
107                                   (gimp-layer-set-visible nw-curlay FALSE)
108                                   )
109                               
110                               (if (and (>= (string-length nw-namelay) (string-length (string-append name "-")))
111                                        (equal? (substring nw-namelay 0 (+ namelen 1)) (string-append name "-")))
112                                   (begin
113                                         ;                                  (gimp-message (string-append "layer activated: " nw-namelay))
114                                     
115                                     (gimp-layer-set-visible nw-curlay TRUE)
116                                     )
117                                   )
118                               
119                                         ;                            (gimp-message "end else")
120                               
121                               ))
122                         (set! nw-i (+ nw-i 1))
123                         )
124                                         ;               (gimp-message "out while2")
125                  
126                  (gimp-crop nw-img nw-w nw-h nw-x nw-y)
127                  (set! fla (car (gimp-image-merge-visible-layers nw-img 0)))
128                                         ; (set! fla (car (gimp-image-flatten nw-img)))
129                                         ; (gimp-message (string-append namelay "." type))
130                  
131                                         ;              (set! namelen (string-length namelay))
132                                         ;              (set! namefile (string-append path "/" (substring namelay
133                                         ;                                                       char-num namelen)
134                                         ;                                            "." type))
135                  
136                  (set! namefile (string-append path "/" name "." type))
137                  
138                  (gimp-file-save 1 nw-img fla namefile namefile)
139                                         ; (file-png-save2 1 nw-img fla namefile namefile 0 9 1 1 1 1 1 0 1)
140                                         ; (file-png-save2 1 nw-img fla namefile namefile 1 9 0 0 0 0 0 1 0)
141                  (gimp-image-delete nw-img)
142                                         ; (gimp-display-new nw-img)
143                  (set! ct (+ ct 1))
144                  )
145                )
146            (set! i (+ i 1))
147            ) ; end of while
148     
149                                         ;  (gimp-message "the end")
150     
151     
152     (gimp-image-undo-thaw img)
153     )
154   )
155
156   (script-fu-register "script-fu-mop-im-explode-via-transplay"
157                       "<Image>/Script-Fu/MOP/Explode via Trasp Lay"
158                       "Convert a selection to a new transparent layer"
159                     "Matteo Nastasi <nastasi@alternativeoutput.it>"
160                     "Matteo Nastasi"
161                     "09/11/2000"
162                     "RGB RGBA GRAY GRAYA"
163                     SF-IMAGE "Image" 0
164                     SF-DRAWABLE "Drawable" 0
165                     SF-STRING "Prefix" "SUB_"
166                     SF-STRING "Extension" "png"
167                     SF-DIRNAME "Path"       ".")
168
169
170