Ejemplo n.º 1
0
 def read_ram(self, filename):
     # Check file size
     with open(filename, 'rb') as fp:
         file_size = os.fstat(fp.fileno()).st_size
         if file_size != 0x4000:
             raise errors.FileFormatError(file_size, size=0x4000)
         mem = ppu_memory.PpuMemory()
         mem.allocate_num_pages(2)
         # Read CHR from $0000-$2000
         mem.chr_set = chr_data.ChrBank.from_binary(fp.read(0x2000))
         # TODO: Handle chr order (background / sprite at $0000 / $1000).
         # For each graphics page, read nametable & attribute.
         for loop_num in range(2):
             gfx = mem.gfx[0] if not loop_num else mem.gfx[1]
             if loop_num == 1:
                 # Skip $2400-$2c00
                 fp.read(0x800)
             # Read nametable from $2000-$23c0 & $2c00-$2fc0.
             # TODO: Handle mirroring.
             for y in range(30):
                 for x in range(32):
                     gfx.nametable[y][x] = asbyte(fp.read(1)[0])
             # Read attributes from $23c0-$2400 & $2fc0-$3000.
             for k in range(64):
                 byte = asbyte(fp.read(1)[0])
                 y = (k // 8) * 4
                 x = (k % 8) * 4
                 gfx.colorization[y + 0][x + 0] = (byte & 0x03)
                 gfx.colorization[y + 0][x + 2] = (byte & 0x0c) >> 2
                 if y < (7 * 4):
                     gfx.colorization[y + 2][x + 0] = (byte & 0x30) >> 4
                     gfx.colorization[y + 2][x + 2] = (byte & 0xc0) >> 6
         # Unused $3000-$3f00.
         unused = fp.read(0x0f00)
         # Read palette $3f00.
         mem.palette_nt = palette.Palette()
         mem.palette_spr = palette.Palette()
         # Two palettes, first for nametable, then for palette.
         bg_color = None
         pal = mem.palette_nt
         for loop_num in range(2):
             pal = mem.palette_nt if not loop_num else mem.palette_spr
             for k in range(4):
                 opt = [asbyte(p) for p in fp.read(4)]
                 if bg_color is None:
                     bg_color = opt[0]
                 pal.set_bg_color(bg_color)
                 pal.add(opt)
     return mem
Ejemplo n.º 2
0
 def read_valiant(self, filename):
     import gen.valiant_pb2 as valiant
     fp = open(filename, 'rb')
     content = fp.read()
     fp.close()
     mem = ppu_memory.PpuMemory()
     mem.allocate_num_pages(2)
     obj_file = valiant.ObjectFile()
     obj_file.ParseFromString(content)
     binary_map = {}
     for packet in obj_file.body.packets:
         if packet.role == valiant.CHR:
             binary_map['chr'] = packet.binary
         if packet.role == valiant.NAMETABLE:
             binary_map['nametable'] = packet.binary
         if packet.role == valiant.ATTRIBUTE:
             binary_map['attribute'] = packet.binary
         if packet.role == valiant.PALETTE:
             binary_map['palette'] = packet.binary
     chr_bin = self._expand_binary(binary_map['chr'])
     nt_bin = self._expand_binary(binary_map['nametable'])
     at_bin = self._expand_binary(binary_map['attribute'])
     pal_bin = self._expand_binary(binary_map['palette'])
     # Parse chr data.
     mem.chr_set = chr_data.ChrBank.from_binary(bytes(bytearray(chr_bin)))
     # Parse nametable data.
     for y in range(30):
         for x in range(32):
             mem.gfx[0].nametable[y][x] = nt_bin[y * 32 + x]
     # Parse attributes data.
     for a in range(64):
         p0 = (at_bin[a] >> 0) & 0x03
         p1 = (at_bin[a] >> 2) & 0x03
         p2 = (at_bin[a] >> 4) & 0x03
         p3 = (at_bin[a] >> 6) & 0x03
         y = (a // 8) * 4
         x = (a % 8) * 4
         mem.gfx[0].colorization[y + 0][x + 0] = p0
         mem.gfx[0].colorization[y + 0][x + 2] = p1
         if y < (7 * 4):
             mem.gfx[0].colorization[y + 2][x + 0] = p2
             mem.gfx[0].colorization[y + 2][x + 2] = p3
     # Parse palette data.
     pal = palette.Palette()
     pal.set_bg_color(pal_bin[0])
     for i in range(4):
         p = []
         for j in range(4):
             p.append(pal_bin[i * 4 + j])
         pal.add(p)
     mem.palette_nt = pal
     return mem
Ejemplo n.º 3
0
    def get_palette(self, possibilities):
        """Pick a single palette.

    Given list of possible palettes, just pick and build the first one.

    possibilities: List of possible palettes, must have at least one element.
    """
        (bg_color, color_set_collection) = possibilities[0]
        pal = palette.Palette()
        pal.set_bg_color(bg_color)
        for color_set in color_set_collection:
            pal.add(
                [bg_color] +
                sorted([c for c in color_set if c != bg_color], reverse=True))
        return pal
Ejemplo n.º 4
0
 def _build_palette(self):
     cols = self.width // self.unit_size
     rows = self.height // self.unit_size
     pal = palette.Palette()
     for y in range(1, rows - 1):
         p = []
         for x in range(1, cols - 1):
             nc = self._unit_nes_color(y, x)
             if nc != -1:
                 p.append(nc)
         if p:
             pal.set_bg_color(p[0])
             pal.add(p)
     pal.ensure_alignment()
     return pal
Ejemplo n.º 5
0
 def extract_palette(self, imgpal):
   pal = palette.Palette()
   for j in xrange(4):
     p = []
     for k in xrange(4):
       i = j * 4 + k
       nc = self._to_nescolor(imgpal.get(i))
       if nc == -1:
         raise errors.PaletteExtractionError('Color not found: %s' %
                                             imgpal.get(i))
       if k == 0 and pal.bg_color is None:
         pal.set_bg_color(nc)
       elif k == 0 and pal.bg_color != nc:
         raise errors.PaletteExtractionError(
           'Background color did not match: %s <> %s' %
           (pal.bg_color, nc))
       p.append(nc)
     pal.add(p)
   return pal
Ejemplo n.º 6
0
 def __init__(self, image, palette_choice):
     self.image = image.convert('RGB')
     # If you need this many symbols, you're doing pixel art wrong
     self.avail_symbols = list(":,.()-=!@#$%^&*+=?;<>~") + list(string.digits) + list(string.ascii_lowercase[::-1]) + list(string.ascii_uppercase[::-1])
     self.my_palette = palette.Palette(palette_choice)
Ejemplo n.º 7
0
 def setUpImage(self, redLevel, blueLevel):
     self._image = image.Image(self._imageWidth, self._imageHeight)
     # Load the palette to find colours
     self._colours = palette.Palette(self._maxIterations, self._image,
                                     redLevel, blueLevel)
Ejemplo n.º 8
0
from PIL import Image
import traceback
import legolize
import palette
import base64
from io import BytesIO
import simple_websocket

logger = utils.init_log()
app = Flask(__name__)
CORS(app)
HOST = os.environ['HOST']
DEBUG = os.environ.get('DEBUG', 'False')
sock = Sock(app)

pal = palette.Palette()

logger.info(f"palette loaded of {len(pal.colors)} colors")


@app.route('/upload/<size>', methods=['POST'])
def upload(size):
    uid = str(uuid.uuid4())
    input_name = utils.input_name(uid)

    file = request.files['file']

    file.save(input_name)

    image = image_utils.image_thumbnail(input_name)
    image.save(utils.thumb_name(uid))
Ejemplo n.º 9
0
 def test_bg_color_override_old(self):
     pal = palette.Palette()
     pal.set_bg_color(0x0f)
     pal.add([0x0f, 0x01])
     pal.set_bg_color(0x30)
     self.assertEqual(str(pal), 'P/30-01/')
Ejemplo n.º 10
0
 def test_bg_color_add_zero_bg_color_is_ignored(self):
     pal = palette.Palette()
     pal.set_bg_color(0x0f)
     pal.add([0x0f, 0x01, 0x02, 0x03])
     pal.add([0x00, 0x04, 0x05, 0x06])
     self.assertEqual(str(pal), 'P/0f-01-02-03/0f-04-05-06/')
Ejemplo n.º 11
0
 def test_bg_color_add_error_conflict(self):
     pal = palette.Palette()
     pal.set_bg_color(0x0f)
     with self.assertRaises(errors.PaletteBackgroundColorConflictError):
         pal.add([0x30, 0x01])
Ejemplo n.º 12
0
 def test_bg_color_add_error_missing(self):
     pal = palette.Palette()
     with self.assertRaises(errors.PaletteBackgroundColorMissingError):
         pal.add([0x30, 0x01])
Ejemplo n.º 13
0
 def test_bg_color_simple(self):
     pal = palette.Palette()
     pal.set_bg_color(0x0f)
     self.assertEqual(str(pal), 'P//')
     pal.add([0x0f])
     self.assertEqual(str(pal), 'P/0f/')
Ejemplo n.º 14
0
import image_utils
import legolize
import palette
import json

input_image = "smiling-emoji.jpg"
size = 20
print_backgrounds = False

thumbnail_image = "demo-thumbnail.png"
palette_point_file = "demo-palette-points.json"
css_palette = "palette.css"

pal = palette.Palette(COMPUTE_IMAGE=True)

image = image_utils.image_thumbnail(input_image, (300, 300))
image.save(thumbnail_image)

image = legolize.image(input_image)
max_len = legolize.max_len(image)

step = max_len // size

palette_point = []

new_size = None


def generating_events_new_size(ns):
    global new_size
    new_size = ns