Ejemplo n.º 1
0
def chess(screen, cola, colb):
    for i in xrange(WIDTH):
        for j in xrange(HEIGHT):
            if (i + j) % 2:
                c = cola
            else:
                c = colb
            RSDL_helper.set_pixel(screen, i, j, c)
Ejemplo n.º 2
0
def stripes_m(screen, cola, colb):
    for j in xrange(WIDTH):
        for i in xrange(HEIGHT):
            k = j * WIDTH + i
            if k % 2:
                c = cola
            else:
                c = colb
            RSDL_helper.set_pixel(screen, i, j, c)
Ejemplo n.º 3
0
 def draw_pixels(self):
     #pass
     str = ""
     for y in range(self.height):
         # str += "\n"
         for x in range(self.width):
             #if y%2 == 0 or True:
             #    px = self.get_pixel_color(x, y)
             #    str += ["#", "%", "+", " ", " "][px]
             RSDL_helper.set_pixel(self.screen, x, y,
                                   self.get_pixel_color(x, y))
Ejemplo n.º 4
0
    def draw_pixel(self, x, y, color):
        color = self.COLOR_MAP[color]
        start_x = x * self.scale
        start_y = y * self.scale

        if self.scale > 1:
            for x in range(self.scale):
                for y in range(self.scale):
                    RSDL_helper.set_pixel(self.screen, start_x + x,
                                          start_y + y, color)
        else:
            RSDL_helper.set_pixel(self.screen, start_x, start_y, color)
Ejemplo n.º 5
0
def test_image_pixels():
    for filename in ["demo.jpg", "demo.png"]:
        image = RIMG.Load(os.path.join(autopath.this_dir, filename))
        assert image
        assert rffi.getintfield(image.c_format, 'c_BytesPerPixel') in (3, 4)
        RSDL.LockSurface(image)
        result = {}
        try:
            rgb = lltype.malloc(rffi.CArray(RSDL.Uint8), 3, flavor='raw')
            try:
                for y in range(23):
                    for x in range(y % 13, 17, 13):
                        color = RSDL_helper.get_pixel(image, x, y)
                        RSDL.GetRGB(color, image.c_format, rffi.ptradd(rgb, 0),
                                    rffi.ptradd(rgb, 1), rffi.ptradd(rgb, 2))
                        r = rffi.cast(lltype.Signed, rgb[0])
                        g = rffi.cast(lltype.Signed, rgb[1])
                        b = rffi.cast(lltype.Signed, rgb[2])
                        result[x, y] = r, g, b
            finally:
                lltype.free(rgb, flavor='raw')
        finally:
            RSDL.UnlockSurface(image)
        RSDL.FreeSurface(image)
        for x, y in result:
            f = (x * 17 + y * 23) / float(17 * 17 + 23 * 23)
            expected_r = int(255.0 * (1.0 - f))
            expected_g = 0
            expected_b = int(255.0 * f)
            r, g, b = result[x, y]
            assert abs(r - expected_r) < 10
            assert abs(g - expected_g) < 10
            assert abs(b - expected_b) < 10
Ejemplo n.º 6
0
    def test_blit_rect(self):
        surface = RSDL.CreateRGBSurface(0, 150, 50, 32,
                                        r_uint(0x000000FF),
                                        r_uint(0x0000FF00),
                                        r_uint(0x00FF0000),
                                        r_uint(0xFF000000))
        fmt = surface.c_format
        color = RSDL.MapRGB(fmt, 255, 0, 0)
        RSDL.FillRect(surface, lltype.nullptr(RSDL.Rect), color)
        
        paintrect = RSDL_helper.mallocrect(75, 0, 150, 50)
        dstrect = lltype.malloc(RSDL.Rect, flavor='raw')
        try:
            color = RSDL.MapRGB(fmt, 255, 128, 0)
            RSDL.FillRect(surface, paintrect, color)

            rffi.setintfield(dstrect, 'c_x',  10)
            rffi.setintfield(dstrect, 'c_y',  10)
            rffi.setintfield(dstrect, 'c_w', 150)
            rffi.setintfield(dstrect, 'c_h',  50)
            RSDL.BlitSurface(surface, lltype.nullptr(RSDL.Rect), self.screen, dstrect)
            RSDL.Flip(self.screen)
        finally:
            lltype.free(dstrect, flavor='raw')
            lltype.free(paintrect, flavor='raw')
        RSDL.FreeSurface(surface)
        self.check("Half Red/Orange rectangle(150px * 50px) at the top left, 10 pixels from the border")
