#!/usr/bin/env perl # # Updated : Thu Mar 29, 2012 # Script : cubeart.pl # Version : 0.4 # Author : unixfreak # Licence : GPLv3 [ http://www.gnu.org/licenses/gpl.txt ] # # Converts an image, into a *.cfg file which is compatible with # cube-art (by "SomeDude") for Cube2: Sauerbraten. I Re-created # the converter so that i could run it on linux/bsd/etc # # All standard/common image formats should work; # BMP, PNG, JPG/JPEG, TIFF ...etc # # # Make sure imagemagick is installed before trying to run it. # # You can find SomeDude's cubeart mod here: # http://mmc.binarybuddies.net/forum/viewtopic.php?f=8&t=19 # # thanks goes to... # SomeDude for creating the awesome cube-art script/program/mod. # B0ng|PWN3R|G3R for reminding me about cube-art # # Have fun! # # # Changelog ----------------------------------------------------- # # 29/03/12 (0.4) - save output file in the current dir by default # - can now set output.cfg as second argument # # 03/09/11 (0.3) - don't print to stdout, print to file instead # # 18/08/11 (0.2) - drawalpha set to 0 as default (not working, is alpha 0-255?) # - fixed problem where height of image increased by one # # 17/08/11 (0.1) - first release # # use strict; use warnings; use POSIX; use Image::Magick; my $version = "0.4"; my $file = $ARGV[0]; my $output = $ARGV[1] || "output.cfg"; # make sure filename is given + that it exists if ( ! $file || ! -e $file ) { print "\n\tunixfreak's cube-art image converter (v$version)\n"; print "\n\tusage\t: $0 image.png image.cfg\n\n"; exit 2; } # read the filename's data into '$image' my $image = new Image::Magick; $image->Read($file); sub getPixelData { # pixel co-ord's from image my ($x,$y) = @_; # returns "R G B A" value for specified pixel as 16-bit data # ex; ( White = 65535 65535 65535 0) my @data = split( /,/, $image -> Get("Pixel[$x,$y]") ); # loop each value and round-down to 8-bit # ex; ( White = 255 255 255 0) my $i = 0; foreach my $val (@data) { $data[$i] = floor($val/256); ++$i; } return "@data"; } package main; my $width = $image->Get('columns'); my $height = $image->Get('rows'); warn "Warning: image is larger than 75 pixels wide\n" if ($width > 75); warn "Warning: image is larger than 75 pixels high\n" if ($height > 75); my ($wpos,$hpos) = (0,0); open FILE, ">", $output or die $!; # start generating the cubescript config print FILE "echo \"Converted w/ unixfreak's cube-art image converter (v$version)\"\n"; print FILE "//Base image: $file\n"; print FILE "imgwidth = $width\n"; print FILE "drawalpha = 0\n"; print FILE "imgdata = [ "; row: do { # loop through pixels on each row of the image print FILE &getPixelData($wpos,$hpos) . " "; ++$wpos; } until $wpos == $width; $wpos = 0; ++$hpos; # if we havent reached the last line of the image, do the next row if ( $hpos < ($height) ) { goto row; } # append a closing bracket print FILE "]\n"; # cfg file should be availabble at ($output . cfg) close(FILE); print "Config file saved to ". $ENV{PWD} ."/". $output ."\n"; 1;