]> marstr Code Repo - marstr/orthographic.git/commitdiff
Different approach to resource placement raytracing, several layergen fixes, adjustme...
authorMarcus Str. <marcus@marstr.online>
Thu, 19 Dec 2024 11:16:21 +0000 (12:16 +0100)
committerMarcus Str. <marcus@marstr.online>
Thu, 19 Dec 2024 11:16:21 +0000 (12:16 +0100)
defines.py
layergen.py
log.py
og.py
orthographic.py
photogen.py
tileprep.py

index 638d86b40dd4eedc23a8f835ba1d5d967a080791..4aaf21e3c80ad5aee090f84820fa568479fed853 100644 (file)
@@ -44,7 +44,7 @@ mstr_clear_cache = True
 \r
 # Whether or not you want to see progress of the tool as it walks on.\r
 # High recommendation to leave this on.\r
-mstr_show_log = True\r
+mstr_show_log = False\r
 \r
 \r
 # Should a pseudo shadow be rendered on certain elements?\r
index 5f6aa96bbec941d43156498f805a60eae32b720e..8a676f5e20c10cee3b596eafbf790b13bcc6e882 100644 (file)
@@ -390,7 +390,7 @@ class mstr_layergen:
 \r
                     # Make sure patch is within bounds\r
                     if patchmask.width > self._imgsize or patchmask.height > self._imgsize:\r
-                        patchmask.resize((mstr_photores, mstr_photores), Image.Resampling.BILINEAR)\r
+                        patchmask = patchmask.resize((mstr_photores, mstr_photores), Image.Resampling.BILINEAR)\r
 \r
                     patchpix = patchmask.load()\r
                     # Pick from possible tags and values for the patches\r
@@ -403,7 +403,16 @@ class mstr_layergen:
                     rg.setLayerContrast(ctr)\r
                     ptch = rg.gensource()\r
 \r
-                    rg_img = ptch[0]\r
+                    # Generate a full size of the source\r
+                    ptc_full = Image.new("RGBA", (mstr_photores, mstr_photores))\r
+\r
+                    # Generate the source image\r
+                    for p in range(1, 201):\r
+                        rx = randrange(0 - int(ptch[0].width / 2), ptc_full.width - int(ptch[0].width / 2))\r
+                        ry = randrange(0 - int(ptch[0].height / 2), ptc_full.height - int(ptch[0].height / 2))\r
+                        ptc_full.alpha_composite(ptch[0], dest=(rx, ry))\r
+\r
+                    rg_img = ptc_full\r
                     rg_pix = rg_img.load()\r
 \r
                     # The patch to be used in the layer\r
@@ -593,6 +602,7 @@ class mstr_layergen:
                         if lx < self._imgsize and ly < self._imgsize:\r
                             tree = self.generate_tree()\r
                             trees.alpha_composite(tree, (lx, ly))\r
+                trees = ImageEnhance.Contrast(trees).enhance(0.8)\r
                 if mstr_shadow_enabled == True:\r
                     tree_shadow = Image.new("RGBA", (self._imgsize, self._imgsize))\r
                     tree_pix = trees.load()\r
diff --git a/log.py b/log.py
index 34eb6d2d83420767f2038a6de4d0b1cf61f64f19..ef711dc3e2d28c5d09fdd7613b82153090902572 100644 (file)
--- a/log.py
+++ b/log.py
@@ -15,6 +15,11 @@ from colorama import init as colorama_init
 from colorama import Fore\r
 from colorama import Style\r
 from defines import *\r
+import sys\r
+import warnings\r
+\r
+if not sys.warnoptions:\r
+    warnings.simplefilter("ignore")\r
 \r
 def mstr_msg(fnc, msg):\r
     if mstr_show_log == True:\r
@@ -22,4 +27,12 @@ def mstr_msg(fnc, msg):
         now = datetime.datetime.now()\r
 \r
         print(f' {Fore.GREEN}'+now.strftime("%H:%M:%S")+f'{Style.RESET_ALL} | {Fore.YELLOW}[' + fnc + f']{Style.RESET_ALL} | {Fore.CYAN}'+ msg + f'{Style.RESET_ALL}')\r
