Advertisement
Guest User

a8_to_tga

a guest
Jul 15th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # decodes the A8 files in
  2. # Microsoft Windows NT 4.0 SDK (1996)
  3. # MSTOOLS\SAMPLES\OPENGL\SCRSAVE\MAZE
  4.  
  5. import struct
  6.  
  7. def a8decompress(d):
  8.     # defines
  9.     ESCAPE = 0
  10.     ESC_ENDLINE = 0
  11.     ESC_ENDBITMAP = 1
  12.     ESC_DELTA = 2
  13.     ESC_RANDOM = 3
  14.     def RANDOM_COUNT(c):
  15.         return c - (ESC_RANDOM-1)
  16.     # HwuRld
  17.     s = 0
  18.     o = []
  19.     while True:
  20.         run = d[s+0]
  21.         esc = d[s+1]
  22.         s += 2
  23.         if run == ESCAPE:
  24.             if esc == ESC_ENDBITMAP:
  25.                 break
  26.             elif esc == ESC_DELTA:
  27.                 ln = struct.unpack(">H",d[s:s+2])[0]
  28.                 s += 2
  29.                 while ln > 0:
  30.                     o.append(0)
  31.                     ln -= 1
  32.             elif esc >= ESC_RANDOM:
  33.                 ln = RANDOM_COUNT(esc)
  34.                 while ln > 0:
  35.                     o.append(d[s])
  36.                     s += 1
  37.                     ln -= 1
  38.         else:
  39.             while run > 0:
  40.                 o.append(esc)
  41.                 run -= 1
  42.     return o
  43.  
  44. def a8_to_tga(filea8,filetga=None):
  45.     if filetga == None:
  46.         filetga = filea8 + ".TGA"
  47.         filea8 = filea8 + ".A8"
  48.     a8 = open(filea8,"rb").read()
  49.     (a8tag,w,h,a8bpp,a8compress,a8compsize) = struct.unpack("<LLLLLL",a8[0:24])
  50.     a8pal = a8[24:24+(256*4) ]
  51.     a8pix = a8[   24+(256*4):]
  52.     tga = bytearray()
  53.     tga.append(0) # idlength
  54.     tga.append(0) # colourmap = none
  55.     tga.append(2) # datatypecode = RGB
  56.     tga += bytes([0,0,0,0,0]) # colourmap specification ignored
  57.     tga += bytes(struct.pack("<H",0)) # x origin
  58.     tga += bytes(struct.pack("<H",0)) # y origin
  59.     tga += bytes(struct.pack("<H",w)) # width
  60.     tga += bytes(struct.pack("<H",h)) # height
  61.     tga.append(32) # bits per pixel
  62.     tga.append(0x18) # image descriptor: 8 bit alpha, top left origin
  63.     pal = []
  64.     for i in range(256):
  65.         pal.append(a8pal[i*4:i*4+4])
  66.     if (a8compress != 0):
  67.         a8pix = a8decompress(a8pix)
  68.     for i in range(w*h):
  69.         a8idx = a8pix[i]
  70.         rgba = pal[a8idx]
  71.         tga.append(rgba[2]) # B
  72.         tga.append(rgba[1]) # G
  73.         tga.append(rgba[0]) # R
  74.         tga.append(rgba[3]) # A
  75.     open(filetga,"wb").write(tga)
  76.     print (filea8 + " > " + filetga + " (%d x %d)" % (w,h))
  77.  
  78. a8_to_tga("BHOLE")
  79. a8_to_tga("CURL4")
  80. a8_to_tga("HAPPY")
  81. a8_to_tga("LETTERS")
  82. a8_to_tga("RAT")
  83. a8_to_tga("SNOWFLAK")
  84. a8_to_tga("START")
  85. a8_to_tga("SWIRLX4")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement