Ejemplo n.º 1
0
 def __init__(self, **kwargs):
     self.size = kwargs.get('size', (32, 32))
     self.pattern = kwargs.get('pattern', 'hsv')
     self.datatype = kwargs.get('datatype', chr)
     self.arraytype = datatype_map[self.datatype]['array']
     self.oglbuffertype = datatype_map[self.datatype]['ogl']
     self.color_format = kwargs.get('color_format', 'rgb')
     self.update_threads = collections.deque()
     self.threaded_updater = ThreadedUpdater(
         job_complete_cb=self.on_threaded_job_complete)
     self.threaded_updater.start()
     self.grid = PixelGrid(size=self.size)
     self.build_texture()
     self.build_pattern()
     self.blit_texture()
Ejemplo n.º 2
0
 def __init__(self, **kwargs):
     self.size = kwargs.get('size', (32, 32))
     self.pattern = kwargs.get('pattern', 'hsv')
     self.datatype = kwargs.get('datatype', chr)
     self.arraytype = datatype_map[self.datatype]['array']
     self.oglbuffertype = datatype_map[self.datatype]['ogl']
     self.color_format = kwargs.get('color_format', 'rgb')
     self.update_threads = collections.deque()
     self.threaded_updater = ThreadedUpdater(job_complete_cb=self.on_threaded_job_complete)
     self.threaded_updater.start()
     self.grid = PixelGrid(size=self.size)
     self.build_texture()
     self.build_pattern()
     self.blit_texture()
Ejemplo n.º 3
0
class TextureObj(object):
    hsv_keys = ['hue', 'sat', 'val']
    def __init__(self, **kwargs):
        self.size = kwargs.get('size', (32, 32))
        self.pattern = kwargs.get('pattern', 'hsv')
        self.datatype = kwargs.get('datatype', chr)
        self.arraytype = datatype_map[self.datatype]['array']
        self.oglbuffertype = datatype_map[self.datatype]['ogl']
        self.color_format = kwargs.get('color_format', 'rgb')
        self.update_threads = collections.deque()
        self.threaded_updater = ThreadedUpdater(job_complete_cb=self.on_threaded_job_complete)
        self.threaded_updater.start()
        self.grid = PixelGrid(size=self.size)
        self.build_texture()
        self.build_pattern()
        self.blit_texture()
        
    def unlink(self):
        self.threaded_updater.stop(blocking=True)
        
    def add_update_job(self, cb, **kwargs):
        self.threaded_updater.add_job(cb, **kwargs)
        
    def on_threaded_job_complete(self, *args, **kwargs):
        KivyClock.schedule_once(self.blit_texture, 0)
        
    def build_pattern(self, cb=None):
        if cb is not None:
            pass
        elif self.pattern == 'hsv':
            cb = self.build_hsv_pattern
        elif self.pattern == 'rgb':
            cb = self.build_rgb_pattern
        else:
            return
        self.add_update_job(cb)
    
    def build_texture(self):
        self._texture = kivy.graphics.texture.Texture.create(size=self.size, colorfmt=self.color_format)
    
    def find_pixel_from_hsv(self, hsv):
        l = self.grid.find_pixels_from_hsv(hsv)
        if not len(l):
            return False
        return l[0]
        
    def setattr_all_pixels(self, **kwargs):
        self.add_update_job(self.grid.setattr_all_pixels, **kwargs)
        #self.grid.setattr_all_pixels(**kwargs)
        #self.blit_texture()
    
    def build_hsv_pattern(self, hue=0.):
        for y, row in enumerate(self.grid.pixels):
            for x, pixel in enumerate(row):
                pixel.hue = hue
                pixel.sat = y / float(self.grid.num_rows)
                pixel.val = x / float(self.grid.num_cols)
                #pixel.set_hsv(hue=hue, sat=y / float(self.grid.num_rows), val=x / float(self.grid.num_cols))
                
    def build_rgb_pattern(self):
        for y, row in enumerate(self.grid.pixels):
            for x, pixel in enumerate(row):
                pixel.red = y * 255. / self.grid.num_rows
                pixel.green = (y * -255. / self.grid.num_rows) + 255
                pixel.blue = x * 255. / self.grid.num_cols
    
    def resize(self, **kwargs):
        size = kwargs.get('size')
        size = [int(f) for f in size]
        if size == self.size:
            return
        self.grid.resize(size=size)
        self.size = size
        self.build_texture()
        self.build_test_pattern()
        
    def blit_texture(self, *args, **kwargs):
        a = self.grid.get_ogl_pixel_data(color_format=self.color_format, arraytype=self.arraytype)
        bfr = a.tostring()
        self._texture.blit_buffer(bfr, colorfmt=self.color_format, bufferfmt=self.oglbuffertype)