-        #print(f"{Fore.GREEN}" + now.strftime(" %H:%M:%S" + " | ["+fnc+"] | " + msg))
\ No newline at end of file
+        #print(f"{Fore.GREEN}" + now.strftime(" %H:%M:%S" + " | ["+fnc+"] | " + msg))\r
+\r
+\r
+def mstr_important_msg(fnc, msg):\r
+    colorama_init()\r
+    now = datetime.datetime.now()\r
+\r
+    print(f' {Fore.GREEN}' + now.strftime(\r
+        "%H:%M:%S") + f'{Style.RESET_ALL} | {Fore.YELLOW}[' + fnc + f']{Style.RESET_ALL} | {Fore.CYAN}' + msg + f'{Style.RESET_ALL}')
\ No newline at end of file
diff --git a/og.py b/og.py
index 9c646ff333c3c71e0113ef0dcbdcad5340f1a961..53a5186d47aa82615ee802dda0c6d5fb8a5fad9a 100644 (file)
--- a/og.py
+++ b/og.py
@@ -19,12 +19,25 @@ from defines import *
 \r
 \r
 # Print a welcome message\r
-print(" ")\r
-print(" ---------------------------------------------------------------- ")\r
-print(" ORTHOGRAPHIC: An ortho-photo generator, using real world data.")\r
-print(" Developed by MarStr - Code available on marstr.online")\r
-print(" ---------------------------------------------------------------- ")\r
-print(" ")\r
+print(r' ')\r
+print(r'   ____       __  __                                 __    _     ')\r
+print(r'  / __ \_____/ /_/ /_  ____  ____ __________ _____  / /_  (_)____')\r
+print(r' / / / / ___/ __/ __ \/ __ \/ __ `/ ___/ __ `/ __ \/ __ \/ / ___/')\r
+print(r'/ /_/ / /  / /_/ / / / /_/ / /_/ / /  / /_/ / /_/ / / / / / /__  ')\r
+print(r'\____/_/   \__/_/ /_/\____/\__, /_/   \__,_/ .___/_/ /_/_/\___/  ')\r
+print(r'                          /____/          /_/                    ')\r
+print(r' ----------------------------------------------------------------')\r
+print(r'   A ground texture generator, using photo realistic resources,')\r
+print(r'                    for flight simulators.')\r
+print(r' ----------------------------------------------------------------')\r
+print(r'               Developed by and (c) Marcus Str.')\r
+print(r'          Website: https://marstr.online/orthographic')\r
+print(r' Code repo:  https://marstr.online/code/gitweb.cgi?p=orthographic')\r
+print(r' ----------------------------------------------------------------')\r
+print(r' If you paid for this software, you have been scammed. The source')\r
+print(r'           code and always available free of charge.')\r
+print(r' ----------------------------------------------------------------')\r
+print(r' ')\r
 \r
 \r
 # Evaluate CLI arguments and process tile.\r
index 459bb68f53899864dcedc667147f267c49fd35cb..3a7e4d9d00abe7ae071811dea502ddf3388c9735 100644 (file)
@@ -279,9 +279,7 @@ class mstr_orthographic:
                 pg = mstr_photogen(self._lat, self._long, grid_lat, grid_lng,  maxlatlng[0], maxlatlng[1])\r
                 pg.setLayerNames(layers)\r
                 pg.genphoto(photolayers, waterlayers, cpl)\r
-                mstr_msg("orthographic", " -- Ortho photo generated -- ")\r
-                print("")\r
-                print("")\r
+                mstr_important_msg("orthographic", "Ortho photo " + str(grid_lat)+"_"+str(grid_lng)+" generated!")\r
 \r
             # Perform adjustment of grid position\r
             n_lng = grid_lng + step\r
index 17b6b0fccc1975042ef479df72c82dd1dba66813..fe3e605075607b635384b32af691071c5b6f6924 100644 (file)
@@ -129,7 +129,7 @@ class mstr_photogen:
 \r
                 # Make sure patch is within bounds\r
                 if patchmask.width > self._tile.width or patchmask.height > self._tile.height:\r
