// File: /workspace/github_cpp_makefile_star2_O3/ticky[P]ps2iconsys/src/ps2icon_to_obj.cpp
// Function: WriteTextureFile(PS2Icon*)
void WriteTextureFile(PS2Icon* ps2_icon)
{
	unsigned int texture_data[128*128];
	if(verbose_output)
		std::cout << " * Convert texture data from \"" << ps2_input_file << "\"..." ;
	ps2_icon->GetTextureData(texture_data);
	//manual conversion required, since WriteImage() requires 
	// GBCOLOR32 data, whose bit pattern is not fix;
	for(int i=0; i<16384; i++) {
		texture_data[i] = GhulbusGraphics::GBCOLOR32::ARGB( static_cast<int>((texture_data[i] >> 24) & 0xff),
															static_cast<int>((texture_data[i] >> 16) & 0xff),
															static_cast<int>((texture_data[i] >>  8) & 0xff),
															static_cast<int>((texture_data[i])       & 0xff) );
	}
	//in addition the texture is flipped horizontally:
	for(int row=0; row<64; row++) {
		for(int i=0; i<128; i++) {
			texture_data[row*128 + i] ^= texture_data[(127-row)*128 + i] 
				^= texture_data[row*128 + i] ^= texture_data[(127-row)*128 + i];
		}
	}

	if(verbose_output)
		std::cout << "done." << std::endl;

	if(verbose_output)
		std::cout << " * Writing texture to file \"" << texture_output_file << "\"...";
	try {
		GhulbusUtil::WriteImage(texture_output_file, texture_data, 128, 128);
	} catch( Ghulbus::gbException e ) {
		std::cout << "\nError while writing to \"" << texture_output_file << "\"" << std::endl;
		exit(1);
	}
	if(verbose_output)
		std::cout << "done." << std::endl;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10020, %rsp          # imm = 0x10020
movq %rdi, %rbx
cmpb $0x1, 0x62c3(%rip)      # 0xf270
jne 0x9022
movq 0x6022(%rip), %r14      # 0xefd8
leaq 0x1b4a(%rip), %rsi      # 0xab07
movl $0x1e, %edx
movq %r14, %rdi
callq 0x21f0
movq 0x6287(%rip), %r15      # 0xf258
testq %r15, %r15
je 0x8ff2
movq %r15, %rdi
callq 0x20c0
movq 0x5ff3(%rip), %rdi      # 0xefd8
movq %r15, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x900a
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22f0
movq 0x5fc7(%rip), %rdi      # 0xefd8
leaq 0x1aab(%rip), %rsi      # 0xaac3
movl $0x4, %edx
callq 0x21f0
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6bb4
leaq 0xfe20(%rsp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movaps (%r14,%rdx,4), %xmm0
xorps (%rax,%rdx,4), %xmm0
movaps %xmm0, (%r14,%rdx,4)
xorps (%rax,%rdx,4), %xmm0
movaps %xmm0, (%rax,%rdx,4)
xorps (%r14,%rdx,4), %xmm0
movaps %xmm0, (%r14,%rdx,4)
addq $0x4, %rdx
cmpq $0x80, %rdx
jne 0x903e
incq %rcx
addq $-0x200, %rax           # imm = 0xFE00
addq $0x200, %r14            # imm = 0x200
cmpq $0x40, %rcx
jne 0x903c
movb 0x61e9(%rip), %al       # 0xf270
testb %al, %al
je 0x90d3
movq 0x5f46(%rip), %rbx      # 0xefd8
leaq 0x1a04(%rip), %rsi      # 0xaa9d
movl $0x5, %edx
movq %rbx, %rdi
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movb 0x619d(%rip), %al       # 0xf270
testb $0x1, %al
je 0x9149
movq 0x5efa(%rip), %rbx      # 0xefd8
leaq 0x1a41(%rip), %rsi      # 0xab26
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x21f0
movq 0x616f(%rip), %r14      # 0xf268
testq %r14, %r14
je 0x911a
movq %r14, %rdi
callq 0x20c0
movq 0x5ecb(%rip), %rdi      # 0xefd8
movq %r14, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x9131
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22f0
movq 0x5ea0(%rip), %rdi      # 0xefd8
leaq 0x1984(%rip), %rsi      # 0xaac3
movl $0x4, %edx
callq 0x21f0
movq 0x6118(%rip), %rdi      # 0xf268
leaq 0x20(%rsp), %rsi
movl $0x80, %edx
movl $0x80, %ecx
callq 0x76d8
cmpb $0x1, 0x6105(%rip)      # 0xf270
jne 0x91af
movq 0x5e64(%rip), %rbx      # 0xefd8
leaq 0x1922(%rip), %rsi      # 0xaa9d
movl $0x5, %edx
movq %rbx, %rdi
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
addq $0x10020, %rsp          # imm = 0x10020
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x924b
movq %rbx, %rdi
callq 0x2220
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x8322
movq %rbx, %rdi
callq 0x20b0
movq 0x5dec(%rip), %rdi      # 0xefd8
leaq 0x18fa(%rip), %rsi      # 0xaaed
movl $0x19, %edx
callq 0x21f0
movq 0x6064(%rip), %rsi      # 0xf268
movq 0x5dcd(%rip), %rdi      # 0xefd8
callq 0x21b0
movq %rax, %rbx
leaq 0x1830(%rip), %rsi      # 0xaa4a
movl $0x1, %edx
movq %rax, %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2210
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x833c
callq 0x22e0
movq %rbx, %rdi
callq 0x2330
movq %rax, %rdi
callq 0x7068