SHARE
TWEET

sprite2c.pl

a guest Oct 31st, 2010 436 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. # sprite2c.pl by Travis Goodspeed
  4.  
  5. # This is an ugly hack for converting PNM sprites to C.  I'd rather
  6. # use the GIMP's exporter, but it prefers 24-bit color, which can't be
  7. # afforded for embedded applications.
  8.  
  9. # N.B., that this expects some GIMP-specific crap.
  10.  
  11.  
  12. print "// ZombieGotcha Sprite Translator \n\n";
  13.  
  14. my $pnmtype, $line;
  15.  
  16.  
  17. #First, verify the filetype
  18. $pnmtype=<>;
  19. print STDERR "ERROR: Type is not P3.  Use ASCII GIMP PNM in RGB!." if $pnmtype ne "P3\n";
  20.  
  21. #    P1 Portable bitmap ASCII
  22. #    P2 Portable graymap ASCII
  23. #    P3 Portable pixmap ASCII
  24. #    P4 Portable bitmap Binary
  25. #    P5 Portable graymap Binary
  26. #    P6 Portable pixmap Binary
  27.  
  28. #Next, find the file dimensions.  Comments should be printed in C.
  29. do{
  30.     $line=<>;
  31.     chomp $line;
  32.     print STDOUT "  // $line\n" if $line=~m/^#/;
  33. }while($line=~m/^#/);
  34.  
  35. #$line still has the dimensions, parse it.
  36. $line=~ s/ /,/;
  37. print "  $line, //Width, Height\n";
  38.  
  39. $max=<>;
  40. chomp $max;
  41. print "  //Max $max\n\n";
  42.  
  43. #Now handle all the pixels in B&W.
  44. my $atbit=0;
  45. my $byte=5;
  46. print "  //Pixels\n";
  47. while (<STDIN>){
  48.     chomp;
  49.     $bit=int($_)?0:1;
  50.     $byte=$byte>>1;;
  51.     $byte=$byte|($bit?0x80:0);
  52.    
  53.     #Waste two bits.
  54.     <STDIN>;
  55.     <STDIN>;
  56.    
  57.     #Advance the bit counter and print byte if needed.
  58.     $atbit++;
  59.     if ($atbit%8 ==0) {
  60.         $byte&=0xFF;
  61.         printf("  0x%02x,",$byte);
  62.         print "\n" if $atbit%192==0;
  63.     }
  64. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top