// File: /workspace/github_cpp_makefile_star2_O1/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, 0x6018(%rip)      # 0xf270
jne 0x92cd
movq 0x5d77(%rip), %r14      # 0xefd8
leaq 0x186b(%rip), %rsi      # 0xaad3
movl $0x1e, %edx
movq %r14, %rdi
callq 0x21f0
movq 0x5fdc(%rip), %r15      # 0xf258
testq %r15, %r15
je 0x929d
movq %r15, %rdi
callq 0x20c0
movq 0x5d48(%rip), %rdi      # 0xefd8
movq %r15, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x92b5
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22f0
movq 0x5d1c(%rip), %rdi      # 0xefd8
leaq 0x17cc(%rip), %rsi      # 0xaa8f
movl $0x4, %edx
callq 0x21f0
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6db6
leaq 0xfe20(%rsp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r14,%rdx,4), %esi
xorl (%rax,%rdx,4), %esi
movl %esi, (%r14,%rdx,4)
xorl (%rax,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
xorl %esi, (%r14,%rdx,4)
incq %rdx
cmpq $0x80, %rdx
jne 0x92e9
incq %rcx
addq $-0x200, %rax           # imm = 0xFE00
addq $0x200, %r14            # imm = 0x200
cmpq $0x40, %rcx
jne 0x92e7
cmpb $0x0, 0x5f49(%rip)      # 0xf270
je 0x936b
movq 0x5ca8(%rip), %rbx      # 0xefd8
leaq 0x1732(%rip), %rsi      # 0xaa69
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
cmpb $0x1, 0x5efe(%rip)      # 0xf270
jne 0x93e6
movq 0x5c5d(%rip), %rbx      # 0xefd8
leaq 0x1770(%rip), %rsi      # 0xaaf2
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x21f0
movq 0x5ed2(%rip), %r14      # 0xf268
testq %r14, %r14
je 0x93b7
movq %r14, %rdi
callq 0x20c0
movq 0x5c2e(%rip), %rdi      # 0xefd8
movq %r14, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x93ce
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22f0
movq 0x5c03(%rip), %rdi      # 0xefd8
leaq 0x16b3(%rip), %rsi      # 0xaa8f
movl $0x4, %edx
callq 0x21f0
movq 0x5e7b(%rip), %rdi      # 0xf268
leaq 0x20(%rsp), %rsi
movl $0x80, %edx
movl $0x80, %ecx
callq 0x79e0
cmpb $0x1, 0x5e68(%rip)      # 0xf270
jne 0x944c
movq 0x5bc7(%rip), %rbx      # 0xefd8
leaq 0x1651(%rip), %rsi      # 0xaa69
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 0x94e8
movq %rbx, %rdi
callq 0x2220
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x85ea
movq %rbx, %rdi
callq 0x20b0
movq 0x5b4f(%rip), %rdi      # 0xefd8
leaq 0x1629(%rip), %rsi      # 0xaab9
movl $0x19, %edx
callq 0x21f0
movq 0x5dc7(%rip), %rsi      # 0xf268
movq 0x5b30(%rip), %rdi      # 0xefd8
callq 0x21b0
movq %rax, %rbx
leaq 0x155f(%rip), %rsi      # 0xaa16
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 0x8604
callq 0x22e0
movq %rbx, %rdi
callq 0x2330
movq %rax, %rdi
callq 0x7272