Ejemplo n.º 7
0
    def test_set_color_key(self):
        # prepare
        fillrect = RSDL_helper.mallocrect(10, 10, 30, 30)
        RSDL.FillRect(self.src_surf, fillrect, self.blue)
        lltype.free(fillrect, flavor='raw')
        assert RSDL.SetColorKey(self.src_surf, RSDL.SRCCOLORKEY,
                                self.blue) == 0

        # draw
        RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 0, 0)

        # check
        for position, color in (((0, 0), self.red), ((10, 10), self.black),
                                ((20, 20), self.black), ((40, 40), self.red)):
            fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0],
                                                  position[1])
            assert fetched_color == color
Ejemplo n.º 8
0
 def test_bit_pattern(self):
     HEIGHT = WIDTH = 10
     fmt = self.screen.c_format
     white = RSDL.MapRGB(fmt, 255, 255, 255)
     black = RSDL.MapRGB(fmt, 0, 0, 0)
     RSDL.LockSurface(self.screen)
     for i in xrange(WIDTH):
         for j in xrange(HEIGHT):
             k = j*WIDTH + i
             if k % 2:
                 c = white
             else:
                 c = black
             RSDL_helper.set_pixel(self.screen, i, j, c)
     RSDL.UnlockSurface(self.screen)
     RSDL.Flip(self.screen)
     self.check("Upper left corner 10x10 field with vertical black/white stripes")
 def create_screen(self):
     if constants.USE_RSDL:
         self.screen = RSDL.SetVideoMode(self.width*self.scale, self.height*self.scale, 32, 0)
         fmt = self.screen.c_format
         self.colors = []
         for color in self.COLOR_MAP:
             color = RSDL.MapRGB(fmt, *color)
             self.colors.append(color)
         self.blit_rect = RSDL_helper.mallocrect(0, 0, self.scale, self.scale)
Ejemplo n.º 10
0
    def test_set_color_key(self):
        # prepare
        fillrect = RSDL_helper.mallocrect(10, 10, 30, 30)
        RSDL.FillRect(self.src_surf, fillrect, self.blue)
        lltype.free(fillrect, flavor='raw')
        assert RSDL.SetColorKey(self.src_surf, RSDL.SRCCOLORKEY, self.blue) == 0

        # draw
        RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 0, 0)

        # check
        for position, color in (
                (( 0, 0), self.red),
                ((10,10), self.black),
                ((20,20), self.black),
                ((40,40), self.red)
            ):
            fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1])
            assert fetched_color == color
Ejemplo n.º 11
0
    def test_set_alpha(self):
        # prepare
        assert RSDL.SetAlpha(self.src_surf, RSDL.SRCALPHA, 128) == 0

        # draw
        RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 10, 10)
        RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 20, 20)

        # check
        for position, color in (
            ((0, 0), (0, 0, 0)),  # no rect
            ((10, 10), (127, 0, 0)),  # one rect
            ((20, 20), (191, 0, 0))  # two overlapping rects
        ):
            fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0],
                                                  position[1])
            assert RSDL_helper.get_rgb(fetched_color,
                                       self.dst_surf.c_format) == color
Ejemplo n.º 12
0
def test_image_pixels():
    for filename in ["demo.jpg", "demo.png"]:
        image = RIMG.Load(os.path.join(autopath.this_dir, filename))
        assert image
        assert rffi.getintfield(image.c_format, 'c_BytesPerPixel') in (3, 4)
        RSDL.LockSurface(image)
        result = {}
        try:
            rgb = lltype.malloc(rffi.CArray(RSDL.Uint8), 3, flavor='raw')
            try:
                for y in range(23):
                    for x in range(y % 13, 17, 13):
                        color = RSDL_helper.get_pixel(image, x, y)
                        RSDL.GetRGB(color,
                                    image.c_format,
                                    rffi.ptradd(rgb, 0),
                                    rffi.ptradd(rgb, 1),
                                    rffi.ptradd(rgb, 2))
                        r = rffi.cast(lltype.Signed, rgb[0])
                        g = rffi.cast(lltype.Signed, rgb[1])
                        b = rffi.cast(lltype.Signed, rgb[2])
                        result[x, y] = r, g, b
            finally:
                lltype.free(rgb, flavor='raw')
        finally:
            RSDL.UnlockSurface(image)
        RSDL.FreeSurface(image)
        for x, y in result:
            f = (x*17 + y*23) / float(17*17+23*23)
            expected_r = int(255.0 * (1.0-f))
            expected_g = 0
            expected_b = int(255.0 * f)
            r, g, b = result[x, y]
            assert abs(r-expected_r) < 10
            assert abs(g-expected_g) < 10
            assert abs(b-expected_b) < 10
