def _tostring_RGB(surf, flipped): rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask, surf.format.Bmask, surf.format.Amask) rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift, surf.format.Bshift, surf.format.Ashift) rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss, surf.format.Bloss, surf.format.Aloss) bpp = surf.format.BytesPerPixel h, w = surf.h, surf.w data = ffi.new('char[]', w * h * 3) if bpp == 1: pixels = ffi.cast('uint8_t*', surf.pixels) colors = surf.format.palette.colors for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 3 * (y * w + x) color = pixels[src_start + x] data[dest] = chr_(colors[color].r) data[dest + 1] = chr_(colors[color].g) data[dest + 2] = chr_(colors[color].b) elif bpp == 2: pixels = ffi.cast('uint16_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 3 * (y * w + x) color = pixels[src_start + x] data[dest] = chr_(((color & rmask) >> rshift) << rloss) data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss) data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss) elif bpp == 3: pixels = ffi.cast('uint8_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * surf.pitch if flipped \ else y * surf.pitch for x in range(w): dest = 3 * (y * w + x) color = (pixels[src_start + x * 3 + BYTE0] + (pixels[src_start + x * 3 + BYTE1] << 8) + (pixels[src_start + x * 3 + BYTE2] << 16)) data[dest] = chr_(((color & rmask) >> rshift) << rloss) data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss) data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss) elif bpp == 4: pixels = ffi.cast('uint32_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 3 * (y * w + x) color = pixels[src_start + x] data[dest] = chr_(((color & rmask) >> rshift) << rloss) data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss) data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss) else: raise ValueError("invalid color depth") return ffi.buffer(data)[:]
def _tostring_RGBA_PREMULT(surf, flipped, argb=False): if surf.format.BytesPerPixel == 1 or surf.format.Amask == 0: raise ValueError("Can only create pre-multiplied alpha strings if " "the surface has per-pixel alpha") rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask, surf.format.Bmask, surf.format.Amask) rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift, surf.format.Bshift, surf.format.Ashift) rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss, surf.format.Bloss, surf.format.Aloss) bpp = surf.format.BytesPerPixel h, w = surf.h, surf.w if argb: ri, gi, bi, ai = 1, 2, 3, 0 else: ri, gi, bi, ai = 0, 1, 2, 3 data = ffi.new('char[]', w * h * 4) if bpp == 2: pixels = ffi.cast('uint16_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] alpha = ((color & amask) >> ashift) << aloss data[dest + ri] = chr_((((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_((((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_((((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) elif bpp == 3: pixels = ffi.cast('uint8_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * surf.pitch if flipped \ else y * surf.pitch for x in range(w): dest = 4 * (y * w + x) color = (pixels[src_start + x * 3 + BYTE0] + (pixels[src_start + x * 3 + BYTE1] << 8) + (pixels[src_start + x * 3 + BYTE2] << 16)) alpha = ((color & amask) >> ashift) << aloss data[dest + ri] = chr_((((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_((((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_((((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) elif bpp == 4: pixels = ffi.cast('uint32_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] alpha = ((color & amask) >> ashift) << aloss if alpha == 0: data[dest + ri] = data[dest + gi] = data[dest + bi] \ = chr_(0) else: data[dest + ri] = chr_( (((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_( (((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_( (((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) else: raise ValueError("invalid color depth") return ffi.buffer(data)[:]
def _tostring_RGBA(surf, flipped, has_colorkey=True, argb=False): rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask, surf.format.Bmask, surf.format.Amask) rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift, surf.format.Bshift, surf.format.Ashift) rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss, surf.format.Bloss, surf.format.Aloss) bpp = surf.format.BytesPerPixel h, w = surf.h, surf.w colorkey = surf.format.colorkey if argb: has_colorkey = False ri, gi, bi, ai = 1, 2, 3, 0 else: has_colorkey = (has_colorkey and surf.flags & sdl.SDL_SRCCOLORKEY and not amask) ri, gi, bi, ai = 0, 1, 2, 3 data = ffi.new('char[]', w * h * 4) if bpp == 1: pixels = ffi.cast('uint8_t*', surf.pixels) colors = surf.format.palette.colors for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(colors[color].r) data[dest + gi] = chr_(colors[color].g) data[dest + bi] = chr_(colors[color].b) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_(255) elif bpp == 2: pixels = ffi.cast('uint16_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_( (((color & amask) >> ashift) << aloss) if amask else 255) elif bpp == 3: pixels = ffi.cast('uint8_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * surf.pitch if flipped \ else y * surf.pitch for x in range(w): dest = 4 * (y * w + x) color = (pixels[src_start + x * 3 + BYTE0] + (pixels[src_start + x * 3 + BYTE1] << 8) + (pixels[src_start + x * 3 + BYTE2] << 16)) data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_( (((color & amask) >> ashift) << aloss) if amask else 255) elif bpp == 4: pixels = ffi.cast('uint32_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_( (((color & amask) >> ashift) << aloss) if amask else 255) else: raise ValueError("invalid color depth") return ffi.buffer(data)[:]
def _tostring_RGBA_PREMULT(surf, flipped, argb=False): if surf.format.BytesPerPixel == 1 or surf.format.Amask == 0: raise ValueError("Can only create pre-multiplied alpha strings if " "the surface has per-pixel alpha") rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask, surf.format.Bmask, surf.format.Amask) rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift, surf.format.Bshift, surf.format.Ashift) rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss, surf.format.Bloss, surf.format.Aloss) bpp = surf.format.BytesPerPixel h, w = surf.h, surf.w if argb: ri, gi, bi, ai = 1, 2, 3, 0 else: ri, gi, bi, ai = 0, 1, 2, 3 data = ffi.new('char[]', w * h * 4) if bpp == 2: pixels = ffi.cast('uint16_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] alpha = ((color & amask) >> ashift) << aloss data[dest + ri] = chr_( (((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_( (((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_( (((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) elif bpp == 3: pixels = ffi.cast('uint8_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * surf.pitch if flipped \ else y * surf.pitch for x in range(w): dest = 4 * (y * w + x) color = (pixels[src_start + x * 3 + BYTE0] + (pixels[src_start + x * 3 + BYTE1] << 8) + (pixels[src_start + x * 3 + BYTE2] << 16)) alpha = ((color & amask) >> ashift) << aloss data[dest + ri] = chr_( (((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_( (((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_( (((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) elif bpp == 4: pixels = ffi.cast('uint32_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] alpha = ((color & amask) >> ashift) << aloss if alpha == 0: data[dest + ri] = data[dest + gi] = data[dest + bi] \ = chr_(0) else: data[dest + ri] = chr_( (((color & rmask) >> rshift) << rloss) * alpha // 255) data[dest + gi] = chr_( (((color & gmask) >> gshift) << gloss) * alpha // 255) data[dest + bi] = chr_( (((color & bmask) >> bshift) << bloss) * alpha // 255) data[dest + ai] = chr_(alpha) else: raise ValueError("invalid color depth") return ffi.buffer(data)[:]
def _tostring_RGBA(surf, flipped, has_colorkey=True, argb=False): rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask, surf.format.Bmask, surf.format.Amask) rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift, surf.format.Bshift, surf.format.Ashift) rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss, surf.format.Bloss, surf.format.Aloss) bpp = surf.format.BytesPerPixel h, w = surf.h, surf.w colorkey = surf.format.colorkey if argb: has_colorkey = False ri, gi, bi, ai = 1, 2, 3, 0 else: has_colorkey = (has_colorkey and surf.flags & sdl.SDL_SRCCOLORKEY and not amask) ri, gi, bi, ai = 0, 1, 2, 3 data = ffi.new('char[]', w * h * 4) if bpp == 1: pixels = ffi.cast('uint8_t*', surf.pixels) colors = surf.format.palette.colors for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(colors[color].r) data[dest + gi] = chr_(colors[color].g) data[dest + bi] = chr_(colors[color].b) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_(255) elif bpp == 2: pixels = ffi.cast('uint16_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_((( (color & amask) >> ashift) << aloss) if amask else 255) elif bpp == 3: pixels = ffi.cast('uint8_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * surf.pitch if flipped \ else y * surf.pitch for x in range(w): dest = 4 * (y * w + x) color = (pixels[src_start + x * 3 + BYTE0] + (pixels[src_start + x * 3 + BYTE1] << 8) + (pixels[src_start + x * 3 + BYTE2] << 16)) data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_((( (color & amask) >> ashift) << aloss) if amask else 255) elif bpp == 4: pixels = ffi.cast('uint32_t*', surf.pixels) for y in range(h): src_start = (h - 1 - y) * w if flipped \ else y * w for x in range(w): dest = 4 * (y * w + x) color = pixels[src_start + x] data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss) data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss) data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss) if has_colorkey: data[dest + ai] = chr_( ffi.cast('char', color != colorkey) * 255) else: data[dest + ai] = chr_((( (color & amask) >> ashift) << aloss) if amask else 255) else: raise ValueError("invalid color depth") return ffi.buffer(data)[:]