Exemplo n.º 1
0
 def __init__(self, pixel_map):
     """Initialize new predictor instance with actual pixel map and build codes.
     
     -- pixel_map - original pixel map used for calculation
     """
     self.original_pixel_map = pixel_map
     self.codes = PixelMap(pixel_map.height, pixel_map.width)
     self.build_codes()
Exemplo n.º 2
0
class Predictor(ABC):
    """Abstract class for prediction calculation of the whole pixel map."""

    def __init__(self, pixel_map):
        """Initialize new predictor instance with actual pixel map and build codes.
        
        -- pixel_map - original pixel map used for calculation
        """
        self.original_pixel_map = pixel_map
        self.codes = PixelMap(pixel_map.height, pixel_map.width)
        self.build_codes()

    def build_codes(self):
        """Build pixel map of predictions using methods for calculating each color ingredient."""
        for row in range(self.codes.height):
            for col in range(self.codes.width):
                self.codes.new_pixel(
                    row, col, 
                    self._get_prediction(row, col, get_red),
                    self._get_prediction(row, col, get_green),
                    self._get_prediction(row, col, get_blue)
                )

    def _get_prediction(self, row, column, color_extractor):
        """Calculate and return prediction on the specific coordinates.
        
        -- row - y-coordinate
        -- column - x-coordinate
        -- color_extractor -- function to extract specific color ingredient value
        """
        n = color_extractor(self.n(row, column))
        w = color_extractor(self.w(row, column))
        nw = color_extractor(self.nw(row, column))
        return color_extractor(self.original_pixel_map[row, column]) - self.get_prediction(n, w, nw)

    @abstractmethod
    def get_prediction(self, n, w, nw):
        """Calculate and return predictor according to standard.
        
        -- n - pixel above the current
        -- w - pixel on the left of the current
        -- nw -- pixel on the up-left of the current
        """
        pass

    def n(self, row, col):
        """Return pixel on the north of the considered one."""
        return self.original_pixel_map[row-1, col]

    def w(self, row, col):
        """Return pixel on the west of the considered one."""
        return self.original_pixel_map[row, col-1]

    def nw(self, row, col):
        """Return pixel on the north-west of the considered one."""
        return self.original_pixel_map[row-1, col-1]
Exemplo n.º 3
0
    def __init__(self, ship, orientation):

        w = ship.w
        h = ship.h

        PixelMap.__init__(self, (w, h))
        self.ship = ship

        self.orientation = orientation
        self.run_function = self.assign_run_function()
Exemplo n.º 4
0
    def __init__(self, ship, map, points, edges):

        w = len(map[0])
        h = len(map)
        PixelMap.__init__(self, (w, h))
        self.ship = ship

        self.map = self.copy_map(map)
        self.points = points.copy()
        self.edges = edges.copy()
Exemplo n.º 5
0
    def __init__(self, parent, chunk_dict, num):

        PixelMap.__init__(self, (parent.w, parent.h))

        self.parent = parent
        self.copy(parent)

        self.chunk_dict = chunk_dict
        self.num = num
        self.chunk_ids = range(1, num + 1)

        self.chunks = self.get_chunks()
        self.centers = self.get_chunk_centers()
Exemplo n.º 6
0
    def __init__(self, base_map):

        self.base_map = base_map
        w = self.base_map.w + 4
        h = self.base_map.h + 4
        PixelMap.__init__(self, (w, h), colorkey=True)
        self.fill_color = WHITE

        self.silhouette = set()

        self.set_scan_outline()

        self.update_image()
    def __init__(self, ship):

        self.ship = ship
        w = self.ship.w + 4
        h = self.ship.h + 4
        PixelMap.__init__(self, (w, h), colorkey=True)
        self.fill_color = WHITE

        self.silhouette = set()

        self.set_scan_outline()

        self.trace = self.set_trace()

        self.update_image()
Exemplo n.º 8
0
def read_image(file, height, width):
    """Read image from the file.

    Create PixelMap and store there read pixels from the file stream.

    -- file - opende file stream to read from
    -- height - height of the image in pixels
    -- width - width of the image in pixels
    """
    pixel_map = PixelMap(height, width)
    for row in range(height-1, -1, -1):
        for col in range(width):
            blue = file.read(1)[0]
            green = file.read(1)[0]
            red = file.read(1)[0]
            pixel_map.new_pixel(row, col, red, green, blue)
    
    return pixel_map