def init_info(self, info): if info is self.info: return fallback, fallback_active = self.default_icon_path() if info.favicon: thumb_path = info.favicon else: thumb_path = fallback # we don't use the ImagePool because 'favicon.ico' is a name with too # many hits (#16573). try: image = widgetset.Image(thumb_path) if image.width > 16 or image.height > 16: image = imagepool.resize_image(image, 16, 16) info.icon = widgetset.ImageSurface(image) if not info.favicon: info.active_icon = imagepool.get_surface(fallback_active, size=(16, 16)) except ValueError: # 16842 - if we ever get sent an invalid icon - don't crash with # ValueError. info.icon = imagepool.get_surface(fallback, size=(16, 16)) info.active_icon = imagepool.get_surface(fallback_active, size=(16, 16)) info.unwatched = info.available = 0 info.type = self.type
class ImagePool(util.Cache): def create_new_value(self, (path, size)): try: image = widgetset.Image(path) except StandardError: logging.warn("error loading image %s:\n%s", path, traceback.format_exc()) image = broken_image if size is not None: image = resize_image(image, *size) return image
"""``miro.frontends.widgets.imagepool`` -- Get Image objects for image filenames. imagepool handles creating Image and ImageSurface objects for image filenames. It caches Image/ImageSurface objecsts so to avoid re-creating them. """ import logging import traceback from miro import util from miro.plat import resources from miro.plat.frontends.widgets import widgetset broken_image = widgetset.Image(resources.path('images/broken-image.gif')) CACHE_SIZE = 2000 # number of objects to keep in memory def resize_image(image, dest_width, dest_height, upsize_threshold=1.5): # handle corner case of empty dest if (dest_width * dest_height) == 0: return broken_image # calculate how much we need to enlarge/shrink the image so that a # dimension lines up with the destination size width_scale = float(dest_width) / image.width height_scale = float(dest_height) / image.height # scale such that one dimension lines up and one is overlapping scale = max(width_scale, height_scale) # check that we don't upsize too much