3 # prw2ppm - convert Artlantis Preview files to PPM
 
   5 # Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
 
   7 # This program is free software: you can redistribute it and/or modify
 
   8 # it under the terms of the GNU General Public License as published by
 
   9 # the Free Software Foundation, either version 3 of the License, or
 
  10 # (at your option) any later version.
 
  12 # This program is distributed in the hope that it will be useful,
 
  13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15 # GNU General Public License for more details.
 
  17 # You should have received a copy of the GNU General Public License
 
  18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
  28     length = struct.calcsize(fmt)
 
  30     value = struct.unpack_from(fmt, data)
 
  36     r = (data & 0x00FF0000) >> 16
 
  37     g = (data & 0x0000FF00) >> 8
 
  38     b = (data & 0x000000FF)
 
  44     sys.stdout.write("usage: %s <prw file>\n" % name)
 
  47 def prw2ppm(prw_filename, ppm_filename):
 
  48     f = open(prw_filename, "rb")
 
  50     # file type or frame number,
 
  51     # or maybe the type of the first packet? (run-length or raw)
 
  52     file_type = get_be32(f)
 
  54         sys.stdderr.write("Unknown preview file type.\n")
 
  60     outfile = open(ppm_filename, "w")
 
  63     outfile.write("%d %d\n" % (width, height))
 
  64     outfile.write("%d\n" % 255)
 
  67     n_pixels = width * height
 
  69     # Read the first packet here,
 
  70     # AFAIK it is always a run-length packet
 
  78             for i in range(0, count):
 
  79                 outfile.write("%d %d %d\n" % to_rgb(data))
 
  82             outfile.write("%d %d %d\n" % to_rgb(data))
 
  83             for i in range(0, count - 1):
 
  85                 outfile.write("%d %d %d\n" % to_rgb(data))
 
 100 if __name__ == "__main__":
 
 102     if len(sys.argv) < 2:
 
 106     prw_filename = sys.argv[1]
 
 108     basename_no_ext = os.path.splitext(prw_filename)[0]
 
 109     ppm_filename = basename_no_ext + ".ppm"
 
 111     prw2ppm(prw_filename, ppm_filename)