-                    patchmask.resize((mstr_photores, mstr_photores), Image.Resampling.BILINEAR)\r
+                    patchmask = patchmask.resize((mstr_photores, mstr_photores), Image.Resampling.BILINEAR)\r
 \r
                 patchpix = patchmask.load()\r
 \r
@@ -143,7 +143,16 @@ class mstr_photogen:
                 rg.setLayerContrast(randrange(1, 4))\r
                 ptch = rg.gensource()\r
 \r
-                rg_img = ptch[0]\r
+                # Generate a full size of the source\r
+                ptc_full = Image.new("RGBA", (mstr_photores, mstr_photores))\r
+\r
+                # Generate the source image\r
+                for p in range(1, 201):\r
+                    rx = randrange(0 - int(ptch[0].width / 2), ptc_full.width - int(ptch[0].width / 2))\r
+                    ry = randrange(0 - int(ptch[0].height / 2), ptc_full.height - int(ptch[0].height / 2))\r
+                    ptc_full.alpha_composite(ptch[0], dest=(rx, ry))\r
+\r
+                rg_img = ptc_full\r
                 rg_pix = rg_img.load()\r
 \r
                 # The patch to be used in the layer\r
@@ -348,6 +357,8 @@ class mstr_photogen:
                         tree = self.generate_tree()\r
                         trees.alpha_composite(tree, (lx, ly))\r
 \r
+                trees = ImageEnhance.Contrast(trees).enhance(0.8)\r
+\r
                 tree_shadow = Image.new("RGBA", (self._imgsize, self._imgsize))\r
                 tree_pix = trees.load()\r
                 shadow_pix = tree_shadow.load()\r
index 473582711a88180a8ca3f0e545017f4e9bd0c09d..a43123ee6b1fc3dd3d250d8db92b3046e3031e2a 100644 (file)
@@ -127,12 +127,30 @@ class mstr_tileprep:
             for ln in fnlines:
                 l = ln.split(" ")
                 if l[0] == self._tag and l[1] == self._value:
-                    l[3] = l[3].replace("\n", "")
                     src = l[2]
 
         return src
 
 
+    # Get existing contrast value
+    def _getResourceContrast(self, tv, th):
+        # This either remains 0 or a string different to "0" in the end
+        ctr = "0"
+        df = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tv) + "_" + str(th)
+        fnlines = []
+        if os.path.isfile(df) == True: # It is possible that the requested file does not yet exist
+            with open(df) as textfile:
+                fnlines = textfile.readlines()
+            
+            for ln in fnlines:
+                l = ln.split(" ")
+                if l[0] == self._tag and l[1] == self._value:
+                    l[4] = l[4].replace("\n", "")
+                    ctr = l[4]
+
+        return ctr
+
+
     # Find the edge touch info
     def _getResourceTouch(self, tv, th):
         touch = ""
@@ -145,7 +163,6 @@ class mstr_tileprep:
             for ln in fnlines:
                 l = ln.split(" ")
                 if l[0] == self._tag and l[1] == self._value:
-                    l[3] = l[3].replace("\n", "")
                     touch = l[3]
 
         return touch
@@ -167,43 +184,47 @@ class mstr_tileprep:
 
 
     # Store the required resource information into the appropriate tile
-    def _storeResourceInfo(self, tile_v, tile_h, res):
+    def _storeResourceInfo(self, tile_v, tile_h, res, ctr):
         df = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tile_v) + "_" + str(tile_h)
         fnlines = []
-        contrast = 0
+        contrast = ctr
         if os.path.isfile(df) == True: # It is possible that the requested file does not yet exist
             with open(df) as textfile:
                 fnlines = textfile.readlines()
             curline = 0
             for ln in fnlines:
                 l = ln.split(" ")
