// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/include/utils/MnistHelper.h
// Function: abcdl::utils::MnistHelper<float>::read_image(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, abcdl::algebra::Matrix<float>*, int, unsigned long)
bool MnistHelper<T>::read_image(const std::string& image_file,
                                abcdl::algebra::Matrix<T>* out_mat,
                                const int limit,
                                const size_t threshold){

    size_t count = 0;
    auto image_buffer = read_mnist_file(image_file, 0x803, &count);

    if(!image_buffer || count == 0){
        return false;
    }

    if( limit > 0 && limit < (int)count){
        count = limit;
    }

    //read image data
    auto rows = read_header(image_buffer, 2);
    auto cols = read_header(image_buffer, 3);

    auto image_data_buffer = reinterpret_cast<unsigned char*>(image_buffer.get() + 16);

    size_t size = count * rows * cols;
    T* data = new T[size];
    for(size_t i = 0; i < size; i++){
        if(threshold > 0){
            data[i] = static_cast<T>(*image_data_buffer++) > threshold ? 1 : 0;
        }else{
            data[i] = static_cast<T>(*image_data_buffer++) / 255;
        }
    }

    out_mat->set_shallow_data(data, count, rows * cols);

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, 0x20(%rsp)
movq %rsi, %rdx
movq %rdi, %rsi
leaq 0x10(%rsp), %rbx
movq $0x0, (%rbx)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movl $0x803, %ecx            # imm = 0x803
movq %rbx, %r8
callq 0x12272
movq (%r12), %r13
testq %r13, %r13
setne %cl
movq (%rbx), %rax
testq %rax, %rax
setne %bl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x14bb6
testl %ebp, %ebp
setg %cl
cmpl %ebp, %eax
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0x14ad6
movl %ebp, %eax
movq %rax, 0x10(%rsp)
movl 0x8(%r13), %ebp
movl 0xc(%r13), %r14d
movl $0xffffff, %eax         # imm = 0xFFFFFF
movl %ebp, %ecx
andl %eax, %ecx
bswapl %ecx
sarl $0x18, %ebp
orl %ecx, %ebp
andl %r14d, %eax
bswapl %eax
sarl $0x18, %r14d
orl %eax, %r14d
movq 0x10(%rsp), %rcx
movslq %ebp, %rax
movq %rcx, 0x18(%rsp)
imulq %rcx, %rax
movslq %r14d, %r12
imulq %rax, %r12
movq %r12, %rax
shrq $0x3e, %rax
leaq (,%r12,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
testq %r12, %r12
je 0x14b9d
testq %r15, %r15
js 0x14b42
cvtsi2ss %r15, %xmm0
jmp 0x14b5a
movq %r15, %rcx
shrq %rcx
movl %r15d, %edx
andl $0x1, %edx
orq %rcx, %rdx
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
xorl %ecx, %ecx
movss 0x254a4(%rip), %xmm1    # 0x3a008
movss 0x254a0(%rip), %xmm2    # 0x3a00c
movzbl 0x10(%r13,%rcx), %edx
xorps %xmm4, %xmm4
cvtsi2ss %edx, %xmm4
movaps %xmm4, %xmm3
divss %xmm2, %xmm3
testq %r15, %r15
je 0x14b90
movaps %xmm0, %xmm3
cmpltss %xmm4, %xmm3
andps %xmm1, %xmm3
movss %xmm3, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x14b6c
imull %ebp, %r14d
movslq %r14d, %rcx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
callq 0x1a00a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x14bc5
callq 0x9420
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x14be8
callq 0x9420
movq %rbx, %rdi
callq 0x95d0