Ejemplo n.º 4
0
class TextureObj(object):
    hsv_keys = ['hue', 'sat', 'val']

    def __init__(self, **kwargs):
        self.size = kwargs.get('size', (32, 32))
        self.pattern = kwargs.get('pattern', 'hsv')
        self.datatype = kwargs.get('datatype', chr)
        self.arraytype = datatype_map[self.datatype]['array']
        self.oglbuffertype = datatype_map[self.datatype]['ogl']
        self.color_format = kwargs.get('color_format', 'rgb')
        self.update_threads = collections.deque()
        self.threaded_updater = ThreadedUpdater(
            job_complete_cb=self.on_threaded_job_complete)
        self.threaded_updater.start()
        self.grid = PixelGrid(size=self.size)
        self.build_texture()
        self.build_pattern()
        self.blit_texture()

    def unlink(self):
        self.threaded_updater.stop(blocking=True)

    def add_update_job(self, cb, **kwargs):
        self.threaded_updater.add_job(cb, **kwargs)

    def on_threaded_job_complete(self, *args, **kwargs):
        KivyClock.schedule_once(self.blit_texture, 0)

    def build_pattern(self, cb=None):
        if cb is not None:
            pass
        elif self.pattern == 'hsv':
            cb = self.build_hsv_pattern
        elif self.pattern == 'rgb':
            cb = self.build_rgb_pattern
        else:
            return
        self.add_update_job(cb)

    def build_texture(self):
        self._texture = kivy.graphics.texture.Texture.create(
            size=self.size, colorfmt=self.color_format)

    def find_pixel_from_hsv(self, hsv):
        l = self.grid.find_pixels_from_hsv(hsv)
        if not len(l):
            return False
        return l[0]

    def setattr_all_pixels(self, **kwargs):
        self.add_update_job(self.grid.setattr_all_pixels, **kwargs)
        #self.grid.setattr_all_pixels(**kwargs)
        #self.blit_texture()

    def build_hsv_pattern(self, hue=0.):
        for y, row in enumerate(self.grid.pixels):
            for x, pixel in enumerate(row):
                pixel.hue = hue
                pixel.sat = y / float(self.grid.num_rows)
                pixel.val = x / float(self.grid.num_cols)
                #pixel.set_hsv(hue=hue, sat=y / float(self.grid.num_rows), val=x / float(self.grid.num_cols))

    def build_rgb_pattern(self):
        for y, row in enumerate(self.grid.pixels):
            for x, pixel in enumerate(row):
                pixel.red = y * 255. / self.grid.num_rows
                pixel.green = (y * -255. / self.grid.num_rows) + 255
                pixel.blue = x * 255. / self.grid.num_cols

    def resize(self, **kwargs):
        size = kwargs.get('size')
        size = [int(f) for f in size]
        if size == self.size:
            return
        self.grid.resize(size=size)
        self.size = size
        self.build_texture()
        self.build_test_pattern()

    def blit_texture(self, *args, **kwargs):
        a = self.grid.get_ogl_pixel_data(color_format=self.color_format,
                                         arraytype=self.arraytype)
        bfr = a.tostring()
        self._texture.blit_buffer(bfr,
                                  colorfmt=self.color_format,
                                  bufferfmt=self.oglbuffertype)