// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %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 0x45ab
movl %esi, %r12d
movq %rdi, %r14
movb %al, 0x7(%rsp)
movl %edx, 0xc(%rsp)
movl %edx, %eax
imull %esi, %eax
movslq 0x10(%rdi), %r13
imull %r13d, %eax
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnsl %eax, %ecx
movslq %ecx, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %r14, 0x40(%rsp)
movl 0xc(%r14), %r14d
testl %r14d, %r14d
jle 0x4585
movl %r13d, %eax
imull %r12d, %eax
movl %eax, 0x34(%rsp)
cltq
movq %rax, 0x68(%rsp)
movq 0x40(%rsp), %rax
movl 0x8(%rax), %ebp
movl %ebp, %eax
imull %r13d, %eax
movl %eax, 0x28(%rsp)
movl %ebp, %eax
subl %r12d, %eax
movl %eax, 0x24(%rsp)
movl %r13d, %eax
negl %eax
leal (%r14,%r14), %ecx
movl %ecx, 0x30(%rsp)
movl %eax, 0x20(%rsp)
cltq
movq %rax, 0x58(%rsp)
movq 0x18(%rsp), %rax
addq %r13, %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movl $0x0, 0x8(%rsp)
movl %r12d, 0x38(%rsp)
movq %r14, 0x60(%rsp)
movl %r15d, 0x2c(%rsp)
movl %ebx, 0x14(%rsp)
testl %ebp, %ebp
jle 0x4525
movslq 0x14(%rsp), %rax
addq 0x50(%rsp), %rax
movq %rax, 0x78(%rsp)
movslq 0x8(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x70(%rsp)
xorl %eax, %eax
movq 0x58(%rsp), %rcx
movl 0x20(%rsp), %r14d
movl 0x24(%rsp), %r15d
movl %eax, 0x3c(%rsp)
addq %r13, %rcx
addl %r12d, %r15d
cmpl %ebp, %r15d
movq %rcx, 0x80(%rsp)
jl 0x450e
movq 0x70(%rsp), %rax
leaq (%rax,%rcx), %r12
movslq %r14d, %rbx
addq 0x78(%rsp), %rbx
subl %ebp, %r15d
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2170
addl %r13d, %r14d
addq %r13, %rbx
cmpl %ebp, %r15d
jge 0x44f2
movl 0x3c(%rsp), %eax
incl %eax
cmpl %ebp, %eax
movl 0x38(%rsp), %r12d
movq 0x80(%rsp), %rcx
jne 0x44ca
movl 0x2c(%rsp), %r15d
addl 0xc(%rsp), %r15d
movq 0x60(%rsp), %r14
cmpl %r14d, %r15d
movl 0x14(%rsp), %ebx
jl 0x4566
cmpl 0x30(%rsp), %r15d
jl 0x455a
movslq %ebx, %rsi
addq 0x18(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq (%rsi,%rdx), %rdi
callq 0x2170
subl %r14d, %r15d
addl 0x34(%rsp), %ebx
cmpl %r14d, %r15d
jge 0x453d
movl 0x8(%rsp), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
cmpl %r14d, %eax
jne 0x448a
movq 0x48(%rsp), %rdi
callq 0x2200
movq 0x40(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movl %r12d, 0x8(%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0xc(%rax)
movb 0x7(%rsp), %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq