Example #1
0
def argb_swap(image, rgb_formats, supports_transparency):
    """ use the argb codec to do the RGB byte swapping """
    pixel_format = image.get_pixel_format()
    if None in (bgra_to_rgb, bgra_to_rgba, argb_to_rgb, argb_to_rgba):
        warn_encoding_once("argb-module-missing", "no argb module, cannot convert %s to one of: %s" % (pixel_format, rgb_formats))
        return False

    #try to fallback to argb module
    #if we have one of the target pixel formats:
    pixels = image.get_pixels()
    rs = image.get_rowstride()
    if pixel_format in ("BGRX", "BGRA"):
        if supports_transparency and "RGBA" in rgb_formats:
            image.set_pixels(bgra_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            image.set_pixels(bgra_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs/4*3)
            return True
    if pixel_format in ("XRGB", "ARGB"):
        if supports_transparency and "RGBA" in rgb_formats:
            image.set_pixels(argb_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            image.set_pixels(argb_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs/4*3)
            return True
    warn_encoding_once(pixel_format+"-format-not-handled", "no matching argb function: cannot convert %s to one of: %s" % (pixel_format, rgb_formats))
    return False
Example #2
0
def argb_swap(image, rgb_formats, supports_transparency):
    """ use the argb codec to do the RGB byte swapping """
    pixel_format = image.get_pixel_format()
    #try to fallback to argb module
    #if we have one of the target pixel formats:
    pixels = image.get_pixels()
    assert pixels, "failed to get pixels from %s" % image
    rs = image.get_rowstride()
    if pixel_format in ("BGRX", "BGRA"):
        if supports_transparency and "RGBA" in rgb_formats:
            log("argb_swap: bgra_to_rgba for %s on %s", pixel_format, type(pixels))
            image.set_pixels(bgra_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            log("argb_swap: bgra_to_rgb for %s on %s", pixel_format, type(pixels))
            image.set_pixels(bgra_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs*3//4)
            return True
    if pixel_format in ("XRGB", "ARGB"):
        if supports_transparency and "RGBA" in rgb_formats:
            log("argb_swap: argb_to_rgba for %s on %s", pixel_format, type(pixels))
            image.set_pixels(argb_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            log("argb_swap: argb_to_rgb for %s on %s", pixel_format, type(pixels))
            image.set_pixels(argb_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs*3//4)
            return True
    warn_encoding_once(pixel_format+"-format-not-handled", "no matching argb function: cannot convert %s to one of: %s" % (pixel_format, rgb_formats))
    return False
Example #3
0
def argb_swap(image, rgb_formats, supports_transparency):
    """ use the argb codec to do the RGB byte swapping """
    pixel_format = image.get_pixel_format()
    #try to fallback to argb module
    #if we have one of the target pixel formats:
    pixels = image.get_pixels()
    rs = image.get_rowstride()
    if pixel_format in ("BGRX", "BGRA"):
        if supports_transparency and "RGBA" in rgb_formats:
            image.set_pixels(bgra_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            image.set_pixels(bgra_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs / 4 * 3)
            return True
    if pixel_format in ("XRGB", "ARGB"):
        if supports_transparency and "RGBA" in rgb_formats:
            image.set_pixels(argb_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            image.set_pixels(argb_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs / 4 * 3)
            return True
    warn_encoding_once(
        pixel_format + "-format-not-handled",
        "no matching argb function: cannot convert %s to one of: %s" %
        (pixel_format, rgb_formats))
    return False
Example #4
0
def argb_swap(image, rgb_formats, supports_transparency):
    """ use the argb codec to do the RGB byte swapping """
    pixel_format = image.get_pixel_format()
    #try to fallback to argb module
    #if we have one of the target pixel formats:
    pixels = image.get_pixels()
    assert pixels, "failed to get pixels from %s" % image
    rs = image.get_rowstride()
    if pixel_format == "r210":
        if supports_transparency and "RGBA" in rgb_formats:
            log("argb_swap: r210_to_rgba for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(r210_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            log("argb_swap: r210_to_rgb for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(r210_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs * 3 // 4)
            return True
    elif pixel_format in ("BGRX", "BGRA"):
        if supports_transparency and "RGBA" in rgb_formats:
            log("argb_swap: bgra_to_rgba for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(bgra_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            log("argb_swap: bgra_to_rgb for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(bgra_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs * 3 // 4)
            return True
    elif pixel_format in ("XRGB", "ARGB"):
        if supports_transparency and "RGBA" in rgb_formats:
            log("argb_swap: argb_to_rgba for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(argb_to_rgba(pixels))
            image.set_pixel_format("RGBA")
            return True
        if "RGB" in rgb_formats:
            log("argb_swap: argb_to_rgb for %s on %s", pixel_format,
                type(pixels))
            image.set_pixels(argb_to_rgb(pixels))
            image.set_pixel_format("RGB")
            image.set_rowstride(rs * 3 // 4)
            return True
    warn_encoding_once(
        pixel_format + "-format-not-handled",
        "no matching argb function: cannot convert %s to one of: %s" %
        (pixel_format, rgb_formats))
    return False
 def update_icon(self, width, height, coding, data):
     coding = bytestostr(coding)
     iconlog("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data))
     if PYTHON3 and WIN32:
         iconlog("not setting icon to prevent crashes..")
         return
     if coding == "premult_argb32":            #we usually cannot do in-place and this is not performance critical
         data = unpremultiply_argb(data)
         rgba = memoryview_to_bytes(bgra_to_rgba(data))
         pixbuf = get_pixbuf_from_data(rgba, True, width, height, width*4)
     else:
         loader = PixbufLoader()
         loader.write(data)
         loader.close()
         pixbuf = loader.get_pixbuf()
     #for debugging, save to a file so we can see it:
     #pixbuf.save("C-%s-%s.png" % (self._id, int(time.time())), "png")
     iconlog("%s.set_icon(%s)", self, pixbuf)
     self.set_icon(pixbuf)
 def update_icon(self, width, height, coding, data):
     log("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data))
     coding = bytestostr(coding)
     if coding == "premult_argb32":
         if unpremultiply_argb is None:
             #we could use PIL here with mode 'RGBa'
             log.warn("cannot process premult_argb32 icon without the argb module")
             return
         #we usually cannot do in-place and this is not performance critical
         data = unpremultiply_argb(data)
         rgba = byte_buffer_to_buffer(bgra_to_rgba(data))
         pixbuf = get_pixbuf_from_data(rgba, True, width, height, width*4)
     else:
         loader = PixbufLoader()
         loader.write(data)
         loader.close()
         pixbuf = loader.get_pixbuf()
     log("%s.set_icon(%s)", self, pixbuf)
     self.set_icon(pixbuf)
 def update_icon(self, width, height, coding, data):
     coding = bytestostr(coding)
     iconlog("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height,
             coding, len(data))
     if PYTHON3 and WIN32:
         iconlog("not setting icon to prevent crashes..")
         return
     if coding == "premult_argb32":  #we usually cannot do in-place and this is not performance critical
         data = unpremultiply_argb(data)
         rgba = memoryview_to_bytes(bgra_to_rgba(data))
         pixbuf = get_pixbuf_from_data(rgba, True, width, height, width * 4)
     else:
         loader = PixbufLoader()
         loader.write(data)
         loader.close()
         pixbuf = loader.get_pixbuf()
     #for debugging, save to a file so we can see it:
     #pixbuf.save("C-%s-%s.png" % (self._id, int(time.time())), "png")
     iconlog("%s.set_icon(%s)", self, pixbuf)
     self.set_icon(pixbuf)
Example #8
0
 def update_icon(self, width, height, coding, data):
     log("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height,
         coding, len(data))
     coding = bytestostr(coding)
     if coding == "premult_argb32":
         if unpremultiply_argb is None:
             #we could use PIL here with mode 'RGBa'
             log.warn(
                 "cannot process premult_argb32 icon without the argb module"
             )
             return
         #we usually cannot do in-place and this is not performance critical
         data = unpremultiply_argb(data)
         rgba = byte_buffer_to_buffer(bgra_to_rgba(data))
         pixbuf = get_pixbuf_from_data(rgba, True, width, height, width * 4)
     else:
         loader = PixbufLoader()
         loader.write(data)
         loader.close()
         pixbuf = loader.get_pixbuf()
     log("%s.set_icon(%s)", self, pixbuf)
     self.set_icon(pixbuf)