Create decode.py

This commit is contained in:
blackorbird 2019-04-03 22:18:03 +08:00 committed by GitHub
parent de2d9db4db
commit d77a8c89ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

24
Oceanlotus/decode.py Normal file
View File

@ -0,0 +1,24 @@
import png
def get_rgba(w, h, pixels, x, y):
Get RGBA pixel DWORD from x, y
pos = x + y * w
pixel = pixels[pos * 4 : (pos + 1) * 4]
return pixel[0], pixel[1], pixel[2], pixel[3]
def decode_pixel(w, h, pixels, x, y):
Get RGBA pixel DWORD at x, y and decode to BYTE
r, g, b, a = get_rgba(w, h, pixels, x, y)
return (r & 7 | 8 * (8 * b | g & 7)) & 0xff
# Open payload image
w, h, pixels, metadata = png.Reader(filename=payload.png).read_flat()
size = 0
x = 0
y = 0
# Decode size of payload
while x < 4:
size = (size >> 8) | decode_pixel(w, h, pixels, x, y) << 24
x = x + 1
print(hex(size))
# Decode first row
while x < w:
print(hex(decode_pixel(w, h, pixels, x, y)))
x = x + 1