// File: shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::scale(int, int)
bool TGAImage::scale(int w, int h) {
    if (w<=0 || h<=0 || !data) return false;
    unsigned char *tdata = new unsigned char[w*h*bytespp];
    int nscanline = 0;
    int oscanline = 0;
    int erry = 0;
    unsigned long nlinebytes = w*bytespp;
    unsigned long olinebytes = width*bytespp;
    for (int j=0; j<height; j++) {
        int errx = width-w;
        int nx   = -bytespp;
        int ox   = -bytespp;
        for (int i=0; i<width; i++) {
            ox += bytespp;
            errx += w;
            while (errx>=(int)width) {
                errx -= width;
                nx += bytespp;
                memcpy(tdata+nscanline+nx, data+oscanline+ox, bytespp);
            }
        }
        erry += h;
        oscanline += olinebytes;
        while (erry>=(int)height) {
            if (erry>=(int)height<<1) // it means we jump over a scanline
                memcpy(tdata+nscanline+nlinebytes, tdata+nscanline, nlinebytes);
            erry -= height;
            nscanline += nlinebytes;
        }
    }
    delete [] data;
    data = tdata;
    width = w;
    height = h;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x78a0
cmpl $0x0, -0x18(%rbp)
jle 0x78a0
movq -0x60(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x78a9
movb $0x0, -0x1(%rbp)
jmp 0x7a7f
movq -0x60(%rbp), %rcx
movl -0x14(%rbp), %eax
imull -0x18(%rbp), %eax
imull 0x10(%rcx), %eax
movslq %eax, %rdi
movq $-0x1, %rax
cmpq $0x0, %rdi
cmovlq %rax, %rdi
callq 0x2040
movq -0x60(%rbp), %rcx
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x14(%rbp), %eax
imull 0x10(%rcx), %eax
cltq
movq %rax, -0x38(%rbp)
movl 0x8(%rcx), %eax
imull 0x10(%rcx), %eax
cltq
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x60(%rbp), %rcx
movl -0x44(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x7a4a
movq -0x60(%rbp), %rcx
movl 0x8(%rcx), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x48(%rbp)
xorl %eax, %eax
subl 0x10(%rcx), %eax
movl %eax, -0x4c(%rbp)
xorl %eax, %eax
subl 0x10(%rcx), %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x60(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x79c5
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x14(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x60(%rbp), %rcx
movl -0x48(%rbp), %eax
cmpl 0x8(%rcx), %eax
jl 0x79b5
movq -0x60(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x48(%rbp), %ecx
subl %edx, %ecx
movl %ecx, -0x48(%rbp)
movl 0x10(%rax), %ecx
addl -0x4c(%rbp), %ecx
movl %ecx, -0x4c(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rcx
addq %rcx, %rdi
movslq -0x4c(%rbp), %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movslq -0x28(%rbp), %rcx
addq %rcx, %rsi
movslq -0x50(%rbp), %rcx
addq %rcx, %rsi
movslq 0x10(%rax), %rdx
callq 0x2190
jmp 0x7963
jmp 0x79b7
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x7941
movl -0x18(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rcx
movslq -0x28(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x28(%rbp)
movq -0x60(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0xc(%rcx), %eax
jl 0x7a3a
movq -0x60(%rbp), %rcx
movl -0x2c(%rbp), %eax
movl 0xc(%rcx), %ecx
shll %ecx
cmpl %ecx, %eax
jl 0x7a1b
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rax
addq %rax, %rdi
addq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rax
addq %rax, %rsi
movq -0x38(%rbp), %rdx
callq 0x2190
movq -0x60(%rbp), %rax
movl 0xc(%rax), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rcx
movslq -0x24(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x24(%rbp)
jmp 0x79dc
jmp 0x7a3c
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x790d
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x7a64
movq -0x68(%rbp), %rdi
callq 0x2230
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, (%rax)
movl -0x14(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x18(%rbp), %ecx
movl %ecx, 0xc(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)