Page 1 of 1

A VT creation tool for the GIMP

Posted: 23.02.2004, 16:36
by Toti
If you want to make your own VT, the best solution is ImageMagick and Fridger's Virtualtex script.
It is fast (especially for such an intensive job), simple and reliable.

Unfortunately, it needs a Unix-kind of environment to work.
If you do not want to mess with Cygwin, console commands, etc. and the GIMP is installed in your system, you can use the script below:

Code: Select all

(define (script-fu-map-create-vt inImageFile inPath inLevel inBaseTileSize inFirstX inFirstY inOrigLevel inHemisphere inSaveFormat)
      
      (set! inPath (string-append inPath "/level" (number->string (+ inLevel inOrigLevel))))
      (set! theImage (car (gimp-file-load 1 inImageFile inImageFile)))
      (gimp-image-undo-disable theImage)    
      (set! theDrawable (car (gimp-image-active-drawable theImage)))
      (set! theImageWidth (car (gimp-drawable-width theDrawable)))
        (set! theImageHeight (car (gimp-drawable-height theDrawable)))
      (set! theAspectRatio (/ theImageWidth theImageHeight))
       (set! theImageType (car (gimp-image-base-type theImage)))               
      (set! theMultiplier (pow 2 inLevel))
      (set! theNewWidth (* (* inBaseTileSize theMultiplier) theAspectRatio))   
      (set!   theNewHeight (* inBaseTileSize theMultiplier))   
      (if (= inHemisphere 1)
         (set! inFirstX 1)
      ); if
      (set! inFirstX (* inFirstX theMultiplier))
      (set! inFirstY (* inFirstY theMultiplier))
      
      (if (<= theNewWidth theImageWidth)         ;avoids oversampling
      (begin               
         (if (< theNewWidth theImageWidth) (gimp-image-scale theImage theNewWidth theNewHeight))
         (set! j 0)   
         (set! theInitPosY 0)      
         (while (<= (+  theInitPosY inBaseTileSize) theNewHeight)
            (set! i 0)
            (set! theInitPosX 0)      
            (while (<= (+ theInitPosX inBaseTileSize) theNewWidth)
               (gimp-rect-select theImage theInitPosX theInitPosY inBaseTileSize inBaseTileSize 2 FALSE 0)               
               (gimp-edit-copy theDrawable)   
               (set! theImage2 (car (gimp-image-new inBaseTileSize inBaseTileSize theImageType)))
               (set! theLayer (car (gimp-layer-new theImage2 inBaseTileSize inBaseTileSize theImageType "layer1" 100 0)))
               (set! theFloat (car (gimp-edit-paste theLayer 0)))
               (gimp-floating-sel-anchor theFloat)            
               (set! theTileName (string-append "tx_" (number->string (+ i inFirstX)) "_" (number->string (+ j inFirstY))))
                     
               ;ugly workaround to avoid gimp-file-save internal bug:
               (cond
                  ((= inSaveFormat 0)
                     (set! theFileName (string-append inPath "/" theTileName ".tga"))
                     (file-tga-save 1 theImage2 theLayer theFileName theFileName 0 0))                                                
                  ((= inSaveFormat 1)
                     (set! theFileName (string-append inPath "/" theTileName ".jpg"))
                     (file-jpeg-save 1 theImage2 theLayer theFileName theFileName 0.75 0 0 0 "" 0 0 0 0))               
                  ((= inSaveFormat 2)
                     (set! theFileName (string-append inPath "/" theTileName ".png"))
                     (file-png-save 1 theImage2 theLayer theFileName theFileName 0 0 0 0 0 0 0))   
               ) ;cond                     

               (gimp-image-delete theImage2)               
               (set! i (+ i 1))      
               (set! theInitPosX (* inBaseTileSize i))         
            ) ;inner while
         (set! j (+ j 1))
         (set! theInitPosY (* inBaseTileSize j))
         ) ;outer while   
         (gimp-message (string-append "Level " (number->string inLevel) " succesfully completed"))   
      )  ;begin;
      (begin
         ;this should be an 'if' but this way is shorter to write:
         (cond
            ((> inBaseTileSize theImageWidth ) (gimp-message "'Size of each tile' parameter too high for your texture"))
            ((<= inBaseTileSize theImageWidth ) (gimp-message "'Level to split...' parameter too high for your texture"))
         ) ;cond
      )  ;begin      
      )  ;if
(gimp-selection-none theImage) 
(gimp-image-clean-all theImage)
(gimp-image-undo-enable theImage)
(gimp-image-delete theImage)   
)
      
(script-fu-register
   "script-fu-map-create-vt"
   _"<Toolbox>/Xtns/Script-Fu/Celestia Scripts/Map Create VT"   
   "creates a specified level of Celestia VT from a source image"
   "Toti. Based on Dr. Fridger Schrempp's Virtualtex functionality"
   "2004"
   "March 2 2004"
   "RGB* GRAY* INDEXED*"
SF-FILENAME _"Image file" ""     
SF-FILENAME _"Path to save tiles into" ""
SF-VALUE _"Level to split texture into" "0"
SF-VALUE _"Size of each tile (powers of two)" "1024"
SF-VALUE _"First X" "0"
SF-VALUE _"First Y" "0"
SF-VALUE _"Original tile level" "0"
SF-OPTION _"Working Hemisphere" '(_"Whole map/West" _"East")
SF-OPTION _"Format for saved tiles" '(_"tga" _"jpg" _"png")

)


    1)Copy and paste it in your text editor. Then save it as any_name.scm in any of your GIMP <scripts> folders.
    2)Create your VT folder. Example:
      celestia\textures\hires\earth\level0
      celestia\textures\hires\earth\level1
      celestia\textures\hires\earth\level2
      ...
    (sorry, I tried to implement this in the script, but the limited SIOD Scheme version that comes with the GIMP does not include directory creation routines. For the same reason, if the script doesn't find the correct directory, it will fail)
    3)Run the Gimp

    4)For each tile level that you want to produce do the following:
      a)Go to Xtns -> Script-Fu -> Celestia Scripts -> Map Create VT
      b)In the window that appears, enter:
        * the source image file.
        * the base directory where your produced tiles will be saved. In the above example celestia\textures\hires\earth. Important: check that the directory displayed in this field really is the one you selected.
        * the tile level that you want to produce.
        * the tile size that you have chosen for your VT (must be a power of 2)
        * The original tile offset X index, if you want to create ultra high resolution tiles from/inside individual existing tiles; otherwise leave it as 0.
        * Same for the original tile offset Y index
        * Same for the original tile level
        * The hemisphere that corresponds with your original image, If you downloaded it already splitted in two hemispheres (west and east). If not, leave it as it is.
        * the graphic format that you want the tiles to be in (loseless tga is recommended if you plan to convert them to DXT format)
      c)The process can take a lot of time, depending on your original texture size, the tile level selected and your system specs.
      d)When finished, a message will appear. Your tiles will be saved in the corresponding directory structure.

If you create more than a level in the same session (ie. without closing the GIMP) some of the above parameters will remain in memory, so you will only need to enter the tile level desired.

Remember that GIMP in not the best tool to do this kind of job. This script is only implemented as a resource for certain particular cases, so if GIMP can't manage some really huge textures, you will need to learn a tiny bit of Unix environments and run ImageMagick and Virtualtex.

Please post bugs, improvement ideas, etc. here, in this thread.

Hope this helps. I know this time at least Ziggy will use it ;)
Bye

Edit: ordered the post

Posted: 23.02.2004, 16:46
by Bob Hegwood
Hey thanks Toti... :lol:

Thanks for the script. I'll be playing with it later tonight.

Now that I know how I'm supposed to create VT's maybe I can figure out how I'm NOT supposed to create them too. :wink:

Appreciate your hard work...

Take care, Bob

Re: A VT creation tool for the GIMP

Posted: 28.02.2004, 22:47
by Ptarmigan
Toti wrote:If you do not want to mess with Cygwin, console commands, etc. and the GIMP is installed in your system, you can use the script below:
Snip
Very fine script Toti, thank you lots!
It worked beautifully,

I had previously tried a couple of Gimp scripts that I had found on the web to split images into tiles but neither of them had worked.
So it was a great pleasure to to see yours 'in action' on my humble WinME/Gimp1.2.3 system.

Thanks,
Malcolm.

Re: A VT creation tool for the GIMP

Posted: 29.02.2004, 05:23
by Toti
Ptarmigan wrote:It worked beautifully


I am glad it worked fine :D

Bye

Posted: 29.02.2004, 14:58
by Bob Hegwood
Toti,

Got the script to work just fine now. See my message HERE for more information.

Thanks, Bob

Posted: 12.03.2004, 03:17
by Toti
Hello all:

It seems that there is generalized trouble with the output directory specification feature.
Let's assume that your tiles are from earth, so they must be saved into c:\...\textures\hires\earth
Inside c:\...\textures\hires you must have created the following folders:

earth\level0
earth\level1
earth\level2
earth\level3
earth\level4
earth\level5
...

If you didn't, the script will fail. (remember that there is no folder creation routines support under Gimp's mini SIOD Scheme)

Using the graphical interfase (accessed with the [...] button), navigate through the directories until you are inside hires\
Now, under the 'directories' list you'll have this entries (and may be others):

Code: Select all

.\
..\
earth\
C:\


Select earth\ (highlight it) but do not enter in it. Hit OK. You will return to the script main window.
If you did it exactly as above, in the 'Path to save tiles into' line will say c:\...\textures\hires\earth (in sum, the root folder for all your tile levels)
If it says c:\...\textures\hires\earth\level0 or something else, you did it the wrong way, and must delete the extra folders (in this case \level0).
Forget about the little red cross. It is only there because you have not selected an actual file, but a folder.

The ironical thing is that I coded it this way in order to make things easier, but if instead of this (rather troublesome) folder selection method you'd prefer a simple line where you type the folder path, please tell me. I will post another script version with the change.

Bye :)

Posted: 12.03.2004, 05:12
by Bob Hegwood
Toti,

Just so you know... I haven't had *any* trouble with the script after I read your instructions thoroughly. Works fine at my end. <shrug>

Take care, Bob