def populate_from_dmd_file(self, f, composite_op=None): f.seek(0, os.SEEK_END) # Go to the end of the file to get its length file_length = f.tell() f.seek(0) # Skip back to the 4 byte DMD header. dmd_version = struct.unpack("I", f.read(4))[0] dmd_style = 0 # old if (dmd_version == 0x00646D64): # print("old dmd style") pass elif (dmd_version == 0x00DEFACE): # print("full color dmd style") dmd_style = 1 frame_count = struct.unpack("I", f.read(4))[0] self.width = struct.unpack("I", f.read(4))[0] self.height = struct.unpack("I", f.read(4))[0] if (dmd_style == 0): if file_length != 16 + self.width * self.height * frame_count: logging.getLogger('game.dmdcache').warning(f) logging.getLogger('game.dmdcache').warning( "expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length)) raise ValueError, "File size inconsistent with original DMD format header information. Old or incompatible file format?" elif (dmd_style == 1): if file_length != 16 + self.width * self.height * frame_count * 3: logging.getLogger('game.dmdcache').warning(f) raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?" for frame_index in range(frame_count): new_frame = Frame(self.width, self.height) if (dmd_style == 0): str_frame = f.read(self.width * self.height) new_frame.build_surface_from_8bit_dmd_string( str_frame, composite_op) elif (dmd_style == 1): str_frame = f.read(self.width * self.height * 3) new_frame.pySurface = sdl2_DisplayManager.inst( ).make_texture_from_imagebits(bits=str_frame, width=self.width, height=self.height, composite_op=composite_op) if (frame_index == 1): new_frame.font_dots = str_frame[0:97] self.frames.append(new_frame)
def populate_from_dmd_file(self, f, composite_op = None): f.seek(0, os.SEEK_END) # Go to the end of the file to get its length file_length = f.tell() ## MJO: Don't just skip the header! Check it. f.seek(0) # Skip over the 4 byte DMD header. dmd_version = struct.unpack("I", f.read(4))[0] dmd_style = 0 # old if(dmd_version == 0x00646D64): # print("old dmd style") pass elif(dmd_version == 0x00DEFACE): # print("full color dmd style") dmd_style = 1 frame_count = struct.unpack("I", f.read(4))[0] self.width = struct.unpack("I", f.read(4))[0] self.height = struct.unpack("I", f.read(4))[0] if(dmd_style==0): if file_length != 16 + self.width * self.height * frame_count: logging.getLogger('game.dmdcache').warning(f) logging.getLogger('game.dmdcache').warning("expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length)) raise ValueError, "File size inconsistent with original DMD format header information. Old or incompatible file format?" elif(dmd_style==1): if file_length != 16 + self.width * self.height * frame_count * 3: logging.getLogger('game.dmdcache').warning(f) raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?" for frame_index in range(frame_count): new_frame = Frame(self.width, self.height) if(dmd_style==0): str_frame = f.read(self.width * self.height) new_frame.build_surface_from_8bit_dmd_string(str_frame, composite_op) elif(dmd_style==1): # print("LOADING FRAME...") str_frame = f.read(self.width * self.height * 3) # surface = pygame.image.fromstring(str_frame, (self.width, self.height), 'RGB') # new_frame.set_surface(surface) # https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom?? new_frame.pySurface = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=str_frame, width=self.width, height=self.height, composite_op=composite_op) # print("made texture for this frame -- contents: " + str(new_frame.pySurface)) if(frame_index==1): new_frame.font_dots = str_frame[0:97] self.frames.append(new_frame)