Ejemplo n.º 13
0
    def test_set_alpha(self):
        # prepare
        assert RSDL.SetAlpha(self.src_surf, RSDL.SRCALPHA, 128) == 0

        # draw
        RSDL_helper.blit_complete_surface(
            self.src_surf,
            self.dst_surf,
            10, 10)
        RSDL_helper.blit_complete_surface(
            self.src_surf,
            self.dst_surf,
            20, 20)

        # check
        for position, color in (
                (( 0, 0), (  0,0,0)), # no rect
                ((10,10), (127,0,0)), # one rect
                ((20,20), (191,0,0))  # two overlapping rects
            ):
            fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1])
            assert RSDL_helper.get_rgb(fetched_color, self.dst_surf.c_format) == color 
Ejemplo n.º 14
0
 def draw_pixels(self):
     for x in range(self.width):
         for y in range(self.height):
             RSDL_helper.set_pixel(screen, x, y, self.get_pixel_color(x, y))
Ejemplo n.º 15
0
    def __init__(self):
        start = float(time.time())
        self._neurons = []
        self._columns = []
        self._layers = [[]] * LAYERS
        self._pulse_layers = [0] * LAYERS
        self._pulse_layers[0] = 1

        self._pulse_columns = [0] * COLUMNS
        self._pulse_columns[0] = 1
        self._pulse_columns[1] = 1
        self._pulse_columns[2] = 1
        self._pulse_columns[3] = 1

        inc = 360.0 / COLUMNS
        scale = float(LAYERS)
        expansion = 1.333
        linc = scale / LAYERS
        for column in range(COLUMNS):
            colNeurons = []
            self._columns.append(colNeurons)
            X = math.sin(radians(column * inc))
            Y = math.cos(radians(column * inc))
            expanding = STEM
            width = 1.0 / scale
            for layer in range(LAYERS):
                Z = layer * linc
                r = random() * random()
                g = 0.2
                b = 0.2
                for i in range(int(expanding)):
                    x = uniform(-width, width)
                    rr = random() * random()  # DJ's trick
                    y = uniform(-width * rr, width * rr) + X
                    z = Z + Y
                    # create 50/50 exitatory/inhibitory
                    n = RecurrentSpikingModel(x=x,
                                              y=y,
                                              z=z,
                                              column=column,
                                              layer=layer,
                                              red=r,
                                              green=g,
                                              blue=b)
                    self._neurons.append(n)
                    colNeurons.append(n)
                    self._layers[layer].append(n)

                expanding *= expansion
                width *= expansion

        dendrites = 0
        interlayer = 0
        for lay in self._layers:
            for a in lay:
                for b in lay:
                    if a is not b and a._column == b._column:
                        a.attach_dendrite(b)
                        dendrites += 1
                        interlayer += 1

        intercol = 0
        for col in self._columns:
            for a in col:
                for b in col:
                    if a is not b and random() * random() > 0.75:
                        a.attach_dendrite(b)
                        intercol += 1
                        dendrites += 1

        intercore = 0
        core = self._layers[-1]
        for a in core:
            for b in core:
                if a is not b and random() * random() > 0.85:
                    a.attach_dendrite(b)
                    intercore += 1
                    dendrites += 1

        print 'brain creation time (seconds)', float(time.time()) - start
        print 'neurons per column', len(self._columns[0])
        print 'inter-layer dendrites', interlayer
        print 'inter-column dendrites', intercol
        print 'inter-neocoretex dendrites', intercore
        print 'total dendrites', dendrites
        print 'total neurons', len(self._neurons)
        for i, lay in enumerate(self._layers):
            print 'layer: %s	neurons: %s' % (i, len(lay))
        for i, col in enumerate(self._columns):
            print 'column: %s	neurons: %s' % (i, len(col))

        self._stdin = streamio.fdopen_as_stream(0, 'r', 1)
        #self._stdout = streamio.fdopen_as_stream(1, 'w', 1)
        #self._stderr = streamio.fdopen_as_stream(2, 'w', 1)

        self._width = 640
        self._height = 480
        assert RSDL.Init(RSDL.INIT_VIDEO) >= 0
        self.screen = RSDL.SetVideoMode(self._width, self._height, 32, 0)
        assert self.screen
        fmt = self.screen.c_format
        self.ColorWhite = white = RSDL.MapRGB(fmt, 255, 255, 255)
        self.ColorGrey = grey = RSDL.MapRGB(fmt, 128, 128, 128)
        self.ColorBlack = black = RSDL.MapRGB(fmt, 0, 0, 0)
        self.ColorBlue = blue = RSDL.MapRGB(fmt, 0, 0, 200)

        colors = {'white': white, 'grey': grey, 'black': black, 'blue': blue}

        x = 1
        y = 1
        for i, n in enumerate(self._neurons):
            braneRect = RSDL_helper.mallocrect(x, y, 12, 12)
            groupRect = RSDL_helper.mallocrect(x, y, 12, 2)
            spikeRect = RSDL_helper.mallocrect(x + 4, y + 4, 4, 4)
            n.setup_draw(self.screen.c_format, braneRect, groupRect, spikeRect,
                         colors)
            x += 13
            if x >= self._width - 14:
                x = 1
                y += 13
