extract-font.py 文件源码

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

项目:R11-psp-english 作者: dreambottle 项目源码 文件源码
def pngsToRawGlyphBlock(blockSize, pngPath0, pngPath1):
  rawData = bytearray(blockSize)
  r0 = png.Reader(filename=pngPath0)
  r1 = png.Reader(filename=pngPath1)
  pngMaps = (r0.read(), r1.read())

  if (pngMaps[0][0] != pngMaps[1][0]) or (pngMaps[0][1] != pngMaps[1][1]):
    print("Dimensions mismatch between {0} and {1}".format(pngPath0, pngPath1))
    exit(1)
  if pngMaps[0][3]['bitdepth'] != 2 or pngMaps[1][3]['bitdepth'] != 2:
    print("Bitdepth must be 2! (files: {0} {1})".format(pngPath0, pngPath1))
    exit(1)

  BPP = 2
  w = pngMaps[0][0]
  h = pngMaps[0][1]
  bytesPerLineImg = (2 * w * BPP) // 8
  bytesPerLineBlock = blockSize // h
  pixels = (list(pngMaps[0][2]), list(pngMaps[1][2]))
  for l in range(h):
    for nbyte in range(bytesPerLineImg):
      npx = nbyte * 2
      # lo to hi
      px = [
        pixels[0][l][npx],
        pixels[1][l][npx],
        pixels[0][l][npx+1],
        pixels[1][l][npx+1]
      ]
      byte = 0
      # doing the magic conversion and packing into a byte
      for i, p in enumerate(px): byte |= magicPxConvert(p) << 2*i
      rawData[l*bytesPerLineBlock+nbyte] = byte
  return rawData
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号