def decode_animation(self, file, filename): # If file is not an animated GIF, it will be loaded as a single-frame animation. file_bytes = file.read() data = c_void_p(cf.CFDataCreate(None, file_bytes, len(file_bytes))) sourceRef = c_void_p(quartz.CGImageSourceCreateWithData(data, None)) # Get number of frames in the animation. count = quartz.CGImageSourceGetCount(sourceRef) frames = [] for index in range(count): # Try to determine frame duration from GIF properties dictionary. duration = 0.1 # default duration if none found props = c_void_p(quartz.CGImageSourceCopyPropertiesAtIndex(sourceRef, index, None)) if cf.CFDictionaryContainsKey(props, kCGImagePropertyGIFDictionary): gif_props = c_void_p(cf.CFDictionaryGetValue(props, kCGImagePropertyGIFDictionary)) if cf.CFDictionaryContainsKey(gif_props, kCGImagePropertyGIFDelayTime): duration = cfnumber_to_number(c_void_p(cf.CFDictionaryGetValue(gif_props, kCGImagePropertyGIFDelayTime))) cf.CFRelease(props) image = self._get_pyglet_ImageData_from_source_at_index(sourceRef, index) frames.append( AnimationFrame(image, duration) ) cf.CFRelease(data) cf.CFRelease(sourceRef) return Animation(frames)
def decode(self, file, filename): file_bytes = file.read() data = c_void_p(cf.CFDataCreate(None, file_bytes, len(file_bytes))) # Second argument is an options dictionary. It might be a good idea to provide # a value for kCGImageSourceTypeIdentifierHint here using filename extension. sourceRef = c_void_p(quartz.CGImageSourceCreateWithData(data, None)) image = self._get_pyglet_ImageData_from_source_at_index(sourceRef, 0) cf.CFRelease(data) cf.CFRelease(sourceRef) return image