// File: ticky[P]ps2iconsys/gbLib/src/gbImageLoader_BMP.cpp
// Function: GhulbusUtil::gbImageType_BMP_T::ReadFile(std::basic_ifstream<char, std::char_traits<char>>&, int*, int*, int*, unsigned char**, unsigned int**)
void gbImageType_BMP_T::ReadFile(std::ifstream& file, int* width, int* height, int* bpp, 
		                           unsigned char** pp_data, unsigned int** pp_palette) 
	{
		BITMAPFILEHEADER fheader;
		BITMAPINFOHEADER iheader;
		file.seekg(m_file_offset, ::std::ios::beg);
		file.read( reinterpret_cast<char*>(&fheader), sizeof(BITMAPFILEHEADER) );
		file.read( reinterpret_cast<char*>(&iheader), sizeof(BITMAPINFOHEADER));

		if(iheader.biClrUsed || (iheader.biHeight < 0)) { 
			throw( Ghulbus::gbException( Ghulbus::gbException::GB_NOTIMPLEMENTED ) );
		}

		unsigned int* palette = NULL;
		unsigned char* data = new unsigned char[iheader.biWidth*iheader.biHeight*((iheader.biBitCount >= 8)?(iheader.biBitCount>>3):1)];

		try {
			switch(iheader.biBitCount)
			{
			case 1:
				ReadData1Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			case 4:
				ReadData4Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			case 8:
				ReadData8Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			case 16:
				ReadData16Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			case 24:
				ReadData24Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			case 32:
				ReadData32Bit(file, iheader, fheader, data, &palette, m_file_offset);
				break;
			default:
				throw( Ghulbus::gbException( Ghulbus::gbException::GB_FAILED,
											 "Illegal bit depth in BMP image file") );
			}
		} catch(Ghulbus::gbException) {
			if(data)    { delete[] data; }
			if(palette) { delete[] palette; }
			throw;
		}

		FlipImageVertical(iheader, data);

		*width      = iheader.biWidth;
		*height     = iheader.biHeight;
		*bpp        = iheader.biBitCount;
		*pp_data    = data;
		*pp_palette = palette;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
movl 0x8(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2290
movq -0x10(%rbp), %rdi
leaq -0x3e(%rbp), %rsi
movl $0xe, %edx
callq 0x2260
movq -0x10(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movl $0x28, %edx
callq 0x2260
cmpl $0x0, -0x48(%rbp)
jne 0xe492
cmpl $0x0, -0x60(%rbp)
jge 0xe4ca
movl $0x18, %edi
callq 0x20f0
movq %rax, %rdi
movq %rdi, -0xb0(%rbp)
movl $0x5, %esi
callq 0xf060
movq -0xb0(%rbp), %rdi
leaq 0x98d2(%rip), %rsi      # 0x17d90
leaq 0xc8b(%rip), %rdx       # 0xf150
callq 0x2340
movq $0x0, -0x70(%rbp)
movl -0x64(%rbp), %eax
imull -0x60(%rbp), %eax
movl %eax, -0xb4(%rbp)
movzwl -0x5a(%rbp), %eax
cmpl $0x8, %eax
jl 0xe4f7
movzwl -0x5a(%rbp), %eax
sarl $0x3, %eax
movl %eax, -0xb8(%rbp)
jmp 0xe504
movl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0xe504
movl -0xb4(%rbp), %eax
movl -0xb8(%rbp), %ecx
imull %ecx, %eax
movslq %eax, %rdi
callq 0x2040
movq %rax, -0x78(%rbp)
movzwl -0x5a(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x1f, %eax
ja 0xe6c4
movq -0xc0(%rbp), %rax
leaq 0x2f63(%rip), %rcx      # 0x114a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xe7c0
jmp 0xe574
jmp 0xe708
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xe7a3
movq -0x80(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x2240
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0xf100
movq -0xc8(%rbp), %rdi
callq 0x20c0
cmpq $0x0, -0x78(%rbp)
je 0xe70a
movq -0x78(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0xe5ee
movq -0xd0(%rbp), %rdi
callq 0x2270
jmp 0xe70a
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xea00
jmp 0xe619
jmp 0xe708
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xeb40
jmp 0xe644
jmp 0xe708
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xec10
jmp 0xe66f
jmp 0xe708
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xec70
jmp 0xe69a
jmp 0xe708
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl 0x8(%rax), %r9d
leaq -0x68(%rbp), %rsi
leaq -0x3e(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0xed20
jmp 0xe6c2
jmp 0xe708
movl $0x18, %edi
callq 0x20f0
movq %rax, %rdi
movq %rdi, -0xd8(%rbp)
leaq 0x2e6c(%rip), %rdx      # 0x1154b
movl $0x1, %esi
callq 0xf0b0
movq -0xd8(%rbp), %rdi
leaq 0x9699(%rip), %rsi      # 0x17d90
leaq 0xa52(%rip), %rdx       # 0xf150
callq 0x2340
jmp 0xe7b4
jmp 0xe75b
cmpq $0x0, -0x70(%rbp)
je 0xe730
movq -0x70(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0xe72e
movq -0xe0(%rbp), %rdi
callq 0x2270
jmp 0xe730
callq 0x22d0
jmp 0xe7b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xf150
callq 0x2330
jmp 0xe759
jmp 0xe7a3
movq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0xeeb0
movl -0x64(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x60(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movzwl -0x5a(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x70(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0xe0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x2370
movq %rax, %rdi
callq 0x5470
nopw %cs:(%rax,%rax)