// File: /workspace/github_c_makefile_star2_O2/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 0x39e12
movq 0x8(%rbx), %rdi
callq 0x27378
leaq 0x5643(%rip), %rsi      # 0x3f3c2
movq %rax, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x39f61
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdi
pushq $0x2
popq %rdx
callq 0x26de2
testb %al, %al
jne 0x39f61
movq 0x38(%rbx), %r14
testq %r14, %r14
jne 0x39de6
pushq $0x18
popq %rdi
callq 0x279f9
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x39dd7
xorl %edi, %edi
callq 0x7994
movl %eax, %ecx
movq 0x38(%rbx), %rax
testb %cl, %cl
je 0x39fea
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r14
pushq $0x10
popq %rdi
callq 0x279f9
movq %rax, (%r14)
testq %rax, %rax
jne 0x39e0a
xorl %edi, %edi
callq 0x7994
movl %eax, %ecx
movq (%r14), %rax
testb %cl, %cl
je 0x39fc8
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rax)
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 0x39e4c
cmpb $0x0, 0x18(%rbx)
jne 0x39f50
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rbx)
jmp 0x39f55
leal -0x7(%rcx), %edx
cmpl $0x3, %edx
ja 0x39f48
andb $0x1f, %al
movzbl %al, %edi
callq 0x38514
pushq $0x1
popq %rbp
movl %eax, %ecx
shll %cl, %ebp
movq 0x38(%rbx), %r12
testq %r12, %r12
jne 0x39ea7
pushq $0x18
popq %rdi
callq 0x279f9
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x39e98
xorl %edi, %edi
callq 0x7994
movl %eax, %ecx
movq 0x38(%rbx), %rax
testb %cl, %cl
je 0x39fd9
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r12
movl %ebp, %r14d
leaq (%r14,%r14), %rdi
callq 0x279f9
movq %rax, 0x8(%r12)
movq %r14, %rdi
callq 0x279f9
movq %rax, 0x10(%r12)
cmpq $0x0, 0x8(%r12)
je 0x39ed2
testq %rax, %rax
jne 0x39ee3
movq (%r12), %rdi
callq 0x7994
testb %al, %al
je 0x39fb6
cmpb $0x1, 0x18(%rbx)
jne 0x39f72
movq (%r12), %rdi
pushq $0x4
popq %rsi
callq 0x27046
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
cmpq %r13, %r14
je 0x39f50
movq (%r12), %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x26fa2
movq 0x8(%r12), %rbp
movzbl 0xa(%rsp), %edi
movzbl 0x9(%rsp), %esi
movzbl 0x8(%rsp), %edx
callq 0x3a6e7
movw %ax, (%rbp,%r13,2)
movb 0xb(%rsp), %al
movq 0x10(%r12), %rcx
movb %al, (%rcx,%r13)
incq %r13
jmp 0x39f01
addl $-0xb, %ecx
cmpl $0x3, %ecx
ja 0x39f59
andq $0x0, 0x20(%rbx)
movb $0x1, %al
jmp 0x39f63
movq %rbx, %rsi
callq 0x3a4d9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rax
movq 0x8(%rax), %r15
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x39f50
movq 0x8(%r12), %rbp
movzbl 0x2(%r15,%r13,4), %edi
movzbl 0x1(%r15,%r13,4), %esi
movzbl (%r15,%r13,4), %edx
callq 0x3a6e7
movw %ax, (%rbp,%r13,2)
movb 0x3(%r15,%r13,4), %al
movq 0x10(%r12), %rcx
movb %al, (%rcx,%r13)
incq %r13
jmp 0x39f7d
movq (%r12), %rsi
leaq 0x107f(%rip), %rdi      # 0x3b040
callq 0x7b01
jmp 0x39fc6
leaq 0x1071(%rip), %rdi      # 0x3b040
movq %rax, %rsi
callq 0x7b01
jmp 0x39fd7
leaq 0x1060(%rip), %rdi      # 0x3b040
movq %rax, %rsi
callq 0x7b01
jmp 0x39fe8
leaq 0x104f(%rip), %rdi      # 0x3b040
movq %rax, %rsi
callq 0x7b01
jmp 0x39ff9