Beispiel #1
0
def main():
  parser = argparse.ArgumentParser(
    description='Compresses ILBM IFF file with LZO or Deflate algorithm.')
  parser.add_argument(
    '-m', '--method', type=str, choices=['none', 'lzo', 'deflate'],
    default='lzo', help='Compression method to use.')
  parser.add_argument(
    '-f', '--force', action='store_true',
    help='If the output file exists, the tool will overwrite it.')
  parser.add_argument(
    'input', metavar='INPUT', type=str,
    help='Input ILBM IFF file name.')
  parser.add_argument(
    'output', metavar='OUTPUT', type=str, nargs='?',
    help='Output ILBM IFF file name.')
  args = parser.parse_args()

  if args.output is None:
    args.output = args.input

  if not os.path.isfile(args.input):
    raise SystemExit('File "%s" does not exists!' % args.input)

  if os.path.exists(args.output) and not args.force:
    raise SystemExit(
      'File "%s" already exists (use "-f" to override).' %
      args.output)

  ilbm = ILBM()
  ilbm.ChunkBlackList.append('CRNG')

  if ilbm.load(args.input):
    bmhd = ilbm.get('BMHD')
    body = ilbm.get('BODY')

    size = ((bmhd.data.w + 15) & ~15) / 8 * bmhd.data.h * bmhd.data.nPlanes

    logging.info(bmhd.data)
    logging.info(
      'BODY size before compression: %d/%d' % (len(body.data), size))

    if bmhd.data.compression in [0, 1, 254, 255]:
      body.data = body.data.read()
      if bmhd.data.compression == 1:
        body.data = UnRLE(body.data)
      if bmhd.data.compression == 254:
        body.data = zopfli.decompress(body.data, size)
      if bmhd.data.compression == 255:
        body.data = lzo.decompress(body.data, size)
      if args.method == 'deflate':
        opts = zopfli.Options()
        body.data = zopfli.compress(opts, body.data, len(body.data))
        bmhd.data = bmhd.data._replace(compression=254)
      if args.method == 'lzo':
        body.data = lzo.compress(body.data)
        bmhd.data = bmhd.data._replace(compression=255)
      if args.method == 'none':
        bmhd.data = bmhd.data._replace(compression=0)
      logging.info(
        'BODY size after compression: %d/%d' % (len(body.data), size))
      ilbm.save(args.output)
    else:
      logging.warning('Unknown compression: %d' % bmhd.data.compression)
Beispiel #2
0
    except ValueError:
        continue
    mime_type = l[:last_tab].strip()
    for extension in l[last_tab:].split():
        if not extension in known_exts:
            known_exts.add(extension)
            types.append((mime_type, extension))

out = b''
entries = 0
for typ, ext in sorted(types, key=itemgetter(1)):
    entries += 1
    out += pack_string(ext)
    out += pack_string(typ)

compressed_out = zlib.compress(out, 9)

print('#ifndef __MIME_TYPES_H__')
print('#define __MIME_TYPES_H__')
print('/* Auto generated from parse-mime-types.py, do not modify */')

print('#define MIME_UNCOMPRESSED_LEN %d' % len(out))
print('#define MIME_COMPRESSED_LEN %d' % len(compressed_out))
print('#define MIME_ENTRIES %d' % entries)

print('struct mime_entry {')
print('  const char *extension;')
print('  const char *type;')
print('};')

print('static const unsigned char mime_entries_compressed[] = {')
Beispiel #3
0
    types.append((mime_type, extension))

types.sort(key = itemgetter(1))

max_ext_len = max(len(ext) for typ, ext in types)
max_typ_len = max(len(typ) for typ, ext in types)
total_len = len(types) * (max_ext_len + 1 + max_typ_len + 1)

out = b''
entries = 0
for typ, ext in types:
  entries += 1
  out += pack_string(ext)
  out += pack_string(typ)

compressed_out = zlib.compress(out, 9)

print('#ifndef __MIME_TYPES_H__')
print('#define __MIME_TYPES_H__')
print('/* Auto generated from parse-mime-types.py, do not modify */')

print('#define MIME_UNCOMPRESSED_LEN %d' % len(out))
print('#define MIME_COMPRESSED_LEN %d' % len(compressed_out))
print('#define MIME_ENTRIES %d' % entries)

print('struct mime_entry {')
print('  const char *extension;')
print('  const char *type;')
print('};')

print('static const unsigned char mime_entries_compressed[] = {')
def main():
    parser = argparse.ArgumentParser(
        description='Compresses ILBM IFF file with Deflate algorithm.')
    parser.add_argument('-m',
                        '--method',
                        type=str,
                        choices=['none', 'deflate'],
                        default='deflate',
                        help='Compression method to use.')
    parser.add_argument('-q',
                        '--quiet',
                        action='store_true',
                        help='Silence out diagnostic messages.')
    parser.add_argument(
        '-f',
        '--force',
        action='store_true',
        help='If the output file exists, the tool will overwrite it.')
    parser.add_argument('input',
                        metavar='INPUT',
                        type=str,
                        help='Input ILBM IFF file name.')
    parser.add_argument('output',
                        metavar='OUTPUT',
                        type=str,
                        nargs='?',
                        help='Output ILBM IFF file name.')
    args = parser.parse_args()

    logLevel = [logging.INFO, logging.WARNING][args.quiet]
    logging.basicConfig(level=logLevel, format="%(levelname)s: %(message)s")

    if args.output is None:
        args.output = args.input

    if not os.path.isfile(args.input):
        raise SystemExit('File "%s" does not exists!' % args.input)

    if os.path.exists(args.output) and not args.force:
        raise SystemExit('File "%s" already exists (use "-f" to override).' %
                         args.output)

    ilbm = ILBM()
    ilbm.ChunkBlackList.append('CRNG')

    if ilbm.load(args.input):
        bmhd = ilbm.get('BMHD')
        body = ilbm.get('BODY')

        size = ((bmhd.data.w + 15) & ~15) / 8 * bmhd.data.h * bmhd.data.nPlanes
        payload = body.data.read()

        logging.info(bmhd.data)
        logging.info('BODY size before compression: %d/%d' %
                     (len(payload), size))

        if bmhd.data.compression in [0, 1, 254, 255]:
            if bmhd.data.compression == 1:
                payload = UnRLE(payload)
            if bmhd.data.compression == 254:
                payload = zopfli.decompress(payload, size)
            compression = 0
            if args.method == 'deflate':
                opts = zopfli.Options()
                payload = zopfli.compress(opts, payload, len(payload))
                compression = 254
            if args.method == 'none':
                compression = 0
            body.data = payload
            bmhd.data = bmhd.data._replace(compression=compression)
            logging.info('BODY size after compression: %d/%d' %
                         (len(body.data), size))
            ilbm.save(args.output)
        else:
            logging.warning('Unknown compression: %d' % bmhd.data.compression)
Beispiel #5
0
def main():
  parser = argparse.ArgumentParser(
    description='Compresses ILBM IFF file with Deflate algorithm.')
  parser.add_argument(
    '-m', '--method', type=str, choices=['none', 'deflate'],
    default='deflate', help='Compression method to use.')
  parser.add_argument(
    '-q', '--quiet', action='store_true',
    help='Silence out diagnostic messages.')
  parser.add_argument(
    '-f', '--force', action='store_true',
    help='If the output file exists, the tool will overwrite it.')
  parser.add_argument(
    'input', metavar='INPUT', type=str,
    help='Input ILBM IFF file name.')
  parser.add_argument(
    'output', metavar='OUTPUT', type=str, nargs='?',
    help='Output ILBM IFF file name.')
  args = parser.parse_args()

  logLevel = [logging.INFO, logging.WARNING][args.quiet]
  logging.basicConfig(level=logLevel, format="%(levelname)s: %(message)s")

  if args.output is None:
    args.output = args.input

  if not os.path.isfile(args.input):
    raise SystemExit('File "%s" does not exists!' % args.input)

  if os.path.exists(args.output) and not args.force:
    raise SystemExit(
      'File "%s" already exists (use "-f" to override).' %
      args.output)

  ilbm = ILBM()
  ilbm.ChunkBlackList.append('CRNG')

  if ilbm.load(args.input):
    bmhd = ilbm.get('BMHD')
    body = ilbm.get('BODY')

    size = ((bmhd.data.w + 15) & ~15) / 8 * bmhd.data.h * bmhd.data.nPlanes
    payload = body.data.read()

    logging.info(bmhd.data)
    logging.info('BODY size before compression: %d/%d' % (len(payload), size))

    if bmhd.data.compression in [0, 1, 254, 255]:
      if bmhd.data.compression == 1:
        payload = UnRLE(payload)
      if bmhd.data.compression == 254:
        payload = zopfli.decompress(payload, size)
      compression = 0
      if args.method == 'deflate':
        opts = zopfli.Options()
        payload = zopfli.compress(opts, payload, len(payload))
        compression = 254
      if args.method == 'none':
        compression = 0
      body.data = payload
      bmhd.data = bmhd.data._replace(compression=compression)
      logging.info(
        'BODY size after compression: %d/%d' % (len(body.data), size))
      ilbm.save(args.output)
    else:
      logging.warning('Unknown compression: %d' % bmhd.data.compression)