def make_texture_from_bits(self, bits, width, height): if width < 1: raise ValueError("width must be greater than 0") rmask = gmask = bmask = amask = 0 imgsurface = sdl2.surface.SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask) if not imgsurface: raise sdl2.ext.SDLError() swtarget = sdl2.ext.SoftwareSprite(imgsurface.contents, True) rtarget = swtarget.surface sdl2.surface.SDL_FreeSurface(imgsurface) pitch = rtarget.pitch bpp = rtarget.format.contents.BytesPerPixel frac = pitch / bpp clip_rect = rtarget.clip_rect left, right = clip_rect.x, clip_rect.x + clip_rect.w - 1 top, bottom = clip_rect.y, clip_rect.y + clip_rect.h - 1 pxbuf = ctypes.cast(rtarget.pixels, ctypes.POINTER(ctypes.c_uint32)) for idx in range(0,width*height): b_data = bits[idx:idx+3] # print("data: [%s]" % b_data) b_num = [ord(b) for b in b_data] col = (b_num[0],b_num[1],b_num[2],255) #col = (random.randint(0,255),0,0, 128) # pxbuf[int(i)] = prepare_color( col , swtarget) # if(col[0]!=0) or (col[1]!=0) or (col[2]!=0): # print("[%05d] copy data %s" % (idx,col)) pxbuf[int(idx)] = prepare_color( col , swtarget) # Uint32 * pixels = new Uint32[640 * 480]; ###sdl2.SDL_UpdateTexture(rtarget, None, pxbuf, width * sizeof(ctypes.c_uint32)); # sw_sprite = self.load_surface("assets/dmd/smile.png") # tx = self.texture_from_surface(sw_sprite) tx = self.texture_from_surface(swtarget) #self.copy_rect(tx) del swtarget return tx
def make_texture_from_image_tedious(self, img): """Creates a Sprite from a Python imaging Image.""" width, height = img.size if width < 1: raise ValueError("width must be greater than 0") # rtarget = sdl2.render.SDL_CreateTexture(self.texture_renderer.renderer, sdl2.pixels.SDL_PIXELFORMAT_RGB888, # sdl2.render.SDL_TEXTUREACCESS_TARGET, # width, height) # sdl2.SDL_SetTextureBlendMode(rtarget, sdl2.SDL_BLENDMODE_BLEND) # txtarget = sdl2.ext.TextureSprite(rtarget) rmask = gmask = bmask = amask = 0 imgsurface = sdl2.surface.SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask) if not imgsurface: raise sdl2.ext.SDLError() swtarget = sdl2.ext.SoftwareSprite(imgsurface.contents, True) rtarget = swtarget.surface pitch = rtarget.pitch bpp = rtarget.format.contents.BytesPerPixel frac = pitch / bpp clip_rect = rtarget.clip_rect left, right = clip_rect.x, clip_rect.x + clip_rect.w - 1 top, bottom = clip_rect.y, clip_rect.y + clip_rect.h - 1 print((width, height)) print(pitch) print(bpp) print(frac) print(clip_rect) # if bpp == 3: # raise UnsupportedError(line, "24bpp are currently not supported") # if bpp == 2: # pxbuf = ctypes.cast(rtarget.pixels, ctypes.POINTER(ctypes.c_uint16)) # elif bpp == 4: pxbuf = ctypes.cast(rtarget.pixels, ctypes.POINTER(ctypes.c_uint32)) # else: # pxbuf = rtarget.pixels # byte-wise access. #pxbuf = bytes # for y in range(0, height): # for x in range(0,width): # pxbuff[int()] # casting -- no. # b = (ctypes.c_uint32 * int(width * height / int(4)) )(bytes) # for i,b in enumerate(bytes): # pxbuf[int(i)] = (b) for i, col in enumerate(img.getdata()): #range(0,pitch): # col = mpx # # col = (ord(bytes[i]),ord(bytes[i+1]),ord(bytes[i+2]),ord(bytes[i+3])) #col = (random.randint(0,255),0,0, 128) # pxbuf[int(i)] = prepare_color( col , swtarget) pxbuf[int(i)] = prepare_color(col, swtarget) print("[%05d] copy data %s" % (i, col)) # Uint32 * pixels = new Uint32[640 * 480]; ###sdl2.SDL_UpdateTexture(rtarget, None, pxbuf, width * sizeof(ctypes.c_uint32)); # sw_sprite = self.load_surface("assets/dmd/smile.png") # tx = self.texture_from_surface(sw_sprite) tx = self.texture_from_surface(swtarget) #self.copy_rect(tx) del swtarget return tx
def make_texture_from_image_tedious(self, img): """Creates a Sprite from a Python imaging Image.""" width, height = img.size if width < 1: raise ValueError("width must be greater than 0") # rtarget = sdl2.render.SDL_CreateTexture(self.texture_renderer.renderer, sdl2.pixels.SDL_PIXELFORMAT_RGB888, # sdl2.render.SDL_TEXTUREACCESS_TARGET, # width, height) # sdl2.SDL_SetTextureBlendMode(rtarget, sdl2.SDL_BLENDMODE_BLEND) # txtarget = sdl2.ext.TextureSprite(rtarget) rmask = gmask = bmask = amask = 0 imgsurface = sdl2.surface.SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask) if not imgsurface: raise sdl2.ext.SDLError() swtarget = sdl2.ext.SoftwareSprite(imgsurface.contents, True) rtarget = swtarget.surface pitch = rtarget.pitch bpp = rtarget.format.contents.BytesPerPixel frac = pitch / bpp clip_rect = rtarget.clip_rect left, right = clip_rect.x, clip_rect.x + clip_rect.w - 1 top, bottom = clip_rect.y, clip_rect.y + clip_rect.h - 1 print((width, height)) print(pitch) print(bpp) print(frac) print(clip_rect) # if bpp == 3: # raise UnsupportedError(line, "24bpp are currently not supported") # if bpp == 2: # pxbuf = ctypes.cast(rtarget.pixels, ctypes.POINTER(ctypes.c_uint16)) # elif bpp == 4: pxbuf = ctypes.cast(rtarget.pixels, ctypes.POINTER(ctypes.c_uint32)) # else: # pxbuf = rtarget.pixels # byte-wise access. #pxbuf = bytes # for y in range(0, height): # for x in range(0,width): # pxbuff[int()] # casting -- no. # b = (ctypes.c_uint32 * int(width * height / int(4)) )(bytes) # for i,b in enumerate(bytes): # pxbuf[int(i)] = (b) for i,col in enumerate(img.getdata()): #range(0,pitch): # col = mpx # # col = (ord(bytes[i]),ord(bytes[i+1]),ord(bytes[i+2]),ord(bytes[i+3])) #col = (random.randint(0,255),0,0, 128) # pxbuf[int(i)] = prepare_color( col , swtarget) pxbuf[int(i)] = prepare_color( col , swtarget) print("[%05d] copy data %s" % (i,col)) # Uint32 * pixels = new Uint32[640 * 480]; ###sdl2.SDL_UpdateTexture(rtarget, None, pxbuf, width * sizeof(ctypes.c_uint32)); # sw_sprite = self.load_surface("assets/dmd/smile.png") # tx = self.texture_from_surface(sw_sprite) tx = self.texture_from_surface(swtarget) #self.copy_rect(tx) del swtarget return tx