#include "Bitmap.cuh"

HostMatrix Bitmap::loadFromFile(const char* fileName) {
    std::ifstream fin;
    fin.open(fileName);
    if (!fin.is_open()) {
        std::cerr << "Could not open " << fileName << "." << std::endl;
        exit(EXIT_FAILURE);
    }

    unsigned int buffer = 0;

    // BMP Header
    buffer = 0;             fin.read((char*)&buffer, 2); // BM
    if (buffer != 19778) {
        std::cerr << "The image format is not supported. " <<
            "(Offse = " << fin.tellg() << ", Value = " << buffer << ")" << std::endl;
        exit(EXIT_FAILURE);
    }
    fin.seekg(12, std::ios::cur);

    // DIB Header
    buffer = 0;             fin.read((char*)&buffer, 4); // DIB Header Size
    if (buffer != 40) {
        std::cerr << "The image format is not supported. " <<
            "(Offse = " << fin.tellg() << ", Value = " << buffer << ")" << std::endl;
        exit(EXIT_FAILURE);
    }
    buffer = 0;             fin.read((char*)&buffer, 4); // Width
    unsigned int width = buffer;
    buffer = 0;             fin.read((char*)&buffer, 4); // Height
    unsigned int height = buffer;
    fin.seekg(2, std::ios::cur);
    buffer = 0;             fin.read((char*)&buffer, 2); // Number of Bits per Pixel
    if (buffer != 8 && buffer != 24) {
        std::cerr << "The image format is not supported. " <<
            "(Offse = " << fin.tellg() << ", Value = " << buffer << ")" << std::endl;
        exit(EXIT_FAILURE);
    }
    bool grayScale = (buffer == 8);
    buffer = 0;             fin.read((char*)&buffer, 4); // Compression
    if (buffer != 0) {
        std::cerr << "The image format is not supported. " <<
            "(Offse = " << fin.tellg() << ", Value = " << buffer << ")" << std::endl;
        exit(EXIT_FAILURE);
    }
    fin.seekg(20, std::ios::cur);

    // Color Palette
    if (grayScale) {
        fin.seekg(1024, std::ios::cur);
    }

    // Image Data
    HostMatrix image(height, width);
    if (grayScale) {
        unsigned int padding = (width + 3) / 4 * 4 - width;
        for (unsigned int i = 0; i < height; ++i) {
            for (unsigned int j = 0; j < width; ++j) {
                buffer = 0; fin.read((char*)&buffer, 1);
                image.setElement(i, j, convertToFloat(buffer));
            }
            fin.seekg(padding, std::ios::cur);
        }
    } else {
        unsigned int padding = (3 * width + 3) / 4 * 4 - width * 3;
        for (unsigned int i = 0; i < height; ++i) {
            for (unsigned int j = 0; j < width; ++j) {
                float value = 0.0f;
                buffer = 0; fin.read((char*)&buffer, 1);
                value += convertToFloat(buffer) * 0.11f;
                buffer = 0; fin.read((char*)&buffer, 1);
                value += convertToFloat(buffer) * 0.59f;
                buffer = 0; fin.read((char*)&buffer, 1);
                value += convertToFloat(buffer) * 0.30f;
                image.setElement(i, j, value);
            }
            fin.seekg(padding, std::ios::cur);
        }
    }

    fin.close();
    return image;
}

