// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
testl %esi, %esi
setg %al
testl %edx, %edx
setg %cl
andb %al, %cl
movq (%rdi), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0x4122
movl %edx, %r14d
movl %esi, %r15d
movb %al, 0xf(%rsp)
movl %edx, %eax
imull %esi, %eax
movq %rdi, %rbx
movslq 0x10(%rdi), %r13
imull %r13d, %eax
testl %eax, %eax
pushq $-0x1
popq %rcx
cmovnsl %eax, %ecx
movslq %ecx, %rdi
callq 0x2030
movl %r13d, %ecx
imull %r15d, %ecx
movl %ecx, 0x34(%rsp)
movslq %ecx, %rcx
movq %rcx, 0x70(%rsp)
movl 0x8(%rbx), %ebp
movq %rbx, 0x58(%rsp)
movl 0xc(%rbx), %ecx
movl %ebp, %edx
imull %r13d, %edx
movl %edx, 0x18(%rsp)
movl %ebp, %edx
subl %r15d, %edx
movl %edx, 0x14(%rsp)
movl %r13d, %edx
negl %edx
leal (%rcx,%rcx), %esi
movl %esi, 0x30(%rsp)
movl %edx, 0x10(%rsp)
movslq %edx, %rdx
movq %rdx, 0x68(%rsp)
xorl %edx, %edx
testl %ebp, %ebp
movl $0x0, %esi
cmovgl %ebp, %esi
movl %esi, 0x40(%rsp)
testl %ecx, %ecx
movl $0x0, %esi
movq %rcx, 0x88(%rsp)
cmovgl %ecx, %esi
movl %esi, 0x20(%rsp)
movq %rax, 0x50(%rsp)
addq %r13, %rax
movq %rax, 0x60(%rsp)
xorl %esi, %esi
xorl %eax, %eax
xorl %ecx, %ecx
movl %r14d, 0x1c(%rsp)
movl %r15d, 0x38(%rsp)
cmpl 0x20(%rsp), %ecx
je 0x40ff
movl %ecx, 0x24(%rsp)
movl %eax, 0x28(%rsp)
cltq
addq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
movl %edx, 0x2c(%rsp)
movslq %edx, %rax
addq 0x48(%rsp), %rax
movq %rax, 0x78(%rsp)
xorl %ecx, %ecx
movq 0x68(%rsp), %rdx
movl 0x14(%rsp), %eax
movl 0x10(%rsp), %r14d
movl %esi, 0x3c(%rsp)
cmpl 0x40(%rsp), %ecx
je 0x40ab
movl %ecx, 0x44(%rsp)
addq %r13, %rdx
addl %r15d, %eax
movq 0x78(%rsp), %rcx
movq %rdx, 0x90(%rsp)
leaq (%rcx,%rdx), %rbx
movslq %r14d, %r12
addq 0x80(%rsp), %r12
movl %eax, %r15d
subl %ebp, %r15d
jl 0x4092
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2170
addl %r13d, %r14d
addq %r13, %r12
movl %r15d, %eax
jmp 0x4074
movl 0x44(%rsp), %ecx
incl %ecx
movl 0x38(%rsp), %r15d
movl 0x3c(%rsp), %esi
movq 0x90(%rsp), %rdx
jmp 0x4045
movl 0x1c(%rsp), %r14d
addl %r14d, %esi
movl %esi, %ebx
movl 0x28(%rsp), %eax
subl 0x88(%rsp), %ebx
jl 0x40ec
cmpl 0x30(%rsp), %esi
jl 0x40e4
movslq %eax, %rsi
addq 0x50(%rsp), %rsi
movq 0x70(%rsp), %rdx
leaq (%rsi,%rdx), %rdi
movl %eax, %r12d
callq 0x2170
movl %r12d, %eax
addl 0x34(%rsp), %eax
movl %ebx, %esi
jmp 0x40b9
movl 0x2c(%rsp), %edx
addl 0x18(%rsp), %edx
movl 0x24(%rsp), %ecx
incl %ecx
jmp 0x3fff
movq 0x48(%rsp), %rdi
callq 0x2200
movq 0x58(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
movl %r15d, 0x8(%rax)
movl %r14d, 0xc(%rax)
movb 0xf(%rsp), %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq