]> marstr Code Repo - orthographic/commitdiff
Implemented new function in layergen to combat the issue of different sources for...
authorMarcus Str <marcus@marstr.online>
Sat, 28 Sep 2024 19:06:40 +0000 (21:06 +0200)
committerMarcus Str <marcus@marstr.online>
Sat, 28 Sep 2024 19:06:40 +0000 (21:06 +0200)
functions.py
layergen.py
textures/multi_source/brd_b.png [new file with mode: 0644]
textures/multi_source/brd_l.png [new file with mode: 0644]
textures/multi_source/brd_r.png [new file with mode: 0644]
textures/multi_source/brd_t.png [new file with mode: 0644]

index 9f022515a4be1d2a5d98a91762644eb2de6a96df..664a875ff39fbee15a4e70356a107dc95c05b4ae 100644 (file)
@@ -83,4 +83,6 @@ def findAirportTiles(av, ah):
 # Testing\r
 def in_circle(center_x, center_y, radius, x, y):\r
     square_dist = (center_x - x) ** 2 + (center_y - y) ** 2\r
-    return square_dist <= radius ** 2
\ No newline at end of file
+    return square_dist <= radius ** 2\r
+\r
+\r
index b47d29dec98d34dd7a56590e4a35f15b82f0dac0..1015283ed3b125cb508d9be12f334bb19b12b074 100644 (file)
@@ -441,6 +441,29 @@ class mstr_layergen:
                     layer_border = self.genborder(osm_edge, "landuse", "meadow")\r
                     layer_comp.alpha_composite(layer_border)\r
 \r