void Bitmap::saveToFile(const HostMatrix& image, const char* fileName) {
    std::ofstream fout;
    fout.open(fileName);
    if (!fout.is_open()) {
        std::cerr << "Could not open " << fileName << "." << std::endl;
        exit(EXIT_FAILURE);
    }

    unsigned int height = image.getHeight();
    unsigned int width = image.getWidth();
    unsigned int padding = (width + 3) / 4 * 4 - width;
    unsigned int headerSize = 14 + 40 + 1024;
    unsigned int imageSize = height * (width + padding);
    unsigned int fileSize = headerSize + imageSize;

    unsigned int buffer = 0;

    // BMP Header
    buffer = 19778;         fout.write((char*)&buffer, 2);  // BM
    buffer = fileSize;      fout.write((char*)&buffer, 4);  // File Size
    buffer = 0;             fout.write((char*)&buffer, 2);  // Reserved
    buffer = 0;             fout.write((char*)&buffer, 2);  // Reserved
    buffer = headerSize;    fout.write((char*)&buffer, 4);  // Offset

    // DIB Header
    buffer = 40;            fout.write((char*)&buffer, 4);  // DIB Header Size
    buffer = width;         fout.write((char*)&buffer, 4);  // Width
    buffer = height;        fout.write((char*)&buffer, 4);  // Height
    buffer = 1;             fout.write((char*)&buffer, 2);  // Number of Color Planes
    buffer = 8;             fout.write((char*)&buffer, 2);  // Number of Bits per Pixel
    buffer = 0;             fout.write((char*)&buffer, 4);  // Compression
    buffer = imageSize;     fout.write((char*)&buffer, 4);  // Image Data Size
    buffer = 0;             fout.write((char*)&buffer, 4);  // Horizontal Resolution
    buffer = 0;             fout.write((char*)&buffer, 4);  // Vertical Resolution
    buffer = 0;             fout.write((char*)&buffer, 4);  // Number of Colors in Color Palette
    buffer = 0;             fout.write((char*)&buffer, 4);  // Number of Important Colors

    // Color Palette
    for (unsigned int shade = 0; shade < 256; ++shade) {
        buffer = shade;     fout.write((char*)&buffer, 1);  // Red
                            fout.write((char*)&buffer, 1);  // Green
                            fout.write((char*)&buffer, 1);  // Blue
        buffer = 0;         fout.write((char*)&buffer, 1);  // Padding
    }

    // Image Data
    for (unsigned int i = 0; i < height; ++i) {
        for (unsigned int j = 0; j < width; ++j) {
            buffer = convertToByte(image.getElement(i, j));
            fout.write((char*)&buffer, 1);
        }
        buffer = 0;         fout.write((char*)&buffer, padding);  // Padding
    }

    fout.close();
}
	.file	"tmpxft_002bf8a6_00000000-6_Bitmap.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4686:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4686:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB5457:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE5457:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4709:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4709:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.text._ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv,"axG",@progbits,_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv,comdat
	.align 2
	.weak	_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv
	.type	_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv, @function
_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv:
.LFB5029:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	(%rdi), %rax
	movq	%rdi, 8(%rsp)
	call	*16(%rax)
	cmpb	$0, __libc_single_threaded(%rip)
	movq	8(%rsp), %rdi
	je	.L6
	movl	12(%rdi), %eax
	leal	-1(%rax), %edx
	movl	%edx, 12(%rdi)
	jmp	.L7
.L6:
	orl	$-1, %eax
	lock xaddl	%eax, 12(%rdi)
.L7:
	decl	%eax
	jne	.L5
	movq	(%rdi), %rax
	movq	24(%rax), %rax
	addq	$24, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	jmp	*%rax
.L5:
	.cfi_restore_state
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE5029:
	.size	_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv, .-_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv
	.section	.text._ZN10HostMatrixD2Ev,"axG",@progbits,_ZN10HostMatrixD5Ev,comdat
	.align 2
	.weak	_ZN10HostMatrixD2Ev
	.type	_ZN10HostMatrixD2Ev, @function
_ZN10HostMatrixD2Ev:
.LFB4675:
	.cfi_startproc
	endbr64
	leaq	16+_ZTV6Matrix(%rip), %rax
	movq	%rax, (%rdi)
	movq	32(%rdi), %rdi
	testq	%rdi, %rdi
	je	.L11
	movq	8(%rdi), %rcx
	leaq	8(%rdi), %rdx
	movabsq	$4294967297, %rax
	cmpq	%rax, %rcx
	jne	.L14
	xorl	%eax, %eax
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rax, 8(%rdi)
	movq	(%rdi), %rax
	movq	%rdi, 8(%rsp)
	call	*16(%rax)
	movq	8(%rsp), %rdi
	movq	(%rdi), %rax
	movq	24(%rax), %rax
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	jmp	*%rax
.L14:
	cmpb	$0, __libc_single_threaded(%rip)
	je	.L15
	movl	8(%rdi), %eax
	leal	-1(%rax), %edx
	movl	%edx, 8(%rdi)
	jmp	.L16
