💾 Archived View for gemini.sensorstation.co › assets › computing.uxn.utils › indexed-to-chr.py captured on 2022-04-28 at 17:55:00.
⬅️ Previous capture (2021-11-30)
-=-=-=-=-=-=-
#!/usr/bin/env python3 ''' Converts images with up to 4 indexed colors into the UXN 2-bits-per-pixel CHR format. Usage: - input: path to raw image input, where each pixel is one byte between 0x00 and 0x03 inclusive - output: path to chr output file - width: image width in number of 8x8 sprites (not pixels) ./indexed-to-chr.py [input] [output] [width] ./indexed-to-chr.py input.data output.chr 12 ''' import sys WIDTH = int(sys.argv[3]) with open(sys.argv[1], "rb") as input_file, open(sys.argv[2], "wb") as output_file: data = input_file.read() sprite = 0 while sprite * 8 * 8 < len(data): sprite_y = sprite // WIDTH sprite_x = sprite % WIDTH pixel_y = sprite_y * 8 pixel_x = sprite_x * 8 # write a sprite low = bytearray(8) high = bytearray(8) for y in range(8): offset = (pixel_y + y) * WIDTH * 8 + pixel_x row = data[slice(offset, offset+8)] for x in range(8): # mask, shift, and combine low[y] |= (row[x] & 0b00000001) << (7-x) high[y] |= ((row[x] & 0b00000010) >> 1) << (7-x) output_file.write(bytes(low)) output_file.write(bytes(high)) sprite += 1