// File: /workspace/github_cpp_makefile_star2_O2/shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::read_tga_file(char const*)
bool TGAImage::read_tga_file(const char *filename) {
    if (data) delete [] data;
    data = NULL;
    std::ifstream in;
    in.open (filename, std::ios::binary);
    if (!in.is_open()) {
        std::cerr << "can't open file " << filename << "\n";
        in.close();
        return false;
    }
    TGA_Header header;
    in.read((char *)&header, sizeof(header));
    if (!in.good()) {
        in.close();
        std::cerr << "an error occured while reading the header\n";
        return false;
    }
    width   = header.width;
    height  = header.height;
    bytespp = header.bitsperpixel>>3;
    if (width<=0 || height<=0 || (bytespp!=GRAYSCALE && bytespp!=RGB && bytespp!=RGBA)) {
        in.close();
        std::cerr << "bad bpp (or width/height) value\n";
        return false;
    }
    unsigned long nbytes = bytespp*width*height;
    data = new unsigned char[nbytes];
    if (3==header.datatypecode || 2==header.datatypecode) {
        in.read((char *)data, nbytes);
        if (!in.good()) {
            in.close();
            std::cerr << "an error occured while reading the data\n";
            return false;
        }
    } else if (10==header.datatypecode||11==header.datatypecode) {
        if (!load_rle_data(in)) {
            in.close();
            std::cerr << "an error occured while reading the data\n";
            return false;
        }
    } else {
        in.close();
        std::cerr << "unknown file format " << (int)header.datatypecode << "\n";
        return false;
    }
    if (!(header.imagedescriptor & 0x20)) {
        flip_vertically();
    }
    if (header.imagedescriptor & 0x10) {
        flip_horizontally();
    }
    std::cerr << width << "x" << height << "/" << bytespp*8 << "\n";
    in.close();
    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x34eb
callq 0x2200
andq $0x0, (%rbx)
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x21c0
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2140
leaq 0x90(%rsp), %rdi
callq 0x2080
testb %al, %al
je 0x3550
leaq 0x18(%rsp), %rdi
leaq 0x6(%rsp), %rsi
pushq $0x12
popq %rdx
callq 0x21f0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x38(%rsp,%rax)
je 0x3589
leaq 0x18(%rsp), %rdi
callq 0x2070
leaq 0x1b3e(%rip), %rsi      # 0x508c
jmp 0x35cf
movq 0x4a99(%rip), %rdi      # 0x7ff0
leaq 0x1b1d(%rip), %rsi      # 0x507b
callq 0x21a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21a0
leaq 0x1b8a(%rip), %rsi      # 0x50ff
movq %rax, %rdi
callq 0x21a0
leaq 0x18(%rsp), %rdi
callq 0x2070
jmp 0x35db
movswl 0x12(%rsp), %eax
testl %eax, %eax
setle %sil
movl %eax, 0x8(%rbx)
movzwl 0x14(%rsp), %ecx
movswl %cx, %r14d
movl %r14d, 0xc(%rbx)
movb 0x16(%rsp), %dl
sarb $0x3, %dl
movsbl %dl, %ecx
movl %ecx, 0x10(%rbx)
testw %r14w, %r14w
setle %dil
orb %sil, %dil
je 0x35f6
leaq 0x18(%rsp), %rdi
callq 0x2070
leaq 0x1ae8(%rip), %rsi      # 0x50b7
movq 0x4a1a(%rip), %rdi      # 0x7ff0
callq 0x21a0
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x2090
movl %ebx, %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
cmpb $0x4, %dl
ja 0x35be
movzbl %dl, %edx
pushq $0x1a
popq %rsi
btl %edx, %esi
jae 0x35be
imull %eax, %r14d
imull %ecx, %r14d
movq %r14, %rdi
callq 0x2030
movq %rax, (%rbx)
movzbl 0x8(%rsp), %ecx
andl $-0x2, %ecx
cmpl $0xa, %ecx
je 0x366c
cmpl $0x2, %ecx
jne 0x3703
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x21f0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x38(%rsp,%rax)
je 0x367d
leaq 0x18(%rsp), %rdi
callq 0x2070
leaq 0x1a78(%rip), %rsi      # 0x50d8
movq 0x4989(%rip), %rdi      # 0x7ff0
jmp 0x3737
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x3762
testb %al, %al
je 0x364f
movb 0x17(%rsp), %al
testb $0x20, %al
jne 0x3691
movq %rbx, %rdi
callq 0x38d6
movb 0x17(%rsp), %al
testb $0x10, %al
je 0x369d
movq %rbx, %rdi
callq 0x39b2
movl 0x8(%rbx), %esi
movq 0x4949(%rip), %rdi      # 0x7ff0
callq 0x22c0
leaq 0x1a63(%rip), %rsi      # 0x5116
movq %rax, %rdi
callq 0x21a0
movl 0xc(%rbx), %esi
movq %rax, %rdi
callq 0x22c0
leaq 0x1a4b(%rip), %rsi      # 0x5118
movq %rax, %rdi
callq 0x21a0
movl 0x10(%rbx), %esi
shll $0x3, %esi
movq %rax, %rdi
callq 0x22c0
leaq 0x1a15(%rip), %rsi      # 0x50ff
movq %rax, %rdi
callq 0x21a0
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x2070
jmp 0x35dd
leaq 0x18(%rsp), %rdi
callq 0x2070
movq 0x48dc(%rip), %rdi      # 0x7ff0
leaq 0x19e6(%rip), %rsi      # 0x5101
callq 0x21a0
movsbl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x19c8(%rip), %rsi      # 0x50ff
callq 0x21a0
jmp 0x35db
jmp 0x3745
jmp 0x3745
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x2a25