.L15:
	orl	$-1, %eax
	lock xaddl	%eax, (%rdx)
.L16:
	decl	%eax
	jne	.L11
	jmp	_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE24_M_release_last_use_coldEv
.L11:
	ret
	.cfi_endproc
.LFE4675:
	.size	_ZN10HostMatrixD2Ev, .-_ZN10HostMatrixD2Ev
	.weak	_ZN10HostMatrixD1Ev
	.set	_ZN10HostMatrixD1Ev,_ZN10HostMatrixD2Ev
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Could not open "
.LC1:
	.string	"."
.LC2:
	.string	"The image format is not supported. "
.LC3:
	.string	"(Offse = "
.LC4:
	.string	", Value = "
.LC5:
	.string	")"
	.text
	.align 2
	.globl	_ZN6Bitmap12loadFromFileEPKc
	.type	_ZN6Bitmap12loadFromFileEPKc, @function
_ZN6Bitmap12loadFromFileEPKc:
.LFB4673:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4673
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$584, %rsp
	.cfi_def_cfa_offset 640
	movq	%rdi, 16(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 568(%rsp)
	xorl	%eax, %eax
	leaq	48(%rsp), %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
.LEHB0:
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEEC1Ev@PLT
.LEHE0:
	movq	8(%rsp), %rdi
	movl	$8, %edx
	movq	%rbx, %rsi
.LEHB1:
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode@PLT
	leaq	168(%rsp), %rdi
	call	_ZNKSt12__basic_fileIcE7is_openEv@PLT
	testb	%al, %al
	jne	.L22
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	%rbx, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	leaq	.LC1(%rip), %rsi
	jmp	.L69
.L22:
	xorl	%ebx, %ebx
	movq	8(%rsp), %rdi
	movl	$2, %edx
	movl	%ebx, 44(%rsp)
	leaq	44(%rsp), %rbx
	movq	%rbx, %rsi
	call	_ZNSi4readEPcl@PLT
	cmpl	$19778, 44(%rsp)
	jne	.L93
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movl	$12, %esi
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
	movq	8(%rsp), %rdi
	xorl	%r11d, %r11d
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r11d, 44(%rsp)
	call	_ZNSi4readEPcl@PLT
	cmpl	$40, 44(%rsp)
	jne	.L93
	movq	8(%rsp), %rdi
	xorl	%r10d, %r10d
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r10d, 44(%rsp)
	call	_ZNSi4readEPcl@PLT
	movq	8(%rsp), %rdi
	xorl	%r9d, %r9d
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	44(%rsp), %ebp
	movl	%r9d, 44(%rsp)
	call	_ZNSi4readEPcl@PLT
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movl	$2, %esi
	movl	44(%rsp), %r14d
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
	movq	8(%rsp), %rdi
	xorl	%r8d, %r8d
	movl	$2, %edx
	movq	%rbx, %rsi
	movl	%r8d, 44(%rsp)
	call	_ZNSi4readEPcl@PLT
	movl	44(%rsp), %r12d
	movl	%r12d, %eax
	andl	$-17, %eax
	cmpl	$8, %eax
	jne	.L93
	xorl	%edi, %edi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%edi, 44(%rsp)
	movq	8(%rsp), %rdi
	call	_ZNSi4readEPcl@PLT
	cmpl	$0, 44(%rsp)
	je	.L26
.L93:
	leaq	.LC2(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	leaq	.LC3(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	8(%rsp), %rdi
	movq	%rax, %rbx
	call	_ZNSi5tellgEv@PLT
	movq	%rax, %rsi
	movq	%rbx, %rdi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	movq	%rax, %rdi
	leaq	.LC4(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	44(%rsp), %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	%rax, %rdi
	leaq	.LC5(%rip), %rsi
.L69:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1, %edi
	call	exit@PLT
.L26:
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movl	$20, %esi
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
	cmpl	$8, %r12d
	je	.L27
.L29:
	movq	16(%rsp), %rdi
	movl	%ebp, %edx
	movl	%r14d, %esi
	call	_ZN10HostMatrixC1Ejj@PLT
	jmp	.L96
.L27:
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movl	$1024, %esi
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
.LEHE1:
	jmp	.L29
.L96:
	cmpl	$8, %r12d
	jne	.L30
	leal	3(%rbp), %r12d
	xorl	%r13d, %r13d
	andl	$-4, %r12d
	subl	%ebp, %r12d
.L31:
	cmpl	%r14d, %r13d
	je	.L36
	xorl	%r15d, %r15d
.L35:
	cmpl	%ebp, %r15d
	je	.L97
	xorl	%esi, %esi
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movl	%esi, 44(%rsp)
	movq	%rbx, %rsi
.LEHB2:
	call	_ZNSi4readEPcl@PLT
	movl	44(%rsp), %eax
	movq	16(%rsp), %rdi
	movl	%r15d, %edx
	movl	%r13d, %esi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC6(%rip), %xmm0
	call	_ZN10HostMatrix10setElementEjjf@PLT
	incl	%r15d
	jmp	.L35
.L97:
	movq	8(%rsp), %rdi
	movl	%r12d, %esi
	movl	$1, %edx
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
	incl	%r13d
	jmp	.L31
.L30:
	leal	1(%rbp), %eax
	xorl	%r12d, %r12d
	leal	(%rax,%rax,2), %eax
	movl	%eax, %r15d
	subl	$3, %eax
	andl	$-4, %r15d
	subl	%eax, %r15d
.L37:
	cmpl	%r14d, %r12d
	je	.L36
	xorl	%r13d, %r13d
.L45:
	cmpl	%ebp, %r13d
	je	.L98
	movq	8(%rsp), %rdi
	xorl	%ecx, %ecx
	movl	$1, %edx
	movq	%rbx, %rsi
	movl	%ecx, 44(%rsp)
	call	_ZNSi4readEPcl@PLT
	movl	44(%rsp), %eax
	xorps	%xmm1, %xmm1
	xorl	%edx, %edx
	movq	8(%rsp), %rdi
	movl	%edx, 44(%rsp)
	movq	%rbx, %rsi
	movl	$1, %edx
	cvtsi2ssq	%rax, %xmm0
	divss	.LC6(%rip), %xmm0
	mulss	.LC7(%rip), %xmm0
	addss	%xmm1, %xmm0
	movss	%xmm0, 28(%rsp)
	call	_ZNSi4readEPcl@PLT
	movl	44(%rsp), %eax
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movq	%rbx, %rsi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC6(%rip), %xmm0
	xorl	%eax, %eax
	mulss	.LC9(%rip), %xmm0
	movl	%eax, 44(%rsp)
	addss	28(%rsp), %xmm0
	movss	%xmm0, 28(%rsp)
	call	_ZNSi4readEPcl@PLT
	movl	44(%rsp), %eax
	movq	16(%rsp), %rdi
	movl	%r13d, %edx
	movl	%r12d, %esi
	cvtsi2ssq	%rax, %xmm0
	divss	.LC6(%rip), %xmm0
	mulss	.LC10(%rip), %xmm0
	addss	28(%rsp), %xmm0
	call	_ZN10HostMatrix10setElementEjjf@PLT
	incl	%r13d
	jmp	.L45
.L98:
	movq	8(%rsp), %rdi
	movl	%r15d, %esi
	movl	$1, %edx
	call	_ZNSi5seekgElSt12_Ios_Seekdir@PLT
	incl	%r12d
	jmp	.L37
.L36:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE2:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	568(%rsp), %rax
	subq	%fs:40, %rax
	je	.L49
	jmp	.L55
.L53:
	endbr64
	movq	%rax, %rbx
.L46:
	movq	16(%rsp), %rdi
	call	_ZN10HostMatrixD1Ev
	jmp	.L47
.L52:
	endbr64
	movq	%rax, %rbx
.L47:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	568(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L55
	movq	%rbx, %rdi
.LEHB3:
	call	_Unwind_Resume@PLT
.LEHE3:
.L55:
	call	__stack_chk_fail@PLT
.L49:
	movq	16(%rsp), %rax
	addq	$584, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4673:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA4673:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4673-.LLSDACSB4673
.LLSDACSB4673:
	.uleb128 .LEHB0-.LFB4673
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB4673
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L52-.LFB4673
	.uleb128 0
	.uleb128 .LEHB2-.LFB4673
	.uleb128 .LEHE2-.LEHB2
	.uleb128 .L53-.LFB4673
	.uleb128 0
	.uleb128 .LEHB3-.LFB4673
	.uleb128 .LEHE3-.LEHB3
	.uleb128 0
	.uleb128 0
.LLSDACSE4673:
	.text
	.size	_ZN6Bitmap12loadFromFileEPKc, .-_ZN6Bitmap12loadFromFileEPKc
	.align 2
	.globl	_ZN6Bitmap10saveToFileERK10HostMatrixPKc
	.type	_ZN6Bitmap10saveToFileERK10HostMatrixPKc, @function
_ZN6Bitmap10saveToFileERK10HostMatrixPKc:
.LFB4683:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4683
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$552, %rsp
	.cfi_def_cfa_offset 608
	movq	%fs:40, %rax
	movq	%rax, 536(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movq	%rax, %rdi
	movq	%rax, (%rsp)
.LEHB4:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev@PLT
.LEHE4:
	movq	(%rsp), %rdi
	movl	$16, %edx
	movq	%rbx, %rsi
.LEHB5:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode@PLT
	leaq	136(%rsp), %rdi
	call	_ZNKSt12__basic_fileIcE7is_openEv@PLT
	testb	%al, %al
	jne	.L100
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	%rbx, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	leaq	.LC1(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1, %edi
	call	exit@PLT
.L100:
	movl	12(%r12), %r13d
	movl	8(%r12), %r14d
	leaq	20(%rsp), %rbx
	movl	$2, %edx
	movq	(%rsp), %rdi
	movq	%rbx, %rsi
	movl	$19778, 20(%rsp)
	leal	3(%r13), %ebp
	andl	$-4, %ebp
	movl	%ebp, %eax
	imull	%r14d, %ebp
	subl	%r13d, %eax
	movl	%eax, 12(%rsp)
	leal	1078(%rbp), %r15d
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r15d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	xorl	%r11d, %r11d
	movl	$2, %edx
	movq	%rbx, %rsi
	movl	%r11d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	xorl	%r10d, %r10d
	movl	$2, %edx
	movq	%rbx, %rsi
	movl	%r10d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	$1078, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	$40, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r13d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r14d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$2, %edx
	movq	%rbx, %rsi
	movl	$1, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$2, %edx
	movq	%rbx, %rsi
	movl	$8, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	xorl	%r9d, %r9d
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r9d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%ebp, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	xorl	%r8d, %r8d
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%r8d, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	xorl	%edi, %edi
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%edi, 20(%rsp)
	movq	(%rsp), %rdi
	call	_ZNSo5writeEPKcl@PLT
	xorl	%esi, %esi
	movq	(%rsp), %rdi
	movl	$4, %edx
	movl	%esi, 20(%rsp)
	movq	%rbx, %rsi
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	xorl	%ecx, %ecx
	movl	$4, %edx
	movq	%rbx, %rsi
	movl	%ecx, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	xorl	%ebp, %ebp
.L101:
	movq	(%rsp), %rdi
	movl	$1, %edx
	movq	%rbx, %rsi
	movl	%ebp, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$1, %edx
	movq	%rbx, %rsi
	call	_ZNSo5writeEPKcl@PLT
	movq	(%rsp), %rdi
	movl	$1, %edx
	movq	%rbx, %rsi
	call	_ZNSo5writeEPKcl@PLT
	xorl	%edx, %edx
	movq	(%rsp), %rdi
	movq	%rbx, %rsi
	movl	%edx, 20(%rsp)
	movl	$1, %edx
	call	_ZNSo5writeEPKcl@PLT
	incl	%ebp
	cmpl	$256, %ebp
	jne	.L101
	xorl	%ebp, %ebp
.L102:
	cmpl	%r14d, %ebp
	je	.L116
	xorl	%r15d, %r15d
.L105:
	cmpl	%r13d, %r15d
	je	.L117
	movq	(%r12), %rax
	movl	%r15d, %edx
	movl	%ebp, %esi
	movq	%r12, %rdi
	call	*(%rax)
	xorps	%xmm1, %xmm1
	comiss	%xmm0, %xmm1
	ja	.L109
	movss	.LC11(%rip), %xmm2
	minss	%xmm0, %xmm2
	movaps	%xmm2, %xmm0
	jmp	.L103
.L109:
	xorps	%xmm0, %xmm0
.L103:
	mulss	.LC6(%rip), %xmm0
	call	roundf@PLT
	movq	(%rsp), %rdi
	movl	$1, %edx
	movq	%rbx, %rsi
	cvttss2siq	%xmm0, %rax
	movl	%eax, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	incl	%r15d
	jmp	.L105
.L117:
	movl	12(%rsp), %edx
	movq	(%rsp), %rdi
	xorl	%eax, %eax
	movq	%rbx, %rsi
	movl	%eax, 20(%rsp)
	call	_ZNSo5writeEPKcl@PLT
	incl	%ebp
	jmp	.L102
.L116:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE5:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	536(%rsp), %rax
	subq	%fs:40, %rax
	je	.L108
	jmp	.L114
.L112:
	endbr64
	movq	%rax, %rbx
.L106:
	movq	(%rsp), %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	536(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L114
	movq	%rbx, %rdi
.LEHB6:
	call	_Unwind_Resume@PLT
.LEHE6:
.L114:
	call	__stack_chk_fail@PLT
.L108:
	addq	$552, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4683:
	.section	.gcc_except_table
.LLSDA4683:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4683-.LLSDACSB4683
.LLSDACSB4683:
	.uleb128 .LEHB4-.LFB4683
	.uleb128 .LEHE4-.LEHB4
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB5-.LFB4683
	.uleb128 .LEHE5-.LEHB5
	.uleb128 .L112-.LFB4683
	.uleb128 0
	.uleb128 .LEHB6-.LFB4683
	.uleb128 .LEHE6-.LEHB6
	.uleb128 0
	.uleb128 0
.LLSDACSE4683:
	.text
	.size	_ZN6Bitmap10saveToFileERK10HostMatrixPKc, .-_ZN6Bitmap10saveToFileERK10HostMatrixPKc
	.weak	_ZTS6Matrix
	.section	.rodata._ZTS6Matrix,"aG",@progbits,_ZTS6Matrix,comdat
	.align 8
	.type	_ZTS6Matrix, @object
	.size	_ZTS6Matrix, 8
_ZTS6Matrix:
	.string	"6Matrix"
	.weak	_ZTI6Matrix
	.section	.data.rel.ro._ZTI6Matrix,"awG",@progbits,_ZTI6Matrix,comdat
	.align 8
	.type	_ZTI6Matrix, @object
	.size	_ZTI6Matrix, 16
_ZTI6Matrix:
	.quad	_ZTVN10__cxxabiv117__class_type_infoE+16
	.quad	_ZTS6Matrix
	.weak	_ZTV6Matrix
	.section	.data.rel.ro._ZTV6Matrix,"awG",@progbits,_ZTV6Matrix,comdat
	.align 8
	.type	_ZTV6Matrix, @object
	.size	_ZTV6Matrix, 32
_ZTV6Matrix:
	.quad	0
	.quad	_ZTI6Matrix
	.quad	__cxa_pure_virtual
	.quad	__cxa_pure_virtual
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC6:
	.long	1132396544
	.align 4
.LC7:
	.long	1038174126
	.align 4
.LC9:
	.long	1058474557
	.align 4
.LC10:
	.long	1050253722
	.align 4
.LC11:
	.long	1065353216
	.weak	__cxa_pure_virtual
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#ifndef _BITMAP_CUH_
#define _BITMAP_CUH_

#include "Matrix.cuh"

class Bitmap {
public:
    static HostMatrix loadFromFile(const char* fileName);
    static void saveToFile(const HostMatrix& image, const char* fileName);

private:
    static float convertToFloat(unsigned int value);
    static unsigned int convertToByte(float value);
};

inline float Bitmap::convertToFloat(unsigned int value) {
    return (float)value / 255.0f;
}

inline unsigned int Bitmap::convertToByte(float value) {
    if (value < 0.0f) value = 0.0f;
    if (value > 1.0f) value = 1.0f;
    return (unsigned int)roundf(value * 255.0f);
}

#endif
	.text
	.file	"Bitmap.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
