def add_data(hd, size, data, width, height, depth): assert depth in (1, 4, 8, 24) bits = (width * depth) lsize = bits / 8 if bits % 8 != 0: lsize += 1 tail = lsize % 4 padding = 0 if tail != 0: padding = 4 - tail lsize += padding shift = (8 - min(depth, 8)) mask = (0xff >> shift) << shift off = 0 for h in range(1, height + 1): lineiter = add_iter(hd, 'Line %d' % h, '', off, lsize, '%ds' % lsize) i = 1 while i < width + 1: if depth == 24: (color, off) = rdata(data, off, '3s') add_iter(hd, 'Pixel %d (BGR)' % i, d2hex(color), off - 3, 3, '3s', parent=lineiter) i += 1 else: (index, off) = rdata(data, off, '<B') for j in range(0, 8 / depth): add_iter(hd, 'Pixel %d (index)' % i, (index & mask) >> shift, off - 1, 1, '<B', parent=lineiter) index = index << depth i += 1 if i == width + 1: break if padding > 0: add_iter(hd, 'Padding', '', off, padding, '%ds' % padding, parent=lineiter) off += padding
def add_palette(hd, size, data): off = 0 i = 0 while off + 4 <= size: (color, off) = rdata(data, off, '3s') add_iter(hd, 'Color %d (BGR)' % (i + 1), d2hex(color), off - 3, 3, '3s') off += 1 i += 1