-                if l[0] == self._tag and l[1] == self._value:
-                    l[2] = res
-                contrast = int(l[4])
-
-                # Find contrast values for some tags
-                if (
-                        (l[0] == "landuse" and l[1] == "forest") or
-                        (l[0] == "landuse" and l[1] == "meadow") or
-                        (l[0] == "landuse" and l[1] == "grass") or
-                        (l[0] == "leisure" and l[1] == "nature_reserve") or
-                        (l[0] == "natural" and l[1] == "grassland") or
-                        (l[0] == "landuse" and l[1] == "greenfield") or
-                        (l[0] == "natural" and l[1] == "heath") or
-                        (l[0] == "natural" and l[1] == "wetland") or
-                        (l[0] == "leisure" and l[1] == "park") or
-                        (l[0] == "building")
-                ):
-                    if int(l[4]) == 0: contrast = randrange(1, 4)
-
-                l[3] = l[3].replace("\n", "")
-                fnlines[curline] = l[0] + " " + l[1] + " " + l[2] + " " + l[3] + " " + str(contrast) + "\n"
-
+                if len(l) > 1:
+                    if l[0] == self._tag and l[1] == self._value:
+                        l[2] = res
+                    l[4] = l[4].replace("\n", "")
+                    #contrast = l[4]
+
+                    # Find contrast values for some tags
+                    if contrast == "0":
+                        if (
+                                (l[0] == "landuse" and l[1] == "forest") or
+                                (l[0] == "landuse" and l[1] == "meadow") or
+                                (l[0] == "landuse" and l[1] == "grass") or
+                                (l[0] == "leisure" and l[1] == "nature_reserve") or
+                                (l[0] == "natural" and l[1] == "grassland") or
+                                (l[0] == "landuse" and l[1] == "greenfield") or
+                                (l[0] == "natural" and l[1] == "heath") or
+                                (l[0] == "natural" and l[1] == "wetland") or
+                                (l[0] == "leisure" and l[1] == "park") or
+                                (l[0] == "building")
+                        ):
+                            contrast = str(randrange(1, 4))
+                    
+                    l[4] = l[4].replace("\n", "")
+                    fnlines[curline] = l[0] + " " + l[1] + " " + l[2] + " " + l[3] + " " + contrast
+                
                 curline = curline+1
 
             lines = ""
             for l in range(len(fnlines)):
                 lines = lines + fnlines[l]
+                if l < len(lines)-1: lines = lines+"\n"
 
             with open(df, 'w') as textfile:
                 textfile.write(lines)
@@ -213,78 +234,100 @@ class mstr_tileprep:
     # sources for every tile, thus evading previous edge detection errors
     def _placeTileSources(self, mlat, mlng):
 