+\r
+            # Here we want to make sure that the generated image fits well with others, so\r
+            # let's do that.\r
+            mstr_msg("layergen", "Generating adjacent fades")\r
+            self.generate_adjacent_fades()\r
+\r
+            # Determine if there are any fades, and if so, fade those in first before we save the layer\r
+            fade_fn = mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value\r
+            if os.path.isfile(fade_fn + "_fade_top.png") == True:\r
+                fade = Image.open(fade_fn + "_fade_top.png")\r
+                layer_comp.alpha_composite(fade)\r
+            if os.path.isfile(fade_fn + "_fade_right.png") == True:\r
+                fade = Image.open(fade_fn + "_fade_right.png")\r
+                layer_comp.alpha_composite(fade)\r
+            if os.path.isfile(fade_fn + "_fade_bottom.png") == True:\r
+                fade = Image.open(fade_fn + "_fade_bottom.png")\r
+                layer_comp.alpha_composite(fade)\r
+            if os.path.isfile(fade_fn + "_fade_left.png") == True:\r
+                fade = Image.open(fade_fn + "_fade_left.png")\r
+                layer_comp.alpha_composite(fade)\r
+            mstr_msg("layergen", "Adjacent fading completed")\r
+            \r
+\r
             # Store layer\r
             if self._is_completion == False:\r
                 layer_comp.save( mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_layer.png" )\r
@@ -805,6 +828,7 @@ class mstr_layergen:
             if self._tag == "waterway" and (self._value == "river" or self._value == "stream"):\r
                 layer_comp = layer_comp.filter(ImageFilter.GaussianBlur(radius=4))\r
 \r
+\r
             # Store layer\r
             layer_comp.save( mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_layer.png" )\r
             mstr_msg("layergen", "Layer image finalized and saved.")\r
@@ -823,3 +847,146 @@ class mstr_layergen:
                         nrm = mstr_xp_normalmap(self._latitude, self._longitude, self._tag, self._value, self._lat_number, self._lng_number, self._latlngfld)\r
                         nrm.build_normalmap()\r
 \r
+\r
+    # Should we find more than one source, the first one found will take precedence.\r
+    # For the others, we will need to generate fading images, so that the final layer \r
+    # image works with other tiles\r
+    def generate_adjacent_fades(self):\r
+        adj_sources = self.find_all_adjacent_sources()\r
+        precedence = -1\r
+\r
+        # Be prepared for every border\r
+        brd_t = Image.open(mstr_datafolder + "textures/multi_source/brd_t.png")\r
+        brd_r = Image.open(mstr_datafolder + "textures/multi_source/brd_r.png")\r
+        brd_b = Image.open(mstr_datafolder + "textures/multi_source/brd_b.png")\r
+        brd_l = Image.open(mstr_datafolder + "textures/multi_source/brd_l.png")\r
+\r
+        brd_t_pix = brd_t.load()\r
+        brd_r_pix = brd_r.load()\r
+        brd_b_pix = brd_b.load()\r
+        brd_l_pix = brd_l.load()\r
+\r
+        for s in range(0, 4):\r
+            if adj_sources[s] != -1:\r
+                precedence = adj_sources[s]\r
+                break\r
+\r
+        # Generate required images\r
+        # Basically a shortened version of the main layergen call\r
+        for s in range(0, 4):\r
+            if adj_sources[s] != precedence and adj_sources[s] != -1:\r
+                src = adj_sources[s]\r
+\r
+                adj_image = Image.new("RGBA", (self._imgsize, self._imgsize))\r
+                adj_pix = adj_image.load()\r
+\r
+                # Root folder\r
+                root_folder = mstr_datafolder + "textures/" + self._tag + "/" + self._value\r
+\r
+                # Load in the sources to work with\r
+                ptc = glob.glob(root_folder + "/ptc/b" + str(src) + "_p*.png")\r
+                brd_src = Image.open(root_folder + "/brd/b" + str(src) + ".png")\r
+                ptc_src = []\r
+                for p in ptc:\r
+                    ptc_src.append(Image.open(p))\r
+\r
+                osm_mask = Image.open( mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + ".png" )\r
+\r
+                for i in mstr_mask_blur:\r
+                    if i[0] == self._tag and i[1] == self._value:\r
+                        if self._tag != "place" and (self._value != "sea" or self._value != "ocean"): \r
+                            osm_mask = osm_mask.filter(ImageFilter.BoxBlur(radius=i[2]))\r
+                            break\r
+                mask_pix = osm_mask.load()\r
+            \r
+                # Begin producing a largely random image\r
+                samples = 250   # <- We need this in a moment\r
+                for i in range(samples):\r
+                    imgid = 0\r
+                    if len(ptc_src) == 1: imgid = 0\r
+                    if len(ptc_src) >= 2:\r
+                        imgid = randrange(1, len(ptc_src)+1) - 1\r
+                    l = 0 - int(ptc_src[imgid].width / 2)\r
+                    r = adj_image.width - int(ptc_src[imgid].width / 2)\r
+                    t = 0 - int(ptc_src[imgid].height / 2)\r
+                    b = adj_image.height - int(ptc_src[imgid].height / 2)\r
+                    adj_image.alpha_composite( ptc_src[imgid], ( randrange(l, r), randrange(t, b) ) )\r
+\r
+                adj_image.alpha_composite( brd_src )\r
+\r
+                for y in range(self._imgsize):\r
+                    for x in range(self._imgsize):\r
+                        if mask_pix[x, y][3] > 0:\r
+                            rgb=adj_pix[x,y]\r
+                            a=mask_pix[x,y]\r
+                            adj_pix[x, y] = ( rgb[0], rgb[1], rgb[2], a[3])\r
+\r
+                # Up until here we mimiced the exact same behavior as layergen. However, now\r
+                # we need to adjust the alpha to make this layer fade.\r
+                # Then, we save the image\r
+                if s == 0:\r
+                    for y in range(self._imgsize):\r
+                        for x in range(self._imgsize):\r
+                            fade_a = brd_t_pix[0, y]\r
+                            if mask_pix[x, y][3] > 0:\r
+                                c = adj_pix[x,y]\r
+                                adj_pix[x,y] = (c[0], c[1], c[2], fade_a[3])\r
+                            else:\r
+                                adj_pix[x,y] = (0,0,0,0)\r
+                    adj_image.save(mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_fade_top.png")\r
+                \r
+                if s == 1:\r
+                    for y in range(self._imgsize):\r
+                        for x in range(self._imgsize):\r
+                            fade_a = brd_r_pix[x, 0]\r
+                            if mask_pix[x, y][3] > 0:\r
+                                c = adj_pix[x,y]\r
+                                adj_pix[x,y] = (c[0], c[1], c[2], fade_a[3])\r
+                            else:\r
+                                adj_pix[x,y] = (0,0,0,0)\r
+                    adj_image.save(mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_fade_right.png")\r
+                \r
+                if s == 2:\r
+                    for y in range(self._imgsize):\r
+                        for x in range(self._imgsize):\r
+                            fade_a = brd_b_pix[0, y]\r
+                            if mask_pix[x, y][3] > 0:\r
+                                c = adj_pix[x,y]\r
+                                adj_pix[x,y] = (c[0], c[1], c[2], fade_a[3])\r
+                            else:\r
+                                adj_pix[x,y] = (0,0,0,0)\r
+                    adj_image.save(mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_fade_bottom.png")\r
+\r
+                if s == 3:\r
+                    for y in range(self._imgsize):\r
+                        for x in range(self._imgsize):\r
+                            fade_a = brd_l_pix[x, 0]\r
+                            if mask_pix[x, y][3] > 0:\r
+                                c = adj_pix[x,y]\r
+                                adj_pix[x,y] = (c[0], c[1], c[2], fade_a[3])\r
+                            else:\r
+                                adj_pix[x,y] = (0,0,0,0)\r
+                    adj_image.save(mstr_datafolder + "_cache/" + str(self._latitude) + "-" + str(self._lat_number) + "_" + str(self._longitude) + "-" + str(self._lng_number) + "_" + self._tag + "-" + self._value + "_fade_left.png")\r
+\r
+\r
+    def find_all_adjacent_sources(self):\r
+        # Sources for this tag and value - top, right, bottom, left\r
+        sources = [-1,-1,-1,-1] \r
+\r
+        # Perform query for each neighboring tile\r
+        src_top = self._tileinfo.get_adjacency_for_tag_and_value(self._lat_number+1, self._lng_number, self._tag, self._value)\r
+        src_rgt = self._tileinfo.get_adjacency_for_tag_and_value(self._lat_number, self._lng_number+1, self._tag, self._value)\r
+        src_btm = self._tileinfo.get_adjacency_for_tag_and_value(self._lat_number-1, self._lng_number, self._tag, self._value)\r
+        src_lft = self._tileinfo.get_adjacency_for_tag_and_value(self._lat_number, self._lng_number-1, self._tag, self._value)\r
+\r
+        if len(src_top) == 2:\r
+            if "b" in src_top[1]: sources[0] = src_top[0]\r
+        if len(src_rgt) == 2:\r
+            if "l" in src_rgt[1]: sources[1] = src_rgt[0]\r
+        if len(src_btm) == 2:\r
+            if "t" in src_btm[1]: sources[2] = src_btm[0]\r
+        if len(src_lft) == 2:\r
+            if "r" in src_lft[1]: sources[3] = src_lft[0]\r
+\r
+        # Report our findings\r
+        return sources\r
diff --git a/textures/multi_source/brd_b.png b/textures/multi_source/brd_b.png
new file mode 100644 (file)
index 0000000..c28c1e1
Binary files /dev/null and b/textures/multi_source/brd_b.png differ
diff --git a/textures/multi_source/brd_l.png b/textures/multi_source/brd_l.png
new file mode 100644 (file)
index 0000000..7d0768d
Binary files /dev/null and b/textures/multi_source/brd_l.png differ
diff --git a/textures/multi_source/brd_r.png b/textures/multi_source/brd_r.png
new file mode 100644 (file)
index 0000000..d4dddf9
Binary files /dev/null and b/textures/multi_source/brd_r.png differ
diff --git a/textures/multi_source/brd_t.png b/textures/multi_source/brd_t.png
new file mode 100644 (file)
index 0000000..7436210
Binary files /dev/null and b/textures/multi_source/brd_t.png differ