Skip to content

Instantly share code, notes, and snippets.

@odudex
Created October 2, 2024 14:32
Show Gist options
  • Save odudex/b7d04b42ae5d0bfd0cd9f4ad7b7e4cb5 to your computer and use it in GitHub Desktop.
Save odudex/b7d04b42ae5d0bfd0cd9f4ad7b7e4cb5 to your computer and use it in GitHub Desktop.
Render images from Krux flash dumps
import os
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
def create_image_from_file(filename, start_pos):
def maxv(number_of_bits):
return (2**number_of_bits) - 1
with open(filename, 'rb') as f:
f.seek(start_pos)
data = f.read(153600) # Read 153,600 bytes
# Ensure we have enough data to form an image
if len(data) < 153600:
# Pad the remaining bytes with zeros if needed
data += b'\x00' * (153600 - len(data))
pixels = []
for i in range(0, len(data), 2):
byte1 = data[i]
byte2 = data[i+1]
rgb_int = (byte1 << 8) | byte2
# big = True
# rgb_int = int.from_bytes(rgb, "big" if big else "little")
# left 5 bits of red multiplied to fill 8 bit space
r = round((rgb_int >> 11) * maxv(8) / maxv(5))
# middle 6 bits of green multiplied to fill 8 bit space
g = round(((rgb_int & maxv(11)) >> 5) * maxv(8) / maxv(6))
# right 5 bits of blue multiplied to fill 8 bit space
b = round((rgb_int & maxv(5)) * maxv(8) / maxv(5))
pixels.append((r, g, b))
# Create an image from the pixel data
image = Image.new('RGB', (320, 240))
image.putdata(pixels)
return image
def main():
root = tk.Tk()
root.title("Krux Flash Viewer")
# Ask user for filename
filename = filedialog.askopenfilename(title="Select binary file")
if not filename:
print("No file selected")
return
# Get the file size
file_size = os.path.getsize(filename)
# Calculate the maximum start position (must be multiple of 2)
max_start_pos = (file_size - 153600) // 2 * 2
if max_start_pos < 0:
max_start_pos = 0
# Create a label to hold the image
label = tk.Label(root)
label.pack(side='left')
# Create a scrollbar (Scale widget)
# The range is from 0 to max_start_pos // 2 to ensure positions are multiples of 2
scale = tk.Scale(
root,
from_=0,
to=max_start_pos // 640,
orient='vertical',
length=400,
command=lambda val: update_image(int(val) * 640)
)
scale.pack(side='right', fill='y')
def update_image(position):
# Read and display the image starting from the given position
image = create_image_from_file(filename, position)
photo = ImageTk.PhotoImage(image)
label.config(image=photo)
label.image = photo # Keep a reference to prevent garbage collection
# Initialize the display with the starting position 0
update_image(0)
root.mainloop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment