// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movb $0x2, 0x12(%rsp)
cmpl $0xffff, %edx           # imm = 0xFFFF
jg 0x785d
movl %ecx, %ebx
cmpl $0x10000, %ecx          # imm = 0x10000
jge 0x785d
movl %edx, %r14d
movq %rsi, %rbp
movq %rdi, %rsi
movw %r14w, 0x1c(%rsp)
movw %bx, 0x1e(%rsp)
movw $0x2820, 0x20(%rsp)     # imm = 0x2820
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movl $0x14, %edx
callq 0x21e0
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x48(%rsp,%rax)
jne 0x788d
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x12, %edx
callq 0x2260
imull %r14d, %ebx
testl %ebx, %ebx
jle 0x7809
movl %ebx, %r12d
xorl %r13d, %r13d
leaq 0x28(%rsp), %r14
leaq 0xf(%rsp), %r15
movl (%rbp,%r13,4), %ebx
movb %bl, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x21f0
movb %bh, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
shrl $0x18, %ebx
movb %bl, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
incq %r13
cmpq %r13, %r12
jne 0x77a4
movq 0x28(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x48(%rsp,%rax)
jne 0x7896
movq 0x77a8(%rip), %rax      # 0xefc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x30(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x28(%rsp,%rcx)
callq 0x2150
leaq 0x120(%rsp), %rdi
callq 0x2080
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x18, %edi
callq 0x20e0
movq %rax, %rbx
movq %rax, %rdi
movl $0x3, %esi
callq 0x831a
leaq 0x7512(%rip), %rsi      # 0xed90
leaq 0xad7(%rip), %rdx       # 0x835c
movq %rbx, %rdi
callq 0x2320
leaq 0x2bcb(%rip), %rbx      # 0xa45f
jmp 0x789d
leaq 0x2be2(%rip), %rbx      # 0xa47f
movl $0x18, %edi
callq 0x20e0
movq %rax, %r14
movq %rax, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x8330
leaq 0x74cf(%rip), %rsi      # 0xed90
leaq 0xa94(%rip), %rdx       # 0x835c
movq %r14, %rdi
callq 0x2320
jmp 0x78d2
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x22b0
movq %rbx, %rdi
callq 0x2350
nop