Example #1
0
def run(snafile, imgfname, options):
    snapshot = get_snapshot(snafile)
    skool = BackToSkool(snapshot)
    width = 192
    eric = 210
    for address in options.mutable:
        skool.alter_skool_udgs(address)
    x = y = 0
    height = 21
    skool.hide_chars()

    if options.blackboard:
        bb = BLACKBOARDS[options.blackboard]
        width, height, x, y = bb['geometry']
        eric_x, eric_y = bb['location']
        skool.place_char(eric, eric_x, eric_y, 0)
    elif options.geometry:
        wh, xy = options.geometry.split('+', 1)
        width, height = [int(n) for n in wh.split('x')]
        x, y = [int(n) for n in xy.split('+')]

    for spec in options.text:
        board_id, sep, text = spec.partition(':')
        if sep:
            bb = BLACKBOARDS[board_id]
            skool.write(bb['tiles'], text)

    for spec in options.place_char:
        values = []
        for n in spec.split(','):
            try:
                values.append(int(n))
            except ValueError:
                values.append(None)
        skool.place_char(*values[:4])

    for spec in options.pokes:
        addr, val = spec.split(',', 1)
        step = 1
        if '-' in addr:
            addr1, addr2 = addr.split('-', 1)
            addr1 = int(addr1)
            if '-' in addr2:
                addr2, step = [int(i) for i in addr2.split('-', 1)]
            else:
                addr2 = int(addr2)
        else:
            addr1 = int(addr)
            addr2 = addr1
        addr2 += 1
        value = int(val)
        for a in range(addr1, addr2, step):
            snapshot[a] = value

    udg_array = skool.get_skool_udgs(x, y, width, height)
    frame = Frame(udg_array, options.scale)
    image_writer = ImageWriter()
    image_format = 'gif' if imgfname.lower()[-4:] == '.gif' else 'png'
    with open(imgfname, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #2
0
def _write_image(frame, img_file, animated):
    iw_options = {}
    if not animated:
        iw_options[PNG_ENABLE_ANIMATION] = 0
    image_writer = ImageWriter(options=iw_options)
    with open(img_file, "wb") as f:
        image_writer.write_image([frame], f)
Example #3
0
def run(snafile, imgfname, options):
    snapshot = get_snapshot(snafile)
    game = ContactSamCruise(snapshot)
    x = y = 0
    width, height = 256, 42
    game.hide_chars()
    game.reset_fuses()
    game.switch_lights_on()
    game.raise_blinds()
    game.close_doors()
    game.adjust_rope(options.rope)

    if options.geometry:
        wh, xy = options.geometry.split('+', 1)
        width, height = [int(n) for n in wh.split('x')]
        x, y = [int(n) for n in xy.split('+')]

    for spec in options.place_char:
        values = []
        index = 0
        for n in spec.split(','):
            if not values and '.' in n:
                n, i = n.split('.', 1)
                try:
                    index = int(i)
                except:
                    pass
            try:
                values.append(int(n))
            except ValueError:
                values.append(None)
        game.place_char(*values[:5], index=index)

    for spec in options.pokes:
        addr, val = spec.split(',', 1)
        step = 1
        if '-' in addr:
            addr1, addr2 = addr.split('-', 1)
            addr1 = int(addr1)
            if '-' in addr2:
                addr2, step = [int(i) for i in addr2.split('-', 1)]
            else:
                addr2 = int(addr2)
        else:
            addr1 = int(addr)
            addr2 = addr1
        addr2 += 1
        value = int(val)
        for a in range(addr1, addr2, step):
            snapshot[a] = value

    udg_array = game.get_play_area_udgs(x, y, width, height)
    frame = Frame(udg_array, options.scale)
    image_writer = ImageWriter()
    image_format = 'gif' if imgfname.lower()[-4:] == '.gif' else 'png'
    with open(imgfname, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #4
0
def _write_image(frame, img_file, animated):
    iw_options = {}
    if not animated:
        iw_options[GIF_ENABLE_ANIMATION] = 0
        iw_options[PNG_ENABLE_ANIMATION] = 0
    image_writer = ImageWriter(options=iw_options)
    image_format = 'gif' if img_file.lower()[-4:] == '.gif' else 'png'
    with open(img_file, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #5
0
def _write_image(frame, img_file, animated):
    iw_options = {}
    if not animated:
        iw_options[GIF_ENABLE_ANIMATION] = 0
        iw_options[PNG_ENABLE_ANIMATION] = 0
    image_writer = ImageWriter(options=iw_options)
    image_format = 'gif' if img_file.lower()[-4:] == '.gif' else 'png'
    with open(img_file, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #6
0
def write_image(udgs, img_file, scale, no_anim):
    iw_options = {}
    if no_anim:
        iw_options[GIF_ENABLE_ANIMATION] = 0
        iw_options[PNG_ENABLE_ANIMATION] = 0
    image_writer = ImageWriter(options=iw_options)
    image_format = 'gif' if img_file.lower()[-4:] == '.gif' else 'png'
    frame = Frame(udgs, scale)
    with open(img_file, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #7
0
def run(imgfname, options):
    snapshot = get_snapshot('{}/build/manic_miner.z80'.format(MANICMINER_HOME))
    _do_pokes(options.pokes, snapshot)
    mm = ManicMiner(snapshot)
    udg_array = _place_willy(mm, options.cavern, options.willy)
    if options.geometry:
        wh, xy = options.geometry.split('+', 1)
        width, height = [int(n) for n in wh.split('x')]
        x, y = [int(n) for n in xy.split('+')]
        udg_array = [row[x:x + width] for row in udg_array[y:y + height]]
    frame = Frame(udg_array, options.scale)
    image_writer = ImageWriter()
    with open(imgfname, "wb") as f:
        image_writer.write_image([frame], f)
Example #8
0
def run(imgfname, options):
    snapshot = get_snapshot('{}/build/jet_set_willy.z80'.format(JETSETWILLY_HOME))
    _do_pokes(options.pokes, snapshot)
    jsw = JetSetWilly(snapshot)
    udg_array = _place_willy(jsw, options.room, options.willy)
    if options.geometry:
        wh, xy = options.geometry.split('+', 1)
        width, height = [int(n) for n in wh.split('x')]
        x, y = [int(n) for n in xy.split('+')]
        udg_array = [row[x:x + width] for row in udg_array[y:y + height]]
    frame = Frame(udg_array, options.scale)
    image_writer = ImageWriter()
    with open(imgfname, "wb") as f:
        image_writer.write_image([frame], f)
Example #9
0
def run(imgfname, options):
    snapshot = get_snapshot('{}/build/manic_miner.z80'.format(MANICMINER_HOME))
    _do_pokes(options.pokes, snapshot)
    mm = ManicMiner(snapshot)
    udg_array = _place_willy(mm, options.cavern, options.willy)
    if options.geometry:
        wh, xy = options.geometry.split('+', 1)
        width, height = [int(n) for n in wh.split('x')]
        x, y = [int(n) for n in xy.split('+')]
        udg_array = [row[x:x + width] for row in udg_array[y:y + height]]
    frame = Frame(udg_array, options.scale)
    image_format = 'gif' if imgfname.lower()[-4:] == '.gif' else 'png'
    image_writer = ImageWriter()
    with open(imgfname, "wb") as f:
        image_writer.write_image([frame], f, image_format)
Example #10
0
    def _test_image(self, udg_array, scale=1, mask=0, tindex=0, alpha=-1, x=0, y=0, width=None, height=None, iw_args=None, exp_pixels=None):
        image_writer = ImageWriter(**(iw_args or {}))
        img_bytes = self._get_image_data(image_writer, udg_array, scale, mask, tindex, alpha, x, y, width, height)

        exp_pixels2 = None
        if exp_pixels is None:
            exp_palette, has_trans, has_tindex, exp_pixels, exp_pixels2, frame2_xy = self._get_pixels_from_udg_array(udg_array, scale, mask, tindex, x, y, width, height)
            if not image_writer.options[PNG_ENABLE_ANIMATION]:
                exp_pixels2 = None
        else:
            exp_palette = []
            has_trans = has_tindex = False
            for row in exp_pixels:
                for pixel in row:
                    if pixel not in exp_palette:
                        exp_palette.append(pixel)
        palette_size = len(exp_palette)
        if palette_size > 4:
            exp_bit_depth = 4
        elif palette_size > 2:
            exp_bit_depth = 2
        else:
            exp_bit_depth = 1

        # PNG signature
        i = self._check_signature(img_bytes)

        # IHDR
        exp_width = 8 * scale * len(udg_array[0]) - x if width is None else width
        exp_height = 8 * scale * len(udg_array) - y if height is None else height
        i = self._check_ihdr(img_bytes, i, exp_width, exp_height, exp_bit_depth)

        # PLTE
        i, palette = self._check_plte(img_bytes, i, exp_palette, has_trans or has_tindex)

        # tRNS
        if has_trans or has_tindex:
            if alpha < 0:
                alpha = image_writer.options[PNG_ALPHA]
            if alpha < 255:
                i = self._check_trns(img_bytes, i, alpha)

        # acTL and fcTL
        if exp_pixels2:
            i = self._check_actl(img_bytes, i)
            i = self._check_fctl(img_bytes, i, 0, exp_width, exp_height)

        # IDAT
        i = self._check_idat(img_bytes, i, exp_bit_depth, palette, exp_pixels, exp_width)

        # fcTL and fdAT
        if exp_pixels2:
            exp_width = len(exp_pixels2[0])
            exp_height = len(exp_pixels2)
            exp_x_offset, exp_y_offset = frame2_xy
            i = self._check_fctl(img_bytes, i, 1, exp_width, exp_height, exp_x_offset, exp_y_offset)
            i = self._check_fdat(img_bytes, i, exp_bit_depth, palette, exp_pixels2, exp_width)

        # IEND
        self.assertEqual(img_bytes[i:], IEND_CHUNK)
Example #11
0
def time_methods(method1_name, method2_name, udg_arrays, scale):
    for name1, name2, f in METHODS:
        if set((name1, name2)) == set((method1_name, method2_name)):
            f(ImageWriter(), name1, name2, udg_arrays, scale)
            break
    else:
        write('{} v. {}: not implemented'.format(method1_name, method2_name))
Example #12
0
def list_methods():
    prefix = '_build_image_data_'
    methods = []
    iw = ImageWriter()
    png_writer = iw.writers['png']
    for attr in dir(png_writer):
        if attr.startswith(prefix) and not attr.endswith('_bd0'):
            methods.append(attr)
    max_len = max([len(m) for m in methods]) - len(prefix)
    for m in methods:
        write('{} ({})'.format(m[len(prefix):].ljust(max_len), m))
    sys.exit()
Example #13
0
 def test_invalid_option_value(self):
     image_writer = ImageWriter(options={PNG_COMPRESSION_LEVEL: 'NaN'})
     self.assertEqual(image_writer.options[PNG_COMPRESSION_LEVEL], 9)
Example #14
0
 def test_default_option_values(self):
     image_writer = ImageWriter()
     self.assertEqual(image_writer.options[PNG_COMPRESSION_LEVEL], 9)
     self.assertEqual(image_writer.options[PNG_ENABLE_ANIMATION], 1)
     self.assertEqual(image_writer.options[PNG_ALPHA], 255)
Example #15
0
 def test_change_option_values(self):
     options = {PNG_COMPRESSION_LEVEL: 3}
     image_writer = ImageWriter(options=options)
     self.assertEqual(image_writer.options[PNG_COMPRESSION_LEVEL], 3)
Example #16
0
    def _test_animated_image(self, frames, iw_args=None):
        image_writer = ImageWriter(**(iw_args or {}))
        img_bytes = self._get_animated_image_data(image_writer, frames)

        exp_palette = []
        frame_data = []
        has_trans = 0
        has_tindex = False
        exp_tindex = frames[0].tindex
        for frame in frames:
            x, y, width, height = frame.x, frame.y, frame.width, frame.height
            frame_palette, f_has_trans, f_has_tindex, pixels, pixels2, frame2_xy = self._get_pixels_from_udg_array(frame.udgs, frame.scale, frame.mask, exp_tindex, x, y, width, height)
            has_trans = has_trans or f_has_trans
            has_tindex = has_tindex or f_has_tindex
            frame_data.append((width, height, pixels, frame.delay))
            for c in frame_palette:
                if c not in exp_palette:
                    exp_palette.append(c)
        if has_trans:
            exp_palette.remove(PALETTE[0])
            exp_palette.insert(0, PALETTE[0])
        elif exp_tindex:
            exp_palette.remove(PALETTE[exp_tindex])
            exp_palette.insert(0, PALETTE[exp_tindex])

        palette_size = len(exp_palette)
        if palette_size > 4:
            exp_bit_depth = 4
        elif palette_size > 2:
            exp_bit_depth = 2
        else:
            exp_bit_depth = 1

        # PNG signature
        i = self._check_signature(img_bytes)

        # IHDR
        exp_width, exp_height = frame_data[0][:2]
        i = self._check_ihdr(img_bytes, i, exp_width, exp_height, exp_bit_depth)

        # PLTE
        i, palette = self._check_plte(img_bytes, i, exp_palette, has_trans or has_tindex)

        # tRNS
        if has_trans or has_tindex:
            alpha = image_writer.options[PNG_ALPHA] if frames[0].alpha < 0 else frames[0].alpha
            if alpha < 255:
                i = self._check_trns(img_bytes, i, alpha)

        # acTL
        i = self._check_actl(img_bytes, i, len(frames))

        # Frames
        seq_num = 0
        for exp_width, exp_height, exp_pixels, exp_delay in frame_data:
            i = self._check_fctl(img_bytes, i, seq_num, exp_width, exp_height, exp_delay=exp_delay)
            seq_num += 1
            if seq_num == 1:
                i = self._check_idat(img_bytes, i, exp_bit_depth, palette, exp_pixels, exp_width)
            else:
                i = self._check_fdat(img_bytes, i, exp_bit_depth, palette, exp_pixels, exp_width, seq_num)
                seq_num += 1

        # IEND
        self.assertEqual(img_bytes[i:], IEND_CHUNK)