// File: 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 %rbp
movq %rsp, %rbp
subq $0x10070, %rsp          # imm = 0x10070
movq %rdi, -0x8(%rbp)
testb $0x1, 0x848a(%rip)      # 0x18280
je 0xfe29
movq 0x81d9(%rip), %rdi      # 0x17fd8
leaq 0x1d27(%rip), %rsi      # 0x11b2d
callq 0x21b0
movq %rax, %rdi
movq 0x8453(%rip), %rsi      # 0x18268
callq 0x21b0
movq %rax, %rdi
leaq 0x1cc5(%rip), %rsi      # 0x11ae9
callq 0x21b0
movq -0x8(%rbp), %rdi
leaq -0x10010(%rbp), %rsi
callq 0xbd30
movl $0x0, -0x10014(%rbp)
cmpl $0x4000, -0x10014(%rbp) # imm = 0x4000
jge 0xfed5
movslq -0x10014(%rbp), %rax
movl -0x10010(%rbp,%rax,4), %edi
shrl $0x18, %edi
andl $0xff, %edi
movslq -0x10014(%rbp), %rax
movl -0x10010(%rbp,%rax,4), %esi
shrl $0x10, %esi
andl $0xff, %esi
movslq -0x10014(%rbp), %rax
movl -0x10010(%rbp,%rax,4), %edx
shrl $0x8, %edx
andl $0xff, %edx
movslq -0x10014(%rbp), %rax
movl -0x10010(%rbp,%rax,4), %ecx
andl $0xff, %ecx
callq 0xdcb0
movl %eax, %ecx
movslq -0x10014(%rbp), %rax
movl %ecx, -0x10010(%rbp,%rax,4)
movl -0x10014(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10014(%rbp)
jmp 0xfe43
movl $0x0, -0x10018(%rbp)
cmpl $0x40, -0x10018(%rbp)
jge 0xffaf
movl $0x0, -0x1001c(%rbp)
cmpl $0x80, -0x1001c(%rbp)
jge 0xff99
movl $0x7f, %eax
subl -0x10018(%rbp), %eax
shll $0x7, %eax
addl -0x1001c(%rbp), %eax
cltq
movl -0x10010(%rbp,%rax,4), %ecx
movl -0x10018(%rbp), %eax
shll $0x7, %eax
addl -0x1001c(%rbp), %eax
cltq
xorl -0x10010(%rbp,%rax,4), %ecx
movl %ecx, -0x10010(%rbp,%rax,4)
movl $0x7f, %eax
subl -0x10018(%rbp), %eax
shll $0x7, %eax
addl -0x1001c(%rbp), %eax
cltq
xorl -0x10010(%rbp,%rax,4), %ecx
movl %ecx, -0x10010(%rbp,%rax,4)
movl -0x10018(%rbp), %eax
shll $0x7, %eax
addl -0x1001c(%rbp), %eax
cltq
xorl -0x10010(%rbp,%rax,4), %ecx
movl %ecx, -0x10010(%rbp,%rax,4)
movl -0x1001c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1001c(%rbp)
jmp 0xfef6
jmp 0xff9b
movl -0x10018(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10018(%rbp)
jmp 0xfedf
testb $0x1, 0x82ca(%rip)      # 0x18280
je 0xffda
movq 0x8019(%rip), %rdi      # 0x17fd8
leaq 0x1afd(%rip), %rsi      # 0x11ac3
callq 0x21b0
movq %rax, %rdi
movq 0x7feb(%rip), %rsi      # 0x17fc0
callq 0x21e0
testb $0x1, 0x829f(%rip)      # 0x18280
je 0x10014
movq 0x7fee(%rip), %rdi      # 0x17fd8
leaq 0x1b5b(%rip), %rsi      # 0x11b4c
callq 0x21b0
movq %rax, %rdi
movq 0x8278(%rip), %rsi      # 0x18278
callq 0x21b0
movq %rax, %rdi
leaq 0x1ada(%rip), %rsi      # 0x11ae9
callq 0x21b0
movq 0x825d(%rip), %rdi      # 0x18278
leaq -0x10010(%rbp), %rsi
movl $0x80, %ecx
movl %ecx, %edx
callq 0xd780
jmp 0x10030
jmp 0x10122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10028(%rbp)
movl %eax, -0x1002c(%rbp)
movl -0x1002c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10156
movq -0x10028(%rbp), %rdi
movq %rdi, -0x10058(%rbp)
callq 0x2220
movq %rax, %rsi
leaq -0x10048(%rbp), %rdi
callq 0xf0d0
movq -0x10058(%rbp), %rdi
callq 0x20c0
movq 0x7f49(%rip), %rdi      # 0x17fd8
leaq 0x1a7d(%rip), %rsi      # 0x11b13
callq 0x21b0
movq %rax, -0x10050(%rbp)
jmp 0x100a4
movq -0x10050(%rbp), %rdi
movq 0x81c6(%rip), %rsi      # 0x18278
callq 0x21b0
movq %rax, -0x10060(%rbp)
jmp 0x100c0
movq -0x10060(%rbp), %rdi
leaq 0x19a2(%rip), %rsi      # 0x11a70
callq 0x21b0
movq %rax, -0x10068(%rbp)
jmp 0x100dc
movq -0x10068(%rbp), %rdi
movq 0x7ed6(%rip), %rsi      # 0x17fc0
callq 0x21e0
jmp 0x100f1
movl $0x1, %edi
callq 0x2210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10028(%rbp)
movl %eax, -0x1002c(%rbp)
leaq -0x10048(%rbp), %rdi
callq 0xf120
callq 0x2300
jmp 0x10120
jmp 0x10156
testb $0x1, 0x8157(%rip)      # 0x18280
je 0x1014d
movq 0x7ea6(%rip), %rdi      # 0x17fd8
leaq 0x198a(%rip), %rsi      # 0x11ac3
callq 0x21b0
movq %rax, %rdi
movq 0x7e78(%rip), %rsi      # 0x17fc0
callq 0x21e0
addq $0x10070, %rsp          # imm = 0x10070
popq %rbp
retq
movq -0x10028(%rbp), %rdi
callq 0x2340
movq %rax, %rdi
callq 0x5440
nopw (%rax,%rax)