// File: /workspace/github_c_makefile_star2_O1/OnionIoT[P]lv_example/lvgl/src/lv_draw/lv_img_decoder.c
// Function: lv_img_decoder_built_in_open
lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
{
    /*Open the file if it's a file*/
    if(dsc->src_type == LV_IMG_SRC_FILE) {
#if LV_USE_FILESYSTEM

        /*Support only "*.bin" files*/
        if(strcmp(lv_fs_get_ext(dsc->src), "bin")) return LV_RES_INV;

        lv_fs_file_t f;
        lv_fs_res_t res = lv_fs_open(&f, dsc->src, LV_FS_MODE_RD);
        if(res != LV_FS_RES_OK) {
            LV_LOG_WARN("Built-in image decoder can't open the file");
            return LV_RES_INV;
        }

        /*If the file was open successfully save the file descriptor*/
        if(dsc->user_data == NULL) {
            dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
            if(dsc->user_data == NULL) {
                LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
                LV_ASSERT_MEM(dsc->user_data);
            }
            memset(dsc->user_data, 0, sizeof(lv_img_decoder_built_in_data_t));
        }

        lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
        user_data->f                               = lv_mem_alloc(sizeof(f));
        if(user_data->f == NULL) {
            LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
            LV_ASSERT_MEM(user_data->f);
        }

        memcpy(user_data->f, &f, sizeof(f));

#else
        LV_LOG_WARN("Image built-in decoder cannot read file because LV_USE_FILESYSTEM = 0");
        return LV_RES_INV;
#endif
    }

    lv_img_cf_t cf = dsc->header.cf;
    /*Process true color formats*/
    if(cf == LV_IMG_CF_TRUE_COLOR || cf == LV_IMG_CF_TRUE_COLOR_ALPHA || cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
        if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
            /* In case of uncompressed formats the image stored in the ROM/RAM.
             * So simply give its pointer*/
            dsc->img_data = ((lv_img_dsc_t *)dsc->src)->data;
            return LV_RES_OK;
        } else {
            /*If it's a file it need to be read line by line later*/
            dsc->img_data = NULL;
            return LV_RES_OK;
        }
    }
    /*Process indexed images. Build a palette*/
    else if(cf == LV_IMG_CF_INDEXED_1BIT || cf == LV_IMG_CF_INDEXED_2BIT || cf == LV_IMG_CF_INDEXED_4BIT ||
            cf == LV_IMG_CF_INDEXED_8BIT) {

#if LV_IMG_CF_INDEXED
        uint8_t px_size       = lv_img_color_format_get_px_size(cf);
        uint32_t palette_size = 1 << px_size;

        /*Allocate the palette*/
        if(dsc->user_data == NULL) {
            dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
            if(dsc->user_data == NULL) {
                LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
                LV_ASSERT_MEM(dsc->user_data);
            }
            memset(dsc->user_data, 0, sizeof(lv_img_decoder_built_in_data_t));
        }

        lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
        user_data->palette                         = lv_mem_alloc(palette_size * sizeof(lv_color_t));
        user_data->opa                             = lv_mem_alloc(palette_size * sizeof(lv_opa_t));
        if(user_data->palette == NULL || user_data->opa == NULL) {
            LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
#if LV_USE_FILESYSTEM
            LV_ASSERT_MEM(user_data->f);
#endif
        }

        if(dsc->src_type == LV_IMG_SRC_FILE) {
            /*Read the palette from file*/
#if LV_USE_FILESYSTEM
            lv_fs_seek(user_data->f, 4); /*Skip the header*/
            lv_color32_t cur_color;
            uint32_t i;
            for(i = 0; i < palette_size; i++) {
                lv_fs_read(user_data->f, &cur_color, sizeof(lv_color32_t), NULL);
                user_data->palette[i] = lv_color_make(cur_color.ch.red, cur_color.ch.green, cur_color.ch.blue);
                user_data->opa[i]     = cur_color.ch.alpha;
            }
#else
            LV_LOG_WARN("Image built-in decoder can read the palette because LV_USE_FILESYSTEM = 0");
            return LV_RES_INV;
#endif
        } else {
            /*The palette begins in the beginning of the image data. Just point to it.*/
            lv_color32_t * palette_p = (lv_color32_t *)((lv_img_dsc_t *)dsc->src)->data;

            uint32_t i;
            for(i = 0; i < palette_size; i++) {
                user_data->palette[i] = lv_color_make(palette_p[i].ch.red, palette_p[i].ch.green, palette_p[i].ch.blue);
                user_data->opa[i]     = palette_p[i].ch.alpha;
            }
        }

        dsc->img_data = NULL;
        return LV_RES_OK;
#else
        LV_LOG_WARN("Indexed (palette) images are not enabled in lv_conf.h. See LV_IMG_CF_INDEXED");
        return LV_RES_INV;
#endif
    }
    /*Alpha indexed images. */
    else if(cf == LV_IMG_CF_ALPHA_1BIT || cf == LV_IMG_CF_ALPHA_2BIT || cf == LV_IMG_CF_ALPHA_4BIT ||
            cf == LV_IMG_CF_ALPHA_8BIT) {
#if LV_IMG_CF_ALPHA
        dsc->img_data = NULL;
        return LV_RES_OK; /*Nothing to process*/
#else
        LV_LOG_WARN("Alpha indexed images are not enabled in lv_conf.h. See LV_IMG_CF_ALPHA");
        return LV_RES_INV;
#endif
    }
    /*Unknown format. Can't decode it.*/
    else {
        /*Free the potentially allocated memories*/
        lv_img_decoder_built_in_close(decoder, dsc);

        LV_LOG_WARN("Image decoder open: unknown color format")
        return LV_RES_INV;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpb $0x1, 0x18(%rsi)
jne 0x3d9c6
movq 0x8(%rbx), %rdi
callq 0x2921b
leaq 0x5a25(%rip), %rsi      # 0x4334a
movq %rax, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x3db21
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdi
movl $0x2, %edx
callq 0x28c4f
testb %al, %al
jne 0x3db21
movl %eax, %ebp
cmpq $0x0, 0x38(%rbx)
jne 0x3d98d
movl $0x18, %edi
callq 0x298d9
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x3d97b
xorl %edi, %edi
callq 0x80d4
testb %al, %al
je 0x3dbbf
movq 0x38(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r14
movl $0x10, %edi
callq 0x298d9
movq %rax, (%r14)
testq %rax, %rax
jne 0x3d9b2
xorl %edi, %edi
callq 0x80d4
testb %al, %al
je 0x3db9c
movq (%r14), %rax
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rax)
testb %bpl, %bpl
jne 0x3db21
movl 0x1c(%rbx), %eax
movl %eax, %ecx
andl $0x1f, %ecx
movl %eax, %edx
andl $0x1e, %edx
cmpl $0x4, %edx
setne %dl
cmpl $0x6, %ecx
setne %sil
testb %sil, %dl
jne 0x3da00
cmpb $0x0, 0x18(%rbx)
jne 0x3db71
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rbx)
jmp 0x3db79
leal -0x7(%rcx), %edx
cmpl $0x3, %edx
ja 0x3db11
andb $0x1f, %al
movzbl %al, %edi
callq 0x3bfce
movl $0x1, %ebp
movl %eax, %ecx
shll %cl, %ebp
cmpq $0x0, 0x38(%rbx)
jne 0x3da5a
movl $0x18, %edi
callq 0x298d9
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x3da48
xorl %edi, %edi
callq 0x80d4
testb %al, %al
je 0x3dbad
movq 0x38(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r12
movl %ebp, %r14d
leaq (%r14,%r14), %rdi
callq 0x298d9
movq %rax, 0x8(%r12)
movq %r14, %rdi
callq 0x298d9
movq %rax, 0x10(%r12)
cmpq $0x0, 0x8(%r12)
je 0x3da89
testq %rax, %rax
jne 0x3da9a
movq (%r12), %rdi
callq 0x80d4
testb %al, %al
je 0x3db8a
cmpb $0x1, 0x18(%rbx)
jne 0x3db25
movq (%r12), %rdi
movl $0x4, %esi
callq 0x28eaf
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
movq (%r12), %rdi
movq %r15, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x28dff
movq 0x8(%r12), %rax
movzbl 0xa(%rsp), %ecx
movzbl 0x9(%rsp), %edx
movzbl 0x8(%rsp), %esi
shrl $0x3, %esi
andl $0xfc, %edx
leal (%rsi,%rdx,8), %edx
andl $-0x8, %ecx
shll $0x8, %ecx
orl %edx, %ecx
movw %cx, (%rax,%r13,2)
movb 0xb(%rsp), %al
movq 0x10(%r12), %rcx
movb %al, (%rcx,%r13)
incq %r13
cmpq %r13, %r14
jne 0x3daba
jmp 0x3db71
addl $-0xb, %ecx
cmpl $0x3, %ecx
jbe 0x3db71
movq %rbx, %rsi
callq 0x3e0ac
xorl %eax, %eax
jmp 0x3db7b
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
xorl %ecx, %ecx
movq 0x8(%r12), %rdx
movzbl 0x2(%rax,%rcx,4), %esi
movzbl 0x1(%rax,%rcx,4), %edi
movzbl (%rax,%rcx,4), %r8d
shrl $0x3, %r8d
andl $0xfc, %edi
leal (%r8,%rdi,8), %edi
andl $-0x8, %esi
shll $0x8, %esi
orl %edi, %esi
movw %si, (%rdx,%rcx,2)
movb 0x3(%rax,%rcx,4), %dl
movq 0x10(%r12), %rsi
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %r14
jne 0x3db2f
movq $0x0, 0x20(%rbx)
movb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rsi
leaq 0x14ab(%rip), %rdi      # 0x3f040
callq 0x829e
jmp 0x3db9a
movq (%r14), %rsi
leaq 0x149a(%rip), %rdi      # 0x3f040
callq 0x829e
jmp 0x3dbab
movq 0x38(%rbx), %rsi
leaq 0x1488(%rip), %rdi      # 0x3f040
callq 0x829e
jmp 0x3dbbd
movq 0x38(%rbx), %rsi
leaq 0x1476(%rip), %rdi      # 0x3f040
callq 0x829e
jmp 0x3dbcf