Пример #1
0
def read_madspack(madspack_name):
    """
	f -- input stream
	"""

    f = open2(madspack_name, 'rb')

    magic = f.read(12).decode('ascii')
    if magic != 'MADSPACK 2.0':
        fail(
            "invalid madspack version; expected=MADSPACK 2.0; got={}; file={}",
            magic, madspack_name)

    f.seek(14)
    _count = read_uint16(f)  # num of parts

    header = io.BytesIO(f.read(0xA0))  # max 16 parts
    header.seek(0)

    parts = []
    for i in range(_count):
        flag = read_uint16(header)
        size = read_uint32(header)
        compressed_size = read_uint32(header)

        if (flag & 1) == 0:
            assert compressed_size == size
            ## no compression on this entry
            data = BytesIO(f.read(size))

        elif (flag & 1) == 1:
            if compressed_size == size:
                warning(
                    "madspack decoder: flag indicate fab compression but csize equals usize in: {}",
                    madspack_name)
            ## fab compressed
            data = read_fab(f, size)

        else:
            raise Error("madspack unknown mode = {}".format(mode))

        parts.append(data)

    return parts
Пример #2
0
def read_madspack(madspack_name):
	"""
	f -- input stream
	"""
	
	f = open2(madspack_name, 'rb')
	
	magic = f.read(12).decode('ascii')
	assert magic == 'MADSPACK 2.0', magic
	
	
	
	
	f.seek(14)
	_count = read_uint16(f)    # num of parts
	
	header = io.BytesIO(f.read(0xA0))   # max 16 parts
	header.seek(0)
	
	parts = []
	for i in range(_count):
		flag = read_uint16(header)
		size = read_uint32(header)
		compressed_size = read_uint32(header)
		
		if (flag & 1) == 0:
			assert compressed_size == size
			## no compression on this entry
			data = BytesIO(f.read(size))
			
		elif (flag & 1) == 1:
			assert compressed_size != size
			## fab compressed
			data = read_fab(f, size)
			
		else:
			raise Error("madspack unknown mode = {}".format(mode))
						
		parts.append(data)
	
	return parts
Пример #3
0
def read_sprite(ti, rdata, pal, mode, verbose=0):
	"""
	ti -- sprite header
	rdata -- file-like object
	pal -- array of (R,G,B)
	"""
	
	rdata.seek(ti.start_offset)
	
	if mode == 0:
		data = rdata.read(ti.length)
	
	elif mode == 1:
		data = read_fab(rdata, ti.length).read()
		
	else:
		raise Error('invalid sprite mode: {}'.format(mode))
	
	
	
	# special case
	if ti.width == 0 or ti.height == 0:
		if data[0] == 252:		
			img = Image.new("RGBA", (1,1))
			img.putpixel((0,0), (0,0,0,0))
			return img
		else:
			raise Error('invalid encoding of 0x0 image while reading SS file')
			

	img = Image.new('RGBA', (ti.width, ti.height), 'black')
	pix = img.load()
	
	if verbose:
		print("sprite size =", ti.width, ti.height)
	
	i = j = 0
	k = 0
	bg = 0xFD
	
	def write_ind(ci, l=1):
		if ci == bg:
			
			c = (0,0,0,0)
			#c = pal[ci] + (0,)
		else:
			c = pal[ci] + (255,)
			
		nonlocal i
		while l > 0:
			if verbose:
				print(i,j,c)
			pix[i,j] = c
			i += 1
			l -= 1
	
	def nextline():
		nonlocal i,j
		i = 0
		j += 1
	
	def read():
		nonlocal k
		r = data[k]
		k += 1
		return r
	
	
	def read_lm():
		x = read()
		if verbose:
			print('lm=',x)
		return x
			
			
	lm = read_lm()
	
			
	while 1:   #j < ti.height:  #k < ti.length:
		
		# line mode
		if lm == 255:
			# fill with bg color to the end of this line
			write_ind(bg, ti.width - i)
			nextline()
			lm = read_lm()
			
			
		elif lm == 252:
			# end of image
			break
			
		else:
			x = read()
			if x == 255:
				write_ind(bg, ti.width - i)			
				nextline()
				lm = read_lm()			
		
			else:
				if lm == 254:
					# pix			
					if x == 254:
						l = read()
						ci = read()
						write_ind(ci, l)
					
					else:
						write_ind(x)
					
				elif lm == 253: 
					ci = read()
					write_ind(ci, x)
				
				else:
					print('ERROR: unknown lm:', lm)
					assert 0
	
	
	return img