extrad.pl

#!/usr/bin/perl -w

# This script takes RADIANCE .rad files containing the whole scene
# as created by DesignWorkshop or SiVIEW and creates a seperate file
# for each material.
#
# The -v switch turns on verbosity for debugging.
# The -h and --help switches print the usage. It also comes up when
# no argument is passed on the command line.
#
# The only argument required is the name of the input file.
#
# (c) Axel Jacobs
# Written 16th October 1999
# Revised 20 July 2006

use strict;

my $HASH_COUNT = 10;
my $hash = 1;

my $verbose = 0;

my $now;
my $total_blocks = 0;
my $total_files = 0;

my $file_in;
my $last_file;
my %files;

# Print out usage instructions if no arguments.
if ($#ARGV == "-1") {
	usage();
}

sub usage {
    print <>$file_name") or
				die ("Cannot append to output file: $file_name. $!");
		if ( "$file_name" eq "$last_file" ) {
			if ( $hash == $HASH_COUNT ) {
				print "#" if $verbose;
					# Autoflush the output buffer
				$| = 1;
				$hash = 1;
			} else {
				++$hash;
			}
		} else {
			print "\nAppending to file: $file_name\n" if $verbose;
			$hash = 1;
		}
    } else {
		open (FH, ">$file_name") or
				die ("Cannot open or create output file $file_name. $!");
		$now = gmtime;
		print FH < ) {
    # Don't copy any blank or comment lines
    if ( (m/^\s*$/) || (m/^#/) ) {
		next;
	}

    if ( ! $count ) {
		# Handle the first line of each section

		m/^\s*(\S+)/;
		# Get the first argument
		$FH_OUT = set_output_file( $1 );
		$count = 1;
    } else {
		# Handle all subsequent lines of the section

		$nof_args = args_per_line( $_ );
		if ( ! $continue ) {
			m/^\s*(\S+)/;
			$no_indicated = $1;
			if ( $no_indicated > $nof_args ) {
				# Arguments are split across several lines
				$continue = 1;
				$no_so_far = --$nof_args;
			} else {
				# All arguments are on the same line
				$count++;
			}
		} else {
			$no_so_far += $nof_args;
			if ( $no_indicated == $no_so_far ) {
				$continue = 0;
				$count++;
			}
		}
    }
    print $FH_OUT "$_";
    if ( $count == 4 ) {
		print $FH_OUT "\n";
		$count = 0;
		++$total_blocks;
    }
}

# Clean up.
close ( FH_IN );

# Write out some statistics
print "\n$total_blocks blocks written to $total_files files.\n";

# EOF