// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
andq $0x0, 0x8(%rsp)
andl $0x0, 0x10(%rsp)
movb $0x2, 0xa(%rsp)
cmpl $0xffff, %edx           # imm = 0xFFFF
jg 0x6b8c
movl %ecx, %ebx
cmpl $0x10000, %ecx          # imm = 0x10000
jge 0x6b8c
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rsi
movw %bp, 0x14(%rsp)
movw %bx, 0x16(%rsp)
movw $0x2820, 0x18(%rsp)     # imm = 0x2820
leaq 0x20(%rsp), %r15
pushq $0x14
popq %rdx
movq %r15, %rdi
callq 0x21c0
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x40(%rsp,%rax)
jne 0x6bb8
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x12
popq %rdx
callq 0x2240
imull %ebp, %ebx
xorl %r12d, %r12d
testl %ebx, %ebx
cmovlel %r12d, %ebx
leaq 0x20(%rsp), %r15
cmpq %r12, %rbx
je 0x6b62
movl (%r14,%r12,4), %ebp
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x21d0
movl %ebp, %ecx
shrl $0x8, %ecx
movsbl %cl, %esi
movq %rax, %rdi
callq 0x21d0
movl %ebp, %ecx
shrl $0x10, %ecx
movsbl %cl, %esi
movq %rax, %rdi
callq 0x21d0
shrl $0x18, %ebp
movsbl %bpl, %esi
movq %rax, %rdi
callq 0x21d0
incq %r12
jmp 0x6b19
movq 0x20(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x40(%rsp,%rax)
jne 0x6bc1
leaq 0x20(%rsp), %rdi
callq 0x2290
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x18
popq %rdi
callq 0x20e0
movq %rax, %rbx
pushq $0x3
popq %rsi
movq %rax, %rdi
callq 0x7614
leaq 0x81ef(%rip), %rsi      # 0xed98
leaq 0xaa4(%rip), %rdx       # 0x7654
movq %rbx, %rdi
callq 0x22f0
leaq 0x28a0(%rip), %rbx      # 0x945f
jmp 0x6bc8
leaq 0x28b7(%rip), %rbx      # 0x947f
pushq $0x18
popq %rdi
callq 0x20e0
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x7628
leaq 0x81b0(%rip), %rsi      # 0xed98
leaq 0xa65(%rip), %rdx       # 0x7654
movq %r14, %rdi
callq 0x22f0
jmp 0x6bf9
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2290
movq %rbx, %rdi
callq 0x2320