Browse code

tiler.pike: add transparency support

Dario Rodriguez authored on 09/03/2023 00:05:04
Showing 1 changed files
... ...
@@ -6,7 +6,8 @@
6 6
  *
7 7
  * History:
8 8
  *       1/07/2019 Creation
9
- *         5/11/2019 Fix erroneous 1-pixel border around every tile
9
+ *       5/11/2019 Fix erroneous 1-pixel border around every tile
10
+ *       9/03/2023 Support transparency
10 11
  *
11 12
  * Author: Dario Rodriguez dario@softhome.net
12 13
  * This program is distributed under the terms of the GNU GPL v2.1+
... ...
@@ -20,7 +21,10 @@ main(int argc, array(string) argv)
20 21
         int px,py;
21 22
         string destfilename;
22 23
         int total;
23
-        Image.Image img,result;
24
+        Image.Image image,alpha;
25
+        Image.Layer l;
26
+        mapping imagemapping;
27
+        array(Image.Layer) layers=({});
24 28
         if(argc<4 || (argc>1 && argv[1]=="--help")) {
25 29
                 Stdio.werror("Syntax:  "+argv[0]+" <width_in_tiles> <height_in_tiles> <file1.png> [<file2.png> [...]] <destfile.png>\n");
26 30
                 Stdio.werror("Example: "+argv[0]+" 3 2 file1.png file2.png file3.png file4.png file5.png file6.png dest.png\n");
... ...
@@ -34,19 +38,28 @@ main(int argc, array(string) argv)
34 38
         maxx=maxy=1;
35 39
         write("* Calculating tile size\n");
36 40
         foreach(infiles, string f) {
37
-                img=Image.load(f);
38
-                if(img->xsize()>maxx)
39
-                        maxx=img->xsize();
40
-                if(img->ysize()>maxy)
41
-                        maxy=img->ysize();
41
+                image=Image.load(f);
42
+                if(image->xsize()>maxx)
43
+                        maxx=image->xsize();
44
+                if(image->ysize()>maxy)
45
+                        maxy=image->ysize();
42 46
         }
43
-        result=Image.Image(maxx*a4w,maxy*a4h,255,255,255);
47
+        image=Image.Image(maxx*a4w,maxy*a4h,255,255,255);
48
+        alpha=Image.Image(maxx*a4w,maxy*a4h,0,0,0);
49
+        l=Image.Layer(image,alpha,"normal");
50
+        l->set_offset(0,0);
51
+        layers+=({l});
44 52
         px=py=0;
45 53
         total=0;
46 54
         foreach(infiles, string f) {
47 55
                 write("* Processing "+f+"...\n");
48
-                img=Image.load(f);
49
-                result->paste(img,px*(maxx),py*(maxy));
56
+                imagemapping=Image._load(f);
57
+                image=imagemapping["image"];
58
+                alpha=imagemapping["alpha"];
59
+                l=Image.Layer(image,alpha,"normal");
60
+                l->set_offset(px*(maxx),py*(maxy));
61
+                layers+=({l});
62
+                //result->paste(img,px*(maxx),py*(maxy));
50 63
                 px++;
51 64
                 if(px>=a4w) {
52 65
                         px=0;
... ...
@@ -54,8 +67,10 @@ main(int argc, array(string) argv)
54 67
                 }
55 68
                 total++;
56 69
         }
70
+        l=Image.lay(layers);
71
+        l->set_offset(0,0);
57 72
         write("* Writing "+destfilename+"...\n");
58
-        Stdio.write_file(destfilename,Image.PNG.encode(result));
73
+        Stdio.write_file(destfilename,Image.PNG.encode(l->image(),(["alpha":l->alpha()])));
59 74
         write("tiled "+total+" images into "+destfilename+". Process finished.\n");
60 75
         return(0);
61 76
 }