File size: 2,412 Bytes
5d892f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
import numpy as np
from PIL import Image
import os

# Convert 24-bit RGB to 16-bit RGB565
def rgb_to_rgb565(r, g, b):
    return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)

# Convert image to RGB565 format
def convert_to_rgb565(image):
    image = image.convert("RGB")  # Ensure image is in RGB mode
    pixels = np.array(image)
    height, width, _ = pixels.shape
    rgb565_data = []

    for y in range(height):
        for x in range(width):
            r, g, b = pixels[y, x]
            rgb565_data.append(rgb_to_rgb565(r, g, b))

    return rgb565_data, width, height

# Generate a .c file with RGB565 image data
def generate_c_file(frames, width, height, output_filename="gif_frames.c"):
    with open(output_filename, "w") as f:
        f.write("#include <stdint.h>\n\n")
        f.write(f"const uint16_t gif_width = {width};\n")
        f.write(f"const uint16_t gif_height = {height};\n\n")

        for i, frame in enumerate(frames):
            f.write(f"const uint16_t frame_{i}[] = {{\n")
            for j, pixel in enumerate(frame):
                f.write(f"0x{pixel:04X}, ")
                if (j + 1) % 10 == 0:
                    f.write("\n")
            f.write("};\n\n")

        f.write(f"const uint16_t* gif_frames[{len(frames)}] = {{\n")
        for i in range(len(frames)):
            f.write(f"  frame_{i},\n")
        f.write("};\n")

# Function to process GIF and return the .c file
def process_gif(gif_file):
    gif = Image.open(gif_file)
    frames = []
    
    frame_index = 0
    while True:
        rgb565_data, width, height = convert_to_rgb565(gif)
        frames.append(rgb565_data)
        frame_index += 1

        try:
            gif.seek(gif.tell() + 1)  # Move to next frame
        except EOFError:
            break

    output_filename = "/tmp/gif_frames.c"
    generate_c_file(frames, width, height, output_filename)
    return output_filename

# Gradio Interface
def gif_to_c_file(gif_file):
    output_file = process_gif(gif_file)
    with open(output_file, "r") as file:
        return file.read()

iface = gr.Interface(fn=gif_to_c_file, 
                     inputs=gr.File(label="Upload GIF"), 
                     outputs="text",
                     title="GIF to C File Converter",
                     description="Upload a GIF to convert it into a C file with RGB565 format for use on LilyGO T-Display S3.")

iface.launch()