extract-font.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:R11-psp-english 作者: dreambottle 项目源码 文件源码
def writePngs(font, firstHalfHack=False):
  # out = 'font00.png'
  raw = font.imageRawData

  bs = font.header.blockSize
  h = font.header.height
  w = font.header.width
  wb = bs // h
  BPP = 2

  glyphs = []
  for i in range(len(raw) // bs):
    pos = i * bs
    rawBlock = raw[pos:pos+bs]
    # 2 glyphs in every block
    glyph0 = []
    glyph1 = []
    for l in range(h):
      rawLine = rawBlock[ l*wb : l*wb+wb ]

      line0 = []
      line1 = []
      rng = wb
      if firstHalfHack:
        rng //= 2
      for p in range(rng):
        # byte = rawLine[p]
        # px = ((byte & 1) << 1) | ((byte >> 1) & 1)
        # line0.append(px)
        # byte >>= 2
        # px = ((byte & 1) << 1) | ((byte >> 1) & 1)
        # line1.append(px)
        # byte >>= 2
        # px = ((byte & 1) << 1) | ((byte >> 1) & 1)
        # line0.append(px)
        # byte >>= 2
        # px = ((byte & 1) << 1) | ((byte >> 1) & 1)
        # line1.append(px)
        # Nope, let's try sth different

        loHalf = rawLine[p] & 0xF
        hiHalf = rawLine[p] >> 4 & 0xF
        px0 = loHalf & 0x3
        line0.extend([magicPxConvert(loHalf & 0x3), magicPxConvert(hiHalf & 0x3)])
        line1.extend([magicPxConvert(loHalf >> 2), magicPxConvert(hiHalf >> 2)])
      glyph0.append(line0)
      glyph1.append(line1)
    # every block
    glyphs.append(glyph0)
    glyphs.append(glyph1)

  print("decoded glyphs count:", len(glyphs))
  print("Writing glyphs to files...")
  writer = png.Writer(w, h, greyscale=True, bitdepth=2)
  for i in range(0, len(glyphs)):
    file = open('glyphs/{0:d}.png'.format(i), 'wb')
    writer.write(file, glyphs[i])
    file.close()


# Works both ways (encode & decode). I still have no idea why they did it though.
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号