Beispiel #1
0
    def fromImage(self, img, tolerance=2, max_depth=5, debugOutput=False):

        self.ow = img.getXSize()
        self.oh = img.getYSize()
        w, h = self.ow, self.oh

        expand = None
        if (w & (w - 1) != 0) or (h & (h - 1) != 0):
            # resize to the next power of 2
            w2 = self.next_pow_2(w)[0] if w & (w - 1) != 0 else w
            h2 = self.next_pow_2(h)[0] if h & (h - 1) != 0 else h
            self.log.debug('resizing to %i, %i' % (w2, h2))
            img.expandBorder(0, w2 - w, h2 - h, 0, VBase4D(0, 0, 0, 0))
            expand = (w2 - w, h2 - h)
            w, h = w2, h2

        self._reset(w, h, max_depth)

        image_array = array.array('f')
        for j in xrange(h):
            for i in xrange(w):
                el = img.getXel(i, j)
                image_array.append(255.0 * el[0])
                image_array.append(255.0 * el[1])
                image_array.append(255.0 * el[2])

        self.log.debug('subdividing nodes...%s' % time.asctime())
        self.subdv(self.root, image_array, 3 * w, tolerance, 0, 0, w, h, 0,
                   None)
        self.log.debug('done %s' % time.asctime())

        if expand is not None:
            img.expandBorder(0, -expand[0], -expand[1], 0, VBase4D(0, 0, 0, 0))
Beispiel #2
0
    def _createMapTextureCard(self):
        mapImage = PNMImage(MAP_RESOLUTION, MAP_RESOLUTION)
        mapImage.fill(*self._bgColor)
        fgColor = VBase4D(*self._fgColor)
        for x in range(self._mazeHeight):
            for y in range(self._mazeWidth):
                if self._mazeCollTable[y][x] == 1:
                    ax = float(x) / self._mazeWidth * MAP_RESOLUTION
                    invertedY = self._mazeHeight - 1 - y
                    ay = float(invertedY) / self._mazeHeight * MAP_RESOLUTION
                    self._drawSquare(mapImage, int(ax), int(ay), 10, fgColor)

        mapTexture = Texture('mapTexture')
        mapTexture.setupTexture(Texture.TT2dTexture, self._maskResolution,
                                self._maskResolution, 1, Texture.TUnsignedByte,
                                Texture.FRgba)
        mapTexture.setMinfilter(Texture.FTLinear)
        mapTexture.load(mapImage)
        mapTexture.setWrapU(Texture.WMClamp)
        mapTexture.setWrapV(Texture.WMClamp)
        mapImage.clear()
        del mapImage
        cm = CardMaker('map_cardMaker')
        cm.setFrame(-1.0, 1.0, -1.0, 1.0)
        map = self.attachNewNode(cm.generate())
        map.setTexture(mapTexture, 1)
        return map
Beispiel #3
0
 def _revealSmoothCircle(self, x, y, center):
     length = (Vec2(x, y) - center).length()
     goalAlpha = max(0.0, length / float(self._radius) - 0.5)
     self._maskImage.setXelA(
         x, y,
         VBase4D(0.0, 0.0, 0.0,
                 min(self._maskImage.getAlpha(x, y), goalAlpha * 2.0)))
Beispiel #4
0
	def update(self, pnm, texture_data, palettes):
		from pandac.PandaModules import PNMImage, VBase4D
		from pandac.PandaModules import Texture as P3DTexture
		
		self.palettes = []
		temp = []
		for x in range (16):
			temp.append((x, x, x, 1))
			
		self.palettes.append(temp)
		
		for y, palette in enumerate(palettes):
			selfpalette = []
			for x, color in enumerate(palette.colors.colors):
				selfpalette.append((color[0],color[1],color[2],1))
			self.palettes.append(selfpalette)
		
		i = 0
		for palette in self.palettes:
			colors = []
			for color in palette:
				color_list = [c / 15.0 for c in color[:3]]
				color_list.append(0 if color == (0, 0, 0, 0) else 1)
				colors.append(VBase4D(*color_list))
				
			for y in range(1024):
				row = texture_data.image[y]
				for x in range(256):
					pnm.setXelA(x + (256*i), y, colors[row[x]])	
			i += 1
			
		self.texture = P3DTexture()
		self.texture.load(pnm)
		self.texture.setMagfilter(P3DTexture.FTNearest)
		self.texture.setMinfilter(P3DTexture.FTLinear)	
