【问题标题】:Python: How to calculate png crc valuePython:如何计算 png crc 值
【发布时间】:2017-08-02 13:25:45
【问题描述】:
crc_table = None

def make_crc_table():
  global crc_table
  crc_table = [0] * 256
  for n in xrange(256):
    c = n
    for k in xrange(8):
        if c & 1:
            c = 0xedb88320L ^ (c >> 1)
        else:
            c = c >> 1
    crc_table[n] = c

make_crc_table()

"""
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
should be initialized to all 1's, and the transmitted value
is the 1's complement of the final running CRC (see the
crc() routine below)). */
"""
def update_crc(crc, buf):
  c = crc
  for byte in buf:
    c = crc_table[int((c ^ ord(byte)) & 0xff)] ^ (c >> 8)
  return c

# /* Return the CRC of the bytes buf[0..len-1]. */
def crc(buf):
  return update_crc(0xffffffffL, buf) ^ 0xffffffffL

我使用此代码计算 png crc 值
我的 IHDR 块数据是 000008A0 000002FA 08020000 00,该代码的结果是 0xa1565b1L
然而真正的 crc 是0x84E42B87。我用众所周知的 png 检查工具检查了这个值,正确的 crc 是 0x84E42B87
我不明白这个值是如何计算的和正确的值。

【问题讨论】:

    标签: python image image-processing crc crc32


    【解决方案1】:

    CRC 是根据块类型和数据计算的,而不仅仅是数据。因此,这些字节前面将有四个字节IHDR。然后你会得到正确的 CRC。

    顺便说一句,我不知道你是如何从000008A0 000002FA 08020000 00 得到0xa1565b1L 的。我得到0xa500050a 作为这些字节的CRC。一定还有其他事情你做错了。你需要提供一个完整的例子让我们知道。

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多