Skip to content

Instantly share code, notes, and snippets.

@bbbradsmith
Created July 16, 2018 06:01
Show Gist options
  • Save bbbradsmith/1477fea00b8b19d4a4812accdf34a5d0 to your computer and use it in GitHub Desktop.
Save bbbradsmith/1477fea00b8b19d4a4812accdf34a5d0 to your computer and use it in GitHub Desktop.
Microsoft screensaver sample A8 to TGA decoder
# decodes the A8 files in
# Microsoft Windows NT 4.0 SDK (1996)
# MSTOOLS\SAMPLES\OPENGL\SCRSAVE\MAZE
#
# based on MSTOOLS\SAMPLES\OPENGL\SCRSAVE\COMMON\SSA8.C
import struct
def a8decompress(d):
# defines
ESCAPE = 0
ESC_ENDLINE = 0
ESC_ENDBITMAP = 1
ESC_DELTA = 2
ESC_RANDOM = 3
def RANDOM_COUNT(c):
return c - (ESC_RANDOM-1)
# HwuRld
s = 0
o = []
while True:
run = d[s+0]
esc = d[s+1]
s += 2
if run == ESCAPE:
if esc == ESC_ENDBITMAP:
break
elif esc == ESC_DELTA:
ln = struct.unpack("<H",d[s:s+2])[0]
s += 2
while ln > 0:
o.append(0)
ln -= 1
elif esc >= ESC_RANDOM:
ln = RANDOM_COUNT(esc)
while ln > 0:
o.append(d[s])
s += 1
ln -= 1
else:
while run > 0:
o.append(esc)
run -= 1
return o
def a8_to_tga(filea8,filetga=None):
if filetga == None:
filetga = filea8 + ".TGA"
filea8 = filea8 + ".A8"
a8 = open(filea8,"rb").read()
(a8tag,w,h,a8bpp,a8compress,a8compsize) = struct.unpack("<LLLLLL",a8[0:24])
a8pal = a8[24:24+(256*4) ]
a8pix = a8[ 24+(256*4):]
tga = bytearray()
tga.append(0) # idlength
tga.append(0) # colourmap = none
tga.append(2) # datatypecode = RGB
tga += bytes([0,0,0,0,0]) # colourmap specification ignored
tga += bytes(struct.pack("<H",0)) # x origin
tga += bytes(struct.pack("<H",0)) # y origin
tga += bytes(struct.pack("<H",w)) # width
tga += bytes(struct.pack("<H",h)) # height
tga.append(32) # bits per pixel
tga.append(0x08) # image descriptor: 8 bit alpha, lower left origin
pal = []
for i in range(256):
pal.append(a8pal[i*4:i*4+4])
if (a8compress != 0):
a8pix = a8decompress(a8pix)
for i in range(w*h):
a8idx = a8pix[i]
bgra = pal[a8idx]
tga.append(bgra[0]) # B
tga.append(bgra[1]) # G
tga.append(bgra[2]) # R
tga.append(bgra[3]) # A
open(filetga,"wb").write(tga)
print (filea8 + " > " + filetga + " (%d x %d)" % (w,h))
a8_to_tga("BHOLE")
a8_to_tga("CURL4")
a8_to_tga("HAPPY")
a8_to_tga("LETTERS")
a8_to_tga("RAT")
a8_to_tga("SNOWFLAK")
a8_to_tga("START")
a8_to_tga("SWIRLX4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment