// File: /workspace/github_cpp_makefile_star2_O1/dannye[P]jed/src/encoder.cpp
// Function: readBMP(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
BMPImage readBMP(const std::string& filename) {
    BMPImage image;

    // open file
    std::cout << "Reading " << filename << "...\n";
    std::ifstream inFile(filename, std::ios::in | std::ios::binary);
    if (!inFile.is_open()) {
        std::cout << "Error - Error opening input file\n";
        return image;
    }

    if (inFile.get() != 'B' || inFile.get() != 'M') {
        std::cout << "Error - Invalid BMP file\n";
        inFile.close();
        return image;
    }

    getInt(inFile); // size
    getInt(inFile); // nothing
    if (getInt(inFile) != 0x1A) {
        std::cout << "Error - Invalid offset\n";
        inFile.close();
        return image;
    }
    if (getInt(inFile) != 12) {
        std::cout << "Error - Invalid DIB size\n";
        inFile.close();
        return image;
    }
    image.width = getShort(inFile);
    image.height = getShort(inFile);
    if (getShort(inFile) != 1) {
        std::cout << "Error - Invalid number of planes\n";
        inFile.close();
        return image;
    }
    if (getShort(inFile) != 24) {
        std::cout << "Error - Invalid bit depth\n";
        inFile.close();
        return image;
    }

    if (image.height == 0 || image.width == 0) {
        std::cout << "Error - Invalid dimensions\n";
        inFile.close();
        return image;
    }

    image.blockHeight = (image.height + 7) / 8;
    image.blockWidth = (image.width + 7) / 8;

    image.blocks = new (std::nothrow) Block[image.blockHeight * image.blockWidth];
    if (image.blocks == nullptr) {
        std::cout << "Error - Memory error\n";
        inFile.close();
        return image;
    }

    const uint paddingSize = image.width % 4;

    for (uint y = image.height - 1; y < image.height; --y) {
        const uint blockRow = y / 8;
        const uint pixelRow = y % 8;
        for (uint x = 0; x < image.width; ++x) {
            const uint blockColumn = x / 8;
            const uint pixelColumn = x % 8;
            const uint blockIndex = blockRow * image.blockWidth + blockColumn;
            const uint pixelIndex = pixelRow * 8 + pixelColumn;
            image.blocks[blockIndex].b[pixelIndex] = inFile.get();
            image.blocks[blockIndex].g[pixelIndex] = inFile.get();
            image.blocks[blockIndex].r[pixelIndex] = inFile.get();
        }
        for (uint i = 0; i < paddingSize; ++i) {
            inFile.get();
        }
    }

    inFile.close();
    return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rsi, %r14
movq %rdi, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x4b8d(%rip), %r15      # 0x6fd8
leaq 0x2c7e(%rip), %rsi      # 0x50d0
movl $0x8, %edx
movq %r15, %rdi
callq 0x2120
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x2120
leaq 0x2c64(%rip), %rsi      # 0x50d9
movl $0x4, %edx
movq %rax, %rdi
callq 0x2120
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x2190
leaq 0xb8(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x25bd
leaq 0x40(%rsp), %rdi
callq 0x2130
movl $0x19, %r14d
leaq 0x2c40(%rip), %r15      # 0x5100
cmpl $0x42, %eax
jne 0x260b
leaq 0x40(%rsp), %rdi
callq 0x2130
cmpl $0x4d, %eax
jne 0x260b
leaq 0x40(%rsp), %rdi
callq 0x23bc
leaq 0x40(%rsp), %rdi
callq 0x23bc
leaq 0x40(%rsp), %rdi
callq 0x23bc
cmpl $0x1a, %eax
jne 0x25d7
leaq 0x40(%rsp), %rdi
callq 0x23bc
cmpl $0xc, %eax
jne 0x25e6
leaq 0x40(%rsp), %rdi
callq 0x2130
movl %eax, 0xc(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x2130
movl %eax, %r14d
shll $0x8, %r14d
addl 0xc(%rsp), %r14d
movl %r14d, 0x4(%r13)
leaq 0x40(%rsp), %rdi
callq 0x2130
movl %eax, %r12d
leaq 0x40(%rsp), %rdi
callq 0x2130
shll $0x8, %eax
addl %r12d, %eax
movq %rax, 0x28(%rsp)
movl %eax, (%r13)
leaq 0x40(%rsp), %rdi
callq 0x2130
movl %eax, %r12d
leaq 0x40(%rsp), %rdi
callq 0x2130
shll $0x8, %eax
addl %r12d, %eax
cmpl $0x1, %eax
jne 0x25ef
leaq 0x40(%rsp), %rdi
callq 0x23fe
cmpl $0x18, %eax
jne 0x25fe
movq 0x28(%rsp), %r15
testl %r15d, %r15d
sete %al
testl %r14d, %r14d
sete %cl
orb %al, %cl
je 0x2646
movl $0x1b, %r14d
leaq 0x2bce(%rip), %r15      # 0x5189
jmp 0x260b
movq 0x4a14(%rip), %rdi      # 0x6fd8
leaq 0x2b13(%rip), %rsi      # 0x50de
movl $0x21, %edx
callq 0x2120
jmp 0x2627
movl $0x17, %r14d
leaq 0x2b36(%rip), %r15      # 0x511a
jmp 0x260b
leaq 0x2b45(%rip), %r15      # 0x5132
jmp 0x260b
movl $0x21, %r14d
leaq 0x2b50(%rip), %r15      # 0x514c
jmp 0x260b
movl $0x1a, %r14d
leaq 0x2b63(%rip), %r15      # 0x516e
movq 0x49c6(%rip), %rdi      # 0x6fd8
movq %r15, %rsi
movq %r14, %rdx
callq 0x2120
leaq 0x40(%rsp), %rdi
callq 0x2040
leaq 0x40(%rsp), %rdi
callq 0x2060
movq %r13, %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x7(%r15), %ebx
shrl $0x3, %ebx
movl %ebx, 0x10(%r13)
leal 0x7(%r14), %eax
shrl $0x3, %eax
movq %r13, 0x20(%rsp)
movl %eax, 0x14(%r13)
movl %eax, 0x1c(%rsp)
imull %eax, %ebx
movq %rbx, %rax
shlq $0x8, %rax
leaq (%rax,%rax,2), %r13
movq 0x493e(%rip), %rsi      # 0x6fb8
movq %r13, %rdi
callq 0x21b0
testq %rax, %rax
je 0x26af
movq %rax, %r12
testq %rbx, %rbx
je 0x26b2
xorl %ebx, %ebx
leaq (%r12,%rbx), %rdi
movl $0x300, %edx            # imm = 0x300
xorl %esi, %esi
callq 0x20a0
addq $0x300, %rbx            # imm = 0x300
cmpq %rbx, %r13
jne 0x2691
jmp 0x26b2
xorl %r12d, %r12d
movq 0x20(%rsp), %r13
movq %r12, 0x8(%r13)
testq %r12, %r12
je 0x27ad
testl %r15d, %r15d
je 0x2799
andl $0x3, 0xc(%rsp)
leal -0x1(%r15), %eax
movq %rax, 0x10(%rsp)
leaq 0x40(%rsp), %r13
movq %r14, 0x30(%rsp)
movq %r12, 0x38(%rsp)
testl %r14d, %r14d
je 0x2764
movq 0x10(%rsp), %rax
movl %eax, %r15d
shrl $0x3, %r15d
leal (,%rax,8), %ebp
andl $0x38, %ebp
imull 0x1c(%rsp), %r15d
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x2130
movl %r14d, %r12d
andl $0x7, %r12d
orl %ebp, %r12d
movl %r14d, %ecx
shrl $0x3, %ecx
addl %r15d, %ecx
leaq (%rcx,%rcx,2), %rbx
shlq $0x8, %rbx
addq 0x38(%rsp), %rbx
movl %eax, 0x200(%rbx,%r12,4)
movq %r13, %rdi
callq 0x2130
movl %eax, 0x100(%rbx,%r12,4)
movq %r13, %rdi
callq 0x2130
movl %eax, (%rbx,%r12,4)
incl %r14d
cmpl %r14d, 0x30(%rsp)
jne 0x270e
movl 0xc(%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r15
je 0x2784
movq %r13, %rdi
callq 0x2130
decl %ebx
jne 0x2778
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
cmpl %r15d, %eax
jb 0x26ea
leaq 0x40(%rsp), %rdi
callq 0x2040
movq 0x20(%rsp), %r13
jmp 0x2627
movl $0x15, %r14d
leaq 0x29eb(%rip), %r15      # 0x51a5
jmp 0x260b
jmp 0x27c5
jmp 0x27c5
jmp 0x27c5
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x21c0