Beispiel #5
0
	def from_data(self, texture_data, palettes):
		from pandac.PandaModules import PNMImage, VBase4D
		from pandac.PandaModules import Texture as P3DTexture
		
		tex_pnm = PNMImage(17*256, 1024)
		tex_pnm.addAlpha()

		
		testpnm = PNMImage(256, 1024)
		
		palette = [(x, x, x, 1) for x in range(16)]
		colors = []
		for color in palette:
			color_list = [c / 15.0 for c in color[:3]]
			color_list.append(0 if color == (0, 0, 0, 0) else 1)
			colors.append(VBase4D(*color_list))
			
		for y in range(1024):
			row = texture_data.image[y]
			for x in range(256):
				testpnm.setXelA(x, y, colors[row[x]])
		
		self.texture2 = P3DTexture()
		self.texture2.load(testpnm)
		self.texture2.setMagfilter(P3DTexture.FTNearest)
		self.texture2.setMinfilter(P3DTexture.FTLinear)
		
		
		self.update(tex_pnm, texture_data, palettes)
    def __init__(self):
        self.editTexture = None
        self.editModel = None

        self.texturePainterStatus = TEXTURE_PAINTER_STATUS_DISABLED

        self.paintColor = VBase4D(1, 1, 1, 1)
        self.paintSize = 10
        self.paintEffect = PNMBrush.BEBlend
        self.paintSmooth = True
        self.paintMode = TEXTUREPAINTER_FUNCTION_PAINT_POINT

        self.painter = None
Beispiel #7
0
    def _createMapTextureCard(self):
        """
        This will return a NodePath with a card textured with the minimap.  The
        minimap texture is dynamically created from the map data.
        """
        # create and fill empty map image
        mapImage = PNMImage(MAP_RESOLUTION, MAP_RESOLUTION)
        blockFiles = []
        for i in range(5):
            blockFiles.append(PNMImage())
            #blockFiles[i].read(Filename("mapBlock%i.jpg"%(i+1)))
            # TODO:maze either reference a set of textures for each piece or fill with color
            blockFiles[i].read(Filename('phase_4/maps/male_sleeve4New.jpg'))
        mapImage.fill(0.8, 0.8, 0.8)

        # iterate through the map data and place a block in the map image where appropriate
        for x in range( len(self._mazeLayout[0]) ):
            for y in range( len(self._mazeLayout) ):
                if self._mazeLayout[y][x]:
                    ax = float(x)/len(self._mazeLayout[0]) * MAP_RESOLUTION
                    ay = float(y)/len(self._mazeLayout) * MAP_RESOLUTION

                    #TODO:maze use different blocks for different wall types or items
                    #mapImage.copySubImage(random.choice(blockFiles), int(ax), int(ay), 20, 20, 32, 32)

                    #TODO:maze find the ideal block texture size for the map so we dont
                    #          have to do this strange offset
                    #mapImage.copySubImage(blockFiles[0], int(ax), int(ay), 0, 0, 32, 32)
                    self._drawSquare(mapImage, int(ax), int(ay), 10, VBase4D(0.5, 0.5, 0.5, 1.0))

        # create a texture from the map image
        mapTexture = Texture("mapTexture")
        mapTexture.setupTexture(Texture.TT2dTexture, self._maskResolution, self._maskResolution, 1, Texture.TUnsignedByte, Texture.FRgba)
        mapTexture.setMinfilter(Texture.FTLinear)
        mapTexture.load(mapImage)
        mapTexture.setWrapU(Texture.WMClamp)
        mapTexture.setWrapV(Texture.WMClamp)

        mapImage.clear()
        del mapImage

        # put the texture on a card and return it
        cm = CardMaker("map_cardMaker")
        cm.setFrame(-1.0,1.0,-1.0,1.0)
        map = self.attachNewNode(cm.generate())
        map.setTexture(mapTexture, 1)
        return map
