tuupola/esp_effects

Documentation of sdkconfig usage and color format

georgik opened this issue · 2 comments

Nice demo @tuupola . Thank you for making it.

One recommendation how to avoid cp of sdkconfig for each platform, is to use environment variable and build like this:

export SDKCONFIG_DEFAULTS=sdkconfig.m5stack
idf.py build flash monitor

This approach is descripted in docs here.

Please, could you provide a hint how the picture in head.h was created? I guess that it's RGB565, yet RGB565 or BGR565 is not producing the correct result.

Did not know about the SDKCONFIG_DEFAULTS. That is much cleaner way to do the same.

For the head.h I do not remember anymore. I will try to figure out how I did it. Are you testing with some microcontroller or SDL2? It might also be big endian vs small endian problem in the code itself.

@tuupola The endianity was the problem. Thanks for the hint.

I wrote small script to convert PNG into the source format, so it can be dropped into the demo:

from PIL import Image
import numpy as np

def rgb888_to_rgb565(r, g, b, byteorder='little'):
    """Convert from RGB888 to RGB565 with correct endianness."""
    rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
    if byteorder == 'little':
        rgb565 = ((rgb565 & 0xFF) << 8) | ((rgb565 >> 8) & 0xFF)
    return rgb565

def convert_image_to_rgb565_header(png_filepath, header_filepath):
    """Convert a PNG image to RGB565 format and save as a C header file."""
    img = Image.open(png_filepath).convert('RGB')
    pixels = np.array(img)
    height, width, _ = pixels.shape

    with open(header_filepath, 'w') as header_file:
        header_file.write("#ifndef IMAGE_H\n")
        header_file.write("#define IMAGE_H\n\n")
        header_file.write("#include <stdint.h>\n\n")
        header_file.write(f"static const uint16_t HEAD_WIDTH = {width};\n")
        header_file.write(f"static const uint16_t HEAD_HEIGHT = {height};\n")
        header_file.write(f"static const uint16_t head[] = {{\n")

        for y in range(height):
            for x in range(width):
                r, g, b = pixels[y, x]
                rgb565 = rgb888_to_rgb565(r, g, b)
                header_file.write(f"    0x{rgb565:04X},")
            header_file.write("\n")

        header_file.write("};\n\n")
        header_file.write("#endif // IMAGE_H\n")

png_filepath = 'input.png'
header_filepath = 'output_image.h'
convert_image_to_rgb565_header(png_filepath, header_filepath)