csh script for generating clipmap data files

New Message Reply Date view Thread view Subject view Author view

Dr Colin Bridgewater (crccobr++at++nocrc.abb.no)
Wed, 05 Nov 1997 15:59:34 +0100


Hi folks

Freebie time - wrote this script a while back when starting to play with
clip maps (got fed up using subimg upmteen times on a large image). Let
me know if anyone finds it useful and I may mail some other scripts as
well.

Best wishes

Colin
_______________________________________________________________________
Dr Colin Bridgewater crccobr++at++nocrc.abb.no Marine Oil and Gas Dept
ABB Teknologi AS, Bergerveien 12, PO Box 91, N-1361 BILLINGSTAD, Norway
work tel: +47 66 84 35 36, fax: +47 66 84 35 41, home: +47 32 81 71 70

#!/bin/csh -f
#
# mktiles Colin Bridgewater 05.11.97
# ABB Teknologi AS, Norway
#
# usage: mktiles <input file> [x tile size [y tile size]]
#
# Description
# Script to take a large image and generate lots of sub-tiles at
# texture-memory-friendly sizes. Used for generating clip-maps.
#
# Because the sub-tiles have sides which are some power of 2 in
# length (eg 128x128 or 64x256), the tiles may not make up the
# image size exactly. In this case, the script warps the input
# image to ensure that the side lengths will fit the tiles. The
# warped image is named accordingly.
#
# Assumes a default tile size of 128 x 128 unless told otherwise.
# Checks that subimg, izoom and imginfo are installed.
#
# Problems (challenges?)
# 1. Does not check that the tile sizes are numbers....
# 2. Only outputs SGI RGB format image files.
#
# Enhancements
# 1. Use getopts to extract the arguments (re-write for sh).
# 2. User-definable output file name (prefix for tiles).
# 3. User-definable output file type (tif vs rgb etc).
#
# ---------------------------------------------------------------------
#
# if no file specified, quit with rude error message

if ("$1" == "" | "$1" == "-h") then
        if ("$1" == "") then
                echo "Error: no argument(s) supplied, aborting"
                echo ""
        endif
        echo "Usage is: mktiles <input file> [xtilesize [ytilesize]]"
        echo " mktiles -h"
        exit 0
endif

# if imginfo or izoom don't exist, then there are problems

if (-s /usr/sbin/subimg & -s /usr/sbin/imginfo & -s /usr/sbin/izoom) then
        ;
else
        echo "Error: subimg, imginfo or izoom not available, aborting."
        echo " Check permissions of /usr/sbin and re-install"
        echo " software product subsytem: imgtools.sw.tools"
        exit 0
endif

# set the default tile size to 128 x 128

++at++ defsize = 128

# set the file name from which sub-images are to be taken
# necessary for when the warped image is to be used

set infile = $1

# if no tile sizes specified, assume defaults
# else if x tile size is specified, set y tile size to match
# else both sizes are specified on the command line

if ("$2" == "") then
        echo "No tile sizes specified, using $defsize x $defsize"
        ++at++ xtsize = $defsize
        ++at++ ytsize = $defsize
else
        ++at++ xtsize = $2
        if ("$3" == "") then
                ++at++ ytsize = $xtsize
                echo "No y tile size specified, using $xtsize"
        else
                ++at++ ytsize = $3
        endif
endif

# Get the image size from the image file

++at++ xsize = `imginfo $infile | grep Dimensions | cut -f2 -d : | cut -f1 -d ,`
++at++ ysize = `imginfo $infile | grep Dimensions | cut -f2 -d : | cut -f2 -d ,`

echo ""
echo "Size of image in $infile is $xsize by $ysize pixels"

# calculate the number of tiles to be made
# round the results (up or down) to the nearest tile size

++at++ xn = ( 2 * $xsize + $xtsize ) / ( 2 * $xtsize )
++at++ yn = ( 2 * $ysize + $ytsize ) / ( 2 * $ytsize )

++at++ xs = $xn * $xtsize
++at++ ys = $yn * $ytsize

echo " - will make $xn columns by $yn rows of tiles"
echo " - final image size will be $xs by $ys pixels"

# check that the tiles give exact coverage of the input image
# if they do not, warp the input image accordingly before
# extracting the sub-images
# nawk command string courtesy of Greg Edwards, SGI UK

if ($xs != $xsize | $ys != $ysize) then

        # print a warning
        echo ""
        echo "Final tiled image is of different size to input image"
        echo " - warping input image to suit tile sizes"

        # warp the image using izoom and nawk
        set warpfile = "bigtile.rgb"
        set nawkcom = `nawk 'BEGIN { xzoom = '$xs' / '$xsize' ; yzoom = '$ys' / '$ysize' ; print "izoom", "'$infile'", "'$warpfile'", xzoom, yzoom } { }' /dev/null `
        echo -n " "
        echo $nawkcom

        $nawkcom
        echo " - done"
        set infile = "$warpfile"

        # print some file information
        echo ""
        echo "File Information"
        echo " - warped image name is $infile"
else
        echo ""
        echo "File Information"
endif

# tell the user what the file name means

++at++ xn -= 1
++at++ yn -= 1

echo " - file names will be tile.[x:0..$xn].[y:0..$yn].rgb"

# set up the mask variables for taking the sub-images

++at++ x0 = 0
++at++ x1 = $xtsize - 1
++at++ y0 = 0
++at++ y1 = $ytsize - 1

# set the output file name to tile.a.b.rgb

++at++ a = 0
++at++ b = 0

# use two nested while loops to chop up the image into lots of smaller
# ones. Have to do some mucking around with the counters to make sure
# that the tile sizes are correct....

echo
echo "Making the tiles now"
echo -n " "

while ($x1 <= $xsize)

        while ($y1 <= $ysize)
                # make the sub-image from the input file
                subimg $infile tile.$a.$b.rgb $x0 $x1 $y0 $y1

                # increment the image sizes in the y sense
                ++at++ y0 = $y1 + 1
                ++at++ y1 += $ytsize
                ++at++ b += 1
                echo -n "."
        end

        # reset the image sizes in the x and y senses
        ++at++ x0 = $x1 + 1
        ++at++ x1 += $xtsize
        ++at++ y0 = 0
        ++at++ y1 = $ytsize - 1

        # reset the counters
        ++at++ a += 1
        ++at++ b = 0

        echo ""
        echo -n " "
end

# Tell the user we have finished

echo ""
echo Done

# ---- End of File ----

=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
            Submissions: info-performer++at++sgi.com
        Admin. requests: info-performer-request++at++sgi.com


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:56:12 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.