Beispiel #8
0
 def _revealSquare(self, x, y, center):
     self._maskImage.setXelA(x, y, VBase4D(0, 0, 0, 0))
Beispiel #9
0
 def _revealHardCircle(self, x, y, center):
     length = (Vec2(x, y) - center).length()
     if length <= self._radius:
         self._maskImage.setXelA(x, y, VBase4D(0, 0, 0, 0))
Beispiel #10
0
	def import_(self, file_name, palettes):
		from pandac.PandaModules import PNMImage, Filename, VBase4D
		from pandac.PandaModules import Texture as P3DTexture
		
		pnm = PNMImage()
		pnm.read(Filename.fromOsSpecific(file_name))
		
		tex_pnm = PNMImage(17*256, 1024)
		tex_pnm.addAlpha()
		
		
		#convert data to same sequence as files
		texdata = []
		for y in range(1024):
			row = []
			for x in range(256):
				gray = pnm.getXel(x, y)
				pal_i = int(gray[0] * 15.0)
				row.append(pal_i)
			texdata.append(row)
		
		#update saving texture
		testpnm = PNMImage(256, 1024)
		
		palette = [(x, x, x, 1) for x in range(16)]
		colors = []
		for color in palette:
			color_list = [c / 15.0 for c in color[:3]]
			color_list.append(0 if color == (0, 0, 0, 0) else 1)
			colors.append(VBase4D(*color_list))
			
		for y in range(1024):
			row = texdata[y]
			for x in range(256):
				testpnm.setXelA(x, y, colors[row[x]])
		
		self.texture2.load(testpnm)
		self.texture2.setMagfilter(P3DTexture.FTNearest)
		self.texture2.setMinfilter(P3DTexture.FTLinear)

		
		#update texture visible on map


		self.palettes = []
		temp = []
		for x in range (16):
			temp.append((x, x, x, 1))
			
		self.palettes.append(temp)
		
		for y, palette in enumerate(palettes):
			selfpalette = []
			for x, color in enumerate(palette.colors.colors):
				selfpalette.append((color[0],color[1],color[2],1))
			self.palettes.append(selfpalette)
		
		i = 0
		for palette in self.palettes:
			colors = []
			for color in palette:
				color_list = [c / 15.0 for c in color[:3]]
				color_list.append(0 if color == (0, 0, 0, 0) else 1)
				colors.append(VBase4D(*color_list))
				
			for y in range(1024):
				row = texdata[y]
				for x in range(256):
					tex_pnm.setXelA(x + (256*i), y, colors[row[x]])	
			i += 1
			
		self.texture.load(tex_pnm)
		self.texture.setMagfilter(P3DTexture.FTNearest)
		self.texture.setMinfilter(P3DTexture.FTLinear)	
    def __paintPixel(self, x, y):
        ''' paint at x/y with the defined settings '''

        imageMaxX = self.editImage.getXSize()
        imageMaxY = self.editImage.getYSize()

        def inImage(x, y):
            ''' is the given x/y position within the image '''
            return ((imageMaxX > x >= 0) and (imageMaxY > y >= 0))

        # how smooth should be painted
        if self.paintSmooth:
            # a smooth brush
            hardness = 1.0
        else:
            # a hard brush
            hardness = 0.1
        hardness = min(1.0, max(0.05, hardness))

        # the paint radius
        radius = int(round(self.paintSize / 2.0))
        radiusSquare = float(radius * radius)

        # a function to get the brush color/strength, depending on the radius
        def getBrushColor(diffPosX, diffPosY):
            distance = diffPosX**2 + diffPosY**2
            brushStrength = (
                1 - (min(distance, radiusSquare) / radiusSquare)) / hardness
            return min(1.0, max(0.0, brushStrength))

        if inImage(x, y):
            if self.paintMode == TEXTUREPAINTER_FUNCTION_PAINT_POINT:
                if self.paintEffect in [
                        PNMBrush.BESet, PNMBrush.BEBlend, PNMBrush.BEDarken,
                        PNMBrush.BELighten
                ]:
                    # render a spot into the texture
                    self.painter.drawPoint(x, y)

                elif self.paintEffect in [
                        TEXTUREPAINTER_BRUSH_FLATTEN,
                        TEXTUREPAINTER_BRUSH_SMOOTH,
                        TEXTUREPAINTER_BRUSH_RANDOMIZE
                ]:

                    if self.paintEffect == TEXTUREPAINTER_BRUSH_SMOOTH:
                        # calculate average values
                        data = dict()
                        smoothRadius = 2
                        for dx in xrange(-radius, radius + 1):
                            for dy in xrange(-radius, radius + 1):
                                if inImage(x + dx, y + dy):
                                    average = VBase4D(0)
                                    dividor = 0
                                    for px in xrange(-smoothRadius,
                                                     smoothRadius + 1):
                                        for py in xrange(
                                                -smoothRadius,
                                                smoothRadius + 1):
                                            if inImage(x + dx + px,
                                                       y + dy + py):
                                                average += self.editImage.getXelA(
                                                    x + dx + px, y + dy + py)
                                                dividor += 1
                                    average /= float(dividor)
                                    data[(x + dx, y + dy)] = average

                        # save to image
                        for (px, py), newValue in data.items():
                            currentValue = self.editImage.getXelA(px, py)
                            diffValue = currentValue - newValue

                            dx = px - x
                            dy = py - y

                            multiplier = getBrushColor(dx, dy)
                            print dx, dy, multiplier
                            '''if self.paintSmooth:
                multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy))) / (radius*radius)
              else:
                # not sure if this is correct
                multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy)))'''
                            '''r = currentValue.getX() * (1-multiplier*self.paintColor.getX()) + diffValue.getX() * multiplier*self.paintColor.getX()
              g = currentValue.getY() * (1-multiplier*self.paintColor.getY()) + diffValue.getY() * multiplier*self.paintColor.getY()
              b = currentValue.getZ() * (1-multiplier*self.paintColor.getZ()) + diffValue.getZ() * multiplier*self.paintColor.getZ()
              a = currentValue.getW() * (1-multiplier*self.paintColor.getW()) + diffValue.getW() * multiplier*self.paintColor.getW()'''
                            r = currentValue.getX(
                            ) - multiplier * diffValue.getX()
                            g = currentValue.getY(
                            ) - multiplier * diffValue.getY()
                            b = currentValue.getZ(
                            ) - multiplier * diffValue.getZ()
                            a = currentValue.getW(
                            ) - multiplier * diffValue.getW()
                            if self.editImage.hasAlpha():
                                self.editImage.setXelA(px, py,
                                                       VBase4D(r, g, b, a))
                            else:
                                self.editImage.setXel(px, py, VBase3D(r, g, b))

                            #self.editImage.setXelA(x,y,value)

                    if self.paintEffect == TEXTUREPAINTER_BRUSH_FLATTEN:
                        dividor = 0
                        average = VBase4D(0)
                        for dx in xrange(-radius, radius + 1):
                            for dy in xrange(-radius, radius + 1):
                                if inImage(x + dx, y + dy):
                                    multiplier = getBrushColor(dx, dy)
                                    '''if self.paintSmooth:
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy))) / (radius*radius)
                  else:
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy)))'''
                                    dividor += multiplier
                                    average += self.editImage.getXelA(
                                        x + dx, y + dy) * multiplier
                        average /= dividor
                        for dx in xrange(-radius, radius + 1):
                            for dy in xrange(-radius, radius + 1):
                                if inImage(x + dx, y + dy):
                                    multiplier = getBrushColor(dx, dy)
                                    '''if self.paintSmooth:
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy))) / (radius*radius)
                  else:
                    # not sure if this is correct
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy)))'''
                                    currentValue = self.editImage.getXelA(
                                        x + dx, y + dy)
                                    r = currentValue.getX() * (
                                        1 -
                                        multiplier * self.paintColor.getX()
                                    ) + average.getX(
                                    ) * multiplier * self.paintColor.getX()
                                    g = currentValue.getY() * (
                                        1 -
                                        multiplier * self.paintColor.getY()
                                    ) + average.getY(
                                    ) * multiplier * self.paintColor.getY()
                                    b = currentValue.getZ() * (
                                        1 -
                                        multiplier * self.paintColor.getZ()
                                    ) + average.getZ(
                                    ) * multiplier * self.paintColor.getZ()
                                    a = currentValue.getW() * (
                                        1 -
                                        multiplier * self.paintColor.getW()
                                    ) + average.getW(
                                    ) * multiplier * self.paintColor.getW()
                                    if self.editImage.hasAlpha():
                                        self.editImage.setXelA(
                                            x + dx, y + dy,
                                            VBase4D(r, g, b, a))
                                    else:
                                        self.editImage.setXel(
                                            x + dx, y + dy, VBase3D(r, g, b))

                    elif self.paintEffect == TEXTUREPAINTER_BRUSH_RANDOMIZE:
                        for dx in xrange(-radius, radius + 1):
                            for dy in xrange(-radius, radius + 1):
                                if inImage(x + dx, y + dy):
                                    r = VBase4D(
                                        random.random() *
                                        self.paintColor.getX() -
                                        self.paintColor.getX() / 2.,
                                        random.random() *
                                        self.paintColor.getY() -
                                        self.paintColor.getY() / 2.,
                                        random.random() *
                                        self.paintColor.getZ() -
                                        self.paintColor.getZ() / 2.,
                                        random.random() *
                                        self.paintColor.getW() -
                                        self.paintColor.getW() / 2.)
                                    multiplier = getBrushColor(dx, dy)
                                    '''if self.paintSmooth:
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy))) / (radius*radius)
                  else:
                    # not sure if this is correct
                    multiplier = ((radius-math.fabs(dx))*(radius-math.fabs(dy)))'''
                                    currentValue = self.editImage.getXelA(
                                        x + dx, y + dy)
                                    self.editImage.setXelA(
                                        x + dx, y + dy,
                                        currentValue + r * multiplier)

            elif self.paintMode == TEXTUREPAINTER_FUNCTION_READ:
                if inImage(x, y):
                    col = self.editImage.getXelA(x, y)
                    if self.editImage.hasAlpha():
                        self.paintColor = VBase4D(col[0], col[1], col[2],
                                                  col[3])
                    else:
                        self.paintColor = VBase4D(col[0], col[1], col[2], 1.0)
                    messenger.send(EVENT_TEXTUREPAINTER_BRUSHCHANGED)

            elif self.paintMode == TEXTUREPAINTER_FUNCTION_PAINT_LINE:
                if self.lastPoint != None:
                    self.painter.drawLine(x, y, self.lastPoint[0],
                                          self.lastPoint[1])

            elif self.paintMode == TEXTUREPAINTER_FUNCTION_PAINT_RECTANGLE:
                if self.lastPoint != None:
                    self.painter.drawRectangle(x, y, self.lastPoint[0],
                                               self.lastPoint[1])

            self.lastPoint = (x, y)