Ejemplo n.º 16
0
def black(screen, cola, colb):
    for i in xrange(WIDTH):
        for j in xrange(HEIGHT):
            RSDL_helper.set_pixel(screen, i, j, cola)
	def __init__(self):
		start = float(time.time())
		self._neurons = []
		self._columns = []
		self._layers = [ [] ] * LAYERS
		self._pulse_layers = [0] * LAYERS
		self._pulse_layers[ 0 ] = 1

		self._pulse_columns = [0] * COLUMNS
		self._pulse_columns[ 0 ] = 1
		self._pulse_columns[ 1 ] = 1
		self._pulse_columns[ 2 ] = 1
		self._pulse_columns[ 3 ] = 1


		inc = 360.0 / COLUMNS
		scale = float( LAYERS )
		expansion = 1.333
		linc = scale / LAYERS
		for column in range(COLUMNS):
			colNeurons = []
			self._columns.append( colNeurons )
			X = math.sin( radians(column*inc) )
			Y = math.cos( radians(column*inc) )
			expanding = STEM
			width = 1.0 / scale
			for layer in range(LAYERS):
				Z = layer * linc
				r = random() * random()
				g = 0.2
				b = 0.2
				for i in range(int(expanding)):
					x = uniform( -width, width )
					rr = random()*random()		# DJ's trick
					y = uniform( -width*rr, width*rr ) + X
					z = Z + Y
					# create 50/50 exitatory/inhibitory
					n = RecurrentSpikingModel(x=x, y=y, z=z, column=column, layer=layer, red=r, green=g, blue=b )
					self._neurons.append( n )
					colNeurons.append( n )
					self._layers[ layer ].append( n )

				expanding *= expansion
				width *= expansion

		dendrites = 0
		interlayer = 0
		for lay in self._layers:
			for a in lay:
				for b in lay:
					if a is not b and a._column == b._column:
						a.attach_dendrite( b )
						dendrites += 1
						interlayer += 1

		intercol = 0
		for col in self._columns:
			for a in col:
				for b in col:
					if a is not b and random()*random() > 0.75:
						a.attach_dendrite( b )
						intercol += 1
						dendrites += 1

		intercore = 0
		core = self._layers[-1]
		for a in core:
			for b in core:
				if a is not b and random()*random() > 0.85:
					a.attach_dendrite( b )
					intercore += 1
					dendrites += 1

		print 'brain creation time (seconds)', float(time.time())-start
		print 'neurons per column', len(self._columns[0])
		print 'inter-layer dendrites', interlayer
		print 'inter-column dendrites', intercol
		print 'inter-neocoretex dendrites', intercore
		print 'total dendrites', dendrites
		print 'total neurons', len(self._neurons)
		for i,lay in enumerate(self._layers):
			print 'layer: %s	neurons: %s' %(i,len(lay))
		for i,col in enumerate(self._columns):
			print 'column: %s	neurons: %s' %(i,len(col))



		self._stdin = streamio.fdopen_as_stream(0, 'r', 1)
		#self._stdout = streamio.fdopen_as_stream(1, 'w', 1)
		#self._stderr = streamio.fdopen_as_stream(2, 'w', 1)

		self._width = 640; self._height = 480
		assert RSDL.Init(RSDL.INIT_VIDEO) >= 0
		self.screen = RSDL.SetVideoMode(self._width, self._height, 32, 0)
		assert self.screen
		fmt = self.screen.c_format
		self.ColorWhite = white = RSDL.MapRGB(fmt, 255, 255, 255)
		self.ColorGrey = grey = RSDL.MapRGB(fmt, 128, 128, 128)
		self.ColorBlack = black = RSDL.MapRGB(fmt, 0, 0, 0)
		self.ColorBlue = blue = RSDL.MapRGB(fmt, 0, 0, 200)

		colors = {'white':white, 'grey':grey, 'black':black, 'blue':blue}

		x = 1; y = 1
		for i,n in enumerate(self._neurons):
			braneRect = RSDL_helper.mallocrect(x, y, 12, 12)
			groupRect = RSDL_helper.mallocrect(x, y, 12, 2)
			spikeRect = RSDL_helper.mallocrect(x+4, y+4, 4, 4)
			n.setup_draw( self.screen.c_format, braneRect, groupRect, spikeRect, colors )
			x += 13
			if x >= self._width-14:
				x = 1
				y += 13