// File: ticky[P]ps2iconsys/gbLib/src/gbImageLoader.cpp
// Function: GhulbusUtil::WriteImage(char const*, unsigned int const*, int, int)
void WriteImage(char const* fname, GhulbusGraphics::GBCOLOR const* data, int width, int height)
	{
		struct {
			unsigned char  nCharIDField;		// Number of Chars in ID Field (1 Byte)
			unsigned char  ColorMapType;		// is 1 if color map specified; must be 0 for us
			unsigned char  ImageTypeCode;		// Data Type; must be 2 for us
			unsigned char  ColorMapSpec[5];		// should be 0
			unsigned short XOrigin;				// expected to be 0
			unsigned short YOrigin;				// expected to be 0
			unsigned short Width;				// Picture Width
			unsigned short Height;				// Picture Height
			unsigned char  ImagePixelSize;		// Bits per Pixel
			unsigned char  ImageDescByte;		// Image Descriptor Byte
		} header;

		memset(&header, 0, sizeof(header));
		header.ImageTypeCode = 2;
		if((width > USHRT_MAX) || (height > USHRT_MAX)) { 
			throw( Ghulbus::gbException( Ghulbus::gbException::GB_ILLEGALPARAMETER ) );
		}
		header.Width  = static_cast<unsigned short>(width);
		header.Height = static_cast<unsigned short>(height);
		header.ImagePixelSize = 32;
		header.ImageDescByte = 0x28;		//magic number: 8 bits per channel, origin upper left, no padding

		std::ofstream  fout( fname, std::ios_base::out | std::ios_base::binary );
		if(fout.fail()) {
			throw( Ghulbus::gbException( Ghulbus::gbException::GB_FAILED, "Output file could not be opened" ) );
		}
		fout.write( reinterpret_cast<char*>(&header), sizeof(header) );
		
		for(int i=0; i<width*height; ++i) {
			unsigned char a = GhulbusGraphics::GBCOLOR32::GetA(data[i]);
			unsigned char r = GhulbusGraphics::GBCOLOR32::GetR(data[i]);
			unsigned char g = GhulbusGraphics::GBCOLOR32::GetG(data[i]);
			unsigned char b = GhulbusGraphics::GBCOLOR32::GetB(data[i]);
			fout << b << g << r << a;
		}
		if(fout.fail()) {
			throw( Ghulbus::gbException( Ghulbus::gbException::GB_FAILED, "Error while writing to output file" ) );
		}
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
leaq -0x2a(%rbp), %rdi
xorl %esi, %esi
movl $0x12, %edx
callq 0x2110
movb $0x2, -0x28(%rbp)
cmpl $0xffff, -0x14(%rbp)    # imm = 0xFFFF
jg 0xd7ef
cmpl $0xffff, -0x18(%rbp)    # imm = 0xFFFF
jle 0xd827
movl $0x18, %edi
callq 0x20f0
movq %rax, %rdi
movq %rdi, -0x250(%rbp)
movl $0x3, %esi
callq 0xf060
movq -0x250(%rbp), %rdi
leaq 0xa575(%rip), %rsi      # 0x17d90
leaq 0x192e(%rip), %rdx      # 0xf150
callq 0x2340
movw -0x14(%rbp), %ax
movw %ax, -0x1e(%rbp)
movw -0x18(%rbp), %ax
movw %ax, -0x1c(%rbp)
movb $0x20, -0x1a(%rbp)
movb $0x28, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x260(%rbp)
movl $0x10, %edi
movl $0x4, %esi
callq 0x9de0
movq -0x260(%rbp), %rsi
movl %eax, %edx
leaq -0x230(%rbp), %rdi
callq 0x2200
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x251(%rbp)
jmp 0xd88e
movb -0x251(%rbp), %al
testb $0x1, %al
jne 0xd89a
jmp 0xd901
movl $0x18, %edi
callq 0x20f0
movq %rax, %rdi
movq %rdi, -0x268(%rbp)
leaq 0x3b69(%rip), %rdx      # 0x1141e
movl $0x1, %esi
callq 0xf0b0
movq -0x268(%rbp), %rdi
leaq 0xa4c3(%rip), %rsi      # 0x17d90
leaq 0x187c(%rip), %rdx      # 0xf150
callq 0x2340
jmp 0xdae0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x22e0
jmp 0xdad4
leaq -0x230(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
movl $0x12, %edx
callq 0x2280
jmp 0xd918
movl $0x0, -0x240(%rbp)
movl -0x240(%rbp), %eax
movl -0x14(%rbp), %ecx
imull -0x18(%rbp), %ecx
cmpl %ecx, %eax
jge 0xda52
movq -0x10(%rbp), %rax
movslq -0x240(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0xdd30
movb %al, -0x269(%rbp)
jmp 0xd952
movb -0x269(%rbp), %al
movb %al, -0x241(%rbp)
movq -0x10(%rbp), %rax
movslq -0x240(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0xdd50
movb %al, -0x26a(%rbp)
jmp 0xd979
movb -0x26a(%rbp), %al
movb %al, -0x242(%rbp)
movq -0x10(%rbp), %rax
movslq -0x240(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0xdd70
movb %al, -0x26b(%rbp)
jmp 0xd9a0
movb -0x26b(%rbp), %al
movb %al, -0x243(%rbp)
movq -0x10(%rbp), %rax
movslq -0x240(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0xdd90
movb %al, -0x26c(%rbp)
jmp 0xd9c7
movb -0x26c(%rbp), %al
movb %al, -0x244(%rbp)
movzbl -0x244(%rbp), %esi
leaq -0x230(%rbp), %rdi
callq 0x22f0
movq %rax, -0x278(%rbp)
jmp 0xd9ef
movq -0x278(%rbp), %rdi
movzbl -0x243(%rbp), %esi
callq 0x22f0
movq %rax, -0x280(%rbp)
jmp 0xda0b
movq -0x280(%rbp), %rdi
movzbl -0x242(%rbp), %esi
callq 0x22f0
movq %rax, -0x288(%rbp)
jmp 0xda27
movq -0x288(%rbp), %rdi
movzbl -0x241(%rbp), %esi
callq 0x22f0
jmp 0xda3c
jmp 0xda3e
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0xd922
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x289(%rbp)
jmp 0xda72
movb -0x289(%rbp), %al
testb $0x1, %al
jne 0xda7e
jmp 0xdabf
movl $0x18, %edi
callq 0x20f0
movq %rax, %rdi
movq %rdi, -0x298(%rbp)
leaq 0x39a5(%rip), %rdx      # 0x1143e
movl $0x1, %esi
callq 0xf0b0
movq -0x298(%rbp), %rdi
leaq 0xa2df(%rip), %rsi      # 0x17d90
leaq 0x1698(%rip), %rdx      # 0xf150
callq 0x2340
jmp 0xdae0
leaq -0x230(%rbp), %rdi
callq 0x22e0
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x2370