// File: 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 %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x258(%rbp)
cmpq $0x0, (%rax)
je 0x6599
movq -0x258(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x260(%rbp)
cmpq $0x0, %rax
je 0x6597
movq -0x260(%rbp), %rdi
callq 0x2230
jmp 0x6599
movq -0x258(%rbp), %rax
movq $0x0, (%rax)
leaq -0x220(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x2200
movq -0x268(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x2160
jmp 0x65d1
leaq -0x220(%rbp), %rdi
callq 0x22e0
movb %al, -0x269(%rbp)
jmp 0x65e5
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x6683
jmp 0x65f5
movq 0x59f4(%rip), %rdi      # 0xbff0
leaq 0x1a68(%rip), %rsi      # 0x806b
callq 0x21c0
movq %rax, -0x278(%rbp)
jmp 0x6611
movq -0x278(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x21c0
movq %rax, -0x280(%rbp)
jmp 0x662a
movq -0x280(%rbp), %rdi
leaq 0x1ab7(%rip), %rsi      # 0x80ef
callq 0x21c0
jmp 0x663f
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x664d
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x20b0
jmp 0x6a96
leaq -0x220(%rbp), %rdi
leaq -0x242(%rbp), %rsi
movl $0x12, %edx
callq 0x2220
jmp 0x669d
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x281(%rbp)
jmp 0x66bd
movb -0x281(%rbp), %al
testb $0x1, %al
jne 0x66ff
jmp 0x66c9
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x66d7
movq 0x5912(%rip), %rdi      # 0xbff0
leaq 0x1997(%rip), %rsi      # 0x807c
callq 0x21c0
jmp 0x66ec
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
movq -0x258(%rbp), %rax
movswl -0x236(%rbp), %ecx
movl %ecx, 0x8(%rax)
movswl -0x234(%rbp), %ecx
movl %ecx, 0xc(%rax)
movsbl -0x232(%rbp), %ecx
sarl $0x3, %ecx
movl %ecx, 0x10(%rax)
cmpl $0x0, 0x8(%rax)
jle 0x6761
movq -0x258(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jle 0x6761
movq -0x258(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0x6797
movq -0x258(%rbp), %rax
cmpl $0x3, 0x10(%rax)
je 0x6797
movq -0x258(%rbp), %rax
cmpl $0x4, 0x10(%rax)
je 0x6797
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x676f
movq 0x587a(%rip), %rdi      # 0xbff0
leaq 0x192a(%rip), %rsi      # 0x80a7
callq 0x21c0
jmp 0x6784
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
movq -0x258(%rbp), %rcx
movl 0x10(%rcx), %eax
movl 0x8(%rcx), %edx
movl 0xc(%rcx), %ecx
imull %edx, %eax
imull %ecx, %eax
cltq
movq %rax, -0x250(%rbp)
movq -0x250(%rbp), %rdi
callq 0x2040
movq %rax, -0x290(%rbp)
jmp 0x67cb
movq -0x258(%rbp), %rax
movq -0x290(%rbp), %rcx
movq %rcx, (%rax)
movsbl -0x240(%rbp), %ecx
movl $0x3, %eax
cmpl %ecx, %eax
je 0x6800
movsbl -0x240(%rbp), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x6886
movq -0x258(%rbp), %rax
movq (%rax), %rsi
movq -0x250(%rbp), %rdx
leaq -0x220(%rbp), %rdi
callq 0x2220
jmp 0x681f
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x291(%rbp)
jmp 0x683f
movb -0x291(%rbp), %al
testb $0x1, %al
jne 0x6881
jmp 0x684b
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x6859
movq 0x5790(%rip), %rdi      # 0xbff0
leaq 0x1861(%rip), %rsi      # 0x80c8
callq 0x21c0
jmp 0x686e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
jmp 0x6975
movsbl -0x240(%rbp), %ecx
movl $0xa, %eax
cmpl %ecx, %eax
je 0x68a6
movsbl -0x240(%rbp), %ecx
movl $0xb, %eax
cmpl %ecx, %eax
jne 0x6905
movq -0x258(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x6ab0
movb %al, -0x292(%rbp)
jmp 0x68c1
movb -0x292(%rbp), %al
testb $0x1, %al
jne 0x6903
jmp 0x68cd
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x68db
movq 0x570e(%rip), %rdi      # 0xbff0
leaq 0x17df(%rip), %rsi      # 0x80c8
callq 0x21c0
jmp 0x68f0
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
jmp 0x6973
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x6913
movq 0x56d6(%rip), %rdi      # 0xbff0
leaq 0x17d0(%rip), %rsi      # 0x80f1
callq 0x21c0
movq %rax, -0x2a0(%rbp)
jmp 0x692f
movq -0x2a0(%rbp), %rdi
movsbl -0x240(%rbp), %esi
callq 0x2340
movq %rax, -0x2a8(%rbp)
jmp 0x694b
movq -0x2a8(%rbp), %rdi
leaq 0x1796(%rip), %rsi      # 0x80ef
callq 0x21c0
jmp 0x6960
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x6a7c
jmp 0x6975
movsbl -0x231(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x6994
movq -0x258(%rbp), %rdi
callq 0x6d30
jmp 0x6992
jmp 0x6994
movsbl -0x231(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x69b3
movq -0x258(%rbp), %rdi
callq 0x6e30
jmp 0x69b1
jmp 0x69b3
movq -0x258(%rbp), %rax
movl 0x8(%rax), %esi
movq 0x562c(%rip), %rdi      # 0xbff0
callq 0x2340
movq %rax, -0x2b0(%rbp)
jmp 0x69d2
movq -0x2b0(%rbp), %rdi
leaq 0x1726(%rip), %rsi      # 0x8106
callq 0x21c0
movq %rax, -0x2b8(%rbp)
jmp 0x69ee
movq -0x2b8(%rbp), %rdi
movq -0x258(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x2340
movq %rax, -0x2c0(%rbp)
jmp 0x6a0d
movq -0x2c0(%rbp), %rdi
leaq 0x16ed(%rip), %rsi      # 0x8108
callq 0x21c0
movq %rax, -0x2c8(%rbp)
jmp 0x6a29
movq -0x2c8(%rbp), %rdi
movq -0x258(%rbp), %rax
movl 0x10(%rax), %esi
shll $0x3, %esi
callq 0x2340
movq %rax, -0x2d0(%rbp)
jmp 0x6a4b
movq -0x2d0(%rbp), %rdi
leaq 0x1696(%rip), %rsi      # 0x80ef
callq 0x21c0
jmp 0x6a60
leaq -0x220(%rbp), %rdi
callq 0x2090
jmp 0x6a6e
movb $0x1, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x20b0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
jmp 0x6a98
movq -0x228(%rbp), %rdi
callq 0x2350
movq %rax, %rdi
callq 0x30f0
nopl (%rax)