-        # The first tile gets to choose something for itself
-        if self._tile_v == 1 and self._tile_h == 1:
-            resstr = self._selectResources()
-            self._storeResourceInfo(1, 1, resstr)
-
-        # Start "raytracing"
-
-        # Initial reset
-        tv = self._tile_v
-        th = self._tile_h
-        # Start marching north
-        while tv < mlat+1:
-            restch = self._getResourceTouch(tv, th)
-            if "t" in restch:
-                resstr = self._getResourceInfo(tv, th)
-                if resstr == "0":
-                    resstr = self._selectResources()
-                    self._storeResourceInfo(tv, th, resstr)
-                resd = self._getResourceInfo(tv+1, th)
-                if resd=="0":
-                    self._storeResourceInfo(tv + 1, th, resstr)
-            else:
-                break
-            tv = tv + 1
-
-        # Start marching east
-        tv = self._tile_v
-        th = self._tile_h
-        while th < mlng + 1:
-            restch = self._getResourceTouch(tv, th)
-            if "r" in restch:
-                resstr = self._getResourceInfo(tv, th)
-                if resstr == "0":
-                    resstr = self._selectResources()
-                    self._storeResourceInfo(tv, th, resstr)
-                resd = self._getResourceInfo(tv, th+1)
-                if resd == "0":
-                    self._storeResourceInfo(tv, th + 1, resstr)
-            else:
-                break
-            th = th + 1
-
-        # Start marching south
-        tv = self._tile_v
-        th = self._tile_h
-        while tv > 0:
-            restch = self._getResourceTouch(tv, th)
-            if "b" in restch:
-                resstr = self._getResourceInfo(tv, th)
-                if resstr == "0":
-                    resstr = self._selectResources()
-                    self._storeResourceInfo(tv, th, resstr)
-                resd = self._getResourceInfo(tv - 1, th)
-                if resd == "0":
-                    self._storeResourceInfo(tv - 1, th, resstr)
-            else:
-                break
-            tv = tv - 1
-
-        # Start marching west
-        tv = self._tile_v
-        th = self._tile_h
-        while th > 0:
-            restch = self._getResourceTouch(tv, th)
-            if "l" in restch:
-                resstr = self._getResourceInfo(tv, th)
-                if resstr == "0":
-                    resstr = self._selectResources()
-                    self._storeResourceInfo(tv, th, resstr)
-                resd = self._getResourceInfo(tv, th-1)
-                if resd == "0":
-                    self._storeResourceInfo(tv, th - 1, resstr)
-            else:
-                break
-            th = th - 1
\ No newline at end of file
+        # None of the edges have reached their end yet
+        edge_end = [0,0,0,0] # top, right, bottom, left
+
+        # Array with info we need
+        resinfo = ["", "", ""] # Touch, Resource numbers, contrast
+
+        # Go through everything until the end is reached (no more options left)
+        end_reached = False
+        while end_reached == False:
+
+            # Go north
+            tv = self._tile_v
+            th = self._tile_h
+
+            while edge_end[0] == 0:
+                resinfo[0] = self._getResourceTouch(tv, th)
+                resinfo[1] = self._getResourceInfo(tv, th)
+                resinfo[2] = self._getResourceContrast(tv, th)
+
+                if resinfo[1] == "0":
+                    self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2])
+                    resinfo[1] = self._getResourceInfo(tv, th)
+                    resinfo[2] = self._getResourceContrast(tv, th)
+
+                if "t" in resinfo[0]:
+                    tv=tv+1
+                    rinfo = self._getResourceInfo(tv, th)
+                    if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2])
+                else:
+                    edge_end[0] = 1
+
+            # Go east
+            tv = self._tile_v
+            th = self._tile_h
+
+            while edge_end[1] == 0:
+                resinfo[0] = self._getResourceTouch(tv, th)
+                resinfo[1] = self._getResourceInfo(tv, th)
+                resinfo[2] = self._getResourceContrast(tv, th)
+
+                if resinfo[1] == "0":
+                    self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2])
+                    resinfo[1] = self._getResourceInfo(tv, th)
+                    resinfo[2] = self._getResourceContrast(tv, th)
+
+                if "r" in resinfo[0]:
+                    th=th+1
+                    rinfo = self._getResourceInfo(tv, th)
+                    if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2])
+                else:
+                    edge_end[1] = 1
+
+            # Go south
+            tv = self._tile_v
+            th = self._tile_h
+
+            while edge_end[2] == 0:
+                resinfo[0] = self._getResourceTouch(tv, th)
+                resinfo[1] = self._getResourceInfo(tv, th)
+                resinfo[2] = self._getResourceContrast(tv, th)
+
+                if resinfo[1] == "0":
+                    self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2])
+                    resinfo[1] = self._getResourceInfo(tv, th)
+                    resinfo[2] = self._getResourceContrast(tv, th)
+
+                if "b" in resinfo[0]:
+                    tv=tv-1
+                    rinfo = self._getResourceInfo(tv, th)
+                    if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2])
+                else:
+                    edge_end[2] = 1
+
+            # Go west
+            tv = self._tile_v
+            th = self._tile_h
+
+            while edge_end[3] == 0:
+                resinfo[0] = self._getResourceTouch(tv, th)
+                resinfo[1] = self._getResourceInfo(tv, th)
+                resinfo[2] = self._getResourceContrast(tv, th)
+
+                if resinfo[1] == "0":
+                    self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2])
+                    resinfo[1] = self._getResourceInfo(tv, th)
+                    resinfo[2] = self._getResourceContrast(tv, th)
+
+                if "l" in resinfo[0]:
+                    th=th-1
+                    rinfo = self._getResourceInfo(tv, th)
+                    if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2])
+                else:
+                    edge_end[3] = 1
+
+
+            if edge_end[0] == 1 and edge_end[1] == 1 and edge_end[2] == 1 and edge_end[3] == 1:
+                end_reached = True # <- Break loop
\ No newline at end of file