Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tool): add compressed binary image support to C-array #4924

Merged
merged 2 commits into from
Dec 5, 2023

Conversation

XuNeo
Copy link
Collaborator

@XuNeo XuNeo commented Dec 4, 2023

Description of the feature or fix

This PR enables PNG to compressed C-array conversion.

Key updates include:

  • move compress parameter to to_bin method, since it's only used in lvgl binary image
  • C-array map data is now static
  • compression method is appended to C-array variable name, preventing naming conflicts.

Checkpoints

Be sure the following conventions are followed:

  • Follow the Styling guide
  • Prefer enums instead of macros. If inevitable to use defines export them with LV_EXPORT_CONST_INT(defined_value) right after the define.
  • In function arguments prefer type name[] declaration for array parameters instead of type * name
  • Use typed pointers instead of void * pointers
  • Do not malloc into a static or global variables. Instead declare the variable in lv_global_t structure in lv_global.h and mark the variable with (LV_GLOBAL_DEFAULT()->variable) when it's used. See a detailed description here.
  • Widget constructor must follow the lv_<widget_name>_create(lv_obj_t * parent) pattern.
  • Widget members function must start with lv_<module_name> and should receive lv_obj_t * as first argument which is a pointer to widget object itself.
  • structs should be used via an API and not modified directly via their elements.
  • struct APIs should follow the widgets' conventions. That is to receive a pointer to the struct as the first argument, and the prefix of the struct name should be used as the prefix of the function name too (e.g. lv_disp_set_default(lv_disp_t * disp))
  • Functions and structs which are not part of the public API must begin with underscore in order to mark them as "private".
  • Arguments must be named in H files too.
  • To register and use callbacks one of the following needs to be followed (see a detailed description here):
    • For both the registration function and the callback pass a pointer to a struct as the first argument. The struct must contain void * user_data field.
    • The last argument of the registration function must be void * user_data and the same user_data needs to be passed as the last argument of the callback.
    • Callback types not following these conventions should end with xcb_t.

Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
@XuNeo XuNeo changed the title feat: add compressed binary image support to C-array feat(tool): add compressed binary image support to C-array Dec 4, 2023
@kisvegabor
Copy link
Member

compression method is appended to C-array variable name, preventing naming conflicts

I think it's not required. The developer who is creating the UI is probably not interested in the compression method. And when the compression method changes for any reason it shouldn't be required to update all usage of the image in the UI.

What about adding an "output-name" parameter to the script to allow defining the names explicitly.

Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
@XuNeo
Copy link
Collaborator Author

XuNeo commented Dec 5, 2023

What about adding an "output-name" parameter to the script to allow defining the names explicitly.

Update to use filename as var name. File name can be specified from img.to_c_array(filename.c). So it makes sense to keep filename as unique, thus var name unique.

    img.to_c_array("output/cogwheel-abc.c")  # file name is used as c var name

@kisvegabor
Copy link
Member

I meant allowing for the user to specify the output name. E.g.
LVGLImage.py <some params> --output-name my_image1 or
LVGLImage.py <some params> --output-name "my_image1 my_image2 my_image3" in case of multiple files

@XuNeo
Copy link
Collaborator Author

XuNeo commented Dec 5, 2023

I meant allowing for the user to specify the output name. E.g. LVGLImage.py <some params> --output-name my_image1 or LVGLImage.py <some params> --output-name "my_image1 my_image2 my_image3" in case of multiple files

There's a much simpler solution without messing around with large params. Here's a script I plan for CI to auto-generate test images.

def generate_test_images():
    invalids = (ColorFormat.UNKNOWN, ColorFormat.TRUECOLOR, ColorFormat.TRUECOLOR_ALPHA)
    formats = [i for i in ColorFormat if i not in invalids]
    png_path = os.path.join(lvgl_test_dir, "test_images/pngs")
    pngs = list(Path(png_path).rglob("*.[pP][nN][gG]"))
    print(f"png files: {pngs}")

    align_options = [1, 64]

    for align in align_options:
        for compress in CompressMethod:
            compress_name = compress.name if compress != CompressMethod.NONE else "UNCOMPRESSED"
            outputs = os.path.join(lvgl_test_dir, f"test_images/stride_align{align}/{compress_name}/")
            os.makedirs(outputs, exist_ok=True)
            for fmt in formats:
                for png in pngs:
                    img = LVGLImage().from_png(png, cf=fmt, background=0xffffff)
                    img.adjust_stride(align=16)
                    img.set_compress(compress)
                    output = os.path.join(outputs, f"{Path(png).stem}_{fmt.name}.bin")
                    img.to_bin(output)
                    output = os.path.join(outputs, f"{Path(png).stem}_{fmt.name}.c")
                    img.to_c_array(output)
                    print(f"converting {os.path.basename(png)}, format: {fmt.name}, compress: {compress_name}")

@kisvegabor
Copy link
Member

Aaaahhaa, very nice! I didn't think of using the image converter in a Python script. I need to break my C chains 😄

@kisvegabor kisvegabor merged commit b655486 into lvgl:master Dec 5, 2023
15 checks passed
@XuNeo XuNeo deleted the image-tool-compressed-c-array branch December 6, 2023 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants